1 //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper ---------*- C++ -*-===//
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 wrap 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"
27 static cl::opt<unsigned>
28 AsmWriterNum("asmwriternum", cl::init(0),
29 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
31 /// getValueType - Return the MCV::ValueType that the specified TableGen record
33 MVT::ValueType llvm::getValueType(Record *Rec) {
34 return (MVT::ValueType)Rec->getValueAsInt("Value");
37 std::string llvm::getName(MVT::ValueType T) {
39 case MVT::Other: return "UNKNOWN";
40 case MVT::i1: return "MVT::i1";
41 case MVT::i8: return "MVT::i8";
42 case MVT::i16: return "MVT::i16";
43 case MVT::i32: return "MVT::i32";
44 case MVT::i64: return "MVT::i64";
45 case MVT::i128: return "MVT::i128";
46 case MVT::iAny: return "MVT::iAny";
47 case MVT::fAny: return "MVT::fAny";
48 case MVT::f32: return "MVT::f32";
49 case MVT::f64: return "MVT::f64";
50 case MVT::f80: return "MVT::f80";
51 case MVT::f128: return "MVT::f128";
52 case MVT::ppcf128: return "MVT::ppcf128";
53 case MVT::Flag: return "MVT::Flag";
54 case MVT::isVoid:return "MVT::void";
55 case MVT::v8i8: return "MVT::v8i8";
56 case MVT::v4i16: return "MVT::v4i16";
57 case MVT::v2i32: return "MVT::v2i32";
58 case MVT::v1i64: return "MVT::v1i64";
59 case MVT::v16i8: return "MVT::v16i8";
60 case MVT::v8i16: return "MVT::v8i16";
61 case MVT::v4i32: return "MVT::v4i32";
62 case MVT::v2i64: return "MVT::v2i64";
63 case MVT::v2f32: return "MVT::v2f32";
64 case MVT::v4f32: return "MVT::v4f32";
65 case MVT::v2f64: return "MVT::v2f64";
66 case MVT::v3i32: return "MVT::v3i32";
67 case MVT::v3f32: return "MVT::v3f32";
68 case MVT::iPTR: return "TLI.getPointerTy()";
69 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
73 std::string llvm::getEnumName(MVT::ValueType 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 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
109 /// getQualifiedName - Return the name of the specified record, with a
110 /// namespace qualifier if the record contains one.
112 std::string llvm::getQualifiedName(const Record *R) {
113 std::string Namespace = R->getValueAsString("Namespace");
114 if (Namespace.empty()) return R->getName();
115 return Namespace + "::" + R->getName();
121 /// getTarget - Return the current instance of the Target class.
123 CodeGenTarget::CodeGenTarget() {
124 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
125 if (Targets.size() == 0)
126 throw std::string("ERROR: No 'Target' subclasses defined!");
127 if (Targets.size() != 1)
128 throw std::string("ERROR: Multiple subclasses of Target defined!");
129 TargetRec = Targets[0];
133 const std::string &CodeGenTarget::getName() const {
134 return TargetRec->getName();
137 Record *CodeGenTarget::getInstructionSet() const {
138 return TargetRec->getValueAsDef("InstructionSet");
141 /// getAsmWriter - Return the AssemblyWriter definition for this target.
143 Record *CodeGenTarget::getAsmWriter() const {
144 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
145 if (AsmWriterNum >= LI.size())
146 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
147 return LI[AsmWriterNum];
150 void CodeGenTarget::ReadRegisters() const {
151 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
153 throw std::string("No 'Register' subclasses defined!");
155 Registers.reserve(Regs.size());
156 Registers.assign(Regs.begin(), Regs.end());
159 CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
160 DeclaredSpillSize = R->getValueAsInt("SpillSize");
161 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
164 const std::string &CodeGenRegister::getName() const {
165 return TheDef->getName();
168 void CodeGenTarget::ReadRegisterClasses() const {
169 std::vector<Record*> RegClasses =
170 Records.getAllDerivedDefinitions("RegisterClass");
171 if (RegClasses.empty())
172 throw std::string("No 'RegisterClass' subclasses defined!");
174 RegisterClasses.reserve(RegClasses.size());
175 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
178 std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
179 std::vector<unsigned char> Result;
180 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
181 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
182 const CodeGenRegisterClass &RC = RegisterClasses[i];
183 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
184 if (R == RC.Elements[ei]) {
185 const std::vector<MVT::ValueType> &InVTs = RC.getValueTypes();
186 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
187 Result.push_back(InVTs[i]);
195 CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
196 // Rename anonymous register classes.
197 if (R->getName().size() > 9 && R->getName()[9] == '.') {
198 static unsigned AnonCounter = 0;
199 R->setName("AnonRegClass_"+utostr(AnonCounter++));
202 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
203 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
204 Record *Type = TypeList[i];
205 if (!Type->isSubClassOf("ValueType"))
206 throw "RegTypes list member '" + Type->getName() +
207 "' does not derive from the ValueType class!";
208 VTs.push_back(getValueType(Type));
210 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
212 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
213 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
214 Record *Reg = RegList[i];
215 if (!Reg->isSubClassOf("Register"))
216 throw "Register Class member '" + Reg->getName() +
217 "' does not derive from the Register class!";
218 Elements.push_back(Reg);
221 std::vector<Record*> SubRegClassList =
222 R->getValueAsListOfDefs("SubRegClassList");
223 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
224 Record *SubRegClass = SubRegClassList[i];
225 if (!SubRegClass->isSubClassOf("RegisterClass"))
226 throw "Register Class member '" + SubRegClass->getName() +
227 "' does not derive from the RegisterClass class!";
228 SubRegClasses.push_back(SubRegClass);
231 // Allow targets to override the size in bits of the RegisterClass.
232 unsigned Size = R->getValueAsInt("Size");
234 Namespace = R->getValueAsString("Namespace");
235 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
236 SpillAlignment = R->getValueAsInt("Alignment");
237 CopyCost = R->getValueAsInt("CopyCost");
238 MethodBodies = R->getValueAsCode("MethodBodies");
239 MethodProtos = R->getValueAsCode("MethodProtos");
242 const std::string &CodeGenRegisterClass::getName() const {
243 return TheDef->getName();
246 void CodeGenTarget::ReadLegalValueTypes() const {
247 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
248 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
249 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
250 LegalValueTypes.push_back(RCs[i].VTs[ri]);
252 // Remove duplicates.
253 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
254 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
255 LegalValueTypes.end()),
256 LegalValueTypes.end());
260 void CodeGenTarget::ReadInstructions() const {
261 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
262 if (Insts.size() <= 2)
263 throw std::string("No 'Instruction' subclasses defined!");
265 // Parse the instructions defined in the .td file.
266 std::string InstFormatName =
267 getAsmWriter()->getValueAsString("InstFormatName");
269 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
270 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
271 Instructions.insert(std::make_pair(Insts[i]->getName(),
272 CodeGenInstruction(Insts[i], AsmStr)));
276 /// getInstructionsByEnumValue - Return all of the instructions defined by the
277 /// target, ordered by their enum value.
279 getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
280 &NumberedInstructions) {
281 std::map<std::string, CodeGenInstruction>::const_iterator I;
282 I = getInstructions().find("PHI");
283 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
284 const CodeGenInstruction *PHI = &I->second;
286 I = getInstructions().find("INLINEASM");
287 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
288 const CodeGenInstruction *INLINEASM = &I->second;
290 I = getInstructions().find("LABEL");
291 if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!";
292 const CodeGenInstruction *LABEL = &I->second;
294 I = getInstructions().find("EXTRACT_SUBREG");
295 if (I == Instructions.end())
296 throw "Could not find 'EXTRACT_SUBREG' instruction!";
297 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
299 I = getInstructions().find("INSERT_SUBREG");
300 if (I == Instructions.end())
301 throw "Could not find 'INSERT_SUBREG' instruction!";
302 const CodeGenInstruction *INSERT_SUBREG = &I->second;
304 // Print out the rest of the instructions now.
305 NumberedInstructions.push_back(PHI);
306 NumberedInstructions.push_back(INLINEASM);
307 NumberedInstructions.push_back(LABEL);
308 NumberedInstructions.push_back(EXTRACT_SUBREG);
309 NumberedInstructions.push_back(INSERT_SUBREG);
310 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
311 if (&II->second != PHI &&
312 &II->second != INLINEASM &&
313 &II->second != LABEL &&
314 &II->second != EXTRACT_SUBREG &&
315 &II->second != INSERT_SUBREG)
316 NumberedInstructions.push_back(&II->second);
320 /// isLittleEndianEncoding - Return whether this target encodes its instruction
321 /// in little-endian format, i.e. bits laid out in the order [0..n]
323 bool CodeGenTarget::isLittleEndianEncoding() const {
324 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
329 static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
330 // FIXME: Only supports TIED_TO for now.
331 std::string::size_type pos = CStr.find_first_of('=');
332 assert(pos != std::string::npos && "Unrecognized constraint");
333 std::string Name = CStr.substr(0, pos);
335 // TIED_TO: $src1 = $dst
336 std::string::size_type wpos = Name.find_first_of(" \t");
337 if (wpos == std::string::npos)
338 throw "Illegal format for tied-to constraint: '" + CStr + "'";
339 std::string DestOpName = Name.substr(0, wpos);
340 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
342 Name = CStr.substr(pos+1);
343 wpos = Name.find_first_not_of(" \t");
344 if (wpos == std::string::npos)
345 throw "Illegal format for tied-to constraint: '" + CStr + "'";
347 std::pair<unsigned,unsigned> SrcOp =
348 I->ParseOperandName(Name.substr(wpos), false);
350 throw "Illegal tied-to operand constraint '" + CStr + "'";
353 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
354 // Build the string for the operand.
355 std::string OpConstraint =
356 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
359 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
360 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
361 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
364 static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
365 // Make sure the constraints list for each operand is large enough to hold
366 // constraint info, even if none is present.
367 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
368 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
370 if (CStr.empty()) return;
372 const std::string delims(",");
373 std::string::size_type bidx, eidx;
375 bidx = CStr.find_first_not_of(delims);
376 while (bidx != std::string::npos) {
377 eidx = CStr.find_first_of(delims, bidx);
378 if (eidx == std::string::npos)
379 eidx = CStr.length();
381 ParseConstraint(CStr.substr(bidx, eidx), I);
382 bidx = CStr.find_first_not_of(delims, eidx);
386 CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
387 : TheDef(R), AsmString(AsmStr) {
388 Name = R->getValueAsString("Name");
389 Namespace = R->getValueAsString("Namespace");
391 isReturn = R->getValueAsBit("isReturn");
392 isBranch = R->getValueAsBit("isBranch");
393 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
394 isBarrier = R->getValueAsBit("isBarrier");
395 isCall = R->getValueAsBit("isCall");
396 isLoad = R->getValueAsBit("isLoad");
397 isStore = R->getValueAsBit("isStore");
398 isImplicitDef= R->getValueAsBit("isImplicitDef");
399 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
400 isPredicable = R->getValueAsBit("isPredicable");
401 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
402 isCommutable = R->getValueAsBit("isCommutable");
403 isTerminator = R->getValueAsBit("isTerminator");
404 isReMaterializable = R->getValueAsBit("isReMaterializable");
405 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
406 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
407 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
408 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
409 mayHaveSideEffects = R->getValueAsBit("mayHaveSideEffects");
410 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
411 hasOptionalDef = false;
412 hasVariableNumberOfOperands = false;
414 if (mayHaveSideEffects && neverHasSideEffects)
416 ": cannot have both 'mayHaveSideEffects' and 'neverHasSideEffects' set!";
420 DI = R->getValueAsDag("OutOperandList");
422 // Error getting operand list, just ignore it (sparcv9).
427 NumDefs = DI->getNumArgs();
431 IDI = R->getValueAsDag("InOperandList");
433 // Error getting operand list, just ignore it (sparcv9).
438 DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI))->Fold();
440 unsigned MIOperandNo = 0;
441 std::set<std::string> OperandNames;
442 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
443 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
445 throw "Illegal operand for the '" + R->getName() + "' instruction!";
447 Record *Rec = Arg->getDef();
448 std::string PrintMethod = "printOperand";
450 DagInit *MIOpInfo = 0;
451 if (Rec->isSubClassOf("Operand")) {
452 PrintMethod = Rec->getValueAsString("PrintMethod");
453 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
455 // Verify that MIOpInfo has an 'ops' root value.
456 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
457 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
458 ->getDef()->getName() != "ops")
459 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
462 // If we have MIOpInfo, then we have #operands equal to number of entries
464 if (unsigned NumArgs = MIOpInfo->getNumArgs())
467 if (Rec->isSubClassOf("PredicateOperand"))
469 else if (Rec->isSubClassOf("OptionalDefOperand"))
470 hasOptionalDef = true;
471 } else if (Rec->getName() == "variable_ops") {
472 hasVariableNumberOfOperands = true;
474 } else if (!Rec->isSubClassOf("RegisterClass") &&
475 Rec->getName() != "ptr_rc")
476 throw "Unknown operand class '" + Rec->getName() +
477 "' in instruction '" + R->getName() + "' instruction!";
479 // Check that the operand has a name and that it's unique.
480 if (DI->getArgName(i).empty())
481 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
483 if (!OperandNames.insert(DI->getArgName(i)).second)
484 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
485 " has the same name as a previous operand!";
487 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
488 MIOperandNo, NumOps, MIOpInfo));
489 MIOperandNo += NumOps;
492 // Parse Constraints.
493 ParseConstraints(R->getValueAsString("Constraints"), this);
495 // For backward compatibility: isTwoAddress means operand 1 is tied to
498 if (!OperandList[1].Constraints[0].empty())
499 throw R->getName() + ": cannot use isTwoAddress property: instruction "
500 "already has constraint set!";
501 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
504 // Any operands with unset constraints get 0 as their constraint.
505 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
506 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
507 if (OperandList[op].Constraints[j].empty())
508 OperandList[op].Constraints[j] = "0";
510 // Parse the DisableEncoding field.
511 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
513 std::string OpName = getToken(DisableEncoding, " ,\t");
514 if (OpName.empty()) break;
516 // Figure out which operand this is.
517 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
519 // Mark the operand as not-to-be encoded.
520 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
521 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
522 OperandList[Op.first].DoNotEncode[Op.second] = true;
528 /// getOperandNamed - Return the index of the operand with the specified
529 /// non-empty name. If the instruction does not have an operand with the
530 /// specified name, throw an exception.
532 unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
533 assert(!Name.empty() && "Cannot search for operand with no name!");
534 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
535 if (OperandList[i].Name == Name) return i;
536 throw "Instruction '" + TheDef->getName() +
537 "' does not have an operand named '$" + Name + "'!";
540 std::pair<unsigned,unsigned>
541 CodeGenInstruction::ParseOperandName(const std::string &Op,
543 if (Op.empty() || Op[0] != '$')
544 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
546 std::string OpName = Op.substr(1);
547 std::string SubOpName;
549 // Check to see if this is $foo.bar.
550 std::string::size_type DotIdx = OpName.find_first_of(".");
551 if (DotIdx != std::string::npos) {
552 SubOpName = OpName.substr(DotIdx+1);
553 if (SubOpName.empty())
554 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
555 OpName = OpName.substr(0, DotIdx);
558 unsigned OpIdx = getOperandNamed(OpName);
560 if (SubOpName.empty()) { // If no suboperand name was specified:
561 // If one was needed, throw.
562 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
564 throw TheDef->getName() + ": Illegal to refer to"
565 " whole operand part of complex operand '" + Op + "'";
567 // Otherwise, return the operand.
568 return std::make_pair(OpIdx, 0U);
571 // Find the suboperand number involved.
572 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
574 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
576 // Find the operand with the right name.
577 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
578 if (MIOpInfo->getArgName(i) == SubOpName)
579 return std::make_pair(OpIdx, i);
581 // Otherwise, didn't find it!
582 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
588 //===----------------------------------------------------------------------===//
589 // ComplexPattern implementation
591 ComplexPattern::ComplexPattern(Record *R) {
592 Ty = ::getValueType(R->getValueAsDef("Ty"));
593 NumOperands = R->getValueAsInt("NumOperands");
594 SelectFunc = R->getValueAsString("SelectFunc");
595 RootNodes = R->getValueAsListOfDefs("RootNodes");
597 // Parse the properties.
599 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
600 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
601 if (PropList[i]->getName() == "SDNPHasChain") {
602 Properties |= 1 << SDNPHasChain;
603 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
604 Properties |= 1 << SDNPOptInFlag;
606 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
607 << "' on ComplexPattern '" << R->getName() << "'!\n";
612 //===----------------------------------------------------------------------===//
613 // CodeGenIntrinsic Implementation
614 //===----------------------------------------------------------------------===//
616 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
617 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
619 std::vector<CodeGenIntrinsic> Result;
621 // If we are in the context of a target .td file, get the target info so that
622 // we can decode the current intptr_t.
623 CodeGenTarget *CGT = 0;
624 if (Records.getClass("Target") &&
625 Records.getAllDerivedDefinitions("Target").size() == 1)
626 CGT = new CodeGenTarget();
628 for (unsigned i = 0, e = I.size(); i != e; ++i)
629 Result.push_back(CodeGenIntrinsic(I[i], CGT));
634 CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) {
636 std::string DefName = R->getName();
638 isOverloaded = false;
640 if (DefName.size() <= 4 ||
641 std::string(DefName.begin(), DefName.begin()+4) != "int_")
642 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
643 EnumName = std::string(DefName.begin()+4, DefName.end());
644 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
645 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
646 TargetPrefix = R->getValueAsString("TargetPrefix");
647 Name = R->getValueAsString("LLVMName");
649 // If an explicit name isn't specified, derive one from the DefName.
651 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
652 if (EnumName[i] == '_')
657 // Verify it starts with "llvm.".
658 if (Name.size() <= 5 ||
659 std::string(Name.begin(), Name.begin()+5) != "llvm.")
660 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
663 // If TargetPrefix is specified, make sure that Name starts with
664 // "llvm.<targetprefix>.".
665 if (!TargetPrefix.empty()) {
666 if (Name.size() < 6+TargetPrefix.size() ||
667 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
668 != (TargetPrefix+"."))
669 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
670 TargetPrefix + ".'!";
673 // Parse the list of argument types.
674 ListInit *TypeList = R->getValueAsListInit("Types");
675 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
676 Record *TyEl = TypeList->getElementAsRecord(i);
677 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
678 MVT::ValueType VT = getValueType(TyEl->getValueAsDef("VT"));
679 isOverloaded |= VT == MVT::iAny || VT == MVT::fAny;
680 ArgVTs.push_back(VT);
681 ArgTypeDefs.push_back(TyEl);
683 if (ArgVTs.size() == 0)
684 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
687 // Parse the intrinsic properties.
688 ListInit *PropList = R->getValueAsListInit("Properties");
689 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
690 Record *Property = PropList->getElementAsRecord(i);
691 assert(Property->isSubClassOf("IntrinsicProperty") &&
692 "Expected a property!");
694 if (Property->getName() == "IntrNoMem")
696 else if (Property->getName() == "IntrReadArgMem")
698 else if (Property->getName() == "IntrReadMem")
700 else if (Property->getName() == "IntrWriteArgMem")
701 ModRef = WriteArgMem;
702 else if (Property->getName() == "IntrWriteMem")
705 assert(0 && "Unknown property!");