Chris Lattner | 803a5f6 | 2004-08-01 04:04:35 +0000 | [diff] [blame] | 1 | //===- CodeGenTarget.h - Target Class Wrapper -------------------*- C++ -*-===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 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. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 803a5f6 | 2004-08-01 04:04:35 +0000 | [diff] [blame] | 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. |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 803a5f6 | 2004-08-01 04:04:35 +0000 | [diff] [blame] | 17 | #ifndef CODEGEN_TARGET_H |
| 18 | #define CODEGEN_TARGET_H |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 2669311 | 2004-08-16 01:10:21 +0000 | [diff] [blame] | 20 | #include "CodeGenRegisters.h" |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 21 | #include "CodeGenInstruction.h" |
Jakob Stoklund Olesen | 09bc029 | 2010-05-24 21:46:58 +0000 | [diff] [blame^] | 22 | #include "Record.h" |
Chris Lattner | e14d2e2 | 2010-03-19 01:07:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 8a17870 | 2009-04-13 15:24:11 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
| 26 | namespace llvm { |
| 27 | |
Chris Lattner | 1fca5ff | 2004-10-27 16:14:51 +0000 | [diff] [blame] | 28 | struct CodeGenRegister; |
Chris Lattner | 8850a1b | 2006-03-27 22:48:18 +0000 | [diff] [blame] | 29 | class CodeGenTarget; |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 30 | |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 31 | // SelectionDAG node properties. |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 32 | // SDNPMemOperand: indicates that a node touches memory and therefore must |
| 33 | // have an associated memory operand that describes the access. |
Chris Lattner | c8478d8 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 34 | enum SDNP { |
| 35 | SDNPCommutative, |
| 36 | SDNPAssociative, |
| 37 | SDNPHasChain, |
| 38 | SDNPOutFlag, |
| 39 | SDNPInFlag, |
| 40 | SDNPOptInFlag, |
Chris Lattner | 710e995 | 2008-01-10 04:38:57 +0000 | [diff] [blame] | 41 | SDNPMayLoad, |
Chris Lattner | bc0b9f7 | 2008-01-10 05:39:30 +0000 | [diff] [blame] | 42 | SDNPMayStore, |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 43 | SDNPSideEffect, |
Chris Lattner | e8cabf3 | 2010-03-19 05:07:09 +0000 | [diff] [blame] | 44 | SDNPMemOperand, |
| 45 | SDNPVariadic |
Chris Lattner | c8478d8 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 46 | }; |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 47 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 48 | /// getValueType - Return the MVT::SimpleValueType that the specified TableGen |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 49 | /// record corresponds to. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 50 | MVT::SimpleValueType getValueType(Record *Rec); |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 51 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 52 | std::string getName(MVT::SimpleValueType T); |
| 53 | std::string getEnumName(MVT::SimpleValueType T); |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 55 | /// getQualifiedName - Return the name of the specified record, with a |
| 56 | /// namespace qualifier if the record contains one. |
| 57 | std::string getQualifiedName(const Record *R); |
| 58 | |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 59 | /// CodeGenTarget - This class corresponds to the Target class in the .td files. |
| 60 | /// |
| 61 | class CodeGenTarget { |
| 62 | Record *TargetRec; |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 63 | |
Chris Lattner | e14d2e2 | 2010-03-19 01:07:44 +0000 | [diff] [blame] | 64 | mutable DenseMap<const Record*, CodeGenInstruction*> Instructions; |
Chris Lattner | 2669311 | 2004-08-16 01:10:21 +0000 | [diff] [blame] | 65 | mutable std::vector<CodeGenRegister> Registers; |
Jakob Stoklund Olesen | 09bc029 | 2010-05-24 21:46:58 +0000 | [diff] [blame^] | 66 | mutable std::vector<Record*> SubRegIndices; |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 67 | mutable std::vector<CodeGenRegisterClass> RegisterClasses; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 68 | mutable std::vector<MVT::SimpleValueType> LegalValueTypes; |
Chris Lattner | 2669311 | 2004-08-16 01:10:21 +0000 | [diff] [blame] | 69 | void ReadRegisters() const; |
Jakob Stoklund Olesen | 09bc029 | 2010-05-24 21:46:58 +0000 | [diff] [blame^] | 70 | void ReadSubRegIndices() const; |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 71 | void ReadRegisterClasses() const; |
| 72 | void ReadInstructions() const; |
Chris Lattner | e9f4ba8 | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 73 | void ReadLegalValueTypes() const; |
Chris Lattner | f650278 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 6a91b18 | 2010-03-19 01:00:55 +0000 | [diff] [blame] | 75 | mutable std::vector<const CodeGenInstruction*> InstrsByEnum; |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 76 | public: |
| 77 | CodeGenTarget(); |
| 78 | |
| 79 | Record *getTargetRecord() const { return TargetRec; } |
| 80 | const std::string &getName() const; |
| 81 | |
Dan Gohman | 1e0ee4b | 2008-08-20 21:45:57 +0000 | [diff] [blame] | 82 | /// getInstNamespace - Return the target-specific instruction namespace. |
| 83 | /// |
| 84 | std::string getInstNamespace() const; |
| 85 | |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 86 | /// getInstructionSet - Return the InstructionSet object. |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 87 | /// |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 88 | Record *getInstructionSet() const; |
| 89 | |
Daniel Dunbar | 0e2771f | 2009-07-29 00:02:19 +0000 | [diff] [blame] | 90 | /// getAsmParser - Return the AssemblyParser definition for this target. |
| 91 | /// |
| 92 | Record *getAsmParser() const; |
| 93 | |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 94 | /// getAsmWriter - Return the AssemblyWriter definition for this target. |
| 95 | /// |
| 96 | Record *getAsmWriter() const; |
| 97 | |
Chris Lattner | e9f4ba8 | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 98 | const std::vector<CodeGenRegister> &getRegisters() const { |
Chris Lattner | 2669311 | 2004-08-16 01:10:21 +0000 | [diff] [blame] | 99 | if (Registers.empty()) ReadRegisters(); |
| 100 | return Registers; |
| 101 | } |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 102 | |
Jakob Stoklund Olesen | 09bc029 | 2010-05-24 21:46:58 +0000 | [diff] [blame^] | 103 | const std::vector<Record*> &getSubRegIndices() const { |
| 104 | if (SubRegIndices.empty()) ReadSubRegIndices(); |
| 105 | return SubRegIndices; |
| 106 | } |
| 107 | |
| 108 | // Map a SubRegIndex Record to its number. |
| 109 | unsigned getSubRegIndexNo(Record *idx) const { |
| 110 | return idx->getValueAsInt("NumberHack"); |
| 111 | } |
| 112 | |
Chris Lattner | e9f4ba8 | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 113 | const std::vector<CodeGenRegisterClass> &getRegisterClasses() const { |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 114 | if (RegisterClasses.empty()) ReadRegisterClasses(); |
| 115 | return RegisterClasses; |
| 116 | } |
Jakob Stoklund Olesen | 09bc029 | 2010-05-24 21:46:58 +0000 | [diff] [blame^] | 117 | |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 118 | const CodeGenRegisterClass &getRegisterClass(Record *R) const { |
| 119 | const std::vector<CodeGenRegisterClass> &RC = getRegisterClasses(); |
| 120 | for (unsigned i = 0, e = RC.size(); i != e; ++i) |
| 121 | if (RC[i].TheDef == R) |
| 122 | return RC[i]; |
| 123 | assert(0 && "Didn't find the register class"); |
| 124 | abort(); |
| 125 | } |
Chris Lattner | 5c4736a | 2005-12-05 02:35:08 +0000 | [diff] [blame] | 126 | |
| 127 | /// getRegisterClassForRegister - Find the register class that contains the |
Dan Gohman | 8a17870 | 2009-04-13 15:24:11 +0000 | [diff] [blame] | 128 | /// specified physical register. If the register is not in a register |
| 129 | /// class, return null. If the register is in multiple classes, and the |
| 130 | /// classes have a superset-subset relationship and the same set of |
| 131 | /// types, return the superclass. Otherwise return null. |
Chris Lattner | 5c4736a | 2005-12-05 02:35:08 +0000 | [diff] [blame] | 132 | const CodeGenRegisterClass *getRegisterClassForRegister(Record *R) const { |
| 133 | const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses(); |
| 134 | const CodeGenRegisterClass *FoundRC = 0; |
| 135 | for (unsigned i = 0, e = RCs.size(); i != e; ++i) { |
| 136 | const CodeGenRegisterClass &RC = RegisterClasses[i]; |
| 137 | for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) { |
Dan Gohman | 8a17870 | 2009-04-13 15:24:11 +0000 | [diff] [blame] | 138 | if (R != RC.Elements[ei]) |
| 139 | continue; |
| 140 | |
| 141 | // If a register's classes have different types, return null. |
| 142 | if (FoundRC && RC.getValueTypes() != FoundRC->getValueTypes()) |
| 143 | return 0; |
| 144 | |
| 145 | // If this is the first class that contains the register, |
| 146 | // make a note of it and go on to the next class. |
| 147 | if (!FoundRC) { |
Chris Lattner | 5c4736a | 2005-12-05 02:35:08 +0000 | [diff] [blame] | 148 | FoundRC = &RC; |
| 149 | break; |
| 150 | } |
Dan Gohman | 8a17870 | 2009-04-13 15:24:11 +0000 | [diff] [blame] | 151 | |
| 152 | std::vector<Record *> Elements(RC.Elements); |
| 153 | std::vector<Record *> FoundElements(FoundRC->Elements); |
| 154 | std::sort(Elements.begin(), Elements.end()); |
| 155 | std::sort(FoundElements.begin(), FoundElements.end()); |
| 156 | |
| 157 | // Check to see if the previously found class that contains |
| 158 | // the register is a subclass of the current class. If so, |
| 159 | // prefer the superclass. |
| 160 | if (std::includes(Elements.begin(), Elements.end(), |
| 161 | FoundElements.begin(), FoundElements.end())) { |
| 162 | FoundRC = &RC; |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | // Check to see if the previously found class that contains |
| 167 | // the register is a superclass of the current class. If so, |
| 168 | // prefer the superclass. |
| 169 | if (std::includes(FoundElements.begin(), FoundElements.end(), |
| 170 | Elements.begin(), Elements.end())) |
| 171 | break; |
| 172 | |
| 173 | // Multiple classes, and neither is a superclass of the other. |
| 174 | // Return null. |
| 175 | return 0; |
Chris Lattner | 5c4736a | 2005-12-05 02:35:08 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | return FoundRC; |
| 179 | } |
Evan Cheng | 44a65fa | 2006-05-16 07:05:30 +0000 | [diff] [blame] | 180 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 181 | /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the |
Evan Cheng | 44a65fa | 2006-05-16 07:05:30 +0000 | [diff] [blame] | 182 | /// specified physical register. |
Chris Lattner | 2cacec5 | 2010-03-15 06:00:16 +0000 | [diff] [blame] | 183 | std::vector<MVT::SimpleValueType> getRegisterVTs(Record *R) const; |
Chris Lattner | 5c4736a | 2005-12-05 02:35:08 +0000 | [diff] [blame] | 184 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 185 | const std::vector<MVT::SimpleValueType> &getLegalValueTypes() const { |
Chris Lattner | e9f4ba8 | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 186 | if (LegalValueTypes.empty()) ReadLegalValueTypes(); |
| 187 | return LegalValueTypes; |
| 188 | } |
| 189 | |
| 190 | /// isLegalValueType - Return true if the specified value type is natively |
| 191 | /// supported by the target (i.e. there are registers that directly hold it). |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 192 | bool isLegalValueType(MVT::SimpleValueType VT) const { |
| 193 | const std::vector<MVT::SimpleValueType> &LegalVTs = getLegalValueTypes(); |
Chris Lattner | e9f4ba8 | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 194 | for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i) |
| 195 | if (LegalVTs[i] == VT) return true; |
| 196 | return false; |
| 197 | } |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 198 | |
Chris Lattner | b61e09d | 2010-03-19 00:18:23 +0000 | [diff] [blame] | 199 | private: |
Chris Lattner | e14d2e2 | 2010-03-19 01:07:44 +0000 | [diff] [blame] | 200 | DenseMap<const Record*, CodeGenInstruction*> &getInstructions() const { |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 201 | if (Instructions.empty()) ReadInstructions(); |
| 202 | return Instructions; |
| 203 | } |
Chris Lattner | f30187a | 2010-03-19 00:07:20 +0000 | [diff] [blame] | 204 | public: |
| 205 | |
Chris Lattner | e14d2e2 | 2010-03-19 01:07:44 +0000 | [diff] [blame] | 206 | CodeGenInstruction &getInstruction(const Record *InstRec) const { |
| 207 | if (Instructions.empty()) ReadInstructions(); |
| 208 | DenseMap<const Record*, CodeGenInstruction*>::iterator I = |
| 209 | Instructions.find(InstRec); |
| 210 | assert(I != Instructions.end() && "Not an instruction"); |
| 211 | return *I->second; |
| 212 | } |
Chris Lattner | a974b20 | 2005-09-14 18:02:53 +0000 | [diff] [blame] | 213 | |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 214 | /// getInstructionsByEnumValue - Return all of the instructions defined by the |
| 215 | /// target, ordered by their enum value. |
Chris Lattner | 6a91b18 | 2010-03-19 01:00:55 +0000 | [diff] [blame] | 216 | const std::vector<const CodeGenInstruction*> & |
| 217 | getInstructionsByEnumValue() const { |
Chris Lattner | f650278 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 218 | if (InstrsByEnum.empty()) ComputeInstrsByEnum(); |
| 219 | return InstrsByEnum; |
| 220 | } |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 6a91b18 | 2010-03-19 01:00:55 +0000 | [diff] [blame] | 222 | typedef std::vector<const CodeGenInstruction*>::const_iterator inst_iterator; |
| 223 | inst_iterator inst_begin() const{return getInstructionsByEnumValue().begin();} |
| 224 | inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); } |
| 225 | |
| 226 | |
Misha Brukman | 35e83cc | 2004-10-14 05:50:43 +0000 | [diff] [blame] | 227 | /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]? |
| 228 | /// |
| 229 | bool isLittleEndianEncoding() const; |
Chris Lattner | f650278 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 230 | |
| 231 | private: |
Chris Lattner | 6a91b18 | 2010-03-19 01:00:55 +0000 | [diff] [blame] | 232 | void ComputeInstrsByEnum() const; |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 235 | /// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern |
| 236 | /// tablegen class in TargetSelectionDAG.td |
| 237 | class ComplexPattern { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 238 | MVT::SimpleValueType Ty; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 239 | unsigned NumOperands; |
| 240 | std::string SelectFunc; |
Evan Cheng | 3aa39f4 | 2005-12-08 02:14:08 +0000 | [diff] [blame] | 241 | std::vector<Record*> RootNodes; |
Christopher Lamb | 8535624 | 2008-01-31 07:27:46 +0000 | [diff] [blame] | 242 | unsigned Properties; // Node properties |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 243 | public: |
Chris Lattner | c128b3e | 2009-11-06 06:33:01 +0000 | [diff] [blame] | 244 | ComplexPattern() : NumOperands(0) {} |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 245 | ComplexPattern(Record *R); |
| 246 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 247 | MVT::SimpleValueType getValueType() const { return Ty; } |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 248 | unsigned getNumOperands() const { return NumOperands; } |
| 249 | const std::string &getSelectFunc() const { return SelectFunc; } |
Evan Cheng | 3aa39f4 | 2005-12-08 02:14:08 +0000 | [diff] [blame] | 250 | const std::vector<Record*> &getRootNodes() const { |
| 251 | return RootNodes; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 252 | } |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 253 | bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); } |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 256 | } // End llvm namespace |
| 257 | |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 258 | #endif |