1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
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 tablegen backend emits information about intrinsic functions.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenTarget.h"
15 #include "IntrinsicEmitter.h"
16 #include "StringMatcher.h"
17 #include "llvm/TableGen/Record.h"
18 #include "llvm/ADT/StringExtras.h"
22 //===----------------------------------------------------------------------===//
23 // IntrinsicEmitter Implementation
24 //===----------------------------------------------------------------------===//
26 void IntrinsicEmitter::run(raw_ostream &OS) {
27 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
29 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
31 if (TargetOnly && !Ints.empty())
32 TargetPrefix = Ints[0].TargetPrefix;
36 // Emit the enum information.
37 EmitEnumInfo(Ints, OS);
39 // Emit the intrinsic ID -> name table.
40 EmitIntrinsicToNameTable(Ints, OS);
42 // Emit the intrinsic ID -> overload table.
43 EmitIntrinsicToOverloadTable(Ints, OS);
45 // Emit the function name recognizer.
46 EmitFnNameRecognizer(Ints, OS);
48 // Emit the intrinsic verifier.
49 EmitVerifier(Ints, OS);
51 // Emit the intrinsic declaration generator.
52 EmitGenerator(Ints, OS);
54 // Emit the intrinsic parameter attributes.
55 EmitAttributes(Ints, OS);
57 // Emit intrinsic alias analysis mod/ref behavior.
58 EmitModRefBehavior(Ints, OS);
60 // Emit code to translate GCC builtins into LLVM intrinsics.
61 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
66 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
67 OS << "// VisualStudio defines setjmp as _setjmp\n"
68 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
69 " !defined(setjmp_undefined_for_msvc)\n"
70 "# pragma push_macro(\"setjmp\")\n"
72 "# define setjmp_undefined_for_msvc\n"
76 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
77 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
78 "// let's return it to _setjmp state\n"
79 "# pragma pop_macro(\"setjmp\")\n"
80 "# undef setjmp_undefined_for_msvc\n"
84 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
86 OS << "// Enum values for Intrinsics.h\n";
87 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
88 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
89 OS << " " << Ints[i].EnumName;
90 OS << ((i != e-1) ? ", " : " ");
91 OS << std::string(40-Ints[i].EnumName.size(), ' ')
92 << "// " << Ints[i].Name << "\n";
97 void IntrinsicEmitter::
98 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
100 // Build a 'first character of function name' -> intrinsic # mapping.
101 std::map<char, std::vector<unsigned> > IntMapping;
102 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
103 IntMapping[Ints[i].Name[5]].push_back(i);
105 OS << "// Function name -> enum value recognizer code.\n";
106 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
107 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
108 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
109 OS << " default: break;\n";
110 // Emit the intrinsic matching stuff by first letter.
111 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
112 E = IntMapping.end(); I != E; ++I) {
113 OS << " case '" << I->first << "':\n";
114 std::vector<unsigned> &IntList = I->second;
116 // Emit all the overloaded intrinsics first, build a table of the
117 // non-overloaded ones.
118 std::vector<StringMatcher::StringPair> MatchTable;
120 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
121 unsigned IntNo = IntList[i];
122 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
123 Ints[IntNo].EnumName + ";";
125 if (!Ints[IntNo].isOverloaded) {
126 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
130 // For overloaded intrinsics, only the prefix needs to match
131 std::string TheStr = Ints[IntNo].Name.substr(6);
132 TheStr += '.'; // Require "bswap." instead of bswap.
133 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
137 // Emit the matcher logic for the fixed length strings.
138 StringMatcher("NameR", MatchTable, OS).Emit(1);
139 OS << " break; // end of '" << I->first << "' case.\n";
146 void IntrinsicEmitter::
147 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
149 OS << "// Intrinsic ID to name table\n";
150 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
151 OS << " // Note that entry #0 is the invalid intrinsic!\n";
152 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
153 OS << " \"" << Ints[i].Name << "\",\n";
157 void IntrinsicEmitter::
158 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
160 OS << "// Intrinsic ID to overload bitset\n";
161 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
162 OS << "static const uint8_t OTable[] = {\n";
164 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
165 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
168 if (Ints[i].isOverloaded)
169 OS << " | (1<<" << (i+1)%8 << ')';
172 // OTable contains a true bit at the position if the intrinsic is overloaded.
173 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
177 static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
178 if (EVT(VT).isInteger()) {
179 unsigned BitWidth = EVT(VT).getSizeInBits();
180 OS << "IntegerType::get(Context, " << BitWidth << ")";
181 } else if (VT == MVT::Other) {
182 // MVT::OtherVT is used to mean the empty struct type here.
183 OS << "StructType::get(Context)";
184 } else if (VT == MVT::f16) {
185 OS << "Type::getHalfTy(Context)";
186 } else if (VT == MVT::f32) {
187 OS << "Type::getFloatTy(Context)";
188 } else if (VT == MVT::f64) {
189 OS << "Type::getDoubleTy(Context)";
190 } else if (VT == MVT::f80) {
191 OS << "Type::getX86_FP80Ty(Context)";
192 } else if (VT == MVT::f128) {
193 OS << "Type::getFP128Ty(Context)";
194 } else if (VT == MVT::ppcf128) {
195 OS << "Type::getPPC_FP128Ty(Context)";
196 } else if (VT == MVT::isVoid) {
197 OS << "Type::getVoidTy(Context)";
198 } else if (VT == MVT::Metadata) {
199 OS << "Type::getMetadataTy(Context)";
200 } else if (VT == MVT::x86mmx) {
201 OS << "Type::getX86_MMXTy(Context)";
203 assert(false && "Unsupported ValueType!");
207 static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
210 static void EmitTypeGenerate(raw_ostream &OS,
211 const std::vector<Record*> &ArgTypes,
213 if (ArgTypes.empty())
214 return EmitTypeForValueType(OS, MVT::isVoid);
216 if (ArgTypes.size() == 1)
217 return EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
219 OS << "StructType::get(";
221 for (std::vector<Record*>::const_iterator
222 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
223 EmitTypeGenerate(OS, *I, ArgNo);
230 static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
232 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
234 if (ArgType->isSubClassOf("LLVMMatchType")) {
235 unsigned Number = ArgType->getValueAsInt("Number");
236 assert(Number < ArgNo && "Invalid matching number!");
237 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
238 OS << "VectorType::getExtendedElementVectorType"
239 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
240 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
241 OS << "VectorType::getTruncatedElementVectorType"
242 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
244 OS << "Tys[" << Number << "]";
245 } else if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny) {
246 // NOTE: The ArgNo variable here is not the absolute argument number, it is
247 // the index of the "arbitrary" type in the Tys array passed to the
248 // Intrinsic::getDeclaration function. Consequently, we only want to
249 // increment it when we actually hit an overloaded type. Getting this wrong
250 // leads to very subtle bugs!
251 OS << "Tys[" << ArgNo++ << "]";
252 } else if (EVT(VT).isVector()) {
254 OS << "VectorType::get(";
255 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT().SimpleTy);
256 OS << ", " << VVT.getVectorNumElements() << ")";
257 } else if (VT == MVT::iPTR) {
258 OS << "PointerType::getUnqual(";
259 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
261 } else if (VT == MVT::iPTRAny) {
262 // Make sure the user has passed us an argument type to overload. If not,
263 // treat it as an ordinary (not overloaded) intrinsic.
264 OS << "(" << ArgNo << " < Tys.size()) ? Tys[" << ArgNo
265 << "] : PointerType::getUnqual(";
266 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
269 } else if (VT == MVT::isVoid) {
271 OS << "Type::getVoidTy(Context)";
273 // MVT::isVoid is used to mean varargs here.
276 EmitTypeForValueType(OS, VT);
280 /// RecordListComparator - Provide a deterministic comparator for lists of
283 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
284 struct RecordListComparator {
285 bool operator()(const RecPair &LHS,
286 const RecPair &RHS) const {
288 const std::vector<Record*> *LHSVec = &LHS.first;
289 const std::vector<Record*> *RHSVec = &RHS.first;
290 unsigned RHSSize = RHSVec->size();
291 unsigned LHSSize = LHSVec->size();
293 for (; i != LHSSize; ++i) {
294 if (i == RHSSize) return false; // RHS is shorter than LHS.
295 if ((*LHSVec)[i] != (*RHSVec)[i])
296 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
299 if (i != RHSSize) return true;
302 LHSVec = &LHS.second;
303 RHSVec = &RHS.second;
304 RHSSize = RHSVec->size();
305 LHSSize = LHSVec->size();
307 for (i = 0; i != LHSSize; ++i) {
308 if (i == RHSSize) return false; // RHS is shorter than LHS.
309 if ((*LHSVec)[i] != (*RHSVec)[i])
310 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
318 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
320 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
321 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
322 OS << " switch (ID) {\n";
323 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
325 // This checking can emit a lot of very common code. To reduce the amount of
326 // code that we emit, batch up cases that have identical types. This avoids
327 // problems where GCC can run out of memory compiling Verifier.cpp.
328 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
329 MapTy UniqueArgInfos;
331 // Compute the unique argument type info.
332 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
333 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
334 Ints[i].IS.ParamTypeDefs)].push_back(i);
336 // Loop through the array, emitting one comparison for each batch.
337 for (MapTy::iterator I = UniqueArgInfos.begin(),
338 E = UniqueArgInfos.end(); I != E; ++I) {
339 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
340 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
341 << Ints[I->second[i]].Name << "\n";
343 const RecPair &ArgTypes = I->first;
344 const std::vector<Record*> &RetTys = ArgTypes.first;
345 const std::vector<Record*> &ParamTys = ArgTypes.second;
346 std::vector<unsigned> OverloadedTypeIndices;
348 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
351 // Emit return types.
352 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
353 Record *ArgType = RetTys[j];
356 if (ArgType->isSubClassOf("LLVMMatchType")) {
357 unsigned Number = ArgType->getValueAsInt("Number");
358 assert(Number < OverloadedTypeIndices.size() &&
359 "Invalid matching number!");
360 Number = OverloadedTypeIndices[Number];
361 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
362 OS << "~(ExtendedElementVectorType | " << Number << ")";
363 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
364 OS << "~(TruncatedElementVectorType | " << Number << ")";
368 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
369 OS << getEnumName(VT);
371 if (EVT(VT).isOverloaded())
372 OverloadedTypeIndices.push_back(j);
374 if (VT == MVT::isVoid && j != 0 && j != je - 1)
375 throw "Var arg type not last argument";
379 // Emit the parameter types.
380 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
381 Record *ArgType = ParamTys[j];
384 if (ArgType->isSubClassOf("LLVMMatchType")) {
385 unsigned Number = ArgType->getValueAsInt("Number");
386 assert(Number < OverloadedTypeIndices.size() &&
387 "Invalid matching number!");
388 Number = OverloadedTypeIndices[Number];
389 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
390 OS << "~(ExtendedElementVectorType | " << Number << ")";
391 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
392 OS << "~(TruncatedElementVectorType | " << Number << ")";
396 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
397 OS << getEnumName(VT);
399 if (EVT(VT).isOverloaded())
400 OverloadedTypeIndices.push_back(j + RetTys.size());
402 if (VT == MVT::isVoid && j != 0 && j != je - 1)
403 throw "Var arg type not last argument";
414 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
416 OS << "// Code for generating Intrinsic function declarations.\n";
417 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
418 OS << " switch (id) {\n";
419 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
421 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
423 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
424 MapTy UniqueArgInfos;
426 // Compute the unique argument type info.
427 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
428 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
429 Ints[i].IS.ParamTypeDefs)].push_back(i);
431 // Loop through the array, emitting one generator for each batch.
432 std::string IntrinsicStr = TargetPrefix + "Intrinsic::";
434 for (MapTy::iterator I = UniqueArgInfos.begin(),
435 E = UniqueArgInfos.end(); I != E; ++I) {
436 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
437 OS << " case " << IntrinsicStr << Ints[I->second[i]].EnumName
438 << ":\t\t// " << Ints[I->second[i]].Name << "\n";
440 const RecPair &ArgTypes = I->first;
441 const std::vector<Record*> &RetTys = ArgTypes.first;
442 const std::vector<Record*> &ParamTys = ArgTypes.second;
444 unsigned N = ParamTys.size();
447 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
448 OS << " IsVarArg = true;\n";
453 OS << " ResultTy = ";
454 EmitTypeGenerate(OS, RetTys, ArgNo);
457 for (unsigned j = 0; j != N; ++j) {
458 OS << " ArgTys.push_back(";
459 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
477 ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
478 switch (intrinsic.ModRef) {
479 case CodeGenIntrinsic::NoMem:
481 case CodeGenIntrinsic::ReadArgMem:
482 case CodeGenIntrinsic::ReadMem:
484 case CodeGenIntrinsic::ReadWriteArgMem:
485 case CodeGenIntrinsic::ReadWriteMem:
488 llvm_unreachable("bad mod-ref kind");
491 struct AttributeComparator {
492 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
493 // Sort throwing intrinsics after non-throwing intrinsics.
494 if (L->canThrow != R->canThrow)
497 // Try to order by readonly/readnone attribute.
498 ModRefKind LK = getModRefKind(*L);
499 ModRefKind RK = getModRefKind(*R);
500 if (LK != RK) return (LK > RK);
502 // Order by argument attributes.
503 // This is reliable because each side is already sorted internally.
504 return (L->ArgumentAttributes < R->ArgumentAttributes);
509 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
510 void IntrinsicEmitter::
511 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
512 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
513 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
515 OS << "static AttrListPtr getAttributes(" << TargetPrefix
516 << "Intrinsic::ID id) {\n";
518 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {\n";
520 // Compute the maximum number of attribute arguments and the map
521 typedef std::map<const CodeGenIntrinsic*, unsigned,
522 AttributeComparator> UniqAttrMapTy;
523 UniqAttrMapTy UniqAttributes;
524 unsigned maxArgAttrs = 0;
525 unsigned AttrNum = 0;
526 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
527 const CodeGenIntrinsic &intrinsic = Ints[i];
529 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
530 unsigned &N = UniqAttributes[&intrinsic];
532 assert(AttrNum < 256 && "Too many unique attributes for table!");
536 // Emit an array of AttributeWithIndex. Most intrinsics will have
537 // at least one entry, for the function itself (index ~1), which is
539 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
540 OS << " 255, // Invalid intrinsic\n";
542 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
543 const CodeGenIntrinsic &intrinsic = Ints[i];
545 OS << " " << UniqAttributes[&intrinsic] << ", // "
546 << intrinsic.Name << "\n";
550 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n";
551 OS << " unsigned NumAttrs = 0;\n";
552 OS << " switch(IntrinsicsToAttributesMap[id]) {\n";
553 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
554 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
555 E = UniqAttributes.end(); I != E; ++I) {
556 OS << " case " << I->second << ":\n";
558 const CodeGenIntrinsic &intrinsic = *(I->first);
560 // Keep track of the number of attributes we're writing out.
561 unsigned numAttrs = 0;
563 // The argument attributes are alreadys sorted by argument index.
564 for (unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); ai != ae;) {
565 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
567 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get("
570 bool moreThanOne = false;
573 if (moreThanOne) OS << '|';
575 switch (intrinsic.ArgumentAttributes[ai].second) {
576 case CodeGenIntrinsic::NoCapture:
577 OS << "Attribute::NoCapture";
583 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
588 ModRefKind modRef = getModRefKind(intrinsic);
590 if (!intrinsic.canThrow || modRef) {
591 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(~0, ";
592 if (!intrinsic.canThrow) {
593 OS << "Attribute::NoUnwind";
594 if (modRef) OS << '|';
597 case MRK_none: break;
598 case MRK_readonly: OS << "Attribute::ReadOnly"; break;
599 case MRK_readnone: OS << "Attribute::ReadNone"; break;
605 OS << " NumAttrs = " << numAttrs << ";\n";
608 OS << " return AttrListPtr();\n";
613 OS << " return AttrListPtr::get(AWI, NumAttrs);\n";
615 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
618 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
619 void IntrinsicEmitter::
620 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
621 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
622 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
623 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
624 << "\"Unknown intrinsic.\");\n\n";
626 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
627 << " /* invalid */ UnknownModRefBehavior,\n";
628 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
629 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
630 switch (Ints[i].ModRef) {
631 case CodeGenIntrinsic::NoMem:
632 OS << "DoesNotAccessMemory,\n";
634 case CodeGenIntrinsic::ReadArgMem:
635 OS << "OnlyReadsArgumentPointees,\n";
637 case CodeGenIntrinsic::ReadMem:
638 OS << "OnlyReadsMemory,\n";
640 case CodeGenIntrinsic::ReadWriteArgMem:
641 OS << "OnlyAccessesArgumentPointees,\n";
643 case CodeGenIntrinsic::ReadWriteMem:
644 OS << "UnknownModRefBehavior,\n";
649 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
650 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
653 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
654 /// same target, and we already checked it.
655 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
656 const std::string &TargetPrefix,
659 std::vector<StringMatcher::StringPair> Results;
661 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
662 E = BIM.end(); I != E; ++I) {
663 std::string ResultCode =
664 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
665 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
668 StringMatcher("BuiltinName", Results, OS).Emit();
672 void IntrinsicEmitter::
673 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
675 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
677 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
678 if (!Ints[i].GCCBuiltinName.empty()) {
679 // Get the map for this target prefix.
680 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
682 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
683 Ints[i].EnumName)).second)
684 throw "Intrinsic '" + Ints[i].TheDef->getName() +
685 "': duplicate GCC builtin name!";
689 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
690 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
691 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
692 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
693 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
696 OS << "static " << TargetPrefix << "Intrinsic::ID "
697 << "getIntrinsicForGCCBuiltin(const char "
698 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
700 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
701 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
704 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
705 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
707 // Note: this could emit significantly better code if we cared.
708 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
710 if (!I->first.empty())
711 OS << "if (TargetPrefix == \"" << I->first << "\") ";
713 OS << "/* Target Independent Builtins */ ";
716 // Emit the comparisons for this target prefix.
717 EmitTargetBuiltins(I->second, TargetPrefix, OS);
721 if (!TargetPrefix.empty())
722 OS << "(" << TargetPrefix << "Intrinsic::ID)";
723 OS << "Intrinsic::not_intrinsic;\n";