1 //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class wraps target description classes used by the various code
11 // generation TableGen backends. This makes it easier to access the data and
12 // provides a single place that needs to check it for validity. All of these
13 // classes abort on error conditions.
15 //===----------------------------------------------------------------------===//
17 #include "CodeGenTarget.h"
18 #include "CodeGenIntrinsics.h"
19 #include "CodeGenSchedule.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/TableGen/Error.h"
24 #include "llvm/TableGen/Record.h"
28 static cl::opt<unsigned>
29 AsmParserNum("asmparsernum", cl::init(0),
30 cl::desc("Make -gen-asm-parser emit assembly parser #N"));
32 static cl::opt<unsigned>
33 AsmWriterNum("asmwriternum", cl::init(0),
34 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
36 /// getValueType - Return the MVT::SimpleValueType that the specified TableGen
37 /// record corresponds to.
38 MVT::SimpleValueType llvm::getValueType(Record *Rec) {
39 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
42 std::string llvm::getName(MVT::SimpleValueType T) {
44 case MVT::Other: return "UNKNOWN";
45 case MVT::iPTR: return "TLI.getPointerTy()";
46 case MVT::iPTRAny: return "TLI.getPointerTy()";
47 default: return getEnumName(T);
51 std::string llvm::getEnumName(MVT::SimpleValueType T) {
53 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";
63 case MVT::f16: return "MVT::f16";
64 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";
68 case MVT::ppcf128: return "MVT::ppcf128";
69 case MVT::x86mmx: return "MVT::x86mmx";
70 case MVT::Glue: return "MVT::Glue";
71 case MVT::isVoid: return "MVT::isVoid";
72 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";
76 case MVT::v32i1: return "MVT::v32i1";
77 case MVT::v64i1: return "MVT::v64i1";
78 case MVT::v2i8: return "MVT::v2i8";
79 case MVT::v4i8: return "MVT::v4i8";
80 case MVT::v8i8: return "MVT::v8i8";
81 case MVT::v16i8: return "MVT::v16i8";
82 case MVT::v32i8: return "MVT::v32i8";
83 case MVT::v64i8: return "MVT::v64i8";
84 case MVT::v1i16: return "MVT::v1i16";
85 case MVT::v2i16: return "MVT::v2i16";
86 case MVT::v4i16: return "MVT::v4i16";
87 case MVT::v8i16: return "MVT::v8i16";
88 case MVT::v16i16: return "MVT::v16i16";
89 case MVT::v32i16: return "MVT::v32i16";
90 case MVT::v1i32: return "MVT::v1i32";
91 case MVT::v2i32: return "MVT::v2i32";
92 case MVT::v4i32: return "MVT::v4i32";
93 case MVT::v8i32: return "MVT::v8i32";
94 case MVT::v16i32: return "MVT::v16i32";
95 case MVT::v1i64: return "MVT::v1i64";
96 case MVT::v2i64: return "MVT::v2i64";
97 case MVT::v4i64: return "MVT::v4i64";
98 case MVT::v8i64: return "MVT::v8i64";
99 case MVT::v16i64: return "MVT::v16i64";
100 case MVT::v2f16: return "MVT::v2f16";
101 case MVT::v8f16: return "MVT::v8f16";
102 case MVT::v2f32: return "MVT::v2f32";
103 case MVT::v4f32: return "MVT::v4f32";
104 case MVT::v8f32: return "MVT::v8f32";
105 case MVT::v16f32: return "MVT::v16f32";
106 case MVT::v2f64: return "MVT::v2f64";
107 case MVT::v4f64: return "MVT::v4f64";
108 case MVT::v8f64: return "MVT::v8f64";
109 case MVT::Metadata: return "MVT::Metadata";
110 case MVT::iPTR: return "MVT::iPTR";
111 case MVT::iPTRAny: return "MVT::iPTRAny";
112 case MVT::Untyped: return "MVT::Untyped";
113 default: llvm_unreachable("ILLEGAL VALUE TYPE!");
117 /// getQualifiedName - Return the name of the specified record, with a
118 /// namespace qualifier if the record contains one.
120 std::string llvm::getQualifiedName(const Record *R) {
121 std::string Namespace;
122 if (R->getValue("Namespace"))
123 Namespace = R->getValueAsString("Namespace");
124 if (Namespace.empty()) return R->getName();
125 return Namespace + "::" + R->getName();
129 /// getTarget - Return the current instance of the Target class.
131 CodeGenTarget::CodeGenTarget(RecordKeeper &records)
132 : Records(records), RegBank(0), SchedModels(0) {
133 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
134 if (Targets.size() == 0)
135 PrintFatalError("ERROR: No 'Target' subclasses defined!");
136 if (Targets.size() != 1)
137 PrintFatalError("ERROR: Multiple subclasses of Target defined!");
138 TargetRec = Targets[0];
141 CodeGenTarget::~CodeGenTarget() {
146 const std::string &CodeGenTarget::getName() const {
147 return TargetRec->getName();
150 std::string CodeGenTarget::getInstNamespace() const {
151 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
152 // Make sure not to pick up "TargetOpcode" by accidentally getting
153 // the namespace off the PHI instruction or something.
154 if ((*i)->Namespace != "TargetOpcode")
155 return (*i)->Namespace;
161 Record *CodeGenTarget::getInstructionSet() const {
162 return TargetRec->getValueAsDef("InstructionSet");
166 /// getAsmParser - Return the AssemblyParser definition for this target.
168 Record *CodeGenTarget::getAsmParser() const {
169 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
170 if (AsmParserNum >= LI.size())
171 PrintFatalError("Target does not have an AsmParser #" + utostr(AsmParserNum) + "!");
172 return LI[AsmParserNum];
175 /// getAsmParserVariant - Return the AssmblyParserVariant definition for
178 Record *CodeGenTarget::getAsmParserVariant(unsigned i) const {
179 std::vector<Record*> LI =
180 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
182 PrintFatalError("Target does not have an AsmParserVariant #" + utostr(i) + "!");
186 /// getAsmParserVariantCount - Return the AssmblyParserVariant definition
187 /// available for this target.
189 unsigned CodeGenTarget::getAsmParserVariantCount() const {
190 std::vector<Record*> LI =
191 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
195 /// getAsmWriter - Return the AssemblyWriter definition for this target.
197 Record *CodeGenTarget::getAsmWriter() const {
198 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
199 if (AsmWriterNum >= LI.size())
200 PrintFatalError("Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!");
201 return LI[AsmWriterNum];
204 CodeGenRegBank &CodeGenTarget::getRegBank() const {
206 RegBank = new CodeGenRegBank(Records);
210 void CodeGenTarget::ReadRegAltNameIndices() const {
211 RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex");
212 std::sort(RegAltNameIndices.begin(), RegAltNameIndices.end(), LessRecord());
215 /// getRegisterByName - If there is a register with the specific AsmName,
217 const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
218 const StringMap<CodeGenRegister*> &Regs = getRegBank().getRegistersByName();
219 StringMap<CodeGenRegister*>::const_iterator I = Regs.find(Name);
225 std::vector<MVT::SimpleValueType> CodeGenTarget::
226 getRegisterVTs(Record *R) const {
227 const CodeGenRegister *Reg = getRegBank().getReg(R);
228 std::vector<MVT::SimpleValueType> Result;
229 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
230 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
231 const CodeGenRegisterClass &RC = *RCs[i];
232 if (RC.contains(Reg)) {
233 ArrayRef<MVT::SimpleValueType> InVTs = RC.getValueTypes();
234 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
238 // Remove duplicates.
239 array_pod_sort(Result.begin(), Result.end());
240 Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
245 void CodeGenTarget::ReadLegalValueTypes() const {
246 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
247 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
248 for (unsigned ri = 0, re = RCs[i]->VTs.size(); ri != re; ++ri)
249 LegalValueTypes.push_back(RCs[i]->VTs[ri]);
251 // Remove duplicates.
252 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
253 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
254 LegalValueTypes.end()),
255 LegalValueTypes.end());
258 CodeGenSchedModels &CodeGenTarget::getSchedModels() const {
260 SchedModels = new CodeGenSchedModels(Records, *this);
264 void CodeGenTarget::ReadInstructions() const {
265 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
266 if (Insts.size() <= 2)
267 PrintFatalError("No 'Instruction' subclasses defined!");
269 // Parse the instructions defined in the .td file.
270 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
271 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i]);
274 static const CodeGenInstruction *
275 GetInstByName(const char *Name,
276 const DenseMap<const Record*, CodeGenInstruction*> &Insts,
277 RecordKeeper &Records) {
278 const Record *Rec = Records.getDef(Name);
280 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
282 if (Rec == 0 || I == Insts.end())
283 PrintFatalError(std::string("Could not find '") + Name + "' instruction!");
288 /// SortInstByName - Sorting predicate to sort instructions by name.
290 struct SortInstByName {
291 bool operator()(const CodeGenInstruction *Rec1,
292 const CodeGenInstruction *Rec2) const {
293 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
298 /// getInstructionsByEnumValue - Return all of the instructions defined by the
299 /// target, ordered by their enum value.
300 void CodeGenTarget::ComputeInstrsByEnum() const {
301 // The ordering here must match the ordering in TargetOpcodes.h.
302 static const char *const FixedInstrs[] = {
322 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
323 for (const char *const *p = FixedInstrs; *p; ++p) {
324 const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
325 assert(Instr && "Missing target independent instruction");
326 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
327 InstrsByEnum.push_back(Instr);
329 unsigned EndOfPredefines = InstrsByEnum.size();
331 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
332 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
333 const CodeGenInstruction *CGI = I->second;
334 if (CGI->Namespace != "TargetOpcode")
335 InstrsByEnum.push_back(CGI);
338 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
340 // All of the instructions are now in random order based on the map iteration.
341 // Sort them by name.
342 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(),
347 /// isLittleEndianEncoding - Return whether this target encodes its instruction
348 /// in little-endian format, i.e. bits laid out in the order [0..n]
350 bool CodeGenTarget::isLittleEndianEncoding() const {
351 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
354 /// guessInstructionProperties - Return true if it's OK to guess instruction
355 /// properties instead of raising an error.
357 /// This is configurable as a temporary migration aid. It will eventually be
358 /// permanently false.
359 bool CodeGenTarget::guessInstructionProperties() const {
360 return getInstructionSet()->getValueAsBit("guessInstructionProperties");
363 //===----------------------------------------------------------------------===//
364 // ComplexPattern implementation
366 ComplexPattern::ComplexPattern(Record *R) {
367 Ty = ::getValueType(R->getValueAsDef("Ty"));
368 NumOperands = R->getValueAsInt("NumOperands");
369 SelectFunc = R->getValueAsString("SelectFunc");
370 RootNodes = R->getValueAsListOfDefs("RootNodes");
372 // Parse the properties.
374 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
375 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
376 if (PropList[i]->getName() == "SDNPHasChain") {
377 Properties |= 1 << SDNPHasChain;
378 } else if (PropList[i]->getName() == "SDNPOptInGlue") {
379 Properties |= 1 << SDNPOptInGlue;
380 } else if (PropList[i]->getName() == "SDNPMayStore") {
381 Properties |= 1 << SDNPMayStore;
382 } else if (PropList[i]->getName() == "SDNPMayLoad") {
383 Properties |= 1 << SDNPMayLoad;
384 } else if (PropList[i]->getName() == "SDNPSideEffect") {
385 Properties |= 1 << SDNPSideEffect;
386 } else if (PropList[i]->getName() == "SDNPMemOperand") {
387 Properties |= 1 << SDNPMemOperand;
388 } else if (PropList[i]->getName() == "SDNPVariadic") {
389 Properties |= 1 << SDNPVariadic;
390 } else if (PropList[i]->getName() == "SDNPWantRoot") {
391 Properties |= 1 << SDNPWantRoot;
392 } else if (PropList[i]->getName() == "SDNPWantParent") {
393 Properties |= 1 << SDNPWantParent;
395 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
396 << "' on ComplexPattern '" << R->getName() << "'!\n";
401 //===----------------------------------------------------------------------===//
402 // CodeGenIntrinsic Implementation
403 //===----------------------------------------------------------------------===//
405 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
407 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
409 std::vector<CodeGenIntrinsic> Result;
411 for (unsigned i = 0, e = I.size(); i != e; ++i) {
412 bool isTarget = I[i]->getValueAsBit("isTarget");
413 if (isTarget == TargetOnly)
414 Result.push_back(CodeGenIntrinsic(I[i]));
419 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
421 std::string DefName = R->getName();
422 ModRef = ReadWriteMem;
423 isOverloaded = false;
424 isCommutative = false;
428 if (DefName.size() <= 4 ||
429 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
430 PrintFatalError("Intrinsic '" + DefName + "' does not start with 'int_'!");
432 EnumName = std::string(DefName.begin()+4, DefName.end());
434 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
435 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
437 TargetPrefix = R->getValueAsString("TargetPrefix");
438 Name = R->getValueAsString("LLVMName");
441 // If an explicit name isn't specified, derive one from the DefName.
444 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
445 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
447 // Verify it starts with "llvm.".
448 if (Name.size() <= 5 ||
449 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
450 PrintFatalError("Intrinsic '" + DefName + "'s name does not start with 'llvm.'!");
453 // If TargetPrefix is specified, make sure that Name starts with
454 // "llvm.<targetprefix>.".
455 if (!TargetPrefix.empty()) {
456 if (Name.size() < 6+TargetPrefix.size() ||
457 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
458 != (TargetPrefix + "."))
459 PrintFatalError("Intrinsic '" + DefName + "' does not start with 'llvm." +
460 TargetPrefix + ".'!");
463 // Parse the list of return types.
464 std::vector<MVT::SimpleValueType> OverloadedVTs;
465 ListInit *TypeList = R->getValueAsListInit("RetTypes");
466 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
467 Record *TyEl = TypeList->getElementAsRecord(i);
468 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
469 MVT::SimpleValueType VT;
470 if (TyEl->isSubClassOf("LLVMMatchType")) {
471 unsigned MatchTy = TyEl->getValueAsInt("Number");
472 assert(MatchTy < OverloadedVTs.size() &&
473 "Invalid matching number!");
474 VT = OverloadedVTs[MatchTy];
475 // It only makes sense to use the extended and truncated vector element
476 // variants with iAny types; otherwise, if the intrinsic is not
477 // overloaded, all the types can be specified directly.
478 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
479 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
480 VT == MVT::iAny || VT == MVT::vAny) &&
481 "Expected iAny or vAny type");
483 VT = getValueType(TyEl->getValueAsDef("VT"));
485 if (EVT(VT).isOverloaded()) {
486 OverloadedVTs.push_back(VT);
490 // Reject invalid types.
491 if (VT == MVT::isVoid)
492 PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
494 IS.RetVTs.push_back(VT);
495 IS.RetTypeDefs.push_back(TyEl);
498 // Parse the list of parameter types.
499 TypeList = R->getValueAsListInit("ParamTypes");
500 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
501 Record *TyEl = TypeList->getElementAsRecord(i);
502 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
503 MVT::SimpleValueType VT;
504 if (TyEl->isSubClassOf("LLVMMatchType")) {
505 unsigned MatchTy = TyEl->getValueAsInt("Number");
506 assert(MatchTy < OverloadedVTs.size() &&
507 "Invalid matching number!");
508 VT = OverloadedVTs[MatchTy];
509 // It only makes sense to use the extended and truncated vector element
510 // variants with iAny types; otherwise, if the intrinsic is not
511 // overloaded, all the types can be specified directly.
512 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
513 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
514 VT == MVT::iAny || VT == MVT::vAny) &&
515 "Expected iAny or vAny type");
517 VT = getValueType(TyEl->getValueAsDef("VT"));
519 if (EVT(VT).isOverloaded()) {
520 OverloadedVTs.push_back(VT);
524 // Reject invalid types.
525 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
526 PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
528 IS.ParamVTs.push_back(VT);
529 IS.ParamTypeDefs.push_back(TyEl);
532 // Parse the intrinsic properties.
533 ListInit *PropList = R->getValueAsListInit("Properties");
534 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
535 Record *Property = PropList->getElementAsRecord(i);
536 assert(Property->isSubClassOf("IntrinsicProperty") &&
537 "Expected a property!");
539 if (Property->getName() == "IntrNoMem")
541 else if (Property->getName() == "IntrReadArgMem")
543 else if (Property->getName() == "IntrReadMem")
545 else if (Property->getName() == "IntrReadWriteArgMem")
546 ModRef = ReadWriteArgMem;
547 else if (Property->getName() == "Commutative")
548 isCommutative = true;
549 else if (Property->getName() == "Throws")
551 else if (Property->getName() == "IntrNoReturn")
553 else if (Property->isSubClassOf("NoCapture")) {
554 unsigned ArgNo = Property->getValueAsInt("ArgNo");
555 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
556 } else if (Property->isSubClassOf("ReadOnly")) {
557 unsigned ArgNo = Property->getValueAsInt("ArgNo");
558 ArgumentAttributes.push_back(std::make_pair(ArgNo, ReadOnly));
559 } else if (Property->isSubClassOf("ReadNone")) {
560 unsigned ArgNo = Property->getValueAsInt("ArgNo");
561 ArgumentAttributes.push_back(std::make_pair(ArgNo, ReadNone));
563 llvm_unreachable("Unknown property!");
566 // Sort the argument attributes for later benefit.
567 std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end());