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 "CodeGenIntrinsics.h"
15 #include "CodeGenTarget.h"
16 #include "SequenceToOffsetTable.h"
17 #include "TableGenBackends.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/TableGen/Record.h"
21 #include "llvm/TableGen/StringMatcher.h"
22 #include "llvm/TableGen/TableGenBackend.h"
27 class IntrinsicEmitter {
28 RecordKeeper &Records;
30 std::string TargetPrefix;
33 IntrinsicEmitter(RecordKeeper &R, bool T)
34 : Records(R), TargetOnly(T) {}
36 void run(raw_ostream &OS);
38 void EmitPrefix(raw_ostream &OS);
40 void EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
43 void EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
45 void EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
47 void EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
49 void EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
51 void EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints,
53 void EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints,
55 void EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
57 void EmitSuffix(raw_ostream &OS);
59 } // End anonymous namespace
61 //===----------------------------------------------------------------------===//
62 // IntrinsicEmitter Implementation
63 //===----------------------------------------------------------------------===//
65 void IntrinsicEmitter::run(raw_ostream &OS) {
66 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
68 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
70 if (TargetOnly && !Ints.empty())
71 TargetPrefix = Ints[0].TargetPrefix;
75 // Emit the enum information.
76 EmitEnumInfo(Ints, OS);
78 // Emit the intrinsic ID -> name table.
79 EmitIntrinsicToNameTable(Ints, OS);
81 // Emit the intrinsic ID -> overload table.
82 EmitIntrinsicToOverloadTable(Ints, OS);
84 // Emit the function name recognizer.
85 EmitFnNameRecognizer(Ints, OS);
87 // Emit the intrinsic declaration generator.
88 EmitGenerator(Ints, OS);
90 // Emit the intrinsic parameter attributes.
91 EmitAttributes(Ints, OS);
93 // Emit intrinsic alias analysis mod/ref behavior.
94 EmitModRefBehavior(Ints, OS);
96 // Emit code to translate GCC builtins into LLVM intrinsics.
97 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
102 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
103 OS << "// VisualStudio defines setjmp as _setjmp\n"
104 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
105 " !defined(setjmp_undefined_for_msvc)\n"
106 "# pragma push_macro(\"setjmp\")\n"
108 "# define setjmp_undefined_for_msvc\n"
112 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
113 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
114 "// let's return it to _setjmp state\n"
115 "# pragma pop_macro(\"setjmp\")\n"
116 "# undef setjmp_undefined_for_msvc\n"
120 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
122 OS << "// Enum values for Intrinsics.h\n";
123 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
124 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
125 OS << " " << Ints[i].EnumName;
126 OS << ((i != e-1) ? ", " : " ");
127 OS << std::string(40-Ints[i].EnumName.size(), ' ')
128 << "// " << Ints[i].Name << "\n";
133 void IntrinsicEmitter::
134 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
136 // Build a 'first character of function name' -> intrinsic # mapping.
137 std::map<char, std::vector<unsigned> > IntMapping;
138 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
139 IntMapping[Ints[i].Name[5]].push_back(i);
141 OS << "// Function name -> enum value recognizer code.\n";
142 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
143 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
144 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
145 OS << " default: break;\n";
146 // Emit the intrinsic matching stuff by first letter.
147 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
148 E = IntMapping.end(); I != E; ++I) {
149 OS << " case '" << I->first << "':\n";
150 std::vector<unsigned> &IntList = I->second;
152 // Sort in reverse order of intrinsic name so "abc.def" appears after
153 // "abd.def.ghi" in the overridden name matcher
154 std::sort(IntList.begin(), IntList.end(), [&](unsigned i, unsigned j) {
155 return Ints[i].Name > Ints[j].Name;
158 // Emit all the overloaded intrinsics first, build a table of the
159 // non-overloaded ones.
160 std::vector<StringMatcher::StringPair> MatchTable;
162 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
163 unsigned IntNo = IntList[i];
164 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
165 Ints[IntNo].EnumName + ";";
167 if (!Ints[IntNo].isOverloaded) {
168 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
172 // For overloaded intrinsics, only the prefix needs to match
173 std::string TheStr = Ints[IntNo].Name.substr(6);
174 TheStr += '.'; // Require "bswap." instead of bswap.
175 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
179 // Emit the matcher logic for the fixed length strings.
180 StringMatcher("NameR", MatchTable, OS).Emit(1);
181 OS << " break; // end of '" << I->first << "' case.\n";
188 void IntrinsicEmitter::
189 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
191 OS << "// Intrinsic ID to name table\n";
192 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
193 OS << " // Note that entry #0 is the invalid intrinsic!\n";
194 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
195 OS << " \"" << Ints[i].Name << "\",\n";
199 void IntrinsicEmitter::
200 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
202 OS << "// Intrinsic ID to overload bitset\n";
203 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
204 OS << "static const uint8_t OTable[] = {\n";
206 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
207 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
210 if (Ints[i].isOverloaded)
211 OS << " | (1<<" << (i+1)%8 << ')';
214 // OTable contains a true bit at the position if the intrinsic is overloaded.
215 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
220 // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
222 // Common values should be encoded with 0-15.
240 // Values from 16+ are only encodable with the inefficient encoding.
243 IIT_EMPTYSTRUCT = 18,
253 IIT_HALF_VEC_ARG = 28
257 static void EncodeFixedValueType(MVT::SimpleValueType VT,
258 std::vector<unsigned char> &Sig) {
259 if (MVT(VT).isInteger()) {
260 unsigned BitWidth = MVT(VT).getSizeInBits();
262 default: PrintFatalError("unhandled integer type width in intrinsic!");
263 case 1: return Sig.push_back(IIT_I1);
264 case 8: return Sig.push_back(IIT_I8);
265 case 16: return Sig.push_back(IIT_I16);
266 case 32: return Sig.push_back(IIT_I32);
267 case 64: return Sig.push_back(IIT_I64);
272 default: PrintFatalError("unhandled MVT in intrinsic!");
273 case MVT::f16: return Sig.push_back(IIT_F16);
274 case MVT::f32: return Sig.push_back(IIT_F32);
275 case MVT::f64: return Sig.push_back(IIT_F64);
276 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
277 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
278 // MVT::OtherVT is used to mean the empty struct type here.
279 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
280 // MVT::isVoid is used to represent varargs here.
281 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
286 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
289 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
290 std::vector<unsigned char> &Sig) {
292 if (R->isSubClassOf("LLVMMatchType")) {
293 unsigned Number = R->getValueAsInt("Number");
294 assert(Number < ArgCodes.size() && "Invalid matching number!");
295 if (R->isSubClassOf("LLVMExtendedType"))
296 Sig.push_back(IIT_EXTEND_ARG);
297 else if (R->isSubClassOf("LLVMTruncatedType"))
298 Sig.push_back(IIT_TRUNC_ARG);
299 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
300 Sig.push_back(IIT_HALF_VEC_ARG);
302 Sig.push_back(IIT_ARG);
303 return Sig.push_back((Number << 2) | ArgCodes[Number]);
306 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
311 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
312 case MVT::vAny: ++Tmp; // FALL THROUGH.
313 case MVT::fAny: ++Tmp; // FALL THROUGH.
315 // If this is an "any" valuetype, then the type is the type of the next
316 // type in the list specified to getIntrinsic().
317 Sig.push_back(IIT_ARG);
319 // Figure out what arg # this is consuming, and remember what kind it was.
320 unsigned ArgNo = ArgCodes.size();
321 ArgCodes.push_back(Tmp);
323 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
324 return Sig.push_back((ArgNo << 2) | Tmp);
328 unsigned AddrSpace = 0;
329 if (R->isSubClassOf("LLVMQualPointerType")) {
330 AddrSpace = R->getValueAsInt("AddrSpace");
331 assert(AddrSpace < 256 && "Address space exceeds 255");
334 Sig.push_back(IIT_ANYPTR);
335 Sig.push_back(AddrSpace);
337 Sig.push_back(IIT_PTR);
339 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
343 if (MVT(VT).isVector()) {
345 switch (VVT.getVectorNumElements()) {
346 default: PrintFatalError("unhandled vector type width in intrinsic!");
347 case 1: Sig.push_back(IIT_V1); break;
348 case 2: Sig.push_back(IIT_V2); break;
349 case 4: Sig.push_back(IIT_V4); break;
350 case 8: Sig.push_back(IIT_V8); break;
351 case 16: Sig.push_back(IIT_V16); break;
352 case 32: Sig.push_back(IIT_V32); break;
355 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
358 EncodeFixedValueType(VT, Sig);
362 #pragma optimize("",on)
365 /// ComputeFixedEncoding - If we can encode the type signature for this
366 /// intrinsic into 32 bits, return it. If not, return ~0U.
367 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
368 std::vector<unsigned char> &TypeSig) {
369 std::vector<unsigned char> ArgCodes;
371 if (Int.IS.RetVTs.empty())
372 TypeSig.push_back(IIT_Done);
373 else if (Int.IS.RetVTs.size() == 1 &&
374 Int.IS.RetVTs[0] == MVT::isVoid)
375 TypeSig.push_back(IIT_Done);
377 switch (Int.IS.RetVTs.size()) {
379 case 2: TypeSig.push_back(IIT_STRUCT2); break;
380 case 3: TypeSig.push_back(IIT_STRUCT3); break;
381 case 4: TypeSig.push_back(IIT_STRUCT4); break;
382 case 5: TypeSig.push_back(IIT_STRUCT5); break;
383 default: llvm_unreachable("Unhandled case in struct");
386 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
387 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
390 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
391 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
394 static void printIITEntry(raw_ostream &OS, unsigned char X) {
398 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
400 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
401 // capture it in this vector, otherwise store a ~0U.
402 std::vector<unsigned> FixedEncodings;
404 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
406 std::vector<unsigned char> TypeSig;
408 // Compute the unique argument type info.
409 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
410 // Get the signature for the intrinsic.
412 ComputeFixedEncoding(Ints[i], TypeSig);
414 // Check to see if we can encode it into a 32-bit word. We can only encode
415 // 8 nibbles into a 32-bit word.
416 if (TypeSig.size() <= 8) {
419 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
420 // If we had an unencodable argument, bail out.
421 if (TypeSig[i] > 15) {
425 Result = (Result << 4) | TypeSig[e-i-1];
428 // If this could be encoded into a 31-bit word, return it.
429 if (!Failed && (Result >> 31) == 0) {
430 FixedEncodings.push_back(Result);
435 // Otherwise, we're going to unique the sequence into the
436 // LongEncodingTable, and use its offset in the 32-bit table instead.
437 LongEncodingTable.add(TypeSig);
439 // This is a placehold that we'll replace after the table is laid out.
440 FixedEncodings.push_back(~0U);
443 LongEncodingTable.layout();
445 OS << "// Global intrinsic function declaration type table.\n";
446 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
448 OS << "static const unsigned IIT_Table[] = {\n ";
450 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
454 // If the entry fit in the table, just emit it.
455 if (FixedEncodings[i] != ~0U) {
456 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
461 ComputeFixedEncoding(Ints[i], TypeSig);
464 // Otherwise, emit the offset into the long encoding table. We emit it this
465 // way so that it is easier to read the offset in the .def file.
466 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
471 // Emit the shared table of register lists.
472 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
473 if (!LongEncodingTable.empty())
474 LongEncodingTable.emit(OS, printIITEntry);
475 OS << " 255\n};\n\n";
477 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
488 static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
489 switch (intrinsic.ModRef) {
490 case CodeGenIntrinsic::NoMem:
492 case CodeGenIntrinsic::ReadArgMem:
493 case CodeGenIntrinsic::ReadMem:
495 case CodeGenIntrinsic::ReadWriteArgMem:
496 case CodeGenIntrinsic::ReadWriteMem:
499 llvm_unreachable("bad mod-ref kind");
503 struct AttributeComparator {
504 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
505 // Sort throwing intrinsics after non-throwing intrinsics.
506 if (L->canThrow != R->canThrow)
509 if (L->isNoDuplicate != R->isNoDuplicate)
510 return R->isNoDuplicate;
512 if (L->isNoReturn != R->isNoReturn)
513 return R->isNoReturn;
515 // Try to order by readonly/readnone attribute.
516 ModRefKind LK = getModRefKind(*L);
517 ModRefKind RK = getModRefKind(*R);
518 if (LK != RK) return (LK > RK);
520 // Order by argument attributes.
521 // This is reliable because each side is already sorted internally.
522 return (L->ArgumentAttributes < R->ArgumentAttributes);
525 } // End anonymous namespace
527 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
528 void IntrinsicEmitter::
529 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
530 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
531 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
533 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
534 << "Intrinsic::ID id) {\n";
536 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
538 // Compute the maximum number of attribute arguments and the map
539 typedef std::map<const CodeGenIntrinsic*, unsigned,
540 AttributeComparator> UniqAttrMapTy;
541 UniqAttrMapTy UniqAttributes;
542 unsigned maxArgAttrs = 0;
543 unsigned AttrNum = 0;
544 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
545 const CodeGenIntrinsic &intrinsic = Ints[i];
547 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
548 unsigned &N = UniqAttributes[&intrinsic];
550 assert(AttrNum < 256 && "Too many unique attributes for table!");
554 // Emit an array of AttributeSet. Most intrinsics will have at least one
555 // entry, for the function itself (index ~1), which is usually nounwind.
556 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
558 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
559 const CodeGenIntrinsic &intrinsic = Ints[i];
561 OS << " " << UniqAttributes[&intrinsic] << ", // "
562 << intrinsic.Name << "\n";
566 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
567 OS << " unsigned NumAttrs = 0;\n";
568 OS << " if (id != 0) {\n";
569 OS << " switch(IntrinsicsToAttributesMap[id - ";
571 OS << "Intrinsic::num_intrinsics";
575 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
576 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
577 E = UniqAttributes.end(); I != E; ++I) {
578 OS << " case " << I->second << ": {\n";
580 const CodeGenIntrinsic &intrinsic = *(I->first);
582 // Keep track of the number of attributes we're writing out.
583 unsigned numAttrs = 0;
585 // The argument attributes are alreadys sorted by argument index.
586 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
589 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
591 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
592 bool addComma = false;
595 switch (intrinsic.ArgumentAttributes[ai].second) {
596 case CodeGenIntrinsic::NoCapture:
599 OS << "Attribute::NoCapture";
602 case CodeGenIntrinsic::ReadOnly:
605 OS << "Attribute::ReadOnly";
608 case CodeGenIntrinsic::ReadNone:
611 OS << "Attributes::ReadNone";
617 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
619 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
620 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
624 ModRefKind modRef = getModRefKind(intrinsic);
626 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn ||
627 intrinsic.isNoDuplicate) {
628 OS << " const Attribute::AttrKind Atts[] = {";
629 bool addComma = false;
630 if (!intrinsic.canThrow) {
631 OS << "Attribute::NoUnwind";
634 if (intrinsic.isNoReturn) {
637 OS << "Attribute::NoReturn";
640 if (intrinsic.isNoDuplicate) {
643 OS << "Attribute::NoDuplicate";
648 case MRK_none: break;
652 OS << "Attribute::ReadOnly";
657 OS << "Attribute::ReadNone";
661 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
662 << "AttributeSet::FunctionIndex, Atts);\n";
666 OS << " NumAttrs = " << numAttrs << ";\n";
670 OS << " return AttributeSet();\n";
677 OS << " return AttributeSet::get(C, ArrayRef<AttributeSet>(AS, "
680 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
683 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
684 void IntrinsicEmitter::
685 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
686 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
687 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
688 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
689 << "\"Unknown intrinsic.\");\n\n";
691 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
692 << " /* invalid */ UnknownModRefBehavior,\n";
693 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
694 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
695 switch (Ints[i].ModRef) {
696 case CodeGenIntrinsic::NoMem:
697 OS << "DoesNotAccessMemory,\n";
699 case CodeGenIntrinsic::ReadArgMem:
700 OS << "OnlyReadsArgumentPointees,\n";
702 case CodeGenIntrinsic::ReadMem:
703 OS << "OnlyReadsMemory,\n";
705 case CodeGenIntrinsic::ReadWriteArgMem:
706 OS << "OnlyAccessesArgumentPointees,\n";
708 case CodeGenIntrinsic::ReadWriteMem:
709 OS << "UnknownModRefBehavior,\n";
714 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
715 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
718 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
719 /// same target, and we already checked it.
720 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
721 const std::string &TargetPrefix,
724 std::vector<StringMatcher::StringPair> Results;
726 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
727 E = BIM.end(); I != E; ++I) {
728 std::string ResultCode =
729 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
730 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
733 StringMatcher("BuiltinName", Results, OS).Emit();
737 void IntrinsicEmitter::
738 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
740 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
742 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
743 if (!Ints[i].GCCBuiltinName.empty()) {
744 // Get the map for this target prefix.
745 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
747 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
748 Ints[i].EnumName)).second)
749 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
750 "': duplicate GCC builtin name!");
754 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
755 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
756 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
757 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
758 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
761 OS << "static " << TargetPrefix << "Intrinsic::ID "
762 << "getIntrinsicForGCCBuiltin(const char "
763 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
765 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
766 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
769 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
770 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
772 // Note: this could emit significantly better code if we cared.
773 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
775 if (!I->first.empty())
776 OS << "if (TargetPrefix == \"" << I->first << "\") ";
778 OS << "/* Target Independent Builtins */ ";
781 // Emit the comparisons for this target prefix.
782 EmitTargetBuiltins(I->second, TargetPrefix, OS);
786 if (!TargetPrefix.empty())
787 OS << "(" << TargetPrefix << "Intrinsic::ID)";
788 OS << "Intrinsic::not_intrinsic;\n";
793 void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
794 IntrinsicEmitter(RK, TargetOnly).run(OS);