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 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
55 void EmitIntrinsicToMSBuiltinMap(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 code to translate GCC builtins into LLVM intrinsics.
94 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
96 // Emit code to translate MS builtins into LLVM intrinsics.
97 EmitIntrinsicToMSBuiltinMap(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 if (Ints[i].EnumName.size() < 40)
128 OS << std::string(40-Ints[i].EnumName.size(), ' ');
129 OS << " // " << Ints[i].Name << "\n";
134 void IntrinsicEmitter::
135 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
137 // Build a 'first character of function name' -> intrinsic # mapping.
138 std::map<char, std::vector<unsigned> > IntMapping;
139 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
140 IntMapping[Ints[i].Name[5]].push_back(i);
142 OS << "// Function name -> enum value recognizer code.\n";
143 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
144 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
145 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
146 OS << " default: break;\n";
147 // Emit the intrinsic matching stuff by first letter.
148 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
149 E = IntMapping.end(); I != E; ++I) {
150 OS << " case '" << I->first << "':\n";
151 std::vector<unsigned> &IntList = I->second;
153 // Sort in reverse order of intrinsic name so "abc.def" appears after
154 // "abd.def.ghi" in the overridden name matcher
155 std::sort(IntList.begin(), IntList.end(), [&](unsigned i, unsigned j) {
156 return Ints[i].Name > Ints[j].Name;
159 // Emit all the overloaded intrinsics first, build a table of the
160 // non-overloaded ones.
161 std::vector<StringMatcher::StringPair> MatchTable;
163 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
164 unsigned IntNo = IntList[i];
165 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
166 Ints[IntNo].EnumName + ";";
168 if (!Ints[IntNo].isOverloaded) {
169 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
173 // For overloaded intrinsics, only the prefix needs to match
174 std::string TheStr = Ints[IntNo].Name.substr(6);
175 TheStr += '.'; // Require "bswap." instead of bswap.
176 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
180 // Emit the matcher logic for the fixed length strings.
181 StringMatcher("NameR", MatchTable, OS).Emit(1);
182 OS << " break; // end of '" << I->first << "' case.\n";
189 void IntrinsicEmitter::
190 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
192 OS << "// Intrinsic ID to name table\n";
193 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
194 OS << " // Note that entry #0 is the invalid intrinsic!\n";
195 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
196 OS << " \"" << Ints[i].Name << "\",\n";
200 void IntrinsicEmitter::
201 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
203 OS << "// Intrinsic ID to overload bitset\n";
204 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
205 OS << "static const uint8_t OTable[] = {\n";
207 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
208 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
211 if (Ints[i].isOverloaded)
212 OS << " | (1<<" << (i+1)%8 << ')';
215 // OTable contains a true bit at the position if the intrinsic is overloaded.
216 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
221 // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
223 // Common values should be encoded with 0-15.
241 // Values from 16+ are only encodable with the inefficient encoding.
246 IIT_EMPTYSTRUCT = 20,
256 IIT_HALF_VEC_ARG = 30,
257 IIT_SAME_VEC_WIDTH_ARG = 31,
259 IIT_VEC_OF_PTRS_TO_ELT = 33,
264 static void EncodeFixedValueType(MVT::SimpleValueType VT,
265 std::vector<unsigned char> &Sig) {
266 if (MVT(VT).isInteger()) {
267 unsigned BitWidth = MVT(VT).getSizeInBits();
269 default: PrintFatalError("unhandled integer type width in intrinsic!");
270 case 1: return Sig.push_back(IIT_I1);
271 case 8: return Sig.push_back(IIT_I8);
272 case 16: return Sig.push_back(IIT_I16);
273 case 32: return Sig.push_back(IIT_I32);
274 case 64: return Sig.push_back(IIT_I64);
275 case 128: return Sig.push_back(IIT_I128);
280 default: PrintFatalError("unhandled MVT in intrinsic!");
281 case MVT::f16: return Sig.push_back(IIT_F16);
282 case MVT::f32: return Sig.push_back(IIT_F32);
283 case MVT::f64: return Sig.push_back(IIT_F64);
284 case MVT::token: return Sig.push_back(IIT_TOKEN);
285 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
286 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
287 // MVT::OtherVT is used to mean the empty struct type here.
288 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
289 // MVT::isVoid is used to represent varargs here.
290 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
294 #if defined(_MSC_VER) && !defined(__clang__)
295 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
298 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
299 std::vector<unsigned char> &Sig) {
301 if (R->isSubClassOf("LLVMMatchType")) {
302 unsigned Number = R->getValueAsInt("Number");
303 assert(Number < ArgCodes.size() && "Invalid matching number!");
304 if (R->isSubClassOf("LLVMExtendedType"))
305 Sig.push_back(IIT_EXTEND_ARG);
306 else if (R->isSubClassOf("LLVMTruncatedType"))
307 Sig.push_back(IIT_TRUNC_ARG);
308 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
309 Sig.push_back(IIT_HALF_VEC_ARG);
310 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
311 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
312 Sig.push_back((Number << 3) | ArgCodes[Number]);
313 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
314 EncodeFixedValueType(VT, Sig);
317 else if (R->isSubClassOf("LLVMPointerTo"))
318 Sig.push_back(IIT_PTR_TO_ARG);
319 else if (R->isSubClassOf("LLVMVectorOfPointersToElt"))
320 Sig.push_back(IIT_VEC_OF_PTRS_TO_ELT);
322 Sig.push_back(IIT_ARG);
323 return Sig.push_back((Number << 3) | ArgCodes[Number]);
326 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
331 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
332 case MVT::vAny: ++Tmp; // FALL THROUGH.
333 case MVT::fAny: ++Tmp; // FALL THROUGH.
334 case MVT::iAny: ++Tmp; // FALL THROUGH.
336 // If this is an "any" valuetype, then the type is the type of the next
337 // type in the list specified to getIntrinsic().
338 Sig.push_back(IIT_ARG);
340 // Figure out what arg # this is consuming, and remember what kind it was.
341 unsigned ArgNo = ArgCodes.size();
342 ArgCodes.push_back(Tmp);
344 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
345 return Sig.push_back((ArgNo << 3) | Tmp);
349 unsigned AddrSpace = 0;
350 if (R->isSubClassOf("LLVMQualPointerType")) {
351 AddrSpace = R->getValueAsInt("AddrSpace");
352 assert(AddrSpace < 256 && "Address space exceeds 255");
355 Sig.push_back(IIT_ANYPTR);
356 Sig.push_back(AddrSpace);
358 Sig.push_back(IIT_PTR);
360 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
364 if (MVT(VT).isVector()) {
366 switch (VVT.getVectorNumElements()) {
367 default: PrintFatalError("unhandled vector type width in intrinsic!");
368 case 1: Sig.push_back(IIT_V1); break;
369 case 2: Sig.push_back(IIT_V2); break;
370 case 4: Sig.push_back(IIT_V4); break;
371 case 8: Sig.push_back(IIT_V8); break;
372 case 16: Sig.push_back(IIT_V16); break;
373 case 32: Sig.push_back(IIT_V32); break;
374 case 64: Sig.push_back(IIT_V64); break;
377 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
380 EncodeFixedValueType(VT, Sig);
383 #if defined(_MSC_VER) && !defined(__clang__)
384 #pragma optimize("",on)
387 /// ComputeFixedEncoding - If we can encode the type signature for this
388 /// intrinsic into 32 bits, return it. If not, return ~0U.
389 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
390 std::vector<unsigned char> &TypeSig) {
391 std::vector<unsigned char> ArgCodes;
393 if (Int.IS.RetVTs.empty())
394 TypeSig.push_back(IIT_Done);
395 else if (Int.IS.RetVTs.size() == 1 &&
396 Int.IS.RetVTs[0] == MVT::isVoid)
397 TypeSig.push_back(IIT_Done);
399 switch (Int.IS.RetVTs.size()) {
401 case 2: TypeSig.push_back(IIT_STRUCT2); break;
402 case 3: TypeSig.push_back(IIT_STRUCT3); break;
403 case 4: TypeSig.push_back(IIT_STRUCT4); break;
404 case 5: TypeSig.push_back(IIT_STRUCT5); break;
405 default: llvm_unreachable("Unhandled case in struct");
408 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
409 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
412 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
413 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
416 static void printIITEntry(raw_ostream &OS, unsigned char X) {
420 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
422 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
423 // capture it in this vector, otherwise store a ~0U.
424 std::vector<unsigned> FixedEncodings;
426 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
428 std::vector<unsigned char> TypeSig;
430 // Compute the unique argument type info.
431 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
432 // Get the signature for the intrinsic.
434 ComputeFixedEncoding(Ints[i], TypeSig);
436 // Check to see if we can encode it into a 32-bit word. We can only encode
437 // 8 nibbles into a 32-bit word.
438 if (TypeSig.size() <= 8) {
441 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
442 // If we had an unencodable argument, bail out.
443 if (TypeSig[i] > 15) {
447 Result = (Result << 4) | TypeSig[e-i-1];
450 // If this could be encoded into a 31-bit word, return it.
451 if (!Failed && (Result >> 31) == 0) {
452 FixedEncodings.push_back(Result);
457 // Otherwise, we're going to unique the sequence into the
458 // LongEncodingTable, and use its offset in the 32-bit table instead.
459 LongEncodingTable.add(TypeSig);
461 // This is a placehold that we'll replace after the table is laid out.
462 FixedEncodings.push_back(~0U);
465 LongEncodingTable.layout();
467 OS << "// Global intrinsic function declaration type table.\n";
468 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
470 OS << "static const unsigned IIT_Table[] = {\n ";
472 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
476 // If the entry fit in the table, just emit it.
477 if (FixedEncodings[i] != ~0U) {
478 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
483 ComputeFixedEncoding(Ints[i], TypeSig);
486 // Otherwise, emit the offset into the long encoding table. We emit it this
487 // way so that it is easier to read the offset in the .def file.
488 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
493 // Emit the shared table of register lists.
494 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
495 if (!LongEncodingTable.empty())
496 LongEncodingTable.emit(OS, printIITEntry);
497 OS << " 255\n};\n\n";
499 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
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 if (L->isConvergent != R->isConvergent)
516 return R->isConvergent;
518 // Try to order by readonly/readnone attribute.
519 CodeGenIntrinsic::ModRefKind LK = L->ModRef;
520 CodeGenIntrinsic::ModRefKind RK = R->ModRef;
521 if (LK != RK) return (LK > RK);
523 // Order by argument attributes.
524 // This is reliable because each side is already sorted internally.
525 return (L->ArgumentAttributes < R->ArgumentAttributes);
528 } // End anonymous namespace
530 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
531 void IntrinsicEmitter::
532 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
533 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
534 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
536 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
537 << "Intrinsic::ID id) {\n";
539 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
541 // Compute the maximum number of attribute arguments and the map
542 typedef std::map<const CodeGenIntrinsic*, unsigned,
543 AttributeComparator> UniqAttrMapTy;
544 UniqAttrMapTy UniqAttributes;
545 unsigned maxArgAttrs = 0;
546 unsigned AttrNum = 0;
547 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
548 const CodeGenIntrinsic &intrinsic = Ints[i];
550 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
551 unsigned &N = UniqAttributes[&intrinsic];
553 assert(AttrNum < 256 && "Too many unique attributes for table!");
557 // Emit an array of AttributeSet. Most intrinsics will have at least one
558 // entry, for the function itself (index ~1), which is usually nounwind.
559 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
561 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
562 const CodeGenIntrinsic &intrinsic = Ints[i];
564 OS << " " << UniqAttributes[&intrinsic] << ", // "
565 << intrinsic.Name << "\n";
569 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
570 OS << " unsigned NumAttrs = 0;\n";
571 OS << " if (id != 0) {\n";
572 OS << " switch(IntrinsicsToAttributesMap[id - ";
574 OS << "Intrinsic::num_intrinsics";
578 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
579 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
580 E = UniqAttributes.end(); I != E; ++I) {
581 OS << " case " << I->second << ": {\n";
583 const CodeGenIntrinsic &intrinsic = *(I->first);
585 // Keep track of the number of attributes we're writing out.
586 unsigned numAttrs = 0;
588 // The argument attributes are alreadys sorted by argument index.
589 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
592 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
594 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
595 bool addComma = false;
598 switch (intrinsic.ArgumentAttributes[ai].second) {
599 case CodeGenIntrinsic::NoCapture:
602 OS << "Attribute::NoCapture";
605 case CodeGenIntrinsic::ReadOnly:
608 OS << "Attribute::ReadOnly";
611 case CodeGenIntrinsic::ReadNone:
614 OS << "Attribute::ReadNone";
620 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
622 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
623 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
627 if (!intrinsic.canThrow ||
628 intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
629 intrinsic.isNoReturn || intrinsic.isNoDuplicate ||
630 intrinsic.isConvergent) {
631 OS << " const Attribute::AttrKind Atts[] = {";
632 bool addComma = false;
633 if (!intrinsic.canThrow) {
634 OS << "Attribute::NoUnwind";
637 if (intrinsic.isNoReturn) {
640 OS << "Attribute::NoReturn";
643 if (intrinsic.isNoDuplicate) {
646 OS << "Attribute::NoDuplicate";
649 if (intrinsic.isConvergent) {
652 OS << "Attribute::Convergent";
656 switch (intrinsic.ModRef) {
657 case CodeGenIntrinsic::NoMem:
660 OS << "Attribute::ReadNone";
662 case CodeGenIntrinsic::ReadArgMem:
665 OS << "Attribute::ReadOnly,";
666 OS << "Attribute::ArgMemOnly";
668 case CodeGenIntrinsic::ReadMem:
671 OS << "Attribute::ReadOnly";
673 case CodeGenIntrinsic::ReadWriteArgMem:
676 OS << "Attribute::ArgMemOnly";
678 case CodeGenIntrinsic::ReadWriteMem:
682 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
683 << "AttributeSet::FunctionIndex, Atts);\n";
687 OS << " NumAttrs = " << numAttrs << ";\n";
691 OS << " return AttributeSet();\n";
698 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
700 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
703 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
704 /// same target, and we already checked it.
705 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
706 const std::string &TargetPrefix,
709 std::vector<StringMatcher::StringPair> Results;
711 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
712 E = BIM.end(); I != E; ++I) {
713 std::string ResultCode =
714 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
715 Results.emplace_back(I->first, ResultCode);
718 StringMatcher("BuiltinName", Results, OS).Emit();
722 void IntrinsicEmitter::
723 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
725 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
727 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
728 if (!Ints[i].GCCBuiltinName.empty()) {
729 // Get the map for this target prefix.
730 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
732 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
733 Ints[i].EnumName)).second)
734 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
735 "': duplicate GCC builtin name!");
739 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
740 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
741 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
742 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
743 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
746 OS << "static " << TargetPrefix << "Intrinsic::ID "
747 << "getIntrinsicForGCCBuiltin(const char "
748 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
750 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
751 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
754 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
755 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
757 // Note: this could emit significantly better code if we cared.
758 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
760 if (!I->first.empty())
761 OS << "if (TargetPrefix == \"" << I->first << "\") ";
763 OS << "/* Target Independent Builtins */ ";
766 // Emit the comparisons for this target prefix.
767 EmitTargetBuiltins(I->second, TargetPrefix, OS);
771 if (!TargetPrefix.empty())
772 OS << "(" << TargetPrefix << "Intrinsic::ID)";
773 OS << "Intrinsic::not_intrinsic;\n";
778 void IntrinsicEmitter::
779 EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
781 std::map<std::string, std::map<std::string, std::string>> TargetBuiltins;
783 for (const auto &Intrinsic : Ints) {
784 if (Intrinsic.MSBuiltinName.empty())
787 auto &Builtins = TargetBuiltins[Intrinsic.TargetPrefix];
788 if (!Builtins.insert(std::make_pair(Intrinsic.MSBuiltinName,
789 Intrinsic.EnumName)).second)
790 PrintFatalError("Intrinsic '" + Intrinsic.TheDef->getName() + "': "
791 "duplicate MS builtin name!");
794 OS << "// Get the LLVM intrinsic that corresponds to a MS builtin.\n"
795 "// This is used by the C front-end. The MS builtin name is passed\n"
796 "// in as a BuiltinName, and a target prefix (e.g. 'arm') is passed\n"
797 "// in as a TargetPrefix. The result is assigned to 'IntrinsicID'.\n"
798 "#ifdef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN\n";
800 OS << (TargetOnly ? "static " + TargetPrefix : "") << "Intrinsic::ID "
801 << (TargetOnly ? "" : "Intrinsic::")
802 << "getIntrinsicForMSBuiltin(const char *TP, const char *BN) {\n";
803 OS << " StringRef BuiltinName(BN);\n"
804 " StringRef TargetPrefix(TP);\n"
807 for (const auto &Builtins : TargetBuiltins) {
809 if (Builtins.first.empty())
810 OS << "/* Target Independent Builtins */ ";
812 OS << "if (TargetPrefix == \"" << Builtins.first << "\") ";
814 EmitTargetBuiltins(Builtins.second, TargetPrefix, OS);
819 if (!TargetPrefix.empty())
820 OS << "(" << TargetPrefix << "Intrinsic::ID)";
821 OS << "Intrinsic::not_intrinsic;\n";
827 void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
828 IntrinsicEmitter(RK, TargetOnly).run(OS);