blob: e280a49bd3b899313419a2cb8ed8806387f6a00f [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"
Bill Wendlingf5da1332006-12-07 22:21:48 +000022#include "llvm/Support/Streams.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>
27AsmWriterNum("asmwriternum", cl::init(0),
28 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
29
Duncan Sands83ec4b62008-06-06 12:08:01 +000030/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
31/// record corresponds to.
32MVT::SimpleValueType llvm::getValueType(Record *Rec) {
33 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000034}
35
Duncan Sands83ec4b62008-06-06 12:08:01 +000036std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000037 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000038 case MVT::Other: return "UNKNOWN";
Evan Chengd7c2c862006-06-15 00:16:37 +000039 case MVT::i1: return "MVT::i1";
40 case MVT::i8: return "MVT::i8";
41 case MVT::i16: return "MVT::i16";
42 case MVT::i32: return "MVT::i32";
43 case MVT::i64: return "MVT::i64";
44 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000045 case MVT::iAny: return "MVT::iAny";
Dan Gohman0fee3ff2007-08-16 21:57:19 +000046 case MVT::fAny: return "MVT::fAny";
Evan Chengd7c2c862006-06-15 00:16:37 +000047 case MVT::f32: return "MVT::f32";
48 case MVT::f64: return "MVT::f64";
49 case MVT::f80: return "MVT::f80";
50 case MVT::f128: return "MVT::f128";
Dale Johannesen317096a2007-09-28 01:08:20 +000051 case MVT::ppcf128: return "MVT::ppcf128";
Evan Chengd7c2c862006-06-15 00:16:37 +000052 case MVT::Flag: return "MVT::Flag";
Dan Gohmanba6783e2008-08-20 01:44:30 +000053 case MVT::isVoid:return "MVT::isVoid";
Mon P Wang87c8a8f2008-12-18 20:03:17 +000054 case MVT::v2i8: return "MVT::v2i8";
55 case MVT::v4i8: return "MVT::v4i8";
Evan Chengd7c2c862006-06-15 00:16:37 +000056 case MVT::v8i8: return "MVT::v8i8";
Evan Chengd7c2c862006-06-15 00:16:37 +000057 case MVT::v16i8: return "MVT::v16i8";
David Greene9b9838d2009-06-29 16:47:10 +000058 case MVT::v32i8: return "MVT::v32i8";
59 case MVT::v2i16: return "MVT::v2i16";
60 case MVT::v4i16: return "MVT::v4i16";
Evan Chengd7c2c862006-06-15 00:16:37 +000061 case MVT::v8i16: return "MVT::v8i16";
David Greene9b9838d2009-06-29 16:47:10 +000062 case MVT::v16i16: return "MVT::v16i16";
63 case MVT::v2i32: return "MVT::v2i32";
Evan Chengd7c2c862006-06-15 00:16:37 +000064 case MVT::v4i32: return "MVT::v4i32";
David Greene9b9838d2009-06-29 16:47:10 +000065 case MVT::v8i32: return "MVT::v8i32";
66 case MVT::v1i64: return "MVT::v1i64";
Evan Chengd7c2c862006-06-15 00:16:37 +000067 case MVT::v2i64: return "MVT::v2i64";
David Greene9b9838d2009-06-29 16:47:10 +000068 case MVT::v4i64: return "MVT::v4i64";
Evan Chengd7c2c862006-06-15 00:16:37 +000069 case MVT::v2f32: return "MVT::v2f32";
70 case MVT::v4f32: return "MVT::v4f32";
David Greene9b9838d2009-06-29 16:47:10 +000071 case MVT::v8f32: return "MVT::v8f32";
Evan Chengd7c2c862006-06-15 00:16:37 +000072 case MVT::v2f64: return "MVT::v2f64";
David Greene9b9838d2009-06-29 16:47:10 +000073 case MVT::v4f64: return "MVT::v4f64";
Christopher Lamb82455102007-07-26 06:41:18 +000074 case MVT::v3i32: return "MVT::v3i32";
75 case MVT::v3f32: return "MVT::v3f32";
Evan Cheng6b125162006-05-17 20:55:51 +000076 case MVT::iPTR: return "TLI.getPointerTy()";
Mon P Wange3b3a722008-07-30 04:36:53 +000077 case MVT::iPTRAny: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000078 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000079 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000080}
81
Duncan Sands83ec4b62008-06-06 12:08:01 +000082std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000083 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000084 case MVT::Other: return "MVT::Other";
85 case MVT::i1: return "MVT::i1";
86 case MVT::i8: return "MVT::i8";
87 case MVT::i16: return "MVT::i16";
88 case MVT::i32: return "MVT::i32";
89 case MVT::i64: return "MVT::i64";
90 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000091 case MVT::iAny: return "MVT::iAny";
Dan Gohman0fee3ff2007-08-16 21:57:19 +000092 case MVT::fAny: return "MVT::fAny";
Evan Cheng2618d072006-05-17 20:37:59 +000093 case MVT::f32: return "MVT::f32";
94 case MVT::f64: return "MVT::f64";
95 case MVT::f80: return "MVT::f80";
96 case MVT::f128: return "MVT::f128";
Dale Johannesen317096a2007-09-28 01:08:20 +000097 case MVT::ppcf128: return "MVT::ppcf128";
Evan Cheng2618d072006-05-17 20:37:59 +000098 case MVT::Flag: return "MVT::Flag";
99 case MVT::isVoid:return "MVT::isVoid";
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000100 case MVT::v2i8: return "MVT::v2i8";
101 case MVT::v4i8: return "MVT::v4i8";
Evan Cheng2618d072006-05-17 20:37:59 +0000102 case MVT::v8i8: return "MVT::v8i8";
Evan Cheng2618d072006-05-17 20:37:59 +0000103 case MVT::v16i8: return "MVT::v16i8";
David Greene9b9838d2009-06-29 16:47:10 +0000104 case MVT::v32i8: return "MVT::v32i8";
105 case MVT::v2i16: return "MVT::v2i16";
106 case MVT::v4i16: return "MVT::v4i16";
Evan Cheng2618d072006-05-17 20:37:59 +0000107 case MVT::v8i16: return "MVT::v8i16";
David Greene9b9838d2009-06-29 16:47:10 +0000108 case MVT::v16i16: return "MVT::v16i16";
109 case MVT::v2i32: return "MVT::v2i32";
Evan Cheng2618d072006-05-17 20:37:59 +0000110 case MVT::v4i32: return "MVT::v4i32";
David Greene9b9838d2009-06-29 16:47:10 +0000111 case MVT::v8i32: return "MVT::v8i32";
112 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +0000113 case MVT::v2i64: return "MVT::v2i64";
David Greene9b9838d2009-06-29 16:47:10 +0000114 case MVT::v4i64: return "MVT::v4i64";
Evan Cheng2618d072006-05-17 20:37:59 +0000115 case MVT::v2f32: return "MVT::v2f32";
116 case MVT::v4f32: return "MVT::v4f32";
David Greene9b9838d2009-06-29 16:47:10 +0000117 case MVT::v8f32: return "MVT::v8f32";
Evan Cheng2618d072006-05-17 20:37:59 +0000118 case MVT::v2f64: return "MVT::v2f64";
David Greene9b9838d2009-06-29 16:47:10 +0000119 case MVT::v4f64: return "MVT::v4f64";
Christopher Lamb82455102007-07-26 06:41:18 +0000120 case MVT::v3i32: return "MVT::v3i32";
121 case MVT::v3f32: return "MVT::v3f32";
Chandler Carruth69940402007-08-04 01:51:18 +0000122 case MVT::iPTR: return "MVT::iPTR";
Mon P Wange3b3a722008-07-30 04:36:53 +0000123 case MVT::iPTRAny: return "MVT::iPTRAny";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +0000124 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
125 }
126}
127
Chris Lattner6cefb772008-01-05 22:25:12 +0000128/// getQualifiedName - Return the name of the specified record, with a
129/// namespace qualifier if the record contains one.
130///
131std::string llvm::getQualifiedName(const Record *R) {
132 std::string Namespace = R->getValueAsString("Namespace");
133 if (Namespace.empty()) return R->getName();
134 return Namespace + "::" + R->getName();
135}
136
137
138
Chris Lattnerd3464c12003-08-07 23:15:21 +0000139
Chris Lattner45872072003-08-07 05:38:11 +0000140/// getTarget - Return the current instance of the Target class.
141///
Evan Cheng2618d072006-05-17 20:37:59 +0000142CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000143 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000144 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000145 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000146 if (Targets.size() != 1)
147 throw std::string("ERROR: Multiple subclasses of Target defined!");
148 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000149}
150
151
152const std::string &CodeGenTarget::getName() const {
153 return TargetRec->getName();
154}
155
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000156std::string CodeGenTarget::getInstNamespace() const {
157 std::string InstNS;
158
159 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
160 InstNS = i->second.Namespace;
161
162 // Make sure not to pick up "TargetInstrInfo" by accidentally getting
163 // the namespace off the PHI instruction or something.
164 if (InstNS != "TargetInstrInfo")
165 break;
166 }
167
168 return InstNS;
169}
170
Chris Lattner45872072003-08-07 05:38:11 +0000171Record *CodeGenTarget::getInstructionSet() const {
172 return TargetRec->getValueAsDef("InstructionSet");
173}
Brian Gaeked0fde302003-11-11 22:41:34 +0000174
Chris Lattner175580c2004-08-14 22:50:53 +0000175/// getAsmWriter - Return the AssemblyWriter definition for this target.
176///
177Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000178 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
179 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000180 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000181 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000182}
183
Chris Lattner26693112004-08-16 01:10:21 +0000184void CodeGenTarget::ReadRegisters() const {
185 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
186 if (Regs.empty())
187 throw std::string("No 'Register' subclasses defined!");
188
189 Registers.reserve(Regs.size());
190 Registers.assign(Regs.begin(), Regs.end());
191}
192
Chris Lattner7a680c62004-08-21 02:24:57 +0000193CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
194 DeclaredSpillSize = R->getValueAsInt("SpillSize");
195 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
196}
197
Chris Lattner26693112004-08-16 01:10:21 +0000198const std::string &CodeGenRegister::getName() const {
199 return TheDef->getName();
200}
201
Chris Lattner056afef2004-08-21 04:05:00 +0000202void CodeGenTarget::ReadRegisterClasses() const {
203 std::vector<Record*> RegClasses =
204 Records.getAllDerivedDefinitions("RegisterClass");
205 if (RegClasses.empty())
206 throw std::string("No 'RegisterClass' subclasses defined!");
207
208 RegisterClasses.reserve(RegClasses.size());
209 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
210}
211
Evan Cheng44a65fa2006-05-16 07:05:30 +0000212std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
213 std::vector<unsigned char> Result;
214 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
215 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
216 const CodeGenRegisterClass &RC = RegisterClasses[i];
217 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
218 if (R == RC.Elements[ei]) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000219 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Evan Cheng44a65fa2006-05-16 07:05:30 +0000220 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
221 Result.push_back(InVTs[i]);
222 }
223 }
224 }
225 return Result;
226}
227
228
Chris Lattner056afef2004-08-21 04:05:00 +0000229CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000230 // Rename anonymous register classes.
231 if (R->getName().size() > 9 && R->getName()[9] == '.') {
232 static unsigned AnonCounter = 0;
233 R->setName("AnonRegClass_"+utostr(AnonCounter++));
234 }
235
Nate Begeman6510b222005-12-01 04:51:06 +0000236 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
237 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
238 Record *Type = TypeList[i];
239 if (!Type->isSubClassOf("ValueType"))
240 throw "RegTypes list member '" + Type->getName() +
241 "' does not derive from the ValueType class!";
242 VTs.push_back(getValueType(Type));
243 }
244 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000245
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000246 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
247 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
248 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000249 if (!Reg->isSubClassOf("Register"))
250 throw "Register Class member '" + Reg->getName() +
251 "' does not derive from the Register class!";
252 Elements.push_back(Reg);
253 }
Nate Begeman6510b222005-12-01 04:51:06 +0000254
Christopher Lamba3211252007-06-13 22:20:15 +0000255 std::vector<Record*> SubRegClassList =
256 R->getValueAsListOfDefs("SubRegClassList");
257 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
258 Record *SubRegClass = SubRegClassList[i];
259 if (!SubRegClass->isSubClassOf("RegisterClass"))
260 throw "Register Class member '" + SubRegClass->getName() +
261 "' does not derive from the RegisterClass class!";
262 SubRegClasses.push_back(SubRegClass);
263 }
264
Nate Begeman6510b222005-12-01 04:51:06 +0000265 // Allow targets to override the size in bits of the RegisterClass.
266 unsigned Size = R->getValueAsInt("Size");
267
268 Namespace = R->getValueAsString("Namespace");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000269 SpillSize = Size ? Size : MVT(VTs[0]).getSizeInBits();
Nate Begeman6510b222005-12-01 04:51:06 +0000270 SpillAlignment = R->getValueAsInt("Alignment");
Evan Chenga3ca3142007-09-19 01:35:01 +0000271 CopyCost = R->getValueAsInt("CopyCost");
Nate Begeman6510b222005-12-01 04:51:06 +0000272 MethodBodies = R->getValueAsCode("MethodBodies");
273 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000274}
275
276const std::string &CodeGenRegisterClass::getName() const {
277 return TheDef->getName();
278}
279
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000280void CodeGenTarget::ReadLegalValueTypes() const {
281 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
282 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000283 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
284 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000285
286 // Remove duplicates.
287 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
288 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
289 LegalValueTypes.end()),
290 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000291}
Chris Lattner056afef2004-08-21 04:05:00 +0000292
Chris Lattner175580c2004-08-14 22:50:53 +0000293
Chris Lattnerec352402004-08-01 05:04:00 +0000294void CodeGenTarget::ReadInstructions() const {
295 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000296 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000297 throw std::string("No 'Instruction' subclasses defined!");
298
Chris Lattneraa77d772006-01-27 01:45:06 +0000299 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000300 std::string InstFormatName =
301 getAsmWriter()->getValueAsString("InstFormatName");
302
303 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
304 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
305 Instructions.insert(std::make_pair(Insts[i]->getName(),
306 CodeGenInstruction(Insts[i], AsmStr)));
307 }
Chris Lattnerec352402004-08-01 05:04:00 +0000308}
309
Chris Lattnerd6488672005-01-22 18:58:51 +0000310/// getInstructionsByEnumValue - Return all of the instructions defined by the
311/// target, ordered by their enum value.
312void CodeGenTarget::
313getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
314 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000315 std::map<std::string, CodeGenInstruction>::const_iterator I;
316 I = getInstructions().find("PHI");
317 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
318 const CodeGenInstruction *PHI = &I->second;
319
320 I = getInstructions().find("INLINEASM");
321 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
322 const CodeGenInstruction *INLINEASM = &I->second;
323
Dan Gohman44066042008-07-01 00:05:16 +0000324 I = getInstructions().find("DBG_LABEL");
325 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
326 const CodeGenInstruction *DBG_LABEL = &I->second;
327
328 I = getInstructions().find("EH_LABEL");
329 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
330 const CodeGenInstruction *EH_LABEL = &I->second;
331
332 I = getInstructions().find("GC_LABEL");
333 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
334 const CodeGenInstruction *GC_LABEL = &I->second;
Jim Laskeya683f9b2007-01-26 17:29:20 +0000335
Evan Chenga844bde2008-02-02 04:07:54 +0000336 I = getInstructions().find("DECLARE");
337 if (I == Instructions.end()) throw "Could not find 'DECLARE' instruction!";
338 const CodeGenInstruction *DECLARE = &I->second;
339
Christopher Lamb08d52072007-07-26 07:48:21 +0000340 I = getInstructions().find("EXTRACT_SUBREG");
341 if (I == Instructions.end())
342 throw "Could not find 'EXTRACT_SUBREG' instruction!";
343 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
344
345 I = getInstructions().find("INSERT_SUBREG");
346 if (I == Instructions.end())
347 throw "Could not find 'INSERT_SUBREG' instruction!";
348 const CodeGenInstruction *INSERT_SUBREG = &I->second;
349
Evan Chengda47e6e2008-03-15 00:03:38 +0000350 I = getInstructions().find("IMPLICIT_DEF");
351 if (I == Instructions.end())
352 throw "Could not find 'IMPLICIT_DEF' instruction!";
353 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
354
Christopher Lambc9298232008-03-16 03:12:01 +0000355 I = getInstructions().find("SUBREG_TO_REG");
356 if (I == Instructions.end())
357 throw "Could not find 'SUBREG_TO_REG' instruction!";
358 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000359
Dan Gohman88c7af02009-04-13 21:06:25 +0000360 I = getInstructions().find("COPY_TO_REGCLASS");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000361 if (I == Instructions.end())
Dan Gohman88c7af02009-04-13 21:06:25 +0000362 throw "Could not find 'COPY_TO_REGCLASS' instruction!";
363 const CodeGenInstruction *COPY_TO_REGCLASS = &I->second;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000364
Chris Lattnerd6488672005-01-22 18:58:51 +0000365 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000366 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000367 NumberedInstructions.push_back(INLINEASM);
Dan Gohman44066042008-07-01 00:05:16 +0000368 NumberedInstructions.push_back(DBG_LABEL);
369 NumberedInstructions.push_back(EH_LABEL);
370 NumberedInstructions.push_back(GC_LABEL);
Evan Chenga844bde2008-02-02 04:07:54 +0000371 NumberedInstructions.push_back(DECLARE);
Christopher Lamb08d52072007-07-26 07:48:21 +0000372 NumberedInstructions.push_back(EXTRACT_SUBREG);
373 NumberedInstructions.push_back(INSERT_SUBREG);
Evan Chengda47e6e2008-03-15 00:03:38 +0000374 NumberedInstructions.push_back(IMPLICIT_DEF);
Christopher Lambc9298232008-03-16 03:12:01 +0000375 NumberedInstructions.push_back(SUBREG_TO_REG);
Dan Gohman88c7af02009-04-13 21:06:25 +0000376 NumberedInstructions.push_back(COPY_TO_REGCLASS);
Chris Lattnerd6488672005-01-22 18:58:51 +0000377 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000378 if (&II->second != PHI &&
379 &II->second != INLINEASM &&
Dan Gohman44066042008-07-01 00:05:16 +0000380 &II->second != DBG_LABEL &&
381 &II->second != EH_LABEL &&
382 &II->second != GC_LABEL &&
Evan Chenga844bde2008-02-02 04:07:54 +0000383 &II->second != DECLARE &&
Christopher Lamb08d52072007-07-26 07:48:21 +0000384 &II->second != EXTRACT_SUBREG &&
Evan Chengda47e6e2008-03-15 00:03:38 +0000385 &II->second != INSERT_SUBREG &&
Christopher Lambc9298232008-03-16 03:12:01 +0000386 &II->second != IMPLICIT_DEF &&
Dan Gohmanf8c73942009-04-13 15:38:05 +0000387 &II->second != SUBREG_TO_REG &&
Dan Gohman88c7af02009-04-13 21:06:25 +0000388 &II->second != COPY_TO_REGCLASS)
Chris Lattnerd6488672005-01-22 18:58:51 +0000389 NumberedInstructions.push_back(&II->second);
390}
391
392
Misha Brukman35e83cc2004-10-14 05:50:43 +0000393/// isLittleEndianEncoding - Return whether this target encodes its instruction
394/// in little-endian format, i.e. bits laid out in the order [0..n]
395///
396bool CodeGenTarget::isLittleEndianEncoding() const {
397 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
398}
399
Evan Cheng0fc71982005-12-08 02:00:36 +0000400//===----------------------------------------------------------------------===//
401// ComplexPattern implementation
402//
403ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000404 Ty = ::getValueType(R->getValueAsDef("Ty"));
405 NumOperands = R->getValueAsInt("NumOperands");
406 SelectFunc = R->getValueAsString("SelectFunc");
407 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000408
409 // Parse the properties.
410 Properties = 0;
411 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
412 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
413 if (PropList[i]->getName() == "SDNPHasChain") {
414 Properties |= 1 << SDNPHasChain;
415 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
416 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerba7e7562008-01-10 07:59:24 +0000417 } else if (PropList[i]->getName() == "SDNPMayStore") {
418 Properties |= 1 << SDNPMayStore;
419 } else if (PropList[i]->getName() == "SDNPMayLoad") {
420 Properties |= 1 << SDNPMayLoad;
421 } else if (PropList[i]->getName() == "SDNPSideEffect") {
422 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000423 } else if (PropList[i]->getName() == "SDNPMemOperand") {
424 Properties |= 1 << SDNPMemOperand;
Evan Cheng94b30402006-10-11 21:02:01 +0000425 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000426 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
427 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000428 exit(1);
429 }
Christopher Lamb85356242008-01-31 07:27:46 +0000430
431 // Parse the attributes.
432 Attributes = 0;
433 PropList = R->getValueAsListOfDefs("Attributes");
434 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
435 if (PropList[i]->getName() == "CPAttrParentAsRoot") {
436 Attributes |= 1 << CPAttrParentAsRoot;
437 } else {
438 cerr << "Unsupported pattern attribute '" << PropList[i]->getName()
439 << "' on ComplexPattern '" << R->getName() << "'!\n";
440 exit(1);
441 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000442}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000443
Chris Lattner43fbbc32006-03-24 19:49:31 +0000444//===----------------------------------------------------------------------===//
445// CodeGenIntrinsic Implementation
446//===----------------------------------------------------------------------===//
447
Dale Johannesen49de9822009-02-05 01:49:45 +0000448std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
449 bool TargetOnly) {
Chris Lattner43fbbc32006-03-24 19:49:31 +0000450 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000451
452 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000453
Dale Johannesen49de9822009-02-05 01:49:45 +0000454 for (unsigned i = 0, e = I.size(); i != e; ++i) {
455 bool isTarget = I[i]->getValueAsBit("isTarget");
456 if (isTarget == TargetOnly)
457 Result.push_back(CodeGenIntrinsic(I[i]));
458 }
Chris Lattner8850a1b2006-03-27 22:48:18 +0000459 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000460}
461
Dan Gohmanee4fa192008-04-03 00:02:49 +0000462CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000463 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000464 std::string DefName = R->getName();
465 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000466 isOverloaded = false;
Evan Cheng6bd95672008-06-16 20:29:38 +0000467 isCommutative = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000468
469 if (DefName.size() <= 4 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000470 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000471 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000472
Chris Lattner43fbbc32006-03-24 19:49:31 +0000473 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000474
Chris Lattner43fbbc32006-03-24 19:49:31 +0000475 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
476 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000477
478 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000479 Name = R->getValueAsString("LLVMName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000480
Chris Lattner43fbbc32006-03-24 19:49:31 +0000481 if (Name == "") {
482 // If an explicit name isn't specified, derive one from the DefName.
483 Name = "llvm.";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000484
Chris Lattner43fbbc32006-03-24 19:49:31 +0000485 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000486 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner43fbbc32006-03-24 19:49:31 +0000487 } else {
488 // Verify it starts with "llvm.".
489 if (Name.size() <= 5 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000490 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000491 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
492 }
493
494 // If TargetPrefix is specified, make sure that Name starts with
495 // "llvm.<targetprefix>.".
496 if (!TargetPrefix.empty()) {
497 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000498 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
499 != (TargetPrefix + "."))
500 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Chris Lattner43fbbc32006-03-24 19:49:31 +0000501 TargetPrefix + ".'!";
502 }
503
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000504 // Parse the list of return types.
Bob Wilsond305e702009-04-16 21:51:05 +0000505 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000506 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000507 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000508 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000509 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000510 MVT::SimpleValueType VT;
511 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsond305e702009-04-16 21:51:05 +0000512 unsigned MatchTy = TyEl->getValueAsInt("Number");
513 assert(MatchTy < OverloadedVTs.size() &&
514 "Invalid matching number!");
515 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000516 // It only makes sense to use the extended and truncated vector element
517 // variants with iAny types; otherwise, if the intrinsic is not
518 // overloaded, all the types can be specified directly.
519 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
520 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
521 VT == MVT::iAny) && "Expected iAny type");
Bob Wilsond305e702009-04-16 21:51:05 +0000522 } else {
Bob Wilsonbc039792009-01-07 00:09:01 +0000523 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsond305e702009-04-16 21:51:05 +0000524 }
525 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny) {
526 OverloadedVTs.push_back(VT);
527 isOverloaded |= true;
528 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000529 IS.RetVTs.push_back(VT);
530 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000531 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000532
533 if (IS.RetVTs.size() == 0)
Chris Lattner43fbbc32006-03-24 19:49:31 +0000534 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000535
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000536 // Parse the list of parameter types.
537 TypeList = R->getValueAsListInit("ParamTypes");
538 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
539 Record *TyEl = TypeList->getElementAsRecord(i);
540 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000541 MVT::SimpleValueType VT;
542 if (TyEl->isSubClassOf("LLVMMatchType")) {
543 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsond305e702009-04-16 21:51:05 +0000544 assert(MatchTy < OverloadedVTs.size() &&
545 "Invalid matching number!");
546 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000547 // It only makes sense to use the extended and truncated vector element
548 // variants with iAny types; otherwise, if the intrinsic is not
549 // overloaded, all the types can be specified directly.
550 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
551 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
552 VT == MVT::iAny) && "Expected iAny type");
553 } else
554 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsond305e702009-04-16 21:51:05 +0000555 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny) {
556 OverloadedVTs.push_back(VT);
557 isOverloaded |= true;
558 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000559 IS.ParamVTs.push_back(VT);
560 IS.ParamTypeDefs.push_back(TyEl);
561 }
562
Chris Lattner43fbbc32006-03-24 19:49:31 +0000563 // Parse the intrinsic properties.
564 ListInit *PropList = R->getValueAsListInit("Properties");
565 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000566 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000567 assert(Property->isSubClassOf("IntrinsicProperty") &&
568 "Expected a property!");
569
Chris Lattner4b2362e2006-04-10 22:02:59 +0000570 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000571 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000572 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000573 ModRef = ReadArgMem;
574 else if (Property->getName() == "IntrReadMem")
575 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000576 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000577 ModRef = WriteArgMem;
578 else if (Property->getName() == "IntrWriteMem")
579 ModRef = WriteMem;
Evan Cheng6bd95672008-06-16 20:29:38 +0000580 else if (Property->getName() == "Commutative")
581 isCommutative = true;
Chris Lattnera62c3022009-01-12 01:12:03 +0000582 else if (Property->isSubClassOf("NoCapture")) {
583 unsigned ArgNo = Property->getValueAsInt("ArgNo");
584 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
585 } else
Chris Lattner43fbbc32006-03-24 19:49:31 +0000586 assert(0 && "Unknown property!");
587 }
588}