blob: de00dc6407ebe942a040f806f097ad307d925cbc [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)
Craig Topper24064772014-04-15 07:20:03 +0000136 : Records(records), RegBank(nullptr), SchedModels(nullptr) {
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() {
Craig Topperf1aab452014-02-06 06:27:59 +0000146 DeleteContainerSeconds(Instructions);
Andrew Trick87255e32012-07-07 04:00:00 +0000147 delete RegBank;
148 delete SchedModels;
149}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000150
151const std::string &CodeGenTarget::getName() const {
152 return TargetRec->getName();
153}
154
Dan Gohman3e2225d2008-08-20 21:45:57 +0000155std::string CodeGenTarget::getInstNamespace() const {
Dan Gohman3e2225d2008-08-20 21:45:57 +0000156 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
Chris Lattner4763dbe2010-03-19 01:00:55 +0000157 // Make sure not to pick up "TargetOpcode" by accidentally getting
Dan Gohman3e2225d2008-08-20 21:45:57 +0000158 // the namespace off the PHI instruction or something.
Chris Lattner4763dbe2010-03-19 01:00:55 +0000159 if ((*i)->Namespace != "TargetOpcode")
160 return (*i)->Namespace;
Dan Gohman3e2225d2008-08-20 21:45:57 +0000161 }
162
Chris Lattner4763dbe2010-03-19 01:00:55 +0000163 return "";
Dan Gohman3e2225d2008-08-20 21:45:57 +0000164}
165
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000166Record *CodeGenTarget::getInstructionSet() const {
167 return TargetRec->getValueAsDef("InstructionSet");
168}
Brian Gaeke960707c2003-11-11 22:41:34 +0000169
Chris Lattner9aec14b2010-03-19 00:07:20 +0000170
Daniel Dunbar00331992009-07-29 00:02:19 +0000171/// getAsmParser - Return the AssemblyParser definition for this target.
172///
173Record *CodeGenTarget::getAsmParser() const {
174 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
175 if (AsmParserNum >= LI.size())
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000176 PrintFatalError("Target does not have an AsmParser #" +
177 Twine(AsmParserNum) + "!");
Daniel Dunbar00331992009-07-29 00:02:19 +0000178 return LI[AsmParserNum];
179}
180
Devang Patel85d684a2012-01-09 19:13:28 +0000181/// getAsmParserVariant - Return the AssmblyParserVariant definition for
182/// this target.
183///
184Record *CodeGenTarget::getAsmParserVariant(unsigned i) const {
Andrew Trick91118a62012-07-07 03:59:51 +0000185 std::vector<Record*> LI =
Devang Patel85d684a2012-01-09 19:13:28 +0000186 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
187 if (i >= LI.size())
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000188 PrintFatalError("Target does not have an AsmParserVariant #" + Twine(i) +
189 "!");
Devang Patel85d684a2012-01-09 19:13:28 +0000190 return LI[i];
191}
192
Andrew Trick91118a62012-07-07 03:59:51 +0000193/// getAsmParserVariantCount - Return the AssmblyParserVariant definition
Devang Patel85d684a2012-01-09 19:13:28 +0000194/// available for this target.
195///
196unsigned CodeGenTarget::getAsmParserVariantCount() const {
Andrew Trick91118a62012-07-07 03:59:51 +0000197 std::vector<Record*> LI =
Devang Patel85d684a2012-01-09 19:13:28 +0000198 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
199 return LI.size();
200}
201
Chris Lattner6ffa5012004-08-14 22:50:53 +0000202/// getAsmWriter - Return the AssemblyWriter definition for this target.
203///
204Record *CodeGenTarget::getAsmWriter() const {
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000205 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
206 if (AsmWriterNum >= LI.size())
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000207 PrintFatalError("Target does not have an AsmWriter #" +
208 Twine(AsmWriterNum) + "!");
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000209 return LI[AsmWriterNum];
Chris Lattner6ffa5012004-08-14 22:50:53 +0000210}
211
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +0000212CodeGenRegBank &CodeGenTarget::getRegBank() const {
213 if (!RegBank)
214 RegBank = new CodeGenRegBank(Records);
215 return *RegBank;
216}
217
Owen Andersona84be6c2011-06-27 21:06:21 +0000218void CodeGenTarget::ReadRegAltNameIndices() const {
219 RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex");
220 std::sort(RegAltNameIndices.begin(), RegAltNameIndices.end(), LessRecord());
221}
222
Chris Lattner77d3ead2010-11-02 18:10:06 +0000223/// getRegisterByName - If there is a register with the specific AsmName,
224/// return it.
225const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
Owen Andersonccd682c2012-09-11 23:32:17 +0000226 const StringMap<CodeGenRegister*> &Regs = getRegBank().getRegistersByName();
227 StringMap<CodeGenRegister*>::const_iterator I = Regs.find(Name);
228 if (I == Regs.end())
Craig Topper24064772014-04-15 07:20:03 +0000229 return nullptr;
Owen Andersonccd682c2012-09-11 23:32:17 +0000230 return I->second;
Chris Lattner77d3ead2010-11-02 18:10:06 +0000231}
232
Chris Lattnercabe0372010-03-15 06:00:16 +0000233std::vector<MVT::SimpleValueType> CodeGenTarget::
234getRegisterVTs(Record *R) const {
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000235 const CodeGenRegister *Reg = getRegBank().getReg(R);
Chris Lattnercabe0372010-03-15 06:00:16 +0000236 std::vector<MVT::SimpleValueType> Result;
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000237 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
Evan Chengd985d662006-05-16 07:05:30 +0000238 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000239 const CodeGenRegisterClass &RC = *RCs[i];
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000240 if (RC.contains(Reg)) {
Jakob Stoklund Olesen13d4a072013-03-17 17:26:09 +0000241 ArrayRef<MVT::SimpleValueType> InVTs = RC.getValueTypes();
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000242 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Evan Chengd985d662006-05-16 07:05:30 +0000243 }
244 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000245
Chris Lattner4a5f7be2010-03-27 20:32:26 +0000246 // Remove duplicates.
247 array_pod_sort(Result.begin(), Result.end());
248 Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
Evan Chengd985d662006-05-16 07:05:30 +0000249 return Result;
250}
251
252
Chris Lattnercee994b2005-09-08 21:43:21 +0000253void CodeGenTarget::ReadLegalValueTypes() const {
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000254 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
Chris Lattnercee994b2005-09-08 21:43:21 +0000255 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Jakob Stoklund Olesen19be2ab2011-09-29 22:28:37 +0000256 for (unsigned ri = 0, re = RCs[i]->VTs.size(); ri != re; ++ri)
257 LegalValueTypes.push_back(RCs[i]->VTs[ri]);
Jim Grosbachf910bf22011-03-11 01:27:24 +0000258
Chris Lattner90c5b9c2005-10-14 03:54:49 +0000259 // Remove duplicates.
260 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
261 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
262 LegalValueTypes.end()),
263 LegalValueTypes.end());
Chris Lattnercee994b2005-09-08 21:43:21 +0000264}
Chris Lattner2a86fab2004-08-21 04:05:00 +0000265
Andrew Trick87255e32012-07-07 04:00:00 +0000266CodeGenSchedModels &CodeGenTarget::getSchedModels() const {
267 if (!SchedModels)
268 SchedModels = new CodeGenSchedModels(Records, *this);
269 return *SchedModels;
270}
Chris Lattner6ffa5012004-08-14 22:50:53 +0000271
Chris Lattnerc860eca2004-08-01 05:04:00 +0000272void CodeGenTarget::ReadInstructions() const {
273 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattner543fe4b2006-01-27 01:45:06 +0000274 if (Insts.size() <= 2)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000275 PrintFatalError("No 'Instruction' subclasses defined!");
Chris Lattnerc860eca2004-08-01 05:04:00 +0000276
Chris Lattner543fe4b2006-01-27 01:45:06 +0000277 // Parse the instructions defined in the .td file.
Chris Lattnera3977162010-11-01 02:15:23 +0000278 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
279 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i]);
Chris Lattnerc860eca2004-08-01 05:04:00 +0000280}
281
Chris Lattnerc9ae94c2010-03-19 00:23:20 +0000282static const CodeGenInstruction *
283GetInstByName(const char *Name,
Jim Grosbachf910bf22011-03-11 01:27:24 +0000284 const DenseMap<const Record*, CodeGenInstruction*> &Insts,
Chris Lattner77d369c2010-12-13 00:23:57 +0000285 RecordKeeper &Records) {
Chris Lattner1802b172010-03-19 01:07:44 +0000286 const Record *Rec = Records.getDef(Name);
Jim Grosbachf910bf22011-03-11 01:27:24 +0000287
Chris Lattner1802b172010-03-19 01:07:44 +0000288 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
289 I = Insts.find(Rec);
Craig Topper24064772014-04-15 07:20:03 +0000290 if (!Rec || I == Insts.end())
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000291 PrintFatalError(Twine("Could not find '") + Name + "' instruction!");
Chris Lattner1802b172010-03-19 01:07:44 +0000292 return I->second;
293}
294
Craig Topper1129d452014-02-05 07:21:07 +0000295/// \brief Return all of the instructions defined by the target, ordered by
296/// their enum value.
Chris Lattner4763dbe2010-03-19 01:00:55 +0000297void CodeGenTarget::ComputeInstrsByEnum() const {
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000298 // The ordering here must match the ordering in TargetOpcodes.h.
Craig Topper06b3b662013-07-15 08:02:13 +0000299 static const char *const FixedInstrs[] = {
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000300 "PHI", "INLINEASM", "CFI_INSTRUCTION", "EH_LABEL",
Rafael Espindolac994c6a2014-03-07 05:32:03 +0000301 "GC_LABEL", "KILL", "EXTRACT_SUBREG", "INSERT_SUBREG",
302 "IMPLICIT_DEF", "SUBREG_TO_REG", "COPY_TO_REGCLASS", "DBG_VALUE",
303 "REG_SEQUENCE", "COPY", "BUNDLE", "LIFETIME_START",
Craig Topper24064772014-04-15 07:20:03 +0000304 "LIFETIME_END", "STACKMAP", "PATCHPOINT", nullptr};
Chris Lattner1802b172010-03-19 01:07:44 +0000305 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000306 for (const char *const *p = FixedInstrs; *p; ++p) {
Chris Lattner77d369c2010-12-13 00:23:57 +0000307 const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000308 assert(Instr && "Missing target independent instruction");
309 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
310 InstrsByEnum.push_back(Instr);
311 }
Chris Lattner1802b172010-03-19 01:07:44 +0000312 unsigned EndOfPredefines = InstrsByEnum.size();
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000313
Chris Lattner1802b172010-03-19 01:07:44 +0000314 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
315 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
316 const CodeGenInstruction *CGI = I->second;
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000317 if (CGI->Namespace != "TargetOpcode")
Chris Lattner4763dbe2010-03-19 01:00:55 +0000318 InstrsByEnum.push_back(CGI);
319 }
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000320
321 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
322
Chris Lattner1802b172010-03-19 01:07:44 +0000323 // All of the instructions are now in random order based on the map iteration.
324 // Sort them by name.
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000325 std::sort(InstrsByEnum.begin() + EndOfPredefines, InstrsByEnum.end(),
326 [](const CodeGenInstruction *Rec1, const CodeGenInstruction *Rec2) {
327 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
328 });
Chris Lattner945e8652005-01-22 18:58:51 +0000329}
330
331
Misha Brukman243ded52004-10-14 05:50:43 +0000332/// isLittleEndianEncoding - Return whether this target encodes its instruction
333/// in little-endian format, i.e. bits laid out in the order [0..n]
334///
335bool CodeGenTarget::isLittleEndianEncoding() const {
336 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
337}
338
Hal Finkel81e6fcc2013-12-17 22:37:50 +0000339/// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
340/// encodings, reverse the bit order of all instructions.
341void CodeGenTarget::reverseBitsForLittleEndianEncoding() {
342 if (!isLittleEndianEncoding())
343 return;
344
345 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
346 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
347 I != E; ++I) {
348 Record *R = *I;
349 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
350 R->getValueAsBit("isPseudo"))
351 continue;
352
353 BitsInit *BI = R->getValueAsBitsInit("Inst");
354
355 unsigned numBits = BI->getNumBits();
356
357 SmallVector<Init *, 16> NewBits(numBits);
358
359 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
360 unsigned bitSwapIdx = numBits - bit - 1;
361 Init *OrigBit = BI->getBit(bit);
362 Init *BitSwap = BI->getBit(bitSwapIdx);
363 NewBits[bit] = BitSwap;
364 NewBits[bitSwapIdx] = OrigBit;
365 }
366 if (numBits % 2) {
367 unsigned middle = (numBits + 1) / 2;
368 NewBits[middle] = BI->getBit(middle);
369 }
370
371 BitsInit *NewBI = BitsInit::get(NewBits);
372
373 // Update the bits in reversed order so that emitInstrOpBits will get the
374 // correct endianness.
375 R->getValue("Inst")->setValue(NewBI);
376 }
377}
378
Jakob Stoklund Olesen9dc03bb2012-08-23 19:34:41 +0000379/// guessInstructionProperties - Return true if it's OK to guess instruction
380/// properties instead of raising an error.
381///
382/// This is configurable as a temporary migration aid. It will eventually be
383/// permanently false.
384bool CodeGenTarget::guessInstructionProperties() const {
385 return getInstructionSet()->getValueAsBit("guessInstructionProperties");
386}
387
Evan Cheng9b9567b2005-12-08 02:00:36 +0000388//===----------------------------------------------------------------------===//
389// ComplexPattern implementation
390//
391ComplexPattern::ComplexPattern(Record *R) {
Evan Chengc9a62002005-12-08 02:14:08 +0000392 Ty = ::getValueType(R->getValueAsDef("Ty"));
393 NumOperands = R->getValueAsInt("NumOperands");
394 SelectFunc = R->getValueAsString("SelectFunc");
395 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng2022c792006-10-11 21:02:01 +0000396
397 // Parse the properties.
398 Properties = 0;
399 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
400 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
401 if (PropList[i]->getName() == "SDNPHasChain") {
402 Properties |= 1 << SDNPHasChain;
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000403 } else if (PropList[i]->getName() == "SDNPOptInGlue") {
404 Properties |= 1 << SDNPOptInGlue;
Chris Lattner317332f2008-01-10 07:59:24 +0000405 } else if (PropList[i]->getName() == "SDNPMayStore") {
406 Properties |= 1 << SDNPMayStore;
407 } else if (PropList[i]->getName() == "SDNPMayLoad") {
408 Properties |= 1 << SDNPMayLoad;
409 } else if (PropList[i]->getName() == "SDNPSideEffect") {
410 Properties |= 1 << SDNPSideEffect;
Mon P Wang6a490372008-06-25 08:15:39 +0000411 } else if (PropList[i]->getName() == "SDNPMemOperand") {
412 Properties |= 1 << SDNPMemOperand;
Chris Lattner83aeaab2010-03-19 05:07:09 +0000413 } else if (PropList[i]->getName() == "SDNPVariadic") {
414 Properties |= 1 << SDNPVariadic;
Chris Lattner0e023ea2010-09-21 20:31:19 +0000415 } else if (PropList[i]->getName() == "SDNPWantRoot") {
416 Properties |= 1 << SDNPWantRoot;
417 } else if (PropList[i]->getName() == "SDNPWantParent") {
418 Properties |= 1 << SDNPWantParent;
Evan Cheng2022c792006-10-11 21:02:01 +0000419 } else {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000420 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
421 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng2022c792006-10-11 21:02:01 +0000422 exit(1);
423 }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000424}
Evan Chengc9a62002005-12-08 02:14:08 +0000425
Chris Lattner2c581412006-03-24 19:49:31 +0000426//===----------------------------------------------------------------------===//
427// CodeGenIntrinsic Implementation
428//===----------------------------------------------------------------------===//
429
Dale Johannesenb842d522009-02-05 01:49:45 +0000430std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
431 bool TargetOnly) {
Chris Lattner2c581412006-03-24 19:49:31 +0000432 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000433
Chris Lattnerc92f6882006-03-27 22:48:18 +0000434 std::vector<CodeGenIntrinsic> Result;
Chris Lattner8a2ae8b72006-03-28 00:15:00 +0000435
Dale Johannesenb842d522009-02-05 01:49:45 +0000436 for (unsigned i = 0, e = I.size(); i != e; ++i) {
437 bool isTarget = I[i]->getValueAsBit("isTarget");
438 if (isTarget == TargetOnly)
439 Result.push_back(CodeGenIntrinsic(I[i]));
440 }
Chris Lattnerc92f6882006-03-27 22:48:18 +0000441 return Result;
Chris Lattner2c581412006-03-24 19:49:31 +0000442}
443
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000444CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattnerbbba8232006-03-24 20:25:01 +0000445 TheDef = R;
Chris Lattner2c581412006-03-24 19:49:31 +0000446 std::string DefName = R->getName();
Dan Gohmanddb2d652010-08-05 23:36:21 +0000447 ModRef = ReadWriteMem;
Reid Spencere67d0c22007-04-01 07:20:02 +0000448 isOverloaded = false;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000449 isCommutative = false;
John McCall375dcc92011-05-28 06:31:34 +0000450 canThrow = false;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000451 isNoReturn = false;
Eli Bendersky2281ef92014-03-18 23:51:07 +0000452 isNoDuplicate = false;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000453
454 if (DefName.size() <= 4 ||
Bill Wendling91821472008-11-13 09:08:33 +0000455 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000456 PrintFatalError("Intrinsic '" + DefName + "' does not start with 'int_'!");
Bill Wendling91821472008-11-13 09:08:33 +0000457
Chris Lattner2c581412006-03-24 19:49:31 +0000458 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendling91821472008-11-13 09:08:33 +0000459
Chris Lattner2c581412006-03-24 19:49:31 +0000460 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
461 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendling91821472008-11-13 09:08:33 +0000462
463 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner2c581412006-03-24 19:49:31 +0000464 Name = R->getValueAsString("LLVMName");
Bill Wendling91821472008-11-13 09:08:33 +0000465
Chris Lattner2c581412006-03-24 19:49:31 +0000466 if (Name == "") {
467 // If an explicit name isn't specified, derive one from the DefName.
468 Name = "llvm.";
Bill Wendling91821472008-11-13 09:08:33 +0000469
Chris Lattner2c581412006-03-24 19:49:31 +0000470 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendling91821472008-11-13 09:08:33 +0000471 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner2c581412006-03-24 19:49:31 +0000472 } else {
473 // Verify it starts with "llvm.".
Jim Grosbachf910bf22011-03-11 01:27:24 +0000474 if (Name.size() <= 5 ||
Bill Wendling91821472008-11-13 09:08:33 +0000475 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000476 PrintFatalError("Intrinsic '" + DefName + "'s name does not start with 'llvm.'!");
Chris Lattner2c581412006-03-24 19:49:31 +0000477 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000478
Chris Lattner2c581412006-03-24 19:49:31 +0000479 // If TargetPrefix is specified, make sure that Name starts with
480 // "llvm.<targetprefix>.".
481 if (!TargetPrefix.empty()) {
482 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendling91821472008-11-13 09:08:33 +0000483 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
484 != (TargetPrefix + "."))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000485 PrintFatalError("Intrinsic '" + DefName + "' does not start with 'llvm." +
486 TargetPrefix + ".'!");
Chris Lattner2c581412006-03-24 19:49:31 +0000487 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000488
Bill Wendling91821472008-11-13 09:08:33 +0000489 // Parse the list of return types.
Owen Anderson9f944592009-08-11 20:47:22 +0000490 std::vector<MVT::SimpleValueType> OverloadedVTs;
David Greeneaf8ee2c2011-07-29 22:43:06 +0000491 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner2c581412006-03-24 19:49:31 +0000492 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattnercbebe462007-02-27 22:08:27 +0000493 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner2c581412006-03-24 19:49:31 +0000494 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")) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000497 unsigned MatchTy = TyEl->getValueAsInt("Number");
498 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.
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000504 assert(((!TyEl->isSubClassOf("LLVMExtendedType") &&
505 !TyEl->isSubClassOf("LLVMTruncatedType")) ||
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 Wilsonb8c370a2009-04-16 21:51:05 +0000508 } else {
Bob Wilsonf76486a2009-01-07 00:09:01 +0000509 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000510 }
Craig Topper8561de92014-01-24 20:50:47 +0000511 if (MVT(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 }
Chris Lattnerb424faa2010-03-23 23:46:27 +0000515
516 // Reject invalid types.
517 if (VT == MVT::isVoid)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000518 PrintFatalError("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.RetVTs.push_back(VT);
521 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner2c581412006-03-24 19:49:31 +0000522 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000523
Bill Wendling91821472008-11-13 09:08:33 +0000524 // Parse the list of parameter types.
525 TypeList = R->getValueAsListInit("ParamTypes");
526 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
527 Record *TyEl = TypeList->getElementAsRecord(i);
528 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson9f944592009-08-11 20:47:22 +0000529 MVT::SimpleValueType VT;
Bob Wilsonf76486a2009-01-07 00:09:01 +0000530 if (TyEl->isSubClassOf("LLVMMatchType")) {
531 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000532 assert(MatchTy < OverloadedVTs.size() &&
533 "Invalid matching number!");
534 VT = OverloadedVTs[MatchTy];
Bob Wilsonf76486a2009-01-07 00:09:01 +0000535 // It only makes sense to use the extended and truncated vector element
536 // variants with iAny types; otherwise, if the intrinsic is not
537 // overloaded, all the types can be specified directly.
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000538 assert(((!TyEl->isSubClassOf("LLVMExtendedType") &&
539 !TyEl->isSubClassOf("LLVMTruncatedType")) ||
Owen Anderson9f944592009-08-11 20:47:22 +0000540 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson781797f2009-08-11 05:03:38 +0000541 "Expected iAny or vAny type");
Bob Wilsonf76486a2009-01-07 00:09:01 +0000542 } else
543 VT = getValueType(TyEl->getValueAsDef("VT"));
Jim Grosbachf910bf22011-03-11 01:27:24 +0000544
Craig Topper8561de92014-01-24 20:50:47 +0000545 if (MVT(VT).isOverloaded()) {
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000546 OverloadedVTs.push_back(VT);
Chris Lattnerb424faa2010-03-23 23:46:27 +0000547 isOverloaded = true;
Bob Wilsonb8c370a2009-04-16 21:51:05 +0000548 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000549
Chris Lattnerb424faa2010-03-23 23:46:27 +0000550 // Reject invalid types.
551 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000552 PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000553
Bill Wendling91821472008-11-13 09:08:33 +0000554 IS.ParamVTs.push_back(VT);
555 IS.ParamTypeDefs.push_back(TyEl);
556 }
557
Chris Lattner2c581412006-03-24 19:49:31 +0000558 // Parse the intrinsic properties.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000559 ListInit *PropList = R->getValueAsListInit("Properties");
Chris Lattner2c581412006-03-24 19:49:31 +0000560 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattnercbebe462007-02-27 22:08:27 +0000561 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner2c581412006-03-24 19:49:31 +0000562 assert(Property->isSubClassOf("IntrinsicProperty") &&
563 "Expected a property!");
Jim Grosbachf910bf22011-03-11 01:27:24 +0000564
Chris Lattner989b16e42006-04-10 22:02:59 +0000565 if (Property->getName() == "IntrNoMem")
Chris Lattner2c581412006-03-24 19:49:31 +0000566 ModRef = NoMem;
Chris Lattner989b16e42006-04-10 22:02:59 +0000567 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner2c581412006-03-24 19:49:31 +0000568 ModRef = ReadArgMem;
569 else if (Property->getName() == "IntrReadMem")
570 ModRef = ReadMem;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000571 else if (Property->getName() == "IntrReadWriteArgMem")
572 ModRef = ReadWriteArgMem;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000573 else if (Property->getName() == "Commutative")
574 isCommutative = true;
John McCall375dcc92011-05-28 06:31:34 +0000575 else if (Property->getName() == "Throws")
576 canThrow = true;
Eli Bendersky2281ef92014-03-18 23:51:07 +0000577 else if (Property->getName() == "IntrNoDuplicate")
578 isNoDuplicate = true;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000579 else if (Property->getName() == "IntrNoReturn")
580 isNoReturn = true;
Chris Lattner9a3113a2009-01-12 01:12:03 +0000581 else if (Property->isSubClassOf("NoCapture")) {
582 unsigned ArgNo = Property->getValueAsInt("ArgNo");
583 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000584 } else if (Property->isSubClassOf("ReadOnly")) {
585 unsigned ArgNo = Property->getValueAsInt("ArgNo");
586 ArgumentAttributes.push_back(std::make_pair(ArgNo, ReadOnly));
587 } else if (Property->isSubClassOf("ReadNone")) {
588 unsigned ArgNo = Property->getValueAsInt("ArgNo");
589 ArgumentAttributes.push_back(std::make_pair(ArgNo, ReadNone));
Chris Lattner9a3113a2009-01-12 01:12:03 +0000590 } else
Craig Topperc4965bc2012-02-05 07:21:30 +0000591 llvm_unreachable("Unknown property!");
Chris Lattner2c581412006-03-24 19:49:31 +0000592 }
John McCall375dcc92011-05-28 06:31:34 +0000593
594 // Sort the argument attributes for later benefit.
595 std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end());
Chris Lattner2c581412006-03-24 19:49:31 +0000596}