blob: 091a32fb3fe129a010fb3f3270ceab5be413b704 [file] [log] [blame]
Chris Lattner6cc654b2008-01-06 01:35:39 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner45872072003-08-07 05:38:11 +00009//
Chris Lattner6cc654b2008-01-06 01:35:39 +000010// This class wraps target description classes used by the various code
Chris Lattner45872072003-08-07 05:38:11 +000011// generation TableGen backends. This makes it easier to access the data and
12// provides a single place that needs to check it for validity. All of these
13// classes throw exceptions on error conditions.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner803a5f62004-08-01 04:04:35 +000017#include "CodeGenTarget.h"
Chris Lattner43fbbc32006-03-24 19:49:31 +000018#include "CodeGenIntrinsics.h"
Chris Lattner45872072003-08-07 05:38:11 +000019#include "Record.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner0d7952e2010-03-27 20:32:26 +000021#include "llvm/ADT/STLExtras.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000022#include "llvm/Support/CommandLine.h"
Chris Lattner75ee2eb2005-10-14 03:54:49 +000023#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner560a79f2004-10-03 19:34:31 +000026static cl::opt<unsigned>
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000027AsmParserNum("asmparsernum", cl::init(0),
28 cl::desc("Make -gen-asm-parser emit assembly parser #N"));
29
30static cl::opt<unsigned>
Chris Lattner560a79f2004-10-03 19:34:31 +000031AsmWriterNum("asmwriternum", cl::init(0),
32 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
33
Owen Anderson825b72b2009-08-11 20:47:22 +000034/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands83ec4b62008-06-06 12:08:01 +000035/// record corresponds to.
Owen Anderson825b72b2009-08-11 20:47:22 +000036MVT::SimpleValueType llvm::getValueType(Record *Rec) {
37 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000038}
39
Owen Anderson825b72b2009-08-11 20:47:22 +000040std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000041 switch (T) {
Owen Anderson825b72b2009-08-11 20:47:22 +000042 case MVT::Other: return "UNKNOWN";
43 case MVT::iPTR: return "TLI.getPointerTy()";
44 case MVT::iPTRAny: return "TLI.getPointerTy()";
Bob Wilsonbbf1caa2009-07-10 22:25:24 +000045 default: return getEnumName(T);
Chris Lattner45872072003-08-07 05:38:11 +000046 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000047}
48
Owen Anderson825b72b2009-08-11 20:47:22 +000049std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000050 switch (T) {
Bill Wendling72d10272010-09-07 18:49:14 +000051 case MVT::Other: return "MVT::Other";
52 case MVT::i1: return "MVT::i1";
53 case MVT::i8: return "MVT::i8";
54 case MVT::i16: return "MVT::i16";
55 case MVT::i32: return "MVT::i32";
56 case MVT::i64: return "MVT::i64";
57 case MVT::i128: return "MVT::i128";
58 case MVT::iAny: return "MVT::iAny";
59 case MVT::fAny: return "MVT::fAny";
60 case MVT::vAny: return "MVT::vAny";
61 case MVT::f32: return "MVT::f32";
62 case MVT::f64: return "MVT::f64";
63 case MVT::f80: return "MVT::f80";
64 case MVT::f128: return "MVT::f128";
Owen Anderson825b72b2009-08-11 20:47:22 +000065 case MVT::ppcf128: return "MVT::ppcf128";
Bill Wendlingd8dd5752010-09-07 20:03:56 +000066 case MVT::x86mmx: return "MVT::x86mmx";
Bill Wendling72d10272010-09-07 18:49:14 +000067 case MVT::Flag: return "MVT::Flag";
68 case MVT::isVoid: return "MVT::isVoid";
69 case MVT::v2i8: return "MVT::v2i8";
70 case MVT::v4i8: return "MVT::v4i8";
71 case MVT::v8i8: return "MVT::v8i8";
72 case MVT::v16i8: return "MVT::v16i8";
73 case MVT::v32i8: return "MVT::v32i8";
74 case MVT::v2i16: return "MVT::v2i16";
75 case MVT::v4i16: return "MVT::v4i16";
76 case MVT::v8i16: return "MVT::v8i16";
77 case MVT::v16i16: return "MVT::v16i16";
78 case MVT::v2i32: return "MVT::v2i32";
79 case MVT::v4i32: return "MVT::v4i32";
80 case MVT::v8i32: return "MVT::v8i32";
81 case MVT::v1i64: return "MVT::v1i64";
82 case MVT::v2i64: return "MVT::v2i64";
83 case MVT::v4i64: return "MVT::v4i64";
84 case MVT::v8i64: return "MVT::v8i64";
85 case MVT::v2f32: return "MVT::v2f32";
86 case MVT::v4f32: return "MVT::v4f32";
87 case MVT::v8f32: return "MVT::v8f32";
88 case MVT::v2f64: return "MVT::v2f64";
89 case MVT::v4f64: return "MVT::v4f64";
Owen Anderson825b72b2009-08-11 20:47:22 +000090 case MVT::Metadata: return "MVT::Metadata";
Bill Wendling72d10272010-09-07 18:49:14 +000091 case MVT::iPTR: return "MVT::iPTR";
Owen Anderson825b72b2009-08-11 20:47:22 +000092 case MVT::iPTRAny: return "MVT::iPTRAny";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000093 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
94 }
95}
96
Chris Lattner6cefb772008-01-05 22:25:12 +000097/// getQualifiedName - Return the name of the specified record, with a
98/// namespace qualifier if the record contains one.
99///
100std::string llvm::getQualifiedName(const Record *R) {
101 std::string Namespace = R->getValueAsString("Namespace");
102 if (Namespace.empty()) return R->getName();
103 return Namespace + "::" + R->getName();
104}
105
106
107
Chris Lattnerd3464c12003-08-07 23:15:21 +0000108
Chris Lattner45872072003-08-07 05:38:11 +0000109/// getTarget - Return the current instance of the Target class.
110///
Evan Cheng2618d072006-05-17 20:37:59 +0000111CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000112 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000113 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000114 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000115 if (Targets.size() != 1)
116 throw std::string("ERROR: Multiple subclasses of Target defined!");
117 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000118}
119
120
121const std::string &CodeGenTarget::getName() const {
122 return TargetRec->getName();
123}
124
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000125std::string CodeGenTarget::getInstNamespace() const {
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000126 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
Chris Lattner6a91b182010-03-19 01:00:55 +0000127 // Make sure not to pick up "TargetOpcode" by accidentally getting
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000128 // the namespace off the PHI instruction or something.
Chris Lattner6a91b182010-03-19 01:00:55 +0000129 if ((*i)->Namespace != "TargetOpcode")
130 return (*i)->Namespace;
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000131 }
132
Chris Lattner6a91b182010-03-19 01:00:55 +0000133 return "";
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000134}
135
Chris Lattner45872072003-08-07 05:38:11 +0000136Record *CodeGenTarget::getInstructionSet() const {
137 return TargetRec->getValueAsDef("InstructionSet");
138}
Brian Gaeked0fde302003-11-11 22:41:34 +0000139
Chris Lattnerf30187a2010-03-19 00:07:20 +0000140
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000141/// getAsmParser - Return the AssemblyParser definition for this target.
142///
143Record *CodeGenTarget::getAsmParser() const {
144 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
145 if (AsmParserNum >= LI.size())
146 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
147 return LI[AsmParserNum];
148}
149
Chris Lattner175580c2004-08-14 22:50:53 +0000150/// getAsmWriter - Return the AssemblyWriter definition for this target.
151///
152Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000153 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
154 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000155 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000156 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000157}
158
Chris Lattner26693112004-08-16 01:10:21 +0000159void CodeGenTarget::ReadRegisters() const {
160 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
161 if (Regs.empty())
162 throw std::string("No 'Register' subclasses defined!");
Jakob Stoklund Olesen160a3bf2010-05-26 21:47:28 +0000163 std::sort(Regs.begin(), Regs.end(), LessRecord());
Chris Lattner26693112004-08-16 01:10:21 +0000164
165 Registers.reserve(Regs.size());
166 Registers.assign(Regs.begin(), Regs.end());
167}
168
Chris Lattner7a680c62004-08-21 02:24:57 +0000169CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
170 DeclaredSpillSize = R->getValueAsInt("SpillSize");
171 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
172}
173
Chris Lattner26693112004-08-16 01:10:21 +0000174const std::string &CodeGenRegister::getName() const {
175 return TheDef->getName();
176}
177
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +0000178void CodeGenTarget::ReadSubRegIndices() const {
179 SubRegIndices = Records.getAllDerivedDefinitions("SubRegIndex");
Jakob Stoklund Olesen160a3bf2010-05-26 21:47:28 +0000180 std::sort(SubRegIndices.begin(), SubRegIndices.end(), LessRecord());
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +0000181}
182
Chris Lattner056afef2004-08-21 04:05:00 +0000183void CodeGenTarget::ReadRegisterClasses() const {
184 std::vector<Record*> RegClasses =
185 Records.getAllDerivedDefinitions("RegisterClass");
186 if (RegClasses.empty())
187 throw std::string("No 'RegisterClass' subclasses defined!");
188
189 RegisterClasses.reserve(RegClasses.size());
190 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
191}
192
Chris Lattner2cacec52010-03-15 06:00:16 +0000193std::vector<MVT::SimpleValueType> CodeGenTarget::
194getRegisterVTs(Record *R) const {
195 std::vector<MVT::SimpleValueType> Result;
Evan Cheng44a65fa2006-05-16 07:05:30 +0000196 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
197 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
198 const CodeGenRegisterClass &RC = RegisterClasses[i];
199 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
200 if (R == RC.Elements[ei]) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Chris Lattner2cacec52010-03-15 06:00:16 +0000202 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Evan Cheng44a65fa2006-05-16 07:05:30 +0000203 }
204 }
205 }
Chris Lattner0d7952e2010-03-27 20:32:26 +0000206
207 // Remove duplicates.
208 array_pod_sort(Result.begin(), Result.end());
209 Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
Evan Cheng44a65fa2006-05-16 07:05:30 +0000210 return Result;
211}
212
213
Chris Lattner056afef2004-08-21 04:05:00 +0000214CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000215 // Rename anonymous register classes.
216 if (R->getName().size() > 9 && R->getName()[9] == '.') {
217 static unsigned AnonCounter = 0;
218 R->setName("AnonRegClass_"+utostr(AnonCounter++));
219 }
220
Nate Begeman6510b222005-12-01 04:51:06 +0000221 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
222 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
223 Record *Type = TypeList[i];
224 if (!Type->isSubClassOf("ValueType"))
225 throw "RegTypes list member '" + Type->getName() +
226 "' does not derive from the ValueType class!";
227 VTs.push_back(getValueType(Type));
228 }
229 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000230
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000231 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
232 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
233 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000234 if (!Reg->isSubClassOf("Register"))
235 throw "Register Class member '" + Reg->getName() +
236 "' does not derive from the Register class!";
237 Elements.push_back(Reg);
238 }
Jakob Stoklund Olesen09bc0292010-05-24 21:46:58 +0000239
240 // SubRegClasses is a list<dag> containing (RC, subregindex, ...) dags.
241 ListInit *SRC = R->getValueAsListInit("SubRegClasses");
242 for (ListInit::const_iterator i = SRC->begin(), e = SRC->end(); i != e; ++i) {
243 DagInit *DAG = dynamic_cast<DagInit*>(*i);
244 if (!DAG) throw "SubRegClasses must contain DAGs";
245 DefInit *DAGOp = dynamic_cast<DefInit*>(DAG->getOperator());
246 Record *RCRec;
247 if (!DAGOp || !(RCRec = DAGOp->getDef())->isSubClassOf("RegisterClass"))
248 throw "Operator '" + DAG->getOperator()->getAsString() +
249 "' in SubRegClasses is not a RegisterClass";
250 // Iterate over args, all SubRegIndex instances.
251 for (DagInit::const_arg_iterator ai = DAG->arg_begin(), ae = DAG->arg_end();
252 ai != ae; ++ai) {
253 DefInit *Idx = dynamic_cast<DefInit*>(*ai);
254 Record *IdxRec;
255 if (!Idx || !(IdxRec = Idx->getDef())->isSubClassOf("SubRegIndex"))
256 throw "Argument '" + (*ai)->getAsString() +
257 "' in SubRegClasses is not a SubRegIndex";
258 if (!SubRegClasses.insert(std::make_pair(IdxRec, RCRec)).second)
259 throw "SubRegIndex '" + IdxRec->getName() + "' mentioned twice";
260 }
261 }
262
Nate Begeman6510b222005-12-01 04:51:06 +0000263 // Allow targets to override the size in bits of the RegisterClass.
264 unsigned Size = R->getValueAsInt("Size");
265
266 Namespace = R->getValueAsString("Namespace");
Owen Andersone50ed302009-08-10 22:56:29 +0000267 SpillSize = Size ? Size : EVT(VTs[0]).getSizeInBits();
Nate Begeman6510b222005-12-01 04:51:06 +0000268 SpillAlignment = R->getValueAsInt("Alignment");
Evan Chenga3ca3142007-09-19 01:35:01 +0000269 CopyCost = R->getValueAsInt("CopyCost");
Nate Begeman6510b222005-12-01 04:51:06 +0000270 MethodBodies = R->getValueAsCode("MethodBodies");
271 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000272}
273
274const std::string &CodeGenRegisterClass::getName() const {
275 return TheDef->getName();
276}
277
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000278void CodeGenTarget::ReadLegalValueTypes() const {
279 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
280 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000281 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
282 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000283
284 // Remove duplicates.
285 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
286 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
287 LegalValueTypes.end()),
288 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000289}
Chris Lattner056afef2004-08-21 04:05:00 +0000290
Chris Lattner175580c2004-08-14 22:50:53 +0000291
Chris Lattnerec352402004-08-01 05:04:00 +0000292void CodeGenTarget::ReadInstructions() const {
293 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000294 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000295 throw std::string("No 'Instruction' subclasses defined!");
296
Chris Lattneraa77d772006-01-27 01:45:06 +0000297 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000298 std::string InstFormatName =
299 getAsmWriter()->getValueAsString("InstFormatName");
300
301 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
302 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
Chris Lattnere14d2e22010-03-19 01:07:44 +0000303 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i], AsmStr);
Chris Lattner175580c2004-08-14 22:50:53 +0000304 }
Chris Lattnerec352402004-08-01 05:04:00 +0000305}
306
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000307static const CodeGenInstruction *
308GetInstByName(const char *Name,
Chris Lattnere14d2e22010-03-19 01:07:44 +0000309 const DenseMap<const Record*, CodeGenInstruction*> &Insts) {
310 const Record *Rec = Records.getDef(Name);
311
312 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
313 I = Insts.find(Rec);
314 if (Rec == 0 || I == Insts.end())
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000315 throw std::string("Could not find '") + Name + "' instruction!";
Chris Lattnere14d2e22010-03-19 01:07:44 +0000316 return I->second;
317}
318
319namespace {
320/// SortInstByName - Sorting predicate to sort instructions by name.
321///
322struct SortInstByName {
323 bool operator()(const CodeGenInstruction *Rec1,
324 const CodeGenInstruction *Rec2) const {
325 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
326 }
327};
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000328}
329
Chris Lattnerd6488672005-01-22 18:58:51 +0000330/// getInstructionsByEnumValue - Return all of the instructions defined by the
331/// target, ordered by their enum value.
Chris Lattner6a91b182010-03-19 01:00:55 +0000332void CodeGenTarget::ComputeInstrsByEnum() const {
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000333 // The ordering here must match the ordering in TargetOpcodes.h.
334 const char *const FixedInstrs[] = {
335 "PHI",
336 "INLINEASM",
Bill Wendling7431bea2010-07-16 22:20:36 +0000337 "PROLOG_LABEL",
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000338 "EH_LABEL",
339 "GC_LABEL",
340 "KILL",
341 "EXTRACT_SUBREG",
342 "INSERT_SUBREG",
343 "IMPLICIT_DEF",
344 "SUBREG_TO_REG",
345 "COPY_TO_REGCLASS",
346 "DBG_VALUE",
347 "REG_SEQUENCE",
Jakob Stoklund Olesena4e1ba52010-07-02 22:29:50 +0000348 "COPY",
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000349 0
350 };
Chris Lattnere14d2e22010-03-19 01:07:44 +0000351 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000352 for (const char *const *p = FixedInstrs; *p; ++p) {
353 const CodeGenInstruction *Instr = GetInstByName(*p, Insts);
354 assert(Instr && "Missing target independent instruction");
355 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
356 InstrsByEnum.push_back(Instr);
357 }
Chris Lattnere14d2e22010-03-19 01:07:44 +0000358 unsigned EndOfPredefines = InstrsByEnum.size();
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000359
Chris Lattnere14d2e22010-03-19 01:07:44 +0000360 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
361 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
362 const CodeGenInstruction *CGI = I->second;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000363 if (CGI->Namespace != "TargetOpcode")
Chris Lattner6a91b182010-03-19 01:00:55 +0000364 InstrsByEnum.push_back(CGI);
365 }
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000366
367 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
368
Chris Lattnere14d2e22010-03-19 01:07:44 +0000369 // All of the instructions are now in random order based on the map iteration.
370 // Sort them by name.
371 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(),
372 SortInstByName());
Chris Lattnerd6488672005-01-22 18:58:51 +0000373}
374
375
Misha Brukman35e83cc2004-10-14 05:50:43 +0000376/// isLittleEndianEncoding - Return whether this target encodes its instruction
377/// in little-endian format, i.e. bits laid out in the order [0..n]
378///
379bool CodeGenTarget::isLittleEndianEncoding() const {
380 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
381}
382
Evan Cheng0fc71982005-12-08 02:00:36 +0000383//===----------------------------------------------------------------------===//
384// ComplexPattern implementation
385//
386ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000387 Ty = ::getValueType(R->getValueAsDef("Ty"));
388 NumOperands = R->getValueAsInt("NumOperands");
389 SelectFunc = R->getValueAsString("SelectFunc");
390 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000391
392 // Parse the properties.
393 Properties = 0;
394 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
395 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
396 if (PropList[i]->getName() == "SDNPHasChain") {
397 Properties |= 1 << SDNPHasChain;
398 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
399 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerba7e7562008-01-10 07:59:24 +0000400 } else if (PropList[i]->getName() == "SDNPMayStore") {
401 Properties |= 1 << SDNPMayStore;
402 } else if (PropList[i]->getName() == "SDNPMayLoad") {
403 Properties |= 1 << SDNPMayLoad;
404 } else if (PropList[i]->getName() == "SDNPSideEffect") {
405 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000406 } else if (PropList[i]->getName() == "SDNPMemOperand") {
407 Properties |= 1 << SDNPMemOperand;
Chris Lattnere8cabf32010-03-19 05:07:09 +0000408 } else if (PropList[i]->getName() == "SDNPVariadic") {
409 Properties |= 1 << SDNPVariadic;
Evan Cheng94b30402006-10-11 21:02:01 +0000410 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000411 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
412 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000413 exit(1);
414 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000415}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000416
Chris Lattner43fbbc32006-03-24 19:49:31 +0000417//===----------------------------------------------------------------------===//
418// CodeGenIntrinsic Implementation
419//===----------------------------------------------------------------------===//
420
Dale Johannesen49de9822009-02-05 01:49:45 +0000421std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
422 bool TargetOnly) {
Chris Lattner43fbbc32006-03-24 19:49:31 +0000423 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000424
425 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000426
Dale Johannesen49de9822009-02-05 01:49:45 +0000427 for (unsigned i = 0, e = I.size(); i != e; ++i) {
428 bool isTarget = I[i]->getValueAsBit("isTarget");
429 if (isTarget == TargetOnly)
430 Result.push_back(CodeGenIntrinsic(I[i]));
431 }
Chris Lattner8850a1b2006-03-27 22:48:18 +0000432 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000433}
434
Dan Gohmanee4fa192008-04-03 00:02:49 +0000435CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000436 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000437 std::string DefName = R->getName();
Dan Gohman7365c092010-08-05 23:36:21 +0000438 ModRef = ReadWriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000439 isOverloaded = false;
Evan Cheng6bd95672008-06-16 20:29:38 +0000440 isCommutative = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000441
442 if (DefName.size() <= 4 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000443 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000444 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000445
Chris Lattner43fbbc32006-03-24 19:49:31 +0000446 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000447
Chris Lattner43fbbc32006-03-24 19:49:31 +0000448 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
449 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000450
451 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000452 Name = R->getValueAsString("LLVMName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000453
Chris Lattner43fbbc32006-03-24 19:49:31 +0000454 if (Name == "") {
455 // If an explicit name isn't specified, derive one from the DefName.
456 Name = "llvm.";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000457
Chris Lattner43fbbc32006-03-24 19:49:31 +0000458 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000459 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner43fbbc32006-03-24 19:49:31 +0000460 } else {
461 // Verify it starts with "llvm.".
462 if (Name.size() <= 5 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000463 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000464 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
465 }
466
467 // If TargetPrefix is specified, make sure that Name starts with
468 // "llvm.<targetprefix>.".
469 if (!TargetPrefix.empty()) {
470 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000471 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
472 != (TargetPrefix + "."))
473 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Chris Lattner43fbbc32006-03-24 19:49:31 +0000474 TargetPrefix + ".'!";
475 }
476
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000477 // Parse the list of return types.
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000479 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000480 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000481 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000482 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000484 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsond305e702009-04-16 21:51:05 +0000485 unsigned MatchTy = TyEl->getValueAsInt("Number");
486 assert(MatchTy < OverloadedVTs.size() &&
487 "Invalid matching number!");
488 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000489 // It only makes sense to use the extended and truncated vector element
490 // variants with iAny types; otherwise, if the intrinsic is not
491 // overloaded, all the types can be specified directly.
492 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
493 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000495 "Expected iAny or vAny type");
Bob Wilsond305e702009-04-16 21:51:05 +0000496 } else {
Bob Wilsonbc039792009-01-07 00:09:01 +0000497 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsond305e702009-04-16 21:51:05 +0000498 }
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000499 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000500 OverloadedVTs.push_back(VT);
Chris Lattner9972aff2010-03-23 23:46:27 +0000501 isOverloaded = true;
Bob Wilsond305e702009-04-16 21:51:05 +0000502 }
Chris Lattner9972aff2010-03-23 23:46:27 +0000503
504 // Reject invalid types.
505 if (VT == MVT::isVoid)
506 throw "Intrinsic '" + DefName + " has void in result type list!";
Chris Lattner93dc92e2010-03-22 20:56:36 +0000507
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000508 IS.RetVTs.push_back(VT);
509 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000510 }
Chris Lattner93dc92e2010-03-22 20:56:36 +0000511
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000512 // Parse the list of parameter types.
513 TypeList = R->getValueAsListInit("ParamTypes");
514 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
515 Record *TyEl = TypeList->getElementAsRecord(i);
516 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000518 if (TyEl->isSubClassOf("LLVMMatchType")) {
519 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsond305e702009-04-16 21:51:05 +0000520 assert(MatchTy < OverloadedVTs.size() &&
521 "Invalid matching number!");
522 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000523 // It only makes sense to use the extended and truncated vector element
524 // variants with iAny types; otherwise, if the intrinsic is not
525 // overloaded, all the types can be specified directly.
526 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
527 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000529 "Expected iAny or vAny type");
Bob Wilsonbc039792009-01-07 00:09:01 +0000530 } else
531 VT = getValueType(TyEl->getValueAsDef("VT"));
Chris Lattner9972aff2010-03-23 23:46:27 +0000532
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000533 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000534 OverloadedVTs.push_back(VT);
Chris Lattner9972aff2010-03-23 23:46:27 +0000535 isOverloaded = true;
Bob Wilsond305e702009-04-16 21:51:05 +0000536 }
Chris Lattner9972aff2010-03-23 23:46:27 +0000537
538 // Reject invalid types.
539 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
540 throw "Intrinsic '" + DefName + " has void in result type list!";
541
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000542 IS.ParamVTs.push_back(VT);
543 IS.ParamTypeDefs.push_back(TyEl);
544 }
545
Chris Lattner43fbbc32006-03-24 19:49:31 +0000546 // Parse the intrinsic properties.
547 ListInit *PropList = R->getValueAsListInit("Properties");
548 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000549 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000550 assert(Property->isSubClassOf("IntrinsicProperty") &&
551 "Expected a property!");
552
Chris Lattner4b2362e2006-04-10 22:02:59 +0000553 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000554 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000555 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000556 ModRef = ReadArgMem;
557 else if (Property->getName() == "IntrReadMem")
558 ModRef = ReadMem;
Dan Gohman7365c092010-08-05 23:36:21 +0000559 else if (Property->getName() == "IntrReadWriteArgMem")
560 ModRef = ReadWriteArgMem;
Evan Cheng6bd95672008-06-16 20:29:38 +0000561 else if (Property->getName() == "Commutative")
562 isCommutative = true;
Chris Lattnera62c3022009-01-12 01:12:03 +0000563 else if (Property->isSubClassOf("NoCapture")) {
564 unsigned ArgNo = Property->getValueAsInt("ArgNo");
565 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
566 } else
Chris Lattner43fbbc32006-03-24 19:49:31 +0000567 assert(0 && "Unknown property!");
568 }
569}