blob: 3a3b0aa3803de919a7f9c9314db4c6d5aed9ad3f [file] [log] [blame]
Chris Lattnerea2d52d2008-01-06 01:35:39 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswelld3032032003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswelld3032032003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
Chris Lattnerea2d52d2008-01-06 01:35:39 +000010// This class wraps target description classes used by the various code
Chris Lattnerf5bd1b72003-10-05 19:27:59 +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 Lattnerfce96032004-08-01 04:04:35 +000017#include "CodeGenTarget.h"
Chris Lattner2c581412006-03-24 19:49:31 +000018#include "CodeGenIntrinsics.h"
Andrew Trick87255e32012-07-07 04:00:00 +000019#include "CodeGenSchedule.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000020#include "llvm/TableGen/Record.h"
Chris Lattner91c538f2004-10-03 19:34:31 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner4a5f7be2010-03-27 20:32:26 +000022#include "llvm/ADT/STLExtras.h"
Chris Lattner91c538f2004-10-03 19:34:31 +000023#include "llvm/Support/CommandLine.h"
Chris Lattner90c5b9c2005-10-14 03:54:49 +000024#include <algorithm>
Chris Lattner68478662004-08-01 03:55:39 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner91c538f2004-10-03 19:34:31 +000027static cl::opt<unsigned>
Daniel Dunbar00331992009-07-29 00:02:19 +000028AsmParserNum("asmparsernum", cl::init(0),
29 cl::desc("Make -gen-asm-parser emit assembly parser #N"));
30
31static cl::opt<unsigned>
Chris Lattner91c538f2004-10-03 19:34:31 +000032AsmWriterNum("asmwriternum", cl::init(0),
33 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
34
Owen Anderson9f944592009-08-11 20:47:22 +000035/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands13237ac2008-06-06 12:08:01 +000036/// record corresponds to.
Owen Anderson9f944592009-08-11 20:47:22 +000037MVT::SimpleValueType llvm::getValueType(Record *Rec) {
38 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000039}
40
Owen Anderson9f944592009-08-11 20:47:22 +000041std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000042 switch (T) {
Owen Anderson9f944592009-08-11 20:47:22 +000043 case MVT::Other: return "UNKNOWN";
44 case MVT::iPTR: return "TLI.getPointerTy()";
45 case MVT::iPTRAny: return "TLI.getPointerTy()";
Bob Wilsonbf8e4c12009-07-10 22:25:24 +000046 default: return getEnumName(T);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000047 }
48}
49
Owen Anderson9f944592009-08-11 20:47:22 +000050std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000051 switch (T) {
Bill Wendling02b701f2010-09-07 18:49:14 +000052 case MVT::Other: return "MVT::Other";
53 case MVT::i1: return "MVT::i1";
54 case MVT::i8: return "MVT::i8";
55 case MVT::i16: return "MVT::i16";
56 case MVT::i32: return "MVT::i32";
57 case MVT::i64: return "MVT::i64";
58 case MVT::i128: return "MVT::i128";
59 case MVT::iAny: return "MVT::iAny";
60 case MVT::fAny: return "MVT::fAny";
61 case MVT::vAny: return "MVT::vAny";
Dan Gohman94580ab2011-12-20 00:02:33 +000062 case MVT::f16: return "MVT::f16";
Bill Wendling02b701f2010-09-07 18:49:14 +000063 case MVT::f32: return "MVT::f32";
64 case MVT::f64: return "MVT::f64";
65 case MVT::f80: return "MVT::f80";
66 case MVT::f128: return "MVT::f128";
Owen Anderson9f944592009-08-11 20:47:22 +000067 case MVT::ppcf128: return "MVT::ppcf128";
Bill Wendling35380212010-09-07 20:03:56 +000068 case MVT::x86mmx: return "MVT::x86mmx";
Chris Lattner3e5fbd72010-12-21 02:38:05 +000069 case MVT::Glue: return "MVT::Glue";
Bill Wendling02b701f2010-09-07 18:49:14 +000070 case MVT::isVoid: return "MVT::isVoid";
Micah Villmow75e29c42012-09-19 22:47:07 +000071 case MVT::v2i1: return "MVT::v2i1";
72 case MVT::v4i1: return "MVT::v4i1";
73 case MVT::v8i1: return "MVT::v8i1";
74 case MVT::v16i1: return "MVT::v16i1";
Bill Wendling02b701f2010-09-07 18:49:14 +000075 case MVT::v2i8: return "MVT::v2i8";
76 case MVT::v4i8: return "MVT::v4i8";
77 case MVT::v8i8: return "MVT::v8i8";
78 case MVT::v16i8: return "MVT::v16i8";
79 case MVT::v32i8: return "MVT::v32i8";
Micah Villmow75e29c42012-09-19 22:47:07 +000080 case MVT::v1i16: return "MVT::v1i16";
Bill Wendling02b701f2010-09-07 18:49:14 +000081 case MVT::v2i16: return "MVT::v2i16";
82 case MVT::v4i16: return "MVT::v4i16";
83 case MVT::v8i16: return "MVT::v8i16";
84 case MVT::v16i16: return "MVT::v16i16";
Micah Villmow75e29c42012-09-19 22:47:07 +000085 case MVT::v1i32: return "MVT::v1i32";
Bill Wendling02b701f2010-09-07 18:49:14 +000086 case MVT::v2i32: return "MVT::v2i32";
87 case MVT::v4i32: return "MVT::v4i32";
88 case MVT::v8i32: return "MVT::v8i32";
Micah Villmow75e29c42012-09-19 22:47:07 +000089 case MVT::v16i32: return "MVT::v16i32";
Bill Wendling02b701f2010-09-07 18:49:14 +000090 case MVT::v1i64: return "MVT::v1i64";
91 case MVT::v2i64: return "MVT::v2i64";
92 case MVT::v4i64: return "MVT::v4i64";
93 case MVT::v8i64: return "MVT::v8i64";
Micah Villmow75e29c42012-09-19 22:47:07 +000094 case MVT::v16i64: return "MVT::v16i64";
Pete Cooper9bcb7212012-01-12 23:14:13 +000095 case MVT::v2f16: return "MVT::v2f16";
Bill Wendling02b701f2010-09-07 18:49:14 +000096 case MVT::v2f32: return "MVT::v2f32";
97 case MVT::v4f32: return "MVT::v4f32";
98 case MVT::v8f32: return "MVT::v8f32";
99 case MVT::v2f64: return "MVT::v2f64";
100 case MVT::v4f64: return "MVT::v4f64";
Owen Anderson9f944592009-08-11 20:47:22 +0000101 case MVT::Metadata: return "MVT::Metadata";
Bill Wendling02b701f2010-09-07 18:49:14 +0000102 case MVT::iPTR: return "MVT::iPTR";
Owen Anderson9f944592009-08-11 20:47:22 +0000103 case MVT::iPTRAny: return "MVT::iPTRAny";
Owen Andersonca2f78a2011-11-16 01:02:57 +0000104 case MVT::Untyped: return "MVT::Untyped";
Craig Topperc4965bc2012-02-05 07:21:30 +0000105 default: llvm_unreachable("ILLEGAL VALUE TYPE!");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000106 }
107}
108
Chris Lattner8cab0212008-01-05 22:25:12 +0000109/// getQualifiedName - Return the name of the specified record, with a
110/// namespace qualifier if the record contains one.
111///
112std::string llvm::getQualifiedName(const Record *R) {
Jakob Stoklund Olesenc8c4ded2011-05-07 21:22:39 +0000113 std::string Namespace;
114 if (R->getValue("Namespace"))
115 Namespace = R->getValueAsString("Namespace");
Chris Lattner8cab0212008-01-05 22:25:12 +0000116 if (Namespace.empty()) return R->getName();
117 return Namespace + "::" + R->getName();
118}
119
120
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000121/// getTarget - Return the current instance of the Target class.
122///
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +0000123CodeGenTarget::CodeGenTarget(RecordKeeper &records)
Andrew Trick87255e32012-07-07 04:00:00 +0000124 : Records(records), RegBank(0), SchedModels(0) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000125 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanaff10d02004-06-04 14:59:42 +0000126 if (Targets.size() == 0)
Misha Brukman650ba8e2005-04-22 00:00:37 +0000127 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000128 if (Targets.size() != 1)
129 throw std::string("ERROR: Multiple subclasses of Target defined!");
130 TargetRec = Targets[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000131}
132
Andrew Trick87255e32012-07-07 04:00:00 +0000133CodeGenTarget::~CodeGenTarget() {
134 delete RegBank;
135 delete SchedModels;
136}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000137
138const std::string &CodeGenTarget::getName() const {
139 return TargetRec->getName();
140}
141
Dan Gohman3e2225d2008-08-20 21:45:57 +0000142std::string CodeGenTarget::getInstNamespace() const {
Dan Gohman3e2225d2008-08-20 21:45:57 +0000143 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
Chris Lattner4763dbe2010-03-19 01:00:55 +0000144 // Make sure not to pick up "TargetOpcode" by accidentally getting
Dan Gohman3e2225d2008-08-20 21:45:57 +0000145 // the namespace off the PHI instruction or something.
Chris Lattner4763dbe2010-03-19 01:00:55 +0000146 if ((*i)->Namespace != "TargetOpcode")
147 return (*i)->Namespace;
Dan Gohman3e2225d2008-08-20 21:45:57 +0000148 }
149
Chris Lattner4763dbe2010-03-19 01:00:55 +0000150 return "";
Dan Gohman3e2225d2008-08-20 21:45:57 +0000151}
152
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000153Record *CodeGenTarget::getInstructionSet() const {
154 return TargetRec->getValueAsDef("InstructionSet");
155}
Brian Gaeke960707c2003-11-11 22:41:34 +0000156
Chris Lattner9aec14b2010-03-19 00:07:20 +0000157
Daniel Dunbar00331992009-07-29 00:02:19 +0000158/// getAsmParser - Return the AssemblyParser definition for this target.
159///
160Record *CodeGenTarget::getAsmParser() const {
161 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
162 if (AsmParserNum >= LI.size())
163 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
164 return LI[AsmParserNum];
165}
166
Devang Patel85d684a2012-01-09 19:13:28 +0000167/// getAsmParserVariant - Return the AssmblyParserVariant definition for
168/// this target.
169///
170Record *CodeGenTarget::getAsmParserVariant(unsigned i) const {
Andrew Trick91118a62012-07-07 03:59:51 +0000171 std::vector<Record*> LI =
Devang Patel85d684a2012-01-09 19:13:28 +0000172 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
173 if (i >= LI.size())
174 throw "Target does not have an AsmParserVariant #" + utostr(i) + "!";
175 return LI[i];
176}
177
Andrew Trick91118a62012-07-07 03:59:51 +0000178/// getAsmParserVariantCount - Return the AssmblyParserVariant definition
Devang Patel85d684a2012-01-09 19:13:28 +0000179/// available for this target.
180///
181unsigned CodeGenTarget::getAsmParserVariantCount() const {
Andrew Trick91118a62012-07-07 03:59:51 +0000182 std::vector<Record*> LI =
Devang Patel85d684a2012-01-09 19:13:28 +0000183 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
184 return LI.size();
185}
186
Chris Lattner6ffa5012004-08-14 22:50:53 +0000187/// getAsmWriter - Return the AssemblyWriter definition for this target.
188///
189Record *CodeGenTarget::getAsmWriter() const {
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000190 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
191 if (AsmWriterNum >= LI.size())
Chris Lattner91c538f2004-10-03 19:34:31 +0000192 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000193 return LI[AsmWriterNum];
Chris Lattner6ffa5012004-08-14 22:50:53 +0000194}
195
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +0000196CodeGenRegBank &CodeGenTarget::getRegBank() const {
197 if (!RegBank)
198 RegBank = new CodeGenRegBank(Records);
199 return *RegBank;
200}
201
Owen Andersona84be6c2011-06-27 21:06:21 +0000202void CodeGenTarget::ReadRegAltNameIndices() const {
203 RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex");
204 std::sort(RegAltNameIndices.begin(), RegAltNameIndices.end(), LessRecord());
205}
206
Chris Lattner77d3ead2010-11-02 18:10:06 +0000207/// getRegisterByName - If there is a register with the specific AsmName,
208/// return it.
209const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
Owen Andersonccd682c2012-09-11 23:32:17 +0000210 const StringMap<CodeGenRegister*> &Regs = getRegBank().getRegistersByName();
211 StringMap<CodeGenRegister*>::const_iterator I = Regs.find(Name);
212 if (I == Regs.end())
213 return 0;
214 return I->second;
Chris Lattner77d3ead2010-11-02 18:10:06 +0000215}
216
Chris Lattnercabe0372010-03-15 06:00:16 +0000217std::vector<MVT::SimpleValueType> CodeGenTarget::
218getRegisterVTs(Record *R) const {
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000219 const CodeGenRegister *Reg = getRegBank().getReg(R);
Chris Lattnercabe0372010-03-15 06:00:16 +0000220 std::vector<MVT::SimpleValueType> Result;
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000221 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
Evan Chengd985d662006-05-16 07:05:30 +0000222 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000223 const CodeGenRegisterClass &RC = *RCs[i];
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000224 if (RC.contains(Reg)) {
225 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
226 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Evan Chengd985d662006-05-16 07:05:30 +0000227 }
228 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000229
Chris Lattner4a5f7be2010-03-27 20:32:26 +0000230 // Remove duplicates.
231 array_pod_sort(Result.begin(), Result.end());
232 Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
Evan Chengd985d662006-05-16 07:05:30 +0000233 return Result;
234}
235
236
Chris Lattnercee994b2005-09-08 21:43:21 +0000237void CodeGenTarget::ReadLegalValueTypes() const {
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000238 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
Chris Lattnercee994b2005-09-08 21:43:21 +0000239 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000240 for (unsigned ri = 0, re = RCs[i]->VTs.size(); ri != re; ++ri)
241 LegalValueTypes.push_back(RCs[i]->VTs[ri]);
Jim Grosbachf910bf22011-03-11 01:27:24 +0000242
Chris Lattner90c5b9c2005-10-14 03:54:49 +0000243 // Remove duplicates.
244 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
245 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
246 LegalValueTypes.end()),
247 LegalValueTypes.end());
Chris Lattnercee994b2005-09-08 21:43:21 +0000248}
Chris Lattner2a86fab2004-08-21 04:05:00 +0000249
Andrew Trick87255e32012-07-07 04:00:00 +0000250CodeGenSchedModels &CodeGenTarget::getSchedModels() const {
251 if (!SchedModels)
252 SchedModels = new CodeGenSchedModels(Records, *this);
253 return *SchedModels;
254}
Chris Lattner6ffa5012004-08-14 22:50:53 +0000255
Chris Lattnerc860eca2004-08-01 05:04:00 +0000256void CodeGenTarget::ReadInstructions() const {
257 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattner543fe4b2006-01-27 01:45:06 +0000258 if (Insts.size() <= 2)
Chris Lattnerc860eca2004-08-01 05:04:00 +0000259 throw std::string("No 'Instruction' subclasses defined!");
260
Chris Lattner543fe4b2006-01-27 01:45:06 +0000261 // Parse the instructions defined in the .td file.
Chris Lattnera3977162010-11-01 02:15:23 +0000262 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
263 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i]);
Chris Lattnerc860eca2004-08-01 05:04:00 +0000264}
265
Chris Lattnerc9ae94c2010-03-19 00:23:20 +0000266static const CodeGenInstruction *
267GetInstByName(const char *Name,
Jim Grosbachf910bf22011-03-11 01:27:24 +0000268 const DenseMap<const Record*, CodeGenInstruction*> &Insts,
Chris Lattner77d369c2010-12-13 00:23:57 +0000269 RecordKeeper &Records) {
Chris Lattner1802b172010-03-19 01:07:44 +0000270 const Record *Rec = Records.getDef(Name);
Jim Grosbachf910bf22011-03-11 01:27:24 +0000271
Chris Lattner1802b172010-03-19 01:07:44 +0000272 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
273 I = Insts.find(Rec);
274 if (Rec == 0 || I == Insts.end())
Chris Lattnerc9ae94c2010-03-19 00:23:20 +0000275 throw std::string("Could not find '") + Name + "' instruction!";
Chris Lattner1802b172010-03-19 01:07:44 +0000276 return I->second;
277}
278
279namespace {
280/// SortInstByName - Sorting predicate to sort instructions by name.
281///
282struct SortInstByName {
283 bool operator()(const CodeGenInstruction *Rec1,
284 const CodeGenInstruction *Rec2) const {
285 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
286 }
287};
Chris Lattnerc9ae94c2010-03-19 00:23:20 +0000288}
289
Chris Lattner945e8652005-01-22 18:58:51 +0000290/// getInstructionsByEnumValue - Return all of the instructions defined by the
291/// target, ordered by their enum value.
Chris Lattner4763dbe2010-03-19 01:00:55 +0000292void CodeGenTarget::ComputeInstrsByEnum() const {
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000293 // The ordering here must match the ordering in TargetOpcodes.h.
294 const char *const FixedInstrs[] = {
295 "PHI",
296 "INLINEASM",
Bill Wendling499f7972010-07-16 22:20:36 +0000297 "PROLOG_LABEL",
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000298 "EH_LABEL",
299 "GC_LABEL",
300 "KILL",
301 "EXTRACT_SUBREG",
302 "INSERT_SUBREG",
303 "IMPLICIT_DEF",
304 "SUBREG_TO_REG",
305 "COPY_TO_REGCLASS",
306 "DBG_VALUE",
307 "REG_SEQUENCE",
Jakob Stoklund Olesen676a15b2010-07-02 22:29:50 +0000308 "COPY",
Evan Cheng2a81dd42011-12-06 22:12:01 +0000309 "BUNDLE",
Nadav Rotem7c277da2012-09-06 09:17:37 +0000310 "LIFETIME_START",
311 "LIFETIME_END",
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000312 0
313 };
Chris Lattner1802b172010-03-19 01:07:44 +0000314 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000315 for (const char *const *p = FixedInstrs; *p; ++p) {
Chris Lattner77d369c2010-12-13 00:23:57 +0000316 const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000317 assert(Instr && "Missing target independent instruction");
318 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
319 InstrsByEnum.push_back(Instr);
320 }
Chris Lattner1802b172010-03-19 01:07:44 +0000321 unsigned EndOfPredefines = InstrsByEnum.size();
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000322
Chris Lattner1802b172010-03-19 01:07:44 +0000323 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
324 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
325 const CodeGenInstruction *CGI = I->second;
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000326 if (CGI->Namespace != "TargetOpcode")
Chris Lattner4763dbe2010-03-19 01:00:55 +0000327 InstrsByEnum.push_back(CGI);
328 }
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000329
330 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
331
Chris Lattner1802b172010-03-19 01:07:44 +0000332 // All of the instructions are now in random order based on the map iteration.
333 // Sort them by name.
334 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(),
335 SortInstByName());
Chris Lattner945e8652005-01-22 18:58:51 +0000336}
337
338
Misha Brukman243ded52004-10-14 05:50:43 +0000339/// isLittleEndianEncoding - Return whether this target encodes its instruction
340/// in little-endian format, i.e. bits laid out in the order [0..n]
341///
342bool CodeGenTarget::isLittleEndianEncoding() const {
343 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
344}
345
Jakob Stoklund Olesen9dc03bb2012-08-23 19:34:41 +0000346/// guessInstructionProperties - Return true if it's OK to guess instruction
347/// properties instead of raising an error.
348///
349/// This is configurable as a temporary migration aid. It will eventually be
350/// permanently false.
351bool CodeGenTarget::guessInstructionProperties() const {
352 return getInstructionSet()->getValueAsBit("guessInstructionProperties");
353}
354
Evan Cheng9b9567b2005-12-08 02:00:36 +0000355//===----------------------------------------------------------------------===//
356// ComplexPattern implementation
357//
358ComplexPattern::ComplexPattern(Record *R) {
Evan Chengc9a62002005-12-08 02:14:08 +0000359 Ty = ::getValueType(R->getValueAsDef("Ty"));
360 NumOperands = R->getValueAsInt("NumOperands");
361 SelectFunc = R->getValueAsString("SelectFunc");
362 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng2022c792006-10-11 21:02:01 +0000363
364 // Parse the properties.
365 Properties = 0;
366 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
367 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
368 if (PropList[i]->getName() == "SDNPHasChain") {
369 Properties |= 1 << SDNPHasChain;
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000370 } else if (PropList[i]->getName() == "SDNPOptInGlue") {
371 Properties |= 1 << SDNPOptInGlue;
Chris Lattner317332f2008-01-10 07:59:24 +0000372 } else if (PropList[i]->getName() == "SDNPMayStore") {
373 Properties |= 1 << SDNPMayStore;
374 } else if (PropList[i]->getName() == "SDNPMayLoad") {
375 Properties |= 1 << SDNPMayLoad;
376 } else if (PropList[i]->getName() == "SDNPSideEffect") {
377 Properties |= 1 << SDNPSideEffect;
Mon P Wang6a490372008-06-25 08:15:39 +0000378 } else if (PropList[i]->getName() == "SDNPMemOperand") {
379 Properties |= 1 << SDNPMemOperand;
Chris Lattner83aeaab2010-03-19 05:07:09 +0000380 } else if (PropList[i]->getName() == "SDNPVariadic") {
381 Properties |= 1 << SDNPVariadic;
Chris Lattner0e023ea2010-09-21 20:31:19 +0000382 } else if (PropList[i]->getName() == "SDNPWantRoot") {
383 Properties |= 1 << SDNPWantRoot;
384 } else if (PropList[i]->getName() == "SDNPWantParent") {
385 Properties |= 1 << SDNPWantParent;
Evan Cheng2022c792006-10-11 21:02:01 +0000386 } else {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000387 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
388 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng2022c792006-10-11 21:02:01 +0000389 exit(1);
390 }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000391}
Evan Chengc9a62002005-12-08 02:14:08 +0000392
Chris Lattner2c581412006-03-24 19:49:31 +0000393//===----------------------------------------------------------------------===//
394// CodeGenIntrinsic Implementation
395//===----------------------------------------------------------------------===//
396
Dale Johannesenb842d522009-02-05 01:49:45 +0000397std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
398 bool TargetOnly) {
Chris Lattner2c581412006-03-24 19:49:31 +0000399 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000400
Chris Lattnerc92f6882006-03-27 22:48:18 +0000401 std::vector<CodeGenIntrinsic> Result;
Chris Lattner8a2ae8b72006-03-28 00:15:00 +0000402
Dale Johannesenb842d522009-02-05 01:49:45 +0000403 for (unsigned i = 0, e = I.size(); i != e; ++i) {
404 bool isTarget = I[i]->getValueAsBit("isTarget");
405 if (isTarget == TargetOnly)
406 Result.push_back(CodeGenIntrinsic(I[i]));
407 }
Chris Lattnerc92f6882006-03-27 22:48:18 +0000408 return Result;
Chris Lattner2c581412006-03-24 19:49:31 +0000409}
410
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000411CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattnerbbba8232006-03-24 20:25:01 +0000412 TheDef = R;
Chris Lattner2c581412006-03-24 19:49:31 +0000413 std::string DefName = R->getName();
Dan Gohmanddb2d652010-08-05 23:36:21 +0000414 ModRef = ReadWriteMem;
Reid Spencere67d0c22007-04-01 07:20:02 +0000415 isOverloaded = false;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000416 isCommutative = false;
John McCall375dcc92011-05-28 06:31:34 +0000417 canThrow = false;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000418 isNoReturn = false;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000419
420 if (DefName.size() <= 4 ||
Bill Wendling91821472008-11-13 09:08:33 +0000421 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Chris Lattner2c581412006-03-24 19:49:31 +0000422 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendling91821472008-11-13 09:08:33 +0000423
Chris Lattner2c581412006-03-24 19:49:31 +0000424 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendling91821472008-11-13 09:08:33 +0000425
Chris Lattner2c581412006-03-24 19:49:31 +0000426 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
427 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendling91821472008-11-13 09:08:33 +0000428
429 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner2c581412006-03-24 19:49:31 +0000430 Name = R->getValueAsString("LLVMName");
Bill Wendling91821472008-11-13 09:08:33 +0000431
Chris Lattner2c581412006-03-24 19:49:31 +0000432 if (Name == "") {
433 // If an explicit name isn't specified, derive one from the DefName.
434 Name = "llvm.";
Bill Wendling91821472008-11-13 09:08:33 +0000435
Chris Lattner2c581412006-03-24 19:49:31 +0000436 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendling91821472008-11-13 09:08:33 +0000437 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner2c581412006-03-24 19:49:31 +0000438 } else {
439 // Verify it starts with "llvm.".
Jim Grosbachf910bf22011-03-11 01:27:24 +0000440 if (Name.size() <= 5 ||
Bill Wendling91821472008-11-13 09:08:33 +0000441 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Chris Lattner2c581412006-03-24 19:49:31 +0000442 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
443 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000444
Chris Lattner2c581412006-03-24 19:49:31 +0000445 // If TargetPrefix is specified, make sure that Name starts with
446 // "llvm.<targetprefix>.".
447 if (!TargetPrefix.empty()) {
448 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendling91821472008-11-13 09:08:33 +0000449 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
450 != (TargetPrefix + "."))
451 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Chris Lattner2c581412006-03-24 19:49:31 +0000452 TargetPrefix + ".'!";
453 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000454
Bill Wendling91821472008-11-13 09:08:33 +0000455 // Parse the list of return types.
Owen Anderson9f944592009-08-11 20:47:22 +0000456 std::vector<MVT::SimpleValueType> OverloadedVTs;
David Greeneaf8ee2c2011-07-29 22:43:06 +0000457 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner2c581412006-03-24 19:49:31 +0000458 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattnercbebe462007-02-27 22:08:27 +0000459 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner2c581412006-03-24 19:49:31 +0000460 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson9f944592009-08-11 20:47:22 +0000461 MVT::SimpleValueType VT;
Bob Wilsonf76486a2009-01-07 00:09:01 +0000462 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000463 unsigned MatchTy = TyEl->getValueAsInt("Number");
464 assert(MatchTy < OverloadedVTs.size() &&
465 "Invalid matching number!");
466 VT = OverloadedVTs[MatchTy];
Bob Wilsonf76486a2009-01-07 00:09:01 +0000467 // It only makes sense to use the extended and truncated vector element
468 // variants with iAny types; otherwise, if the intrinsic is not
469 // overloaded, all the types can be specified directly.
470 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
471 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson9f944592009-08-11 20:47:22 +0000472 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson781797f2009-08-11 05:03:38 +0000473 "Expected iAny or vAny type");
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000474 } else {
Bob Wilsonf76486a2009-01-07 00:09:01 +0000475 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000476 }
Bob Wilson2cd5da82009-08-11 01:14:02 +0000477 if (EVT(VT).isOverloaded()) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000478 OverloadedVTs.push_back(VT);
Chris Lattnerb424faa2010-03-23 23:46:27 +0000479 isOverloaded = true;
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000480 }
Chris Lattnerb424faa2010-03-23 23:46:27 +0000481
482 // Reject invalid types.
483 if (VT == MVT::isVoid)
484 throw "Intrinsic '" + DefName + " has void in result type list!";
Jim Grosbachf910bf22011-03-11 01:27:24 +0000485
Bill Wendling91821472008-11-13 09:08:33 +0000486 IS.RetVTs.push_back(VT);
487 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner2c581412006-03-24 19:49:31 +0000488 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000489
Bill Wendling91821472008-11-13 09:08:33 +0000490 // Parse the list of parameter types.
491 TypeList = R->getValueAsListInit("ParamTypes");
492 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
493 Record *TyEl = TypeList->getElementAsRecord(i);
494 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson9f944592009-08-11 20:47:22 +0000495 MVT::SimpleValueType VT;
Bob Wilsonf76486a2009-01-07 00:09:01 +0000496 if (TyEl->isSubClassOf("LLVMMatchType")) {
497 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000498 assert(MatchTy < OverloadedVTs.size() &&
499 "Invalid matching number!");
500 VT = OverloadedVTs[MatchTy];
Bob Wilsonf76486a2009-01-07 00:09:01 +0000501 // It only makes sense to use the extended and truncated vector element
502 // variants with iAny types; otherwise, if the intrinsic is not
503 // overloaded, all the types can be specified directly.
504 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
505 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson9f944592009-08-11 20:47:22 +0000506 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson781797f2009-08-11 05:03:38 +0000507 "Expected iAny or vAny type");
Bob Wilsonf76486a2009-01-07 00:09:01 +0000508 } else
509 VT = getValueType(TyEl->getValueAsDef("VT"));
Jim Grosbachf910bf22011-03-11 01:27:24 +0000510
Bob Wilson2cd5da82009-08-11 01:14:02 +0000511 if (EVT(VT).isOverloaded()) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000512 OverloadedVTs.push_back(VT);
Chris Lattnerb424faa2010-03-23 23:46:27 +0000513 isOverloaded = true;
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000514 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000515
Chris Lattnerb424faa2010-03-23 23:46:27 +0000516 // Reject invalid types.
517 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
518 throw "Intrinsic '" + DefName + " has void in result type list!";
Jim Grosbachf910bf22011-03-11 01:27:24 +0000519
Bill Wendling91821472008-11-13 09:08:33 +0000520 IS.ParamVTs.push_back(VT);
521 IS.ParamTypeDefs.push_back(TyEl);
522 }
523
Chris Lattner2c581412006-03-24 19:49:31 +0000524 // Parse the intrinsic properties.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000525 ListInit *PropList = R->getValueAsListInit("Properties");
Chris Lattner2c581412006-03-24 19:49:31 +0000526 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattnercbebe462007-02-27 22:08:27 +0000527 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner2c581412006-03-24 19:49:31 +0000528 assert(Property->isSubClassOf("IntrinsicProperty") &&
529 "Expected a property!");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000530
Chris Lattner989b16e42006-04-10 22:02:59 +0000531 if (Property->getName() == "IntrNoMem")
Chris Lattner2c581412006-03-24 19:49:31 +0000532 ModRef = NoMem;
Chris Lattner989b16e42006-04-10 22:02:59 +0000533 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner2c581412006-03-24 19:49:31 +0000534 ModRef = ReadArgMem;
535 else if (Property->getName() == "IntrReadMem")
536 ModRef = ReadMem;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000537 else if (Property->getName() == "IntrReadWriteArgMem")
538 ModRef = ReadWriteArgMem;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000539 else if (Property->getName() == "Commutative")
540 isCommutative = true;
John McCall375dcc92011-05-28 06:31:34 +0000541 else if (Property->getName() == "Throws")
542 canThrow = true;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000543 else if (Property->getName() == "IntrNoReturn")
544 isNoReturn = true;
Chris Lattner9a3113a2009-01-12 01:12:03 +0000545 else if (Property->isSubClassOf("NoCapture")) {
546 unsigned ArgNo = Property->getValueAsInt("ArgNo");
547 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
548 } else
Craig Topperc4965bc2012-02-05 07:21:30 +0000549 llvm_unreachable("Unknown property!");
Chris Lattner2c581412006-03-24 19:49:31 +0000550 }
John McCall375dcc92011-05-28 06:31:34 +0000551
552 // Sort the argument attributes for later benefit.
553 std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end());
Chris Lattner2c581412006-03-24 19:49:31 +0000554}