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 AsmParserNum("asmparsernum", cl::init(0),
27 cl::desc("Make -gen-asm-parser emit assembly parser #N"));
29 static cl::opt<unsigned>
30 AsmWriterNum("asmwriternum", cl::init(0),
31 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
33 /// getValueType - Return the MVT::SimpleValueType that the specified TableGen
34 /// record corresponds to.
35 MVT::SimpleValueType llvm::getValueType(Record *Rec) {
36 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
39 std::string llvm::getName(MVT::SimpleValueType T) {
41 case MVT::Other: return "UNKNOWN";
42 case MVT::iPTR: return "TLI.getPointerTy()";
43 case MVT::iPTRAny: return "TLI.getPointerTy()";
44 default: return getEnumName(T);
48 std::string llvm::getEnumName(MVT::SimpleValueType T) {
50 case MVT::Other: return "MVT::Other";
51 case MVT::i1: return "MVT::i1";
52 case MVT::i8: return "MVT::i8";
53 case MVT::i16: return "MVT::i16";
54 case MVT::i32: return "MVT::i32";
55 case MVT::i64: return "MVT::i64";
56 case MVT::i128: return "MVT::i128";
57 case MVT::iAny: return "MVT::iAny";
58 case MVT::fAny: return "MVT::fAny";
59 case MVT::vAny: return "MVT::vAny";
60 case MVT::f32: return "MVT::f32";
61 case MVT::f64: return "MVT::f64";
62 case MVT::f80: return "MVT::f80";
63 case MVT::f128: return "MVT::f128";
64 case MVT::ppcf128: return "MVT::ppcf128";
65 case MVT::Flag: return "MVT::Flag";
66 case MVT::isVoid:return "MVT::isVoid";
67 case MVT::v2i8: return "MVT::v2i8";
68 case MVT::v4i8: return "MVT::v4i8";
69 case MVT::v8i8: return "MVT::v8i8";
70 case MVT::v16i8: return "MVT::v16i8";
71 case MVT::v32i8: return "MVT::v32i8";
72 case MVT::v2i16: return "MVT::v2i16";
73 case MVT::v4i16: return "MVT::v4i16";
74 case MVT::v8i16: return "MVT::v8i16";
75 case MVT::v16i16: return "MVT::v16i16";
76 case MVT::v2i32: return "MVT::v2i32";
77 case MVT::v4i32: return "MVT::v4i32";
78 case MVT::v8i32: return "MVT::v8i32";
79 case MVT::v1i64: return "MVT::v1i64";
80 case MVT::v2i64: return "MVT::v2i64";
81 case MVT::v4i64: return "MVT::v4i64";
82 case MVT::v2f32: return "MVT::v2f32";
83 case MVT::v4f32: return "MVT::v4f32";
84 case MVT::v8f32: return "MVT::v8f32";
85 case MVT::v2f64: return "MVT::v2f64";
86 case MVT::v4f64: return "MVT::v4f64";
87 case MVT::Metadata: return "MVT::Metadata";
88 case MVT::iPTR: return "MVT::iPTR";
89 case MVT::iPTRAny: return "MVT::iPTRAny";
90 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
94 /// getQualifiedName - Return the name of the specified record, with a
95 /// namespace qualifier if the record contains one.
97 std::string llvm::getQualifiedName(const Record *R) {
98 std::string Namespace = R->getValueAsString("Namespace");
99 if (Namespace.empty()) return R->getName();
100 return Namespace + "::" + R->getName();
106 /// getTarget - Return the current instance of the Target class.
108 CodeGenTarget::CodeGenTarget() {
109 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
110 if (Targets.size() == 0)
111 throw std::string("ERROR: No 'Target' subclasses defined!");
112 if (Targets.size() != 1)
113 throw std::string("ERROR: Multiple subclasses of Target defined!");
114 TargetRec = Targets[0];
118 const std::string &CodeGenTarget::getName() const {
119 return TargetRec->getName();
122 std::string CodeGenTarget::getInstNamespace() const {
125 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
126 InstNS = i->second.Namespace;
128 // Make sure not to pick up "TargetInstrInfo" by accidentally getting
129 // the namespace off the PHI instruction or something.
130 if (InstNS != "TargetInstrInfo")
137 Record *CodeGenTarget::getInstructionSet() const {
138 return TargetRec->getValueAsDef("InstructionSet");
141 /// getAsmParser - Return the AssemblyParser definition for this target.
143 Record *CodeGenTarget::getAsmParser() const {
144 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
145 if (AsmParserNum >= LI.size())
146 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
147 return LI[AsmParserNum];
150 /// getAsmWriter - Return the AssemblyWriter definition for this target.
152 Record *CodeGenTarget::getAsmWriter() const {
153 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
154 if (AsmWriterNum >= LI.size())
155 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
156 return LI[AsmWriterNum];
159 void CodeGenTarget::ReadRegisters() const {
160 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
162 throw std::string("No 'Register' subclasses defined!");
164 Registers.reserve(Regs.size());
165 Registers.assign(Regs.begin(), Regs.end());
168 CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
169 DeclaredSpillSize = R->getValueAsInt("SpillSize");
170 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
173 const std::string &CodeGenRegister::getName() const {
174 return TheDef->getName();
177 void CodeGenTarget::ReadRegisterClasses() const {
178 std::vector<Record*> RegClasses =
179 Records.getAllDerivedDefinitions("RegisterClass");
180 if (RegClasses.empty())
181 throw std::string("No 'RegisterClass' subclasses defined!");
183 RegisterClasses.reserve(RegClasses.size());
184 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
187 std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
188 std::vector<unsigned char> Result;
189 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
190 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
191 const CodeGenRegisterClass &RC = RegisterClasses[i];
192 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
193 if (R == RC.Elements[ei]) {
194 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
195 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
196 Result.push_back(InVTs[i]);
204 CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
205 // Rename anonymous register classes.
206 if (R->getName().size() > 9 && R->getName()[9] == '.') {
207 static unsigned AnonCounter = 0;
208 R->setName("AnonRegClass_"+utostr(AnonCounter++));
211 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
212 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
213 Record *Type = TypeList[i];
214 if (!Type->isSubClassOf("ValueType"))
215 throw "RegTypes list member '" + Type->getName() +
216 "' does not derive from the ValueType class!";
217 VTs.push_back(getValueType(Type));
219 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
221 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
222 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
223 Record *Reg = RegList[i];
224 if (!Reg->isSubClassOf("Register"))
225 throw "Register Class member '" + Reg->getName() +
226 "' does not derive from the Register class!";
227 Elements.push_back(Reg);
230 std::vector<Record*> SubRegClassList =
231 R->getValueAsListOfDefs("SubRegClassList");
232 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
233 Record *SubRegClass = SubRegClassList[i];
234 if (!SubRegClass->isSubClassOf("RegisterClass"))
235 throw "Register Class member '" + SubRegClass->getName() +
236 "' does not derive from the RegisterClass class!";
237 SubRegClasses.push_back(SubRegClass);
240 // Allow targets to override the size in bits of the RegisterClass.
241 unsigned Size = R->getValueAsInt("Size");
243 Namespace = R->getValueAsString("Namespace");
244 SpillSize = Size ? Size : EVT(VTs[0]).getSizeInBits();
245 SpillAlignment = R->getValueAsInt("Alignment");
246 CopyCost = R->getValueAsInt("CopyCost");
247 MethodBodies = R->getValueAsCode("MethodBodies");
248 MethodProtos = R->getValueAsCode("MethodProtos");
251 const std::string &CodeGenRegisterClass::getName() const {
252 return TheDef->getName();
255 void CodeGenTarget::ReadLegalValueTypes() const {
256 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
257 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
258 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
259 LegalValueTypes.push_back(RCs[i].VTs[ri]);
261 // Remove duplicates.
262 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
263 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
264 LegalValueTypes.end()),
265 LegalValueTypes.end());
269 void CodeGenTarget::ReadInstructions() const {
270 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
271 if (Insts.size() <= 2)
272 throw std::string("No 'Instruction' subclasses defined!");
274 // Parse the instructions defined in the .td file.
275 std::string InstFormatName =
276 getAsmWriter()->getValueAsString("InstFormatName");
278 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
279 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
280 Instructions.insert(std::make_pair(Insts[i]->getName(),
281 CodeGenInstruction(Insts[i], AsmStr)));
285 /// getInstructionsByEnumValue - Return all of the instructions defined by the
286 /// target, ordered by their enum value.
288 getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
289 &NumberedInstructions) {
290 std::map<std::string, CodeGenInstruction>::const_iterator I;
291 I = getInstructions().find("PHI");
292 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
293 const CodeGenInstruction *PHI = &I->second;
295 I = getInstructions().find("INLINEASM");
296 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
297 const CodeGenInstruction *INLINEASM = &I->second;
299 I = getInstructions().find("DBG_LABEL");
300 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
301 const CodeGenInstruction *DBG_LABEL = &I->second;
303 I = getInstructions().find("EH_LABEL");
304 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
305 const CodeGenInstruction *EH_LABEL = &I->second;
307 I = getInstructions().find("GC_LABEL");
308 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
309 const CodeGenInstruction *GC_LABEL = &I->second;
311 I = getInstructions().find("KILL");
312 if (I == Instructions.end()) throw "Could not find 'KILL' instruction!";
313 const CodeGenInstruction *KILL = &I->second;
315 I = getInstructions().find("EXTRACT_SUBREG");
316 if (I == Instructions.end())
317 throw "Could not find 'EXTRACT_SUBREG' instruction!";
318 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
320 I = getInstructions().find("INSERT_SUBREG");
321 if (I == Instructions.end())
322 throw "Could not find 'INSERT_SUBREG' instruction!";
323 const CodeGenInstruction *INSERT_SUBREG = &I->second;
325 I = getInstructions().find("IMPLICIT_DEF");
326 if (I == Instructions.end())
327 throw "Could not find 'IMPLICIT_DEF' instruction!";
328 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
330 I = getInstructions().find("SUBREG_TO_REG");
331 if (I == Instructions.end())
332 throw "Could not find 'SUBREG_TO_REG' instruction!";
333 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
335 I = getInstructions().find("COPY_TO_REGCLASS");
336 if (I == Instructions.end())
337 throw "Could not find 'COPY_TO_REGCLASS' instruction!";
338 const CodeGenInstruction *COPY_TO_REGCLASS = &I->second;
340 I = getInstructions().find("DEBUG_VALUE");
341 if (I == Instructions.end())
342 throw "Could not find 'DEBUG_VALUE' instruction!";
343 const CodeGenInstruction *DEBUG_VALUE = &I->second;
345 // Print out the rest of the instructions now.
346 NumberedInstructions.push_back(PHI);
347 NumberedInstructions.push_back(INLINEASM);
348 NumberedInstructions.push_back(DBG_LABEL);
349 NumberedInstructions.push_back(EH_LABEL);
350 NumberedInstructions.push_back(GC_LABEL);
351 NumberedInstructions.push_back(KILL);
352 NumberedInstructions.push_back(EXTRACT_SUBREG);
353 NumberedInstructions.push_back(INSERT_SUBREG);
354 NumberedInstructions.push_back(IMPLICIT_DEF);
355 NumberedInstructions.push_back(SUBREG_TO_REG);
356 NumberedInstructions.push_back(COPY_TO_REGCLASS);
357 NumberedInstructions.push_back(DEBUG_VALUE);
358 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
359 if (&II->second != PHI &&
360 &II->second != INLINEASM &&
361 &II->second != DBG_LABEL &&
362 &II->second != EH_LABEL &&
363 &II->second != GC_LABEL &&
364 &II->second != KILL &&
365 &II->second != EXTRACT_SUBREG &&
366 &II->second != INSERT_SUBREG &&
367 &II->second != IMPLICIT_DEF &&
368 &II->second != SUBREG_TO_REG &&
369 &II->second != COPY_TO_REGCLASS &&
370 &II->second != DEBUG_VALUE)
371 NumberedInstructions.push_back(&II->second);
375 /// isLittleEndianEncoding - Return whether this target encodes its instruction
376 /// in little-endian format, i.e. bits laid out in the order [0..n]
378 bool CodeGenTarget::isLittleEndianEncoding() const {
379 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
382 //===----------------------------------------------------------------------===//
383 // ComplexPattern implementation
385 ComplexPattern::ComplexPattern(Record *R) {
386 Ty = ::getValueType(R->getValueAsDef("Ty"));
387 NumOperands = R->getValueAsInt("NumOperands");
388 SelectFunc = R->getValueAsString("SelectFunc");
389 RootNodes = R->getValueAsListOfDefs("RootNodes");
391 // Parse the properties.
393 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
394 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
395 if (PropList[i]->getName() == "SDNPHasChain") {
396 Properties |= 1 << SDNPHasChain;
397 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
398 Properties |= 1 << SDNPOptInFlag;
399 } else if (PropList[i]->getName() == "SDNPMayStore") {
400 Properties |= 1 << SDNPMayStore;
401 } else if (PropList[i]->getName() == "SDNPMayLoad") {
402 Properties |= 1 << SDNPMayLoad;
403 } else if (PropList[i]->getName() == "SDNPSideEffect") {
404 Properties |= 1 << SDNPSideEffect;
405 } else if (PropList[i]->getName() == "SDNPMemOperand") {
406 Properties |= 1 << SDNPMemOperand;
408 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
409 << "' on ComplexPattern '" << R->getName() << "'!\n";
414 //===----------------------------------------------------------------------===//
415 // CodeGenIntrinsic Implementation
416 //===----------------------------------------------------------------------===//
418 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
420 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
422 std::vector<CodeGenIntrinsic> Result;
424 for (unsigned i = 0, e = I.size(); i != e; ++i) {
425 bool isTarget = I[i]->getValueAsBit("isTarget");
426 if (isTarget == TargetOnly)
427 Result.push_back(CodeGenIntrinsic(I[i]));
432 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
434 std::string DefName = R->getName();
436 isOverloaded = false;
437 isCommutative = false;
439 if (DefName.size() <= 4 ||
440 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
441 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
443 EnumName = std::string(DefName.begin()+4, DefName.end());
445 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
446 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
448 TargetPrefix = R->getValueAsString("TargetPrefix");
449 Name = R->getValueAsString("LLVMName");
452 // If an explicit name isn't specified, derive one from the DefName.
455 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
456 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
458 // Verify it starts with "llvm.".
459 if (Name.size() <= 5 ||
460 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
461 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
464 // If TargetPrefix is specified, make sure that Name starts with
465 // "llvm.<targetprefix>.".
466 if (!TargetPrefix.empty()) {
467 if (Name.size() < 6+TargetPrefix.size() ||
468 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
469 != (TargetPrefix + "."))
470 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
471 TargetPrefix + ".'!";
474 // Parse the list of return types.
475 std::vector<MVT::SimpleValueType> OverloadedVTs;
476 ListInit *TypeList = R->getValueAsListInit("RetTypes");
477 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
478 Record *TyEl = TypeList->getElementAsRecord(i);
479 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
480 MVT::SimpleValueType VT;
481 if (TyEl->isSubClassOf("LLVMMatchType")) {
482 unsigned MatchTy = TyEl->getValueAsInt("Number");
483 assert(MatchTy < OverloadedVTs.size() &&
484 "Invalid matching number!");
485 VT = OverloadedVTs[MatchTy];
486 // It only makes sense to use the extended and truncated vector element
487 // variants with iAny types; otherwise, if the intrinsic is not
488 // overloaded, all the types can be specified directly.
489 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
490 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
491 VT == MVT::iAny || VT == MVT::vAny) &&
492 "Expected iAny or vAny type");
494 VT = getValueType(TyEl->getValueAsDef("VT"));
496 if (EVT(VT).isOverloaded()) {
497 OverloadedVTs.push_back(VT);
498 isOverloaded |= true;
500 IS.RetVTs.push_back(VT);
501 IS.RetTypeDefs.push_back(TyEl);
504 if (IS.RetVTs.size() == 0)
505 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
507 // Parse the list of parameter types.
508 TypeList = R->getValueAsListInit("ParamTypes");
509 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
510 Record *TyEl = TypeList->getElementAsRecord(i);
511 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
512 MVT::SimpleValueType VT;
513 if (TyEl->isSubClassOf("LLVMMatchType")) {
514 unsigned MatchTy = TyEl->getValueAsInt("Number");
515 assert(MatchTy < OverloadedVTs.size() &&
516 "Invalid matching number!");
517 VT = OverloadedVTs[MatchTy];
518 // It only makes sense to use the extended and truncated vector element
519 // variants with iAny types; otherwise, if the intrinsic is not
520 // overloaded, all the types can be specified directly.
521 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
522 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
523 VT == MVT::iAny || VT == MVT::vAny) &&
524 "Expected iAny or vAny type");
526 VT = getValueType(TyEl->getValueAsDef("VT"));
527 if (EVT(VT).isOverloaded()) {
528 OverloadedVTs.push_back(VT);
529 isOverloaded |= true;
531 IS.ParamVTs.push_back(VT);
532 IS.ParamTypeDefs.push_back(TyEl);
535 // Parse the intrinsic properties.
536 ListInit *PropList = R->getValueAsListInit("Properties");
537 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
538 Record *Property = PropList->getElementAsRecord(i);
539 assert(Property->isSubClassOf("IntrinsicProperty") &&
540 "Expected a property!");
542 if (Property->getName() == "IntrNoMem")
544 else if (Property->getName() == "IntrReadArgMem")
546 else if (Property->getName() == "IntrReadMem")
548 else if (Property->getName() == "IntrWriteArgMem")
549 ModRef = WriteArgMem;
550 else if (Property->getName() == "IntrWriteMem")
552 else if (Property->getName() == "Commutative")
553 isCommutative = true;
554 else if (Property->isSubClassOf("NoCapture")) {
555 unsigned ArgNo = Property->getValueAsInt("ArgNo");
556 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
558 assert(0 && "Unknown property!");