blob: cd0b8c7b9e110ea2ba3408e21fdac573f1435424 [file] [log] [blame]
Chris Lattner6cc654b2008-01-06 01:35:39 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner45872072003-08-07 05:38:11 +00009//
Chris Lattner6cc654b2008-01-06 01:35:39 +000010// This class wraps target description classes used by the various code
Chris Lattner45872072003-08-07 05:38:11 +000011// generation TableGen backends. This makes it easier to access the data and
12// provides a single place that needs to check it for validity. All of these
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000013// classes abort on error conditions.
Chris Lattner45872072003-08-07 05:38:11 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner803a5f62004-08-01 04:04:35 +000017#include "CodeGenTarget.h"
Chris Lattner43fbbc32006-03-24 19:49:31 +000018#include "CodeGenIntrinsics.h"
Andrew Trick2661b412012-07-07 04:00:00 +000019#include "CodeGenSchedule.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/Support/CommandLine.h"
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000023#include "llvm/TableGen/Error.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000024#include "llvm/TableGen/Record.h"
Chris Lattner75ee2eb2005-10-14 03:54:49 +000025#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner560a79f2004-10-03 19:34:31 +000028static cl::opt<unsigned>
Daniel Dunbar0e2771f2009-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 Lattner560a79f2004-10-03 19:34:31 +000033AsmWriterNum("asmwriternum", cl::init(0),
34 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
35
Owen Anderson825b72b2009-08-11 20:47:22 +000036/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands83ec4b62008-06-06 12:08:01 +000037/// record corresponds to.
Owen Anderson825b72b2009-08-11 20:47:22 +000038MVT::SimpleValueType llvm::getValueType(Record *Rec) {
39 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000040}
41
Owen Anderson825b72b2009-08-11 20:47:22 +000042std::string llvm::getName(MVT::SimpleValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000043 switch (T) {
Owen Anderson825b72b2009-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 Wilsonbbf1caa2009-07-10 22:25:24 +000047 default: return getEnumName(T);
Chris Lattner45872072003-08-07 05:38:11 +000048 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000049}
50
Owen Anderson825b72b2009-08-11 20:47:22 +000051std::string llvm::getEnumName(MVT::SimpleValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000052 switch (T) {
Bill Wendling72d10272010-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 Gohmane3376ec2011-12-20 00:02:33 +000063 case MVT::f16: return "MVT::f16";
Bill Wendling72d10272010-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 Anderson825b72b2009-08-11 20:47:22 +000068 case MVT::ppcf128: return "MVT::ppcf128";
Bill Wendlingd8dd5752010-09-07 20:03:56 +000069 case MVT::x86mmx: return "MVT::x86mmx";
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000070 case MVT::Glue: return "MVT::Glue";
Bill Wendling72d10272010-09-07 18:49:14 +000071 case MVT::isVoid: return "MVT::isVoid";
Micah Villmow3e9b6db2012-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 Demikhovsky94d7ab72012-12-24 10:03:57 +000076 case MVT::v32i1: return "MVT::v32i1";
77 case MVT::v64i1: return "MVT::v64i1";
Jiangning Liu477fc622013-09-24 02:47:27 +000078 case MVT::v1i8: return "MVT::v1i8";
Bill Wendling72d10272010-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 Demikhovsky94d7ab72012-12-24 10:03:57 +000084 case MVT::v64i8: return "MVT::v64i8";
Micah Villmow3e9b6db2012-09-19 22:47:07 +000085 case MVT::v1i16: return "MVT::v1i16";
Bill Wendling72d10272010-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 Demikhovsky94d7ab72012-12-24 10:03:57 +000090 case MVT::v32i16: return "MVT::v32i16";
Micah Villmow3e9b6db2012-09-19 22:47:07 +000091 case MVT::v1i32: return "MVT::v1i32";
Bill Wendling72d10272010-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 Villmow3e9b6db2012-09-19 22:47:07 +000095 case MVT::v16i32: return "MVT::v16i32";
Bill Wendling72d10272010-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 Villmow3e9b6db2012-09-19 22:47:07 +0000100 case MVT::v16i64: return "MVT::v16i64";
Pete Cooperddfda5c2012-01-12 23:14:13 +0000101 case MVT::v2f16: return "MVT::v2f16";
Pete Cooperda750232013-10-03 03:29:21 +0000102 case MVT::v4f16: return "MVT::v4f16";
Jack Carter0fe37922013-08-13 22:34:26 +0000103 case MVT::v8f16: return "MVT::v8f16";
Jiangning Liu477fc622013-09-24 02:47:27 +0000104 case MVT::v1f32: return "MVT::v1f32";
Bill Wendling72d10272010-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 Demikhovsky94d7ab72012-12-24 10:03:57 +0000108 case MVT::v16f32: return "MVT::v16f32";
Jiangning Liu477fc622013-09-24 02:47:27 +0000109 case MVT::v1f64: return "MVT::v1f64";
Bill Wendling72d10272010-09-07 18:49:14 +0000110 case MVT::v2f64: return "MVT::v2f64";
111 case MVT::v4f64: return "MVT::v4f64";
Elena Demikhovsky94d7ab72012-12-24 10:03:57 +0000112 case MVT::v8f64: return "MVT::v8f64";
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 case MVT::Metadata: return "MVT::Metadata";
Bill Wendling72d10272010-09-07 18:49:14 +0000114 case MVT::iPTR: return "MVT::iPTR";
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 case MVT::iPTRAny: return "MVT::iPTRAny";
Owen Anderson99aa14f2011-11-16 01:02:57 +0000116 case MVT::Untyped: return "MVT::Untyped";
Craig Topper655b8de2012-02-05 07:21:30 +0000117 default: llvm_unreachable("ILLEGAL VALUE TYPE!");
Chris Lattnerb72fb7e2003-08-10 19:50:32 +0000118 }
119}
120
Chris Lattner6cefb772008-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 Olesen123cab92011-05-07 21:22:39 +0000125 std::string Namespace;
126 if (R->getValue("Namespace"))
127 Namespace = R->getValueAsString("Namespace");
Chris Lattner6cefb772008-01-05 22:25:12 +0000128 if (Namespace.empty()) return R->getName();
129 return Namespace + "::" + R->getName();
130}
131
132
Chris Lattner45872072003-08-07 05:38:11 +0000133/// getTarget - Return the current instance of the Target class.
134///
Jakob Stoklund Olesendc29c442011-06-10 18:40:00 +0000135CodeGenTarget::CodeGenTarget(RecordKeeper &records)
Andrew Trick2661b412012-07-07 04:00:00 +0000136 : Records(records), RegBank(0), SchedModels(0) {
Chris Lattner45872072003-08-07 05:38:11 +0000137 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000138 if (Targets.size() == 0)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000139 PrintFatalError("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000140 if (Targets.size() != 1)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000141 PrintFatalError("ERROR: Multiple subclasses of Target defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000142 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000143}
144
Andrew Trick2661b412012-07-07 04:00:00 +0000145CodeGenTarget::~CodeGenTarget() {
146 delete RegBank;
147 delete SchedModels;
148}
Chris Lattner45872072003-08-07 05:38:11 +0000149
150const std::string &CodeGenTarget::getName() const {
151 return TargetRec->getName();
152}
153
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000154std::string CodeGenTarget::getInstNamespace() const {
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000155 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
Chris Lattner6a91b182010-03-19 01:00:55 +0000156 // Make sure not to pick up "TargetOpcode" by accidentally getting
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000157 // the namespace off the PHI instruction or something.
Chris Lattner6a91b182010-03-19 01:00:55 +0000158 if ((*i)->Namespace != "TargetOpcode")
159 return (*i)->Namespace;
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000160 }
161
Chris Lattner6a91b182010-03-19 01:00:55 +0000162 return "";
Dan Gohman1e0ee4b2008-08-20 21:45:57 +0000163}
164
Chris Lattner45872072003-08-07 05:38:11 +0000165Record *CodeGenTarget::getInstructionSet() const {
166 return TargetRec->getValueAsDef("InstructionSet");
167}
Brian Gaeked0fde302003-11-11 22:41:34 +0000168
Chris Lattnerf30187a2010-03-19 00:07:20 +0000169
Daniel Dunbar0e2771f2009-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 Sonnenberger61131ab2012-10-25 20:33:17 +0000175 PrintFatalError("Target does not have an AsmParser #" + utostr(AsmParserNum) + "!");
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000176 return LI[AsmParserNum];
177}
178
Devang Patel0dbcada2012-01-09 19:13:28 +0000179/// getAsmParserVariant - Return the AssmblyParserVariant definition for
180/// this target.
181///
182Record *CodeGenTarget::getAsmParserVariant(unsigned i) const {
Andrew Trick06495cd2012-07-07 03:59:51 +0000183 std::vector<Record*> LI =
Devang Patel0dbcada2012-01-09 19:13:28 +0000184 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
185 if (i >= LI.size())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000186 PrintFatalError("Target does not have an AsmParserVariant #" + utostr(i) + "!");
Devang Patel0dbcada2012-01-09 19:13:28 +0000187 return LI[i];
188}
189
Andrew Trick06495cd2012-07-07 03:59:51 +0000190/// getAsmParserVariantCount - Return the AssmblyParserVariant definition
Devang Patel0dbcada2012-01-09 19:13:28 +0000191/// available for this target.
192///
193unsigned CodeGenTarget::getAsmParserVariantCount() const {
Andrew Trick06495cd2012-07-07 03:59:51 +0000194 std::vector<Record*> LI =
Devang Patel0dbcada2012-01-09 19:13:28 +0000195 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
196 return LI.size();
197}
198
Chris Lattner175580c2004-08-14 22:50:53 +0000199/// getAsmWriter - Return the AssemblyWriter definition for this target.
200///
201Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000202 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
203 if (AsmWriterNum >= LI.size())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000204 PrintFatalError("Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!");
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000205 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000206}
207
Jakob Stoklund Olesendc29c442011-06-10 18:40:00 +0000208CodeGenRegBank &CodeGenTarget::getRegBank() const {
209 if (!RegBank)
210 RegBank = new CodeGenRegBank(Records);
211 return *RegBank;
212}
213
Owen Andersonbea6f612011-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 Lattnerec6f0962010-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 Andersond2c69972012-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 Lattnerec6f0962010-11-02 18:10:06 +0000227}
228
Chris Lattner2cacec52010-03-15 06:00:16 +0000229std::vector<MVT::SimpleValueType> CodeGenTarget::
230getRegisterVTs(Record *R) const {
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000231 const CodeGenRegister *Reg = getRegBank().getReg(R);
Chris Lattner2cacec52010-03-15 06:00:16 +0000232 std::vector<MVT::SimpleValueType> Result;
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000233 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
Evan Cheng44a65fa2006-05-16 07:05:30 +0000234 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000235 const CodeGenRegisterClass &RC = *RCs[i];
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000236 if (RC.contains(Reg)) {
Jakob Stoklund Olesen26369a92013-03-17 17:26:09 +0000237 ArrayRef<MVT::SimpleValueType> InVTs = RC.getValueTypes();
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000238 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Evan Cheng44a65fa2006-05-16 07:05:30 +0000239 }
240 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000241
Chris Lattner0d7952e2010-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 Cheng44a65fa2006-05-16 07:05:30 +0000245 return Result;
246}
247
248
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000249void CodeGenTarget::ReadLegalValueTypes() const {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000250 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000251 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Jakob Stoklund Olesen29f018c2011-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 Grosbach510207c2011-03-11 01:27:24 +0000254
Chris Lattner75ee2eb2005-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 Lattnere9f4ba82005-09-08 21:43:21 +0000260}
Chris Lattner056afef2004-08-21 04:05:00 +0000261
Andrew Trick2661b412012-07-07 04:00:00 +0000262CodeGenSchedModels &CodeGenTarget::getSchedModels() const {
263 if (!SchedModels)
264 SchedModels = new CodeGenSchedModels(Records, *this);
265 return *SchedModels;
266}
Chris Lattner175580c2004-08-14 22:50:53 +0000267
Chris Lattnerec352402004-08-01 05:04:00 +0000268void CodeGenTarget::ReadInstructions() const {
269 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000270 if (Insts.size() <= 2)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000271 PrintFatalError("No 'Instruction' subclasses defined!");
Chris Lattnerec352402004-08-01 05:04:00 +0000272
Chris Lattneraa77d772006-01-27 01:45:06 +0000273 // Parse the instructions defined in the .td file.
Chris Lattnerf7808112010-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 Lattnerec352402004-08-01 05:04:00 +0000276}
277
Chris Lattnere4e8bb12010-03-19 00:23:20 +0000278static const CodeGenInstruction *
279GetInstByName(const char *Name,
Jim Grosbach510207c2011-03-11 01:27:24 +0000280 const DenseMap<const Record*, CodeGenInstruction*> &Insts,
Chris Lattner67db8832010-12-13 00:23:57 +0000281 RecordKeeper &Records) {
Chris Lattnere14d2e22010-03-19 01:07:44 +0000282 const Record *Rec = Records.getDef(Name);
Jim Grosbach510207c2011-03-11 01:27:24 +0000283
Chris Lattnere14d2e22010-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 Sonnenberger61131ab2012-10-25 20:33:17 +0000287 PrintFatalError(std::string("Could not find '") + Name + "' instruction!");
Chris Lattnere14d2e22010-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 Lattnere4e8bb12010-03-19 00:23:20 +0000300}
301
Chris Lattnerd6488672005-01-22 18:58:51 +0000302/// getInstructionsByEnumValue - Return all of the instructions defined by the
303/// target, ordered by their enum value.
Chris Lattner6a91b182010-03-19 01:00:55 +0000304void CodeGenTarget::ComputeInstrsByEnum() const {
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000305 // The ordering here must match the ordering in TargetOpcodes.h.
Craig Topper3c70fcf2013-07-15 08:02:13 +0000306 static const char *const FixedInstrs[] = {
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000307 "PHI",
308 "INLINEASM",
Bill Wendling7431bea2010-07-16 22:20:36 +0000309 "PROLOG_LABEL",
Jakob Stoklund Olesen65766ce2010-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 Olesena4e1ba52010-07-02 22:29:50 +0000320 "COPY",
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000321 "BUNDLE",
Nadav Rotemc05d3062012-09-06 09:17:37 +0000322 "LIFETIME_START",
323 "LIFETIME_END",
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000324 0
325 };
Chris Lattnere14d2e22010-03-19 01:07:44 +0000326 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000327 for (const char *const *p = FixedInstrs; *p; ++p) {
Chris Lattner67db8832010-12-13 00:23:57 +0000328 const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000329 assert(Instr && "Missing target independent instruction");
330 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
331 InstrsByEnum.push_back(Instr);
332 }
Chris Lattnere14d2e22010-03-19 01:07:44 +0000333 unsigned EndOfPredefines = InstrsByEnum.size();
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000334
Chris Lattnere14d2e22010-03-19 01:07:44 +0000335 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
336 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
337 const CodeGenInstruction *CGI = I->second;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000338 if (CGI->Namespace != "TargetOpcode")
Chris Lattner6a91b182010-03-19 01:00:55 +0000339 InstrsByEnum.push_back(CGI);
340 }
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000341
342 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
343
Chris Lattnere14d2e22010-03-19 01:07:44 +0000344 // All of the instructions are now in random order based on the map iteration.
345 // Sort them by name.
346 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(),
347 SortInstByName());
Chris Lattnerd6488672005-01-22 18:58:51 +0000348}
349
350
Misha Brukman35e83cc2004-10-14 05:50:43 +0000351/// isLittleEndianEncoding - Return whether this target encodes its instruction
352/// in little-endian format, i.e. bits laid out in the order [0..n]
353///
354bool CodeGenTarget::isLittleEndianEncoding() const {
355 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
356}
357
Jakob Stoklund Olesenf104bf62012-08-23 19:34:41 +0000358/// guessInstructionProperties - Return true if it's OK to guess instruction
359/// properties instead of raising an error.
360///
361/// This is configurable as a temporary migration aid. It will eventually be
362/// permanently false.
363bool CodeGenTarget::guessInstructionProperties() const {
364 return getInstructionSet()->getValueAsBit("guessInstructionProperties");
365}
366
Evan Cheng0fc71982005-12-08 02:00:36 +0000367//===----------------------------------------------------------------------===//
368// ComplexPattern implementation
369//
370ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000371 Ty = ::getValueType(R->getValueAsDef("Ty"));
372 NumOperands = R->getValueAsInt("NumOperands");
373 SelectFunc = R->getValueAsString("SelectFunc");
374 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000375
376 // Parse the properties.
377 Properties = 0;
378 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
379 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
380 if (PropList[i]->getName() == "SDNPHasChain") {
381 Properties |= 1 << SDNPHasChain;
Chris Lattner036609b2010-12-23 18:28:41 +0000382 } else if (PropList[i]->getName() == "SDNPOptInGlue") {
383 Properties |= 1 << SDNPOptInGlue;
Chris Lattnerba7e7562008-01-10 07:59:24 +0000384 } else if (PropList[i]->getName() == "SDNPMayStore") {
385 Properties |= 1 << SDNPMayStore;
386 } else if (PropList[i]->getName() == "SDNPMayLoad") {
387 Properties |= 1 << SDNPMayLoad;
388 } else if (PropList[i]->getName() == "SDNPSideEffect") {
389 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000390 } else if (PropList[i]->getName() == "SDNPMemOperand") {
391 Properties |= 1 << SDNPMemOperand;
Chris Lattnere8cabf32010-03-19 05:07:09 +0000392 } else if (PropList[i]->getName() == "SDNPVariadic") {
393 Properties |= 1 << SDNPVariadic;
Chris Lattner52a261b2010-09-21 20:31:19 +0000394 } else if (PropList[i]->getName() == "SDNPWantRoot") {
395 Properties |= 1 << SDNPWantRoot;
396 } else if (PropList[i]->getName() == "SDNPWantParent") {
397 Properties |= 1 << SDNPWantParent;
Evan Cheng94b30402006-10-11 21:02:01 +0000398 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000399 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
400 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000401 exit(1);
402 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000403}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000404
Chris Lattner43fbbc32006-03-24 19:49:31 +0000405//===----------------------------------------------------------------------===//
406// CodeGenIntrinsic Implementation
407//===----------------------------------------------------------------------===//
408
Dale Johannesen49de9822009-02-05 01:49:45 +0000409std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
410 bool TargetOnly) {
Chris Lattner43fbbc32006-03-24 19:49:31 +0000411 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Jim Grosbach510207c2011-03-11 01:27:24 +0000412
Chris Lattner8850a1b2006-03-27 22:48:18 +0000413 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000414
Dale Johannesen49de9822009-02-05 01:49:45 +0000415 for (unsigned i = 0, e = I.size(); i != e; ++i) {
416 bool isTarget = I[i]->getValueAsBit("isTarget");
417 if (isTarget == TargetOnly)
418 Result.push_back(CodeGenIntrinsic(I[i]));
419 }
Chris Lattner8850a1b2006-03-27 22:48:18 +0000420 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000421}
422
Dan Gohmanee4fa192008-04-03 00:02:49 +0000423CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000424 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000425 std::string DefName = R->getName();
Dan Gohman7365c092010-08-05 23:36:21 +0000426 ModRef = ReadWriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000427 isOverloaded = false;
Evan Cheng6bd95672008-06-16 20:29:38 +0000428 isCommutative = false;
John McCallbd0fa4c2011-05-28 06:31:34 +0000429 canThrow = false;
Chris Lattner86208902012-05-27 23:20:41 +0000430 isNoReturn = false;
Jim Grosbach510207c2011-03-11 01:27:24 +0000431
432 if (DefName.size() <= 4 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000433 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000434 PrintFatalError("Intrinsic '" + DefName + "' does not start with 'int_'!");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000435
Chris Lattner43fbbc32006-03-24 19:49:31 +0000436 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000437
Chris Lattner43fbbc32006-03-24 19:49:31 +0000438 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
439 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000440
441 TargetPrefix = R->getValueAsString("TargetPrefix");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000442 Name = R->getValueAsString("LLVMName");
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000443
Chris Lattner43fbbc32006-03-24 19:49:31 +0000444 if (Name == "") {
445 // If an explicit name isn't specified, derive one from the DefName.
446 Name = "llvm.";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000447
Chris Lattner43fbbc32006-03-24 19:49:31 +0000448 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000449 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Chris Lattner43fbbc32006-03-24 19:49:31 +0000450 } else {
451 // Verify it starts with "llvm.".
Jim Grosbach510207c2011-03-11 01:27:24 +0000452 if (Name.size() <= 5 ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000453 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000454 PrintFatalError("Intrinsic '" + DefName + "'s name does not start with 'llvm.'!");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000455 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000456
Chris Lattner43fbbc32006-03-24 19:49:31 +0000457 // If TargetPrefix is specified, make sure that Name starts with
458 // "llvm.<targetprefix>.".
459 if (!TargetPrefix.empty()) {
460 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000461 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
462 != (TargetPrefix + "."))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000463 PrintFatalError("Intrinsic '" + DefName + "' does not start with 'llvm." +
464 TargetPrefix + ".'!");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000465 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000466
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000467 // Parse the list of return types.
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 std::vector<MVT::SimpleValueType> OverloadedVTs;
David Greene05bce0b2011-07-29 22:43:06 +0000469 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000470 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000471 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000472 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000474 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilsond305e702009-04-16 21:51:05 +0000475 unsigned MatchTy = TyEl->getValueAsInt("Number");
476 assert(MatchTy < OverloadedVTs.size() &&
477 "Invalid matching number!");
478 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000479 // It only makes sense to use the extended and truncated vector element
480 // variants with iAny types; otherwise, if the intrinsic is not
481 // overloaded, all the types can be specified directly.
482 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
483 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000485 "Expected iAny or vAny type");
Bob Wilsond305e702009-04-16 21:51:05 +0000486 } else {
Bob Wilsonbc039792009-01-07 00:09:01 +0000487 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsond305e702009-04-16 21:51:05 +0000488 }
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000489 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000490 OverloadedVTs.push_back(VT);
Chris Lattner9972aff2010-03-23 23:46:27 +0000491 isOverloaded = true;
Bob Wilsond305e702009-04-16 21:51:05 +0000492 }
Chris Lattner9972aff2010-03-23 23:46:27 +0000493
494 // Reject invalid types.
495 if (VT == MVT::isVoid)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000496 PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
Jim Grosbach510207c2011-03-11 01:27:24 +0000497
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000498 IS.RetVTs.push_back(VT);
499 IS.RetTypeDefs.push_back(TyEl);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000500 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000501
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000502 // Parse the list of parameter types.
503 TypeList = R->getValueAsListInit("ParamTypes");
504 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
505 Record *TyEl = TypeList->getElementAsRecord(i);
506 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 MVT::SimpleValueType VT;
Bob Wilsonbc039792009-01-07 00:09:01 +0000508 if (TyEl->isSubClassOf("LLVMMatchType")) {
509 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilsond305e702009-04-16 21:51:05 +0000510 assert(MatchTy < OverloadedVTs.size() &&
511 "Invalid matching number!");
512 VT = OverloadedVTs[MatchTy];
Bob Wilsonbc039792009-01-07 00:09:01 +0000513 // It only makes sense to use the extended and truncated vector element
514 // variants with iAny types; otherwise, if the intrinsic is not
515 // overloaded, all the types can be specified directly.
516 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
517 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson9b6a53a2009-08-11 05:03:38 +0000519 "Expected iAny or vAny type");
Bob Wilsonbc039792009-01-07 00:09:01 +0000520 } else
521 VT = getValueType(TyEl->getValueAsDef("VT"));
Jim Grosbach510207c2011-03-11 01:27:24 +0000522
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000523 if (EVT(VT).isOverloaded()) {
Bob Wilsond305e702009-04-16 21:51:05 +0000524 OverloadedVTs.push_back(VT);
Chris Lattner9972aff2010-03-23 23:46:27 +0000525 isOverloaded = true;
Bob Wilsond305e702009-04-16 21:51:05 +0000526 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000527
Chris Lattner9972aff2010-03-23 23:46:27 +0000528 // Reject invalid types.
529 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000530 PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
Jim Grosbach510207c2011-03-11 01:27:24 +0000531
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000532 IS.ParamVTs.push_back(VT);
533 IS.ParamTypeDefs.push_back(TyEl);
534 }
535
Chris Lattner43fbbc32006-03-24 19:49:31 +0000536 // Parse the intrinsic properties.
David Greene05bce0b2011-07-29 22:43:06 +0000537 ListInit *PropList = R->getValueAsListInit("Properties");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000538 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000539 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000540 assert(Property->isSubClassOf("IntrinsicProperty") &&
541 "Expected a property!");
Jim Grosbach510207c2011-03-11 01:27:24 +0000542
Chris Lattner4b2362e2006-04-10 22:02:59 +0000543 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000544 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000545 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000546 ModRef = ReadArgMem;
547 else if (Property->getName() == "IntrReadMem")
548 ModRef = ReadMem;
Dan Gohman7365c092010-08-05 23:36:21 +0000549 else if (Property->getName() == "IntrReadWriteArgMem")
550 ModRef = ReadWriteArgMem;
Evan Cheng6bd95672008-06-16 20:29:38 +0000551 else if (Property->getName() == "Commutative")
552 isCommutative = true;
John McCallbd0fa4c2011-05-28 06:31:34 +0000553 else if (Property->getName() == "Throws")
554 canThrow = true;
Chris Lattner86208902012-05-27 23:20:41 +0000555 else if (Property->getName() == "IntrNoReturn")
556 isNoReturn = true;
Chris Lattnera62c3022009-01-12 01:12:03 +0000557 else if (Property->isSubClassOf("NoCapture")) {
558 unsigned ArgNo = Property->getValueAsInt("ArgNo");
559 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
Nick Lewyckydc897372013-07-06 00:29:58 +0000560 } else if (Property->isSubClassOf("ReadOnly")) {
561 unsigned ArgNo = Property->getValueAsInt("ArgNo");
562 ArgumentAttributes.push_back(std::make_pair(ArgNo, ReadOnly));
563 } else if (Property->isSubClassOf("ReadNone")) {
564 unsigned ArgNo = Property->getValueAsInt("ArgNo");
565 ArgumentAttributes.push_back(std::make_pair(ArgNo, ReadNone));
Chris Lattnera62c3022009-01-12 01:12:03 +0000566 } else
Craig Topper655b8de2012-02-05 07:21:30 +0000567 llvm_unreachable("Unknown property!");
Chris Lattner43fbbc32006-03-24 19:49:31 +0000568 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000569
570 // Sort the argument attributes for later benefit.
571 std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end());
Chris Lattner43fbbc32006-03-24 19:49:31 +0000572}