blob: d9f4c3e341350d425f7eba3765b74003dfb9fd0d [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"
Bill Wendlingf5da1332006-12-07 22:21:48 +000022#include "llvm/Support/Streams.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>
27AsmWriterNum("asmwriternum", cl::init(0),
28 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
29
Duncan Sands83ec4b62008-06-06 12:08:01 +000030/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
31/// record corresponds to.
32MVT::SimpleValueType llvm::getValueType(Record *Rec) {
33 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000034}
35
Duncan Sands83ec4b62008-06-06 12:08:01 +000036std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000037 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000038 case MVT::Other: return "UNKNOWN";
Evan Chengd7c2c862006-06-15 00:16:37 +000039 case MVT::i1: return "MVT::i1";
40 case MVT::i8: return "MVT::i8";
41 case MVT::i16: return "MVT::i16";
42 case MVT::i32: return "MVT::i32";
43 case MVT::i64: return "MVT::i64";
44 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000045 case MVT::iAny: return "MVT::iAny";
Dan Gohman0fee3ff2007-08-16 21:57:19 +000046 case MVT::fAny: return "MVT::fAny";
Evan Chengd7c2c862006-06-15 00:16:37 +000047 case MVT::f32: return "MVT::f32";
48 case MVT::f64: return "MVT::f64";
49 case MVT::f80: return "MVT::f80";
50 case MVT::f128: return "MVT::f128";
Dale Johannesen317096a2007-09-28 01:08:20 +000051 case MVT::ppcf128: return "MVT::ppcf128";
Evan Chengd7c2c862006-06-15 00:16:37 +000052 case MVT::Flag: return "MVT::Flag";
Dan Gohmanba6783e2008-08-20 01:44:30 +000053 case MVT::isVoid:return "MVT::isVoid";
Evan Chengd7c2c862006-06-15 00:16:37 +000054 case MVT::v8i8: return "MVT::v8i8";
55 case MVT::v4i16: return "MVT::v4i16";
56 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000057 case MVT::v1i64: return "MVT::v1i64";
Evan Chengd7c2c862006-06-15 00:16:37 +000058 case MVT::v16i8: return "MVT::v16i8";
59 case MVT::v8i16: return "MVT::v8i16";
60 case MVT::v4i32: return "MVT::v4i32";
61 case MVT::v2i64: return "MVT::v2i64";
62 case MVT::v2f32: return "MVT::v2f32";
63 case MVT::v4f32: return "MVT::v4f32";
64 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +000065 case MVT::v3i32: return "MVT::v3i32";
66 case MVT::v3f32: return "MVT::v3f32";
Evan Cheng6b125162006-05-17 20:55:51 +000067 case MVT::iPTR: return "TLI.getPointerTy()";
Mon P Wange3b3a722008-07-30 04:36:53 +000068 case MVT::iPTRAny: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000069 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000070 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000071}
72
Duncan Sands83ec4b62008-06-06 12:08:01 +000073std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000074 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000075 case MVT::Other: return "MVT::Other";
76 case MVT::i1: return "MVT::i1";
77 case MVT::i8: return "MVT::i8";
78 case MVT::i16: return "MVT::i16";
79 case MVT::i32: return "MVT::i32";
80 case MVT::i64: return "MVT::i64";
81 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000082 case MVT::iAny: return "MVT::iAny";
Dan Gohman0fee3ff2007-08-16 21:57:19 +000083 case MVT::fAny: return "MVT::fAny";
Evan Cheng2618d072006-05-17 20:37:59 +000084 case MVT::f32: return "MVT::f32";
85 case MVT::f64: return "MVT::f64";
86 case MVT::f80: return "MVT::f80";
87 case MVT::f128: return "MVT::f128";
Dale Johannesen317096a2007-09-28 01:08:20 +000088 case MVT::ppcf128: return "MVT::ppcf128";
Evan Cheng2618d072006-05-17 20:37:59 +000089 case MVT::Flag: return "MVT::Flag";
90 case MVT::isVoid:return "MVT::isVoid";
91 case MVT::v8i8: return "MVT::v8i8";
92 case MVT::v4i16: return "MVT::v4i16";
93 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000094 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +000095 case MVT::v16i8: return "MVT::v16i8";
96 case MVT::v8i16: return "MVT::v8i16";
97 case MVT::v4i32: return "MVT::v4i32";
98 case MVT::v2i64: return "MVT::v2i64";
99 case MVT::v2f32: return "MVT::v2f32";
100 case MVT::v4f32: return "MVT::v4f32";
101 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +0000102 case MVT::v3i32: return "MVT::v3i32";
103 case MVT::v3f32: return "MVT::v3f32";
Chandler Carruth69940402007-08-04 01:51:18 +0000104 case MVT::iPTR: return "MVT::iPTR";
Mon P Wange3b3a722008-07-30 04:36:53 +0000105 case MVT::iPTRAny: return "MVT::iPTRAny";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +0000106 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
107 }
108}
109
Chris Lattner6cefb772008-01-05 22:25:12 +0000110/// getQualifiedName - Return the name of the specified record, with a
111/// namespace qualifier if the record contains one.
112///
113std::string llvm::getQualifiedName(const Record *R) {
114 std::string Namespace = R->getValueAsString("Namespace");
115 if (Namespace.empty()) return R->getName();
116 return Namespace + "::" + R->getName();
117}
118
119
120
Chris Lattnerd3464c12003-08-07 23:15:21 +0000121
Chris Lattner45872072003-08-07 05:38:11 +0000122/// getTarget - Return the current instance of the Target class.
123///
Evan Cheng2618d072006-05-17 20:37:59 +0000124CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000125 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000126 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000127 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000128 if (Targets.size() != 1)
129 throw std::string("ERROR: Multiple subclasses of Target defined!");
130 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000131}
132
133
134const std::string &CodeGenTarget::getName() const {
135 return TargetRec->getName();
136}
137
138Record *CodeGenTarget::getInstructionSet() const {
139 return TargetRec->getValueAsDef("InstructionSet");
140}
Brian Gaeked0fde302003-11-11 22:41:34 +0000141
Chris Lattner175580c2004-08-14 22:50:53 +0000142/// getAsmWriter - Return the AssemblyWriter definition for this target.
143///
144Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000145 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
146 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000147 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000148 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000149}
150
Chris Lattner26693112004-08-16 01:10:21 +0000151void CodeGenTarget::ReadRegisters() const {
152 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
153 if (Regs.empty())
154 throw std::string("No 'Register' subclasses defined!");
155
156 Registers.reserve(Regs.size());
157 Registers.assign(Regs.begin(), Regs.end());
158}
159
Chris Lattner7a680c62004-08-21 02:24:57 +0000160CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
161 DeclaredSpillSize = R->getValueAsInt("SpillSize");
162 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
163}
164
Chris Lattner26693112004-08-16 01:10:21 +0000165const std::string &CodeGenRegister::getName() const {
166 return TheDef->getName();
167}
168
Chris Lattner056afef2004-08-21 04:05:00 +0000169void CodeGenTarget::ReadRegisterClasses() const {
170 std::vector<Record*> RegClasses =
171 Records.getAllDerivedDefinitions("RegisterClass");
172 if (RegClasses.empty())
173 throw std::string("No 'RegisterClass' subclasses defined!");
174
175 RegisterClasses.reserve(RegClasses.size());
176 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
177}
178
Evan Cheng44a65fa2006-05-16 07:05:30 +0000179std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
180 std::vector<unsigned char> Result;
181 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
182 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
183 const CodeGenRegisterClass &RC = RegisterClasses[i];
184 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
185 if (R == RC.Elements[ei]) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000186 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Evan Cheng44a65fa2006-05-16 07:05:30 +0000187 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
188 Result.push_back(InVTs[i]);
189 }
190 }
191 }
192 return Result;
193}
194
195
Chris Lattner056afef2004-08-21 04:05:00 +0000196CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000197 // Rename anonymous register classes.
198 if (R->getName().size() > 9 && R->getName()[9] == '.') {
199 static unsigned AnonCounter = 0;
200 R->setName("AnonRegClass_"+utostr(AnonCounter++));
201 }
202
Nate Begeman6510b222005-12-01 04:51:06 +0000203 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
204 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
205 Record *Type = TypeList[i];
206 if (!Type->isSubClassOf("ValueType"))
207 throw "RegTypes list member '" + Type->getName() +
208 "' does not derive from the ValueType class!";
209 VTs.push_back(getValueType(Type));
210 }
211 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000212
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000213 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
214 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
215 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000216 if (!Reg->isSubClassOf("Register"))
217 throw "Register Class member '" + Reg->getName() +
218 "' does not derive from the Register class!";
219 Elements.push_back(Reg);
220 }
Nate Begeman6510b222005-12-01 04:51:06 +0000221
Christopher Lamba3211252007-06-13 22:20:15 +0000222 std::vector<Record*> SubRegClassList =
223 R->getValueAsListOfDefs("SubRegClassList");
224 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
225 Record *SubRegClass = SubRegClassList[i];
226 if (!SubRegClass->isSubClassOf("RegisterClass"))
227 throw "Register Class member '" + SubRegClass->getName() +
228 "' does not derive from the RegisterClass class!";
229 SubRegClasses.push_back(SubRegClass);
230 }
231
Nate Begeman6510b222005-12-01 04:51:06 +0000232 // Allow targets to override the size in bits of the RegisterClass.
233 unsigned Size = R->getValueAsInt("Size");
234
235 Namespace = R->getValueAsString("Namespace");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000236 SpillSize = Size ? Size : MVT(VTs[0]).getSizeInBits();
Nate Begeman6510b222005-12-01 04:51:06 +0000237 SpillAlignment = R->getValueAsInt("Alignment");
Evan Chenga3ca3142007-09-19 01:35:01 +0000238 CopyCost = R->getValueAsInt("CopyCost");
Nate Begeman6510b222005-12-01 04:51:06 +0000239 MethodBodies = R->getValueAsCode("MethodBodies");
240 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000241}
242
243const std::string &CodeGenRegisterClass::getName() const {
244 return TheDef->getName();
245}
246
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000247void CodeGenTarget::ReadLegalValueTypes() const {
248 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
249 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000250 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
251 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000252
253 // Remove duplicates.
254 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
255 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
256 LegalValueTypes.end()),
257 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000258}
Chris Lattner056afef2004-08-21 04:05:00 +0000259
Chris Lattner175580c2004-08-14 22:50:53 +0000260
Chris Lattnerec352402004-08-01 05:04:00 +0000261void CodeGenTarget::ReadInstructions() const {
262 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000263 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000264 throw std::string("No 'Instruction' subclasses defined!");
265
Chris Lattneraa77d772006-01-27 01:45:06 +0000266 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000267 std::string InstFormatName =
268 getAsmWriter()->getValueAsString("InstFormatName");
269
270 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
271 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
272 Instructions.insert(std::make_pair(Insts[i]->getName(),
273 CodeGenInstruction(Insts[i], AsmStr)));
274 }
Chris Lattnerec352402004-08-01 05:04:00 +0000275}
276
Chris Lattnerd6488672005-01-22 18:58:51 +0000277/// getInstructionsByEnumValue - Return all of the instructions defined by the
278/// target, ordered by their enum value.
279void CodeGenTarget::
280getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
281 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000282 std::map<std::string, CodeGenInstruction>::const_iterator I;
283 I = getInstructions().find("PHI");
284 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
285 const CodeGenInstruction *PHI = &I->second;
286
287 I = getInstructions().find("INLINEASM");
288 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
289 const CodeGenInstruction *INLINEASM = &I->second;
290
Dan Gohman44066042008-07-01 00:05:16 +0000291 I = getInstructions().find("DBG_LABEL");
292 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
293 const CodeGenInstruction *DBG_LABEL = &I->second;
294
295 I = getInstructions().find("EH_LABEL");
296 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
297 const CodeGenInstruction *EH_LABEL = &I->second;
298
299 I = getInstructions().find("GC_LABEL");
300 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
301 const CodeGenInstruction *GC_LABEL = &I->second;
Jim Laskeya683f9b2007-01-26 17:29:20 +0000302
Evan Chenga844bde2008-02-02 04:07:54 +0000303 I = getInstructions().find("DECLARE");
304 if (I == Instructions.end()) throw "Could not find 'DECLARE' instruction!";
305 const CodeGenInstruction *DECLARE = &I->second;
306
Christopher Lamb08d52072007-07-26 07:48:21 +0000307 I = getInstructions().find("EXTRACT_SUBREG");
308 if (I == Instructions.end())
309 throw "Could not find 'EXTRACT_SUBREG' instruction!";
310 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
311
312 I = getInstructions().find("INSERT_SUBREG");
313 if (I == Instructions.end())
314 throw "Could not find 'INSERT_SUBREG' instruction!";
315 const CodeGenInstruction *INSERT_SUBREG = &I->second;
316
Evan Chengda47e6e2008-03-15 00:03:38 +0000317 I = getInstructions().find("IMPLICIT_DEF");
318 if (I == Instructions.end())
319 throw "Could not find 'IMPLICIT_DEF' instruction!";
320 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
321
Christopher Lambc9298232008-03-16 03:12:01 +0000322 I = getInstructions().find("SUBREG_TO_REG");
323 if (I == Instructions.end())
324 throw "Could not find 'SUBREG_TO_REG' instruction!";
325 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
326
Chris Lattnerd6488672005-01-22 18:58:51 +0000327 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000328 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000329 NumberedInstructions.push_back(INLINEASM);
Dan Gohman44066042008-07-01 00:05:16 +0000330 NumberedInstructions.push_back(DBG_LABEL);
331 NumberedInstructions.push_back(EH_LABEL);
332 NumberedInstructions.push_back(GC_LABEL);
Evan Chenga844bde2008-02-02 04:07:54 +0000333 NumberedInstructions.push_back(DECLARE);
Christopher Lamb08d52072007-07-26 07:48:21 +0000334 NumberedInstructions.push_back(EXTRACT_SUBREG);
335 NumberedInstructions.push_back(INSERT_SUBREG);
Evan Chengda47e6e2008-03-15 00:03:38 +0000336 NumberedInstructions.push_back(IMPLICIT_DEF);
Christopher Lambc9298232008-03-16 03:12:01 +0000337 NumberedInstructions.push_back(SUBREG_TO_REG);
Chris Lattnerd6488672005-01-22 18:58:51 +0000338 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000339 if (&II->second != PHI &&
340 &II->second != INLINEASM &&
Dan Gohman44066042008-07-01 00:05:16 +0000341 &II->second != DBG_LABEL &&
342 &II->second != EH_LABEL &&
343 &II->second != GC_LABEL &&
Evan Chenga844bde2008-02-02 04:07:54 +0000344 &II->second != DECLARE &&
Christopher Lamb08d52072007-07-26 07:48:21 +0000345 &II->second != EXTRACT_SUBREG &&
Evan Chengda47e6e2008-03-15 00:03:38 +0000346 &II->second != INSERT_SUBREG &&
Christopher Lambc9298232008-03-16 03:12:01 +0000347 &II->second != IMPLICIT_DEF &&
348 &II->second != SUBREG_TO_REG)
Chris Lattnerd6488672005-01-22 18:58:51 +0000349 NumberedInstructions.push_back(&II->second);
350}
351
352
Misha Brukman35e83cc2004-10-14 05:50:43 +0000353/// isLittleEndianEncoding - Return whether this target encodes its instruction
354/// in little-endian format, i.e. bits laid out in the order [0..n]
355///
356bool CodeGenTarget::isLittleEndianEncoding() const {
357 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
358}
359
Evan Cheng0fc71982005-12-08 02:00:36 +0000360//===----------------------------------------------------------------------===//
361// ComplexPattern implementation
362//
363ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000364 Ty = ::getValueType(R->getValueAsDef("Ty"));
365 NumOperands = R->getValueAsInt("NumOperands");
366 SelectFunc = R->getValueAsString("SelectFunc");
367 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000368
369 // Parse the properties.
370 Properties = 0;
371 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
372 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
373 if (PropList[i]->getName() == "SDNPHasChain") {
374 Properties |= 1 << SDNPHasChain;
375 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
376 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerba7e7562008-01-10 07:59:24 +0000377 } else if (PropList[i]->getName() == "SDNPMayStore") {
378 Properties |= 1 << SDNPMayStore;
379 } else if (PropList[i]->getName() == "SDNPMayLoad") {
380 Properties |= 1 << SDNPMayLoad;
381 } else if (PropList[i]->getName() == "SDNPSideEffect") {
382 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000383 } else if (PropList[i]->getName() == "SDNPMemOperand") {
384 Properties |= 1 << SDNPMemOperand;
Evan Cheng94b30402006-10-11 21:02:01 +0000385 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000386 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
387 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000388 exit(1);
389 }
Christopher Lamb85356242008-01-31 07:27:46 +0000390
391 // Parse the attributes.
392 Attributes = 0;
393 PropList = R->getValueAsListOfDefs("Attributes");
394 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
395 if (PropList[i]->getName() == "CPAttrParentAsRoot") {
396 Attributes |= 1 << CPAttrParentAsRoot;
397 } else {
398 cerr << "Unsupported pattern attribute '" << PropList[i]->getName()
399 << "' on ComplexPattern '" << R->getName() << "'!\n";
400 exit(1);
401 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000402}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000403
Chris Lattner43fbbc32006-03-24 19:49:31 +0000404//===----------------------------------------------------------------------===//
405// CodeGenIntrinsic Implementation
406//===----------------------------------------------------------------------===//
407
408std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
409 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000410
411 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000412
Chris Lattner8850a1b2006-03-27 22:48:18 +0000413 for (unsigned i = 0, e = I.size(); i != e; ++i)
Dan Gohmanee4fa192008-04-03 00:02:49 +0000414 Result.push_back(CodeGenIntrinsic(I[i]));
Chris Lattner8850a1b2006-03-27 22:48:18 +0000415 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000416}
417
Dan Gohmanee4fa192008-04-03 00:02:49 +0000418CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000419 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000420 std::string DefName = R->getName();
421 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000422 isOverloaded = false;
Evan Cheng6bd95672008-06-16 20:29:38 +0000423 isCommutative = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000424
425 if (DefName.size() <= 4 ||
426 std::string(DefName.begin(), DefName.begin()+4) != "int_")
427 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
428 EnumName = std::string(DefName.begin()+4, DefName.end());
429 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
430 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
431 TargetPrefix = R->getValueAsString("TargetPrefix");
432 Name = R->getValueAsString("LLVMName");
433 if (Name == "") {
434 // If an explicit name isn't specified, derive one from the DefName.
435 Name = "llvm.";
436 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
437 if (EnumName[i] == '_')
438 Name += '.';
439 else
440 Name += EnumName[i];
441 } else {
442 // Verify it starts with "llvm.".
443 if (Name.size() <= 5 ||
444 std::string(Name.begin(), Name.begin()+5) != "llvm.")
445 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
446 }
447
448 // If TargetPrefix is specified, make sure that Name starts with
449 // "llvm.<targetprefix>.".
450 if (!TargetPrefix.empty()) {
451 if (Name.size() < 6+TargetPrefix.size() ||
452 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
453 != (TargetPrefix+"."))
454 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
455 TargetPrefix + ".'!";
456 }
457
458 // Parse the list of argument types.
459 ListInit *TypeList = R->getValueAsListInit("Types");
460 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000461 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000462 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000463 MVT::SimpleValueType VT = getValueType(TyEl->getValueAsDef("VT"));
Mon P Wange3b3a722008-07-30 04:36:53 +0000464 isOverloaded |= VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000465 ArgVTs.push_back(VT);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000466 ArgTypeDefs.push_back(TyEl);
467 }
Chandler Carruth69940402007-08-04 01:51:18 +0000468 if (ArgVTs.size() == 0)
Chris Lattner43fbbc32006-03-24 19:49:31 +0000469 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000470
Chris Lattner43fbbc32006-03-24 19:49:31 +0000471
472 // Parse the intrinsic properties.
473 ListInit *PropList = R->getValueAsListInit("Properties");
474 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000475 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000476 assert(Property->isSubClassOf("IntrinsicProperty") &&
477 "Expected a property!");
478
Chris Lattner4b2362e2006-04-10 22:02:59 +0000479 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000480 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000481 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000482 ModRef = ReadArgMem;
483 else if (Property->getName() == "IntrReadMem")
484 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000485 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000486 ModRef = WriteArgMem;
487 else if (Property->getName() == "IntrWriteMem")
488 ModRef = WriteMem;
Evan Cheng6bd95672008-06-16 20:29:38 +0000489 else if (Property->getName() == "Commutative")
490 isCommutative = true;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000491 else
492 assert(0 && "Unknown property!");
493 }
494}