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"
23 #include "llvm/TableGen/StringToOffsetTable.h"
28 class IntrinsicEmitter {
29 RecordKeeper &Records;
31 std::string TargetPrefix;
34 IntrinsicEmitter(RecordKeeper &R, bool T)
35 : Records(R), TargetOnly(T) {}
37 void run(raw_ostream &OS);
39 void EmitPrefix(raw_ostream &OS);
41 void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
42 void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
43 void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints,
45 void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
47 void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
48 void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
49 void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints, bool IsGCC,
51 void EmitSuffix(raw_ostream &OS);
53 } // End anonymous namespace
55 //===----------------------------------------------------------------------===//
56 // IntrinsicEmitter Implementation
57 //===----------------------------------------------------------------------===//
59 void IntrinsicEmitter::run(raw_ostream &OS) {
60 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
62 CodeGenIntrinsicTable Ints(Records, TargetOnly);
64 if (TargetOnly && !Ints.empty())
65 TargetPrefix = Ints[0].TargetPrefix;
69 // Emit the enum information.
70 EmitEnumInfo(Ints, OS);
72 // Emit the target metadata.
73 EmitTargetInfo(Ints, OS);
75 // Emit the intrinsic ID -> name table.
76 EmitIntrinsicToNameTable(Ints, OS);
78 // Emit the intrinsic ID -> overload table.
79 EmitIntrinsicToOverloadTable(Ints, OS);
81 // Emit the intrinsic declaration generator.
82 EmitGenerator(Ints, OS);
84 // Emit the intrinsic parameter attributes.
85 EmitAttributes(Ints, OS);
87 // Individual targets don't need GCC builtin name mappings.
89 // Emit code to translate GCC builtins into LLVM intrinsics.
90 EmitIntrinsicToBuiltinMap(Ints, true, OS);
92 // Emit code to translate MS builtins into LLVM intrinsics.
93 EmitIntrinsicToBuiltinMap(Ints, false, OS);
99 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
100 OS << "// VisualStudio defines setjmp as _setjmp\n"
101 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
102 " !defined(setjmp_undefined_for_msvc)\n"
103 "# pragma push_macro(\"setjmp\")\n"
105 "# define setjmp_undefined_for_msvc\n"
109 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
110 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
111 "// let's return it to _setjmp state\n"
112 "# pragma pop_macro(\"setjmp\")\n"
113 "# undef setjmp_undefined_for_msvc\n"
117 void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
119 OS << "// Enum values for Intrinsics.h\n";
120 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
121 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
122 OS << " " << Ints[i].EnumName;
123 OS << ((i != e-1) ? ", " : " ");
124 if (Ints[i].EnumName.size() < 40)
125 OS << std::string(40-Ints[i].EnumName.size(), ' ');
126 OS << " // " << Ints[i].Name << "\n";
131 void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
133 OS << "// Target mapping\n";
134 OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n";
135 OS << "struct IntrinsicTargetInfo {\n"
136 << " StringRef Name;\n"
137 << " size_t Offset;\n"
138 << " size_t Count;\n"
140 OS << "static const IntrinsicTargetInfo TargetInfos[] = {\n";
141 for (auto Target : Ints.Targets)
142 OS << " {\"" << Target.Name << "\", " << Target.Offset << ", "
143 << Target.Count << "},\n";
148 void IntrinsicEmitter::EmitIntrinsicToNameTable(
149 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
150 OS << "// Intrinsic ID to name table\n";
151 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
152 OS << " // Note that entry #0 is the invalid intrinsic!\n";
153 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
154 OS << " \"" << Ints[i].Name << "\",\n";
158 void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
159 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
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";
178 // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
180 // Common values should be encoded with 0-15.
198 // Values from 16+ are only encodable with the inefficient encoding.
203 IIT_EMPTYSTRUCT = 20,
213 IIT_HALF_VEC_ARG = 30,
214 IIT_SAME_VEC_WIDTH_ARG = 31,
216 IIT_VEC_OF_PTRS_TO_ELT = 33,
223 static void EncodeFixedValueType(MVT::SimpleValueType VT,
224 std::vector<unsigned char> &Sig) {
225 if (MVT(VT).isInteger()) {
226 unsigned BitWidth = MVT(VT).getSizeInBits();
228 default: PrintFatalError("unhandled integer type width in intrinsic!");
229 case 1: return Sig.push_back(IIT_I1);
230 case 8: return Sig.push_back(IIT_I8);
231 case 16: return Sig.push_back(IIT_I16);
232 case 32: return Sig.push_back(IIT_I32);
233 case 64: return Sig.push_back(IIT_I64);
234 case 128: return Sig.push_back(IIT_I128);
239 default: PrintFatalError("unhandled MVT in intrinsic!");
240 case MVT::f16: return Sig.push_back(IIT_F16);
241 case MVT::f32: return Sig.push_back(IIT_F32);
242 case MVT::f64: return Sig.push_back(IIT_F64);
243 case MVT::token: return Sig.push_back(IIT_TOKEN);
244 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
245 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
246 // MVT::OtherVT is used to mean the empty struct type here.
247 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
248 // MVT::isVoid is used to represent varargs here.
249 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
253 #if defined(_MSC_VER) && !defined(__clang__)
254 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
257 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
258 std::vector<unsigned char> &Sig) {
260 if (R->isSubClassOf("LLVMMatchType")) {
261 unsigned Number = R->getValueAsInt("Number");
262 assert(Number < ArgCodes.size() && "Invalid matching number!");
263 if (R->isSubClassOf("LLVMExtendedType"))
264 Sig.push_back(IIT_EXTEND_ARG);
265 else if (R->isSubClassOf("LLVMTruncatedType"))
266 Sig.push_back(IIT_TRUNC_ARG);
267 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
268 Sig.push_back(IIT_HALF_VEC_ARG);
269 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
270 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
271 Sig.push_back((Number << 3) | ArgCodes[Number]);
272 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
273 EncodeFixedValueType(VT, Sig);
276 else if (R->isSubClassOf("LLVMPointerTo"))
277 Sig.push_back(IIT_PTR_TO_ARG);
278 else if (R->isSubClassOf("LLVMVectorOfPointersToElt"))
279 Sig.push_back(IIT_VEC_OF_PTRS_TO_ELT);
281 Sig.push_back(IIT_ARG);
282 return Sig.push_back((Number << 3) | ArgCodes[Number]);
285 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
290 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
291 case MVT::vAny: ++Tmp; // FALL THROUGH.
292 case MVT::fAny: ++Tmp; // FALL THROUGH.
293 case MVT::iAny: ++Tmp; // FALL THROUGH.
295 // If this is an "any" valuetype, then the type is the type of the next
296 // type in the list specified to getIntrinsic().
297 Sig.push_back(IIT_ARG);
299 // Figure out what arg # this is consuming, and remember what kind it was.
300 unsigned ArgNo = ArgCodes.size();
301 ArgCodes.push_back(Tmp);
303 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
304 return Sig.push_back((ArgNo << 3) | Tmp);
308 unsigned AddrSpace = 0;
309 if (R->isSubClassOf("LLVMQualPointerType")) {
310 AddrSpace = R->getValueAsInt("AddrSpace");
311 assert(AddrSpace < 256 && "Address space exceeds 255");
314 Sig.push_back(IIT_ANYPTR);
315 Sig.push_back(AddrSpace);
317 Sig.push_back(IIT_PTR);
319 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
323 if (MVT(VT).isVector()) {
325 switch (VVT.getVectorNumElements()) {
326 default: PrintFatalError("unhandled vector type width in intrinsic!");
327 case 1: Sig.push_back(IIT_V1); break;
328 case 2: Sig.push_back(IIT_V2); break;
329 case 4: Sig.push_back(IIT_V4); break;
330 case 8: Sig.push_back(IIT_V8); break;
331 case 16: Sig.push_back(IIT_V16); break;
332 case 32: Sig.push_back(IIT_V32); break;
333 case 64: Sig.push_back(IIT_V64); break;
334 case 512: Sig.push_back(IIT_V512); break;
335 case 1024: Sig.push_back(IIT_V1024); break;
338 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
341 EncodeFixedValueType(VT, Sig);
344 #if defined(_MSC_VER) && !defined(__clang__)
345 #pragma optimize("",on)
348 /// ComputeFixedEncoding - If we can encode the type signature for this
349 /// intrinsic into 32 bits, return it. If not, return ~0U.
350 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
351 std::vector<unsigned char> &TypeSig) {
352 std::vector<unsigned char> ArgCodes;
354 if (Int.IS.RetVTs.empty())
355 TypeSig.push_back(IIT_Done);
356 else if (Int.IS.RetVTs.size() == 1 &&
357 Int.IS.RetVTs[0] == MVT::isVoid)
358 TypeSig.push_back(IIT_Done);
360 switch (Int.IS.RetVTs.size()) {
362 case 2: TypeSig.push_back(IIT_STRUCT2); break;
363 case 3: TypeSig.push_back(IIT_STRUCT3); break;
364 case 4: TypeSig.push_back(IIT_STRUCT4); break;
365 case 5: TypeSig.push_back(IIT_STRUCT5); break;
366 default: llvm_unreachable("Unhandled case in struct");
369 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
370 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
373 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
374 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
377 static void printIITEntry(raw_ostream &OS, unsigned char X) {
381 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
383 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
384 // capture it in this vector, otherwise store a ~0U.
385 std::vector<unsigned> FixedEncodings;
387 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
389 std::vector<unsigned char> TypeSig;
391 // Compute the unique argument type info.
392 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
393 // Get the signature for the intrinsic.
395 ComputeFixedEncoding(Ints[i], TypeSig);
397 // Check to see if we can encode it into a 32-bit word. We can only encode
398 // 8 nibbles into a 32-bit word.
399 if (TypeSig.size() <= 8) {
402 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
403 // If we had an unencodable argument, bail out.
404 if (TypeSig[i] > 15) {
408 Result = (Result << 4) | TypeSig[e-i-1];
411 // If this could be encoded into a 31-bit word, return it.
412 if (!Failed && (Result >> 31) == 0) {
413 FixedEncodings.push_back(Result);
418 // Otherwise, we're going to unique the sequence into the
419 // LongEncodingTable, and use its offset in the 32-bit table instead.
420 LongEncodingTable.add(TypeSig);
422 // This is a placehold that we'll replace after the table is laid out.
423 FixedEncodings.push_back(~0U);
426 LongEncodingTable.layout();
428 OS << "// Global intrinsic function declaration type table.\n";
429 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
431 OS << "static const unsigned IIT_Table[] = {\n ";
433 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
437 // If the entry fit in the table, just emit it.
438 if (FixedEncodings[i] != ~0U) {
439 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
444 ComputeFixedEncoding(Ints[i], TypeSig);
447 // Otherwise, emit the offset into the long encoding table. We emit it this
448 // way so that it is easier to read the offset in the .def file.
449 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
454 // Emit the shared table of register lists.
455 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
456 if (!LongEncodingTable.empty())
457 LongEncodingTable.emit(OS, printIITEntry);
458 OS << " 255\n};\n\n";
460 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
464 struct AttributeComparator {
465 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
466 // Sort throwing intrinsics after non-throwing intrinsics.
467 if (L->canThrow != R->canThrow)
470 if (L->isNoDuplicate != R->isNoDuplicate)
471 return R->isNoDuplicate;
473 if (L->isNoReturn != R->isNoReturn)
474 return R->isNoReturn;
476 if (L->isConvergent != R->isConvergent)
477 return R->isConvergent;
479 // Try to order by readonly/readnone attribute.
480 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
481 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
482 if (LK != RK) return (LK > RK);
484 // Order by argument attributes.
485 // This is reliable because each side is already sorted internally.
486 return (L->ArgumentAttributes < R->ArgumentAttributes);
489 } // End anonymous namespace
491 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
492 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
494 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
495 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
497 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
498 << "Intrinsic::ID id) {\n";
500 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
502 // Compute the maximum number of attribute arguments and the map
503 typedef std::map<const CodeGenIntrinsic*, unsigned,
504 AttributeComparator> UniqAttrMapTy;
505 UniqAttrMapTy UniqAttributes;
506 unsigned maxArgAttrs = 0;
507 unsigned AttrNum = 0;
508 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
509 const CodeGenIntrinsic &intrinsic = Ints[i];
511 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
512 unsigned &N = UniqAttributes[&intrinsic];
514 assert(AttrNum < 256 && "Too many unique attributes for table!");
518 // Emit an array of AttributeSet. Most intrinsics will have at least one
519 // entry, for the function itself (index ~1), which is usually nounwind.
520 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
522 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
523 const CodeGenIntrinsic &intrinsic = Ints[i];
525 OS << " " << UniqAttributes[&intrinsic] << ", // "
526 << intrinsic.Name << "\n";
530 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
531 OS << " unsigned NumAttrs = 0;\n";
532 OS << " if (id != 0) {\n";
533 OS << " switch(IntrinsicsToAttributesMap[id - ";
535 OS << "Intrinsic::num_intrinsics";
539 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
540 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
541 E = UniqAttributes.end(); I != E; ++I) {
542 OS << " case " << I->second << ": {\n";
544 const CodeGenIntrinsic &intrinsic = *(I->first);
546 // Keep track of the number of attributes we're writing out.
547 unsigned numAttrs = 0;
549 // The argument attributes are alreadys sorted by argument index.
550 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
553 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
555 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
556 bool addComma = false;
559 switch (intrinsic.ArgumentAttributes[ai].second) {
560 case CodeGenIntrinsic::NoCapture:
563 OS << "Attribute::NoCapture";
566 case CodeGenIntrinsic::Returned:
569 OS << "Attribute::Returned";
572 case CodeGenIntrinsic::ReadOnly:
575 OS << "Attribute::ReadOnly";
578 case CodeGenIntrinsic::WriteOnly:
581 OS << "Attribute::WriteOnly";
584 case CodeGenIntrinsic::ReadNone:
587 OS << "Attribute::ReadNone";
593 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
595 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
596 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
600 if (!intrinsic.canThrow ||
601 intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
602 intrinsic.isNoReturn || intrinsic.isNoDuplicate ||
603 intrinsic.isConvergent) {
604 OS << " const Attribute::AttrKind Atts[] = {";
605 bool addComma = false;
606 if (!intrinsic.canThrow) {
607 OS << "Attribute::NoUnwind";
610 if (intrinsic.isNoReturn) {
613 OS << "Attribute::NoReturn";
616 if (intrinsic.isNoDuplicate) {
619 OS << "Attribute::NoDuplicate";
622 if (intrinsic.isConvergent) {
625 OS << "Attribute::Convergent";
629 switch (intrinsic.ModRef) {
630 case CodeGenIntrinsic::NoMem:
633 OS << "Attribute::ReadNone";
635 case CodeGenIntrinsic::ReadArgMem:
638 OS << "Attribute::ReadOnly,";
639 OS << "Attribute::ArgMemOnly";
641 case CodeGenIntrinsic::ReadMem:
644 OS << "Attribute::ReadOnly";
646 case CodeGenIntrinsic::WriteArgMem:
649 OS << "Attribute::WriteOnly,";
650 OS << "Attribute::ArgMemOnly";
652 case CodeGenIntrinsic::WriteMem:
655 OS << "Attribute::WriteOnly";
657 case CodeGenIntrinsic::ReadWriteArgMem:
660 OS << "Attribute::ArgMemOnly";
662 case CodeGenIntrinsic::ReadWriteMem:
666 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
667 << "AttributeSet::FunctionIndex, Atts);\n";
671 OS << " NumAttrs = " << numAttrs << ";\n";
675 OS << " return AttributeSet();\n";
682 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
684 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
687 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
688 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
689 StringRef CompilerName = (IsGCC ? "GCC" : "MS");
690 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
692 StringToOffsetTable Table;
693 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
694 const std::string &BuiltinName =
695 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
696 if (!BuiltinName.empty()) {
697 // Get the map for this target prefix.
698 std::map<std::string, std::string> &BIM =
699 BuiltinMap[Ints[i].TargetPrefix];
701 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
702 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
703 "': duplicate " + CompilerName + " builtin name!");
704 Table.GetOrAddStringOffset(BuiltinName);
708 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
709 OS << "// This is used by the C front-end. The builtin name is passed\n";
710 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
711 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
712 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
715 OS << "static " << TargetPrefix << "Intrinsic::ID "
716 << "getIntrinsicFor" << CompilerName << "Builtin(const char "
717 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
719 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
720 << "Builtin(const char "
721 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
723 OS << " static const char BuiltinNames[] = {\n";
724 Table.EmitCharArray(OS);
727 OS << " struct BuiltinEntry {\n";
728 OS << " Intrinsic::ID IntrinID;\n";
729 OS << " unsigned StrTabOffset;\n";
730 OS << " const char *getName() const {\n";
731 OS << " return &BuiltinNames[StrTabOffset];\n";
733 OS << " bool operator<(const char *RHS) const {\n";
734 OS << " return strcmp(getName(), RHS) < 0;\n";
739 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
740 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
742 // Note: this could emit significantly better code if we cared.
743 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
745 if (!I->first.empty())
746 OS << "if (TargetPrefix == \"" << I->first << "\") ";
748 OS << "/* Target Independent Builtins */ ";
751 // Emit the comparisons for this target prefix.
752 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n";
753 for (const auto &P : I->second) {
754 OS << " {Intrinsic::" << P.second << ", "
755 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
758 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
759 OS << " std::end(" << I->first << "Names),\n";
760 OS << " BuiltinNameStr);\n";
761 OS << " if (I != std::end(" << I->first << "Names) &&\n";
762 OS << " strcmp(I->getName(), BuiltinNameStr) == 0)\n";
763 OS << " return I->IntrinID;\n";
767 if (!TargetPrefix.empty())
768 OS << "(" << TargetPrefix << "Intrinsic::ID)";
769 OS << "Intrinsic::not_intrinsic;\n";
774 void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
775 IntrinsicEmitter(RK, TargetOnly).run(OS);