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 "llvm/ADT/StringExtras.h"
18 #include "llvm/TableGen/Error.h"
19 #include "llvm/TableGen/Record.h"
20 #include "llvm/TableGen/StringMatcher.h"
21 #include "llvm/TableGen/TableGenBackend.h"
26 class IntrinsicEmitter {
27 RecordKeeper &Records;
29 std::string TargetPrefix;
32 IntrinsicEmitter(RecordKeeper &R, bool T)
33 : Records(R), TargetOnly(T) {}
35 void run(raw_ostream &OS);
37 void EmitPrefix(raw_ostream &OS);
39 void EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
42 void EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
44 void EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
46 void EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
48 void EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
50 void EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints,
52 void EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints,
54 void EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
56 void EmitSuffix(raw_ostream &OS);
58 } // End anonymous namespace
60 //===----------------------------------------------------------------------===//
61 // IntrinsicEmitter Implementation
62 //===----------------------------------------------------------------------===//
64 void IntrinsicEmitter::run(raw_ostream &OS) {
65 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
67 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
69 if (TargetOnly && !Ints.empty())
70 TargetPrefix = Ints[0].TargetPrefix;
74 // Emit the enum information.
75 EmitEnumInfo(Ints, OS);
77 // Emit the intrinsic ID -> name table.
78 EmitIntrinsicToNameTable(Ints, OS);
80 // Emit the intrinsic ID -> overload table.
81 EmitIntrinsicToOverloadTable(Ints, OS);
83 // Emit the function name recognizer.
84 EmitFnNameRecognizer(Ints, OS);
86 // Emit the intrinsic declaration generator.
87 EmitGenerator(Ints, OS);
89 // Emit the intrinsic parameter attributes.
90 EmitAttributes(Ints, OS);
92 // Emit intrinsic alias analysis mod/ref behavior.
93 EmitModRefBehavior(Ints, OS);
95 // Emit code to translate GCC builtins into LLVM intrinsics.
96 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
101 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
102 OS << "// VisualStudio defines setjmp as _setjmp\n"
103 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
104 " !defined(setjmp_undefined_for_msvc)\n"
105 "# pragma push_macro(\"setjmp\")\n"
107 "# define setjmp_undefined_for_msvc\n"
111 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
112 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
113 "// let's return it to _setjmp state\n"
114 "# pragma pop_macro(\"setjmp\")\n"
115 "# undef setjmp_undefined_for_msvc\n"
119 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
121 OS << "// Enum values for Intrinsics.h\n";
122 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
123 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
124 OS << " " << Ints[i].EnumName;
125 OS << ((i != e-1) ? ", " : " ");
126 OS << std::string(40-Ints[i].EnumName.size(), ' ')
127 << "// " << Ints[i].Name << "\n";
132 void IntrinsicEmitter::
133 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
135 // Build a 'first character of function name' -> intrinsic # mapping.
136 std::map<char, std::vector<unsigned> > IntMapping;
137 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
138 IntMapping[Ints[i].Name[5]].push_back(i);
140 OS << "// Function name -> enum value recognizer code.\n";
141 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
142 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
143 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
144 OS << " default: break;\n";
145 // Emit the intrinsic matching stuff by first letter.
146 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
147 E = IntMapping.end(); I != E; ++I) {
148 OS << " case '" << I->first << "':\n";
149 std::vector<unsigned> &IntList = I->second;
151 // Sort in reverse order of intrinsic name so "abc.def" appears after
152 // "abd.def.ghi" in the overridden name matcher
153 std::sort(IntList.begin(), IntList.end(), [&](unsigned i, unsigned j) {
154 return Ints[i].Name > Ints[j].Name;
157 // Emit all the overloaded intrinsics first, build a table of the
158 // non-overloaded ones.
159 std::vector<StringMatcher::StringPair> MatchTable;
161 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
162 unsigned IntNo = IntList[i];
163 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
164 Ints[IntNo].EnumName + ";";
166 if (!Ints[IntNo].isOverloaded) {
167 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
171 // For overloaded intrinsics, only the prefix needs to match
172 std::string TheStr = Ints[IntNo].Name.substr(6);
173 TheStr += '.'; // Require "bswap." instead of bswap.
174 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
178 // Emit the matcher logic for the fixed length strings.
179 StringMatcher("NameR", MatchTable, OS).Emit(1);
180 OS << " break; // end of '" << I->first << "' case.\n";
187 void IntrinsicEmitter::
188 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
190 OS << "// Intrinsic ID to name table\n";
191 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
192 OS << " // Note that entry #0 is the invalid intrinsic!\n";
193 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
194 OS << " \"" << Ints[i].Name << "\",\n";
198 void IntrinsicEmitter::
199 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
201 OS << "// Intrinsic ID to overload bitset\n";
202 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
203 OS << "static const uint8_t OTable[] = {\n";
205 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
206 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
209 if (Ints[i].isOverloaded)
210 OS << " | (1<<" << (i+1)%8 << ')';
213 // OTable contains a true bit at the position if the intrinsic is overloaded.
214 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
219 // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
221 // Common values should be encoded with 0-15.
239 // Values from 16+ are only encodable with the inefficient encoding.
242 IIT_EMPTYSTRUCT = 18,
252 IIT_HALF_VEC_ARG = 28
256 static void EncodeFixedValueType(MVT::SimpleValueType VT,
257 std::vector<unsigned char> &Sig) {
258 if (MVT(VT).isInteger()) {
259 unsigned BitWidth = MVT(VT).getSizeInBits();
261 default: PrintFatalError("unhandled integer type width in intrinsic!");
262 case 1: return Sig.push_back(IIT_I1);
263 case 8: return Sig.push_back(IIT_I8);
264 case 16: return Sig.push_back(IIT_I16);
265 case 32: return Sig.push_back(IIT_I32);
266 case 64: return Sig.push_back(IIT_I64);
271 default: PrintFatalError("unhandled MVT in intrinsic!");
272 case MVT::f16: return Sig.push_back(IIT_F16);
273 case MVT::f32: return Sig.push_back(IIT_F32);
274 case MVT::f64: return Sig.push_back(IIT_F64);
275 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
276 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
277 // MVT::OtherVT is used to mean the empty struct type here.
278 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
279 // MVT::isVoid is used to represent varargs here.
280 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
285 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
288 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
289 std::vector<unsigned char> &Sig) {
291 if (R->isSubClassOf("LLVMMatchType")) {
292 unsigned Number = R->getValueAsInt("Number");
293 assert(Number < ArgCodes.size() && "Invalid matching number!");
294 if (R->isSubClassOf("LLVMExtendedType"))
295 Sig.push_back(IIT_EXTEND_ARG);
296 else if (R->isSubClassOf("LLVMTruncatedType"))
297 Sig.push_back(IIT_TRUNC_ARG);
298 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
299 Sig.push_back(IIT_HALF_VEC_ARG);
301 Sig.push_back(IIT_ARG);
302 return Sig.push_back((Number << 2) | ArgCodes[Number]);
305 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
310 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
311 case MVT::vAny: ++Tmp; // FALL THROUGH.
312 case MVT::fAny: ++Tmp; // FALL THROUGH.
314 // If this is an "any" valuetype, then the type is the type of the next
315 // type in the list specified to getIntrinsic().
316 Sig.push_back(IIT_ARG);
318 // Figure out what arg # this is consuming, and remember what kind it was.
319 unsigned ArgNo = ArgCodes.size();
320 ArgCodes.push_back(Tmp);
322 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
323 return Sig.push_back((ArgNo << 2) | Tmp);
327 unsigned AddrSpace = 0;
328 if (R->isSubClassOf("LLVMQualPointerType")) {
329 AddrSpace = R->getValueAsInt("AddrSpace");
330 assert(AddrSpace < 256 && "Address space exceeds 255");
333 Sig.push_back(IIT_ANYPTR);
334 Sig.push_back(AddrSpace);
336 Sig.push_back(IIT_PTR);
338 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
342 if (MVT(VT).isVector()) {
344 switch (VVT.getVectorNumElements()) {
345 default: PrintFatalError("unhandled vector type width in intrinsic!");
346 case 1: Sig.push_back(IIT_V1); break;
347 case 2: Sig.push_back(IIT_V2); break;
348 case 4: Sig.push_back(IIT_V4); break;
349 case 8: Sig.push_back(IIT_V8); break;
350 case 16: Sig.push_back(IIT_V16); break;
351 case 32: Sig.push_back(IIT_V32); break;
354 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
357 EncodeFixedValueType(VT, Sig);
361 #pragma optimize("",on)
364 /// ComputeFixedEncoding - If we can encode the type signature for this
365 /// intrinsic into 32 bits, return it. If not, return ~0U.
366 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
367 std::vector<unsigned char> &TypeSig) {
368 std::vector<unsigned char> ArgCodes;
370 if (Int.IS.RetVTs.empty())
371 TypeSig.push_back(IIT_Done);
372 else if (Int.IS.RetVTs.size() == 1 &&
373 Int.IS.RetVTs[0] == MVT::isVoid)
374 TypeSig.push_back(IIT_Done);
376 switch (Int.IS.RetVTs.size()) {
378 case 2: TypeSig.push_back(IIT_STRUCT2); break;
379 case 3: TypeSig.push_back(IIT_STRUCT3); break;
380 case 4: TypeSig.push_back(IIT_STRUCT4); break;
381 case 5: TypeSig.push_back(IIT_STRUCT5); break;
382 default: assert(0 && "Unhandled case in struct");
385 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
386 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
389 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
390 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
393 static void printIITEntry(raw_ostream &OS, unsigned char X) {
397 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
399 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
400 // capture it in this vector, otherwise store a ~0U.
401 std::vector<unsigned> FixedEncodings;
403 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
405 std::vector<unsigned char> TypeSig;
407 // Compute the unique argument type info.
408 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
409 // Get the signature for the intrinsic.
411 ComputeFixedEncoding(Ints[i], TypeSig);
413 // Check to see if we can encode it into a 32-bit word. We can only encode
414 // 8 nibbles into a 32-bit word.
415 if (TypeSig.size() <= 8) {
418 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
419 // If we had an unencodable argument, bail out.
420 if (TypeSig[i] > 15) {
424 Result = (Result << 4) | TypeSig[e-i-1];
427 // If this could be encoded into a 31-bit word, return it.
428 if (!Failed && (Result >> 31) == 0) {
429 FixedEncodings.push_back(Result);
434 // Otherwise, we're going to unique the sequence into the
435 // LongEncodingTable, and use its offset in the 32-bit table instead.
436 LongEncodingTable.add(TypeSig);
438 // This is a placehold that we'll replace after the table is laid out.
439 FixedEncodings.push_back(~0U);
442 LongEncodingTable.layout();
444 OS << "// Global intrinsic function declaration type table.\n";
445 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
447 OS << "static const unsigned IIT_Table[] = {\n ";
449 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
453 // If the entry fit in the table, just emit it.
454 if (FixedEncodings[i] != ~0U) {
455 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
460 ComputeFixedEncoding(Ints[i], TypeSig);
463 // Otherwise, emit the offset into the long encoding table. We emit it this
464 // way so that it is easier to read the offset in the .def file.
465 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
470 // Emit the shared table of register lists.
471 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
472 if (!LongEncodingTable.empty())
473 LongEncodingTable.emit(OS, printIITEntry);
474 OS << " 255\n};\n\n";
476 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
485 static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
486 switch (intrinsic.ModRef) {
487 case CodeGenIntrinsic::NoMem:
489 case CodeGenIntrinsic::ReadArgMem:
490 case CodeGenIntrinsic::ReadMem:
492 case CodeGenIntrinsic::ReadWriteArgMem:
493 case CodeGenIntrinsic::ReadWriteMem:
496 llvm_unreachable("bad mod-ref kind");
500 struct AttributeComparator {
501 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
502 // Sort throwing intrinsics after non-throwing intrinsics.
503 if (L->canThrow != R->canThrow)
506 if (L->isNoDuplicate != R->isNoDuplicate)
507 return R->isNoDuplicate;
509 if (L->isNoReturn != R->isNoReturn)
510 return R->isNoReturn;
512 // Try to order by readonly/readnone attribute.
513 ModRefKind LK = getModRefKind(*L);
514 ModRefKind RK = getModRefKind(*R);
515 if (LK != RK) return (LK > RK);
517 // Order by argument attributes.
518 // This is reliable because each side is already sorted internally.
519 return (L->ArgumentAttributes < R->ArgumentAttributes);
522 } // End anonymous namespace
524 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
525 void IntrinsicEmitter::
526 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
527 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
528 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
530 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
531 << "Intrinsic::ID id) {\n";
533 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
535 // Compute the maximum number of attribute arguments and the map
536 typedef std::map<const CodeGenIntrinsic*, unsigned,
537 AttributeComparator> UniqAttrMapTy;
538 UniqAttrMapTy UniqAttributes;
539 unsigned maxArgAttrs = 0;
540 unsigned AttrNum = 0;
541 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
542 const CodeGenIntrinsic &intrinsic = Ints[i];
544 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
545 unsigned &N = UniqAttributes[&intrinsic];
547 assert(AttrNum < 256 && "Too many unique attributes for table!");
551 // Emit an array of AttributeSet. Most intrinsics will have at least one
552 // entry, for the function itself (index ~1), which is usually nounwind.
553 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
555 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
556 const CodeGenIntrinsic &intrinsic = Ints[i];
558 OS << " " << UniqAttributes[&intrinsic] << ", // "
559 << intrinsic.Name << "\n";
563 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
564 OS << " unsigned NumAttrs = 0;\n";
565 OS << " if (id != 0) {\n";
566 OS << " switch(IntrinsicsToAttributesMap[id - ";
568 OS << "Intrinsic::num_intrinsics";
572 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
573 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
574 E = UniqAttributes.end(); I != E; ++I) {
575 OS << " case " << I->second << ": {\n";
577 const CodeGenIntrinsic &intrinsic = *(I->first);
579 // Keep track of the number of attributes we're writing out.
580 unsigned numAttrs = 0;
582 // The argument attributes are alreadys sorted by argument index.
583 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
586 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
588 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
589 bool addComma = false;
592 switch (intrinsic.ArgumentAttributes[ai].second) {
593 case CodeGenIntrinsic::NoCapture:
596 OS << "Attribute::NoCapture";
599 case CodeGenIntrinsic::ReadOnly:
602 OS << "Attribute::ReadOnly";
605 case CodeGenIntrinsic::ReadNone:
608 OS << "Attributes::ReadNone";
614 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
616 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
617 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
621 ModRefKind modRef = getModRefKind(intrinsic);
623 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn ||
624 intrinsic.isNoDuplicate) {
625 OS << " const Attribute::AttrKind Atts[] = {";
626 bool addComma = false;
627 if (!intrinsic.canThrow) {
628 OS << "Attribute::NoUnwind";
631 if (intrinsic.isNoReturn) {
634 OS << "Attribute::NoReturn";
637 if (intrinsic.isNoDuplicate) {
640 OS << "Attribute::NoDuplicate";
645 case MRK_none: break;
649 OS << "Attribute::ReadOnly";
654 OS << "Attribute::ReadNone";
658 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
659 << "AttributeSet::FunctionIndex, Atts);\n";
663 OS << " NumAttrs = " << numAttrs << ";\n";
667 OS << " return AttributeSet();\n";
674 OS << " return AttributeSet::get(C, ArrayRef<AttributeSet>(AS, "
677 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
680 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
681 void IntrinsicEmitter::
682 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
683 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
684 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
685 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
686 << "\"Unknown intrinsic.\");\n\n";
688 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
689 << " /* invalid */ UnknownModRefBehavior,\n";
690 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
691 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
692 switch (Ints[i].ModRef) {
693 case CodeGenIntrinsic::NoMem:
694 OS << "DoesNotAccessMemory,\n";
696 case CodeGenIntrinsic::ReadArgMem:
697 OS << "OnlyReadsArgumentPointees,\n";
699 case CodeGenIntrinsic::ReadMem:
700 OS << "OnlyReadsMemory,\n";
702 case CodeGenIntrinsic::ReadWriteArgMem:
703 OS << "OnlyAccessesArgumentPointees,\n";
705 case CodeGenIntrinsic::ReadWriteMem:
706 OS << "UnknownModRefBehavior,\n";
711 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
712 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
715 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
716 /// same target, and we already checked it.
717 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
718 const std::string &TargetPrefix,
721 std::vector<StringMatcher::StringPair> Results;
723 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
724 E = BIM.end(); I != E; ++I) {
725 std::string ResultCode =
726 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
727 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
730 StringMatcher("BuiltinName", Results, OS).Emit();
734 void IntrinsicEmitter::
735 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
737 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
739 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
740 if (!Ints[i].GCCBuiltinName.empty()) {
741 // Get the map for this target prefix.
742 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
744 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
745 Ints[i].EnumName)).second)
746 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
747 "': duplicate GCC builtin name!");
751 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
752 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
753 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
754 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
755 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
758 OS << "static " << TargetPrefix << "Intrinsic::ID "
759 << "getIntrinsicForGCCBuiltin(const char "
760 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
762 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
763 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
766 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
767 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
769 // Note: this could emit significantly better code if we cared.
770 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
772 if (!I->first.empty())
773 OS << "if (TargetPrefix == \"" << I->first << "\") ";
775 OS << "/* Target Independent Builtins */ ";
778 // Emit the comparisons for this target prefix.
779 EmitTargetBuiltins(I->second, TargetPrefix, OS);
783 if (!TargetPrefix.empty())
784 OS << "(" << TargetPrefix << "Intrinsic::ID)";
785 OS << "Intrinsic::not_intrinsic;\n";
792 void EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly = false) {
793 IntrinsicEmitter(RK, TargetOnly).run(OS);
796 } // End llvm namespace