blob: 499d8aca9e16c23cdb5b7aa46fd05fdc2e6b8b2d [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"
21#include "llvm/Support/CommandLine.h"
Chris Lattner75ee2eb2005-10-14 03:54:49 +000022#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner560a79f2004-10-03 19:34:31 +000025static cl::opt<unsigned>
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000026AsmParserNum("asmparsernum", cl::init(0),
27 cl::desc("Make -gen-asm-parser emit assembly parser #N"));
28
29static cl::opt<unsigned>
Chris Lattner560a79f2004-10-03 19:34:31 +000030AsmWriterNum("asmwriternum", cl::init(0),
31 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
32
Owen Anderson825b72b2009-08-11 20:47:22 +000033/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands83ec4b62008-06-06 12:08:01 +000034/// record corresponds to.
Owen Anderson825b72b2009-08-11 20:47:22 +000035MVT::SimpleValueType llvm::getValueType(Record *Rec) {
36 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000037}
38
Owen Anderson825b72b2009-08-11 20:47:22 +000039std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000040 switch (T) {
Owen Anderson825b72b2009-08-11 20:47:22 +000041 case MVT::Other: return "UNKNOWN";
42 case MVT::iPTR: return "TLI.getPointerTy()";
43 case MVT::iPTRAny: return "TLI.getPointerTy()";
Bob Wilsonbbf1caa2009-07-10 22:25:24 +000044 default: return getEnumName(T);
Chris Lattner45872072003-08-07 05:38:11 +000045 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000046}
47
Owen Anderson825b72b2009-08-11 20:47:22 +000048std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000049 switch (T) {
Owen Anderson825b72b2009-08-11 20:47:22 +000050 case MVT::Other: return "MVT::Other";
51 case MVT::i1: return "MVT::i1";
52 case MVT::i8: return "MVT::i8";
53 case MVT::i16: return "MVT::i16";
54 case MVT::i32: return "MVT::i32";
55 case MVT::i64: return "MVT::i64";
56 case MVT::i128: return "MVT::i128";
57 case MVT::iAny: return "MVT::iAny";
58 case MVT::fAny: return "MVT::fAny";
59 case MVT::vAny: return "MVT::vAny";
60 case MVT::f32: return "MVT::f32";
61 case MVT::f64: return "MVT::f64";
62 case MVT::f80: return "MVT::f80";
63 case MVT::f128: return "MVT::f128";
64 case MVT::ppcf128: return "MVT::ppcf128";
65 case MVT::Flag: return "MVT::Flag";
66 case MVT::isVoid:return "MVT::isVoid";
67 case MVT::v2i8: return "MVT::v2i8";
68 case MVT::v4i8: return "MVT::v4i8";
69 case MVT::v8i8: return "MVT::v8i8";
70 case MVT::v16i8: return "MVT::v16i8";
71 case MVT::v32i8: return "MVT::v32i8";
72 case MVT::v2i16: return "MVT::v2i16";
73 case MVT::v4i16: return "MVT::v4i16";
74 case MVT::v8i16: return "MVT::v8i16";
75 case MVT::v16i16: return "MVT::v16i16";
76 case MVT::v2i32: return "MVT::v2i32";
77 case MVT::v4i32: return "MVT::v4i32";
78 case MVT::v8i32: return "MVT::v8i32";
79 case MVT::v1i64: return "MVT::v1i64";
80 case MVT::v2i64: return "MVT::v2i64";
81 case MVT::v4i64: return "MVT::v4i64";
82 case MVT::v2f32: return "MVT::v2f32";
83 case MVT::v4f32: return "MVT::v4f32";
84 case MVT::v8f32: return "MVT::v8f32";
85 case MVT::v2f64: return "MVT::v2f64";
86 case MVT::v4f64: return "MVT::v4f64";
87 case MVT::Metadata: return "MVT::Metadata";
88 case MVT::iPTR: return "MVT::iPTR";
89 case MVT::iPTRAny: return "MVT::iPTRAny";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000090 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
91 }
92}
93
Chris Lattner6cefb772008-01-05 22:25:12 +000094/// getQualifiedName - Return the name of the specified record, with a
95/// namespace qualifier if the record contains one.
96///
97std::string llvm::getQualifiedName(const Record *R) {
98 std::string Namespace = R->getValueAsString("Namespace");
99 if (Namespace.empty()) return R->getName();
100 return Namespace + "::" + R->getName();
101}
102
103
104
Chris Lattnerd3464c12003-08-07 23:15:21 +0000105
Chris Lattner45872072003-08-07 05:38:11 +0000106/// getTarget - Return the current instance of the Target class.
107///
Evan Cheng2618d072006-05-17 20:37:59 +0000108CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000109 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000110 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000111 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000112 if (Targets.size() != 1)
113 throw std::string("ERROR: Multiple subclasses of Target defined!");
114 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000115}
116
117
118const std::string &CodeGenTarget::getName() const {
119 return TargetRec->getName();
120}
121
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000122std::string CodeGenTarget::getInstNamespace() const {
123 std::string InstNS;
124
125 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
126 InstNS = i->second.Namespace;
127
128 // Make sure not to pick up "TargetInstrInfo" by accidentally getting
129 // the namespace off the PHI instruction or something.
130 if (InstNS != "TargetInstrInfo")
131 break;
132 }
133
134 return InstNS;
135}
136
Chris Lattner45872072003-08-07 05:38:11 +0000137Record *CodeGenTarget::getInstructionSet() const {
138 return TargetRec->getValueAsDef("InstructionSet");
139}
Brian Gaeked0fde302003-11-11 22:41:34 +0000140
Chris Lattnerf30187a2010-03-19 00:07:20 +0000141
142CodeGenInstruction &CodeGenTarget::getInstruction(const Record *InstRec) const {
143 return getInstruction(InstRec->getName());
144}
145
146
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000147/// getAsmParser - Return the AssemblyParser definition for this target.
148///
149Record *CodeGenTarget::getAsmParser() const {
150 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
151 if (AsmParserNum >= LI.size())
152 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
153 return LI[AsmParserNum];
154}
155
Chris Lattner175580c2004-08-14 22:50:53 +0000156/// getAsmWriter - Return the AssemblyWriter definition for this target.
157///
158Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000159 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
160 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000161 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000162 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000163}
164
Chris Lattner26693112004-08-16 01:10:21 +0000165void CodeGenTarget::ReadRegisters() const {
166 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
167 if (Regs.empty())
168 throw std::string("No 'Register' subclasses defined!");
169
170 Registers.reserve(Regs.size());
171 Registers.assign(Regs.begin(), Regs.end());
172}
173
Chris Lattner7a680c62004-08-21 02:24:57 +0000174CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
175 DeclaredSpillSize = R->getValueAsInt("SpillSize");
176 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
177}
178
Chris Lattner26693112004-08-16 01:10:21 +0000179const std::string &CodeGenRegister::getName() const {
180 return TheDef->getName();
181}
182
Chris Lattner056afef2004-08-21 04:05:00 +0000183void CodeGenTarget::ReadRegisterClasses() const {
184 std::vector<Record*> RegClasses =
185 Records.getAllDerivedDefinitions("RegisterClass");
186 if (RegClasses.empty())
187 throw std::string("No 'RegisterClass' subclasses defined!");
188
189 RegisterClasses.reserve(RegClasses.size());
190 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
191}
192
Chris Lattner2cacec52010-03-15 06:00:16 +0000193std::vector<MVT::SimpleValueType> CodeGenTarget::
194getRegisterVTs(Record *R) const {
195 std::vector<MVT::SimpleValueType> Result;
Evan Cheng44a65fa2006-05-16 07:05:30 +0000196 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
197 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
198 const CodeGenRegisterClass &RC = RegisterClasses[i];
199 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
200 if (R == RC.Elements[ei]) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Chris Lattner2cacec52010-03-15 06:00:16 +0000202 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Evan Cheng44a65fa2006-05-16 07:05:30 +0000203 }
204 }
205 }
206 return Result;
207}
208
209
Chris Lattner056afef2004-08-21 04:05:00 +0000210CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000211 // Rename anonymous register classes.
212 if (R->getName().size() > 9 && R->getName()[9] == '.') {
213 static unsigned AnonCounter = 0;
214 R->setName("AnonRegClass_"+utostr(AnonCounter++));
215 }
216
Nate Begeman6510b222005-12-01 04:51:06 +0000217 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
218 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
219 Record *Type = TypeList[i];
220 if (!Type->isSubClassOf("ValueType"))
221 throw "RegTypes list member '" + Type->getName() +
222 "' does not derive from the ValueType class!";
223 VTs.push_back(getValueType(Type));
224 }
225 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000226
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000227 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
228 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
229 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000230 if (!Reg->isSubClassOf("Register"))
231 throw "Register Class member '" + Reg->getName() +
232 "' does not derive from the Register class!";
233 Elements.push_back(Reg);
234 }
Nate Begeman6510b222005-12-01 04:51:06 +0000235
Christopher Lamba3211252007-06-13 22:20:15 +0000236 std::vector<Record*> SubRegClassList =
237 R->getValueAsListOfDefs("SubRegClassList");
238 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
239 Record *SubRegClass = SubRegClassList[i];
240 if (!SubRegClass->isSubClassOf("RegisterClass"))
241 throw "Register Class member '" + SubRegClass->getName() +
242 "' does not derive from the RegisterClass class!";
243 SubRegClasses.push_back(SubRegClass);
244 }
245
Nate Begeman6510b222005-12-01 04:51:06 +0000246 // Allow targets to override the size in bits of the RegisterClass.
247 unsigned Size = R->getValueAsInt("Size");
248
249 Namespace = R->getValueAsString("Namespace");
Owen Andersone50ed302009-08-10 22:56:29 +0000250 SpillSize = Size ? Size : EVT(VTs[0]).getSizeInBits();
Nate Begeman6510b222005-12-01 04:51:06 +0000251 SpillAlignment = R->getValueAsInt("Alignment");
Evan Chenga3ca3142007-09-19 01:35:01 +0000252 CopyCost = R->getValueAsInt("CopyCost");
Nate Begeman6510b222005-12-01 04:51:06 +0000253 MethodBodies = R->getValueAsCode("MethodBodies");
254 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000255}
256
257const std::string &CodeGenRegisterClass::getName() const {
258 return TheDef->getName();
259}
260
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000261void CodeGenTarget::ReadLegalValueTypes() const {
262 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
263 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000264 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
265 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000266
267 // Remove duplicates.
268 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
269 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
270 LegalValueTypes.end()),
271 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000272}
Chris Lattner056afef2004-08-21 04:05:00 +0000273
Chris Lattner175580c2004-08-14 22:50:53 +0000274
Chris Lattnerec352402004-08-01 05:04:00 +0000275void CodeGenTarget::ReadInstructions() const {
276 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000277 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000278 throw std::string("No 'Instruction' subclasses defined!");
279
Chris Lattneraa77d772006-01-27 01:45:06 +0000280 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000281 std::string InstFormatName =
282 getAsmWriter()->getValueAsString("InstFormatName");
283
284 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
285 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
286 Instructions.insert(std::make_pair(Insts[i]->getName(),
287 CodeGenInstruction(Insts[i], AsmStr)));
288 }
Chris Lattnerec352402004-08-01 05:04:00 +0000289}
290
Chris Lattnerd6488672005-01-22 18:58:51 +0000291/// getInstructionsByEnumValue - Return all of the instructions defined by the
292/// target, ordered by their enum value.
293void CodeGenTarget::
294getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
295 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000296 std::map<std::string, CodeGenInstruction>::const_iterator I;
297 I = getInstructions().find("PHI");
298 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
299 const CodeGenInstruction *PHI = &I->second;
300
301 I = getInstructions().find("INLINEASM");
302 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
303 const CodeGenInstruction *INLINEASM = &I->second;
304
Dan Gohman44066042008-07-01 00:05:16 +0000305 I = getInstructions().find("DBG_LABEL");
306 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
307 const CodeGenInstruction *DBG_LABEL = &I->second;
308
309 I = getInstructions().find("EH_LABEL");
310 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
311 const CodeGenInstruction *EH_LABEL = &I->second;
312
313 I = getInstructions().find("GC_LABEL");
314 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
315 const CodeGenInstruction *GC_LABEL = &I->second;
Jim Laskeya683f9b2007-01-26 17:29:20 +0000316
Jakob Stoklund Olesen26207e52009-09-28 20:32:26 +0000317 I = getInstructions().find("KILL");
318 if (I == Instructions.end()) throw "Could not find 'KILL' instruction!";
319 const CodeGenInstruction *KILL = &I->second;
Evan Chenga844bde2008-02-02 04:07:54 +0000320
Christopher Lamb08d52072007-07-26 07:48:21 +0000321 I = getInstructions().find("EXTRACT_SUBREG");
322 if (I == Instructions.end())
323 throw "Could not find 'EXTRACT_SUBREG' instruction!";
324 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
325
326 I = getInstructions().find("INSERT_SUBREG");
327 if (I == Instructions.end())
328 throw "Could not find 'INSERT_SUBREG' instruction!";
329 const CodeGenInstruction *INSERT_SUBREG = &I->second;
330
Evan Chengda47e6e2008-03-15 00:03:38 +0000331 I = getInstructions().find("IMPLICIT_DEF");
332 if (I == Instructions.end())
333 throw "Could not find 'IMPLICIT_DEF' instruction!";
334 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
335
Christopher Lambc9298232008-03-16 03:12:01 +0000336 I = getInstructions().find("SUBREG_TO_REG");
337 if (I == Instructions.end())
338 throw "Could not find 'SUBREG_TO_REG' instruction!";
339 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000340
Dan Gohman88c7af02009-04-13 21:06:25 +0000341 I = getInstructions().find("COPY_TO_REGCLASS");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000342 if (I == Instructions.end())
Dan Gohman88c7af02009-04-13 21:06:25 +0000343 throw "Could not find 'COPY_TO_REGCLASS' instruction!";
344 const CodeGenInstruction *COPY_TO_REGCLASS = &I->second;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000345
Chris Lattner518bb532010-02-09 19:54:29 +0000346 I = getInstructions().find("DBG_VALUE");
Dale Johannesen87563b32010-01-08 23:51:25 +0000347 if (I == Instructions.end())
Chris Lattner518bb532010-02-09 19:54:29 +0000348 throw "Could not find 'DBG_VALUE' instruction!";
349 const CodeGenInstruction *DBG_VALUE = &I->second;
Dale Johannesen87563b32010-01-08 23:51:25 +0000350
Chris Lattnerd6488672005-01-22 18:58:51 +0000351 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000352 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000353 NumberedInstructions.push_back(INLINEASM);
Dan Gohman44066042008-07-01 00:05:16 +0000354 NumberedInstructions.push_back(DBG_LABEL);
355 NumberedInstructions.push_back(EH_LABEL);
356 NumberedInstructions.push_back(GC_LABEL);
Jakob Stoklund Olesen26207e52009-09-28 20:32:26 +0000357 NumberedInstructions.push_back(KILL);
Christopher Lamb08d52072007-07-26 07:48:21 +0000358 NumberedInstructions.push_back(EXTRACT_SUBREG);
359 NumberedInstructions.push_back(INSERT_SUBREG);
Evan Chengda47e6e2008-03-15 00:03:38 +0000360 NumberedInstructions.push_back(IMPLICIT_DEF);
Christopher Lambc9298232008-03-16 03:12:01 +0000361 NumberedInstructions.push_back(SUBREG_TO_REG);
Dan Gohman88c7af02009-04-13 21:06:25 +0000362 NumberedInstructions.push_back(COPY_TO_REGCLASS);
Chris Lattner518bb532010-02-09 19:54:29 +0000363 NumberedInstructions.push_back(DBG_VALUE);
Chris Lattnerd6488672005-01-22 18:58:51 +0000364 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000365 if (&II->second != PHI &&
366 &II->second != INLINEASM &&
Dan Gohman44066042008-07-01 00:05:16 +0000367 &II->second != DBG_LABEL &&
368 &II->second != EH_LABEL &&
369 &II->second != GC_LABEL &&
Jakob Stoklund Olesen26207e52009-09-28 20:32:26 +0000370 &II->second != KILL &&
Christopher Lamb08d52072007-07-26 07:48:21 +0000371 &II->second != EXTRACT_SUBREG &&
Evan Chengda47e6e2008-03-15 00:03:38 +0000372 &II->second != INSERT_SUBREG &&
Christopher Lambc9298232008-03-16 03:12:01 +0000373 &II->second != IMPLICIT_DEF &&
Dan Gohmanf8c73942009-04-13 15:38:05 +0000374 &II->second != SUBREG_TO_REG &&
Dale Johannesen87563b32010-01-08 23:51:25 +0000375 &II->second != COPY_TO_REGCLASS &&
Chris Lattner518bb532010-02-09 19:54:29 +0000376 &II->second != DBG_VALUE)
Chris Lattnerd6488672005-01-22 18:58:51 +0000377 NumberedInstructions.push_back(&II->second);
378}
379
380
Misha Brukman35e83cc2004-10-14 05:50:43 +0000381/// isLittleEndianEncoding - Return whether this target encodes its instruction
382/// in little-endian format, i.e. bits laid out in the order [0..n]
383///
384bool CodeGenTarget::isLittleEndianEncoding() const {
385 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
386}
387
Evan Cheng0fc71982005-12-08 02:00:36 +0000388//===----------------------------------------------------------------------===//
389// ComplexPattern implementation
390//
391ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000392 Ty = ::getValueType(R->getValueAsDef("Ty"));
393 NumOperands = R->getValueAsInt("NumOperands");
394 SelectFunc = R->getValueAsString("SelectFunc");
395 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000396
397 // Parse the properties.
398 Properties = 0;
399 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
400 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
401 if (PropList[i]->getName() == "SDNPHasChain") {
402 Properties |= 1 << SDNPHasChain;
403 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
404 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerba7e7562008-01-10 07:59:24 +0000405 } else if (PropList[i]->getName() == "SDNPMayStore") {
406 Properties |= 1 << SDNPMayStore;
407 } else if (PropList[i]->getName() == "SDNPMayLoad") {
408 Properties |= 1 << SDNPMayLoad;
409 } else if (PropList[i]->getName() == "SDNPSideEffect") {
410 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000411 } else if (PropList[i]->getName() == "SDNPMemOperand") {
412 Properties |= 1 << SDNPMemOperand;
Evan Cheng94b30402006-10-11 21:02:01 +0000413 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000414 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
415 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000416 exit(1);
417 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000418}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000419
Chris Lattner43fbbc32006-03-24 19:49:31 +0000420//===----------------------------------------------------------------------===//
421// CodeGenIntrinsic Implementation
422//===----------------------------------------------------------------------===//
423
Dale Johannesen49de9822009-02-05 01:49:45 +0000424std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
425 bool TargetOnly) {
Chris Lattner43fbbc32006-03-24 19:49:31 +0000426 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000427
428 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000429
Dale Johannesen49de9822009-02-05 01:49:45 +0000430 for (unsigned i = 0, e = I.size(); i != e; ++i) {
431 bool isTarget = I[i]->getValueAsBit("isTarget");
432 if (isTarget == TargetOnly)
433 Result.push_back(CodeGenIntrinsic(I[i]));
434 }
Chris Lattner8850a1b2006-03-27 22:48:18 +0000435 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000436}
437
Dan Gohmanee4fa192008-04-03 00:02:49 +0000438CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000439 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000440 std::string DefName = R->getName();
441 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000442 isOverloaded = false;
Evan Cheng6bd95672008-06-16 20:29:38 +0000443 isCommutative = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000444
445 if (DefName.size() <= 4 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000446 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000447 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000448
Chris Lattner43fbbc32006-03-24 19:49:31 +0000449 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000450
Chris Lattner43fbbc32006-03-24 19:49:31 +0000451 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
452 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000453
454 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000455 Name = R->getValueAsString("LLVMName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000456
Chris Lattner43fbbc32006-03-24 19:49:31 +0000457 if (Name == "") {
458 // If an explicit name isn't specified, derive one from the DefName.
459 Name = "llvm.";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000460
Chris Lattner43fbbc32006-03-24 19:49:31 +0000461 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000462 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner43fbbc32006-03-24 19:49:31 +0000463 } else {
464 // Verify it starts with "llvm.".
465 if (Name.size() <= 5 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000466 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000467 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
468 }
469
470 // If TargetPrefix is specified, make sure that Name starts with
471 // "llvm.<targetprefix>.".
472 if (!TargetPrefix.empty()) {
473 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000474 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
475 != (TargetPrefix + "."))
476 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Chris Lattner43fbbc32006-03-24 19:49:31 +0000477 TargetPrefix + ".'!";
478 }
479
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000480 // Parse the list of return types.
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000482 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000483 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000484 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000485 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000487 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsond305e702009-04-16 21:51:05 +0000488 unsigned MatchTy = TyEl->getValueAsInt("Number");
489 assert(MatchTy < OverloadedVTs.size() &&
490 "Invalid matching number!");
491 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000492 // It only makes sense to use the extended and truncated vector element
493 // variants with iAny types; otherwise, if the intrinsic is not
494 // overloaded, all the types can be specified directly.
495 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
496 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000498 "Expected iAny or vAny type");
Bob Wilsond305e702009-04-16 21:51:05 +0000499 } else {
Bob Wilsonbc039792009-01-07 00:09:01 +0000500 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsond305e702009-04-16 21:51:05 +0000501 }
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000502 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000503 OverloadedVTs.push_back(VT);
504 isOverloaded |= true;
505 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000506 IS.RetVTs.push_back(VT);
507 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000508 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000509
510 if (IS.RetVTs.size() == 0)
Chris Lattner43fbbc32006-03-24 19:49:31 +0000511 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000512
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000513 // Parse the list of parameter types.
514 TypeList = R->getValueAsListInit("ParamTypes");
515 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
516 Record *TyEl = TypeList->getElementAsRecord(i);
517 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000519 if (TyEl->isSubClassOf("LLVMMatchType")) {
520 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsond305e702009-04-16 21:51:05 +0000521 assert(MatchTy < OverloadedVTs.size() &&
522 "Invalid matching number!");
523 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000524 // It only makes sense to use the extended and truncated vector element
525 // variants with iAny types; otherwise, if the intrinsic is not
526 // overloaded, all the types can be specified directly.
527 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
528 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000530 "Expected iAny or vAny type");
Bob Wilsonbc039792009-01-07 00:09:01 +0000531 } else
532 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000533 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000534 OverloadedVTs.push_back(VT);
535 isOverloaded |= true;
536 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000537 IS.ParamVTs.push_back(VT);
538 IS.ParamTypeDefs.push_back(TyEl);
539 }
540
Chris Lattner43fbbc32006-03-24 19:49:31 +0000541 // Parse the intrinsic properties.
542 ListInit *PropList = R->getValueAsListInit("Properties");
543 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000544 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000545 assert(Property->isSubClassOf("IntrinsicProperty") &&
546 "Expected a property!");
547
Chris Lattner4b2362e2006-04-10 22:02:59 +0000548 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000549 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000550 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000551 ModRef = ReadArgMem;
552 else if (Property->getName() == "IntrReadMem")
553 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000554 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000555 ModRef = WriteArgMem;
556 else if (Property->getName() == "IntrWriteMem")
557 ModRef = WriteMem;
Evan Cheng6bd95672008-06-16 20:29:38 +0000558 else if (Property->getName() == "Commutative")
559 isCommutative = true;
Chris Lattnera62c3022009-01-12 01:12:03 +0000560 else if (Property->isSubClassOf("NoCapture")) {
561 unsigned ArgNo = Property->getValueAsInt("ArgNo");
562 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
563 } else
Chris Lattner43fbbc32006-03-24 19:49:31 +0000564 assert(0 && "Unknown property!");
565 }
566}