blob: b8d5e2d30f6741355ce973f7042c9fa020aed57d [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
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000013// classes abort on error conditions.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000014//
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"
Chandler Carruth91d19d82012-12-04 10:37:14 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/Support/CommandLine.h"
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000023#include "llvm/TableGen/Error.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000024#include "llvm/TableGen/Record.h"
Chris Lattner90c5b9c2005-10-14 03:54:49 +000025#include <algorithm>
Chris Lattner68478662004-08-01 03:55:39 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner91c538f2004-10-03 19:34:31 +000028static cl::opt<unsigned>
Daniel Dunbar00331992009-07-29 00:02:19 +000029AsmParserNum("asmparsernum", cl::init(0),
30 cl::desc("Make -gen-asm-parser emit assembly parser #N"));
31
32static cl::opt<unsigned>
Chris Lattner91c538f2004-10-03 19:34:31 +000033AsmWriterNum("asmwriternum", cl::init(0),
34 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
35
Owen Anderson9f944592009-08-11 20:47:22 +000036/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands13237ac2008-06-06 12:08:01 +000037/// record corresponds to.
Owen Anderson9f944592009-08-11 20:47:22 +000038MVT::SimpleValueType llvm::getValueType(Record *Rec) {
39 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000040}
41
Owen Anderson9f944592009-08-11 20:47:22 +000042std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000043 switch (T) {
Owen Anderson9f944592009-08-11 20:47:22 +000044 case MVT::Other: return "UNKNOWN";
45 case MVT::iPTR: return "TLI.getPointerTy()";
46 case MVT::iPTRAny: return "TLI.getPointerTy()";
Bob Wilsonbf8e4c12009-07-10 22:25:24 +000047 default: return getEnumName(T);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000048 }
49}
50
Owen Anderson9f944592009-08-11 20:47:22 +000051std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000052 switch (T) {
Bill Wendling02b701f2010-09-07 18:49:14 +000053 case MVT::Other: return "MVT::Other";
54 case MVT::i1: return "MVT::i1";
55 case MVT::i8: return "MVT::i8";
56 case MVT::i16: return "MVT::i16";
57 case MVT::i32: return "MVT::i32";
58 case MVT::i64: return "MVT::i64";
59 case MVT::i128: return "MVT::i128";
60 case MVT::iAny: return "MVT::iAny";
61 case MVT::fAny: return "MVT::fAny";
62 case MVT::vAny: return "MVT::vAny";
Dan Gohman94580ab2011-12-20 00:02:33 +000063 case MVT::f16: return "MVT::f16";
Bill Wendling02b701f2010-09-07 18:49:14 +000064 case MVT::f32: return "MVT::f32";
65 case MVT::f64: return "MVT::f64";
66 case MVT::f80: return "MVT::f80";
67 case MVT::f128: return "MVT::f128";
Owen Anderson9f944592009-08-11 20:47:22 +000068 case MVT::ppcf128: return "MVT::ppcf128";
Bill Wendling35380212010-09-07 20:03:56 +000069 case MVT::x86mmx: return "MVT::x86mmx";
Chris Lattner3e5fbd72010-12-21 02:38:05 +000070 case MVT::Glue: return "MVT::Glue";
Bill Wendling02b701f2010-09-07 18:49:14 +000071 case MVT::isVoid: return "MVT::isVoid";
Micah Villmow75e29c42012-09-19 22:47:07 +000072 case MVT::v2i1: return "MVT::v2i1";
73 case MVT::v4i1: return "MVT::v4i1";
74 case MVT::v8i1: return "MVT::v8i1";
75 case MVT::v16i1: return "MVT::v16i1";
Elena Demikhovsky517afbf2012-12-24 10:03:57 +000076 case MVT::v32i1: return "MVT::v32i1";
77 case MVT::v64i1: return "MVT::v64i1";
Jiangning Liu63dc8402013-09-24 02:47:27 +000078 case MVT::v1i8: return "MVT::v1i8";
Bill Wendling02b701f2010-09-07 18:49:14 +000079 case MVT::v2i8: return "MVT::v2i8";
80 case MVT::v4i8: return "MVT::v4i8";
81 case MVT::v8i8: return "MVT::v8i8";
82 case MVT::v16i8: return "MVT::v16i8";
83 case MVT::v32i8: return "MVT::v32i8";
Elena Demikhovsky517afbf2012-12-24 10:03:57 +000084 case MVT::v64i8: return "MVT::v64i8";
Micah Villmow75e29c42012-09-19 22:47:07 +000085 case MVT::v1i16: return "MVT::v1i16";
Bill Wendling02b701f2010-09-07 18:49:14 +000086 case MVT::v2i16: return "MVT::v2i16";
87 case MVT::v4i16: return "MVT::v4i16";
88 case MVT::v8i16: return "MVT::v8i16";
89 case MVT::v16i16: return "MVT::v16i16";
Elena Demikhovsky517afbf2012-12-24 10:03:57 +000090 case MVT::v32i16: return "MVT::v32i16";
Micah Villmow75e29c42012-09-19 22:47:07 +000091 case MVT::v1i32: return "MVT::v1i32";
Bill Wendling02b701f2010-09-07 18:49:14 +000092 case MVT::v2i32: return "MVT::v2i32";
93 case MVT::v4i32: return "MVT::v4i32";
94 case MVT::v8i32: return "MVT::v8i32";
Micah Villmow75e29c42012-09-19 22:47:07 +000095 case MVT::v16i32: return "MVT::v16i32";
Bill Wendling02b701f2010-09-07 18:49:14 +000096 case MVT::v1i64: return "MVT::v1i64";
97 case MVT::v2i64: return "MVT::v2i64";
98 case MVT::v4i64: return "MVT::v4i64";
99 case MVT::v8i64: return "MVT::v8i64";
Micah Villmow75e29c42012-09-19 22:47:07 +0000100 case MVT::v16i64: return "MVT::v16i64";
Pete Cooper9bcb7212012-01-12 23:14:13 +0000101 case MVT::v2f16: return "MVT::v2f16";
Pete Cooperd5438172013-10-03 03:29:21 +0000102 case MVT::v4f16: return "MVT::v4f16";
Jack Carter52f2bea2013-08-13 22:34:26 +0000103 case MVT::v8f16: return "MVT::v8f16";
Jiangning Liu63dc8402013-09-24 02:47:27 +0000104 case MVT::v1f32: return "MVT::v1f32";
Bill Wendling02b701f2010-09-07 18:49:14 +0000105 case MVT::v2f32: return "MVT::v2f32";
106 case MVT::v4f32: return "MVT::v4f32";
107 case MVT::v8f32: return "MVT::v8f32";
Elena Demikhovsky517afbf2012-12-24 10:03:57 +0000108 case MVT::v16f32: return "MVT::v16f32";
Jiangning Liu63dc8402013-09-24 02:47:27 +0000109 case MVT::v1f64: return "MVT::v1f64";
Bill Wendling02b701f2010-09-07 18:49:14 +0000110 case MVT::v2f64: return "MVT::v2f64";
111 case MVT::v4f64: return "MVT::v4f64";
Elena Demikhovsky517afbf2012-12-24 10:03:57 +0000112 case MVT::v8f64: return "MVT::v8f64";
Owen Anderson9f944592009-08-11 20:47:22 +0000113 case MVT::Metadata: return "MVT::Metadata";
Bill Wendling02b701f2010-09-07 18:49:14 +0000114 case MVT::iPTR: return "MVT::iPTR";
Owen Anderson9f944592009-08-11 20:47:22 +0000115 case MVT::iPTRAny: return "MVT::iPTRAny";
Owen Andersonca2f78a2011-11-16 01:02:57 +0000116 case MVT::Untyped: return "MVT::Untyped";
Craig Topperc4965bc2012-02-05 07:21:30 +0000117 default: llvm_unreachable("ILLEGAL VALUE TYPE!");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000118 }
119}
120
Chris Lattner8cab0212008-01-05 22:25:12 +0000121/// getQualifiedName - Return the name of the specified record, with a
122/// namespace qualifier if the record contains one.
123///
124std::string llvm::getQualifiedName(const Record *R) {
Jakob Stoklund Olesenc8c4ded2011-05-07 21:22:39 +0000125 std::string Namespace;
126 if (R->getValue("Namespace"))
127 Namespace = R->getValueAsString("Namespace");
Chris Lattner8cab0212008-01-05 22:25:12 +0000128 if (Namespace.empty()) return R->getName();
129 return Namespace + "::" + R->getName();
130}
131
132
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000133/// getTarget - Return the current instance of the Target class.
134///
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +0000135CodeGenTarget::CodeGenTarget(RecordKeeper &records)
Andrew Trick87255e32012-07-07 04:00:00 +0000136 : Records(records), RegBank(0), SchedModels(0) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000137 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanaff10d02004-06-04 14:59:42 +0000138 if (Targets.size() == 0)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000139 PrintFatalError("ERROR: No 'Target' subclasses defined!");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000140 if (Targets.size() != 1)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000141 PrintFatalError("ERROR: Multiple subclasses of Target defined!");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000142 TargetRec = Targets[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000143}
144
Andrew Trick87255e32012-07-07 04:00:00 +0000145CodeGenTarget::~CodeGenTarget() {
146 delete RegBank;
147 delete SchedModels;
148}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000149
150const std::string &CodeGenTarget::getName() const {
151 return TargetRec->getName();
152}
153
Dan Gohman3e2225d2008-08-20 21:45:57 +0000154std::string CodeGenTarget::getInstNamespace() const {
Dan Gohman3e2225d2008-08-20 21:45:57 +0000155 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
Chris Lattner4763dbe2010-03-19 01:00:55 +0000156 // Make sure not to pick up "TargetOpcode" by accidentally getting
Dan Gohman3e2225d2008-08-20 21:45:57 +0000157 // the namespace off the PHI instruction or something.
Chris Lattner4763dbe2010-03-19 01:00:55 +0000158 if ((*i)->Namespace != "TargetOpcode")
159 return (*i)->Namespace;
Dan Gohman3e2225d2008-08-20 21:45:57 +0000160 }
161
Chris Lattner4763dbe2010-03-19 01:00:55 +0000162 return "";
Dan Gohman3e2225d2008-08-20 21:45:57 +0000163}
164
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000165Record *CodeGenTarget::getInstructionSet() const {
166 return TargetRec->getValueAsDef("InstructionSet");
167}
Brian Gaeke960707c2003-11-11 22:41:34 +0000168
Chris Lattner9aec14b2010-03-19 00:07:20 +0000169
Daniel Dunbar00331992009-07-29 00:02:19 +0000170/// getAsmParser - Return the AssemblyParser definition for this target.
171///
172Record *CodeGenTarget::getAsmParser() const {
173 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
174 if (AsmParserNum >= LI.size())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000175 PrintFatalError("Target does not have an AsmParser #" + utostr(AsmParserNum) + "!");
Daniel Dunbar00331992009-07-29 00:02:19 +0000176 return LI[AsmParserNum];
177}
178
Devang Patel85d684a2012-01-09 19:13:28 +0000179/// getAsmParserVariant - Return the AssmblyParserVariant definition for
180/// this target.
181///
182Record *CodeGenTarget::getAsmParserVariant(unsigned i) const {
Andrew Trick91118a62012-07-07 03:59:51 +0000183 std::vector<Record*> LI =
Devang Patel85d684a2012-01-09 19:13:28 +0000184 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
185 if (i >= LI.size())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000186 PrintFatalError("Target does not have an AsmParserVariant #" + utostr(i) + "!");
Devang Patel85d684a2012-01-09 19:13:28 +0000187 return LI[i];
188}
189
Andrew Trick91118a62012-07-07 03:59:51 +0000190/// getAsmParserVariantCount - Return the AssmblyParserVariant definition
Devang Patel85d684a2012-01-09 19:13:28 +0000191/// available for this target.
192///
193unsigned CodeGenTarget::getAsmParserVariantCount() const {
Andrew Trick91118a62012-07-07 03:59:51 +0000194 std::vector<Record*> LI =
Devang Patel85d684a2012-01-09 19:13:28 +0000195 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
196 return LI.size();
197}
198
Chris Lattner6ffa5012004-08-14 22:50:53 +0000199/// getAsmWriter - Return the AssemblyWriter definition for this target.
200///
201Record *CodeGenTarget::getAsmWriter() const {
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000202 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
203 if (AsmWriterNum >= LI.size())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000204 PrintFatalError("Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!");
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000205 return LI[AsmWriterNum];
Chris Lattner6ffa5012004-08-14 22:50:53 +0000206}
207
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +0000208CodeGenRegBank &CodeGenTarget::getRegBank() const {
209 if (!RegBank)
210 RegBank = new CodeGenRegBank(Records);
211 return *RegBank;
212}
213
Owen Andersona84be6c2011-06-27 21:06:21 +0000214void CodeGenTarget::ReadRegAltNameIndices() const {
215 RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex");
216 std::sort(RegAltNameIndices.begin(), RegAltNameIndices.end(), LessRecord());
217}
218
Chris Lattner77d3ead2010-11-02 18:10:06 +0000219/// getRegisterByName - If there is a register with the specific AsmName,
220/// return it.
221const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
Owen Andersonccd682c2012-09-11 23:32:17 +0000222 const StringMap<CodeGenRegister*> &Regs = getRegBank().getRegistersByName();
223 StringMap<CodeGenRegister*>::const_iterator I = Regs.find(Name);
224 if (I == Regs.end())
225 return 0;
226 return I->second;
Chris Lattner77d3ead2010-11-02 18:10:06 +0000227}
228
Chris Lattnercabe0372010-03-15 06:00:16 +0000229std::vector<MVT::SimpleValueType> CodeGenTarget::
230getRegisterVTs(Record *R) const {
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000231 const CodeGenRegister *Reg = getRegBank().getReg(R);
Chris Lattnercabe0372010-03-15 06:00:16 +0000232 std::vector<MVT::SimpleValueType> Result;
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000233 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
Evan Chengd985d662006-05-16 07:05:30 +0000234 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000235 const CodeGenRegisterClass &RC = *RCs[i];
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000236 if (RC.contains(Reg)) {
Jakob Stoklund Olesen13d4a072013-03-17 17:26:09 +0000237 ArrayRef<MVT::SimpleValueType> InVTs = RC.getValueTypes();
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000238 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Evan Chengd985d662006-05-16 07:05:30 +0000239 }
240 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000241
Chris Lattner4a5f7be2010-03-27 20:32:26 +0000242 // Remove duplicates.
243 array_pod_sort(Result.begin(), Result.end());
244 Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
Evan Chengd985d662006-05-16 07:05:30 +0000245 return Result;
246}
247
248
Chris Lattnercee994b2005-09-08 21:43:21 +0000249void CodeGenTarget::ReadLegalValueTypes() const {
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000250 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
Chris Lattnercee994b2005-09-08 21:43:21 +0000251 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000252 for (unsigned ri = 0, re = RCs[i]->VTs.size(); ri != re; ++ri)
253 LegalValueTypes.push_back(RCs[i]->VTs[ri]);
Jim Grosbachf910bf22011-03-11 01:27:24 +0000254
Chris Lattner90c5b9c2005-10-14 03:54:49 +0000255 // Remove duplicates.
256 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
257 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
258 LegalValueTypes.end()),
259 LegalValueTypes.end());
Chris Lattnercee994b2005-09-08 21:43:21 +0000260}
Chris Lattner2a86fab2004-08-21 04:05:00 +0000261
Andrew Trick87255e32012-07-07 04:00:00 +0000262CodeGenSchedModels &CodeGenTarget::getSchedModels() const {
263 if (!SchedModels)
264 SchedModels = new CodeGenSchedModels(Records, *this);
265 return *SchedModels;
266}
Chris Lattner6ffa5012004-08-14 22:50:53 +0000267
Chris Lattnerc860eca2004-08-01 05:04:00 +0000268void CodeGenTarget::ReadInstructions() const {
269 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattner543fe4b2006-01-27 01:45:06 +0000270 if (Insts.size() <= 2)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000271 PrintFatalError("No 'Instruction' subclasses defined!");
Chris Lattnerc860eca2004-08-01 05:04:00 +0000272
Chris Lattner543fe4b2006-01-27 01:45:06 +0000273 // Parse the instructions defined in the .td file.
Chris Lattnera3977162010-11-01 02:15:23 +0000274 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
275 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i]);
Chris Lattnerc860eca2004-08-01 05:04:00 +0000276}
277
Chris Lattnerc9ae94c2010-03-19 00:23:20 +0000278static const CodeGenInstruction *
279GetInstByName(const char *Name,
Jim Grosbachf910bf22011-03-11 01:27:24 +0000280 const DenseMap<const Record*, CodeGenInstruction*> &Insts,
Chris Lattner77d369c2010-12-13 00:23:57 +0000281 RecordKeeper &Records) {
Chris Lattner1802b172010-03-19 01:07:44 +0000282 const Record *Rec = Records.getDef(Name);
Jim Grosbachf910bf22011-03-11 01:27:24 +0000283
Chris Lattner1802b172010-03-19 01:07:44 +0000284 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
285 I = Insts.find(Rec);
286 if (Rec == 0 || I == Insts.end())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000287 PrintFatalError(std::string("Could not find '") + Name + "' instruction!");
Chris Lattner1802b172010-03-19 01:07:44 +0000288 return I->second;
289}
290
291namespace {
292/// SortInstByName - Sorting predicate to sort instructions by name.
293///
294struct SortInstByName {
295 bool operator()(const CodeGenInstruction *Rec1,
296 const CodeGenInstruction *Rec2) const {
297 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
298 }
299};
Chris Lattnerc9ae94c2010-03-19 00:23:20 +0000300}
301
Craig Topper1129d452014-02-05 07:21:07 +0000302/// \brief Return all of the instructions defined by the target, ordered by
303/// their enum value.
Chris Lattner4763dbe2010-03-19 01:00:55 +0000304void CodeGenTarget::ComputeInstrsByEnum() const {
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000305 // The ordering here must match the ordering in TargetOpcodes.h.
Craig Topper06b3b662013-07-15 08:02:13 +0000306 static const char *const FixedInstrs[] = {
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000307 "PHI",
308 "INLINEASM",
Bill Wendling499f7972010-07-16 22:20:36 +0000309 "PROLOG_LABEL",
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000310 "EH_LABEL",
311 "GC_LABEL",
312 "KILL",
313 "EXTRACT_SUBREG",
314 "INSERT_SUBREG",
315 "IMPLICIT_DEF",
316 "SUBREG_TO_REG",
317 "COPY_TO_REGCLASS",
318 "DBG_VALUE",
319 "REG_SEQUENCE",
Jakob Stoklund Olesen676a15b2010-07-02 22:29:50 +0000320 "COPY",
Evan Cheng2a81dd42011-12-06 22:12:01 +0000321 "BUNDLE",
Nadav Rotem7c277da2012-09-06 09:17:37 +0000322 "LIFETIME_START",
323 "LIFETIME_END",
Andrew Trick74f4c742013-10-31 17:18:24 +0000324 "STACKMAP",
325 "PATCHPOINT",
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000326 0
327 };
Chris Lattner1802b172010-03-19 01:07:44 +0000328 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000329 for (const char *const *p = FixedInstrs; *p; ++p) {
Chris Lattner77d369c2010-12-13 00:23:57 +0000330 const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000331 assert(Instr && "Missing target independent instruction");
332 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
333 InstrsByEnum.push_back(Instr);
334 }
Chris Lattner1802b172010-03-19 01:07:44 +0000335 unsigned EndOfPredefines = InstrsByEnum.size();
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000336
Chris Lattner1802b172010-03-19 01:07:44 +0000337 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
338 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
339 const CodeGenInstruction *CGI = I->second;
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000340 if (CGI->Namespace != "TargetOpcode")
Chris Lattner4763dbe2010-03-19 01:00:55 +0000341 InstrsByEnum.push_back(CGI);
342 }
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000343
344 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
345
Chris Lattner1802b172010-03-19 01:07:44 +0000346 // All of the instructions are now in random order based on the map iteration.
347 // Sort them by name.
348 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(),
349 SortInstByName());
Chris Lattner945e8652005-01-22 18:58:51 +0000350}
351
352
Misha Brukman243ded52004-10-14 05:50:43 +0000353/// isLittleEndianEncoding - Return whether this target encodes its instruction
354/// in little-endian format, i.e. bits laid out in the order [0..n]
355///
356bool CodeGenTarget::isLittleEndianEncoding() const {
357 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
358}
359
Hal Finkel81e6fcc2013-12-17 22:37:50 +0000360/// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
361/// encodings, reverse the bit order of all instructions.
362void CodeGenTarget::reverseBitsForLittleEndianEncoding() {
363 if (!isLittleEndianEncoding())
364 return;
365
366 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
367 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
368 I != E; ++I) {
369 Record *R = *I;
370 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
371 R->getValueAsBit("isPseudo"))
372 continue;
373
374 BitsInit *BI = R->getValueAsBitsInit("Inst");
375
376 unsigned numBits = BI->getNumBits();
377
378 SmallVector<Init *, 16> NewBits(numBits);
379
380 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
381 unsigned bitSwapIdx = numBits - bit - 1;
382 Init *OrigBit = BI->getBit(bit);
383 Init *BitSwap = BI->getBit(bitSwapIdx);
384 NewBits[bit] = BitSwap;
385 NewBits[bitSwapIdx] = OrigBit;
386 }
387 if (numBits % 2) {
388 unsigned middle = (numBits + 1) / 2;
389 NewBits[middle] = BI->getBit(middle);
390 }
391
392 BitsInit *NewBI = BitsInit::get(NewBits);
393
394 // Update the bits in reversed order so that emitInstrOpBits will get the
395 // correct endianness.
396 R->getValue("Inst")->setValue(NewBI);
397 }
398}
399
Jakob Stoklund Olesen9dc03bb2012-08-23 19:34:41 +0000400/// guessInstructionProperties - Return true if it's OK to guess instruction
401/// properties instead of raising an error.
402///
403/// This is configurable as a temporary migration aid. It will eventually be
404/// permanently false.
405bool CodeGenTarget::guessInstructionProperties() const {
406 return getInstructionSet()->getValueAsBit("guessInstructionProperties");
407}
408
Evan Cheng9b9567b2005-12-08 02:00:36 +0000409//===----------------------------------------------------------------------===//
410// ComplexPattern implementation
411//
412ComplexPattern::ComplexPattern(Record *R) {
Evan Chengc9a62002005-12-08 02:14:08 +0000413 Ty = ::getValueType(R->getValueAsDef("Ty"));
414 NumOperands = R->getValueAsInt("NumOperands");
415 SelectFunc = R->getValueAsString("SelectFunc");
416 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng2022c792006-10-11 21:02:01 +0000417
418 // Parse the properties.
419 Properties = 0;
420 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
421 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
422 if (PropList[i]->getName() == "SDNPHasChain") {
423 Properties |= 1 << SDNPHasChain;
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000424 } else if (PropList[i]->getName() == "SDNPOptInGlue") {
425 Properties |= 1 << SDNPOptInGlue;
Chris Lattner317332f2008-01-10 07:59:24 +0000426 } else if (PropList[i]->getName() == "SDNPMayStore") {
427 Properties |= 1 << SDNPMayStore;
428 } else if (PropList[i]->getName() == "SDNPMayLoad") {
429 Properties |= 1 << SDNPMayLoad;
430 } else if (PropList[i]->getName() == "SDNPSideEffect") {
431 Properties |= 1 << SDNPSideEffect;
Mon P Wang6a490372008-06-25 08:15:39 +0000432 } else if (PropList[i]->getName() == "SDNPMemOperand") {
433 Properties |= 1 << SDNPMemOperand;
Chris Lattner83aeaab2010-03-19 05:07:09 +0000434 } else if (PropList[i]->getName() == "SDNPVariadic") {
435 Properties |= 1 << SDNPVariadic;
Chris Lattner0e023ea2010-09-21 20:31:19 +0000436 } else if (PropList[i]->getName() == "SDNPWantRoot") {
437 Properties |= 1 << SDNPWantRoot;
438 } else if (PropList[i]->getName() == "SDNPWantParent") {
439 Properties |= 1 << SDNPWantParent;
Evan Cheng2022c792006-10-11 21:02:01 +0000440 } else {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000441 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
442 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng2022c792006-10-11 21:02:01 +0000443 exit(1);
444 }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000445}
Evan Chengc9a62002005-12-08 02:14:08 +0000446
Chris Lattner2c581412006-03-24 19:49:31 +0000447//===----------------------------------------------------------------------===//
448// CodeGenIntrinsic Implementation
449//===----------------------------------------------------------------------===//
450
Dale Johannesenb842d522009-02-05 01:49:45 +0000451std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
452 bool TargetOnly) {
Chris Lattner2c581412006-03-24 19:49:31 +0000453 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000454
Chris Lattnerc92f6882006-03-27 22:48:18 +0000455 std::vector<CodeGenIntrinsic> Result;
Chris Lattner8a2ae8b72006-03-28 00:15:00 +0000456
Dale Johannesenb842d522009-02-05 01:49:45 +0000457 for (unsigned i = 0, e = I.size(); i != e; ++i) {
458 bool isTarget = I[i]->getValueAsBit("isTarget");
459 if (isTarget == TargetOnly)
460 Result.push_back(CodeGenIntrinsic(I[i]));
461 }
Chris Lattnerc92f6882006-03-27 22:48:18 +0000462 return Result;
Chris Lattner2c581412006-03-24 19:49:31 +0000463}
464
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000465CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattnerbbba8232006-03-24 20:25:01 +0000466 TheDef = R;
Chris Lattner2c581412006-03-24 19:49:31 +0000467 std::string DefName = R->getName();
Dan Gohmanddb2d652010-08-05 23:36:21 +0000468 ModRef = ReadWriteMem;
Reid Spencere67d0c22007-04-01 07:20:02 +0000469 isOverloaded = false;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000470 isCommutative = false;
John McCall375dcc92011-05-28 06:31:34 +0000471 canThrow = false;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000472 isNoReturn = false;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000473
474 if (DefName.size() <= 4 ||
Bill Wendling91821472008-11-13 09:08:33 +0000475 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000476 PrintFatalError("Intrinsic '" + DefName + "' does not start with 'int_'!");
Bill Wendling91821472008-11-13 09:08:33 +0000477
Chris Lattner2c581412006-03-24 19:49:31 +0000478 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendling91821472008-11-13 09:08:33 +0000479
Chris Lattner2c581412006-03-24 19:49:31 +0000480 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
481 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendling91821472008-11-13 09:08:33 +0000482
483 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner2c581412006-03-24 19:49:31 +0000484 Name = R->getValueAsString("LLVMName");
Bill Wendling91821472008-11-13 09:08:33 +0000485
Chris Lattner2c581412006-03-24 19:49:31 +0000486 if (Name == "") {
487 // If an explicit name isn't specified, derive one from the DefName.
488 Name = "llvm.";
Bill Wendling91821472008-11-13 09:08:33 +0000489
Chris Lattner2c581412006-03-24 19:49:31 +0000490 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendling91821472008-11-13 09:08:33 +0000491 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner2c581412006-03-24 19:49:31 +0000492 } else {
493 // Verify it starts with "llvm.".
Jim Grosbachf910bf22011-03-11 01:27:24 +0000494 if (Name.size() <= 5 ||
Bill Wendling91821472008-11-13 09:08:33 +0000495 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000496 PrintFatalError("Intrinsic '" + DefName + "'s name does not start with 'llvm.'!");
Chris Lattner2c581412006-03-24 19:49:31 +0000497 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000498
Chris Lattner2c581412006-03-24 19:49:31 +0000499 // If TargetPrefix is specified, make sure that Name starts with
500 // "llvm.<targetprefix>.".
501 if (!TargetPrefix.empty()) {
502 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendling91821472008-11-13 09:08:33 +0000503 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
504 != (TargetPrefix + "."))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000505 PrintFatalError("Intrinsic '" + DefName + "' does not start with 'llvm." +
506 TargetPrefix + ".'!");
Chris Lattner2c581412006-03-24 19:49:31 +0000507 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000508
Bill Wendling91821472008-11-13 09:08:33 +0000509 // Parse the list of return types.
Owen Anderson9f944592009-08-11 20:47:22 +0000510 std::vector<MVT::SimpleValueType> OverloadedVTs;
David Greeneaf8ee2c2011-07-29 22:43:06 +0000511 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner2c581412006-03-24 19:49:31 +0000512 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattnercbebe462007-02-27 22:08:27 +0000513 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner2c581412006-03-24 19:49:31 +0000514 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson9f944592009-08-11 20:47:22 +0000515 MVT::SimpleValueType VT;
Bob Wilsonf76486a2009-01-07 00:09:01 +0000516 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000517 unsigned MatchTy = TyEl->getValueAsInt("Number");
518 assert(MatchTy < OverloadedVTs.size() &&
519 "Invalid matching number!");
520 VT = OverloadedVTs[MatchTy];
Bob Wilsonf76486a2009-01-07 00:09:01 +0000521 // It only makes sense to use the extended and truncated vector element
522 // variants with iAny types; otherwise, if the intrinsic is not
523 // overloaded, all the types can be specified directly.
524 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
525 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson9f944592009-08-11 20:47:22 +0000526 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson781797f2009-08-11 05:03:38 +0000527 "Expected iAny or vAny type");
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000528 } else {
Bob Wilsonf76486a2009-01-07 00:09:01 +0000529 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000530 }
Craig Topper8561de92014-01-24 20:50:47 +0000531 if (MVT(VT).isOverloaded()) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000532 OverloadedVTs.push_back(VT);
Chris Lattnerb424faa2010-03-23 23:46:27 +0000533 isOverloaded = true;
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000534 }
Chris Lattnerb424faa2010-03-23 23:46:27 +0000535
536 // Reject invalid types.
537 if (VT == MVT::isVoid)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000538 PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000539
Bill Wendling91821472008-11-13 09:08:33 +0000540 IS.RetVTs.push_back(VT);
541 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner2c581412006-03-24 19:49:31 +0000542 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000543
Bill Wendling91821472008-11-13 09:08:33 +0000544 // Parse the list of parameter types.
545 TypeList = R->getValueAsListInit("ParamTypes");
546 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
547 Record *TyEl = TypeList->getElementAsRecord(i);
548 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson9f944592009-08-11 20:47:22 +0000549 MVT::SimpleValueType VT;
Bob Wilsonf76486a2009-01-07 00:09:01 +0000550 if (TyEl->isSubClassOf("LLVMMatchType")) {
551 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000552 assert(MatchTy < OverloadedVTs.size() &&
553 "Invalid matching number!");
554 VT = OverloadedVTs[MatchTy];
Bob Wilsonf76486a2009-01-07 00:09:01 +0000555 // It only makes sense to use the extended and truncated vector element
556 // variants with iAny types; otherwise, if the intrinsic is not
557 // overloaded, all the types can be specified directly.
558 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
559 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson9f944592009-08-11 20:47:22 +0000560 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson781797f2009-08-11 05:03:38 +0000561 "Expected iAny or vAny type");
Bob Wilsonf76486a2009-01-07 00:09:01 +0000562 } else
563 VT = getValueType(TyEl->getValueAsDef("VT"));
Jim Grosbachf910bf22011-03-11 01:27:24 +0000564
Craig Topper8561de92014-01-24 20:50:47 +0000565 if (MVT(VT).isOverloaded()) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000566 OverloadedVTs.push_back(VT);
Chris Lattnerb424faa2010-03-23 23:46:27 +0000567 isOverloaded = true;
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000568 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000569
Chris Lattnerb424faa2010-03-23 23:46:27 +0000570 // Reject invalid types.
571 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000572 PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000573
Bill Wendling91821472008-11-13 09:08:33 +0000574 IS.ParamVTs.push_back(VT);
575 IS.ParamTypeDefs.push_back(TyEl);
576 }
577
Chris Lattner2c581412006-03-24 19:49:31 +0000578 // Parse the intrinsic properties.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000579 ListInit *PropList = R->getValueAsListInit("Properties");
Chris Lattner2c581412006-03-24 19:49:31 +0000580 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattnercbebe462007-02-27 22:08:27 +0000581 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner2c581412006-03-24 19:49:31 +0000582 assert(Property->isSubClassOf("IntrinsicProperty") &&
583 "Expected a property!");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000584
Chris Lattner989b16e42006-04-10 22:02:59 +0000585 if (Property->getName() == "IntrNoMem")
Chris Lattner2c581412006-03-24 19:49:31 +0000586 ModRef = NoMem;
Chris Lattner989b16e42006-04-10 22:02:59 +0000587 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner2c581412006-03-24 19:49:31 +0000588 ModRef = ReadArgMem;
589 else if (Property->getName() == "IntrReadMem")
590 ModRef = ReadMem;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000591 else if (Property->getName() == "IntrReadWriteArgMem")
592 ModRef = ReadWriteArgMem;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000593 else if (Property->getName() == "Commutative")
594 isCommutative = true;
John McCall375dcc92011-05-28 06:31:34 +0000595 else if (Property->getName() == "Throws")
596 canThrow = true;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000597 else if (Property->getName() == "IntrNoReturn")
598 isNoReturn = true;
Chris Lattner9a3113a2009-01-12 01:12:03 +0000599 else if (Property->isSubClassOf("NoCapture")) {
600 unsigned ArgNo = Property->getValueAsInt("ArgNo");
601 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000602 } else if (Property->isSubClassOf("ReadOnly")) {
603 unsigned ArgNo = Property->getValueAsInt("ArgNo");
604 ArgumentAttributes.push_back(std::make_pair(ArgNo, ReadOnly));
605 } else if (Property->isSubClassOf("ReadNone")) {
606 unsigned ArgNo = Property->getValueAsInt("ArgNo");
607 ArgumentAttributes.push_back(std::make_pair(ArgNo, ReadNone));
Chris Lattner9a3113a2009-01-12 01:12:03 +0000608 } else
Craig Topperc4965bc2012-02-05 07:21:30 +0000609 llvm_unreachable("Unknown property!");
Chris Lattner2c581412006-03-24 19:49:31 +0000610 }
John McCall375dcc92011-05-28 06:31:34 +0000611
612 // Sort the argument attributes for later benefit.
613 std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end());
Chris Lattner2c581412006-03-24 19:49:31 +0000614}