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::v1i8: return "MVT::v1i8";
79 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";
84 case MVT::v64i8: return "MVT::v64i8";
85 case MVT::v1i16: return "MVT::v1i16";
86 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";
90 case MVT::v32i16: return "MVT::v32i16";
91 case MVT::v1i32: return "MVT::v1i32";
92 case MVT::v2i32: return "MVT::v2i32";
93 case MVT::v4i32: return "MVT::v4i32";
94 case MVT::v8i32: return "MVT::v8i32";
95 case MVT::v16i32: return "MVT::v16i32";
96 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";
100 case MVT::v16i64: return "MVT::v16i64";
101 case MVT::v2f16: return "MVT::v2f16";
102 case MVT::v4f16: return "MVT::v4f16";
103 case MVT::v8f16: return "MVT::v8f16";
104 case MVT::v1f32: return "MVT::v1f32";
105 case MVT::v2f32: return "MVT::v2f32";
106 case MVT::v4f32: return "MVT::v4f32";
107 case MVT::v8f32: return "MVT::v8f32";
108 case MVT::v16f32: return "MVT::v16f32";
109 case MVT::v1f64: return "MVT::v1f64";
110 case MVT::v2f64: return "MVT::v2f64";
111 case MVT::v4f64: return "MVT::v4f64";
112 case MVT::v8f64: return "MVT::v8f64";
113 case MVT::Metadata: return "MVT::Metadata";
114 case MVT::iPTR: return "MVT::iPTR";
115 case MVT::iPTRAny: return "MVT::iPTRAny";
116 case MVT::Untyped: return "MVT::Untyped";
117 default: llvm_unreachable("ILLEGAL VALUE TYPE!");
121 /// getQualifiedName - Return the name of the specified record, with a
122 /// namespace qualifier if the record contains one.
124 std::string llvm::getQualifiedName(const Record *R) {
125 std::string Namespace;
126 if (R->getValue("Namespace"))
127 Namespace = R->getValueAsString("Namespace");
128 if (Namespace.empty()) return R->getName();
129 return Namespace + "::" + R->getName();
133 /// getTarget - Return the current instance of the Target class.
135 CodeGenTarget::CodeGenTarget(RecordKeeper &records)
136 : Records(records), RegBank(0), SchedModels(0) {
137 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
138 if (Targets.size() == 0)
139 PrintFatalError("ERROR: No 'Target' subclasses defined!");
140 if (Targets.size() != 1)
141 PrintFatalError("ERROR: Multiple subclasses of Target defined!");
142 TargetRec = Targets[0];
145 CodeGenTarget::~CodeGenTarget() {
150 const std::string &CodeGenTarget::getName() const {
151 return TargetRec->getName();
154 std::string CodeGenTarget::getInstNamespace() const {
155 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
156 // Make sure not to pick up "TargetOpcode" by accidentally getting
157 // the namespace off the PHI instruction or something.
158 if ((*i)->Namespace != "TargetOpcode")
159 return (*i)->Namespace;
165 Record *CodeGenTarget::getInstructionSet() const {
166 return TargetRec->getValueAsDef("InstructionSet");
170 /// getAsmParser - Return the AssemblyParser definition for this target.
172 Record *CodeGenTarget::getAsmParser() const {
173 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
174 if (AsmParserNum >= LI.size())
175 PrintFatalError("Target does not have an AsmParser #" + utostr(AsmParserNum) + "!");
176 return LI[AsmParserNum];
179 /// getAsmParserVariant - Return the AssmblyParserVariant definition for
182 Record *CodeGenTarget::getAsmParserVariant(unsigned i) const {
183 std::vector<Record*> LI =
184 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
186 PrintFatalError("Target does not have an AsmParserVariant #" + utostr(i) + "!");
190 /// getAsmParserVariantCount - Return the AssmblyParserVariant definition
191 /// available for this target.
193 unsigned CodeGenTarget::getAsmParserVariantCount() const {
194 std::vector<Record*> LI =
195 TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
199 /// getAsmWriter - Return the AssemblyWriter definition for this target.
201 Record *CodeGenTarget::getAsmWriter() const {
202 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
203 if (AsmWriterNum >= LI.size())
204 PrintFatalError("Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!");
205 return LI[AsmWriterNum];
208 CodeGenRegBank &CodeGenTarget::getRegBank() const {
210 RegBank = new CodeGenRegBank(Records);
214 void CodeGenTarget::ReadRegAltNameIndices() const {
215 RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex");
216 std::sort(RegAltNameIndices.begin(), RegAltNameIndices.end(), LessRecord());
219 /// getRegisterByName - If there is a register with the specific AsmName,
221 const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
222 const StringMap<CodeGenRegister*> &Regs = getRegBank().getRegistersByName();
223 StringMap<CodeGenRegister*>::const_iterator I = Regs.find(Name);
229 std::vector<MVT::SimpleValueType> CodeGenTarget::
230 getRegisterVTs(Record *R) const {
231 const CodeGenRegister *Reg = getRegBank().getReg(R);
232 std::vector<MVT::SimpleValueType> Result;
233 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
234 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
235 const CodeGenRegisterClass &RC = *RCs[i];
236 if (RC.contains(Reg)) {
237 ArrayRef<MVT::SimpleValueType> InVTs = RC.getValueTypes();
238 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
242 // Remove duplicates.
243 array_pod_sort(Result.begin(), Result.end());
244 Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
249 void CodeGenTarget::ReadLegalValueTypes() const {
250 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses();
251 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
252 for (unsigned ri = 0, re = RCs[i]->VTs.size(); ri != re; ++ri)
253 LegalValueTypes.push_back(RCs[i]->VTs[ri]);
255 // Remove duplicates.
256 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
257 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
258 LegalValueTypes.end()),
259 LegalValueTypes.end());
262 CodeGenSchedModels &CodeGenTarget::getSchedModels() const {
264 SchedModels = new CodeGenSchedModels(Records, *this);
268 void CodeGenTarget::ReadInstructions() const {
269 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
270 if (Insts.size() <= 2)
271 PrintFatalError("No 'Instruction' subclasses defined!");
273 // Parse the instructions defined in the .td file.
274 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
275 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i]);
278 static const CodeGenInstruction *
279 GetInstByName(const char *Name,
280 const DenseMap<const Record*, CodeGenInstruction*> &Insts,
281 RecordKeeper &Records) {
282 const Record *Rec = Records.getDef(Name);
284 DenseMap<const Record*, CodeGenInstruction*>::const_iterator
286 if (Rec == 0 || I == Insts.end())
287 PrintFatalError(std::string("Could not find '") + Name + "' instruction!");
292 /// SortInstByName - Sorting predicate to sort instructions by name.
294 struct SortInstByName {
295 bool operator()(const CodeGenInstruction *Rec1,
296 const CodeGenInstruction *Rec2) const {
297 return Rec1->TheDef->getName() < Rec2->TheDef->getName();
302 /// getInstructionsByEnumValue - Return all of the instructions defined by the
303 /// target, ordered by their enum value.
304 void CodeGenTarget::ComputeInstrsByEnum() const {
305 // The ordering here must match the ordering in TargetOpcodes.h.
306 static const char *const FixedInstrs[] = {
326 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
327 for (const char *const *p = FixedInstrs; *p; ++p) {
328 const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
329 assert(Instr && "Missing target independent instruction");
330 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
331 InstrsByEnum.push_back(Instr);
333 unsigned EndOfPredefines = InstrsByEnum.size();
335 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator
336 I = Insts.begin(), E = Insts.end(); I != E; ++I) {
337 const CodeGenInstruction *CGI = I->second;
338 if (CGI->Namespace != "TargetOpcode")
339 InstrsByEnum.push_back(CGI);
342 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
344 // 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(),
351 /// isLittleEndianEncoding - Return whether this target encodes its instruction
352 /// in little-endian format, i.e. bits laid out in the order [0..n]
354 bool CodeGenTarget::isLittleEndianEncoding() const {
355 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
358 /// guessInstructionProperties - Return true if it's OK to guess instruction
359 /// properties instead of raising an error.
361 /// This is configurable as a temporary migration aid. It will eventually be
362 /// permanently false.
363 bool CodeGenTarget::guessInstructionProperties() const {
364 return getInstructionSet()->getValueAsBit("guessInstructionProperties");
367 //===----------------------------------------------------------------------===//
368 // ComplexPattern implementation
370 ComplexPattern::ComplexPattern(Record *R) {
371 Ty = ::getValueType(R->getValueAsDef("Ty"));
372 NumOperands = R->getValueAsInt("NumOperands");
373 SelectFunc = R->getValueAsString("SelectFunc");
374 RootNodes = R->getValueAsListOfDefs("RootNodes");
376 // Parse the properties.
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;
382 } else if (PropList[i]->getName() == "SDNPOptInGlue") {
383 Properties |= 1 << SDNPOptInGlue;
384 } 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;
390 } else if (PropList[i]->getName() == "SDNPMemOperand") {
391 Properties |= 1 << SDNPMemOperand;
392 } else if (PropList[i]->getName() == "SDNPVariadic") {
393 Properties |= 1 << SDNPVariadic;
394 } else if (PropList[i]->getName() == "SDNPWantRoot") {
395 Properties |= 1 << SDNPWantRoot;
396 } else if (PropList[i]->getName() == "SDNPWantParent") {
397 Properties |= 1 << SDNPWantParent;
399 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
400 << "' on ComplexPattern '" << R->getName() << "'!\n";
405 //===----------------------------------------------------------------------===//
406 // CodeGenIntrinsic Implementation
407 //===----------------------------------------------------------------------===//
409 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
411 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
413 std::vector<CodeGenIntrinsic> Result;
415 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]));
423 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
425 std::string DefName = R->getName();
426 ModRef = ReadWriteMem;
427 isOverloaded = false;
428 isCommutative = false;
432 if (DefName.size() <= 4 ||
433 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
434 PrintFatalError("Intrinsic '" + DefName + "' does not start with 'int_'!");
436 EnumName = std::string(DefName.begin()+4, DefName.end());
438 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
439 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
441 TargetPrefix = R->getValueAsString("TargetPrefix");
442 Name = R->getValueAsString("LLVMName");
445 // If an explicit name isn't specified, derive one from the DefName.
448 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
449 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
451 // Verify it starts with "llvm.".
452 if (Name.size() <= 5 ||
453 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
454 PrintFatalError("Intrinsic '" + DefName + "'s name does not start with 'llvm.'!");
457 // If TargetPrefix is specified, make sure that Name starts with
458 // "llvm.<targetprefix>.".
459 if (!TargetPrefix.empty()) {
460 if (Name.size() < 6+TargetPrefix.size() ||
461 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
462 != (TargetPrefix + "."))
463 PrintFatalError("Intrinsic '" + DefName + "' does not start with 'llvm." +
464 TargetPrefix + ".'!");
467 // Parse the list of return types.
468 std::vector<MVT::SimpleValueType> OverloadedVTs;
469 ListInit *TypeList = R->getValueAsListInit("RetTypes");
470 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
471 Record *TyEl = TypeList->getElementAsRecord(i);
472 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
473 MVT::SimpleValueType VT;
474 if (TyEl->isSubClassOf("LLVMMatchType")) {
475 unsigned MatchTy = TyEl->getValueAsInt("Number");
476 assert(MatchTy < OverloadedVTs.size() &&
477 "Invalid matching number!");
478 VT = OverloadedVTs[MatchTy];
479 // 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")) ||
484 VT == MVT::iAny || VT == MVT::vAny) &&
485 "Expected iAny or vAny type");
487 VT = getValueType(TyEl->getValueAsDef("VT"));
489 if (EVT(VT).isOverloaded()) {
490 OverloadedVTs.push_back(VT);
494 // Reject invalid types.
495 if (VT == MVT::isVoid)
496 PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
498 IS.RetVTs.push_back(VT);
499 IS.RetTypeDefs.push_back(TyEl);
502 // 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!");
507 MVT::SimpleValueType VT;
508 if (TyEl->isSubClassOf("LLVMMatchType")) {
509 unsigned MatchTy = TyEl->getValueAsInt("Number");
510 assert(MatchTy < OverloadedVTs.size() &&
511 "Invalid matching number!");
512 VT = OverloadedVTs[MatchTy];
513 // 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")) ||
518 VT == MVT::iAny || VT == MVT::vAny) &&
519 "Expected iAny or vAny type");
521 VT = getValueType(TyEl->getValueAsDef("VT"));
523 if (EVT(VT).isOverloaded()) {
524 OverloadedVTs.push_back(VT);
528 // Reject invalid types.
529 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
530 PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
532 IS.ParamVTs.push_back(VT);
533 IS.ParamTypeDefs.push_back(TyEl);
536 // Parse the intrinsic properties.
537 ListInit *PropList = R->getValueAsListInit("Properties");
538 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
539 Record *Property = PropList->getElementAsRecord(i);
540 assert(Property->isSubClassOf("IntrinsicProperty") &&
541 "Expected a property!");
543 if (Property->getName() == "IntrNoMem")
545 else if (Property->getName() == "IntrReadArgMem")
547 else if (Property->getName() == "IntrReadMem")
549 else if (Property->getName() == "IntrReadWriteArgMem")
550 ModRef = ReadWriteArgMem;
551 else if (Property->getName() == "Commutative")
552 isCommutative = true;
553 else if (Property->getName() == "Throws")
555 else if (Property->getName() == "IntrNoReturn")
557 else if (Property->isSubClassOf("NoCapture")) {
558 unsigned ArgNo = Property->getValueAsInt("ArgNo");
559 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
560 } 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));
567 llvm_unreachable("Unknown property!");
570 // Sort the argument attributes for later benefit.
571 std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end());