1 //===- CodeGenTarget.h - Target Class Wrapper -------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines wrappers for the Target class and related global 10 // functionality. This makes it easier to access the data and provides a single 11 // place that needs to check it for validity. All of these classes abort 12 // on error conditions. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_UTILS_TABLEGEN_COMMON_CODEGENTARGET_H 17 #define LLVM_UTILS_TABLEGEN_COMMON_CODEGENTARGET_H 18 19 #include "Basic/CodeGenIntrinsics.h" 20 #include "Basic/SDNodeProperties.h" 21 #include "CodeGenHwModes.h" 22 #include "CodeGenInstruction.h" 23 #include "InfoByHwMode.h" 24 #include "llvm/ADT/ArrayRef.h" 25 #include "llvm/ADT/DenseMap.h" 26 #include "llvm/ADT/SmallVector.h" 27 #include "llvm/ADT/StringRef.h" 28 #include "llvm/CodeGenTypes/MachineValueType.h" 29 #include <cassert> 30 #include <memory> 31 #include <optional> 32 #include <string> 33 #include <vector> 34 35 namespace llvm { 36 37 class RecordKeeper; 38 class Record; 39 class CodeGenRegBank; 40 class CodeGenRegister; 41 class CodeGenRegisterClass; 42 class CodeGenSchedModels; 43 class CodeGenSubRegIndex; 44 45 /// getValueType - Return the MVT::SimpleValueType that the specified TableGen 46 /// record corresponds to. 47 MVT::SimpleValueType getValueType(const Record *Rec); 48 49 StringRef getEnumName(MVT::SimpleValueType T); 50 51 /// getQualifiedName - Return the name of the specified record, with a 52 /// namespace qualifier if the record contains one. 53 std::string getQualifiedName(const Record *R); 54 55 /// CodeGenTarget - This class corresponds to the Target class in the .td files. 56 /// 57 class CodeGenTarget { 58 const RecordKeeper &Records; 59 const Record *TargetRec; 60 61 mutable DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>> 62 Instructions; 63 mutable std::unique_ptr<CodeGenRegBank> RegBank; 64 mutable ArrayRef<const Record *> RegAltNameIndices; 65 mutable SmallVector<ValueTypeByHwMode, 8> LegalValueTypes; 66 CodeGenHwModes CGH; 67 ArrayRef<const Record *> MacroFusions; 68 mutable bool HasVariableLengthEncodings = false; 69 70 void ReadInstructions() const; 71 void ReadLegalValueTypes() const; 72 73 mutable std::unique_ptr<CodeGenSchedModels> SchedModels; 74 75 mutable StringRef InstNamespace; 76 mutable std::vector<const CodeGenInstruction *> InstrsByEnum; 77 mutable CodeGenIntrinsicMap Intrinsics; 78 79 mutable unsigned NumPseudoInstructions = 0; 80 81 public: 82 CodeGenTarget(const RecordKeeper &Records); 83 ~CodeGenTarget(); 84 85 const Record *getTargetRecord() const { return TargetRec; } 86 StringRef getName() const; 87 88 /// getInstNamespace - Return the target-specific instruction namespace. 89 /// 90 StringRef getInstNamespace() const; 91 92 /// getRegNamespace - Return the target-specific register namespace. 93 StringRef getRegNamespace() const; 94 95 /// getInstructionSet - Return the InstructionSet object. 96 /// 97 const Record *getInstructionSet() const; 98 99 /// getAllowRegisterRenaming - Return the AllowRegisterRenaming flag value for 100 /// this target. 101 /// 102 bool getAllowRegisterRenaming() const; 103 104 /// getAsmParser - Return the AssemblyParser definition for this target. 105 /// 106 const Record *getAsmParser() const; 107 108 /// getAsmParserVariant - Return the AssemblyParserVariant definition for 109 /// this target. 110 /// 111 const Record *getAsmParserVariant(unsigned i) const; 112 113 /// getAsmParserVariantCount - Return the AssemblyParserVariant definition 114 /// available for this target. 115 /// 116 unsigned getAsmParserVariantCount() const; 117 118 /// getAsmWriter - Return the AssemblyWriter definition for this target. 119 /// 120 const Record *getAsmWriter() const; 121 122 /// getRegBank - Return the register bank description. 123 CodeGenRegBank &getRegBank() const; 124 125 /// Return the largest register class on \p RegBank which supports \p Ty and 126 /// covers \p SubIdx if it exists. 127 const CodeGenRegisterClass * 128 getSuperRegForSubReg(const ValueTypeByHwMode &Ty, CodeGenRegBank &RegBank, 129 const CodeGenSubRegIndex *SubIdx, 130 bool MustBeAllocatable = false) const; 131 132 /// getRegisterByName - If there is a register with the specific AsmName, 133 /// return it. 134 const CodeGenRegister *getRegisterByName(StringRef Name) const; 135 136 ArrayRef<const Record *> getRegAltNameIndices() const { 137 if (RegAltNameIndices.empty()) 138 RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex"); 139 return RegAltNameIndices; 140 } 141 142 const CodeGenRegisterClass &getRegisterClass(const Record *R) const; 143 144 /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the 145 /// specified physical register. 146 std::vector<ValueTypeByHwMode> getRegisterVTs(const Record *R) const; 147 148 ArrayRef<ValueTypeByHwMode> getLegalValueTypes() const { 149 if (LegalValueTypes.empty()) 150 ReadLegalValueTypes(); 151 return LegalValueTypes; 152 } 153 154 CodeGenSchedModels &getSchedModels() const; 155 156 const CodeGenHwModes &getHwModes() const { return CGH; } 157 158 bool hasMacroFusion() const { return !MacroFusions.empty(); } 159 160 ArrayRef<const Record *> getMacroFusions() const { return MacroFusions; } 161 162 private: 163 DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>> & 164 getInstructions() const { 165 if (Instructions.empty()) 166 ReadInstructions(); 167 return Instructions; 168 } 169 170 public: 171 CodeGenInstruction &getInstruction(const Record *InstRec) const { 172 if (Instructions.empty()) 173 ReadInstructions(); 174 auto I = Instructions.find(InstRec); 175 assert(I != Instructions.end() && "Not an instruction"); 176 return *I->second; 177 } 178 179 /// Returns the number of predefined instructions. 180 static unsigned getNumFixedInstructions(); 181 182 /// Returns the number of pseudo instructions. 183 unsigned getNumPseudoInstructions() const { 184 if (InstrsByEnum.empty()) 185 ComputeInstrsByEnum(); 186 return NumPseudoInstructions; 187 } 188 189 /// Return all of the instructions defined by the target, ordered by their 190 /// enum value. 191 /// The following order of instructions is also guaranteed: 192 /// - fixed / generic instructions as declared in TargetOpcodes.def, in order; 193 /// - pseudo instructions in lexicographical order sorted by name; 194 /// - other instructions in lexicographical order sorted by name. 195 ArrayRef<const CodeGenInstruction *> getInstructionsByEnumValue() const { 196 if (InstrsByEnum.empty()) 197 ComputeInstrsByEnum(); 198 return InstrsByEnum; 199 } 200 201 /// Return the integer enum value corresponding to this instruction record. 202 unsigned getInstrIntValue(const Record *R) const { 203 if (InstrsByEnum.empty()) 204 ComputeInstrsByEnum(); 205 return getInstruction(R).EnumVal; 206 } 207 208 typedef ArrayRef<const CodeGenInstruction *>::const_iterator inst_iterator; 209 inst_iterator inst_begin() const { 210 return getInstructionsByEnumValue().begin(); 211 } 212 inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); } 213 214 /// Return whether instructions have variable length encodings on this target. 215 bool hasVariableLengthEncodings() const { return HasVariableLengthEncodings; } 216 217 /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]? 218 /// 219 bool isLittleEndianEncoding() const; 220 221 /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit 222 /// encodings, reverse the bit order of all instructions. 223 void reverseBitsForLittleEndianEncoding(); 224 225 /// guessInstructionProperties - should we just guess unset instruction 226 /// properties? 227 bool guessInstructionProperties() const; 228 229 const CodeGenIntrinsic &getIntrinsic(const Record *Def) const { 230 return Intrinsics[Def]; 231 } 232 233 private: 234 void ComputeInstrsByEnum() const; 235 }; 236 237 /// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern 238 /// tablegen class in TargetSelectionDAG.td 239 class ComplexPattern { 240 const Record *Ty; 241 unsigned NumOperands; 242 std::string SelectFunc; 243 std::vector<const Record *> RootNodes; 244 unsigned Properties; // Node properties 245 unsigned Complexity; 246 bool WantsRoot; 247 bool WantsParent; 248 249 public: 250 ComplexPattern(const Record *R); 251 252 const Record *getValueType() const { return Ty; } 253 unsigned getNumOperands() const { return NumOperands; } 254 const std::string &getSelectFunc() const { return SelectFunc; } 255 const ArrayRef<const Record *> getRootNodes() const { return RootNodes; } 256 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); } 257 unsigned getComplexity() const { return Complexity; } 258 bool wantsRoot() const { return WantsRoot; } 259 bool wantsParent() const { return WantsParent; } 260 }; 261 262 } // namespace llvm 263 264 #endif // LLVM_UTILS_TABLEGEN_COMMON_CODEGENTARGET_H 265