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 throw exceptions on error conditions.
15 //===----------------------------------------------------------------------===//
17 #include "CodeGenTarget.h"
18 #include "CodeGenIntrinsics.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/CommandLine.h"
25 static cl::opt<unsigned>
26 AsmWriterNum("asmwriternum", cl::init(0),
27 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
29 /// getValueType - Return the MVT::SimpleValueType that the specified TableGen
30 /// record corresponds to.
31 MVT::SimpleValueType llvm::getValueType(Record *Rec) {
32 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
35 std::string llvm::getName(MVT::SimpleValueType T) {
37 case MVT::Other: return "UNKNOWN";
38 case MVT::iPTR: return "TLI.getPointerTy()";
39 case MVT::iPTRAny: return "TLI.getPointerTy()";
40 default: return getEnumName(T);
44 std::string llvm::getEnumName(MVT::SimpleValueType T) {
46 case MVT::Other: return "MVT::Other";
47 case MVT::i1: return "MVT::i1";
48 case MVT::i8: return "MVT::i8";
49 case MVT::i16: return "MVT::i16";
50 case MVT::i32: return "MVT::i32";
51 case MVT::i64: return "MVT::i64";
52 case MVT::i128: return "MVT::i128";
53 case MVT::iAny: return "MVT::iAny";
54 case MVT::fAny: return "MVT::fAny";
55 case MVT::f32: return "MVT::f32";
56 case MVT::f64: return "MVT::f64";
57 case MVT::f80: return "MVT::f80";
58 case MVT::f128: return "MVT::f128";
59 case MVT::ppcf128: return "MVT::ppcf128";
60 case MVT::Flag: return "MVT::Flag";
61 case MVT::isVoid:return "MVT::isVoid";
62 case MVT::v2i8: return "MVT::v2i8";
63 case MVT::v4i8: return "MVT::v4i8";
64 case MVT::v8i8: return "MVT::v8i8";
65 case MVT::v16i8: return "MVT::v16i8";
66 case MVT::v24i8: return "MVT::v24i8";
67 case MVT::v32i8: return "MVT::v32i8";
68 case MVT::v48i8: return "MVT::v48i8";
69 case MVT::v64i8: return "MVT::v64i8";
70 case MVT::v2i16: return "MVT::v2i16";
71 case MVT::v4i16: return "MVT::v4i16";
72 case MVT::v8i16: return "MVT::v8i16";
73 case MVT::v12i16: return "MVT::v12i16";
74 case MVT::v16i16: return "MVT::v16i16";
75 case MVT::v24i16: return "MVT::v24i16";
76 case MVT::v32i16: return "MVT::v32i16";
77 case MVT::v2i32: return "MVT::v2i32";
78 case MVT::v3i32: return "MVT::v3i32";
79 case MVT::v4i32: return "MVT::v4i32";
80 case MVT::v6i32: return "MVT::v6i32";
81 case MVT::v8i32: return "MVT::v8i32";
82 case MVT::v12i32: return "MVT::v12i32";
83 case MVT::v16i32: return "MVT::v16i32";
84 case MVT::v1i64: return "MVT::v1i64";
85 case MVT::v2i64: return "MVT::v2i64";
86 case MVT::v3i64: return "MVT::v3i64";
87 case MVT::v4i64: return "MVT::v4i64";
88 case MVT::v6i64: return "MVT::v6i64";
89 case MVT::v8i64: return "MVT::v8i64";
90 case MVT::v2f32: return "MVT::v2f32";
91 case MVT::v3f32: return "MVT::v3f32";
92 case MVT::v4f32: return "MVT::v4f32";
93 case MVT::v6f32: return "MVT::v6f32";
94 case MVT::v8f32: return "MVT::v8f32";
95 case MVT::v12f32: return "MVT::v12f32";
96 case MVT::v16f32: return "MVT::v16f32";
97 case MVT::v2f64: return "MVT::v2f64";
98 case MVT::v4f64: return "MVT::v4f64";
99 case MVT::Metadata: return "MVT::Metadata";
100 case MVT::iPTR: return "MVT::iPTR";
101 case MVT::iPTRAny: return "MVT::iPTRAny";
102 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
106 /// getQualifiedName - Return the name of the specified record, with a
107 /// namespace qualifier if the record contains one.
109 std::string llvm::getQualifiedName(const Record *R) {
110 std::string Namespace = R->getValueAsString("Namespace");
111 if (Namespace.empty()) return R->getName();
112 return Namespace + "::" + R->getName();
118 /// getTarget - Return the current instance of the Target class.
120 CodeGenTarget::CodeGenTarget() {
121 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
122 if (Targets.size() == 0)
123 throw std::string("ERROR: No 'Target' subclasses defined!");
124 if (Targets.size() != 1)
125 throw std::string("ERROR: Multiple subclasses of Target defined!");
126 TargetRec = Targets[0];
130 const std::string &CodeGenTarget::getName() const {
131 return TargetRec->getName();
134 std::string CodeGenTarget::getInstNamespace() const {
137 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
138 InstNS = i->second.Namespace;
140 // Make sure not to pick up "TargetInstrInfo" by accidentally getting
141 // the namespace off the PHI instruction or something.
142 if (InstNS != "TargetInstrInfo")
149 Record *CodeGenTarget::getInstructionSet() const {
150 return TargetRec->getValueAsDef("InstructionSet");
153 /// getAsmWriter - Return the AssemblyWriter definition for this target.
155 Record *CodeGenTarget::getAsmWriter() const {
156 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
157 if (AsmWriterNum >= LI.size())
158 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
159 return LI[AsmWriterNum];
162 void CodeGenTarget::ReadRegisters() const {
163 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
165 throw std::string("No 'Register' subclasses defined!");
167 Registers.reserve(Regs.size());
168 Registers.assign(Regs.begin(), Regs.end());
171 CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
172 DeclaredSpillSize = R->getValueAsInt("SpillSize");
173 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
176 const std::string &CodeGenRegister::getName() const {
177 return TheDef->getName();
180 void CodeGenTarget::ReadRegisterClasses() const {
181 std::vector<Record*> RegClasses =
182 Records.getAllDerivedDefinitions("RegisterClass");
183 if (RegClasses.empty())
184 throw std::string("No 'RegisterClass' subclasses defined!");
186 RegisterClasses.reserve(RegClasses.size());
187 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
190 std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
191 std::vector<unsigned char> Result;
192 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
193 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
194 const CodeGenRegisterClass &RC = RegisterClasses[i];
195 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
196 if (R == RC.Elements[ei]) {
197 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
198 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
199 Result.push_back(InVTs[i]);
207 CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
208 // Rename anonymous register classes.
209 if (R->getName().size() > 9 && R->getName()[9] == '.') {
210 static unsigned AnonCounter = 0;
211 R->setName("AnonRegClass_"+utostr(AnonCounter++));
214 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
215 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
216 Record *Type = TypeList[i];
217 if (!Type->isSubClassOf("ValueType"))
218 throw "RegTypes list member '" + Type->getName() +
219 "' does not derive from the ValueType class!";
220 VTs.push_back(getValueType(Type));
222 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
224 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
225 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
226 Record *Reg = RegList[i];
227 if (!Reg->isSubClassOf("Register"))
228 throw "Register Class member '" + Reg->getName() +
229 "' does not derive from the Register class!";
230 Elements.push_back(Reg);
233 std::vector<Record*> SubRegClassList =
234 R->getValueAsListOfDefs("SubRegClassList");
235 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
236 Record *SubRegClass = SubRegClassList[i];
237 if (!SubRegClass->isSubClassOf("RegisterClass"))
238 throw "Register Class member '" + SubRegClass->getName() +
239 "' does not derive from the RegisterClass class!";
240 SubRegClasses.push_back(SubRegClass);
243 // Allow targets to override the size in bits of the RegisterClass.
244 unsigned Size = R->getValueAsInt("Size");
246 Namespace = R->getValueAsString("Namespace");
247 SpillSize = Size ? Size : MVT(VTs[0]).getSizeInBits();
248 SpillAlignment = R->getValueAsInt("Alignment");
249 CopyCost = R->getValueAsInt("CopyCost");
250 MethodBodies = R->getValueAsCode("MethodBodies");
251 MethodProtos = R->getValueAsCode("MethodProtos");
254 const std::string &CodeGenRegisterClass::getName() const {
255 return TheDef->getName();
258 void CodeGenTarget::ReadLegalValueTypes() const {
259 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
260 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
261 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
262 LegalValueTypes.push_back(RCs[i].VTs[ri]);
264 // Remove duplicates.
265 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
266 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
267 LegalValueTypes.end()),
268 LegalValueTypes.end());
272 void CodeGenTarget::ReadInstructions() const {
273 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
274 if (Insts.size() <= 2)
275 throw std::string("No 'Instruction' subclasses defined!");
277 // Parse the instructions defined in the .td file.
278 std::string InstFormatName =
279 getAsmWriter()->getValueAsString("InstFormatName");
281 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
282 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
283 Instructions.insert(std::make_pair(Insts[i]->getName(),
284 CodeGenInstruction(Insts[i], AsmStr)));
288 /// getInstructionsByEnumValue - Return all of the instructions defined by the
289 /// target, ordered by their enum value.
291 getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
292 &NumberedInstructions) {
293 std::map<std::string, CodeGenInstruction>::const_iterator I;
294 I = getInstructions().find("PHI");
295 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
296 const CodeGenInstruction *PHI = &I->second;
298 I = getInstructions().find("INLINEASM");
299 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
300 const CodeGenInstruction *INLINEASM = &I->second;
302 I = getInstructions().find("DBG_LABEL");
303 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
304 const CodeGenInstruction *DBG_LABEL = &I->second;
306 I = getInstructions().find("EH_LABEL");
307 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
308 const CodeGenInstruction *EH_LABEL = &I->second;
310 I = getInstructions().find("GC_LABEL");
311 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
312 const CodeGenInstruction *GC_LABEL = &I->second;
314 I = getInstructions().find("DECLARE");
315 if (I == Instructions.end()) throw "Could not find 'DECLARE' instruction!";
316 const CodeGenInstruction *DECLARE = &I->second;
318 I = getInstructions().find("EXTRACT_SUBREG");
319 if (I == Instructions.end())
320 throw "Could not find 'EXTRACT_SUBREG' instruction!";
321 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
323 I = getInstructions().find("INSERT_SUBREG");
324 if (I == Instructions.end())
325 throw "Could not find 'INSERT_SUBREG' instruction!";
326 const CodeGenInstruction *INSERT_SUBREG = &I->second;
328 I = getInstructions().find("IMPLICIT_DEF");
329 if (I == Instructions.end())
330 throw "Could not find 'IMPLICIT_DEF' instruction!";
331 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
333 I = getInstructions().find("SUBREG_TO_REG");
334 if (I == Instructions.end())
335 throw "Could not find 'SUBREG_TO_REG' instruction!";
336 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
338 I = getInstructions().find("COPY_TO_REGCLASS");
339 if (I == Instructions.end())
340 throw "Could not find 'COPY_TO_REGCLASS' instruction!";
341 const CodeGenInstruction *COPY_TO_REGCLASS = &I->second;
343 // Print out the rest of the instructions now.
344 NumberedInstructions.push_back(PHI);
345 NumberedInstructions.push_back(INLINEASM);
346 NumberedInstructions.push_back(DBG_LABEL);
347 NumberedInstructions.push_back(EH_LABEL);
348 NumberedInstructions.push_back(GC_LABEL);
349 NumberedInstructions.push_back(DECLARE);
350 NumberedInstructions.push_back(EXTRACT_SUBREG);
351 NumberedInstructions.push_back(INSERT_SUBREG);
352 NumberedInstructions.push_back(IMPLICIT_DEF);
353 NumberedInstructions.push_back(SUBREG_TO_REG);
354 NumberedInstructions.push_back(COPY_TO_REGCLASS);
355 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
356 if (&II->second != PHI &&
357 &II->second != INLINEASM &&
358 &II->second != DBG_LABEL &&
359 &II->second != EH_LABEL &&
360 &II->second != GC_LABEL &&
361 &II->second != DECLARE &&
362 &II->second != EXTRACT_SUBREG &&
363 &II->second != INSERT_SUBREG &&
364 &II->second != IMPLICIT_DEF &&
365 &II->second != SUBREG_TO_REG &&
366 &II->second != COPY_TO_REGCLASS)
367 NumberedInstructions.push_back(&II->second);
371 /// isLittleEndianEncoding - Return whether this target encodes its instruction
372 /// in little-endian format, i.e. bits laid out in the order [0..n]
374 bool CodeGenTarget::isLittleEndianEncoding() const {
375 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
378 //===----------------------------------------------------------------------===//
379 // ComplexPattern implementation
381 ComplexPattern::ComplexPattern(Record *R) {
382 Ty = ::getValueType(R->getValueAsDef("Ty"));
383 NumOperands = R->getValueAsInt("NumOperands");
384 SelectFunc = R->getValueAsString("SelectFunc");
385 RootNodes = R->getValueAsListOfDefs("RootNodes");
387 // Parse the properties.
389 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
390 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
391 if (PropList[i]->getName() == "SDNPHasChain") {
392 Properties |= 1 << SDNPHasChain;
393 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
394 Properties |= 1 << SDNPOptInFlag;
395 } else if (PropList[i]->getName() == "SDNPMayStore") {
396 Properties |= 1 << SDNPMayStore;
397 } else if (PropList[i]->getName() == "SDNPMayLoad") {
398 Properties |= 1 << SDNPMayLoad;
399 } else if (PropList[i]->getName() == "SDNPSideEffect") {
400 Properties |= 1 << SDNPSideEffect;
401 } else if (PropList[i]->getName() == "SDNPMemOperand") {
402 Properties |= 1 << SDNPMemOperand;
404 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
405 << "' on ComplexPattern '" << R->getName() << "'!\n";
409 // Parse the attributes.
411 PropList = R->getValueAsListOfDefs("Attributes");
412 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
413 if (PropList[i]->getName() == "CPAttrParentAsRoot") {
414 Attributes |= 1 << CPAttrParentAsRoot;
416 errs() << "Unsupported pattern attribute '" << PropList[i]->getName()
417 << "' on ComplexPattern '" << R->getName() << "'!\n";
422 //===----------------------------------------------------------------------===//
423 // CodeGenIntrinsic Implementation
424 //===----------------------------------------------------------------------===//
426 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
428 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
430 std::vector<CodeGenIntrinsic> Result;
432 for (unsigned i = 0, e = I.size(); i != e; ++i) {
433 bool isTarget = I[i]->getValueAsBit("isTarget");
434 if (isTarget == TargetOnly)
435 Result.push_back(CodeGenIntrinsic(I[i]));
440 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
442 std::string DefName = R->getName();
444 isOverloaded = false;
445 isCommutative = false;
447 if (DefName.size() <= 4 ||
448 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
449 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
451 EnumName = std::string(DefName.begin()+4, DefName.end());
453 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
454 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
456 TargetPrefix = R->getValueAsString("TargetPrefix");
457 Name = R->getValueAsString("LLVMName");
460 // If an explicit name isn't specified, derive one from the DefName.
463 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
464 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
466 // Verify it starts with "llvm.".
467 if (Name.size() <= 5 ||
468 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
469 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
472 // If TargetPrefix is specified, make sure that Name starts with
473 // "llvm.<targetprefix>.".
474 if (!TargetPrefix.empty()) {
475 if (Name.size() < 6+TargetPrefix.size() ||
476 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
477 != (TargetPrefix + "."))
478 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
479 TargetPrefix + ".'!";
482 // Parse the list of return types.
483 std::vector<MVT::SimpleValueType> OverloadedVTs;
484 ListInit *TypeList = R->getValueAsListInit("RetTypes");
485 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
486 Record *TyEl = TypeList->getElementAsRecord(i);
487 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
488 MVT::SimpleValueType VT;
489 if (TyEl->isSubClassOf("LLVMMatchType")) {
490 unsigned MatchTy = TyEl->getValueAsInt("Number");
491 assert(MatchTy < OverloadedVTs.size() &&
492 "Invalid matching number!");
493 VT = OverloadedVTs[MatchTy];
494 // It only makes sense to use the extended and truncated vector element
495 // variants with iAny types; otherwise, if the intrinsic is not
496 // overloaded, all the types can be specified directly.
497 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
498 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
499 VT == MVT::iAny) && "Expected iAny type");
501 VT = getValueType(TyEl->getValueAsDef("VT"));
503 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny) {
504 OverloadedVTs.push_back(VT);
505 isOverloaded |= true;
507 IS.RetVTs.push_back(VT);
508 IS.RetTypeDefs.push_back(TyEl);
511 if (IS.RetVTs.size() == 0)
512 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
514 // Parse the list of parameter types.
515 TypeList = R->getValueAsListInit("ParamTypes");
516 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
517 Record *TyEl = TypeList->getElementAsRecord(i);
518 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
519 MVT::SimpleValueType VT;
520 if (TyEl->isSubClassOf("LLVMMatchType")) {
521 unsigned MatchTy = TyEl->getValueAsInt("Number");
522 assert(MatchTy < OverloadedVTs.size() &&
523 "Invalid matching number!");
524 VT = OverloadedVTs[MatchTy];
525 // It only makes sense to use the extended and truncated vector element
526 // variants with iAny types; otherwise, if the intrinsic is not
527 // overloaded, all the types can be specified directly.
528 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
529 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
530 VT == MVT::iAny) && "Expected iAny type");
532 VT = getValueType(TyEl->getValueAsDef("VT"));
533 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny) {
534 OverloadedVTs.push_back(VT);
535 isOverloaded |= true;
537 IS.ParamVTs.push_back(VT);
538 IS.ParamTypeDefs.push_back(TyEl);
541 // Parse the intrinsic properties.
542 ListInit *PropList = R->getValueAsListInit("Properties");
543 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
544 Record *Property = PropList->getElementAsRecord(i);
545 assert(Property->isSubClassOf("IntrinsicProperty") &&
546 "Expected a property!");
548 if (Property->getName() == "IntrNoMem")
550 else if (Property->getName() == "IntrReadArgMem")
552 else if (Property->getName() == "IntrReadMem")
554 else if (Property->getName() == "IntrWriteArgMem")
555 ModRef = WriteArgMem;
556 else if (Property->getName() == "IntrWriteMem")
558 else if (Property->getName() == "Commutative")
559 isCommutative = true;
560 else if (Property->isSubClassOf("NoCapture")) {
561 unsigned ArgNo = Property->getValueAsInt("ArgNo");
562 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
564 assert(0 && "Unknown property!");