blob: a0e631e1093faa52a0dfdb5b0ad34eb7b18a432d [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
Daniel Dunbar1a551802009-07-03 00:10:29 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner26693112004-08-16 01:10:21 +000021#include "CodeGenRegisters.h"
Chris Lattnerec352402004-08-01 05:04:00 +000022#include "CodeGenInstruction.h"
Dan Gohman8a178702009-04-13 15:24:11 +000023#include <algorithm>
Chris Lattnerec352402004-08-01 05:04:00 +000024#include <map>
Brian Gaeked0fde302003-11-11 22:41:34 +000025
26namespace llvm {
27
Chris Lattner45872072003-08-07 05:38:11 +000028class Record;
29class RecordKeeper;
Chris Lattner1fca5ff2004-10-27 16:14:51 +000030struct CodeGenRegister;
Chris Lattner8850a1b2006-03-27 22:48:18 +000031class CodeGenTarget;
Chris Lattner45872072003-08-07 05:38:11 +000032
Evan Cheng94b30402006-10-11 21:02:01 +000033// SelectionDAG node properties.
Mon P Wang28873102008-06-25 08:15:39 +000034// SDNPMemOperand: indicates that a node touches memory and therefore must
35// have an associated memory operand that describes the access.
Chris Lattnerc8478d82008-01-06 06:44:58 +000036enum SDNP {
37 SDNPCommutative,
38 SDNPAssociative,
39 SDNPHasChain,
40 SDNPOutFlag,
41 SDNPInFlag,
42 SDNPOptInFlag,
Chris Lattner710e9952008-01-10 04:38:57 +000043 SDNPMayLoad,
Chris Lattnerbc0b9f72008-01-10 05:39:30 +000044 SDNPMayStore,
Mon P Wang28873102008-06-25 08:15:39 +000045 SDNPSideEffect,
Dale Johannesen874ae252009-06-02 03:12:52 +000046 SDNPMemOperand
Chris Lattnerc8478d82008-01-06 06:44:58 +000047};
Evan Cheng94b30402006-10-11 21:02:01 +000048
Owen Anderson825b72b2009-08-11 20:47:22 +000049/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands83ec4b62008-06-06 12:08:01 +000050/// record corresponds to.
Owen Anderson825b72b2009-08-11 20:47:22 +000051MVT::SimpleValueType getValueType(Record *Rec);
Chris Lattner45872072003-08-07 05:38:11 +000052
Owen Anderson825b72b2009-08-11 20:47:22 +000053std::string getName(MVT::SimpleValueType T);
54std::string getEnumName(MVT::SimpleValueType T);
Chris Lattner45872072003-08-07 05:38:11 +000055
Chris Lattner6cefb772008-01-05 22:25:12 +000056/// getQualifiedName - Return the name of the specified record, with a
57/// namespace qualifier if the record contains one.
58std::string getQualifiedName(const Record *R);
59
Chris Lattner45872072003-08-07 05:38:11 +000060/// CodeGenTarget - This class corresponds to the Target class in the .td files.
61///
62class CodeGenTarget {
63 Record *TargetRec;
Chris Lattner45872072003-08-07 05:38:11 +000064
Chris Lattnerec352402004-08-01 05:04:00 +000065 mutable std::map<std::string, CodeGenInstruction> Instructions;
Chris Lattner26693112004-08-16 01:10:21 +000066 mutable std::vector<CodeGenRegister> Registers;
Chris Lattner056afef2004-08-21 04:05:00 +000067 mutable std::vector<CodeGenRegisterClass> RegisterClasses;
Owen Anderson825b72b2009-08-11 20:47:22 +000068 mutable std::vector<MVT::SimpleValueType> LegalValueTypes;
Chris Lattner26693112004-08-16 01:10:21 +000069 void ReadRegisters() const;
Chris Lattner056afef2004-08-21 04:05:00 +000070 void ReadRegisterClasses() const;
71 void ReadInstructions() const;
Chris Lattnere9f4ba82005-09-08 21:43:21 +000072 void ReadLegalValueTypes() const;
Chris Lattnerf6502782010-03-19 00:34:35 +000073
Chris Lattnera28bc682010-03-19 00:40:22 +000074 mutable std::vector<const CodeGenInstruction*> InstrsByEnum;
Chris Lattner45872072003-08-07 05:38:11 +000075public:
76 CodeGenTarget();
77
78 Record *getTargetRecord() const { return TargetRec; }
79 const std::string &getName() const;
80
Dan Gohman1e0ee4b2008-08-20 21:45:57 +000081 /// getInstNamespace - Return the target-specific instruction namespace.
82 ///
83 std::string getInstNamespace() const;
84
Chris Lattner175580c2004-08-14 22:50:53 +000085 /// getInstructionSet - Return the InstructionSet object.
Chris Lattnerec352402004-08-01 05:04:00 +000086 ///
Chris Lattner45872072003-08-07 05:38:11 +000087 Record *getInstructionSet() const;
88
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000089 /// getAsmParser - Return the AssemblyParser definition for this target.
90 ///
91 Record *getAsmParser() const;
92
Chris Lattner175580c2004-08-14 22:50:53 +000093 /// getAsmWriter - Return the AssemblyWriter definition for this target.
94 ///
95 Record *getAsmWriter() const;
96
Chris Lattnere9f4ba82005-09-08 21:43:21 +000097 const std::vector<CodeGenRegister> &getRegisters() const {
Chris Lattner26693112004-08-16 01:10:21 +000098 if (Registers.empty()) ReadRegisters();
99 return Registers;
100 }
Chris Lattnerec352402004-08-01 05:04:00 +0000101
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000102 const std::vector<CodeGenRegisterClass> &getRegisterClasses() const {
Chris Lattner056afef2004-08-21 04:05:00 +0000103 if (RegisterClasses.empty()) ReadRegisterClasses();
104 return RegisterClasses;
105 }
Nate Begemanddb39542005-12-01 00:06:14 +0000106
107 const CodeGenRegisterClass &getRegisterClass(Record *R) const {
108 const std::vector<CodeGenRegisterClass> &RC = getRegisterClasses();
109 for (unsigned i = 0, e = RC.size(); i != e; ++i)
110 if (RC[i].TheDef == R)
111 return RC[i];
112 assert(0 && "Didn't find the register class");
113 abort();
114 }
Chris Lattner5c4736a2005-12-05 02:35:08 +0000115
116 /// getRegisterClassForRegister - Find the register class that contains the
Dan Gohman8a178702009-04-13 15:24:11 +0000117 /// specified physical register. If the register is not in a register
118 /// class, return null. If the register is in multiple classes, and the
119 /// classes have a superset-subset relationship and the same set of
120 /// types, return the superclass. Otherwise return null.
Chris Lattner5c4736a2005-12-05 02:35:08 +0000121 const CodeGenRegisterClass *getRegisterClassForRegister(Record *R) const {
122 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
123 const CodeGenRegisterClass *FoundRC = 0;
124 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
125 const CodeGenRegisterClass &RC = RegisterClasses[i];
126 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
Dan Gohman8a178702009-04-13 15:24:11 +0000127 if (R != RC.Elements[ei])
128 continue;
129
130 // If a register's classes have different types, return null.
131 if (FoundRC && RC.getValueTypes() != FoundRC->getValueTypes())
132 return 0;
133
134 // If this is the first class that contains the register,
135 // make a note of it and go on to the next class.
136 if (!FoundRC) {
Chris Lattner5c4736a2005-12-05 02:35:08 +0000137 FoundRC = &RC;
138 break;
139 }
Dan Gohman8a178702009-04-13 15:24:11 +0000140
141 std::vector<Record *> Elements(RC.Elements);
142 std::vector<Record *> FoundElements(FoundRC->Elements);
143 std::sort(Elements.begin(), Elements.end());
144 std::sort(FoundElements.begin(), FoundElements.end());
145
146 // Check to see if the previously found class that contains
147 // the register is a subclass of the current class. If so,
148 // prefer the superclass.
149 if (std::includes(Elements.begin(), Elements.end(),
150 FoundElements.begin(), FoundElements.end())) {
151 FoundRC = &RC;
152 break;
153 }
154
155 // Check to see if the previously found class that contains
156 // the register is a superclass of the current class. If so,
157 // prefer the superclass.
158 if (std::includes(FoundElements.begin(), FoundElements.end(),
159 Elements.begin(), Elements.end()))
160 break;
161
162 // Multiple classes, and neither is a superclass of the other.
163 // Return null.
164 return 0;
Chris Lattner5c4736a2005-12-05 02:35:08 +0000165 }
166 }
167 return FoundRC;
168 }
Evan Cheng44a65fa2006-05-16 07:05:30 +0000169
Duncan Sands83ec4b62008-06-06 12:08:01 +0000170 /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the
Evan Cheng44a65fa2006-05-16 07:05:30 +0000171 /// specified physical register.
Chris Lattner2cacec52010-03-15 06:00:16 +0000172 std::vector<MVT::SimpleValueType> getRegisterVTs(Record *R) const;
Chris Lattner5c4736a2005-12-05 02:35:08 +0000173
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 const std::vector<MVT::SimpleValueType> &getLegalValueTypes() const {
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000175 if (LegalValueTypes.empty()) ReadLegalValueTypes();
176 return LegalValueTypes;
177 }
178
179 /// isLegalValueType - Return true if the specified value type is natively
180 /// supported by the target (i.e. there are registers that directly hold it).
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 bool isLegalValueType(MVT::SimpleValueType VT) const {
182 const std::vector<MVT::SimpleValueType> &LegalVTs = getLegalValueTypes();
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000183 for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i)
184 if (LegalVTs[i] == VT) return true;
185 return false;
186 }
Chris Lattner056afef2004-08-21 04:05:00 +0000187
Chris Lattnerec352402004-08-01 05:04:00 +0000188 /// getInstructions - Return all of the instructions defined for this target.
189 ///
Chris Lattnerb61e09d2010-03-19 00:18:23 +0000190private:
Chris Lattnerec352402004-08-01 05:04:00 +0000191 const std::map<std::string, CodeGenInstruction> &getInstructions() const {
192 if (Instructions.empty()) ReadInstructions();
193 return Instructions;
194 }
Dan Gohmanee4fa192008-04-03 00:02:49 +0000195 std::map<std::string, CodeGenInstruction> &getInstructions() {
196 if (Instructions.empty()) ReadInstructions();
197 return Instructions;
198 }
Chris Lattnera974b202005-09-14 18:02:53 +0000199 CodeGenInstruction &getInstruction(const std::string &Name) const {
200 const std::map<std::string, CodeGenInstruction> &Insts = getInstructions();
201 assert(Insts.count(Name) && "Not an instruction!");
202 return const_cast<CodeGenInstruction&>(Insts.find(Name)->second);
203 }
Chris Lattnerf30187a2010-03-19 00:07:20 +0000204public:
205
206 CodeGenInstruction &getInstruction(const Record *InstRec) const;
Chris Lattnera974b202005-09-14 18:02:53 +0000207
Chris Lattnerd6488672005-01-22 18:58:51 +0000208 /// getInstructionsByEnumValue - Return all of the instructions defined by the
209 /// target, ordered by their enum value.
Chris Lattnera28bc682010-03-19 00:40:22 +0000210 const std::vector<const CodeGenInstruction*> &
211 getInstructionsByEnumValue() const {
Chris Lattnerf6502782010-03-19 00:34:35 +0000212 if (InstrsByEnum.empty()) ComputeInstrsByEnum();
213 return InstrsByEnum;
214 }
Chris Lattnerd6488672005-01-22 18:58:51 +0000215
Chris Lattnera28bc682010-03-19 00:40:22 +0000216 typedef std::vector<const CodeGenInstruction*>::const_iterator inst_iterator;
217 inst_iterator inst_begin() const{return getInstructionsByEnumValue().begin();}
218 inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }
219
220
Misha Brukman35e83cc2004-10-14 05:50:43 +0000221 /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?
222 ///
223 bool isLittleEndianEncoding() const;
Chris Lattnerf6502782010-03-19 00:34:35 +0000224
225private:
Chris Lattnera28bc682010-03-19 00:40:22 +0000226 void ComputeInstrsByEnum() const;
Chris Lattner45872072003-08-07 05:38:11 +0000227};
228
Evan Cheng0fc71982005-12-08 02:00:36 +0000229/// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
230/// tablegen class in TargetSelectionDAG.td
231class ComplexPattern {
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 MVT::SimpleValueType Ty;
Evan Cheng0fc71982005-12-08 02:00:36 +0000233 unsigned NumOperands;
234 std::string SelectFunc;
Evan Cheng3aa39f42005-12-08 02:14:08 +0000235 std::vector<Record*> RootNodes;
Christopher Lamb85356242008-01-31 07:27:46 +0000236 unsigned Properties; // Node properties
Evan Cheng0fc71982005-12-08 02:00:36 +0000237public:
Chris Lattnerc128b3e2009-11-06 06:33:01 +0000238 ComplexPattern() : NumOperands(0) {}
Evan Cheng0fc71982005-12-08 02:00:36 +0000239 ComplexPattern(Record *R);
240
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 MVT::SimpleValueType getValueType() const { return Ty; }
Evan Cheng0fc71982005-12-08 02:00:36 +0000242 unsigned getNumOperands() const { return NumOperands; }
243 const std::string &getSelectFunc() const { return SelectFunc; }
Evan Cheng3aa39f42005-12-08 02:14:08 +0000244 const std::vector<Record*> &getRootNodes() const {
245 return RootNodes;
Evan Cheng0fc71982005-12-08 02:00:36 +0000246 }
Evan Cheng94b30402006-10-11 21:02:01 +0000247 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Evan Cheng0fc71982005-12-08 02:00:36 +0000248};
249
Brian Gaeked0fde302003-11-11 22:41:34 +0000250} // End llvm namespace
251
Chris Lattner45872072003-08-07 05:38:11 +0000252#endif