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 EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
50 void EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
52 void EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints,
54 void EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints,
56 void EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
58 void EmitSuffix(raw_ostream &OS);
60 } // End anonymous namespace
62 //===----------------------------------------------------------------------===//
63 // IntrinsicEmitter Implementation
64 //===----------------------------------------------------------------------===//
66 void IntrinsicEmitter::run(raw_ostream &OS) {
67 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
69 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
71 if (TargetOnly && !Ints.empty())
72 TargetPrefix = Ints[0].TargetPrefix;
76 // Emit the enum information.
77 EmitEnumInfo(Ints, OS);
79 // Emit the intrinsic ID -> name table.
80 EmitIntrinsicToNameTable(Ints, OS);
82 // Emit the intrinsic ID -> overload table.
83 EmitIntrinsicToOverloadTable(Ints, OS);
85 // Emit the function name recognizer.
86 EmitFnNameRecognizer(Ints, OS);
88 // Emit the intrinsic declaration generator.
89 EmitGenerator(Ints, OS);
91 // Emit the intrinsic parameter attributes.
92 EmitAttributes(Ints, OS);
94 // Emit intrinsic alias analysis mod/ref behavior.
95 EmitModRefBehavior(Ints, OS);
97 // Emit code to translate GCC builtins into LLVM intrinsics.
98 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
103 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
104 OS << "// VisualStudio defines setjmp as _setjmp\n"
105 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
106 " !defined(setjmp_undefined_for_msvc)\n"
107 "# pragma push_macro(\"setjmp\")\n"
109 "# define setjmp_undefined_for_msvc\n"
113 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
114 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
115 "// let's return it to _setjmp state\n"
116 "# pragma pop_macro(\"setjmp\")\n"
117 "# undef setjmp_undefined_for_msvc\n"
121 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
123 OS << "// Enum values for Intrinsics.h\n";
124 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
125 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
126 OS << " " << Ints[i].EnumName;
127 OS << ((i != e-1) ? ", " : " ");
128 OS << std::string(40-Ints[i].EnumName.size(), ' ')
129 << "// " << Ints[i].Name << "\n";
134 struct IntrinsicNameSorter {
135 IntrinsicNameSorter(const std::vector<CodeGenIntrinsic> &I)
138 // Sort in reverse order of intrinsic name so "abc.def" appears after
139 // "abd.def.ghi" in the overridden name matcher
140 bool operator()(unsigned i, unsigned j) {
141 return Ints[i].Name > Ints[j].Name;
145 const std::vector<CodeGenIntrinsic> &Ints;
148 void IntrinsicEmitter::
149 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
151 // Build a 'first character of function name' -> intrinsic # mapping.
152 std::map<char, std::vector<unsigned> > IntMapping;
153 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
154 IntMapping[Ints[i].Name[5]].push_back(i);
156 OS << "// Function name -> enum value recognizer code.\n";
157 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
158 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
159 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
160 OS << " default: break;\n";
161 IntrinsicNameSorter Sorter(Ints);
162 // Emit the intrinsic matching stuff by first letter.
163 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
164 E = IntMapping.end(); I != E; ++I) {
165 OS << " case '" << I->first << "':\n";
166 std::vector<unsigned> &IntList = I->second;
168 // Sort intrinsics in reverse order of their names
169 std::sort(IntList.begin(), IntList.end(), Sorter);
171 // Emit all the overloaded intrinsics first, build a table of the
172 // non-overloaded ones.
173 std::vector<StringMatcher::StringPair> MatchTable;
175 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
176 unsigned IntNo = IntList[i];
177 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
178 Ints[IntNo].EnumName + ";";
180 if (!Ints[IntNo].isOverloaded) {
181 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
185 // For overloaded intrinsics, only the prefix needs to match
186 std::string TheStr = Ints[IntNo].Name.substr(6);
187 TheStr += '.'; // Require "bswap." instead of bswap.
188 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
192 // Emit the matcher logic for the fixed length strings.
193 StringMatcher("NameR", MatchTable, OS).Emit(1);
194 OS << " break; // end of '" << I->first << "' case.\n";
201 void IntrinsicEmitter::
202 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
204 OS << "// Intrinsic ID to name table\n";
205 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
206 OS << " // Note that entry #0 is the invalid intrinsic!\n";
207 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
208 OS << " \"" << Ints[i].Name << "\",\n";
212 void IntrinsicEmitter::
213 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
215 OS << "// Intrinsic ID to overload bitset\n";
216 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
217 OS << "static const uint8_t OTable[] = {\n";
219 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
220 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
223 if (Ints[i].isOverloaded)
224 OS << " | (1<<" << (i+1)%8 << ')';
227 // OTable contains a true bit at the position if the intrinsic is overloaded.
228 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
233 // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
235 // Common values should be encoded with 0-15.
253 // Values from 16+ are only encodable with the inefficient encoding.
256 IIT_EMPTYSTRUCT = 18,
261 IIT_EXTEND_VEC_ARG = 23,
262 IIT_TRUNC_VEC_ARG = 24,
269 static void EncodeFixedValueType(MVT::SimpleValueType VT,
270 std::vector<unsigned char> &Sig) {
271 if (EVT(VT).isInteger()) {
272 unsigned BitWidth = EVT(VT).getSizeInBits();
274 default: PrintFatalError("unhandled integer type width in intrinsic!");
275 case 1: return Sig.push_back(IIT_I1);
276 case 8: return Sig.push_back(IIT_I8);
277 case 16: return Sig.push_back(IIT_I16);
278 case 32: return Sig.push_back(IIT_I32);
279 case 64: return Sig.push_back(IIT_I64);
284 default: PrintFatalError("unhandled MVT in intrinsic!");
285 case MVT::f16: return Sig.push_back(IIT_F16);
286 case MVT::f32: return Sig.push_back(IIT_F32);
287 case MVT::f64: return Sig.push_back(IIT_F64);
288 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
289 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
290 // MVT::OtherVT is used to mean the empty struct type here.
291 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
292 // MVT::isVoid is used to represent varargs here.
293 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
298 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
301 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
302 std::vector<unsigned char> &Sig) {
304 if (R->isSubClassOf("LLVMMatchType")) {
305 unsigned Number = R->getValueAsInt("Number");
306 assert(Number < ArgCodes.size() && "Invalid matching number!");
307 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
308 Sig.push_back(IIT_EXTEND_VEC_ARG);
309 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
310 Sig.push_back(IIT_TRUNC_VEC_ARG);
312 Sig.push_back(IIT_ARG);
313 return Sig.push_back((Number << 2) | ArgCodes[Number]);
316 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
321 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
322 case MVT::vAny: ++Tmp; // FALL THROUGH.
323 case MVT::fAny: ++Tmp; // FALL THROUGH.
325 // If this is an "any" valuetype, then the type is the type of the next
326 // type in the list specified to getIntrinsic().
327 Sig.push_back(IIT_ARG);
329 // Figure out what arg # this is consuming, and remember what kind it was.
330 unsigned ArgNo = ArgCodes.size();
331 ArgCodes.push_back(Tmp);
333 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
334 return Sig.push_back((ArgNo << 2) | Tmp);
338 unsigned AddrSpace = 0;
339 if (R->isSubClassOf("LLVMQualPointerType")) {
340 AddrSpace = R->getValueAsInt("AddrSpace");
341 assert(AddrSpace < 256 && "Address space exceeds 255");
344 Sig.push_back(IIT_ANYPTR);
345 Sig.push_back(AddrSpace);
347 Sig.push_back(IIT_PTR);
349 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
353 if (EVT(VT).isVector()) {
355 switch (VVT.getVectorNumElements()) {
356 default: PrintFatalError("unhandled vector type width in intrinsic!");
357 case 1: Sig.push_back(IIT_V1); break;
358 case 2: Sig.push_back(IIT_V2); break;
359 case 4: Sig.push_back(IIT_V4); break;
360 case 8: Sig.push_back(IIT_V8); break;
361 case 16: Sig.push_back(IIT_V16); break;
362 case 32: Sig.push_back(IIT_V32); break;
365 return EncodeFixedValueType(VVT.getVectorElementType().
366 getSimpleVT().SimpleTy, Sig);
369 EncodeFixedValueType(VT, Sig);
373 #pragma optimize("",on)
376 /// ComputeFixedEncoding - If we can encode the type signature for this
377 /// intrinsic into 32 bits, return it. If not, return ~0U.
378 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
379 std::vector<unsigned char> &TypeSig) {
380 std::vector<unsigned char> ArgCodes;
382 if (Int.IS.RetVTs.empty())
383 TypeSig.push_back(IIT_Done);
384 else if (Int.IS.RetVTs.size() == 1 &&
385 Int.IS.RetVTs[0] == MVT::isVoid)
386 TypeSig.push_back(IIT_Done);
388 switch (Int.IS.RetVTs.size()) {
390 case 2: TypeSig.push_back(IIT_STRUCT2); break;
391 case 3: TypeSig.push_back(IIT_STRUCT3); break;
392 case 4: TypeSig.push_back(IIT_STRUCT4); break;
393 case 5: TypeSig.push_back(IIT_STRUCT5); break;
394 default: assert(0 && "Unhandled case in struct");
397 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
398 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
401 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
402 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
405 static void printIITEntry(raw_ostream &OS, unsigned char X) {
409 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
411 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
412 // capture it in this vector, otherwise store a ~0U.
413 std::vector<unsigned> FixedEncodings;
415 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
417 std::vector<unsigned char> TypeSig;
419 // Compute the unique argument type info.
420 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
421 // Get the signature for the intrinsic.
423 ComputeFixedEncoding(Ints[i], TypeSig);
425 // Check to see if we can encode it into a 32-bit word. We can only encode
426 // 8 nibbles into a 32-bit word.
427 if (TypeSig.size() <= 8) {
430 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
431 // If we had an unencodable argument, bail out.
432 if (TypeSig[i] > 15) {
436 Result = (Result << 4) | TypeSig[e-i-1];
439 // If this could be encoded into a 31-bit word, return it.
440 if (!Failed && (Result >> 31) == 0) {
441 FixedEncodings.push_back(Result);
446 // Otherwise, we're going to unique the sequence into the
447 // LongEncodingTable, and use its offset in the 32-bit table instead.
448 LongEncodingTable.add(TypeSig);
450 // This is a placehold that we'll replace after the table is laid out.
451 FixedEncodings.push_back(~0U);
454 LongEncodingTable.layout();
456 OS << "// Global intrinsic function declaration type table.\n";
457 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
459 OS << "static const unsigned IIT_Table[] = {\n ";
461 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
465 // If the entry fit in the table, just emit it.
466 if (FixedEncodings[i] != ~0U) {
467 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
472 ComputeFixedEncoding(Ints[i], TypeSig);
475 // Otherwise, emit the offset into the long encoding table. We emit it this
476 // way so that it is easier to read the offset in the .def file.
477 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
482 // Emit the shared table of register lists.
483 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
484 if (!LongEncodingTable.empty())
485 LongEncodingTable.emit(OS, printIITEntry);
486 OS << " 255\n};\n\n";
488 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
497 static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
498 switch (intrinsic.ModRef) {
499 case CodeGenIntrinsic::NoMem:
501 case CodeGenIntrinsic::ReadArgMem:
502 case CodeGenIntrinsic::ReadMem:
504 case CodeGenIntrinsic::ReadWriteArgMem:
505 case CodeGenIntrinsic::ReadWriteMem:
508 llvm_unreachable("bad mod-ref kind");
512 struct AttributeComparator {
513 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
514 // Sort throwing intrinsics after non-throwing intrinsics.
515 if (L->canThrow != R->canThrow)
518 if (L->isNoReturn != R->isNoReturn)
519 return R->isNoReturn;
521 // Try to order by readonly/readnone attribute.
522 ModRefKind LK = getModRefKind(*L);
523 ModRefKind RK = getModRefKind(*R);
524 if (LK != RK) return (LK > RK);
526 // Order by argument attributes.
527 // This is reliable because each side is already sorted internally.
528 return (L->ArgumentAttributes < R->ArgumentAttributes);
531 } // End anonymous namespace
533 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
534 void IntrinsicEmitter::
535 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
536 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
537 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
539 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
540 << "Intrinsic::ID id) {\n";
542 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
544 // Compute the maximum number of attribute arguments and the map
545 typedef std::map<const CodeGenIntrinsic*, unsigned,
546 AttributeComparator> UniqAttrMapTy;
547 UniqAttrMapTy UniqAttributes;
548 unsigned maxArgAttrs = 0;
549 unsigned AttrNum = 0;
550 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
551 const CodeGenIntrinsic &intrinsic = Ints[i];
553 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
554 unsigned &N = UniqAttributes[&intrinsic];
556 assert(AttrNum < 256 && "Too many unique attributes for table!");
560 // Emit an array of AttributeSet. Most intrinsics will have at least one
561 // entry, for the function itself (index ~1), which is usually nounwind.
562 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
564 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
565 const CodeGenIntrinsic &intrinsic = Ints[i];
567 OS << " " << UniqAttributes[&intrinsic] << ", // "
568 << intrinsic.Name << "\n";
572 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
573 OS << " unsigned NumAttrs = 0;\n";
574 OS << " if (id != 0) {\n";
575 OS << " SmallVector<Attribute::AttrKind, 8> AttrVec;\n";
576 OS << " switch(IntrinsicsToAttributesMap[id - ";
578 OS << "Intrinsic::num_intrinsics";
582 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
583 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
584 E = UniqAttributes.end(); I != E; ++I) {
585 OS << " case " << I->second << ":\n";
587 const CodeGenIntrinsic &intrinsic = *(I->first);
589 // Keep track of the number of attributes we're writing out.
590 unsigned numAttrs = 0;
592 // The argument attributes are alreadys sorted by argument index.
593 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
596 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
598 OS << " AttrVec.clear();\n";
601 switch (intrinsic.ArgumentAttributes[ai].second) {
602 case CodeGenIntrinsic::NoCapture:
603 OS << " AttrVec.push_back(Attribute::NoCapture);\n";
605 case CodeGenIntrinsic::ReadOnly:
606 OS << " AttrVec.push_back(Attribute::ReadOnly);\n";
608 case CodeGenIntrinsic::ReadNone:
609 OS << " AttrVec.push_back(Attribute::ReadNone);\n";
614 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
616 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
617 << argNo+1 << ", AttrVec);\n";
621 ModRefKind modRef = getModRefKind(intrinsic);
623 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn) {
624 OS << " AttrVec.clear();\n";
626 if (!intrinsic.canThrow)
627 OS << " AttrVec.push_back(Attribute::NoUnwind);\n";
628 if (intrinsic.isNoReturn)
629 OS << " AttrVec.push_back(Attribute::NoReturn);\n";
632 case MRK_none: break;
634 OS << " AttrVec.push_back(Attribute::ReadOnly);\n";
637 OS << " AttrVec.push_back(Attribute::ReadNone);\n";
640 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
641 << "AttributeSet::FunctionIndex, AttrVec);\n";
645 OS << " NumAttrs = " << numAttrs << ";\n";
648 OS << " return AttributeSet();\n";
654 OS << " return AttributeSet::get(C, ArrayRef<AttributeSet>(AS, "
657 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
660 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
661 void IntrinsicEmitter::
662 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
663 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
664 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
665 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
666 << "\"Unknown intrinsic.\");\n\n";
668 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
669 << " /* invalid */ UnknownModRefBehavior,\n";
670 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
671 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
672 switch (Ints[i].ModRef) {
673 case CodeGenIntrinsic::NoMem:
674 OS << "DoesNotAccessMemory,\n";
676 case CodeGenIntrinsic::ReadArgMem:
677 OS << "OnlyReadsArgumentPointees,\n";
679 case CodeGenIntrinsic::ReadMem:
680 OS << "OnlyReadsMemory,\n";
682 case CodeGenIntrinsic::ReadWriteArgMem:
683 OS << "OnlyAccessesArgumentPointees,\n";
685 case CodeGenIntrinsic::ReadWriteMem:
686 OS << "UnknownModRefBehavior,\n";
691 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
692 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
695 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
696 /// same target, and we already checked it.
697 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
698 const std::string &TargetPrefix,
701 std::vector<StringMatcher::StringPair> Results;
703 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
704 E = BIM.end(); I != E; ++I) {
705 std::string ResultCode =
706 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
707 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
710 StringMatcher("BuiltinName", Results, OS).Emit();
714 void IntrinsicEmitter::
715 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
717 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
719 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
720 if (!Ints[i].GCCBuiltinName.empty()) {
721 // Get the map for this target prefix.
722 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
724 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
725 Ints[i].EnumName)).second)
726 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
727 "': duplicate GCC builtin name!");
731 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
732 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
733 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
734 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
735 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
738 OS << "static " << TargetPrefix << "Intrinsic::ID "
739 << "getIntrinsicForGCCBuiltin(const char "
740 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
742 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
743 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
746 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
747 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
749 // Note: this could emit significantly better code if we cared.
750 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
752 if (!I->first.empty())
753 OS << "if (TargetPrefix == \"" << I->first << "\") ";
755 OS << "/* Target Independent Builtins */ ";
758 // Emit the comparisons for this target prefix.
759 EmitTargetBuiltins(I->second, TargetPrefix, OS);
763 if (!TargetPrefix.empty())
764 OS << "(" << TargetPrefix << "Intrinsic::ID)";
765 OS << "Intrinsic::not_intrinsic;\n";
772 void EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly = false) {
773 IntrinsicEmitter(RK, TargetOnly).run(OS);
776 } // End llvm namespace