blob: b0e896b7e840de99646e719b92166f1b06fe93fb [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"
21#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include <algorithm>
23using namespace llvm;
24
25static cl::opt<unsigned>
26AsmWriterNum("asmwriternum", cl::init(0),
27 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
28
Duncan Sands92c43912008-06-06 12:08:01 +000029/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
30/// record corresponds to.
31MVT::SimpleValueType llvm::getValueType(Record *Rec) {
32 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033}
34
Duncan Sands92c43912008-06-06 12:08:01 +000035std::string llvm::getName(MVT::SimpleValueType T) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 switch (T) {
Bob Wilsonb6b793c2009-07-10 22:25:24 +000037 case MVT::Other: return "UNKNOWN";
38 case MVT::iPTR: return "TLI.getPointerTy()";
39 case MVT::iPTRAny: return "TLI.getPointerTy()";
40 default: return getEnumName(T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 }
42}
43
Duncan Sands92c43912008-06-06 12:08:01 +000044std::string llvm::getEnumName(MVT::SimpleValueType T) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 switch (T) {
46 case MVT::Other: return "MVT::Other";
47 case MVT::i1: return "MVT::i1";
48 case MVT::i8: return "MVT::i8";
49 case MVT::i16: return "MVT::i16";
50 case MVT::i32: return "MVT::i32";
51 case MVT::i64: return "MVT::i64";
52 case MVT::i128: return "MVT::i128";
53 case MVT::iAny: return "MVT::iAny";
Dan Gohman00344182007-08-16 21:57:19 +000054 case MVT::fAny: return "MVT::fAny";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 case MVT::f32: return "MVT::f32";
56 case MVT::f64: return "MVT::f64";
57 case MVT::f80: return "MVT::f80";
58 case MVT::f128: return "MVT::f128";
Dale Johannesen0c81a522007-09-28 01:08:20 +000059 case MVT::ppcf128: return "MVT::ppcf128";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 case MVT::Flag: return "MVT::Flag";
61 case MVT::isVoid:return "MVT::isVoid";
Mon P Wang26342922008-12-18 20:03:17 +000062 case MVT::v2i8: return "MVT::v2i8";
63 case MVT::v4i8: return "MVT::v4i8";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 case MVT::v8i8: return "MVT::v8i8";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 case MVT::v16i8: return "MVT::v16i8";
David Greenea5acb6e2009-06-29 16:47:10 +000066 case MVT::v32i8: return "MVT::v32i8";
67 case MVT::v2i16: return "MVT::v2i16";
68 case MVT::v4i16: return "MVT::v4i16";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 case MVT::v8i16: return "MVT::v8i16";
David Greenea5acb6e2009-06-29 16:47:10 +000070 case MVT::v16i16: return "MVT::v16i16";
71 case MVT::v2i32: return "MVT::v2i32";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 case MVT::v4i32: return "MVT::v4i32";
David Greenea5acb6e2009-06-29 16:47:10 +000073 case MVT::v8i32: return "MVT::v8i32";
74 case MVT::v1i64: return "MVT::v1i64";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 case MVT::v2i64: return "MVT::v2i64";
David Greenea5acb6e2009-06-29 16:47:10 +000076 case MVT::v4i64: return "MVT::v4i64";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 case MVT::v2f32: return "MVT::v2f32";
78 case MVT::v4f32: return "MVT::v4f32";
David Greenea5acb6e2009-06-29 16:47:10 +000079 case MVT::v8f32: return "MVT::v8f32";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 case MVT::v2f64: return "MVT::v2f64";
David Greenea5acb6e2009-06-29 16:47:10 +000081 case MVT::v4f64: return "MVT::v4f64";
Christopher Lambd0620a62007-07-26 06:41:18 +000082 case MVT::v3i32: return "MVT::v3i32";
83 case MVT::v3f32: return "MVT::v3f32";
Devang Patel27ac9822009-07-06 23:44:32 +000084 case MVT::Metadata: return "MVT::Metadata";
Chandler Carrutha228e392007-08-04 01:51:18 +000085 case MVT::iPTR: return "MVT::iPTR";
Mon P Wangce3ac892008-07-30 04:36:53 +000086 case MVT::iPTRAny: return "MVT::iPTRAny";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
88 }
89}
90
Chris Lattner4ca8ff02008-01-05 22:25:12 +000091/// getQualifiedName - Return the name of the specified record, with a
92/// namespace qualifier if the record contains one.
93///
94std::string llvm::getQualifiedName(const Record *R) {
95 std::string Namespace = R->getValueAsString("Namespace");
96 if (Namespace.empty()) return R->getName();
97 return Namespace + "::" + R->getName();
98}
99
100
101
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
103/// getTarget - Return the current instance of the Target class.
104///
105CodeGenTarget::CodeGenTarget() {
106 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
107 if (Targets.size() == 0)
108 throw std::string("ERROR: No 'Target' subclasses defined!");
109 if (Targets.size() != 1)
110 throw std::string("ERROR: Multiple subclasses of Target defined!");
111 TargetRec = Targets[0];
112}
113
114
115const std::string &CodeGenTarget::getName() const {
116 return TargetRec->getName();
117}
118
Dan Gohman6a36cc92008-08-20 21:45:57 +0000119std::string CodeGenTarget::getInstNamespace() const {
120 std::string InstNS;
121
122 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
123 InstNS = i->second.Namespace;
124
125 // Make sure not to pick up "TargetInstrInfo" by accidentally getting
126 // the namespace off the PHI instruction or something.
127 if (InstNS != "TargetInstrInfo")
128 break;
129 }
130
131 return InstNS;
132}
133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134Record *CodeGenTarget::getInstructionSet() const {
135 return TargetRec->getValueAsDef("InstructionSet");
136}
137
138/// getAsmWriter - Return the AssemblyWriter definition for this target.
139///
140Record *CodeGenTarget::getAsmWriter() const {
141 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
142 if (AsmWriterNum >= LI.size())
143 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
144 return LI[AsmWriterNum];
145}
146
147void CodeGenTarget::ReadRegisters() const {
148 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
149 if (Regs.empty())
150 throw std::string("No 'Register' subclasses defined!");
151
152 Registers.reserve(Regs.size());
153 Registers.assign(Regs.begin(), Regs.end());
154}
155
156CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
157 DeclaredSpillSize = R->getValueAsInt("SpillSize");
158 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
159}
160
161const std::string &CodeGenRegister::getName() const {
162 return TheDef->getName();
163}
164
165void CodeGenTarget::ReadRegisterClasses() const {
166 std::vector<Record*> RegClasses =
167 Records.getAllDerivedDefinitions("RegisterClass");
168 if (RegClasses.empty())
169 throw std::string("No 'RegisterClass' subclasses defined!");
170
171 RegisterClasses.reserve(RegClasses.size());
172 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
173}
174
175std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
176 std::vector<unsigned char> Result;
177 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
178 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
179 const CodeGenRegisterClass &RC = RegisterClasses[i];
180 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
181 if (R == RC.Elements[ei]) {
Duncan Sands92c43912008-06-06 12:08:01 +0000182 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
184 Result.push_back(InVTs[i]);
185 }
186 }
187 }
188 return Result;
189}
190
191
192CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
193 // Rename anonymous register classes.
194 if (R->getName().size() > 9 && R->getName()[9] == '.') {
195 static unsigned AnonCounter = 0;
196 R->setName("AnonRegClass_"+utostr(AnonCounter++));
197 }
198
199 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
200 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
201 Record *Type = TypeList[i];
202 if (!Type->isSubClassOf("ValueType"))
203 throw "RegTypes list member '" + Type->getName() +
204 "' does not derive from the ValueType class!";
205 VTs.push_back(getValueType(Type));
206 }
207 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
208
209 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
210 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
211 Record *Reg = RegList[i];
212 if (!Reg->isSubClassOf("Register"))
213 throw "Register Class member '" + Reg->getName() +
214 "' does not derive from the Register class!";
215 Elements.push_back(Reg);
216 }
217
218 std::vector<Record*> SubRegClassList =
219 R->getValueAsListOfDefs("SubRegClassList");
220 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
221 Record *SubRegClass = SubRegClassList[i];
222 if (!SubRegClass->isSubClassOf("RegisterClass"))
223 throw "Register Class member '" + SubRegClass->getName() +
224 "' does not derive from the RegisterClass class!";
225 SubRegClasses.push_back(SubRegClass);
226 }
227
228 // Allow targets to override the size in bits of the RegisterClass.
229 unsigned Size = R->getValueAsInt("Size");
230
231 Namespace = R->getValueAsString("Namespace");
Duncan Sands92c43912008-06-06 12:08:01 +0000232 SpillSize = Size ? Size : MVT(VTs[0]).getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 SpillAlignment = R->getValueAsInt("Alignment");
Evan Cheng77ac1822007-09-19 01:35:01 +0000234 CopyCost = R->getValueAsInt("CopyCost");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 MethodBodies = R->getValueAsCode("MethodBodies");
236 MethodProtos = R->getValueAsCode("MethodProtos");
237}
238
239const std::string &CodeGenRegisterClass::getName() const {
240 return TheDef->getName();
241}
242
243void CodeGenTarget::ReadLegalValueTypes() const {
244 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
245 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
246 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
247 LegalValueTypes.push_back(RCs[i].VTs[ri]);
248
249 // Remove duplicates.
250 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
251 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
252 LegalValueTypes.end()),
253 LegalValueTypes.end());
254}
255
256
257void CodeGenTarget::ReadInstructions() const {
258 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
259 if (Insts.size() <= 2)
260 throw std::string("No 'Instruction' subclasses defined!");
261
262 // Parse the instructions defined in the .td file.
263 std::string InstFormatName =
264 getAsmWriter()->getValueAsString("InstFormatName");
265
266 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
267 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
268 Instructions.insert(std::make_pair(Insts[i]->getName(),
269 CodeGenInstruction(Insts[i], AsmStr)));
270 }
271}
272
273/// getInstructionsByEnumValue - Return all of the instructions defined by the
274/// target, ordered by their enum value.
275void CodeGenTarget::
276getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
277 &NumberedInstructions) {
278 std::map<std::string, CodeGenInstruction>::const_iterator I;
279 I = getInstructions().find("PHI");
280 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
281 const CodeGenInstruction *PHI = &I->second;
282
283 I = getInstructions().find("INLINEASM");
284 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
285 const CodeGenInstruction *INLINEASM = &I->second;
286
Dan Gohmanfa607c92008-07-01 00:05:16 +0000287 I = getInstructions().find("DBG_LABEL");
288 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
289 const CodeGenInstruction *DBG_LABEL = &I->second;
290
291 I = getInstructions().find("EH_LABEL");
292 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
293 const CodeGenInstruction *EH_LABEL = &I->second;
294
295 I = getInstructions().find("GC_LABEL");
296 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
297 const CodeGenInstruction *GC_LABEL = &I->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298
Evan Cheng2e28d622008-02-02 04:07:54 +0000299 I = getInstructions().find("DECLARE");
300 if (I == Instructions.end()) throw "Could not find 'DECLARE' instruction!";
301 const CodeGenInstruction *DECLARE = &I->second;
302
Christopher Lamb071a2a72007-07-26 07:48:21 +0000303 I = getInstructions().find("EXTRACT_SUBREG");
304 if (I == Instructions.end())
305 throw "Could not find 'EXTRACT_SUBREG' instruction!";
306 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
307
308 I = getInstructions().find("INSERT_SUBREG");
309 if (I == Instructions.end())
310 throw "Could not find 'INSERT_SUBREG' instruction!";
311 const CodeGenInstruction *INSERT_SUBREG = &I->second;
312
Evan Cheng3c0eda52008-03-15 00:03:38 +0000313 I = getInstructions().find("IMPLICIT_DEF");
314 if (I == Instructions.end())
315 throw "Could not find 'IMPLICIT_DEF' instruction!";
316 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
317
Christopher Lamb76d72da2008-03-16 03:12:01 +0000318 I = getInstructions().find("SUBREG_TO_REG");
319 if (I == Instructions.end())
320 throw "Could not find 'SUBREG_TO_REG' instruction!";
321 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000322
Dan Gohman4c10fc72009-04-13 21:06:25 +0000323 I = getInstructions().find("COPY_TO_REGCLASS");
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000324 if (I == Instructions.end())
Dan Gohman4c10fc72009-04-13 21:06:25 +0000325 throw "Could not find 'COPY_TO_REGCLASS' instruction!";
326 const CodeGenInstruction *COPY_TO_REGCLASS = &I->second;
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000327
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 // Print out the rest of the instructions now.
329 NumberedInstructions.push_back(PHI);
330 NumberedInstructions.push_back(INLINEASM);
Dan Gohmanfa607c92008-07-01 00:05:16 +0000331 NumberedInstructions.push_back(DBG_LABEL);
332 NumberedInstructions.push_back(EH_LABEL);
333 NumberedInstructions.push_back(GC_LABEL);
Evan Cheng2e28d622008-02-02 04:07:54 +0000334 NumberedInstructions.push_back(DECLARE);
Christopher Lamb071a2a72007-07-26 07:48:21 +0000335 NumberedInstructions.push_back(EXTRACT_SUBREG);
336 NumberedInstructions.push_back(INSERT_SUBREG);
Evan Cheng3c0eda52008-03-15 00:03:38 +0000337 NumberedInstructions.push_back(IMPLICIT_DEF);
Christopher Lamb76d72da2008-03-16 03:12:01 +0000338 NumberedInstructions.push_back(SUBREG_TO_REG);
Dan Gohman4c10fc72009-04-13 21:06:25 +0000339 NumberedInstructions.push_back(COPY_TO_REGCLASS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
341 if (&II->second != PHI &&
342 &II->second != INLINEASM &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000343 &II->second != DBG_LABEL &&
344 &II->second != EH_LABEL &&
345 &II->second != GC_LABEL &&
Evan Cheng2e28d622008-02-02 04:07:54 +0000346 &II->second != DECLARE &&
Christopher Lamb071a2a72007-07-26 07:48:21 +0000347 &II->second != EXTRACT_SUBREG &&
Evan Cheng3c0eda52008-03-15 00:03:38 +0000348 &II->second != INSERT_SUBREG &&
Christopher Lamb76d72da2008-03-16 03:12:01 +0000349 &II->second != IMPLICIT_DEF &&
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000350 &II->second != SUBREG_TO_REG &&
Dan Gohman4c10fc72009-04-13 21:06:25 +0000351 &II->second != COPY_TO_REGCLASS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 NumberedInstructions.push_back(&II->second);
353}
354
355
356/// isLittleEndianEncoding - Return whether this target encodes its instruction
357/// in little-endian format, i.e. bits laid out in the order [0..n]
358///
359bool CodeGenTarget::isLittleEndianEncoding() const {
360 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
361}
362
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363//===----------------------------------------------------------------------===//
364// ComplexPattern implementation
365//
366ComplexPattern::ComplexPattern(Record *R) {
367 Ty = ::getValueType(R->getValueAsDef("Ty"));
368 NumOperands = R->getValueAsInt("NumOperands");
369 SelectFunc = R->getValueAsString("SelectFunc");
370 RootNodes = R->getValueAsListOfDefs("RootNodes");
371
372 // Parse the properties.
373 Properties = 0;
374 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
375 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
376 if (PropList[i]->getName() == "SDNPHasChain") {
377 Properties |= 1 << SDNPHasChain;
378 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
379 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerc90ee9c2008-01-10 07:59:24 +0000380 } else if (PropList[i]->getName() == "SDNPMayStore") {
381 Properties |= 1 << SDNPMayStore;
382 } else if (PropList[i]->getName() == "SDNPMayLoad") {
383 Properties |= 1 << SDNPMayLoad;
384 } else if (PropList[i]->getName() == "SDNPSideEffect") {
385 Properties |= 1 << SDNPSideEffect;
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000386 } else if (PropList[i]->getName() == "SDNPMemOperand") {
387 Properties |= 1 << SDNPMemOperand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 } else {
Daniel Dunbard4287062009-07-03 00:10:29 +0000389 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
390 << "' on ComplexPattern '" << R->getName() << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 exit(1);
392 }
Christopher Lamb059c7c92008-01-31 07:27:46 +0000393
394 // Parse the attributes.
395 Attributes = 0;
396 PropList = R->getValueAsListOfDefs("Attributes");
397 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
398 if (PropList[i]->getName() == "CPAttrParentAsRoot") {
399 Attributes |= 1 << CPAttrParentAsRoot;
400 } else {
Daniel Dunbard4287062009-07-03 00:10:29 +0000401 errs() << "Unsupported pattern attribute '" << PropList[i]->getName()
402 << "' on ComplexPattern '" << R->getName() << "'!\n";
Christopher Lamb059c7c92008-01-31 07:27:46 +0000403 exit(1);
404 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405}
406
407//===----------------------------------------------------------------------===//
408// CodeGenIntrinsic Implementation
409//===----------------------------------------------------------------------===//
410
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000411std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
412 bool TargetOnly) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
414
415 std::vector<CodeGenIntrinsic> Result;
416
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000417 for (unsigned i = 0, e = I.size(); i != e; ++i) {
418 bool isTarget = I[i]->getValueAsBit("isTarget");
419 if (isTarget == TargetOnly)
420 Result.push_back(CodeGenIntrinsic(I[i]));
421 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 return Result;
423}
424
Dan Gohman907df572008-04-03 00:02:49 +0000425CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 TheDef = R;
427 std::string DefName = R->getName();
428 ModRef = WriteMem;
429 isOverloaded = false;
Evan Chengbb46d262008-06-16 20:29:38 +0000430 isCommutative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431
432 if (DefName.size() <= 4 ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000433 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendling9b4eab82008-11-13 09:08:33 +0000435
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendling9b4eab82008-11-13 09:08:33 +0000437
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
439 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendling9b4eab82008-11-13 09:08:33 +0000440
441 TargetPrefix = R->getValueAsString("TargetPrefix");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 Name = R->getValueAsString("LLVMName");
Bill Wendling9b4eab82008-11-13 09:08:33 +0000443
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 if (Name == "") {
445 // If an explicit name isn't specified, derive one from the DefName.
446 Name = "llvm.";
Bill Wendling9b4eab82008-11-13 09:08:33 +0000447
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendling9b4eab82008-11-13 09:08:33 +0000449 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 } else {
451 // Verify it starts with "llvm.".
452 if (Name.size() <= 5 ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000453 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
455 }
456
457 // If TargetPrefix is specified, make sure that Name starts with
458 // "llvm.<targetprefix>.".
459 if (!TargetPrefix.empty()) {
460 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000461 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
462 != (TargetPrefix + "."))
463 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 TargetPrefix + ".'!";
465 }
466
Bill Wendling9b4eab82008-11-13 09:08:33 +0000467 // Parse the list of return types.
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000468 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendling9b4eab82008-11-13 09:08:33 +0000469 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
471 Record *TyEl = TypeList->getElementAsRecord(i);
472 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Bob Wilsonc7245f42009-01-07 00:09:01 +0000473 MVT::SimpleValueType VT;
474 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000475 unsigned MatchTy = TyEl->getValueAsInt("Number");
476 assert(MatchTy < OverloadedVTs.size() &&
477 "Invalid matching number!");
478 VT = OverloadedVTs[MatchTy];
Bob Wilsonc7245f42009-01-07 00:09:01 +0000479 // It only makes sense to use the extended and truncated vector element
480 // variants with iAny types; otherwise, if the intrinsic is not
481 // overloaded, all the types can be specified directly.
482 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
483 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
484 VT == MVT::iAny) && "Expected iAny type");
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000485 } else {
Bob Wilsonc7245f42009-01-07 00:09:01 +0000486 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000487 }
488 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny) {
489 OverloadedVTs.push_back(VT);
490 isOverloaded |= true;
491 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000492 IS.RetVTs.push_back(VT);
493 IS.RetTypeDefs.push_back(TyEl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000495
496 if (IS.RetVTs.size() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
498
Bill Wendling9b4eab82008-11-13 09:08:33 +0000499 // Parse the list of parameter types.
500 TypeList = R->getValueAsListInit("ParamTypes");
501 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
502 Record *TyEl = TypeList->getElementAsRecord(i);
503 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Bob Wilsonc7245f42009-01-07 00:09:01 +0000504 MVT::SimpleValueType VT;
505 if (TyEl->isSubClassOf("LLVMMatchType")) {
506 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000507 assert(MatchTy < OverloadedVTs.size() &&
508 "Invalid matching number!");
509 VT = OverloadedVTs[MatchTy];
Bob Wilsonc7245f42009-01-07 00:09:01 +0000510 // It only makes sense to use the extended and truncated vector element
511 // variants with iAny types; otherwise, if the intrinsic is not
512 // overloaded, all the types can be specified directly.
513 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
514 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
515 VT == MVT::iAny) && "Expected iAny type");
516 } else
517 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000518 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny) {
519 OverloadedVTs.push_back(VT);
520 isOverloaded |= true;
521 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000522 IS.ParamVTs.push_back(VT);
523 IS.ParamTypeDefs.push_back(TyEl);
524 }
525
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 // Parse the intrinsic properties.
527 ListInit *PropList = R->getValueAsListInit("Properties");
528 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
529 Record *Property = PropList->getElementAsRecord(i);
530 assert(Property->isSubClassOf("IntrinsicProperty") &&
531 "Expected a property!");
532
533 if (Property->getName() == "IntrNoMem")
534 ModRef = NoMem;
535 else if (Property->getName() == "IntrReadArgMem")
536 ModRef = ReadArgMem;
537 else if (Property->getName() == "IntrReadMem")
538 ModRef = ReadMem;
539 else if (Property->getName() == "IntrWriteArgMem")
540 ModRef = WriteArgMem;
541 else if (Property->getName() == "IntrWriteMem")
542 ModRef = WriteMem;
Evan Chengbb46d262008-06-16 20:29:38 +0000543 else if (Property->getName() == "Commutative")
544 isCommutative = true;
Chris Lattner1b0090f2009-01-12 01:12:03 +0000545 else if (Property->isSubClassOf("NoCapture")) {
546 unsigned ArgNo = Property->getValueAsInt("ArgNo");
547 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
548 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 assert(0 && "Unknown property!");
550 }
551}