Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- CodeGenTarget.h - Target Class Wrapper -------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | fd6c2f0 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines wrappers for the Target class and related global |
| 11 | // functionality. This makes it easier to access the data and provides a single |
| 12 | // place that needs to check it for validity. All of these classes throw |
| 13 | // exceptions on error conditions. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #ifndef CODEGEN_TARGET_H |
| 18 | #define CODEGEN_TARGET_H |
| 19 | |
| 20 | #include "CodeGenRegisters.h" |
| 21 | #include "CodeGenInstruction.h" |
| 22 | #include <iosfwd> |
| 23 | #include <map> |
| 24 | |
| 25 | namespace llvm { |
| 26 | |
| 27 | class Record; |
| 28 | class RecordKeeper; |
| 29 | struct CodeGenRegister; |
| 30 | class CodeGenTarget; |
| 31 | |
| 32 | // SelectionDAG node properties. |
Chris Lattner | ef8d608 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 33 | enum SDNP { |
| 34 | SDNPCommutative, |
| 35 | SDNPAssociative, |
| 36 | SDNPHasChain, |
| 37 | SDNPOutFlag, |
| 38 | SDNPInFlag, |
| 39 | SDNPOptInFlag, |
| 40 | SDNPMayStore |
| 41 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 42 | |
| 43 | /// getValueType - Return the MVT::ValueType that the specified TableGen record |
| 44 | /// corresponds to. |
| 45 | MVT::ValueType getValueType(Record *Rec); |
| 46 | |
| 47 | std::string getName(MVT::ValueType T); |
| 48 | std::string getEnumName(MVT::ValueType T); |
| 49 | |
Chris Lattner | 4ca8ff0 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 50 | /// getQualifiedName - Return the name of the specified record, with a |
| 51 | /// namespace qualifier if the record contains one. |
| 52 | std::string getQualifiedName(const Record *R); |
| 53 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | /// CodeGenTarget - This class corresponds to the Target class in the .td files. |
| 55 | /// |
| 56 | class CodeGenTarget { |
| 57 | Record *TargetRec; |
| 58 | |
| 59 | mutable std::map<std::string, CodeGenInstruction> Instructions; |
| 60 | mutable std::vector<CodeGenRegister> Registers; |
| 61 | mutable std::vector<CodeGenRegisterClass> RegisterClasses; |
| 62 | mutable std::vector<MVT::ValueType> LegalValueTypes; |
| 63 | void ReadRegisters() const; |
| 64 | void ReadRegisterClasses() const; |
| 65 | void ReadInstructions() const; |
| 66 | void ReadLegalValueTypes() const; |
| 67 | public: |
| 68 | CodeGenTarget(); |
| 69 | |
| 70 | Record *getTargetRecord() const { return TargetRec; } |
| 71 | const std::string &getName() const; |
| 72 | |
| 73 | /// getInstructionSet - Return the InstructionSet object. |
| 74 | /// |
| 75 | Record *getInstructionSet() const; |
| 76 | |
| 77 | /// getAsmWriter - Return the AssemblyWriter definition for this target. |
| 78 | /// |
| 79 | Record *getAsmWriter() const; |
| 80 | |
| 81 | const std::vector<CodeGenRegister> &getRegisters() const { |
| 82 | if (Registers.empty()) ReadRegisters(); |
| 83 | return Registers; |
| 84 | } |
| 85 | |
| 86 | const std::vector<CodeGenRegisterClass> &getRegisterClasses() const { |
| 87 | if (RegisterClasses.empty()) ReadRegisterClasses(); |
| 88 | return RegisterClasses; |
| 89 | } |
| 90 | |
| 91 | const CodeGenRegisterClass &getRegisterClass(Record *R) const { |
| 92 | const std::vector<CodeGenRegisterClass> &RC = getRegisterClasses(); |
| 93 | for (unsigned i = 0, e = RC.size(); i != e; ++i) |
| 94 | if (RC[i].TheDef == R) |
| 95 | return RC[i]; |
| 96 | assert(0 && "Didn't find the register class"); |
| 97 | abort(); |
| 98 | } |
| 99 | |
| 100 | /// getRegisterClassForRegister - Find the register class that contains the |
| 101 | /// specified physical register. If there register exists in multiple |
| 102 | /// register classes or is not in a register class, return null. |
| 103 | const CodeGenRegisterClass *getRegisterClassForRegister(Record *R) const { |
| 104 | const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses(); |
| 105 | const CodeGenRegisterClass *FoundRC = 0; |
| 106 | for (unsigned i = 0, e = RCs.size(); i != e; ++i) { |
| 107 | const CodeGenRegisterClass &RC = RegisterClasses[i]; |
| 108 | for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) { |
| 109 | if (R == RC.Elements[ei]) { |
| 110 | if (FoundRC) return 0; // In multiple RC's |
| 111 | FoundRC = &RC; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | return FoundRC; |
| 117 | } |
| 118 | |
| 119 | /// getRegisterVTs - Find the union of all possible ValueTypes for the |
| 120 | /// specified physical register. |
| 121 | std::vector<unsigned char> getRegisterVTs(Record *R) const; |
| 122 | |
| 123 | const std::vector<MVT::ValueType> &getLegalValueTypes() const { |
| 124 | if (LegalValueTypes.empty()) ReadLegalValueTypes(); |
| 125 | return LegalValueTypes; |
| 126 | } |
| 127 | |
| 128 | /// isLegalValueType - Return true if the specified value type is natively |
| 129 | /// supported by the target (i.e. there are registers that directly hold it). |
| 130 | bool isLegalValueType(MVT::ValueType VT) const { |
| 131 | const std::vector<MVT::ValueType> &LegalVTs = getLegalValueTypes(); |
| 132 | for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i) |
| 133 | if (LegalVTs[i] == VT) return true; |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | /// getInstructions - Return all of the instructions defined for this target. |
| 138 | /// |
| 139 | const std::map<std::string, CodeGenInstruction> &getInstructions() const { |
| 140 | if (Instructions.empty()) ReadInstructions(); |
| 141 | return Instructions; |
| 142 | } |
| 143 | |
| 144 | CodeGenInstruction &getInstruction(const std::string &Name) const { |
| 145 | const std::map<std::string, CodeGenInstruction> &Insts = getInstructions(); |
| 146 | assert(Insts.count(Name) && "Not an instruction!"); |
| 147 | return const_cast<CodeGenInstruction&>(Insts.find(Name)->second); |
| 148 | } |
| 149 | |
| 150 | typedef std::map<std::string, |
| 151 | CodeGenInstruction>::const_iterator inst_iterator; |
| 152 | inst_iterator inst_begin() const { return getInstructions().begin(); } |
| 153 | inst_iterator inst_end() const { return Instructions.end(); } |
| 154 | |
| 155 | /// getInstructionsByEnumValue - Return all of the instructions defined by the |
| 156 | /// target, ordered by their enum value. |
| 157 | void getInstructionsByEnumValue(std::vector<const CodeGenInstruction*> |
| 158 | &NumberedInstructions); |
| 159 | |
| 160 | |
| 161 | /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]? |
| 162 | /// |
| 163 | bool isLittleEndianEncoding() const; |
| 164 | }; |
| 165 | |
| 166 | /// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern |
| 167 | /// tablegen class in TargetSelectionDAG.td |
| 168 | class ComplexPattern { |
| 169 | MVT::ValueType Ty; |
| 170 | unsigned NumOperands; |
| 171 | std::string SelectFunc; |
| 172 | std::vector<Record*> RootNodes; |
| 173 | unsigned Properties; |
| 174 | public: |
| 175 | ComplexPattern() : NumOperands(0) {}; |
| 176 | ComplexPattern(Record *R); |
| 177 | |
| 178 | MVT::ValueType getValueType() const { return Ty; } |
| 179 | unsigned getNumOperands() const { return NumOperands; } |
| 180 | const std::string &getSelectFunc() const { return SelectFunc; } |
| 181 | const std::vector<Record*> &getRootNodes() const { |
| 182 | return RootNodes; |
| 183 | } |
| 184 | bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); } |
| 185 | |
| 186 | }; |
| 187 | |
| 188 | } // End llvm namespace |
| 189 | |
| 190 | #endif |