blob: 9656d0b56af487c68444d648f8c33ac63c348601 [file] [log] [blame]
Chris Lattner803a5f62004-08-01 04:04:35 +00001//===- CodeGenTarget.h - Target Class Wrapper -------------------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner45872072003-08-07 05:38:11 +00009//
Chris Lattner803a5f62004-08-01 04:04:35 +000010// 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 Lattner45872072003-08-07 05:38:11 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner803a5f62004-08-01 04:04:35 +000017#ifndef CODEGEN_TARGET_H
18#define CODEGEN_TARGET_H
Chris Lattner45872072003-08-07 05:38:11 +000019
Chris Lattner26693112004-08-16 01:10:21 +000020#include "CodeGenRegisters.h"
Chris Lattnerec352402004-08-01 05:04:00 +000021#include "CodeGenInstruction.h"
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +000022#include "Record.h"
Chris Lattnere14d2e22010-03-19 01:07:44 +000023#include "llvm/Support/raw_ostream.h"
Dan Gohman8a178702009-04-13 15:24:11 +000024#include <algorithm>
Brian Gaeked0fde302003-11-11 22:41:34 +000025
26namespace llvm {
27
Chris Lattner1fca5ff2004-10-27 16:14:51 +000028struct CodeGenRegister;
Chris Lattner8850a1b2006-03-27 22:48:18 +000029class CodeGenTarget;
Chris Lattner45872072003-08-07 05:38:11 +000030
Evan Cheng94b30402006-10-11 21:02:01 +000031// SelectionDAG node properties.
Mon P Wang28873102008-06-25 08:15:39 +000032// SDNPMemOperand: indicates that a node touches memory and therefore must
33// have an associated memory operand that describes the access.
Chris Lattnerc8478d82008-01-06 06:44:58 +000034enum SDNP {
35 SDNPCommutative,
36 SDNPAssociative,
37 SDNPHasChain,
38 SDNPOutFlag,
39 SDNPInFlag,
40 SDNPOptInFlag,
Chris Lattner710e9952008-01-10 04:38:57 +000041 SDNPMayLoad,
Chris Lattnerbc0b9f72008-01-10 05:39:30 +000042 SDNPMayStore,
Mon P Wang28873102008-06-25 08:15:39 +000043 SDNPSideEffect,
Chris Lattnere8cabf32010-03-19 05:07:09 +000044 SDNPMemOperand,
Chris Lattner52a261b2010-09-21 20:31:19 +000045 SDNPVariadic,
46 SDNPWantRoot,
47 SDNPWantParent
Chris Lattnerc8478d82008-01-06 06:44:58 +000048};
Evan Cheng94b30402006-10-11 21:02:01 +000049
Owen Anderson825b72b2009-08-11 20:47:22 +000050/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands83ec4b62008-06-06 12:08:01 +000051/// record corresponds to.
Owen Anderson825b72b2009-08-11 20:47:22 +000052MVT::SimpleValueType getValueType(Record *Rec);
Chris Lattner45872072003-08-07 05:38:11 +000053
Owen Anderson825b72b2009-08-11 20:47:22 +000054std::string getName(MVT::SimpleValueType T);
55std::string getEnumName(MVT::SimpleValueType T);
Chris Lattner45872072003-08-07 05:38:11 +000056
Chris Lattner6cefb772008-01-05 22:25:12 +000057/// getQualifiedName - Return the name of the specified record, with a
58/// namespace qualifier if the record contains one.
59std::string getQualifiedName(const Record *R);
60
Chris Lattner45872072003-08-07 05:38:11 +000061/// CodeGenTarget - This class corresponds to the Target class in the .td files.
62///
63class CodeGenTarget {
Chris Lattner67db8832010-12-13 00:23:57 +000064 RecordKeeper &Records;
Chris Lattner45872072003-08-07 05:38:11 +000065 Record *TargetRec;
Chris Lattner45872072003-08-07 05:38:11 +000066
Chris Lattnere14d2e22010-03-19 01:07:44 +000067 mutable DenseMap<const Record*, CodeGenInstruction*> Instructions;
Chris Lattner26693112004-08-16 01:10:21 +000068 mutable std::vector<CodeGenRegister> Registers;
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +000069 mutable std::vector<Record*> SubRegIndices;
Chris Lattner056afef2004-08-21 04:05:00 +000070 mutable std::vector<CodeGenRegisterClass> RegisterClasses;
Owen Anderson825b72b2009-08-11 20:47:22 +000071 mutable std::vector<MVT::SimpleValueType> LegalValueTypes;
Chris Lattner26693112004-08-16 01:10:21 +000072 void ReadRegisters() const;
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +000073 void ReadSubRegIndices() const;
Chris Lattner056afef2004-08-21 04:05:00 +000074 void ReadRegisterClasses() const;
75 void ReadInstructions() const;
Chris Lattnere9f4ba82005-09-08 21:43:21 +000076 void ReadLegalValueTypes() const;
Chris Lattnerf6502782010-03-19 00:34:35 +000077
Chris Lattner6a91b182010-03-19 01:00:55 +000078 mutable std::vector<const CodeGenInstruction*> InstrsByEnum;
Chris Lattner45872072003-08-07 05:38:11 +000079public:
Chris Lattner67db8832010-12-13 00:23:57 +000080 CodeGenTarget(RecordKeeper &Records);
Chris Lattner45872072003-08-07 05:38:11 +000081
82 Record *getTargetRecord() const { return TargetRec; }
83 const std::string &getName() const;
84
Dan Gohman1e0ee4b2008-08-20 21:45:57 +000085 /// getInstNamespace - Return the target-specific instruction namespace.
86 ///
87 std::string getInstNamespace() const;
88
Chris Lattner175580c2004-08-14 22:50:53 +000089 /// getInstructionSet - Return the InstructionSet object.
Chris Lattnerec352402004-08-01 05:04:00 +000090 ///
Chris Lattner45872072003-08-07 05:38:11 +000091 Record *getInstructionSet() const;
92
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000093 /// getAsmParser - Return the AssemblyParser definition for this target.
94 ///
95 Record *getAsmParser() const;
96
Chris Lattner175580c2004-08-14 22:50:53 +000097 /// getAsmWriter - Return the AssemblyWriter definition for this target.
98 ///
99 Record *getAsmWriter() const;
100
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000101 const std::vector<CodeGenRegister> &getRegisters() const {
Chris Lattner26693112004-08-16 01:10:21 +0000102 if (Registers.empty()) ReadRegisters();
103 return Registers;
104 }
Chris Lattnerec6f0962010-11-02 18:10:06 +0000105
106 /// getRegisterByName - If there is a register with the specific AsmName,
107 /// return it.
108 const CodeGenRegister *getRegisterByName(StringRef Name) const;
Chris Lattnerec352402004-08-01 05:04:00 +0000109
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +0000110 const std::vector<Record*> &getSubRegIndices() const {
111 if (SubRegIndices.empty()) ReadSubRegIndices();
112 return SubRegIndices;
113 }
114
115 // Map a SubRegIndex Record to its number.
116 unsigned getSubRegIndexNo(Record *idx) const {
Jakob Stoklund Olesen48d0c162010-05-25 17:21:04 +0000117 if (SubRegIndices.empty()) ReadSubRegIndices();
118 std::vector<Record*>::const_iterator i =
119 std::find(SubRegIndices.begin(), SubRegIndices.end(), idx);
120 assert(i != SubRegIndices.end() && "Not a SubRegIndex");
121 return (i - SubRegIndices.begin()) + 1;
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +0000122 }
123
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000124 const std::vector<CodeGenRegisterClass> &getRegisterClasses() const {
Chris Lattner056afef2004-08-21 04:05:00 +0000125 if (RegisterClasses.empty()) ReadRegisterClasses();
126 return RegisterClasses;
127 }
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +0000128
Nate Begemanddb39542005-12-01 00:06:14 +0000129 const CodeGenRegisterClass &getRegisterClass(Record *R) const {
130 const std::vector<CodeGenRegisterClass> &RC = getRegisterClasses();
131 for (unsigned i = 0, e = RC.size(); i != e; ++i)
132 if (RC[i].TheDef == R)
133 return RC[i];
134 assert(0 && "Didn't find the register class");
135 abort();
136 }
Chris Lattner5c4736a2005-12-05 02:35:08 +0000137
138 /// getRegisterClassForRegister - Find the register class that contains the
Dan Gohman8a178702009-04-13 15:24:11 +0000139 /// specified physical register. If the register is not in a register
140 /// class, return null. If the register is in multiple classes, and the
141 /// classes have a superset-subset relationship and the same set of
142 /// types, return the superclass. Otherwise return null.
Chris Lattner5c4736a2005-12-05 02:35:08 +0000143 const CodeGenRegisterClass *getRegisterClassForRegister(Record *R) const {
144 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
145 const CodeGenRegisterClass *FoundRC = 0;
146 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
147 const CodeGenRegisterClass &RC = RegisterClasses[i];
148 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
Dan Gohman8a178702009-04-13 15:24:11 +0000149 if (R != RC.Elements[ei])
150 continue;
151
152 // If a register's classes have different types, return null.
153 if (FoundRC && RC.getValueTypes() != FoundRC->getValueTypes())
154 return 0;
155
156 // If this is the first class that contains the register,
157 // make a note of it and go on to the next class.
158 if (!FoundRC) {
Chris Lattner5c4736a2005-12-05 02:35:08 +0000159 FoundRC = &RC;
160 break;
161 }
Dan Gohman8a178702009-04-13 15:24:11 +0000162
163 std::vector<Record *> Elements(RC.Elements);
164 std::vector<Record *> FoundElements(FoundRC->Elements);
165 std::sort(Elements.begin(), Elements.end());
166 std::sort(FoundElements.begin(), FoundElements.end());
167
168 // Check to see if the previously found class that contains
169 // the register is a subclass of the current class. If so,
170 // prefer the superclass.
171 if (std::includes(Elements.begin(), Elements.end(),
172 FoundElements.begin(), FoundElements.end())) {
173 FoundRC = &RC;
174 break;
175 }
176
177 // Check to see if the previously found class that contains
178 // the register is a superclass of the current class. If so,
179 // prefer the superclass.
180 if (std::includes(FoundElements.begin(), FoundElements.end(),
181 Elements.begin(), Elements.end()))
182 break;
183
184 // Multiple classes, and neither is a superclass of the other.
185 // Return null.
186 return 0;
Chris Lattner5c4736a2005-12-05 02:35:08 +0000187 }
188 }
189 return FoundRC;
190 }
Evan Cheng44a65fa2006-05-16 07:05:30 +0000191
Duncan Sands83ec4b62008-06-06 12:08:01 +0000192 /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the
Evan Cheng44a65fa2006-05-16 07:05:30 +0000193 /// specified physical register.
Chris Lattner2cacec52010-03-15 06:00:16 +0000194 std::vector<MVT::SimpleValueType> getRegisterVTs(Record *R) const;
Chris Lattner5c4736a2005-12-05 02:35:08 +0000195
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 const std::vector<MVT::SimpleValueType> &getLegalValueTypes() const {
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000197 if (LegalValueTypes.empty()) ReadLegalValueTypes();
198 return LegalValueTypes;
199 }
200
201 /// isLegalValueType - Return true if the specified value type is natively
202 /// supported by the target (i.e. there are registers that directly hold it).
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 bool isLegalValueType(MVT::SimpleValueType VT) const {
204 const std::vector<MVT::SimpleValueType> &LegalVTs = getLegalValueTypes();
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000205 for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i)
206 if (LegalVTs[i] == VT) return true;
207 return false;
208 }
Chris Lattner056afef2004-08-21 04:05:00 +0000209
Chris Lattnerb61e09d2010-03-19 00:18:23 +0000210private:
Chris Lattnere14d2e22010-03-19 01:07:44 +0000211 DenseMap<const Record*, CodeGenInstruction*> &getInstructions() const {
Chris Lattnerec352402004-08-01 05:04:00 +0000212 if (Instructions.empty()) ReadInstructions();
213 return Instructions;
214 }
Chris Lattnerf30187a2010-03-19 00:07:20 +0000215public:
216
Chris Lattnere14d2e22010-03-19 01:07:44 +0000217 CodeGenInstruction &getInstruction(const Record *InstRec) const {
218 if (Instructions.empty()) ReadInstructions();
219 DenseMap<const Record*, CodeGenInstruction*>::iterator I =
220 Instructions.find(InstRec);
221 assert(I != Instructions.end() && "Not an instruction");
222 return *I->second;
223 }
Chris Lattnera974b202005-09-14 18:02:53 +0000224
Chris Lattnerd6488672005-01-22 18:58:51 +0000225 /// getInstructionsByEnumValue - Return all of the instructions defined by the
226 /// target, ordered by their enum value.
Chris Lattner6a91b182010-03-19 01:00:55 +0000227 const std::vector<const CodeGenInstruction*> &
228 getInstructionsByEnumValue() const {
Chris Lattnerf6502782010-03-19 00:34:35 +0000229 if (InstrsByEnum.empty()) ComputeInstrsByEnum();
230 return InstrsByEnum;
231 }
Chris Lattnerd6488672005-01-22 18:58:51 +0000232
Chris Lattner6a91b182010-03-19 01:00:55 +0000233 typedef std::vector<const CodeGenInstruction*>::const_iterator inst_iterator;
234 inst_iterator inst_begin() const{return getInstructionsByEnumValue().begin();}
235 inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }
236
237
Misha Brukman35e83cc2004-10-14 05:50:43 +0000238 /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?
239 ///
240 bool isLittleEndianEncoding() const;
Chris Lattnerf6502782010-03-19 00:34:35 +0000241
242private:
Chris Lattner6a91b182010-03-19 01:00:55 +0000243 void ComputeInstrsByEnum() const;
Chris Lattner45872072003-08-07 05:38:11 +0000244};
245
Evan Cheng0fc71982005-12-08 02:00:36 +0000246/// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
247/// tablegen class in TargetSelectionDAG.td
248class ComplexPattern {
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 MVT::SimpleValueType Ty;
Evan Cheng0fc71982005-12-08 02:00:36 +0000250 unsigned NumOperands;
251 std::string SelectFunc;
Evan Cheng3aa39f42005-12-08 02:14:08 +0000252 std::vector<Record*> RootNodes;
Christopher Lamb85356242008-01-31 07:27:46 +0000253 unsigned Properties; // Node properties
Evan Cheng0fc71982005-12-08 02:00:36 +0000254public:
Chris Lattnerc128b3e2009-11-06 06:33:01 +0000255 ComplexPattern() : NumOperands(0) {}
Evan Cheng0fc71982005-12-08 02:00:36 +0000256 ComplexPattern(Record *R);
257
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 MVT::SimpleValueType getValueType() const { return Ty; }
Evan Cheng0fc71982005-12-08 02:00:36 +0000259 unsigned getNumOperands() const { return NumOperands; }
260 const std::string &getSelectFunc() const { return SelectFunc; }
Evan Cheng3aa39f42005-12-08 02:14:08 +0000261 const std::vector<Record*> &getRootNodes() const {
262 return RootNodes;
Evan Cheng0fc71982005-12-08 02:00:36 +0000263 }
Evan Cheng94b30402006-10-11 21:02:01 +0000264 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Evan Cheng0fc71982005-12-08 02:00:36 +0000265};
266
Brian Gaeked0fde302003-11-11 22:41:34 +0000267} // End llvm namespace
268
Chris Lattner45872072003-08-07 05:38:11 +0000269#endif