blob: 79bc30d8c9ada4a13a827f0ca645dce238919668 [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 {
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000123 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
Chris Lattner6a91b182010-03-19 01:00:55 +0000124 // Make sure not to pick up "TargetOpcode" by accidentally getting
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000125 // the namespace off the PHI instruction or something.
Chris Lattner6a91b182010-03-19 01:00:55 +0000126 if ((*i)->Namespace != "TargetOpcode")
127 return (*i)->Namespace;
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000128 }
129
Chris Lattner6a91b182010-03-19 01:00:55 +0000130 return "";
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000131}
132
Chris Lattner45872072003-08-07 05:38:11 +0000133Record *CodeGenTarget::getInstructionSet() const {
134 return TargetRec->getValueAsDef("InstructionSet");
135}
Brian Gaeked0fde302003-11-11 22:41:34 +0000136
Chris Lattnerf30187a2010-03-19 00:07:20 +0000137
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000138/// getAsmParser - Return the AssemblyParser definition for this target.
139///
140Record *CodeGenTarget::getAsmParser() const {
141 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
142 if (AsmParserNum >= LI.size())
143 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
144 return LI[AsmParserNum];
145}
146
Chris Lattner175580c2004-08-14 22:50:53 +0000147/// getAsmWriter - Return the AssemblyWriter definition for this target.
148///
149Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000150 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
151 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000152 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000153 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000154}
155
Chris Lattner26693112004-08-16 01:10:21 +0000156void CodeGenTarget::ReadRegisters() const {
157 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
158 if (Regs.empty())
159 throw std::string("No 'Register' subclasses defined!");
160
161 Registers.reserve(Regs.size());
162 Registers.assign(Regs.begin(), Regs.end());
163}
164
Chris Lattner7a680c62004-08-21 02:24:57 +0000165CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
166 DeclaredSpillSize = R->getValueAsInt("SpillSize");
167 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
168}
169
Chris Lattner26693112004-08-16 01:10:21 +0000170const std::string &CodeGenRegister::getName() const {
171 return TheDef->getName();
172}
173
Chris Lattner056afef2004-08-21 04:05:00 +0000174void CodeGenTarget::ReadRegisterClasses() const {
175 std::vector<Record*> RegClasses =
176 Records.getAllDerivedDefinitions("RegisterClass");
177 if (RegClasses.empty())
178 throw std::string("No 'RegisterClass' subclasses defined!");
179
180 RegisterClasses.reserve(RegClasses.size());
181 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
182}
183
Chris Lattner2cacec52010-03-15 06:00:16 +0000184std::vector<MVT::SimpleValueType> CodeGenTarget::
185getRegisterVTs(Record *R) const {
186 std::vector<MVT::SimpleValueType> Result;
Evan Cheng44a65fa2006-05-16 07:05:30 +0000187 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
188 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
189 const CodeGenRegisterClass &RC = RegisterClasses[i];
190 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
191 if (R == RC.Elements[ei]) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Chris Lattner2cacec52010-03-15 06:00:16 +0000193 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Evan Cheng44a65fa2006-05-16 07:05:30 +0000194 }
195 }
196 }
197 return Result;
198}
199
200
Chris Lattner056afef2004-08-21 04:05:00 +0000201CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000202 // Rename anonymous register classes.
203 if (R->getName().size() > 9 && R->getName()[9] == '.') {
204 static unsigned AnonCounter = 0;
205 R->setName("AnonRegClass_"+utostr(AnonCounter++));
206 }
207
Nate Begeman6510b222005-12-01 04:51:06 +0000208 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
209 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
210 Record *Type = TypeList[i];
211 if (!Type->isSubClassOf("ValueType"))
212 throw "RegTypes list member '" + Type->getName() +
213 "' does not derive from the ValueType class!";
214 VTs.push_back(getValueType(Type));
215 }
216 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000217
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000218 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
219 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
220 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000221 if (!Reg->isSubClassOf("Register"))
222 throw "Register Class member '" + Reg->getName() +
223 "' does not derive from the Register class!";
224 Elements.push_back(Reg);
225 }
Nate Begeman6510b222005-12-01 04:51:06 +0000226
Christopher Lamba3211252007-06-13 22:20:15 +0000227 std::vector<Record*> SubRegClassList =
228 R->getValueAsListOfDefs("SubRegClassList");
229 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
230 Record *SubRegClass = SubRegClassList[i];
231 if (!SubRegClass->isSubClassOf("RegisterClass"))
232 throw "Register Class member '" + SubRegClass->getName() +
233 "' does not derive from the RegisterClass class!";
234 SubRegClasses.push_back(SubRegClass);
235 }
236
Nate Begeman6510b222005-12-01 04:51:06 +0000237 // Allow targets to override the size in bits of the RegisterClass.
238 unsigned Size = R->getValueAsInt("Size");
239
240 Namespace = R->getValueAsString("Namespace");
Owen Andersone50ed302009-08-10 22:56:29 +0000241 SpillSize = Size ? Size : EVT(VTs[0]).getSizeInBits();
Nate Begeman6510b222005-12-01 04:51:06 +0000242 SpillAlignment = R->getValueAsInt("Alignment");
Evan Chenga3ca3142007-09-19 01:35:01 +0000243 CopyCost = R->getValueAsInt("CopyCost");
Nate Begeman6510b222005-12-01 04:51:06 +0000244 MethodBodies = R->getValueAsCode("MethodBodies");
245 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000246}
247
248const std::string &CodeGenRegisterClass::getName() const {
249 return TheDef->getName();
250}
251
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000252void CodeGenTarget::ReadLegalValueTypes() const {
253 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
254 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000255 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
256 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000257
258 // Remove duplicates.
259 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
260 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
261 LegalValueTypes.end()),
262 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000263}
Chris Lattner056afef2004-08-21 04:05:00 +0000264
Chris Lattner175580c2004-08-14 22:50:53 +0000265
Chris Lattnerec352402004-08-01 05:04:00 +0000266void CodeGenTarget::ReadInstructions() const {
267 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000268 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000269 throw std::string("No 'Instruction' subclasses defined!");
270
Chris Lattneraa77d772006-01-27 01:45:06 +0000271 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000272 std::string InstFormatName =
273 getAsmWriter()->getValueAsString("InstFormatName");
274
275 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
276 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
Chris Lattnere14d2e22010-03-19 01:07:44 +0000277 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i], AsmStr);
Chris Lattner175580c2004-08-14 22:50:53 +0000278 }
Chris Lattnerec352402004-08-01 05:04:00 +0000279}
280
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000281static const CodeGenInstruction *
282GetInstByName(const char *Name,
Chris Lattnere14d2e22010-03-19 01:07:44 +0000283 const DenseMap<const Record*, CodeGenInstruction*> &Insts) {
284 const Record *Rec = Records.getDef(Name);
285
286 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
287 I = Insts.find(Rec);
288 if (Rec == 0 || I == Insts.end())
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000289 throw std::string("Could not find '") + Name + "' instruction!";
Chris Lattnere14d2e22010-03-19 01:07:44 +0000290 return I->second;
291}
292
293namespace {
294/// SortInstByName - Sorting predicate to sort instructions by name.
295///
296struct SortInstByName {
297 bool operator()(const CodeGenInstruction *Rec1,
298 const CodeGenInstruction *Rec2) const {
299 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
300 }
301};
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000302}
303
Chris Lattnerd6488672005-01-22 18:58:51 +0000304/// getInstructionsByEnumValue - Return all of the instructions defined by the
305/// target, ordered by their enum value.
Chris Lattner6a91b182010-03-19 01:00:55 +0000306void CodeGenTarget::ComputeInstrsByEnum() const {
Chris Lattnere14d2e22010-03-19 01:07:44 +0000307 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000308 const CodeGenInstruction *PHI = GetInstByName("PHI", Insts);
309 const CodeGenInstruction *INLINEASM = GetInstByName("INLINEASM", Insts);
310 const CodeGenInstruction *DBG_LABEL = GetInstByName("DBG_LABEL", Insts);
311 const CodeGenInstruction *EH_LABEL = GetInstByName("EH_LABEL", Insts);
312 const CodeGenInstruction *GC_LABEL = GetInstByName("GC_LABEL", Insts);
313 const CodeGenInstruction *KILL = GetInstByName("KILL", Insts);
314 const CodeGenInstruction *EXTRACT_SUBREG =
315 GetInstByName("EXTRACT_SUBREG", Insts);
316 const CodeGenInstruction *INSERT_SUBREG =
317 GetInstByName("INSERT_SUBREG", Insts);
318 const CodeGenInstruction *IMPLICIT_DEF = GetInstByName("IMPLICIT_DEF", Insts);
319 const CodeGenInstruction *SUBREG_TO_REG =
320 GetInstByName("SUBREG_TO_REG", Insts);
321 const CodeGenInstruction *COPY_TO_REGCLASS =
322 GetInstByName("COPY_TO_REGCLASS", Insts);
323 const CodeGenInstruction *DBG_VALUE = GetInstByName("DBG_VALUE", Insts);
Dale Johannesen87563b32010-01-08 23:51:25 +0000324
Chris Lattnerd6488672005-01-22 18:58:51 +0000325 // Print out the rest of the instructions now.
Chris Lattnerf6502782010-03-19 00:34:35 +0000326 InstrsByEnum.push_back(PHI);
327 InstrsByEnum.push_back(INLINEASM);
328 InstrsByEnum.push_back(DBG_LABEL);
329 InstrsByEnum.push_back(EH_LABEL);
330 InstrsByEnum.push_back(GC_LABEL);
331 InstrsByEnum.push_back(KILL);
332 InstrsByEnum.push_back(EXTRACT_SUBREG);
333 InstrsByEnum.push_back(INSERT_SUBREG);
334 InstrsByEnum.push_back(IMPLICIT_DEF);
335 InstrsByEnum.push_back(SUBREG_TO_REG);
336 InstrsByEnum.push_back(COPY_TO_REGCLASS);
337 InstrsByEnum.push_back(DBG_VALUE);
Chris Lattner6a91b182010-03-19 01:00:55 +0000338
Chris Lattnere14d2e22010-03-19 01:07:44 +0000339 unsigned EndOfPredefines = InstrsByEnum.size();
Chris Lattner6a91b182010-03-19 01:00:55 +0000340
Chris Lattnere14d2e22010-03-19 01:07:44 +0000341 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
342 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
343 const CodeGenInstruction *CGI = I->second;
Chris Lattner6a91b182010-03-19 01:00:55 +0000344 if (CGI != PHI &&
345 CGI != INLINEASM &&
346 CGI != DBG_LABEL &&
347 CGI != EH_LABEL &&
348 CGI != GC_LABEL &&
349 CGI != KILL &&
350 CGI != EXTRACT_SUBREG &&
351 CGI != INSERT_SUBREG &&
352 CGI != IMPLICIT_DEF &&
353 CGI != SUBREG_TO_REG &&
354 CGI != COPY_TO_REGCLASS &&
355 CGI != DBG_VALUE)
356 InstrsByEnum.push_back(CGI);
357 }
Chris Lattnere14d2e22010-03-19 01:07:44 +0000358
359 // All of the instructions are now in random order based on the map iteration.
360 // Sort them by name.
361 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(),
362 SortInstByName());
Chris Lattnerd6488672005-01-22 18:58:51 +0000363}
364
365
Misha Brukman35e83cc2004-10-14 05:50:43 +0000366/// isLittleEndianEncoding - Return whether this target encodes its instruction
367/// in little-endian format, i.e. bits laid out in the order [0..n]
368///
369bool CodeGenTarget::isLittleEndianEncoding() const {
370 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
371}
372
Evan Cheng0fc71982005-12-08 02:00:36 +0000373//===----------------------------------------------------------------------===//
374// ComplexPattern implementation
375//
376ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000377 Ty = ::getValueType(R->getValueAsDef("Ty"));
378 NumOperands = R->getValueAsInt("NumOperands");
379 SelectFunc = R->getValueAsString("SelectFunc");
380 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000381
382 // Parse the properties.
383 Properties = 0;
384 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
385 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
386 if (PropList[i]->getName() == "SDNPHasChain") {
387 Properties |= 1 << SDNPHasChain;
388 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
389 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerba7e7562008-01-10 07:59:24 +0000390 } else if (PropList[i]->getName() == "SDNPMayStore") {
391 Properties |= 1 << SDNPMayStore;
392 } else if (PropList[i]->getName() == "SDNPMayLoad") {
393 Properties |= 1 << SDNPMayLoad;
394 } else if (PropList[i]->getName() == "SDNPSideEffect") {
395 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000396 } else if (PropList[i]->getName() == "SDNPMemOperand") {
397 Properties |= 1 << SDNPMemOperand;
Chris Lattnere8cabf32010-03-19 05:07:09 +0000398 } else if (PropList[i]->getName() == "SDNPVariadic") {
399 Properties |= 1 << SDNPVariadic;
Evan Cheng94b30402006-10-11 21:02:01 +0000400 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000401 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
402 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000403 exit(1);
404 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000405}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000406
Chris Lattner43fbbc32006-03-24 19:49:31 +0000407//===----------------------------------------------------------------------===//
408// CodeGenIntrinsic Implementation
409//===----------------------------------------------------------------------===//
410
Dale Johannesen49de9822009-02-05 01:49:45 +0000411std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
412 bool TargetOnly) {
Chris Lattner43fbbc32006-03-24 19:49:31 +0000413 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000414
415 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000416
Dale Johannesen49de9822009-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 }
Chris Lattner8850a1b2006-03-27 22:48:18 +0000422 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000423}
424
Dan Gohmanee4fa192008-04-03 00:02:49 +0000425CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000426 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000427 std::string DefName = R->getName();
428 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000429 isOverloaded = false;
Evan Cheng6bd95672008-06-16 20:29:38 +0000430 isCommutative = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000431
432 if (DefName.size() <= 4 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000433 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000434 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000435
Chris Lattner43fbbc32006-03-24 19:49:31 +0000436 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000437
Chris Lattner43fbbc32006-03-24 19:49:31 +0000438 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
439 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000440
441 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000442 Name = R->getValueAsString("LLVMName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000443
Chris Lattner43fbbc32006-03-24 19:49:31 +0000444 if (Name == "") {
445 // If an explicit name isn't specified, derive one from the DefName.
446 Name = "llvm.";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000447
Chris Lattner43fbbc32006-03-24 19:49:31 +0000448 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000449 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner43fbbc32006-03-24 19:49:31 +0000450 } else {
451 // Verify it starts with "llvm.".
452 if (Name.size() <= 5 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000453 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Chris Lattner43fbbc32006-03-24 19:49:31 +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 Wendlingcdcc3e62008-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." +
Chris Lattner43fbbc32006-03-24 19:49:31 +0000464 TargetPrefix + ".'!";
465 }
466
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000467 // Parse the list of return types.
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000469 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000470 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000471 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000472 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000474 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsond305e702009-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 Wilsonbc039792009-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")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000485 "Expected iAny or vAny type");
Bob Wilsond305e702009-04-16 21:51:05 +0000486 } else {
Bob Wilsonbc039792009-01-07 00:09:01 +0000487 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsond305e702009-04-16 21:51:05 +0000488 }
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000489 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000490 OverloadedVTs.push_back(VT);
491 isOverloaded |= true;
492 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000493 IS.RetVTs.push_back(VT);
494 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000495 }
Eric Christopherd7de5402010-03-19 23:04:23 +0000496
497 if (IS.RetVTs.size() == 0)
498 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000499
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000500 // Parse the list of parameter types.
501 TypeList = R->getValueAsListInit("ParamTypes");
502 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
503 Record *TyEl = TypeList->getElementAsRecord(i);
504 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000506 if (TyEl->isSubClassOf("LLVMMatchType")) {
507 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsond305e702009-04-16 21:51:05 +0000508 assert(MatchTy < OverloadedVTs.size() &&
509 "Invalid matching number!");
510 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000511 // It only makes sense to use the extended and truncated vector element
512 // variants with iAny types; otherwise, if the intrinsic is not
513 // overloaded, all the types can be specified directly.
514 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
515 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000517 "Expected iAny or vAny type");
Bob Wilsonbc039792009-01-07 00:09:01 +0000518 } else
519 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000520 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000521 OverloadedVTs.push_back(VT);
522 isOverloaded |= true;
523 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000524 IS.ParamVTs.push_back(VT);
525 IS.ParamTypeDefs.push_back(TyEl);
526 }
527
Chris Lattner43fbbc32006-03-24 19:49:31 +0000528 // Parse the intrinsic properties.
529 ListInit *PropList = R->getValueAsListInit("Properties");
530 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000531 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000532 assert(Property->isSubClassOf("IntrinsicProperty") &&
533 "Expected a property!");
534
Chris Lattner4b2362e2006-04-10 22:02:59 +0000535 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000536 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000537 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000538 ModRef = ReadArgMem;
539 else if (Property->getName() == "IntrReadMem")
540 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000541 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000542 ModRef = WriteArgMem;
543 else if (Property->getName() == "IntrWriteMem")
544 ModRef = WriteMem;
Evan Cheng6bd95672008-06-16 20:29:38 +0000545 else if (Property->getName() == "Commutative")
546 isCommutative = true;
Chris Lattnera62c3022009-01-12 01:12:03 +0000547 else if (Property->isSubClassOf("NoCapture")) {
548 unsigned ArgNo = Property->getValueAsInt("ArgNo");
549 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
550 } else
Chris Lattner43fbbc32006-03-24 19:49:31 +0000551 assert(0 && "Unknown property!");
552 }
553}