blob: e337b564063ceb6e94bc43330b5b1978875eb283 [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"
Chris Lattner0d7952e2010-03-27 20:32:26 +000021#include "llvm/ADT/STLExtras.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000022#include "llvm/Support/CommandLine.h"
Chris Lattner75ee2eb2005-10-14 03:54:49 +000023#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner560a79f2004-10-03 19:34:31 +000026static cl::opt<unsigned>
Daniel Dunbar0e2771f2009-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 Lattner560a79f2004-10-03 19:34:31 +000031AsmWriterNum("asmwriternum", cl::init(0),
32 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
33
Owen Anderson825b72b2009-08-11 20:47:22 +000034/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands83ec4b62008-06-06 12:08:01 +000035/// record corresponds to.
Owen Anderson825b72b2009-08-11 20:47:22 +000036MVT::SimpleValueType llvm::getValueType(Record *Rec) {
37 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000038}
39
Owen Anderson825b72b2009-08-11 20:47:22 +000040std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000041 switch (T) {
Owen Anderson825b72b2009-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 Wilsonbbf1caa2009-07-10 22:25:24 +000045 default: return getEnumName(T);
Chris Lattner45872072003-08-07 05:38:11 +000046 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000047}
48
Owen Anderson825b72b2009-08-11 20:47:22 +000049std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000050 switch (T) {
Bill Wendling72d10272010-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 Anderson825b72b2009-08-11 20:47:22 +000065 case MVT::ppcf128: return "MVT::ppcf128";
Bill Wendlingd8dd5752010-09-07 20:03:56 +000066 case MVT::x86mmx: return "MVT::x86mmx";
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000067 case MVT::Glue: return "MVT::Glue";
Bill Wendling72d10272010-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 Anderson825b72b2009-08-11 20:47:22 +000090 case MVT::Metadata: return "MVT::Metadata";
Bill Wendling72d10272010-09-07 18:49:14 +000091 case MVT::iPTR: return "MVT::iPTR";
Owen Anderson825b72b2009-08-11 20:47:22 +000092 case MVT::iPTRAny: return "MVT::iPTRAny";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000093 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
94 }
95}
96
Chris Lattner6cefb772008-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 Olesen123cab92011-05-07 21:22:39 +0000101 std::string Namespace;
102 if (R->getValue("Namespace"))
103 Namespace = R->getValueAsString("Namespace");
Chris Lattner6cefb772008-01-05 22:25:12 +0000104 if (Namespace.empty()) return R->getName();
105 return Namespace + "::" + R->getName();
106}
107
108
Chris Lattner45872072003-08-07 05:38:11 +0000109/// getTarget - Return the current instance of the Target class.
110///
Jakob Stoklund Olesendc29c442011-06-10 18:40:00 +0000111CodeGenTarget::CodeGenTarget(RecordKeeper &records)
112 : Records(records), RegBank(0) {
Chris Lattner45872072003-08-07 05:38:11 +0000113 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000114 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000115 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000116 if (Targets.size() != 1)
117 throw std::string("ERROR: Multiple subclasses of Target defined!");
118 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000119}
120
121
122const std::string &CodeGenTarget::getName() const {
123 return TargetRec->getName();
124}
125
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000126std::string CodeGenTarget::getInstNamespace() const {
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000127 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
Chris Lattner6a91b182010-03-19 01:00:55 +0000128 // Make sure not to pick up "TargetOpcode" by accidentally getting
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000129 // the namespace off the PHI instruction or something.
Chris Lattner6a91b182010-03-19 01:00:55 +0000130 if ((*i)->Namespace != "TargetOpcode")
131 return (*i)->Namespace;
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000132 }
133
Chris Lattner6a91b182010-03-19 01:00:55 +0000134 return "";
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000135}
136
Chris Lattner45872072003-08-07 05:38:11 +0000137Record *CodeGenTarget::getInstructionSet() const {
138 return TargetRec->getValueAsDef("InstructionSet");
139}
Brian Gaeked0fde302003-11-11 22:41:34 +0000140
Chris Lattnerf30187a2010-03-19 00:07:20 +0000141
Daniel Dunbar0e2771f2009-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 Lattner175580c2004-08-14 22:50:53 +0000151/// getAsmWriter - Return the AssemblyWriter definition for this target.
152///
153Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000154 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
155 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000156 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000157 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000158}
159
Jakob Stoklund Olesendc29c442011-06-10 18:40:00 +0000160CodeGenRegBank &CodeGenTarget::getRegBank() const {
161 if (!RegBank)
162 RegBank = new CodeGenRegBank(Records);
163 return *RegBank;
164}
165
Chris Lattner26693112004-08-16 01:10:21 +0000166void CodeGenTarget::ReadRegisters() const {
167 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
168 if (Regs.empty())
169 throw std::string("No 'Register' subclasses defined!");
Jakob Stoklund Olesen160a3bf2010-05-26 21:47:28 +0000170 std::sort(Regs.begin(), Regs.end(), LessRecord());
Chris Lattner26693112004-08-16 01:10:21 +0000171
172 Registers.reserve(Regs.size());
173 Registers.assign(Regs.begin(), Regs.end());
Jim Grosbach17fad042011-03-11 01:33:54 +0000174 // Assign the enumeration values.
175 for (unsigned i = 0, e = Registers.size(); i != e; ++i)
176 Registers[i].EnumValue = i + 1;
Chris Lattner26693112004-08-16 01:10:21 +0000177}
178
Chris Lattner056afef2004-08-21 04:05:00 +0000179void CodeGenTarget::ReadRegisterClasses() const {
180 std::vector<Record*> RegClasses =
181 Records.getAllDerivedDefinitions("RegisterClass");
182 if (RegClasses.empty())
183 throw std::string("No 'RegisterClass' subclasses defined!");
184
185 RegisterClasses.reserve(RegClasses.size());
186 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
187}
188
Chris Lattnerec6f0962010-11-02 18:10:06 +0000189/// getRegisterByName - If there is a register with the specific AsmName,
190/// return it.
191const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
192 const std::vector<CodeGenRegister> &Regs = getRegisters();
193 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
194 const CodeGenRegister &Reg = Regs[i];
195 if (Reg.TheDef->getValueAsString("AsmName") == Name)
196 return &Reg;
197 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000198
Chris Lattnerec6f0962010-11-02 18:10:06 +0000199 return 0;
200}
201
Chris Lattner2cacec52010-03-15 06:00:16 +0000202std::vector<MVT::SimpleValueType> CodeGenTarget::
203getRegisterVTs(Record *R) const {
204 std::vector<MVT::SimpleValueType> Result;
Evan Cheng44a65fa2006-05-16 07:05:30 +0000205 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
206 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
207 const CodeGenRegisterClass &RC = RegisterClasses[i];
208 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
209 if (R == RC.Elements[ei]) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Chris Lattner2cacec52010-03-15 06:00:16 +0000211 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Evan Cheng44a65fa2006-05-16 07:05:30 +0000212 }
213 }
214 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000215
Chris Lattner0d7952e2010-03-27 20:32:26 +0000216 // Remove duplicates.
217 array_pod_sort(Result.begin(), Result.end());
218 Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
Evan Cheng44a65fa2006-05-16 07:05:30 +0000219 return Result;
220}
221
222
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000223void CodeGenTarget::ReadLegalValueTypes() const {
224 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
225 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000226 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
227 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Jim Grosbach510207c2011-03-11 01:27:24 +0000228
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000229 // Remove duplicates.
230 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
231 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
232 LegalValueTypes.end()),
233 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000234}
Chris Lattner056afef2004-08-21 04:05:00 +0000235
Chris Lattner175580c2004-08-14 22:50:53 +0000236
Chris Lattnerec352402004-08-01 05:04:00 +0000237void CodeGenTarget::ReadInstructions() const {
238 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000239 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000240 throw std::string("No 'Instruction' subclasses defined!");
241
Chris Lattneraa77d772006-01-27 01:45:06 +0000242 // Parse the instructions defined in the .td file.
Chris Lattnerf7808112010-11-01 02:15:23 +0000243 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
244 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i]);
Chris Lattnerec352402004-08-01 05:04:00 +0000245}
246
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000247static const CodeGenInstruction *
248GetInstByName(const char *Name,
Jim Grosbach510207c2011-03-11 01:27:24 +0000249 const DenseMap<const Record*, CodeGenInstruction*> &Insts,
Chris Lattner67db8832010-12-13 00:23:57 +0000250 RecordKeeper &Records) {
Chris Lattnere14d2e22010-03-19 01:07:44 +0000251 const Record *Rec = Records.getDef(Name);
Jim Grosbach510207c2011-03-11 01:27:24 +0000252
Chris Lattnere14d2e22010-03-19 01:07:44 +0000253 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
254 I = Insts.find(Rec);
255 if (Rec == 0 || I == Insts.end())
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000256 throw std::string("Could not find '") + Name + "' instruction!";
Chris Lattnere14d2e22010-03-19 01:07:44 +0000257 return I->second;
258}
259
260namespace {
261/// SortInstByName - Sorting predicate to sort instructions by name.
262///
263struct SortInstByName {
264 bool operator()(const CodeGenInstruction *Rec1,
265 const CodeGenInstruction *Rec2) const {
266 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
267 }
268};
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000269}
270
Chris Lattnerd6488672005-01-22 18:58:51 +0000271/// getInstructionsByEnumValue - Return all of the instructions defined by the
272/// target, ordered by their enum value.
Chris Lattner6a91b182010-03-19 01:00:55 +0000273void CodeGenTarget::ComputeInstrsByEnum() const {
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000274 // The ordering here must match the ordering in TargetOpcodes.h.
275 const char *const FixedInstrs[] = {
276 "PHI",
277 "INLINEASM",
Bill Wendling7431bea2010-07-16 22:20:36 +0000278 "PROLOG_LABEL",
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000279 "EH_LABEL",
280 "GC_LABEL",
281 "KILL",
282 "EXTRACT_SUBREG",
283 "INSERT_SUBREG",
284 "IMPLICIT_DEF",
285 "SUBREG_TO_REG",
286 "COPY_TO_REGCLASS",
287 "DBG_VALUE",
288 "REG_SEQUENCE",
Jakob Stoklund Olesena4e1ba52010-07-02 22:29:50 +0000289 "COPY",
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000290 0
291 };
Chris Lattnere14d2e22010-03-19 01:07:44 +0000292 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000293 for (const char *const *p = FixedInstrs; *p; ++p) {
Chris Lattner67db8832010-12-13 00:23:57 +0000294 const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000295 assert(Instr && "Missing target independent instruction");
296 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
297 InstrsByEnum.push_back(Instr);
298 }
Chris Lattnere14d2e22010-03-19 01:07:44 +0000299 unsigned EndOfPredefines = InstrsByEnum.size();
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000300
Chris Lattnere14d2e22010-03-19 01:07:44 +0000301 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
302 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
303 const CodeGenInstruction *CGI = I->second;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000304 if (CGI->Namespace != "TargetOpcode")
Chris Lattner6a91b182010-03-19 01:00:55 +0000305 InstrsByEnum.push_back(CGI);
306 }
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000307
308 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
309
Chris Lattnere14d2e22010-03-19 01:07:44 +0000310 // All of the instructions are now in random order based on the map iteration.
311 // Sort them by name.
312 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(),
313 SortInstByName());
Chris Lattnerd6488672005-01-22 18:58:51 +0000314}
315
316
Misha Brukman35e83cc2004-10-14 05:50:43 +0000317/// isLittleEndianEncoding - Return whether this target encodes its instruction
318/// in little-endian format, i.e. bits laid out in the order [0..n]
319///
320bool CodeGenTarget::isLittleEndianEncoding() const {
321 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
322}
323
Evan Cheng0fc71982005-12-08 02:00:36 +0000324//===----------------------------------------------------------------------===//
325// ComplexPattern implementation
326//
327ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000328 Ty = ::getValueType(R->getValueAsDef("Ty"));
329 NumOperands = R->getValueAsInt("NumOperands");
330 SelectFunc = R->getValueAsString("SelectFunc");
331 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000332
333 // Parse the properties.
334 Properties = 0;
335 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
336 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
337 if (PropList[i]->getName() == "SDNPHasChain") {
338 Properties |= 1 << SDNPHasChain;
Chris Lattner036609b2010-12-23 18:28:41 +0000339 } else if (PropList[i]->getName() == "SDNPOptInGlue") {
340 Properties |= 1 << SDNPOptInGlue;
Chris Lattnerba7e7562008-01-10 07:59:24 +0000341 } else if (PropList[i]->getName() == "SDNPMayStore") {
342 Properties |= 1 << SDNPMayStore;
343 } else if (PropList[i]->getName() == "SDNPMayLoad") {
344 Properties |= 1 << SDNPMayLoad;
345 } else if (PropList[i]->getName() == "SDNPSideEffect") {
346 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000347 } else if (PropList[i]->getName() == "SDNPMemOperand") {
348 Properties |= 1 << SDNPMemOperand;
Chris Lattnere8cabf32010-03-19 05:07:09 +0000349 } else if (PropList[i]->getName() == "SDNPVariadic") {
350 Properties |= 1 << SDNPVariadic;
Chris Lattner52a261b2010-09-21 20:31:19 +0000351 } else if (PropList[i]->getName() == "SDNPWantRoot") {
352 Properties |= 1 << SDNPWantRoot;
353 } else if (PropList[i]->getName() == "SDNPWantParent") {
354 Properties |= 1 << SDNPWantParent;
Evan Cheng94b30402006-10-11 21:02:01 +0000355 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000356 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
357 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000358 exit(1);
359 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000360}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000361
Chris Lattner43fbbc32006-03-24 19:49:31 +0000362//===----------------------------------------------------------------------===//
363// CodeGenIntrinsic Implementation
364//===----------------------------------------------------------------------===//
365
Dale Johannesen49de9822009-02-05 01:49:45 +0000366std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
367 bool TargetOnly) {
Chris Lattner43fbbc32006-03-24 19:49:31 +0000368 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Jim Grosbach510207c2011-03-11 01:27:24 +0000369
Chris Lattner8850a1b2006-03-27 22:48:18 +0000370 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000371
Dale Johannesen49de9822009-02-05 01:49:45 +0000372 for (unsigned i = 0, e = I.size(); i != e; ++i) {
373 bool isTarget = I[i]->getValueAsBit("isTarget");
374 if (isTarget == TargetOnly)
375 Result.push_back(CodeGenIntrinsic(I[i]));
376 }
Chris Lattner8850a1b2006-03-27 22:48:18 +0000377 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000378}
379
Dan Gohmanee4fa192008-04-03 00:02:49 +0000380CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000381 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000382 std::string DefName = R->getName();
Dan Gohman7365c092010-08-05 23:36:21 +0000383 ModRef = ReadWriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000384 isOverloaded = false;
Evan Cheng6bd95672008-06-16 20:29:38 +0000385 isCommutative = false;
John McCallbd0fa4c2011-05-28 06:31:34 +0000386 canThrow = false;
Jim Grosbach510207c2011-03-11 01:27:24 +0000387
388 if (DefName.size() <= 4 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000389 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000390 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000391
Chris Lattner43fbbc32006-03-24 19:49:31 +0000392 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000393
Chris Lattner43fbbc32006-03-24 19:49:31 +0000394 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
395 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000396
397 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000398 Name = R->getValueAsString("LLVMName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000399
Chris Lattner43fbbc32006-03-24 19:49:31 +0000400 if (Name == "") {
401 // If an explicit name isn't specified, derive one from the DefName.
402 Name = "llvm.";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000403
Chris Lattner43fbbc32006-03-24 19:49:31 +0000404 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000405 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner43fbbc32006-03-24 19:49:31 +0000406 } else {
407 // Verify it starts with "llvm.".
Jim Grosbach510207c2011-03-11 01:27:24 +0000408 if (Name.size() <= 5 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000409 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000410 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
411 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000412
Chris Lattner43fbbc32006-03-24 19:49:31 +0000413 // If TargetPrefix is specified, make sure that Name starts with
414 // "llvm.<targetprefix>.".
415 if (!TargetPrefix.empty()) {
416 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000417 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
418 != (TargetPrefix + "."))
419 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Chris Lattner43fbbc32006-03-24 19:49:31 +0000420 TargetPrefix + ".'!";
421 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000422
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000423 // Parse the list of return types.
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000425 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000426 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000427 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000428 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000430 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsond305e702009-04-16 21:51:05 +0000431 unsigned MatchTy = TyEl->getValueAsInt("Number");
432 assert(MatchTy < OverloadedVTs.size() &&
433 "Invalid matching number!");
434 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000435 // It only makes sense to use the extended and truncated vector element
436 // variants with iAny types; otherwise, if the intrinsic is not
437 // overloaded, all the types can be specified directly.
438 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
439 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000441 "Expected iAny or vAny type");
Bob Wilsond305e702009-04-16 21:51:05 +0000442 } else {
Bob Wilsonbc039792009-01-07 00:09:01 +0000443 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsond305e702009-04-16 21:51:05 +0000444 }
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000445 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000446 OverloadedVTs.push_back(VT);
Chris Lattner9972aff2010-03-23 23:46:27 +0000447 isOverloaded = true;
Bob Wilsond305e702009-04-16 21:51:05 +0000448 }
Chris Lattner9972aff2010-03-23 23:46:27 +0000449
450 // Reject invalid types.
451 if (VT == MVT::isVoid)
452 throw "Intrinsic '" + DefName + " has void in result type list!";
Jim Grosbach510207c2011-03-11 01:27:24 +0000453
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000454 IS.RetVTs.push_back(VT);
455 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000456 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000457
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000458 // Parse the list of parameter types.
459 TypeList = R->getValueAsListInit("ParamTypes");
460 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
461 Record *TyEl = TypeList->getElementAsRecord(i);
462 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000464 if (TyEl->isSubClassOf("LLVMMatchType")) {
465 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsond305e702009-04-16 21:51:05 +0000466 assert(MatchTy < OverloadedVTs.size() &&
467 "Invalid matching number!");
468 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000469 // It only makes sense to use the extended and truncated vector element
470 // variants with iAny types; otherwise, if the intrinsic is not
471 // overloaded, all the types can be specified directly.
472 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
473 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000475 "Expected iAny or vAny type");
Bob Wilsonbc039792009-01-07 00:09:01 +0000476 } else
477 VT = getValueType(TyEl->getValueAsDef("VT"));
Jim Grosbach510207c2011-03-11 01:27:24 +0000478
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000479 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000480 OverloadedVTs.push_back(VT);
Chris Lattner9972aff2010-03-23 23:46:27 +0000481 isOverloaded = true;
Bob Wilsond305e702009-04-16 21:51:05 +0000482 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000483
Chris Lattner9972aff2010-03-23 23:46:27 +0000484 // Reject invalid types.
485 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
486 throw "Intrinsic '" + DefName + " has void in result type list!";
Jim Grosbach510207c2011-03-11 01:27:24 +0000487
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000488 IS.ParamVTs.push_back(VT);
489 IS.ParamTypeDefs.push_back(TyEl);
490 }
491
Chris Lattner43fbbc32006-03-24 19:49:31 +0000492 // Parse the intrinsic properties.
493 ListInit *PropList = R->getValueAsListInit("Properties");
494 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000495 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000496 assert(Property->isSubClassOf("IntrinsicProperty") &&
497 "Expected a property!");
Jim Grosbach510207c2011-03-11 01:27:24 +0000498
Chris Lattner4b2362e2006-04-10 22:02:59 +0000499 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000500 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000501 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000502 ModRef = ReadArgMem;
503 else if (Property->getName() == "IntrReadMem")
504 ModRef = ReadMem;
Dan Gohman7365c092010-08-05 23:36:21 +0000505 else if (Property->getName() == "IntrReadWriteArgMem")
506 ModRef = ReadWriteArgMem;
Evan Cheng6bd95672008-06-16 20:29:38 +0000507 else if (Property->getName() == "Commutative")
508 isCommutative = true;
John McCallbd0fa4c2011-05-28 06:31:34 +0000509 else if (Property->getName() == "Throws")
510 canThrow = true;
Chris Lattnera62c3022009-01-12 01:12:03 +0000511 else if (Property->isSubClassOf("NoCapture")) {
512 unsigned ArgNo = Property->getValueAsInt("ArgNo");
513 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
514 } else
Chris Lattner43fbbc32006-03-24 19:49:31 +0000515 assert(0 && "Unknown property!");
516 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000517
518 // Sort the argument attributes for later benefit.
519 std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end());
Chris Lattner43fbbc32006-03-24 19:49:31 +0000520}