blob: 0c9da806cc9834d464809248f0e2c2be67841a23 [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";
56 case MVT::v2i16: return "MVT::v2i16";
Evan Chengd7c2c862006-06-15 00:16:37 +000057 case MVT::v8i8: return "MVT::v8i8";
58 case MVT::v4i16: return "MVT::v4i16";
59 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000060 case MVT::v1i64: return "MVT::v1i64";
Evan Chengd7c2c862006-06-15 00:16:37 +000061 case MVT::v16i8: return "MVT::v16i8";
62 case MVT::v8i16: return "MVT::v8i16";
63 case MVT::v4i32: return "MVT::v4i32";
64 case MVT::v2i64: return "MVT::v2i64";
65 case MVT::v2f32: return "MVT::v2f32";
66 case MVT::v4f32: return "MVT::v4f32";
67 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +000068 case MVT::v3i32: return "MVT::v3i32";
69 case MVT::v3f32: return "MVT::v3f32";
Evan Cheng6b125162006-05-17 20:55:51 +000070 case MVT::iPTR: return "TLI.getPointerTy()";
Mon P Wange3b3a722008-07-30 04:36:53 +000071 case MVT::iPTRAny: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000072 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000073 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000074}
75
Duncan Sands83ec4b62008-06-06 12:08:01 +000076std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000077 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000078 case MVT::Other: return "MVT::Other";
79 case MVT::i1: return "MVT::i1";
80 case MVT::i8: return "MVT::i8";
81 case MVT::i16: return "MVT::i16";
82 case MVT::i32: return "MVT::i32";
83 case MVT::i64: return "MVT::i64";
84 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000085 case MVT::iAny: return "MVT::iAny";
Dan Gohman0fee3ff2007-08-16 21:57:19 +000086 case MVT::fAny: return "MVT::fAny";
Evan Cheng2618d072006-05-17 20:37:59 +000087 case MVT::f32: return "MVT::f32";
88 case MVT::f64: return "MVT::f64";
89 case MVT::f80: return "MVT::f80";
90 case MVT::f128: return "MVT::f128";
Dale Johannesen317096a2007-09-28 01:08:20 +000091 case MVT::ppcf128: return "MVT::ppcf128";
Evan Cheng2618d072006-05-17 20:37:59 +000092 case MVT::Flag: return "MVT::Flag";
93 case MVT::isVoid:return "MVT::isVoid";
Mon P Wang87c8a8f2008-12-18 20:03:17 +000094 case MVT::v2i8: return "MVT::v2i8";
95 case MVT::v4i8: return "MVT::v4i8";
96 case MVT::v2i16: return "MVT::v2i16";
Evan Cheng2618d072006-05-17 20:37:59 +000097 case MVT::v8i8: return "MVT::v8i8";
98 case MVT::v4i16: return "MVT::v4i16";
99 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000100 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +0000101 case MVT::v16i8: return "MVT::v16i8";
102 case MVT::v8i16: return "MVT::v8i16";
103 case MVT::v4i32: return "MVT::v4i32";
104 case MVT::v2i64: return "MVT::v2i64";
105 case MVT::v2f32: return "MVT::v2f32";
106 case MVT::v4f32: return "MVT::v4f32";
107 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +0000108 case MVT::v3i32: return "MVT::v3i32";
109 case MVT::v3f32: return "MVT::v3f32";
Chandler Carruth69940402007-08-04 01:51:18 +0000110 case MVT::iPTR: return "MVT::iPTR";
Mon P Wange3b3a722008-07-30 04:36:53 +0000111 case MVT::iPTRAny: return "MVT::iPTRAny";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +0000112 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
113 }
114}
115
Chris Lattner6cefb772008-01-05 22:25:12 +0000116/// getQualifiedName - Return the name of the specified record, with a
117/// namespace qualifier if the record contains one.
118///
119std::string llvm::getQualifiedName(const Record *R) {
120 std::string Namespace = R->getValueAsString("Namespace");
121 if (Namespace.empty()) return R->getName();
122 return Namespace + "::" + R->getName();
123}
124
125
126
Chris Lattnerd3464c12003-08-07 23:15:21 +0000127
Chris Lattner45872072003-08-07 05:38:11 +0000128/// getTarget - Return the current instance of the Target class.
129///
Evan Cheng2618d072006-05-17 20:37:59 +0000130CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000131 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000132 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000133 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000134 if (Targets.size() != 1)
135 throw std::string("ERROR: Multiple subclasses of Target defined!");
136 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000137}
138
139
140const std::string &CodeGenTarget::getName() const {
141 return TargetRec->getName();
142}
143
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000144std::string CodeGenTarget::getInstNamespace() const {
145 std::string InstNS;
146
147 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
148 InstNS = i->second.Namespace;
149
150 // Make sure not to pick up "TargetInstrInfo" by accidentally getting
151 // the namespace off the PHI instruction or something.
152 if (InstNS != "TargetInstrInfo")
153 break;
154 }
155
156 return InstNS;
157}
158
Chris Lattner45872072003-08-07 05:38:11 +0000159Record *CodeGenTarget::getInstructionSet() const {
160 return TargetRec->getValueAsDef("InstructionSet");
161}
Brian Gaeked0fde302003-11-11 22:41:34 +0000162
Chris Lattner175580c2004-08-14 22:50:53 +0000163/// getAsmWriter - Return the AssemblyWriter definition for this target.
164///
165Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000166 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
167 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000168 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000169 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000170}
171
Chris Lattner26693112004-08-16 01:10:21 +0000172void CodeGenTarget::ReadRegisters() const {
173 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
174 if (Regs.empty())
175 throw std::string("No 'Register' subclasses defined!");
176
177 Registers.reserve(Regs.size());
178 Registers.assign(Regs.begin(), Regs.end());
179}
180
Chris Lattner7a680c62004-08-21 02:24:57 +0000181CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
182 DeclaredSpillSize = R->getValueAsInt("SpillSize");
183 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
184}
185
Chris Lattner26693112004-08-16 01:10:21 +0000186const std::string &CodeGenRegister::getName() const {
187 return TheDef->getName();
188}
189
Chris Lattner056afef2004-08-21 04:05:00 +0000190void CodeGenTarget::ReadRegisterClasses() const {
191 std::vector<Record*> RegClasses =
192 Records.getAllDerivedDefinitions("RegisterClass");
193 if (RegClasses.empty())
194 throw std::string("No 'RegisterClass' subclasses defined!");
195
196 RegisterClasses.reserve(RegClasses.size());
197 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
198}
199
Evan Cheng44a65fa2006-05-16 07:05:30 +0000200std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
201 std::vector<unsigned char> Result;
202 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
203 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
204 const CodeGenRegisterClass &RC = RegisterClasses[i];
205 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
206 if (R == RC.Elements[ei]) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000207 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Evan Cheng44a65fa2006-05-16 07:05:30 +0000208 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
209 Result.push_back(InVTs[i]);
210 }
211 }
212 }
213 return Result;
214}
215
216
Chris Lattner056afef2004-08-21 04:05:00 +0000217CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000218 // Rename anonymous register classes.
219 if (R->getName().size() > 9 && R->getName()[9] == '.') {
220 static unsigned AnonCounter = 0;
221 R->setName("AnonRegClass_"+utostr(AnonCounter++));
222 }
223
Nate Begeman6510b222005-12-01 04:51:06 +0000224 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
225 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
226 Record *Type = TypeList[i];
227 if (!Type->isSubClassOf("ValueType"))
228 throw "RegTypes list member '" + Type->getName() +
229 "' does not derive from the ValueType class!";
230 VTs.push_back(getValueType(Type));
231 }
232 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000233
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000234 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
235 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
236 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000237 if (!Reg->isSubClassOf("Register"))
238 throw "Register Class member '" + Reg->getName() +
239 "' does not derive from the Register class!";
240 Elements.push_back(Reg);
241 }
Nate Begeman6510b222005-12-01 04:51:06 +0000242
Christopher Lamba3211252007-06-13 22:20:15 +0000243 std::vector<Record*> SubRegClassList =
244 R->getValueAsListOfDefs("SubRegClassList");
245 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
246 Record *SubRegClass = SubRegClassList[i];
247 if (!SubRegClass->isSubClassOf("RegisterClass"))
248 throw "Register Class member '" + SubRegClass->getName() +
249 "' does not derive from the RegisterClass class!";
250 SubRegClasses.push_back(SubRegClass);
251 }
252
Nate Begeman6510b222005-12-01 04:51:06 +0000253 // Allow targets to override the size in bits of the RegisterClass.
254 unsigned Size = R->getValueAsInt("Size");
255
256 Namespace = R->getValueAsString("Namespace");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000257 SpillSize = Size ? Size : MVT(VTs[0]).getSizeInBits();
Nate Begeman6510b222005-12-01 04:51:06 +0000258 SpillAlignment = R->getValueAsInt("Alignment");
Evan Chenga3ca3142007-09-19 01:35:01 +0000259 CopyCost = R->getValueAsInt("CopyCost");
Nate Begeman6510b222005-12-01 04:51:06 +0000260 MethodBodies = R->getValueAsCode("MethodBodies");
261 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000262}
263
264const std::string &CodeGenRegisterClass::getName() const {
265 return TheDef->getName();
266}
267
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000268void CodeGenTarget::ReadLegalValueTypes() const {
269 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
270 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000271 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
272 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000273
274 // Remove duplicates.
275 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
276 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
277 LegalValueTypes.end()),
278 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000279}
Chris Lattner056afef2004-08-21 04:05:00 +0000280
Chris Lattner175580c2004-08-14 22:50:53 +0000281
Chris Lattnerec352402004-08-01 05:04:00 +0000282void CodeGenTarget::ReadInstructions() const {
283 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000284 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000285 throw std::string("No 'Instruction' subclasses defined!");
286
Chris Lattneraa77d772006-01-27 01:45:06 +0000287 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000288 std::string InstFormatName =
289 getAsmWriter()->getValueAsString("InstFormatName");
290
291 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
292 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
293 Instructions.insert(std::make_pair(Insts[i]->getName(),
294 CodeGenInstruction(Insts[i], AsmStr)));
295 }
Chris Lattnerec352402004-08-01 05:04:00 +0000296}
297
Chris Lattnerd6488672005-01-22 18:58:51 +0000298/// getInstructionsByEnumValue - Return all of the instructions defined by the
299/// target, ordered by their enum value.
300void CodeGenTarget::
301getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
302 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000303 std::map<std::string, CodeGenInstruction>::const_iterator I;
304 I = getInstructions().find("PHI");
305 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
306 const CodeGenInstruction *PHI = &I->second;
307
308 I = getInstructions().find("INLINEASM");
309 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
310 const CodeGenInstruction *INLINEASM = &I->second;
311
Dan Gohman44066042008-07-01 00:05:16 +0000312 I = getInstructions().find("DBG_LABEL");
313 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
314 const CodeGenInstruction *DBG_LABEL = &I->second;
315
316 I = getInstructions().find("EH_LABEL");
317 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
318 const CodeGenInstruction *EH_LABEL = &I->second;
319
320 I = getInstructions().find("GC_LABEL");
321 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
322 const CodeGenInstruction *GC_LABEL = &I->second;
Jim Laskeya683f9b2007-01-26 17:29:20 +0000323
Evan Chenga844bde2008-02-02 04:07:54 +0000324 I = getInstructions().find("DECLARE");
325 if (I == Instructions.end()) throw "Could not find 'DECLARE' instruction!";
326 const CodeGenInstruction *DECLARE = &I->second;
327
Christopher Lamb08d52072007-07-26 07:48:21 +0000328 I = getInstructions().find("EXTRACT_SUBREG");
329 if (I == Instructions.end())
330 throw "Could not find 'EXTRACT_SUBREG' instruction!";
331 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
332
333 I = getInstructions().find("INSERT_SUBREG");
334 if (I == Instructions.end())
335 throw "Could not find 'INSERT_SUBREG' instruction!";
336 const CodeGenInstruction *INSERT_SUBREG = &I->second;
337
Evan Chengda47e6e2008-03-15 00:03:38 +0000338 I = getInstructions().find("IMPLICIT_DEF");
339 if (I == Instructions.end())
340 throw "Could not find 'IMPLICIT_DEF' instruction!";
341 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
342
Christopher Lambc9298232008-03-16 03:12:01 +0000343 I = getInstructions().find("SUBREG_TO_REG");
344 if (I == Instructions.end())
345 throw "Could not find 'SUBREG_TO_REG' instruction!";
346 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000347
Dan Gohman88c7af02009-04-13 21:06:25 +0000348 I = getInstructions().find("COPY_TO_REGCLASS");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000349 if (I == Instructions.end())
Dan Gohman88c7af02009-04-13 21:06:25 +0000350 throw "Could not find 'COPY_TO_REGCLASS' instruction!";
351 const CodeGenInstruction *COPY_TO_REGCLASS = &I->second;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000352
Chris Lattnerd6488672005-01-22 18:58:51 +0000353 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000354 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000355 NumberedInstructions.push_back(INLINEASM);
Dan Gohman44066042008-07-01 00:05:16 +0000356 NumberedInstructions.push_back(DBG_LABEL);
357 NumberedInstructions.push_back(EH_LABEL);
358 NumberedInstructions.push_back(GC_LABEL);
Evan Chenga844bde2008-02-02 04:07:54 +0000359 NumberedInstructions.push_back(DECLARE);
Christopher Lamb08d52072007-07-26 07:48:21 +0000360 NumberedInstructions.push_back(EXTRACT_SUBREG);
361 NumberedInstructions.push_back(INSERT_SUBREG);
Evan Chengda47e6e2008-03-15 00:03:38 +0000362 NumberedInstructions.push_back(IMPLICIT_DEF);
Christopher Lambc9298232008-03-16 03:12:01 +0000363 NumberedInstructions.push_back(SUBREG_TO_REG);
Dan Gohman88c7af02009-04-13 21:06:25 +0000364 NumberedInstructions.push_back(COPY_TO_REGCLASS);
Chris Lattnerd6488672005-01-22 18:58:51 +0000365 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000366 if (&II->second != PHI &&
367 &II->second != INLINEASM &&
Dan Gohman44066042008-07-01 00:05:16 +0000368 &II->second != DBG_LABEL &&
369 &II->second != EH_LABEL &&
370 &II->second != GC_LABEL &&
Evan Chenga844bde2008-02-02 04:07:54 +0000371 &II->second != DECLARE &&
Christopher Lamb08d52072007-07-26 07:48:21 +0000372 &II->second != EXTRACT_SUBREG &&
Evan Chengda47e6e2008-03-15 00:03:38 +0000373 &II->second != INSERT_SUBREG &&
Christopher Lambc9298232008-03-16 03:12:01 +0000374 &II->second != IMPLICIT_DEF &&
Dan Gohmanf8c73942009-04-13 15:38:05 +0000375 &II->second != SUBREG_TO_REG &&
Dan Gohman88c7af02009-04-13 21:06:25 +0000376 &II->second != COPY_TO_REGCLASS)
Chris Lattnerd6488672005-01-22 18:58:51 +0000377 NumberedInstructions.push_back(&II->second);
378}
379
380
Misha Brukman35e83cc2004-10-14 05:50:43 +0000381/// isLittleEndianEncoding - Return whether this target encodes its instruction
382/// in little-endian format, i.e. bits laid out in the order [0..n]
383///
384bool CodeGenTarget::isLittleEndianEncoding() const {
385 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
386}
387
Evan Cheng0fc71982005-12-08 02:00:36 +0000388//===----------------------------------------------------------------------===//
389// ComplexPattern implementation
390//
391ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000392 Ty = ::getValueType(R->getValueAsDef("Ty"));
393 NumOperands = R->getValueAsInt("NumOperands");
394 SelectFunc = R->getValueAsString("SelectFunc");
395 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000396
397 // Parse the properties.
398 Properties = 0;
399 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
400 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
401 if (PropList[i]->getName() == "SDNPHasChain") {
402 Properties |= 1 << SDNPHasChain;
403 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
404 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerba7e7562008-01-10 07:59:24 +0000405 } else if (PropList[i]->getName() == "SDNPMayStore") {
406 Properties |= 1 << SDNPMayStore;
407 } else if (PropList[i]->getName() == "SDNPMayLoad") {
408 Properties |= 1 << SDNPMayLoad;
409 } else if (PropList[i]->getName() == "SDNPSideEffect") {
410 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000411 } else if (PropList[i]->getName() == "SDNPMemOperand") {
412 Properties |= 1 << SDNPMemOperand;
Evan Cheng94b30402006-10-11 21:02:01 +0000413 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000414 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
415 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000416 exit(1);
417 }
Christopher Lamb85356242008-01-31 07:27:46 +0000418
419 // Parse the attributes.
420 Attributes = 0;
421 PropList = R->getValueAsListOfDefs("Attributes");
422 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
423 if (PropList[i]->getName() == "CPAttrParentAsRoot") {
424 Attributes |= 1 << CPAttrParentAsRoot;
425 } else {
426 cerr << "Unsupported pattern attribute '" << PropList[i]->getName()
427 << "' on ComplexPattern '" << R->getName() << "'!\n";
428 exit(1);
429 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000430}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000431
Chris Lattner43fbbc32006-03-24 19:49:31 +0000432//===----------------------------------------------------------------------===//
433// CodeGenIntrinsic Implementation
434//===----------------------------------------------------------------------===//
435
Dale Johannesen49de9822009-02-05 01:49:45 +0000436std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
437 bool TargetOnly) {
Chris Lattner43fbbc32006-03-24 19:49:31 +0000438 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000439
440 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000441
Dale Johannesen49de9822009-02-05 01:49:45 +0000442 for (unsigned i = 0, e = I.size(); i != e; ++i) {
443 bool isTarget = I[i]->getValueAsBit("isTarget");
444 if (isTarget == TargetOnly)
445 Result.push_back(CodeGenIntrinsic(I[i]));
446 }
Chris Lattner8850a1b2006-03-27 22:48:18 +0000447 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000448}
449
Dan Gohmanee4fa192008-04-03 00:02:49 +0000450CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000451 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000452 std::string DefName = R->getName();
453 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000454 isOverloaded = false;
Evan Cheng6bd95672008-06-16 20:29:38 +0000455 isCommutative = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000456
457 if (DefName.size() <= 4 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000458 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000459 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000460
Chris Lattner43fbbc32006-03-24 19:49:31 +0000461 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000462
Chris Lattner43fbbc32006-03-24 19:49:31 +0000463 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
464 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000465
466 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000467 Name = R->getValueAsString("LLVMName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000468
Chris Lattner43fbbc32006-03-24 19:49:31 +0000469 if (Name == "") {
470 // If an explicit name isn't specified, derive one from the DefName.
471 Name = "llvm.";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000472
Chris Lattner43fbbc32006-03-24 19:49:31 +0000473 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000474 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner43fbbc32006-03-24 19:49:31 +0000475 } else {
476 // Verify it starts with "llvm.".
477 if (Name.size() <= 5 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000478 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000479 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
480 }
481
482 // If TargetPrefix is specified, make sure that Name starts with
483 // "llvm.<targetprefix>.".
484 if (!TargetPrefix.empty()) {
485 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000486 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
487 != (TargetPrefix + "."))
488 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Chris Lattner43fbbc32006-03-24 19:49:31 +0000489 TargetPrefix + ".'!";
490 }
491
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000492 // Parse the list of return types.
493 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000494 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000495 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000496 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000497 MVT::SimpleValueType VT;
498 if (TyEl->isSubClassOf("LLVMMatchType")) {
499 VT = IS.RetVTs[TyEl->getValueAsInt("Number")];
500 // It only makes sense to use the extended and truncated vector element
501 // variants with iAny types; otherwise, if the intrinsic is not
502 // overloaded, all the types can be specified directly.
503 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
504 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
505 VT == MVT::iAny) && "Expected iAny type");
506 } else
507 VT = getValueType(TyEl->getValueAsDef("VT"));
Mon P Wange3b3a722008-07-30 04:36:53 +0000508 isOverloaded |= VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000509 IS.RetVTs.push_back(VT);
510 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000511 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000512
513 if (IS.RetVTs.size() == 0)
Chris Lattner43fbbc32006-03-24 19:49:31 +0000514 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000515
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000516 // Parse the list of parameter types.
517 TypeList = R->getValueAsListInit("ParamTypes");
518 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
519 Record *TyEl = TypeList->getElementAsRecord(i);
520 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000521 MVT::SimpleValueType VT;
522 if (TyEl->isSubClassOf("LLVMMatchType")) {
523 unsigned MatchTy = TyEl->getValueAsInt("Number");
524 if (MatchTy < IS.RetVTs.size())
525 VT = IS.RetVTs[MatchTy];
526 else
527 VT = IS.ParamVTs[MatchTy - IS.RetVTs.size()];
528 // It only makes sense to use the extended and truncated vector element
529 // variants with iAny types; otherwise, if the intrinsic is not
530 // overloaded, all the types can be specified directly.
531 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
532 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
533 VT == MVT::iAny) && "Expected iAny type");
534 } else
535 VT = getValueType(TyEl->getValueAsDef("VT"));
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000536 isOverloaded |= VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny;
537 IS.ParamVTs.push_back(VT);
538 IS.ParamTypeDefs.push_back(TyEl);
539 }
540
Chris Lattner43fbbc32006-03-24 19:49:31 +0000541 // Parse the intrinsic properties.
542 ListInit *PropList = R->getValueAsListInit("Properties");
543 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000544 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000545 assert(Property->isSubClassOf("IntrinsicProperty") &&
546 "Expected a property!");
547
Chris Lattner4b2362e2006-04-10 22:02:59 +0000548 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000549 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000550 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000551 ModRef = ReadArgMem;
552 else if (Property->getName() == "IntrReadMem")
553 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000554 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000555 ModRef = WriteArgMem;
556 else if (Property->getName() == "IntrWriteMem")
557 ModRef = WriteMem;
Evan Cheng6bd95672008-06-16 20:29:38 +0000558 else if (Property->getName() == "Commutative")
559 isCommutative = true;
Chris Lattnera62c3022009-01-12 01:12:03 +0000560 else if (Property->isSubClassOf("NoCapture")) {
561 unsigned ArgNo = Property->getValueAsInt("ArgNo");
562 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
563 } else
Chris Lattner43fbbc32006-03-24 19:49:31 +0000564 assert(0 && "Unknown property!");
565 }
566}