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"
22 #include "llvm/Support/Streams.h"
26 static cl::opt<unsigned>
27 AsmWriterNum("asmwriternum", cl::init(0),
28 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
30 /// getValueType - Return the MVT::SimpleValueType that the specified TableGen
31 /// record corresponds to.
32 MVT::SimpleValueType llvm::getValueType(Record *Rec) {
33 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
36 std::string llvm::getName(MVT::SimpleValueType T) {
38 case MVT::Other: return "UNKNOWN";
39 case MVT::i1: return "MVT::i1";
40 case MVT::i8: return "MVT::i8";
41 case MVT::i16: return "MVT::i16";
42 case MVT::i32: return "MVT::i32";
43 case MVT::i64: return "MVT::i64";
44 case MVT::i128: return "MVT::i128";
45 case MVT::iAny: return "MVT::iAny";
46 case MVT::fAny: return "MVT::fAny";
47 case MVT::f32: return "MVT::f32";
48 case MVT::f64: return "MVT::f64";
49 case MVT::f80: return "MVT::f80";
50 case MVT::f128: return "MVT::f128";
51 case MVT::ppcf128: return "MVT::ppcf128";
52 case MVT::Flag: return "MVT::Flag";
53 case MVT::isVoid:return "MVT::isVoid";
54 case MVT::v8i8: return "MVT::v8i8";
55 case MVT::v4i16: return "MVT::v4i16";
56 case MVT::v2i32: return "MVT::v2i32";
57 case MVT::v1i64: return "MVT::v1i64";
58 case MVT::v16i8: return "MVT::v16i8";
59 case MVT::v8i16: return "MVT::v8i16";
60 case MVT::v4i32: return "MVT::v4i32";
61 case MVT::v2i64: return "MVT::v2i64";
62 case MVT::v2f32: return "MVT::v2f32";
63 case MVT::v4f32: return "MVT::v4f32";
64 case MVT::v2f64: return "MVT::v2f64";
65 case MVT::v3i32: return "MVT::v3i32";
66 case MVT::v3f32: return "MVT::v3f32";
67 case MVT::iPTR: return "TLI.getPointerTy()";
68 case MVT::iPTRAny: return "TLI.getPointerTy()";
69 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
73 std::string llvm::getEnumName(MVT::SimpleValueType T) {
75 case MVT::Other: return "MVT::Other";
76 case MVT::i1: return "MVT::i1";
77 case MVT::i8: return "MVT::i8";
78 case MVT::i16: return "MVT::i16";
79 case MVT::i32: return "MVT::i32";
80 case MVT::i64: return "MVT::i64";
81 case MVT::i128: return "MVT::i128";
82 case MVT::iAny: return "MVT::iAny";
83 case MVT::fAny: return "MVT::fAny";
84 case MVT::f32: return "MVT::f32";
85 case MVT::f64: return "MVT::f64";
86 case MVT::f80: return "MVT::f80";
87 case MVT::f128: return "MVT::f128";
88 case MVT::ppcf128: return "MVT::ppcf128";
89 case MVT::Flag: return "MVT::Flag";
90 case MVT::isVoid:return "MVT::isVoid";
91 case MVT::v8i8: return "MVT::v8i8";
92 case MVT::v4i16: return "MVT::v4i16";
93 case MVT::v2i32: return "MVT::v2i32";
94 case MVT::v1i64: return "MVT::v1i64";
95 case MVT::v16i8: return "MVT::v16i8";
96 case MVT::v8i16: return "MVT::v8i16";
97 case MVT::v4i32: return "MVT::v4i32";
98 case MVT::v2i64: return "MVT::v2i64";
99 case MVT::v2f32: return "MVT::v2f32";
100 case MVT::v4f32: return "MVT::v4f32";
101 case MVT::v2f64: return "MVT::v2f64";
102 case MVT::v3i32: return "MVT::v3i32";
103 case MVT::v3f32: return "MVT::v3f32";
104 case MVT::iPTR: return "MVT::iPTR";
105 case MVT::iPTRAny: return "MVT::iPTRAny";
106 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
110 /// getQualifiedName - Return the name of the specified record, with a
111 /// namespace qualifier if the record contains one.
113 std::string llvm::getQualifiedName(const Record *R) {
114 std::string Namespace = R->getValueAsString("Namespace");
115 if (Namespace.empty()) return R->getName();
116 return Namespace + "::" + R->getName();
122 /// getTarget - Return the current instance of the Target class.
124 CodeGenTarget::CodeGenTarget() {
125 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
126 if (Targets.size() == 0)
127 throw std::string("ERROR: No 'Target' subclasses defined!");
128 if (Targets.size() != 1)
129 throw std::string("ERROR: Multiple subclasses of Target defined!");
130 TargetRec = Targets[0];
134 const std::string &CodeGenTarget::getName() const {
135 return TargetRec->getName();
138 std::string CodeGenTarget::getInstNamespace() const {
141 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
142 InstNS = i->second.Namespace;
144 // Make sure not to pick up "TargetInstrInfo" by accidentally getting
145 // the namespace off the PHI instruction or something.
146 if (InstNS != "TargetInstrInfo")
153 Record *CodeGenTarget::getInstructionSet() const {
154 return TargetRec->getValueAsDef("InstructionSet");
157 /// getAsmWriter - Return the AssemblyWriter definition for this target.
159 Record *CodeGenTarget::getAsmWriter() const {
160 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
161 if (AsmWriterNum >= LI.size())
162 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
163 return LI[AsmWriterNum];
166 void CodeGenTarget::ReadRegisters() const {
167 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
169 throw std::string("No 'Register' subclasses defined!");
171 Registers.reserve(Regs.size());
172 Registers.assign(Regs.begin(), Regs.end());
175 CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
176 DeclaredSpillSize = R->getValueAsInt("SpillSize");
177 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
180 const std::string &CodeGenRegister::getName() const {
181 return TheDef->getName();
184 void CodeGenTarget::ReadRegisterClasses() const {
185 std::vector<Record*> RegClasses =
186 Records.getAllDerivedDefinitions("RegisterClass");
187 if (RegClasses.empty())
188 throw std::string("No 'RegisterClass' subclasses defined!");
190 RegisterClasses.reserve(RegClasses.size());
191 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
194 std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
195 std::vector<unsigned char> Result;
196 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
197 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
198 const CodeGenRegisterClass &RC = RegisterClasses[i];
199 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
200 if (R == RC.Elements[ei]) {
201 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
202 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
203 Result.push_back(InVTs[i]);
211 CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
212 // Rename anonymous register classes.
213 if (R->getName().size() > 9 && R->getName()[9] == '.') {
214 static unsigned AnonCounter = 0;
215 R->setName("AnonRegClass_"+utostr(AnonCounter++));
218 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
219 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
220 Record *Type = TypeList[i];
221 if (!Type->isSubClassOf("ValueType"))
222 throw "RegTypes list member '" + Type->getName() +
223 "' does not derive from the ValueType class!";
224 VTs.push_back(getValueType(Type));
226 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
228 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
229 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
230 Record *Reg = RegList[i];
231 if (!Reg->isSubClassOf("Register"))
232 throw "Register Class member '" + Reg->getName() +
233 "' does not derive from the Register class!";
234 Elements.push_back(Reg);
237 std::vector<Record*> SubRegClassList =
238 R->getValueAsListOfDefs("SubRegClassList");
239 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
240 Record *SubRegClass = SubRegClassList[i];
241 if (!SubRegClass->isSubClassOf("RegisterClass"))
242 throw "Register Class member '" + SubRegClass->getName() +
243 "' does not derive from the RegisterClass class!";
244 SubRegClasses.push_back(SubRegClass);
247 // Allow targets to override the size in bits of the RegisterClass.
248 unsigned Size = R->getValueAsInt("Size");
250 Namespace = R->getValueAsString("Namespace");
251 SpillSize = Size ? Size : MVT(VTs[0]).getSizeInBits();
252 SpillAlignment = R->getValueAsInt("Alignment");
253 CopyCost = R->getValueAsInt("CopyCost");
254 MethodBodies = R->getValueAsCode("MethodBodies");
255 MethodProtos = R->getValueAsCode("MethodProtos");
258 const std::string &CodeGenRegisterClass::getName() const {
259 return TheDef->getName();
262 void CodeGenTarget::ReadLegalValueTypes() const {
263 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
264 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
265 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
266 LegalValueTypes.push_back(RCs[i].VTs[ri]);
268 // Remove duplicates.
269 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
270 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
271 LegalValueTypes.end()),
272 LegalValueTypes.end());
276 void CodeGenTarget::ReadInstructions() const {
277 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
278 if (Insts.size() <= 2)
279 throw std::string("No 'Instruction' subclasses defined!");
281 // Parse the instructions defined in the .td file.
282 std::string InstFormatName =
283 getAsmWriter()->getValueAsString("InstFormatName");
285 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
286 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
287 Instructions.insert(std::make_pair(Insts[i]->getName(),
288 CodeGenInstruction(Insts[i], AsmStr)));
292 /// getInstructionsByEnumValue - Return all of the instructions defined by the
293 /// target, ordered by their enum value.
295 getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
296 &NumberedInstructions) {
297 std::map<std::string, CodeGenInstruction>::const_iterator I;
298 I = getInstructions().find("PHI");
299 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
300 const CodeGenInstruction *PHI = &I->second;
302 I = getInstructions().find("INLINEASM");
303 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
304 const CodeGenInstruction *INLINEASM = &I->second;
306 I = getInstructions().find("DBG_LABEL");
307 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
308 const CodeGenInstruction *DBG_LABEL = &I->second;
310 I = getInstructions().find("EH_LABEL");
311 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
312 const CodeGenInstruction *EH_LABEL = &I->second;
314 I = getInstructions().find("GC_LABEL");
315 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
316 const CodeGenInstruction *GC_LABEL = &I->second;
318 I = getInstructions().find("DECLARE");
319 if (I == Instructions.end()) throw "Could not find 'DECLARE' instruction!";
320 const CodeGenInstruction *DECLARE = &I->second;
322 I = getInstructions().find("EXTRACT_SUBREG");
323 if (I == Instructions.end())
324 throw "Could not find 'EXTRACT_SUBREG' instruction!";
325 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
327 I = getInstructions().find("INSERT_SUBREG");
328 if (I == Instructions.end())
329 throw "Could not find 'INSERT_SUBREG' instruction!";
330 const CodeGenInstruction *INSERT_SUBREG = &I->second;
332 I = getInstructions().find("IMPLICIT_DEF");
333 if (I == Instructions.end())
334 throw "Could not find 'IMPLICIT_DEF' instruction!";
335 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
337 I = getInstructions().find("SUBREG_TO_REG");
338 if (I == Instructions.end())
339 throw "Could not find 'SUBREG_TO_REG' instruction!";
340 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
342 // Print out the rest of the instructions now.
343 NumberedInstructions.push_back(PHI);
344 NumberedInstructions.push_back(INLINEASM);
345 NumberedInstructions.push_back(DBG_LABEL);
346 NumberedInstructions.push_back(EH_LABEL);
347 NumberedInstructions.push_back(GC_LABEL);
348 NumberedInstructions.push_back(DECLARE);
349 NumberedInstructions.push_back(EXTRACT_SUBREG);
350 NumberedInstructions.push_back(INSERT_SUBREG);
351 NumberedInstructions.push_back(IMPLICIT_DEF);
352 NumberedInstructions.push_back(SUBREG_TO_REG);
353 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
354 if (&II->second != PHI &&
355 &II->second != INLINEASM &&
356 &II->second != DBG_LABEL &&
357 &II->second != EH_LABEL &&
358 &II->second != GC_LABEL &&
359 &II->second != DECLARE &&
360 &II->second != EXTRACT_SUBREG &&
361 &II->second != INSERT_SUBREG &&
362 &II->second != IMPLICIT_DEF &&
363 &II->second != SUBREG_TO_REG)
364 NumberedInstructions.push_back(&II->second);
368 /// isLittleEndianEncoding - Return whether this target encodes its instruction
369 /// in little-endian format, i.e. bits laid out in the order [0..n]
371 bool CodeGenTarget::isLittleEndianEncoding() const {
372 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
375 //===----------------------------------------------------------------------===//
376 // ComplexPattern implementation
378 ComplexPattern::ComplexPattern(Record *R) {
379 Ty = ::getValueType(R->getValueAsDef("Ty"));
380 NumOperands = R->getValueAsInt("NumOperands");
381 SelectFunc = R->getValueAsString("SelectFunc");
382 RootNodes = R->getValueAsListOfDefs("RootNodes");
384 // Parse the properties.
386 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
387 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
388 if (PropList[i]->getName() == "SDNPHasChain") {
389 Properties |= 1 << SDNPHasChain;
390 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
391 Properties |= 1 << SDNPOptInFlag;
392 } else if (PropList[i]->getName() == "SDNPMayStore") {
393 Properties |= 1 << SDNPMayStore;
394 } else if (PropList[i]->getName() == "SDNPMayLoad") {
395 Properties |= 1 << SDNPMayLoad;
396 } else if (PropList[i]->getName() == "SDNPSideEffect") {
397 Properties |= 1 << SDNPSideEffect;
398 } else if (PropList[i]->getName() == "SDNPMemOperand") {
399 Properties |= 1 << SDNPMemOperand;
401 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
402 << "' on ComplexPattern '" << R->getName() << "'!\n";
406 // Parse the attributes.
408 PropList = R->getValueAsListOfDefs("Attributes");
409 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
410 if (PropList[i]->getName() == "CPAttrParentAsRoot") {
411 Attributes |= 1 << CPAttrParentAsRoot;
413 cerr << "Unsupported pattern attribute '" << PropList[i]->getName()
414 << "' on ComplexPattern '" << R->getName() << "'!\n";
419 //===----------------------------------------------------------------------===//
420 // CodeGenIntrinsic Implementation
421 //===----------------------------------------------------------------------===//
423 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
424 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
426 std::vector<CodeGenIntrinsic> Result;
428 for (unsigned i = 0, e = I.size(); i != e; ++i)
429 Result.push_back(CodeGenIntrinsic(I[i]));
433 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
435 std::string DefName = R->getName();
437 isOverloaded = false;
438 isCommutative = false;
440 if (DefName.size() <= 4 ||
441 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
442 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
444 EnumName = std::string(DefName.begin()+4, DefName.end());
446 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
447 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
449 TargetPrefix = R->getValueAsString("TargetPrefix");
450 Name = R->getValueAsString("LLVMName");
453 // If an explicit name isn't specified, derive one from the DefName.
456 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
457 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
459 // Verify it starts with "llvm.".
460 if (Name.size() <= 5 ||
461 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
462 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
465 // If TargetPrefix is specified, make sure that Name starts with
466 // "llvm.<targetprefix>.".
467 if (!TargetPrefix.empty()) {
468 if (Name.size() < 6+TargetPrefix.size() ||
469 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
470 != (TargetPrefix + "."))
471 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
472 TargetPrefix + ".'!";
475 // Parse the list of return types.
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 = getValueType(TyEl->getValueAsDef("VT"));
481 isOverloaded |= VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny;
482 IS.RetVTs.push_back(VT);
483 IS.RetTypeDefs.push_back(TyEl);
486 if (IS.RetVTs.size() == 0)
487 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
489 // Parse the list of parameter types.
490 TypeList = R->getValueAsListInit("ParamTypes");
491 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
492 Record *TyEl = TypeList->getElementAsRecord(i);
493 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
494 MVT::SimpleValueType VT = getValueType(TyEl->getValueAsDef("VT"));
495 isOverloaded |= VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny;
496 IS.ParamVTs.push_back(VT);
497 IS.ParamTypeDefs.push_back(TyEl);
500 // Parse the intrinsic properties.
501 ListInit *PropList = R->getValueAsListInit("Properties");
502 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
503 Record *Property = PropList->getElementAsRecord(i);
504 assert(Property->isSubClassOf("IntrinsicProperty") &&
505 "Expected a property!");
507 if (Property->getName() == "IntrNoMem")
509 else if (Property->getName() == "IntrReadArgMem")
511 else if (Property->getName() == "IntrReadMem")
513 else if (Property->getName() == "IntrWriteArgMem")
514 ModRef = WriteArgMem;
515 else if (Property->getName() == "IntrWriteMem")
517 else if (Property->getName() == "Commutative")
518 isCommutative = true;
520 assert(0 && "Unknown property!");