blob: 1efe2ff080d662f6e9e421b0839e9e033634d113 [file] [log] [blame]
Chris Lattner4a7bcdc2008-01-06 01:35:39 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfd6c2f02007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner4a7bcdc2008-01-06 01:35:39 +000010// This class wraps target description classes used by the various code
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
17#include "CodeGenTarget.h"
18#include "CodeGenIntrinsics.h"
19#include "Record.h"
20#include "llvm/ADT/StringExtras.h"
Chris Lattner690a7992010-03-27 20:32:26 +000021#include "llvm/ADT/STLExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include <algorithm>
24using namespace llvm;
25
26static cl::opt<unsigned>
Daniel Dunbar85f1b392009-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>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031AsmWriterNum("asmwriternum", cl::init(0),
32 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
33
Owen Anderson36e3a6e2009-08-11 20:47:22 +000034/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands92c43912008-06-06 12:08:01 +000035/// record corresponds to.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000036MVT::SimpleValueType llvm::getValueType(Record *Rec) {
37 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038}
39
Owen Anderson36e3a6e2009-08-11 20:47:22 +000040std::string llvm::getName(MVT::SimpleValueType T) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 switch (T) {
Owen Anderson36e3a6e2009-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 Wilsonb6b793c2009-07-10 22:25:24 +000045 default: return getEnumName(T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 }
47}
48
Owen Anderson36e3a6e2009-08-11 20:47:22 +000049std::string llvm::getEnumName(MVT::SimpleValueType T) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 switch (T) {
Owen Anderson36e3a6e2009-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";
83 case MVT::v2f32: return "MVT::v2f32";
84 case MVT::v4f32: return "MVT::v4f32";
85 case MVT::v8f32: return "MVT::v8f32";
86 case MVT::v2f64: return "MVT::v2f64";
87 case MVT::v4f64: return "MVT::v4f64";
88 case MVT::Metadata: return "MVT::Metadata";
89 case MVT::iPTR: return "MVT::iPTR";
90 case MVT::iPTRAny: return "MVT::iPTRAny";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
92 }
93}
94
Chris Lattner4ca8ff02008-01-05 22:25:12 +000095/// getQualifiedName - Return the name of the specified record, with a
96/// namespace qualifier if the record contains one.
97///
98std::string llvm::getQualifiedName(const Record *R) {
99 std::string Namespace = R->getValueAsString("Namespace");
100 if (Namespace.empty()) return R->getName();
101 return Namespace + "::" + R->getName();
102}
103
104
105
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107/// getTarget - Return the current instance of the Target class.
108///
109CodeGenTarget::CodeGenTarget() {
110 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
111 if (Targets.size() == 0)
112 throw std::string("ERROR: No 'Target' subclasses defined!");
113 if (Targets.size() != 1)
114 throw std::string("ERROR: Multiple subclasses of Target defined!");
115 TargetRec = Targets[0];
116}
117
118
119const std::string &CodeGenTarget::getName() const {
120 return TargetRec->getName();
121}
122
Dan Gohman6a36cc92008-08-20 21:45:57 +0000123std::string CodeGenTarget::getInstNamespace() const {
Dan Gohman6a36cc92008-08-20 21:45:57 +0000124 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
Chris Lattnerd268a352010-03-19 01:00:55 +0000125 // Make sure not to pick up "TargetOpcode" by accidentally getting
Dan Gohman6a36cc92008-08-20 21:45:57 +0000126 // the namespace off the PHI instruction or something.
Chris Lattnerd268a352010-03-19 01:00:55 +0000127 if ((*i)->Namespace != "TargetOpcode")
128 return (*i)->Namespace;
Dan Gohman6a36cc92008-08-20 21:45:57 +0000129 }
130
Chris Lattnerd268a352010-03-19 01:00:55 +0000131 return "";
Dan Gohman6a36cc92008-08-20 21:45:57 +0000132}
133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134Record *CodeGenTarget::getInstructionSet() const {
135 return TargetRec->getValueAsDef("InstructionSet");
136}
137
Chris Lattner82d9a642010-03-19 00:07:20 +0000138
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000139/// getAsmParser - Return the AssemblyParser definition for this target.
140///
141Record *CodeGenTarget::getAsmParser() const {
142 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
143 if (AsmParserNum >= LI.size())
144 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
145 return LI[AsmParserNum];
146}
147
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148/// getAsmWriter - Return the AssemblyWriter definition for this target.
149///
150Record *CodeGenTarget::getAsmWriter() const {
151 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
152 if (AsmWriterNum >= LI.size())
153 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
154 return LI[AsmWriterNum];
155}
156
157void CodeGenTarget::ReadRegisters() const {
158 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
159 if (Regs.empty())
160 throw std::string("No 'Register' subclasses defined!");
161
162 Registers.reserve(Regs.size());
163 Registers.assign(Regs.begin(), Regs.end());
164}
165
166CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
167 DeclaredSpillSize = R->getValueAsInt("SpillSize");
168 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
169}
170
171const std::string &CodeGenRegister::getName() const {
172 return TheDef->getName();
173}
174
175void CodeGenTarget::ReadRegisterClasses() const {
176 std::vector<Record*> RegClasses =
177 Records.getAllDerivedDefinitions("RegisterClass");
178 if (RegClasses.empty())
179 throw std::string("No 'RegisterClass' subclasses defined!");
180
181 RegisterClasses.reserve(RegClasses.size());
182 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
183}
184
Chris Lattner3f9bead2010-03-15 06:00:16 +0000185std::vector<MVT::SimpleValueType> CodeGenTarget::
186getRegisterVTs(Record *R) const {
187 std::vector<MVT::SimpleValueType> Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
189 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
190 const CodeGenRegisterClass &RC = RegisterClasses[i];
191 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
192 if (R == RC.Elements[ei]) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000193 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Chris Lattner3f9bead2010-03-15 06:00:16 +0000194 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 }
196 }
197 }
Chris Lattner690a7992010-03-27 20:32:26 +0000198
199 // Remove duplicates.
200 array_pod_sort(Result.begin(), Result.end());
201 Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 return Result;
203}
204
205
206CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
207 // Rename anonymous register classes.
208 if (R->getName().size() > 9 && R->getName()[9] == '.') {
209 static unsigned AnonCounter = 0;
210 R->setName("AnonRegClass_"+utostr(AnonCounter++));
211 }
212
213 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
214 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
215 Record *Type = TypeList[i];
216 if (!Type->isSubClassOf("ValueType"))
217 throw "RegTypes list member '" + Type->getName() +
218 "' does not derive from the ValueType class!";
219 VTs.push_back(getValueType(Type));
220 }
221 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
222
223 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
224 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
225 Record *Reg = RegList[i];
226 if (!Reg->isSubClassOf("Register"))
227 throw "Register Class member '" + Reg->getName() +
228 "' does not derive from the Register class!";
229 Elements.push_back(Reg);
230 }
231
232 std::vector<Record*> SubRegClassList =
233 R->getValueAsListOfDefs("SubRegClassList");
234 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
235 Record *SubRegClass = SubRegClassList[i];
236 if (!SubRegClass->isSubClassOf("RegisterClass"))
237 throw "Register Class member '" + SubRegClass->getName() +
238 "' does not derive from the RegisterClass class!";
239 SubRegClasses.push_back(SubRegClass);
240 }
241
242 // Allow targets to override the size in bits of the RegisterClass.
243 unsigned Size = R->getValueAsInt("Size");
244
245 Namespace = R->getValueAsString("Namespace");
Owen Andersonac9de032009-08-10 22:56:29 +0000246 SpillSize = Size ? Size : EVT(VTs[0]).getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 SpillAlignment = R->getValueAsInt("Alignment");
Evan Cheng77ac1822007-09-19 01:35:01 +0000248 CopyCost = R->getValueAsInt("CopyCost");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 MethodBodies = R->getValueAsCode("MethodBodies");
250 MethodProtos = R->getValueAsCode("MethodProtos");
251}
252
253const std::string &CodeGenRegisterClass::getName() const {
254 return TheDef->getName();
255}
256
257void CodeGenTarget::ReadLegalValueTypes() const {
258 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
259 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
260 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
261 LegalValueTypes.push_back(RCs[i].VTs[ri]);
262
263 // Remove duplicates.
264 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
265 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
266 LegalValueTypes.end()),
267 LegalValueTypes.end());
268}
269
270
271void CodeGenTarget::ReadInstructions() const {
272 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
273 if (Insts.size() <= 2)
274 throw std::string("No 'Instruction' subclasses defined!");
275
276 // Parse the instructions defined in the .td file.
277 std::string InstFormatName =
278 getAsmWriter()->getValueAsString("InstFormatName");
279
280 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
281 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
Chris Lattner95127fb2010-03-19 01:07:44 +0000282 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i], AsmStr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 }
284}
285
Chris Lattnere864fde2010-03-19 00:23:20 +0000286static const CodeGenInstruction *
287GetInstByName(const char *Name,
Chris Lattner95127fb2010-03-19 01:07:44 +0000288 const DenseMap<const Record*, CodeGenInstruction*> &Insts) {
289 const Record *Rec = Records.getDef(Name);
290
291 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
292 I = Insts.find(Rec);
293 if (Rec == 0 || I == Insts.end())
Chris Lattnere864fde2010-03-19 00:23:20 +0000294 throw std::string("Could not find '") + Name + "' instruction!";
Chris Lattner95127fb2010-03-19 01:07:44 +0000295 return I->second;
296}
297
298namespace {
299/// SortInstByName - Sorting predicate to sort instructions by name.
300///
301struct SortInstByName {
302 bool operator()(const CodeGenInstruction *Rec1,
303 const CodeGenInstruction *Rec2) const {
304 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
305 }
306};
Chris Lattnere864fde2010-03-19 00:23:20 +0000307}
308
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309/// getInstructionsByEnumValue - Return all of the instructions defined by the
310/// target, ordered by their enum value.
Chris Lattnerd268a352010-03-19 01:00:55 +0000311void CodeGenTarget::ComputeInstrsByEnum() const {
Chris Lattner95127fb2010-03-19 01:07:44 +0000312 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
Chris Lattnere864fde2010-03-19 00:23:20 +0000313 const CodeGenInstruction *PHI = GetInstByName("PHI", Insts);
314 const CodeGenInstruction *INLINEASM = GetInstByName("INLINEASM", Insts);
315 const CodeGenInstruction *DBG_LABEL = GetInstByName("DBG_LABEL", Insts);
316 const CodeGenInstruction *EH_LABEL = GetInstByName("EH_LABEL", Insts);
317 const CodeGenInstruction *GC_LABEL = GetInstByName("GC_LABEL", Insts);
318 const CodeGenInstruction *KILL = GetInstByName("KILL", Insts);
319 const CodeGenInstruction *EXTRACT_SUBREG =
320 GetInstByName("EXTRACT_SUBREG", Insts);
321 const CodeGenInstruction *INSERT_SUBREG =
322 GetInstByName("INSERT_SUBREG", Insts);
323 const CodeGenInstruction *IMPLICIT_DEF = GetInstByName("IMPLICIT_DEF", Insts);
324 const CodeGenInstruction *SUBREG_TO_REG =
325 GetInstByName("SUBREG_TO_REG", Insts);
326 const CodeGenInstruction *COPY_TO_REGCLASS =
327 GetInstByName("COPY_TO_REGCLASS", Insts);
328 const CodeGenInstruction *DBG_VALUE = GetInstByName("DBG_VALUE", Insts);
Evan Chengb58dd0e2010-05-01 00:28:44 +0000329 const CodeGenInstruction *REG_SEQUENCE = GetInstByName("REG_SEQUENCE", Insts);
Dale Johannesen4b04d8a2010-01-08 23:51:25 +0000330
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 // Print out the rest of the instructions now.
Chris Lattnera2e1d002010-03-19 00:34:35 +0000332 InstrsByEnum.push_back(PHI);
333 InstrsByEnum.push_back(INLINEASM);
334 InstrsByEnum.push_back(DBG_LABEL);
335 InstrsByEnum.push_back(EH_LABEL);
336 InstrsByEnum.push_back(GC_LABEL);
337 InstrsByEnum.push_back(KILL);
338 InstrsByEnum.push_back(EXTRACT_SUBREG);
339 InstrsByEnum.push_back(INSERT_SUBREG);
340 InstrsByEnum.push_back(IMPLICIT_DEF);
341 InstrsByEnum.push_back(SUBREG_TO_REG);
342 InstrsByEnum.push_back(COPY_TO_REGCLASS);
343 InstrsByEnum.push_back(DBG_VALUE);
Evan Chengb58dd0e2010-05-01 00:28:44 +0000344 InstrsByEnum.push_back(REG_SEQUENCE);
Chris Lattnerd268a352010-03-19 01:00:55 +0000345
Chris Lattner95127fb2010-03-19 01:07:44 +0000346 unsigned EndOfPredefines = InstrsByEnum.size();
Chris Lattnerd268a352010-03-19 01:00:55 +0000347
Chris Lattner95127fb2010-03-19 01:07:44 +0000348 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
349 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
350 const CodeGenInstruction *CGI = I->second;
Chris Lattnerd268a352010-03-19 01:00:55 +0000351 if (CGI != PHI &&
352 CGI != INLINEASM &&
353 CGI != DBG_LABEL &&
354 CGI != EH_LABEL &&
355 CGI != GC_LABEL &&
356 CGI != KILL &&
357 CGI != EXTRACT_SUBREG &&
358 CGI != INSERT_SUBREG &&
359 CGI != IMPLICIT_DEF &&
360 CGI != SUBREG_TO_REG &&
361 CGI != COPY_TO_REGCLASS &&
Evan Chengb58dd0e2010-05-01 00:28:44 +0000362 CGI != DBG_VALUE &&
363 CGI != REG_SEQUENCE)
Chris Lattnerd268a352010-03-19 01:00:55 +0000364 InstrsByEnum.push_back(CGI);
365 }
Chris Lattner95127fb2010-03-19 01:07:44 +0000366
367 // All of the instructions are now in random order based on the map iteration.
368 // Sort them by name.
369 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(),
370 SortInstByName());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371}
372
373
374/// isLittleEndianEncoding - Return whether this target encodes its instruction
375/// in little-endian format, i.e. bits laid out in the order [0..n]
376///
377bool CodeGenTarget::isLittleEndianEncoding() const {
378 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
379}
380
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381//===----------------------------------------------------------------------===//
382// ComplexPattern implementation
383//
384ComplexPattern::ComplexPattern(Record *R) {
385 Ty = ::getValueType(R->getValueAsDef("Ty"));
386 NumOperands = R->getValueAsInt("NumOperands");
387 SelectFunc = R->getValueAsString("SelectFunc");
388 RootNodes = R->getValueAsListOfDefs("RootNodes");
389
390 // Parse the properties.
391 Properties = 0;
392 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
393 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
394 if (PropList[i]->getName() == "SDNPHasChain") {
395 Properties |= 1 << SDNPHasChain;
396 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
397 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerc90ee9c2008-01-10 07:59:24 +0000398 } else if (PropList[i]->getName() == "SDNPMayStore") {
399 Properties |= 1 << SDNPMayStore;
400 } else if (PropList[i]->getName() == "SDNPMayLoad") {
401 Properties |= 1 << SDNPMayLoad;
402 } else if (PropList[i]->getName() == "SDNPSideEffect") {
403 Properties |= 1 << SDNPSideEffect;
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000404 } else if (PropList[i]->getName() == "SDNPMemOperand") {
405 Properties |= 1 << SDNPMemOperand;
Chris Lattnerd7e53fb2010-03-19 05:07:09 +0000406 } else if (PropList[i]->getName() == "SDNPVariadic") {
407 Properties |= 1 << SDNPVariadic;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 } else {
Daniel Dunbard4287062009-07-03 00:10:29 +0000409 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
410 << "' on ComplexPattern '" << R->getName() << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 exit(1);
412 }
413}
414
415//===----------------------------------------------------------------------===//
416// CodeGenIntrinsic Implementation
417//===----------------------------------------------------------------------===//
418
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000419std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
420 bool TargetOnly) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
422
423 std::vector<CodeGenIntrinsic> Result;
424
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000425 for (unsigned i = 0, e = I.size(); i != e; ++i) {
426 bool isTarget = I[i]->getValueAsBit("isTarget");
427 if (isTarget == TargetOnly)
428 Result.push_back(CodeGenIntrinsic(I[i]));
429 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 return Result;
431}
432
Dan Gohman907df572008-04-03 00:02:49 +0000433CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 TheDef = R;
435 std::string DefName = R->getName();
436 ModRef = WriteMem;
437 isOverloaded = false;
Evan Chengbb46d262008-06-16 20:29:38 +0000438 isCommutative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439
440 if (DefName.size() <= 4 ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000441 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendling9b4eab82008-11-13 09:08:33 +0000443
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendling9b4eab82008-11-13 09:08:33 +0000445
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
447 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendling9b4eab82008-11-13 09:08:33 +0000448
449 TargetPrefix = R->getValueAsString("TargetPrefix");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 Name = R->getValueAsString("LLVMName");
Bill Wendling9b4eab82008-11-13 09:08:33 +0000451
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 if (Name == "") {
453 // If an explicit name isn't specified, derive one from the DefName.
454 Name = "llvm.";
Bill Wendling9b4eab82008-11-13 09:08:33 +0000455
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendling9b4eab82008-11-13 09:08:33 +0000457 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 } else {
459 // Verify it starts with "llvm.".
460 if (Name.size() <= 5 ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000461 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
463 }
464
465 // If TargetPrefix is specified, make sure that Name starts with
466 // "llvm.<targetprefix>.".
467 if (!TargetPrefix.empty()) {
468 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000469 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
470 != (TargetPrefix + "."))
471 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 TargetPrefix + ".'!";
473 }
474
Bill Wendling9b4eab82008-11-13 09:08:33 +0000475 // Parse the list of return types.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000476 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendling9b4eab82008-11-13 09:08:33 +0000477 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
479 Record *TyEl = TypeList->getElementAsRecord(i);
480 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000481 MVT::SimpleValueType VT;
Bob Wilsonc7245f42009-01-07 00:09:01 +0000482 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000483 unsigned MatchTy = TyEl->getValueAsInt("Number");
484 assert(MatchTy < OverloadedVTs.size() &&
485 "Invalid matching number!");
486 VT = OverloadedVTs[MatchTy];
Bob Wilsonc7245f42009-01-07 00:09:01 +0000487 // It only makes sense to use the extended and truncated vector element
488 // variants with iAny types; otherwise, if the intrinsic is not
489 // overloaded, all the types can be specified directly.
490 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
491 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000492 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson7b70d4f2009-08-11 05:03:38 +0000493 "Expected iAny or vAny type");
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000494 } else {
Bob Wilsonc7245f42009-01-07 00:09:01 +0000495 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000496 }
Bob Wilsonf73db022009-08-11 01:14:02 +0000497 if (EVT(VT).isOverloaded()) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000498 OverloadedVTs.push_back(VT);
Chris Lattner4faf9a82010-03-23 23:46:27 +0000499 isOverloaded = true;
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000500 }
Chris Lattner4faf9a82010-03-23 23:46:27 +0000501
502 // Reject invalid types.
503 if (VT == MVT::isVoid)
504 throw "Intrinsic '" + DefName + " has void in result type list!";
Chris Lattner829fd562010-03-22 20:56:36 +0000505
Bill Wendling9b4eab82008-11-13 09:08:33 +0000506 IS.RetVTs.push_back(VT);
507 IS.RetTypeDefs.push_back(TyEl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 }
Chris Lattner829fd562010-03-22 20:56:36 +0000509
Bill Wendling9b4eab82008-11-13 09:08:33 +0000510 // Parse the list of parameter types.
511 TypeList = R->getValueAsListInit("ParamTypes");
512 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
513 Record *TyEl = TypeList->getElementAsRecord(i);
514 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000515 MVT::SimpleValueType VT;
Bob Wilsonc7245f42009-01-07 00:09:01 +0000516 if (TyEl->isSubClassOf("LLVMMatchType")) {
517 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000518 assert(MatchTy < OverloadedVTs.size() &&
519 "Invalid matching number!");
520 VT = OverloadedVTs[MatchTy];
Bob Wilsonc7245f42009-01-07 00:09:01 +0000521 // It only makes sense to use the extended and truncated vector element
522 // variants with iAny types; otherwise, if the intrinsic is not
523 // overloaded, all the types can be specified directly.
524 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
525 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000526 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson7b70d4f2009-08-11 05:03:38 +0000527 "Expected iAny or vAny type");
Bob Wilsonc7245f42009-01-07 00:09:01 +0000528 } else
529 VT = getValueType(TyEl->getValueAsDef("VT"));
Chris Lattner4faf9a82010-03-23 23:46:27 +0000530
Bob Wilsonf73db022009-08-11 01:14:02 +0000531 if (EVT(VT).isOverloaded()) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000532 OverloadedVTs.push_back(VT);
Chris Lattner4faf9a82010-03-23 23:46:27 +0000533 isOverloaded = true;
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000534 }
Chris Lattner4faf9a82010-03-23 23:46:27 +0000535
536 // Reject invalid types.
537 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
538 throw "Intrinsic '" + DefName + " has void in result type list!";
539
Bill Wendling9b4eab82008-11-13 09:08:33 +0000540 IS.ParamVTs.push_back(VT);
541 IS.ParamTypeDefs.push_back(TyEl);
542 }
543
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 // Parse the intrinsic properties.
545 ListInit *PropList = R->getValueAsListInit("Properties");
546 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
547 Record *Property = PropList->getElementAsRecord(i);
548 assert(Property->isSubClassOf("IntrinsicProperty") &&
549 "Expected a property!");
550
551 if (Property->getName() == "IntrNoMem")
552 ModRef = NoMem;
553 else if (Property->getName() == "IntrReadArgMem")
554 ModRef = ReadArgMem;
555 else if (Property->getName() == "IntrReadMem")
556 ModRef = ReadMem;
557 else if (Property->getName() == "IntrWriteArgMem")
558 ModRef = WriteArgMem;
559 else if (Property->getName() == "IntrWriteMem")
560 ModRef = WriteMem;
Evan Chengbb46d262008-06-16 20:29:38 +0000561 else if (Property->getName() == "Commutative")
562 isCommutative = true;
Chris Lattner1b0090f2009-01-12 01:12:03 +0000563 else if (Property->isSubClassOf("NoCapture")) {
564 unsigned ArgNo = Property->getValueAsInt("ArgNo");
565 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
566 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 assert(0 && "Unknown property!");
568 }
569}