blob: 5d4548923013cc750dcf9380190508818aab1845 [file] [log] [blame]
Chris Lattner6cc654b2008-01-06 01:35:39 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
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 Lattner6cc654b2008-01-06 01:35:39 +000010// This class wraps target description classes used by the various code
Chris Lattner45872072003-08-07 05:38:11 +000011// generation TableGen backends. This makes it easier to access the data and
12// provides a single place that needs to check it for validity. All of these
13// classes throw exceptions on error conditions.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner803a5f62004-08-01 04:04:35 +000017#include "CodeGenTarget.h"
Chris Lattner43fbbc32006-03-24 19:49:31 +000018#include "CodeGenIntrinsics.h"
Chris Lattner45872072003-08-07 05:38:11 +000019#include "Record.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner0d7952e2010-03-27 20:32:26 +000021#include "llvm/ADT/STLExtras.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000022#include "llvm/Support/CommandLine.h"
Chris Lattner75ee2eb2005-10-14 03:54:49 +000023#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner560a79f2004-10-03 19:34:31 +000026static cl::opt<unsigned>
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000027AsmParserNum("asmparsernum", cl::init(0),
28 cl::desc("Make -gen-asm-parser emit assembly parser #N"));
29
30static cl::opt<unsigned>
Chris Lattner560a79f2004-10-03 19:34:31 +000031AsmWriterNum("asmwriternum", cl::init(0),
32 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
33
Owen Anderson825b72b2009-08-11 20:47:22 +000034/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands83ec4b62008-06-06 12:08:01 +000035/// record corresponds to.
Owen Anderson825b72b2009-08-11 20:47:22 +000036MVT::SimpleValueType llvm::getValueType(Record *Rec) {
37 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000038}
39
Owen Anderson825b72b2009-08-11 20:47:22 +000040std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000041 switch (T) {
Owen Anderson825b72b2009-08-11 20:47:22 +000042 case MVT::Other: return "UNKNOWN";
43 case MVT::iPTR: return "TLI.getPointerTy()";
44 case MVT::iPTRAny: return "TLI.getPointerTy()";
Bob Wilsonbbf1caa2009-07-10 22:25:24 +000045 default: return getEnumName(T);
Chris Lattner45872072003-08-07 05:38:11 +000046 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000047}
48
Owen Anderson825b72b2009-08-11 20:47:22 +000049std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000050 switch (T) {
Owen Anderson825b72b2009-08-11 20:47:22 +000051 case MVT::Other: return "MVT::Other";
52 case MVT::i1: return "MVT::i1";
53 case MVT::i8: return "MVT::i8";
54 case MVT::i16: return "MVT::i16";
55 case MVT::i32: return "MVT::i32";
56 case MVT::i64: return "MVT::i64";
57 case MVT::i128: return "MVT::i128";
58 case MVT::iAny: return "MVT::iAny";
59 case MVT::fAny: return "MVT::fAny";
60 case MVT::vAny: return "MVT::vAny";
61 case MVT::f32: return "MVT::f32";
62 case MVT::f64: return "MVT::f64";
63 case MVT::f80: return "MVT::f80";
64 case MVT::f128: return "MVT::f128";
65 case MVT::ppcf128: return "MVT::ppcf128";
66 case MVT::Flag: return "MVT::Flag";
67 case MVT::isVoid:return "MVT::isVoid";
68 case MVT::v2i8: return "MVT::v2i8";
69 case MVT::v4i8: return "MVT::v4i8";
70 case MVT::v8i8: return "MVT::v8i8";
71 case MVT::v16i8: return "MVT::v16i8";
72 case MVT::v32i8: return "MVT::v32i8";
73 case MVT::v2i16: return "MVT::v2i16";
74 case MVT::v4i16: return "MVT::v4i16";
75 case MVT::v8i16: return "MVT::v8i16";
76 case MVT::v16i16: return "MVT::v16i16";
77 case MVT::v2i32: return "MVT::v2i32";
78 case MVT::v4i32: return "MVT::v4i32";
79 case MVT::v8i32: return "MVT::v8i32";
80 case MVT::v1i64: return "MVT::v1i64";
81 case MVT::v2i64: return "MVT::v2i64";
82 case MVT::v4i64: return "MVT::v4i64";
Evan Chengdbf67fe2010-05-13 23:55:47 +000083 case MVT::v8i64: return "MVT::v8i64";
Owen Anderson825b72b2009-08-11 20:47:22 +000084 case MVT::v2f32: return "MVT::v2f32";
85 case MVT::v4f32: return "MVT::v4f32";
86 case MVT::v8f32: return "MVT::v8f32";
87 case MVT::v2f64: return "MVT::v2f64";
88 case MVT::v4f64: return "MVT::v4f64";
89 case MVT::Metadata: return "MVT::Metadata";
90 case MVT::iPTR: return "MVT::iPTR";
91 case MVT::iPTRAny: return "MVT::iPTRAny";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000092 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
93 }
94}
95
Chris Lattner6cefb772008-01-05 22:25:12 +000096/// getQualifiedName - Return the name of the specified record, with a
97/// namespace qualifier if the record contains one.
98///
99std::string llvm::getQualifiedName(const Record *R) {
100 std::string Namespace = R->getValueAsString("Namespace");
101 if (Namespace.empty()) return R->getName();
102 return Namespace + "::" + R->getName();
103}
104
105
106
Chris Lattnerd3464c12003-08-07 23:15:21 +0000107
Chris Lattner45872072003-08-07 05:38:11 +0000108/// getTarget - Return the current instance of the Target class.
109///
Evan Cheng2618d072006-05-17 20:37:59 +0000110CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000111 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000112 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000113 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000114 if (Targets.size() != 1)
115 throw std::string("ERROR: Multiple subclasses of Target defined!");
116 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000117}
118
119
120const std::string &CodeGenTarget::getName() const {
121 return TargetRec->getName();
122}
123
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000124std::string CodeGenTarget::getInstNamespace() const {
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000125 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
Chris Lattner6a91b182010-03-19 01:00:55 +0000126 // Make sure not to pick up "TargetOpcode" by accidentally getting
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000127 // the namespace off the PHI instruction or something.
Chris Lattner6a91b182010-03-19 01:00:55 +0000128 if ((*i)->Namespace != "TargetOpcode")
129 return (*i)->Namespace;
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000130 }
131
Chris Lattner6a91b182010-03-19 01:00:55 +0000132 return "";
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000133}
134
Chris Lattner45872072003-08-07 05:38:11 +0000135Record *CodeGenTarget::getInstructionSet() const {
136 return TargetRec->getValueAsDef("InstructionSet");
137}
Brian Gaeked0fde302003-11-11 22:41:34 +0000138
Chris Lattnerf30187a2010-03-19 00:07:20 +0000139
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000140/// getAsmParser - Return the AssemblyParser definition for this target.
141///
142Record *CodeGenTarget::getAsmParser() const {
143 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
144 if (AsmParserNum >= LI.size())
145 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
146 return LI[AsmParserNum];
147}
148
Chris Lattner175580c2004-08-14 22:50:53 +0000149/// getAsmWriter - Return the AssemblyWriter definition for this target.
150///
151Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000152 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
153 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000154 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000155 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000156}
157
Chris Lattner26693112004-08-16 01:10:21 +0000158void CodeGenTarget::ReadRegisters() const {
159 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
160 if (Regs.empty())
161 throw std::string("No 'Register' subclasses defined!");
162
163 Registers.reserve(Regs.size());
164 Registers.assign(Regs.begin(), Regs.end());
165}
166
Chris Lattner7a680c62004-08-21 02:24:57 +0000167CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
168 DeclaredSpillSize = R->getValueAsInt("SpillSize");
169 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
170}
171
Chris Lattner26693112004-08-16 01:10:21 +0000172const std::string &CodeGenRegister::getName() const {
173 return TheDef->getName();
174}
175
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +0000176void CodeGenTarget::ReadSubRegIndices() const {
177 SubRegIndices = Records.getAllDerivedDefinitions("SubRegIndex");
178}
179
Chris Lattner056afef2004-08-21 04:05:00 +0000180void CodeGenTarget::ReadRegisterClasses() const {
181 std::vector<Record*> RegClasses =
182 Records.getAllDerivedDefinitions("RegisterClass");
183 if (RegClasses.empty())
184 throw std::string("No 'RegisterClass' subclasses defined!");
185
186 RegisterClasses.reserve(RegClasses.size());
187 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
188}
189
Chris Lattner2cacec52010-03-15 06:00:16 +0000190std::vector<MVT::SimpleValueType> CodeGenTarget::
191getRegisterVTs(Record *R) const {
192 std::vector<MVT::SimpleValueType> Result;
Evan Cheng44a65fa2006-05-16 07:05:30 +0000193 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
194 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
195 const CodeGenRegisterClass &RC = RegisterClasses[i];
196 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
197 if (R == RC.Elements[ei]) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Chris Lattner2cacec52010-03-15 06:00:16 +0000199 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Evan Cheng44a65fa2006-05-16 07:05:30 +0000200 }
201 }
202 }
Chris Lattner0d7952e2010-03-27 20:32:26 +0000203
204 // Remove duplicates.
205 array_pod_sort(Result.begin(), Result.end());
206 Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
Evan Cheng44a65fa2006-05-16 07:05:30 +0000207 return Result;
208}
209
210
Chris Lattner056afef2004-08-21 04:05:00 +0000211CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000212 // Rename anonymous register classes.
213 if (R->getName().size() > 9 && R->getName()[9] == '.') {
214 static unsigned AnonCounter = 0;
215 R->setName("AnonRegClass_"+utostr(AnonCounter++));
216 }
217
Nate Begeman6510b222005-12-01 04:51:06 +0000218 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
219 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
220 Record *Type = TypeList[i];
221 if (!Type->isSubClassOf("ValueType"))
222 throw "RegTypes list member '" + Type->getName() +
223 "' does not derive from the ValueType class!";
224 VTs.push_back(getValueType(Type));
225 }
226 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000227
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000228 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
229 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
230 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000231 if (!Reg->isSubClassOf("Register"))
232 throw "Register Class member '" + Reg->getName() +
233 "' does not derive from the Register class!";
234 Elements.push_back(Reg);
235 }
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +0000236
237 // SubRegClasses is a list<dag> containing (RC, subregindex, ...) dags.
238 ListInit *SRC = R->getValueAsListInit("SubRegClasses");
239 for (ListInit::const_iterator i = SRC->begin(), e = SRC->end(); i != e; ++i) {
240 DagInit *DAG = dynamic_cast<DagInit*>(*i);
241 if (!DAG) throw "SubRegClasses must contain DAGs";
242 DefInit *DAGOp = dynamic_cast<DefInit*>(DAG->getOperator());
243 Record *RCRec;
244 if (!DAGOp || !(RCRec = DAGOp->getDef())->isSubClassOf("RegisterClass"))
245 throw "Operator '" + DAG->getOperator()->getAsString() +
246 "' in SubRegClasses is not a RegisterClass";
247 // Iterate over args, all SubRegIndex instances.
248 for (DagInit::const_arg_iterator ai = DAG->arg_begin(), ae = DAG->arg_end();
249 ai != ae; ++ai) {
250 DefInit *Idx = dynamic_cast<DefInit*>(*ai);
251 Record *IdxRec;
252 if (!Idx || !(IdxRec = Idx->getDef())->isSubClassOf("SubRegIndex"))
253 throw "Argument '" + (*ai)->getAsString() +
254 "' in SubRegClasses is not a SubRegIndex";
255 if (!SubRegClasses.insert(std::make_pair(IdxRec, RCRec)).second)
256 throw "SubRegIndex '" + IdxRec->getName() + "' mentioned twice";
257 }
258 }
259
Nate Begeman6510b222005-12-01 04:51:06 +0000260 // Allow targets to override the size in bits of the RegisterClass.
261 unsigned Size = R->getValueAsInt("Size");
262
263 Namespace = R->getValueAsString("Namespace");
Owen Andersone50ed302009-08-10 22:56:29 +0000264 SpillSize = Size ? Size : EVT(VTs[0]).getSizeInBits();
Nate Begeman6510b222005-12-01 04:51:06 +0000265 SpillAlignment = R->getValueAsInt("Alignment");
Evan Chenga3ca3142007-09-19 01:35:01 +0000266 CopyCost = R->getValueAsInt("CopyCost");
Nate Begeman6510b222005-12-01 04:51:06 +0000267 MethodBodies = R->getValueAsCode("MethodBodies");
268 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000269}
270
271const std::string &CodeGenRegisterClass::getName() const {
272 return TheDef->getName();
273}
274
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000275void CodeGenTarget::ReadLegalValueTypes() const {
276 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
277 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000278 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
279 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000280
281 // Remove duplicates.
282 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
283 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
284 LegalValueTypes.end()),
285 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000286}
Chris Lattner056afef2004-08-21 04:05:00 +0000287
Chris Lattner175580c2004-08-14 22:50:53 +0000288
Chris Lattnerec352402004-08-01 05:04:00 +0000289void CodeGenTarget::ReadInstructions() const {
290 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000291 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000292 throw std::string("No 'Instruction' subclasses defined!");
293
Chris Lattneraa77d772006-01-27 01:45:06 +0000294 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000295 std::string InstFormatName =
296 getAsmWriter()->getValueAsString("InstFormatName");
297
298 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
299 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
Chris Lattnere14d2e22010-03-19 01:07:44 +0000300 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i], AsmStr);
Chris Lattner175580c2004-08-14 22:50:53 +0000301 }
Chris Lattnerec352402004-08-01 05:04:00 +0000302}
303
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000304static const CodeGenInstruction *
305GetInstByName(const char *Name,
Chris Lattnere14d2e22010-03-19 01:07:44 +0000306 const DenseMap<const Record*, CodeGenInstruction*> &Insts) {
307 const Record *Rec = Records.getDef(Name);
308
309 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
310 I = Insts.find(Rec);
311 if (Rec == 0 || I == Insts.end())
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000312 throw std::string("Could not find '") + Name + "' instruction!";
Chris Lattnere14d2e22010-03-19 01:07:44 +0000313 return I->second;
314}
315
316namespace {
317/// SortInstByName - Sorting predicate to sort instructions by name.
318///
319struct SortInstByName {
320 bool operator()(const CodeGenInstruction *Rec1,
321 const CodeGenInstruction *Rec2) const {
322 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
323 }
324};
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000325}
326
Chris Lattnerd6488672005-01-22 18:58:51 +0000327/// getInstructionsByEnumValue - Return all of the instructions defined by the
328/// target, ordered by their enum value.
Chris Lattner6a91b182010-03-19 01:00:55 +0000329void CodeGenTarget::ComputeInstrsByEnum() const {
Chris Lattnere14d2e22010-03-19 01:07:44 +0000330 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000331 const CodeGenInstruction *PHI = GetInstByName("PHI", Insts);
332 const CodeGenInstruction *INLINEASM = GetInstByName("INLINEASM", Insts);
333 const CodeGenInstruction *DBG_LABEL = GetInstByName("DBG_LABEL", Insts);
334 const CodeGenInstruction *EH_LABEL = GetInstByName("EH_LABEL", Insts);
335 const CodeGenInstruction *GC_LABEL = GetInstByName("GC_LABEL", Insts);
336 const CodeGenInstruction *KILL = GetInstByName("KILL", Insts);
337 const CodeGenInstruction *EXTRACT_SUBREG =
338 GetInstByName("EXTRACT_SUBREG", Insts);
339 const CodeGenInstruction *INSERT_SUBREG =
340 GetInstByName("INSERT_SUBREG", Insts);
341 const CodeGenInstruction *IMPLICIT_DEF = GetInstByName("IMPLICIT_DEF", Insts);
342 const CodeGenInstruction *SUBREG_TO_REG =
343 GetInstByName("SUBREG_TO_REG", Insts);
344 const CodeGenInstruction *COPY_TO_REGCLASS =
345 GetInstByName("COPY_TO_REGCLASS", Insts);
346 const CodeGenInstruction *DBG_VALUE = GetInstByName("DBG_VALUE", Insts);
Evan Chengb55c8be2010-05-01 00:28:44 +0000347 const CodeGenInstruction *REG_SEQUENCE = GetInstByName("REG_SEQUENCE", Insts);
Dale Johannesen87563b32010-01-08 23:51:25 +0000348
Chris Lattnerd6488672005-01-22 18:58:51 +0000349 // Print out the rest of the instructions now.
Chris Lattnerf6502782010-03-19 00:34:35 +0000350 InstrsByEnum.push_back(PHI);
351 InstrsByEnum.push_back(INLINEASM);
352 InstrsByEnum.push_back(DBG_LABEL);
353 InstrsByEnum.push_back(EH_LABEL);
354 InstrsByEnum.push_back(GC_LABEL);
355 InstrsByEnum.push_back(KILL);
356 InstrsByEnum.push_back(EXTRACT_SUBREG);
357 InstrsByEnum.push_back(INSERT_SUBREG);
358 InstrsByEnum.push_back(IMPLICIT_DEF);
359 InstrsByEnum.push_back(SUBREG_TO_REG);
360 InstrsByEnum.push_back(COPY_TO_REGCLASS);
361 InstrsByEnum.push_back(DBG_VALUE);
Evan Chengb55c8be2010-05-01 00:28:44 +0000362 InstrsByEnum.push_back(REG_SEQUENCE);
Chris Lattner6a91b182010-03-19 01:00:55 +0000363
Chris Lattnere14d2e22010-03-19 01:07:44 +0000364 unsigned EndOfPredefines = InstrsByEnum.size();
Chris Lattner6a91b182010-03-19 01:00:55 +0000365
Chris Lattnere14d2e22010-03-19 01:07:44 +0000366 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
367 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
368 const CodeGenInstruction *CGI = I->second;
Chris Lattner6a91b182010-03-19 01:00:55 +0000369 if (CGI != PHI &&
370 CGI != INLINEASM &&
371 CGI != DBG_LABEL &&
372 CGI != EH_LABEL &&
373 CGI != GC_LABEL &&
374 CGI != KILL &&
375 CGI != EXTRACT_SUBREG &&
376 CGI != INSERT_SUBREG &&
377 CGI != IMPLICIT_DEF &&
378 CGI != SUBREG_TO_REG &&
379 CGI != COPY_TO_REGCLASS &&
Evan Chengb55c8be2010-05-01 00:28:44 +0000380 CGI != DBG_VALUE &&
381 CGI != REG_SEQUENCE)
Chris Lattner6a91b182010-03-19 01:00:55 +0000382 InstrsByEnum.push_back(CGI);
383 }
Chris Lattnere14d2e22010-03-19 01:07:44 +0000384
385 // All of the instructions are now in random order based on the map iteration.
386 // Sort them by name.
387 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(),
388 SortInstByName());
Chris Lattnerd6488672005-01-22 18:58:51 +0000389}
390
391
Misha Brukman35e83cc2004-10-14 05:50:43 +0000392/// isLittleEndianEncoding - Return whether this target encodes its instruction
393/// in little-endian format, i.e. bits laid out in the order [0..n]
394///
395bool CodeGenTarget::isLittleEndianEncoding() const {
396 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
397}
398
Evan Cheng0fc71982005-12-08 02:00:36 +0000399//===----------------------------------------------------------------------===//
400// ComplexPattern implementation
401//
402ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000403 Ty = ::getValueType(R->getValueAsDef("Ty"));
404 NumOperands = R->getValueAsInt("NumOperands");
405 SelectFunc = R->getValueAsString("SelectFunc");
406 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000407
408 // Parse the properties.
409 Properties = 0;
410 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
411 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
412 if (PropList[i]->getName() == "SDNPHasChain") {
413 Properties |= 1 << SDNPHasChain;
414 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
415 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerba7e7562008-01-10 07:59:24 +0000416 } else if (PropList[i]->getName() == "SDNPMayStore") {
417 Properties |= 1 << SDNPMayStore;
418 } else if (PropList[i]->getName() == "SDNPMayLoad") {
419 Properties |= 1 << SDNPMayLoad;
420 } else if (PropList[i]->getName() == "SDNPSideEffect") {
421 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000422 } else if (PropList[i]->getName() == "SDNPMemOperand") {
423 Properties |= 1 << SDNPMemOperand;
Chris Lattnere8cabf32010-03-19 05:07:09 +0000424 } else if (PropList[i]->getName() == "SDNPVariadic") {
425 Properties |= 1 << SDNPVariadic;
Evan Cheng94b30402006-10-11 21:02:01 +0000426 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000427 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
428 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000429 exit(1);
430 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000431}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000432
Chris Lattner43fbbc32006-03-24 19:49:31 +0000433//===----------------------------------------------------------------------===//
434// CodeGenIntrinsic Implementation
435//===----------------------------------------------------------------------===//
436
Dale Johannesen49de9822009-02-05 01:49:45 +0000437std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
438 bool TargetOnly) {
Chris Lattner43fbbc32006-03-24 19:49:31 +0000439 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000440
441 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000442
Dale Johannesen49de9822009-02-05 01:49:45 +0000443 for (unsigned i = 0, e = I.size(); i != e; ++i) {
444 bool isTarget = I[i]->getValueAsBit("isTarget");
445 if (isTarget == TargetOnly)
446 Result.push_back(CodeGenIntrinsic(I[i]));
447 }
Chris Lattner8850a1b2006-03-27 22:48:18 +0000448 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000449}
450
Dan Gohmanee4fa192008-04-03 00:02:49 +0000451CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000452 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000453 std::string DefName = R->getName();
454 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000455 isOverloaded = false;
Evan Cheng6bd95672008-06-16 20:29:38 +0000456 isCommutative = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000457
458 if (DefName.size() <= 4 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000459 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000460 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000461
Chris Lattner43fbbc32006-03-24 19:49:31 +0000462 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000463
Chris Lattner43fbbc32006-03-24 19:49:31 +0000464 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
465 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000466
467 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000468 Name = R->getValueAsString("LLVMName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000469
Chris Lattner43fbbc32006-03-24 19:49:31 +0000470 if (Name == "") {
471 // If an explicit name isn't specified, derive one from the DefName.
472 Name = "llvm.";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000473
Chris Lattner43fbbc32006-03-24 19:49:31 +0000474 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000475 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner43fbbc32006-03-24 19:49:31 +0000476 } else {
477 // Verify it starts with "llvm.".
478 if (Name.size() <= 5 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000479 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000480 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
481 }
482
483 // If TargetPrefix is specified, make sure that Name starts with
484 // "llvm.<targetprefix>.".
485 if (!TargetPrefix.empty()) {
486 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000487 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
488 != (TargetPrefix + "."))
489 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Chris Lattner43fbbc32006-03-24 19:49:31 +0000490 TargetPrefix + ".'!";
491 }
492
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000493 // Parse the list of return types.
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000495 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000496 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000497 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000498 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000500 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsond305e702009-04-16 21:51:05 +0000501 unsigned MatchTy = TyEl->getValueAsInt("Number");
502 assert(MatchTy < OverloadedVTs.size() &&
503 "Invalid matching number!");
504 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000505 // It only makes sense to use the extended and truncated vector element
506 // variants with iAny types; otherwise, if the intrinsic is not
507 // overloaded, all the types can be specified directly.
508 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
509 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000511 "Expected iAny or vAny type");
Bob Wilsond305e702009-04-16 21:51:05 +0000512 } else {
Bob Wilsonbc039792009-01-07 00:09:01 +0000513 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsond305e702009-04-16 21:51:05 +0000514 }
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000515 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000516 OverloadedVTs.push_back(VT);
Chris Lattner9972aff2010-03-23 23:46:27 +0000517 isOverloaded = true;
Bob Wilsond305e702009-04-16 21:51:05 +0000518 }
Chris Lattner9972aff2010-03-23 23:46:27 +0000519
520 // Reject invalid types.
521 if (VT == MVT::isVoid)
522 throw "Intrinsic '" + DefName + " has void in result type list!";
Chris Lattner93dc92e2010-03-22 20:56:36 +0000523
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000524 IS.RetVTs.push_back(VT);
525 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000526 }
Chris Lattner93dc92e2010-03-22 20:56:36 +0000527
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000528 // Parse the list of parameter types.
529 TypeList = R->getValueAsListInit("ParamTypes");
530 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
531 Record *TyEl = TypeList->getElementAsRecord(i);
532 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000534 if (TyEl->isSubClassOf("LLVMMatchType")) {
535 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsond305e702009-04-16 21:51:05 +0000536 assert(MatchTy < OverloadedVTs.size() &&
537 "Invalid matching number!");
538 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000539 // It only makes sense to use the extended and truncated vector element
540 // variants with iAny types; otherwise, if the intrinsic is not
541 // overloaded, all the types can be specified directly.
542 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
543 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000545 "Expected iAny or vAny type");
Bob Wilsonbc039792009-01-07 00:09:01 +0000546 } else
547 VT = getValueType(TyEl->getValueAsDef("VT"));
Chris Lattner9972aff2010-03-23 23:46:27 +0000548
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000549 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000550 OverloadedVTs.push_back(VT);
Chris Lattner9972aff2010-03-23 23:46:27 +0000551 isOverloaded = true;
Bob Wilsond305e702009-04-16 21:51:05 +0000552 }
Chris Lattner9972aff2010-03-23 23:46:27 +0000553
554 // Reject invalid types.
555 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
556 throw "Intrinsic '" + DefName + " has void in result type list!";
557
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000558 IS.ParamVTs.push_back(VT);
559 IS.ParamTypeDefs.push_back(TyEl);
560 }
561
Chris Lattner43fbbc32006-03-24 19:49:31 +0000562 // Parse the intrinsic properties.
563 ListInit *PropList = R->getValueAsListInit("Properties");
564 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000565 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000566 assert(Property->isSubClassOf("IntrinsicProperty") &&
567 "Expected a property!");
568
Chris Lattner4b2362e2006-04-10 22:02:59 +0000569 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000570 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000571 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000572 ModRef = ReadArgMem;
573 else if (Property->getName() == "IntrReadMem")
574 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000575 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000576 ModRef = WriteArgMem;
577 else if (Property->getName() == "IntrWriteMem")
578 ModRef = WriteMem;
Evan Cheng6bd95672008-06-16 20:29:38 +0000579 else if (Property->getName() == "Commutative")
580 isCommutative = true;
Chris Lattnera62c3022009-01-12 01:12:03 +0000581 else if (Property->isSubClassOf("NoCapture")) {
582 unsigned ArgNo = Property->getValueAsInt("ArgNo");
583 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
584 } else
Chris Lattner43fbbc32006-03-24 19:49:31 +0000585 assert(0 && "Unknown property!");
586 }
587}