blob: ec0e6d7cc429548a4a94aff40ac5663d770bf64b [file] [log] [blame]
Chris Lattner6cc654b2008-01-06 01:35:39 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner45872072003-08-07 05:38:11 +00009//
Chris Lattner6cc654b2008-01-06 01:35:39 +000010// This class wraps target description classes used by the various code
Chris Lattner45872072003-08-07 05:38:11 +000011// generation TableGen backends. This makes it easier to access the data and
12// provides a single place that needs to check it for validity. All of these
13// classes throw exceptions on error conditions.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner803a5f62004-08-01 04:04:35 +000017#include "CodeGenTarget.h"
Chris Lattner43fbbc32006-03-24 19:49:31 +000018#include "CodeGenIntrinsics.h"
Chris Lattner45872072003-08-07 05:38:11 +000019#include "Record.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000020#include "llvm/ADT/StringExtras.h"
21#include "llvm/Support/CommandLine.h"
Chris Lattner75ee2eb2005-10-14 03:54:49 +000022#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner560a79f2004-10-03 19:34:31 +000025static cl::opt<unsigned>
26AsmWriterNum("asmwriternum", cl::init(0),
27 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
28
Duncan Sands83ec4b62008-06-06 12:08:01 +000029/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
30/// record corresponds to.
31MVT::SimpleValueType llvm::getValueType(Record *Rec) {
32 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000033}
34
Duncan Sands83ec4b62008-06-06 12:08:01 +000035std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000036 switch (T) {
Bob Wilsonbbf1caa2009-07-10 22:25:24 +000037 case MVT::Other: return "UNKNOWN";
38 case MVT::iPTR: return "TLI.getPointerTy()";
39 case MVT::iPTRAny: return "TLI.getPointerTy()";
40 default: return getEnumName(T);
Chris Lattner45872072003-08-07 05:38:11 +000041 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000042}
43
Duncan Sands83ec4b62008-06-06 12:08:01 +000044std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000045 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000046 case MVT::Other: return "MVT::Other";
47 case MVT::i1: return "MVT::i1";
48 case MVT::i8: return "MVT::i8";
49 case MVT::i16: return "MVT::i16";
50 case MVT::i32: return "MVT::i32";
51 case MVT::i64: return "MVT::i64";
52 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000053 case MVT::iAny: return "MVT::iAny";
Dan Gohman0fee3ff2007-08-16 21:57:19 +000054 case MVT::fAny: return "MVT::fAny";
Evan Cheng2618d072006-05-17 20:37:59 +000055 case MVT::f32: return "MVT::f32";
56 case MVT::f64: return "MVT::f64";
57 case MVT::f80: return "MVT::f80";
58 case MVT::f128: return "MVT::f128";
Dale Johannesen317096a2007-09-28 01:08:20 +000059 case MVT::ppcf128: return "MVT::ppcf128";
Evan Cheng2618d072006-05-17 20:37:59 +000060 case MVT::Flag: return "MVT::Flag";
61 case MVT::isVoid:return "MVT::isVoid";
Mon P Wang87c8a8f2008-12-18 20:03:17 +000062 case MVT::v2i8: return "MVT::v2i8";
63 case MVT::v4i8: return "MVT::v4i8";
Evan Cheng2618d072006-05-17 20:37:59 +000064 case MVT::v8i8: return "MVT::v8i8";
Evan Cheng2618d072006-05-17 20:37:59 +000065 case MVT::v16i8: return "MVT::v16i8";
Bob Wilson28eff962009-07-10 23:05:09 +000066 case MVT::v24i8: return "MVT::v24i8";
David Greene9b9838d2009-06-29 16:47:10 +000067 case MVT::v32i8: return "MVT::v32i8";
Bob Wilson28eff962009-07-10 23:05:09 +000068 case MVT::v48i8: return "MVT::v48i8";
69 case MVT::v64i8: return "MVT::v64i8";
David Greene9b9838d2009-06-29 16:47:10 +000070 case MVT::v2i16: return "MVT::v2i16";
71 case MVT::v4i16: return "MVT::v4i16";
Evan Cheng2618d072006-05-17 20:37:59 +000072 case MVT::v8i16: return "MVT::v8i16";
Bob Wilson28eff962009-07-10 23:05:09 +000073 case MVT::v12i16: return "MVT::v12i16";
David Greene9b9838d2009-06-29 16:47:10 +000074 case MVT::v16i16: return "MVT::v16i16";
Bob Wilson28eff962009-07-10 23:05:09 +000075 case MVT::v24i16: return "MVT::v24i16";
76 case MVT::v32i16: return "MVT::v32i16";
David Greene9b9838d2009-06-29 16:47:10 +000077 case MVT::v2i32: return "MVT::v2i32";
Bob Wilson28eff962009-07-10 23:05:09 +000078 case MVT::v3i32: return "MVT::v3i32";
Evan Cheng2618d072006-05-17 20:37:59 +000079 case MVT::v4i32: return "MVT::v4i32";
Bob Wilson28eff962009-07-10 23:05:09 +000080 case MVT::v6i32: return "MVT::v6i32";
David Greene9b9838d2009-06-29 16:47:10 +000081 case MVT::v8i32: return "MVT::v8i32";
Bob Wilson28eff962009-07-10 23:05:09 +000082 case MVT::v12i32: return "MVT::v12i32";
83 case MVT::v16i32: return "MVT::v16i32";
David Greene9b9838d2009-06-29 16:47:10 +000084 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +000085 case MVT::v2i64: return "MVT::v2i64";
Bob Wilson28eff962009-07-10 23:05:09 +000086 case MVT::v3i64: return "MVT::v3i64";
David Greene9b9838d2009-06-29 16:47:10 +000087 case MVT::v4i64: return "MVT::v4i64";
Bob Wilson28eff962009-07-10 23:05:09 +000088 case MVT::v6i64: return "MVT::v6i64";
89 case MVT::v8i64: return "MVT::v8i64";
Evan Cheng2618d072006-05-17 20:37:59 +000090 case MVT::v2f32: return "MVT::v2f32";
Bob Wilson28eff962009-07-10 23:05:09 +000091 case MVT::v3f32: return "MVT::v3f32";
Evan Cheng2618d072006-05-17 20:37:59 +000092 case MVT::v4f32: return "MVT::v4f32";
Bob Wilson28eff962009-07-10 23:05:09 +000093 case MVT::v6f32: return "MVT::v6f32";
David Greene9b9838d2009-06-29 16:47:10 +000094 case MVT::v8f32: return "MVT::v8f32";
Bob Wilson28eff962009-07-10 23:05:09 +000095 case MVT::v12f32: return "MVT::v12f32";
96 case MVT::v16f32: return "MVT::v16f32";
Evan Cheng2618d072006-05-17 20:37:59 +000097 case MVT::v2f64: return "MVT::v2f64";
David Greene9b9838d2009-06-29 16:47:10 +000098 case MVT::v4f64: return "MVT::v4f64";
Devang Patelbee9e352009-07-06 23:44:32 +000099 case MVT::Metadata: return "MVT::Metadata";
Chandler Carruth69940402007-08-04 01:51:18 +0000100 case MVT::iPTR: return "MVT::iPTR";
Mon P Wange3b3a722008-07-30 04:36:53 +0000101 case MVT::iPTRAny: return "MVT::iPTRAny";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +0000102 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
103 }
104}
105
Chris Lattner6cefb772008-01-05 22:25:12 +0000106/// getQualifiedName - Return the name of the specified record, with a
107/// namespace qualifier if the record contains one.
108///
109std::string llvm::getQualifiedName(const Record *R) {
110 std::string Namespace = R->getValueAsString("Namespace");
111 if (Namespace.empty()) return R->getName();
112 return Namespace + "::" + R->getName();
113}
114
115
116
Chris Lattnerd3464c12003-08-07 23:15:21 +0000117
Chris Lattner45872072003-08-07 05:38:11 +0000118/// getTarget - Return the current instance of the Target class.
119///
Evan Cheng2618d072006-05-17 20:37:59 +0000120CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000121 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000122 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000123 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000124 if (Targets.size() != 1)
125 throw std::string("ERROR: Multiple subclasses of Target defined!");
126 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000127}
128
129
130const std::string &CodeGenTarget::getName() const {
131 return TargetRec->getName();
132}
133
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000134std::string CodeGenTarget::getInstNamespace() const {
135 std::string InstNS;
136
137 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
138 InstNS = i->second.Namespace;
139
140 // Make sure not to pick up "TargetInstrInfo" by accidentally getting
141 // the namespace off the PHI instruction or something.
142 if (InstNS != "TargetInstrInfo")
143 break;
144 }
145
146 return InstNS;
147}
148
Chris Lattner45872072003-08-07 05:38:11 +0000149Record *CodeGenTarget::getInstructionSet() const {
150 return TargetRec->getValueAsDef("InstructionSet");
151}
Brian Gaeked0fde302003-11-11 22:41:34 +0000152
Chris Lattner175580c2004-08-14 22:50:53 +0000153/// getAsmWriter - Return the AssemblyWriter definition for this target.
154///
155Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000156 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
157 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000158 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000159 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000160}
161
Chris Lattner26693112004-08-16 01:10:21 +0000162void CodeGenTarget::ReadRegisters() const {
163 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
164 if (Regs.empty())
165 throw std::string("No 'Register' subclasses defined!");
166
167 Registers.reserve(Regs.size());
168 Registers.assign(Regs.begin(), Regs.end());
169}
170
Chris Lattner7a680c62004-08-21 02:24:57 +0000171CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
172 DeclaredSpillSize = R->getValueAsInt("SpillSize");
173 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
174}
175
Chris Lattner26693112004-08-16 01:10:21 +0000176const std::string &CodeGenRegister::getName() const {
177 return TheDef->getName();
178}
179
Chris Lattner056afef2004-08-21 04:05:00 +0000180void CodeGenTarget::ReadRegisterClasses() const {
181 std::vector<Record*> RegClasses =
182 Records.getAllDerivedDefinitions("RegisterClass");
183 if (RegClasses.empty())
184 throw std::string("No 'RegisterClass' subclasses defined!");
185
186 RegisterClasses.reserve(RegClasses.size());
187 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
188}
189
Evan Cheng44a65fa2006-05-16 07:05:30 +0000190std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
191 std::vector<unsigned char> Result;
192 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
193 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
194 const CodeGenRegisterClass &RC = RegisterClasses[i];
195 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
196 if (R == RC.Elements[ei]) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000197 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Evan Cheng44a65fa2006-05-16 07:05:30 +0000198 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
199 Result.push_back(InVTs[i]);
200 }
201 }
202 }
203 return Result;
204}
205
206
Chris Lattner056afef2004-08-21 04:05:00 +0000207CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000208 // Rename anonymous register classes.
209 if (R->getName().size() > 9 && R->getName()[9] == '.') {
210 static unsigned AnonCounter = 0;
211 R->setName("AnonRegClass_"+utostr(AnonCounter++));
212 }
213
Nate Begeman6510b222005-12-01 04:51:06 +0000214 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
215 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
216 Record *Type = TypeList[i];
217 if (!Type->isSubClassOf("ValueType"))
218 throw "RegTypes list member '" + Type->getName() +
219 "' does not derive from the ValueType class!";
220 VTs.push_back(getValueType(Type));
221 }
222 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000223
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000224 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
225 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
226 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000227 if (!Reg->isSubClassOf("Register"))
228 throw "Register Class member '" + Reg->getName() +
229 "' does not derive from the Register class!";
230 Elements.push_back(Reg);
231 }
Nate Begeman6510b222005-12-01 04:51:06 +0000232
Christopher Lamba3211252007-06-13 22:20:15 +0000233 std::vector<Record*> SubRegClassList =
234 R->getValueAsListOfDefs("SubRegClassList");
235 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
236 Record *SubRegClass = SubRegClassList[i];
237 if (!SubRegClass->isSubClassOf("RegisterClass"))
238 throw "Register Class member '" + SubRegClass->getName() +
239 "' does not derive from the RegisterClass class!";
240 SubRegClasses.push_back(SubRegClass);
241 }
242
Nate Begeman6510b222005-12-01 04:51:06 +0000243 // Allow targets to override the size in bits of the RegisterClass.
244 unsigned Size = R->getValueAsInt("Size");
245
246 Namespace = R->getValueAsString("Namespace");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000247 SpillSize = Size ? Size : MVT(VTs[0]).getSizeInBits();
Nate Begeman6510b222005-12-01 04:51:06 +0000248 SpillAlignment = R->getValueAsInt("Alignment");
Evan Chenga3ca3142007-09-19 01:35:01 +0000249 CopyCost = R->getValueAsInt("CopyCost");
Nate Begeman6510b222005-12-01 04:51:06 +0000250 MethodBodies = R->getValueAsCode("MethodBodies");
251 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000252}
253
254const std::string &CodeGenRegisterClass::getName() const {
255 return TheDef->getName();
256}
257
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000258void CodeGenTarget::ReadLegalValueTypes() const {
259 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
260 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000261 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
262 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000263
264 // Remove duplicates.
265 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
266 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
267 LegalValueTypes.end()),
268 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000269}
Chris Lattner056afef2004-08-21 04:05:00 +0000270
Chris Lattner175580c2004-08-14 22:50:53 +0000271
Chris Lattnerec352402004-08-01 05:04:00 +0000272void CodeGenTarget::ReadInstructions() const {
273 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000274 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000275 throw std::string("No 'Instruction' subclasses defined!");
276
Chris Lattneraa77d772006-01-27 01:45:06 +0000277 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000278 std::string InstFormatName =
279 getAsmWriter()->getValueAsString("InstFormatName");
280
281 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
282 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
283 Instructions.insert(std::make_pair(Insts[i]->getName(),
284 CodeGenInstruction(Insts[i], AsmStr)));
285 }
Chris Lattnerec352402004-08-01 05:04:00 +0000286}
287
Chris Lattnerd6488672005-01-22 18:58:51 +0000288/// getInstructionsByEnumValue - Return all of the instructions defined by the
289/// target, ordered by their enum value.
290void CodeGenTarget::
291getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
292 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000293 std::map<std::string, CodeGenInstruction>::const_iterator I;
294 I = getInstructions().find("PHI");
295 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
296 const CodeGenInstruction *PHI = &I->second;
297
298 I = getInstructions().find("INLINEASM");
299 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
300 const CodeGenInstruction *INLINEASM = &I->second;
301
Dan Gohman44066042008-07-01 00:05:16 +0000302 I = getInstructions().find("DBG_LABEL");
303 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
304 const CodeGenInstruction *DBG_LABEL = &I->second;
305
306 I = getInstructions().find("EH_LABEL");
307 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
308 const CodeGenInstruction *EH_LABEL = &I->second;
309
310 I = getInstructions().find("GC_LABEL");
311 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
312 const CodeGenInstruction *GC_LABEL = &I->second;
Jim Laskeya683f9b2007-01-26 17:29:20 +0000313
Evan Chenga844bde2008-02-02 04:07:54 +0000314 I = getInstructions().find("DECLARE");
315 if (I == Instructions.end()) throw "Could not find 'DECLARE' instruction!";
316 const CodeGenInstruction *DECLARE = &I->second;
317
Christopher Lamb08d52072007-07-26 07:48:21 +0000318 I = getInstructions().find("EXTRACT_SUBREG");
319 if (I == Instructions.end())
320 throw "Could not find 'EXTRACT_SUBREG' instruction!";
321 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
322
323 I = getInstructions().find("INSERT_SUBREG");
324 if (I == Instructions.end())
325 throw "Could not find 'INSERT_SUBREG' instruction!";
326 const CodeGenInstruction *INSERT_SUBREG = &I->second;
327
Evan Chengda47e6e2008-03-15 00:03:38 +0000328 I = getInstructions().find("IMPLICIT_DEF");
329 if (I == Instructions.end())
330 throw "Could not find 'IMPLICIT_DEF' instruction!";
331 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
332
Christopher Lambc9298232008-03-16 03:12:01 +0000333 I = getInstructions().find("SUBREG_TO_REG");
334 if (I == Instructions.end())
335 throw "Could not find 'SUBREG_TO_REG' instruction!";
336 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000337
Dan Gohman88c7af02009-04-13 21:06:25 +0000338 I = getInstructions().find("COPY_TO_REGCLASS");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000339 if (I == Instructions.end())
Dan Gohman88c7af02009-04-13 21:06:25 +0000340 throw "Could not find 'COPY_TO_REGCLASS' instruction!";
341 const CodeGenInstruction *COPY_TO_REGCLASS = &I->second;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000342
Chris Lattnerd6488672005-01-22 18:58:51 +0000343 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000344 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000345 NumberedInstructions.push_back(INLINEASM);
Dan Gohman44066042008-07-01 00:05:16 +0000346 NumberedInstructions.push_back(DBG_LABEL);
347 NumberedInstructions.push_back(EH_LABEL);
348 NumberedInstructions.push_back(GC_LABEL);
Evan Chenga844bde2008-02-02 04:07:54 +0000349 NumberedInstructions.push_back(DECLARE);
Christopher Lamb08d52072007-07-26 07:48:21 +0000350 NumberedInstructions.push_back(EXTRACT_SUBREG);
351 NumberedInstructions.push_back(INSERT_SUBREG);
Evan Chengda47e6e2008-03-15 00:03:38 +0000352 NumberedInstructions.push_back(IMPLICIT_DEF);
Christopher Lambc9298232008-03-16 03:12:01 +0000353 NumberedInstructions.push_back(SUBREG_TO_REG);
Dan Gohman88c7af02009-04-13 21:06:25 +0000354 NumberedInstructions.push_back(COPY_TO_REGCLASS);
Chris Lattnerd6488672005-01-22 18:58:51 +0000355 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000356 if (&II->second != PHI &&
357 &II->second != INLINEASM &&
Dan Gohman44066042008-07-01 00:05:16 +0000358 &II->second != DBG_LABEL &&
359 &II->second != EH_LABEL &&
360 &II->second != GC_LABEL &&
Evan Chenga844bde2008-02-02 04:07:54 +0000361 &II->second != DECLARE &&
Christopher Lamb08d52072007-07-26 07:48:21 +0000362 &II->second != EXTRACT_SUBREG &&
Evan Chengda47e6e2008-03-15 00:03:38 +0000363 &II->second != INSERT_SUBREG &&
Christopher Lambc9298232008-03-16 03:12:01 +0000364 &II->second != IMPLICIT_DEF &&
Dan Gohmanf8c73942009-04-13 15:38:05 +0000365 &II->second != SUBREG_TO_REG &&
Dan Gohman88c7af02009-04-13 21:06:25 +0000366 &II->second != COPY_TO_REGCLASS)
Chris Lattnerd6488672005-01-22 18:58:51 +0000367 NumberedInstructions.push_back(&II->second);
368}
369
370
Misha Brukman35e83cc2004-10-14 05:50:43 +0000371/// isLittleEndianEncoding - Return whether this target encodes its instruction
372/// in little-endian format, i.e. bits laid out in the order [0..n]
373///
374bool CodeGenTarget::isLittleEndianEncoding() const {
375 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
376}
377
Evan Cheng0fc71982005-12-08 02:00:36 +0000378//===----------------------------------------------------------------------===//
379// ComplexPattern implementation
380//
381ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000382 Ty = ::getValueType(R->getValueAsDef("Ty"));
383 NumOperands = R->getValueAsInt("NumOperands");
384 SelectFunc = R->getValueAsString("SelectFunc");
385 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000386
387 // Parse the properties.
388 Properties = 0;
389 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
390 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
391 if (PropList[i]->getName() == "SDNPHasChain") {
392 Properties |= 1 << SDNPHasChain;
393 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
394 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerba7e7562008-01-10 07:59:24 +0000395 } else if (PropList[i]->getName() == "SDNPMayStore") {
396 Properties |= 1 << SDNPMayStore;
397 } else if (PropList[i]->getName() == "SDNPMayLoad") {
398 Properties |= 1 << SDNPMayLoad;
399 } else if (PropList[i]->getName() == "SDNPSideEffect") {
400 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000401 } else if (PropList[i]->getName() == "SDNPMemOperand") {
402 Properties |= 1 << SDNPMemOperand;
Evan Cheng94b30402006-10-11 21:02:01 +0000403 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000404 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
405 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000406 exit(1);
407 }
Christopher Lamb85356242008-01-31 07:27:46 +0000408
409 // Parse the attributes.
410 Attributes = 0;
411 PropList = R->getValueAsListOfDefs("Attributes");
412 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
413 if (PropList[i]->getName() == "CPAttrParentAsRoot") {
414 Attributes |= 1 << CPAttrParentAsRoot;
415 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000416 errs() << "Unsupported pattern attribute '" << PropList[i]->getName()
417 << "' on ComplexPattern '" << R->getName() << "'!\n";
Christopher Lamb85356242008-01-31 07:27:46 +0000418 exit(1);
419 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000420}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000421
Chris Lattner43fbbc32006-03-24 19:49:31 +0000422//===----------------------------------------------------------------------===//
423// CodeGenIntrinsic Implementation
424//===----------------------------------------------------------------------===//
425
Dale Johannesen49de9822009-02-05 01:49:45 +0000426std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
427 bool TargetOnly) {
Chris Lattner43fbbc32006-03-24 19:49:31 +0000428 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000429
430 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000431
Dale Johannesen49de9822009-02-05 01:49:45 +0000432 for (unsigned i = 0, e = I.size(); i != e; ++i) {
433 bool isTarget = I[i]->getValueAsBit("isTarget");
434 if (isTarget == TargetOnly)
435 Result.push_back(CodeGenIntrinsic(I[i]));
436 }
Chris Lattner8850a1b2006-03-27 22:48:18 +0000437 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000438}
439
Dan Gohmanee4fa192008-04-03 00:02:49 +0000440CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000441 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000442 std::string DefName = R->getName();
443 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000444 isOverloaded = false;
Evan Cheng6bd95672008-06-16 20:29:38 +0000445 isCommutative = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000446
447 if (DefName.size() <= 4 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000448 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000449 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000450
Chris Lattner43fbbc32006-03-24 19:49:31 +0000451 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000452
Chris Lattner43fbbc32006-03-24 19:49:31 +0000453 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
454 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000455
456 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000457 Name = R->getValueAsString("LLVMName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000458
Chris Lattner43fbbc32006-03-24 19:49:31 +0000459 if (Name == "") {
460 // If an explicit name isn't specified, derive one from the DefName.
461 Name = "llvm.";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000462
Chris Lattner43fbbc32006-03-24 19:49:31 +0000463 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000464 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner43fbbc32006-03-24 19:49:31 +0000465 } else {
466 // Verify it starts with "llvm.".
467 if (Name.size() <= 5 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000468 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000469 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
470 }
471
472 // If TargetPrefix is specified, make sure that Name starts with
473 // "llvm.<targetprefix>.".
474 if (!TargetPrefix.empty()) {
475 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000476 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
477 != (TargetPrefix + "."))
478 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Chris Lattner43fbbc32006-03-24 19:49:31 +0000479 TargetPrefix + ".'!";
480 }
481
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000482 // Parse the list of return types.
Bob Wilsond305e702009-04-16 21:51:05 +0000483 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000484 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000485 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000486 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000487 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000488 MVT::SimpleValueType VT;
489 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsond305e702009-04-16 21:51:05 +0000490 unsigned MatchTy = TyEl->getValueAsInt("Number");
491 assert(MatchTy < OverloadedVTs.size() &&
492 "Invalid matching number!");
493 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000494 // It only makes sense to use the extended and truncated vector element
495 // variants with iAny types; otherwise, if the intrinsic is not
496 // overloaded, all the types can be specified directly.
497 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
498 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
499 VT == MVT::iAny) && "Expected iAny type");
Bob Wilsond305e702009-04-16 21:51:05 +0000500 } else {
Bob Wilsonbc039792009-01-07 00:09:01 +0000501 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsond305e702009-04-16 21:51:05 +0000502 }
503 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny) {
504 OverloadedVTs.push_back(VT);
505 isOverloaded |= true;
506 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000507 IS.RetVTs.push_back(VT);
508 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000509 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000510
511 if (IS.RetVTs.size() == 0)
Chris Lattner43fbbc32006-03-24 19:49:31 +0000512 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000513
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000514 // Parse the list of parameter types.
515 TypeList = R->getValueAsListInit("ParamTypes");
516 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
517 Record *TyEl = TypeList->getElementAsRecord(i);
518 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000519 MVT::SimpleValueType VT;
520 if (TyEl->isSubClassOf("LLVMMatchType")) {
521 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsond305e702009-04-16 21:51:05 +0000522 assert(MatchTy < OverloadedVTs.size() &&
523 "Invalid matching number!");
524 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000525 // It only makes sense to use the extended and truncated vector element
526 // variants with iAny types; otherwise, if the intrinsic is not
527 // overloaded, all the types can be specified directly.
528 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
529 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
530 VT == MVT::iAny) && "Expected iAny type");
531 } else
532 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsond305e702009-04-16 21:51:05 +0000533 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny) {
534 OverloadedVTs.push_back(VT);
535 isOverloaded |= true;
536 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000537 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}