blob: 6751ae7ca8508e72d3934ecf7ac72bf86e17808c [file] [log] [blame]
Chris Lattnerea2d52d2008-01-06 01:35:39 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswelld3032032003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswelld3032032003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
Chris Lattnerea2d52d2008-01-06 01:35:39 +000010// This class wraps target description classes used by the various code
Chris Lattnerf5bd1b72003-10-05 19:27:59 +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 Lattnerfce96032004-08-01 04:04:35 +000017#include "CodeGenTarget.h"
Chris Lattner2c581412006-03-24 19:49:31 +000018#include "CodeGenIntrinsics.h"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000019#include "Record.h"
Chris Lattner91c538f2004-10-03 19:34:31 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner4a5f7be2010-03-27 20:32:26 +000021#include "llvm/ADT/STLExtras.h"
Chris Lattner91c538f2004-10-03 19:34:31 +000022#include "llvm/Support/CommandLine.h"
Chris Lattner90c5b9c2005-10-14 03:54:49 +000023#include <algorithm>
Chris Lattner68478662004-08-01 03:55:39 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner91c538f2004-10-03 19:34:31 +000026static cl::opt<unsigned>
Daniel Dunbar00331992009-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>
Chris Lattner91c538f2004-10-03 19:34:31 +000031AsmWriterNum("asmwriternum", cl::init(0),
32 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
33
Owen Anderson9f944592009-08-11 20:47:22 +000034/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands13237ac2008-06-06 12:08:01 +000035/// record corresponds to.
Owen Anderson9f944592009-08-11 20:47:22 +000036MVT::SimpleValueType llvm::getValueType(Record *Rec) {
37 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000038}
39
Owen Anderson9f944592009-08-11 20:47:22 +000040std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000041 switch (T) {
Owen Anderson9f944592009-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 Wilsonbf8e4c12009-07-10 22:25:24 +000045 default: return getEnumName(T);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000046 }
47}
48
Owen Anderson9f944592009-08-11 20:47:22 +000049std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000050 switch (T) {
Bill Wendling02b701f2010-09-07 18:49:14 +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";
Owen Anderson9f944592009-08-11 20:47:22 +000065 case MVT::ppcf128: return "MVT::ppcf128";
Bill Wendling35380212010-09-07 20:03:56 +000066 case MVT::x86mmx: return "MVT::x86mmx";
Chris Lattner3e5fbd72010-12-21 02:38:05 +000067 case MVT::Glue: return "MVT::Glue";
Bill Wendling02b701f2010-09-07 18:49:14 +000068 case MVT::isVoid: return "MVT::isVoid";
69 case MVT::v2i8: return "MVT::v2i8";
70 case MVT::v4i8: return "MVT::v4i8";
71 case MVT::v8i8: return "MVT::v8i8";
72 case MVT::v16i8: return "MVT::v16i8";
73 case MVT::v32i8: return "MVT::v32i8";
74 case MVT::v2i16: return "MVT::v2i16";
75 case MVT::v4i16: return "MVT::v4i16";
76 case MVT::v8i16: return "MVT::v8i16";
77 case MVT::v16i16: return "MVT::v16i16";
78 case MVT::v2i32: return "MVT::v2i32";
79 case MVT::v4i32: return "MVT::v4i32";
80 case MVT::v8i32: return "MVT::v8i32";
81 case MVT::v1i64: return "MVT::v1i64";
82 case MVT::v2i64: return "MVT::v2i64";
83 case MVT::v4i64: return "MVT::v4i64";
84 case MVT::v8i64: return "MVT::v8i64";
85 case MVT::v2f32: return "MVT::v2f32";
86 case MVT::v4f32: return "MVT::v4f32";
87 case MVT::v8f32: return "MVT::v8f32";
88 case MVT::v2f64: return "MVT::v2f64";
89 case MVT::v4f64: return "MVT::v4f64";
Owen Anderson9f944592009-08-11 20:47:22 +000090 case MVT::Metadata: return "MVT::Metadata";
Bill Wendling02b701f2010-09-07 18:49:14 +000091 case MVT::iPTR: return "MVT::iPTR";
Owen Anderson9f944592009-08-11 20:47:22 +000092 case MVT::iPTRAny: return "MVT::iPTRAny";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000093 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
94 }
95}
96
Chris Lattner8cab0212008-01-05 22:25:12 +000097/// getQualifiedName - Return the name of the specified record, with a
98/// namespace qualifier if the record contains one.
99///
100std::string llvm::getQualifiedName(const Record *R) {
Jakob Stoklund Olesenc8c4ded2011-05-07 21:22:39 +0000101 std::string Namespace;
102 if (R->getValue("Namespace"))
103 Namespace = R->getValueAsString("Namespace");
Chris Lattner8cab0212008-01-05 22:25:12 +0000104 if (Namespace.empty()) return R->getName();
105 return Namespace + "::" + R->getName();
106}
107
108
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000109/// getTarget - Return the current instance of the Target class.
110///
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +0000111CodeGenTarget::CodeGenTarget(RecordKeeper &records)
112 : Records(records), RegBank(0) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000113 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanaff10d02004-06-04 14:59:42 +0000114 if (Targets.size() == 0)
Misha Brukman650ba8e2005-04-22 00:00:37 +0000115 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000116 if (Targets.size() != 1)
117 throw std::string("ERROR: Multiple subclasses of Target defined!");
118 TargetRec = Targets[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000119}
120
121
122const std::string &CodeGenTarget::getName() const {
123 return TargetRec->getName();
124}
125
Dan Gohman3e2225d2008-08-20 21:45:57 +0000126std::string CodeGenTarget::getInstNamespace() const {
Dan Gohman3e2225d2008-08-20 21:45:57 +0000127 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
Chris Lattner4763dbe2010-03-19 01:00:55 +0000128 // Make sure not to pick up "TargetOpcode" by accidentally getting
Dan Gohman3e2225d2008-08-20 21:45:57 +0000129 // the namespace off the PHI instruction or something.
Chris Lattner4763dbe2010-03-19 01:00:55 +0000130 if ((*i)->Namespace != "TargetOpcode")
131 return (*i)->Namespace;
Dan Gohman3e2225d2008-08-20 21:45:57 +0000132 }
133
Chris Lattner4763dbe2010-03-19 01:00:55 +0000134 return "";
Dan Gohman3e2225d2008-08-20 21:45:57 +0000135}
136
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000137Record *CodeGenTarget::getInstructionSet() const {
138 return TargetRec->getValueAsDef("InstructionSet");
139}
Brian Gaeke960707c2003-11-11 22:41:34 +0000140
Chris Lattner9aec14b2010-03-19 00:07:20 +0000141
Daniel Dunbar00331992009-07-29 00:02:19 +0000142/// getAsmParser - Return the AssemblyParser definition for this target.
143///
144Record *CodeGenTarget::getAsmParser() const {
145 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
146 if (AsmParserNum >= LI.size())
147 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
148 return LI[AsmParserNum];
149}
150
Chris Lattner6ffa5012004-08-14 22:50:53 +0000151/// getAsmWriter - Return the AssemblyWriter definition for this target.
152///
153Record *CodeGenTarget::getAsmWriter() const {
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000154 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
155 if (AsmWriterNum >= LI.size())
Chris Lattner91c538f2004-10-03 19:34:31 +0000156 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000157 return LI[AsmWriterNum];
Chris Lattner6ffa5012004-08-14 22:50:53 +0000158}
159
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +0000160CodeGenRegBank &CodeGenTarget::getRegBank() const {
161 if (!RegBank)
162 RegBank = new CodeGenRegBank(Records);
163 return *RegBank;
164}
165
Chris Lattner77d3ead2010-11-02 18:10:06 +0000166/// getRegisterByName - If there is a register with the specific AsmName,
167/// return it.
168const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
Jakob Stoklund Olesen84bd44e2011-06-11 00:28:06 +0000169 const std::vector<CodeGenRegister> &Regs = getRegBank().getRegisters();
Chris Lattner77d3ead2010-11-02 18:10:06 +0000170 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
171 const CodeGenRegister &Reg = Regs[i];
172 if (Reg.TheDef->getValueAsString("AsmName") == Name)
173 return &Reg;
174 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000175
Chris Lattner77d3ead2010-11-02 18:10:06 +0000176 return 0;
177}
178
Chris Lattnercabe0372010-03-15 06:00:16 +0000179std::vector<MVT::SimpleValueType> CodeGenTarget::
180getRegisterVTs(Record *R) const {
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000181 const CodeGenRegister *Reg = getRegBank().getReg(R);
Chris Lattnercabe0372010-03-15 06:00:16 +0000182 std::vector<MVT::SimpleValueType> Result;
Evan Chengd985d662006-05-16 07:05:30 +0000183 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
184 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
Jakob Stoklund Olesen22ea4242011-06-15 00:20:40 +0000185 const CodeGenRegisterClass &RC = RCs[i];
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000186 if (RC.contains(Reg)) {
187 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
188 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Evan Chengd985d662006-05-16 07:05:30 +0000189 }
190 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000191
Chris Lattner4a5f7be2010-03-27 20:32:26 +0000192 // Remove duplicates.
193 array_pod_sort(Result.begin(), Result.end());
194 Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
Evan Chengd985d662006-05-16 07:05:30 +0000195 return Result;
196}
197
198
Chris Lattnercee994b2005-09-08 21:43:21 +0000199void CodeGenTarget::ReadLegalValueTypes() const {
200 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
201 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman006bb042005-12-01 04:51:06 +0000202 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
203 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Jim Grosbachf910bf22011-03-11 01:27:24 +0000204
Chris Lattner90c5b9c2005-10-14 03:54:49 +0000205 // Remove duplicates.
206 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
207 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
208 LegalValueTypes.end()),
209 LegalValueTypes.end());
Chris Lattnercee994b2005-09-08 21:43:21 +0000210}
Chris Lattner2a86fab2004-08-21 04:05:00 +0000211
Chris Lattner6ffa5012004-08-14 22:50:53 +0000212
Chris Lattnerc860eca2004-08-01 05:04:00 +0000213void CodeGenTarget::ReadInstructions() const {
214 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattner543fe4b2006-01-27 01:45:06 +0000215 if (Insts.size() <= 2)
Chris Lattnerc860eca2004-08-01 05:04:00 +0000216 throw std::string("No 'Instruction' subclasses defined!");
217
Chris Lattner543fe4b2006-01-27 01:45:06 +0000218 // Parse the instructions defined in the .td file.
Chris Lattnera3977162010-11-01 02:15:23 +0000219 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
220 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i]);
Chris Lattnerc860eca2004-08-01 05:04:00 +0000221}
222
Chris Lattnerc9ae94c2010-03-19 00:23:20 +0000223static const CodeGenInstruction *
224GetInstByName(const char *Name,
Jim Grosbachf910bf22011-03-11 01:27:24 +0000225 const DenseMap<const Record*, CodeGenInstruction*> &Insts,
Chris Lattner77d369c2010-12-13 00:23:57 +0000226 RecordKeeper &Records) {
Chris Lattner1802b172010-03-19 01:07:44 +0000227 const Record *Rec = Records.getDef(Name);
Jim Grosbachf910bf22011-03-11 01:27:24 +0000228
Chris Lattner1802b172010-03-19 01:07:44 +0000229 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
230 I = Insts.find(Rec);
231 if (Rec == 0 || I == Insts.end())
Chris Lattnerc9ae94c2010-03-19 00:23:20 +0000232 throw std::string("Could not find '") + Name + "' instruction!";
Chris Lattner1802b172010-03-19 01:07:44 +0000233 return I->second;
234}
235
236namespace {
237/// SortInstByName - Sorting predicate to sort instructions by name.
238///
239struct SortInstByName {
240 bool operator()(const CodeGenInstruction *Rec1,
241 const CodeGenInstruction *Rec2) const {
242 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
243 }
244};
Chris Lattnerc9ae94c2010-03-19 00:23:20 +0000245}
246
Chris Lattner945e8652005-01-22 18:58:51 +0000247/// getInstructionsByEnumValue - Return all of the instructions defined by the
248/// target, ordered by their enum value.
Chris Lattner4763dbe2010-03-19 01:00:55 +0000249void CodeGenTarget::ComputeInstrsByEnum() const {
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000250 // The ordering here must match the ordering in TargetOpcodes.h.
251 const char *const FixedInstrs[] = {
252 "PHI",
253 "INLINEASM",
Bill Wendling499f7972010-07-16 22:20:36 +0000254 "PROLOG_LABEL",
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000255 "EH_LABEL",
256 "GC_LABEL",
257 "KILL",
258 "EXTRACT_SUBREG",
259 "INSERT_SUBREG",
260 "IMPLICIT_DEF",
261 "SUBREG_TO_REG",
262 "COPY_TO_REGCLASS",
263 "DBG_VALUE",
264 "REG_SEQUENCE",
Jakob Stoklund Olesen676a15b2010-07-02 22:29:50 +0000265 "COPY",
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000266 0
267 };
Chris Lattner1802b172010-03-19 01:07:44 +0000268 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000269 for (const char *const *p = FixedInstrs; *p; ++p) {
Chris Lattner77d369c2010-12-13 00:23:57 +0000270 const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000271 assert(Instr && "Missing target independent instruction");
272 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
273 InstrsByEnum.push_back(Instr);
274 }
Chris Lattner1802b172010-03-19 01:07:44 +0000275 unsigned EndOfPredefines = InstrsByEnum.size();
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000276
Chris Lattner1802b172010-03-19 01:07:44 +0000277 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
278 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
279 const CodeGenInstruction *CGI = I->second;
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000280 if (CGI->Namespace != "TargetOpcode")
Chris Lattner4763dbe2010-03-19 01:00:55 +0000281 InstrsByEnum.push_back(CGI);
282 }
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000283
284 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
285
Chris Lattner1802b172010-03-19 01:07:44 +0000286 // All of the instructions are now in random order based on the map iteration.
287 // Sort them by name.
288 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(),
289 SortInstByName());
Chris Lattner945e8652005-01-22 18:58:51 +0000290}
291
292
Misha Brukman243ded52004-10-14 05:50:43 +0000293/// isLittleEndianEncoding - Return whether this target encodes its instruction
294/// in little-endian format, i.e. bits laid out in the order [0..n]
295///
296bool CodeGenTarget::isLittleEndianEncoding() const {
297 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
298}
299
Evan Cheng9b9567b2005-12-08 02:00:36 +0000300//===----------------------------------------------------------------------===//
301// ComplexPattern implementation
302//
303ComplexPattern::ComplexPattern(Record *R) {
Evan Chengc9a62002005-12-08 02:14:08 +0000304 Ty = ::getValueType(R->getValueAsDef("Ty"));
305 NumOperands = R->getValueAsInt("NumOperands");
306 SelectFunc = R->getValueAsString("SelectFunc");
307 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng2022c792006-10-11 21:02:01 +0000308
309 // Parse the properties.
310 Properties = 0;
311 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
312 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
313 if (PropList[i]->getName() == "SDNPHasChain") {
314 Properties |= 1 << SDNPHasChain;
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000315 } else if (PropList[i]->getName() == "SDNPOptInGlue") {
316 Properties |= 1 << SDNPOptInGlue;
Chris Lattner317332f2008-01-10 07:59:24 +0000317 } else if (PropList[i]->getName() == "SDNPMayStore") {
318 Properties |= 1 << SDNPMayStore;
319 } else if (PropList[i]->getName() == "SDNPMayLoad") {
320 Properties |= 1 << SDNPMayLoad;
321 } else if (PropList[i]->getName() == "SDNPSideEffect") {
322 Properties |= 1 << SDNPSideEffect;
Mon P Wang6a490372008-06-25 08:15:39 +0000323 } else if (PropList[i]->getName() == "SDNPMemOperand") {
324 Properties |= 1 << SDNPMemOperand;
Chris Lattner83aeaab2010-03-19 05:07:09 +0000325 } else if (PropList[i]->getName() == "SDNPVariadic") {
326 Properties |= 1 << SDNPVariadic;
Chris Lattner0e023ea2010-09-21 20:31:19 +0000327 } else if (PropList[i]->getName() == "SDNPWantRoot") {
328 Properties |= 1 << SDNPWantRoot;
329 } else if (PropList[i]->getName() == "SDNPWantParent") {
330 Properties |= 1 << SDNPWantParent;
Evan Cheng2022c792006-10-11 21:02:01 +0000331 } else {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000332 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
333 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng2022c792006-10-11 21:02:01 +0000334 exit(1);
335 }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000336}
Evan Chengc9a62002005-12-08 02:14:08 +0000337
Chris Lattner2c581412006-03-24 19:49:31 +0000338//===----------------------------------------------------------------------===//
339// CodeGenIntrinsic Implementation
340//===----------------------------------------------------------------------===//
341
Dale Johannesenb842d522009-02-05 01:49:45 +0000342std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
343 bool TargetOnly) {
Chris Lattner2c581412006-03-24 19:49:31 +0000344 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000345
Chris Lattnerc92f6882006-03-27 22:48:18 +0000346 std::vector<CodeGenIntrinsic> Result;
Chris Lattner8a2ae8b72006-03-28 00:15:00 +0000347
Dale Johannesenb842d522009-02-05 01:49:45 +0000348 for (unsigned i = 0, e = I.size(); i != e; ++i) {
349 bool isTarget = I[i]->getValueAsBit("isTarget");
350 if (isTarget == TargetOnly)
351 Result.push_back(CodeGenIntrinsic(I[i]));
352 }
Chris Lattnerc92f6882006-03-27 22:48:18 +0000353 return Result;
Chris Lattner2c581412006-03-24 19:49:31 +0000354}
355
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000356CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattnerbbba8232006-03-24 20:25:01 +0000357 TheDef = R;
Chris Lattner2c581412006-03-24 19:49:31 +0000358 std::string DefName = R->getName();
Dan Gohmanddb2d652010-08-05 23:36:21 +0000359 ModRef = ReadWriteMem;
Reid Spencere67d0c22007-04-01 07:20:02 +0000360 isOverloaded = false;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000361 isCommutative = false;
John McCall375dcc92011-05-28 06:31:34 +0000362 canThrow = false;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000363
364 if (DefName.size() <= 4 ||
Bill Wendling91821472008-11-13 09:08:33 +0000365 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Chris Lattner2c581412006-03-24 19:49:31 +0000366 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendling91821472008-11-13 09:08:33 +0000367
Chris Lattner2c581412006-03-24 19:49:31 +0000368 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendling91821472008-11-13 09:08:33 +0000369
Chris Lattner2c581412006-03-24 19:49:31 +0000370 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
371 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendling91821472008-11-13 09:08:33 +0000372
373 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner2c581412006-03-24 19:49:31 +0000374 Name = R->getValueAsString("LLVMName");
Bill Wendling91821472008-11-13 09:08:33 +0000375
Chris Lattner2c581412006-03-24 19:49:31 +0000376 if (Name == "") {
377 // If an explicit name isn't specified, derive one from the DefName.
378 Name = "llvm.";
Bill Wendling91821472008-11-13 09:08:33 +0000379
Chris Lattner2c581412006-03-24 19:49:31 +0000380 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendling91821472008-11-13 09:08:33 +0000381 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner2c581412006-03-24 19:49:31 +0000382 } else {
383 // Verify it starts with "llvm.".
Jim Grosbachf910bf22011-03-11 01:27:24 +0000384 if (Name.size() <= 5 ||
Bill Wendling91821472008-11-13 09:08:33 +0000385 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Chris Lattner2c581412006-03-24 19:49:31 +0000386 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
387 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000388
Chris Lattner2c581412006-03-24 19:49:31 +0000389 // If TargetPrefix is specified, make sure that Name starts with
390 // "llvm.<targetprefix>.".
391 if (!TargetPrefix.empty()) {
392 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendling91821472008-11-13 09:08:33 +0000393 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
394 != (TargetPrefix + "."))
395 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Chris Lattner2c581412006-03-24 19:49:31 +0000396 TargetPrefix + ".'!";
397 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000398
Bill Wendling91821472008-11-13 09:08:33 +0000399 // Parse the list of return types.
Owen Anderson9f944592009-08-11 20:47:22 +0000400 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendling91821472008-11-13 09:08:33 +0000401 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner2c581412006-03-24 19:49:31 +0000402 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattnercbebe462007-02-27 22:08:27 +0000403 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner2c581412006-03-24 19:49:31 +0000404 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson9f944592009-08-11 20:47:22 +0000405 MVT::SimpleValueType VT;
Bob Wilsonf76486a2009-01-07 00:09:01 +0000406 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000407 unsigned MatchTy = TyEl->getValueAsInt("Number");
408 assert(MatchTy < OverloadedVTs.size() &&
409 "Invalid matching number!");
410 VT = OverloadedVTs[MatchTy];
Bob Wilsonf76486a2009-01-07 00:09:01 +0000411 // It only makes sense to use the extended and truncated vector element
412 // variants with iAny types; otherwise, if the intrinsic is not
413 // overloaded, all the types can be specified directly.
414 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
415 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson9f944592009-08-11 20:47:22 +0000416 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson781797f2009-08-11 05:03:38 +0000417 "Expected iAny or vAny type");
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000418 } else {
Bob Wilsonf76486a2009-01-07 00:09:01 +0000419 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000420 }
Bob Wilson2cd5da82009-08-11 01:14:02 +0000421 if (EVT(VT).isOverloaded()) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000422 OverloadedVTs.push_back(VT);
Chris Lattnerb424faa2010-03-23 23:46:27 +0000423 isOverloaded = true;
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000424 }
Chris Lattnerb424faa2010-03-23 23:46:27 +0000425
426 // Reject invalid types.
427 if (VT == MVT::isVoid)
428 throw "Intrinsic '" + DefName + " has void in result type list!";
Jim Grosbachf910bf22011-03-11 01:27:24 +0000429
Bill Wendling91821472008-11-13 09:08:33 +0000430 IS.RetVTs.push_back(VT);
431 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner2c581412006-03-24 19:49:31 +0000432 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000433
Bill Wendling91821472008-11-13 09:08:33 +0000434 // Parse the list of parameter types.
435 TypeList = R->getValueAsListInit("ParamTypes");
436 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
437 Record *TyEl = TypeList->getElementAsRecord(i);
438 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson9f944592009-08-11 20:47:22 +0000439 MVT::SimpleValueType VT;
Bob Wilsonf76486a2009-01-07 00:09:01 +0000440 if (TyEl->isSubClassOf("LLVMMatchType")) {
441 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000442 assert(MatchTy < OverloadedVTs.size() &&
443 "Invalid matching number!");
444 VT = OverloadedVTs[MatchTy];
Bob Wilsonf76486a2009-01-07 00:09:01 +0000445 // It only makes sense to use the extended and truncated vector element
446 // variants with iAny types; otherwise, if the intrinsic is not
447 // overloaded, all the types can be specified directly.
448 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
449 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson9f944592009-08-11 20:47:22 +0000450 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson781797f2009-08-11 05:03:38 +0000451 "Expected iAny or vAny type");
Bob Wilsonf76486a2009-01-07 00:09:01 +0000452 } else
453 VT = getValueType(TyEl->getValueAsDef("VT"));
Jim Grosbachf910bf22011-03-11 01:27:24 +0000454
Bob Wilson2cd5da82009-08-11 01:14:02 +0000455 if (EVT(VT).isOverloaded()) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000456 OverloadedVTs.push_back(VT);
Chris Lattnerb424faa2010-03-23 23:46:27 +0000457 isOverloaded = true;
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000458 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000459
Chris Lattnerb424faa2010-03-23 23:46:27 +0000460 // Reject invalid types.
461 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
462 throw "Intrinsic '" + DefName + " has void in result type list!";
Jim Grosbachf910bf22011-03-11 01:27:24 +0000463
Bill Wendling91821472008-11-13 09:08:33 +0000464 IS.ParamVTs.push_back(VT);
465 IS.ParamTypeDefs.push_back(TyEl);
466 }
467
Chris Lattner2c581412006-03-24 19:49:31 +0000468 // Parse the intrinsic properties.
469 ListInit *PropList = R->getValueAsListInit("Properties");
470 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattnercbebe462007-02-27 22:08:27 +0000471 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner2c581412006-03-24 19:49:31 +0000472 assert(Property->isSubClassOf("IntrinsicProperty") &&
473 "Expected a property!");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000474
Chris Lattner989b16e42006-04-10 22:02:59 +0000475 if (Property->getName() == "IntrNoMem")
Chris Lattner2c581412006-03-24 19:49:31 +0000476 ModRef = NoMem;
Chris Lattner989b16e42006-04-10 22:02:59 +0000477 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner2c581412006-03-24 19:49:31 +0000478 ModRef = ReadArgMem;
479 else if (Property->getName() == "IntrReadMem")
480 ModRef = ReadMem;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000481 else if (Property->getName() == "IntrReadWriteArgMem")
482 ModRef = ReadWriteArgMem;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000483 else if (Property->getName() == "Commutative")
484 isCommutative = true;
John McCall375dcc92011-05-28 06:31:34 +0000485 else if (Property->getName() == "Throws")
486 canThrow = true;
Chris Lattner9a3113a2009-01-12 01:12:03 +0000487 else if (Property->isSubClassOf("NoCapture")) {
488 unsigned ArgNo = Property->getValueAsInt("ArgNo");
489 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
490 } else
Chris Lattner2c581412006-03-24 19:49:31 +0000491 assert(0 && "Unknown property!");
492 }
John McCall375dcc92011-05-28 06:31:34 +0000493
494 // Sort the argument attributes for later benefit.
495 std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end());
Chris Lattner2c581412006-03-24 19:49:31 +0000496}