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 EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
59 void EmitSuffix(raw_ostream &OS);
61 } // End anonymous namespace
63 //===----------------------------------------------------------------------===//
64 // IntrinsicEmitter Implementation
65 //===----------------------------------------------------------------------===//
67 void IntrinsicEmitter::run(raw_ostream &OS) {
68 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
70 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
72 if (TargetOnly && !Ints.empty())
73 TargetPrefix = Ints[0].TargetPrefix;
77 // Emit the enum information.
78 EmitEnumInfo(Ints, OS);
80 // Emit the intrinsic ID -> name table.
81 EmitIntrinsicToNameTable(Ints, OS);
83 // Emit the intrinsic ID -> overload table.
84 EmitIntrinsicToOverloadTable(Ints, OS);
86 // Emit the function name recognizer.
87 EmitFnNameRecognizer(Ints, OS);
89 // Emit the intrinsic declaration generator.
90 EmitGenerator(Ints, OS);
92 // Emit the intrinsic parameter attributes.
93 EmitAttributes(Ints, OS);
95 // Emit intrinsic alias analysis mod/ref behavior.
96 EmitModRefBehavior(Ints, OS);
98 // Emit code to translate GCC builtins into LLVM intrinsics.
99 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
101 // Emit code to translate MS builtins into LLVM intrinsics.
102 EmitIntrinsicToMSBuiltinMap(Ints, OS);
107 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
108 OS << "// VisualStudio defines setjmp as _setjmp\n"
109 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
110 " !defined(setjmp_undefined_for_msvc)\n"
111 "# pragma push_macro(\"setjmp\")\n"
113 "# define setjmp_undefined_for_msvc\n"
117 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
118 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
119 "// let's return it to _setjmp state\n"
120 "# pragma pop_macro(\"setjmp\")\n"
121 "# undef setjmp_undefined_for_msvc\n"
125 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
127 OS << "// Enum values for Intrinsics.h\n";
128 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
129 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
130 OS << " " << Ints[i].EnumName;
131 OS << ((i != e-1) ? ", " : " ");
132 if (Ints[i].EnumName.size() < 40)
133 OS << std::string(40-Ints[i].EnumName.size(), ' ');
134 OS << " // " << Ints[i].Name << "\n";
139 void IntrinsicEmitter::
140 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
142 // Build a 'first character of function name' -> intrinsic # mapping.
143 std::map<char, std::vector<unsigned> > IntMapping;
144 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
145 IntMapping[Ints[i].Name[5]].push_back(i);
147 OS << "// Function name -> enum value recognizer code.\n";
148 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
149 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
150 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
151 OS << " default: break;\n";
152 // Emit the intrinsic matching stuff by first letter.
153 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
154 E = IntMapping.end(); I != E; ++I) {
155 OS << " case '" << I->first << "':\n";
156 std::vector<unsigned> &IntList = I->second;
158 // Sort in reverse order of intrinsic name so "abc.def" appears after
159 // "abd.def.ghi" in the overridden name matcher
160 std::sort(IntList.begin(), IntList.end(), [&](unsigned i, unsigned j) {
161 return Ints[i].Name > Ints[j].Name;
164 // Emit all the overloaded intrinsics first, build a table of the
165 // non-overloaded ones.
166 std::vector<StringMatcher::StringPair> MatchTable;
168 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
169 unsigned IntNo = IntList[i];
170 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
171 Ints[IntNo].EnumName + ";";
173 if (!Ints[IntNo].isOverloaded) {
174 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
178 // For overloaded intrinsics, only the prefix needs to match
179 std::string TheStr = Ints[IntNo].Name.substr(6);
180 TheStr += '.'; // Require "bswap." instead of bswap.
181 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
185 // Emit the matcher logic for the fixed length strings.
186 StringMatcher("NameR", MatchTable, OS).Emit(1);
187 OS << " break; // end of '" << I->first << "' case.\n";
194 void IntrinsicEmitter::
195 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
197 OS << "// Intrinsic ID to name table\n";
198 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
199 OS << " // Note that entry #0 is the invalid intrinsic!\n";
200 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
201 OS << " \"" << Ints[i].Name << "\",\n";
205 void IntrinsicEmitter::
206 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
208 OS << "// Intrinsic ID to overload bitset\n";
209 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
210 OS << "static const uint8_t OTable[] = {\n";
212 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
213 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
216 if (Ints[i].isOverloaded)
217 OS << " | (1<<" << (i+1)%8 << ')';
220 // OTable contains a true bit at the position if the intrinsic is overloaded.
221 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
226 // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
228 // Common values should be encoded with 0-15.
246 // Values from 16+ are only encodable with the inefficient encoding.
251 IIT_EMPTYSTRUCT = 20,
261 IIT_HALF_VEC_ARG = 30,
262 IIT_SAME_VEC_WIDTH_ARG = 31,
264 IIT_VEC_OF_PTRS_TO_ELT = 33,
269 static void EncodeFixedValueType(MVT::SimpleValueType VT,
270 std::vector<unsigned char> &Sig) {
271 if (MVT(VT).isInteger()) {
272 unsigned BitWidth = MVT(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);
280 case 128: return Sig.push_back(IIT_I128);
285 default: PrintFatalError("unhandled MVT in intrinsic!");
286 case MVT::f16: return Sig.push_back(IIT_F16);
287 case MVT::f32: return Sig.push_back(IIT_F32);
288 case MVT::f64: return Sig.push_back(IIT_F64);
289 case MVT::token: return Sig.push_back(IIT_TOKEN);
290 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
291 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
292 // MVT::OtherVT is used to mean the empty struct type here.
293 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
294 // MVT::isVoid is used to represent varargs here.
295 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
299 #if defined(_MSC_VER) && !defined(__clang__)
300 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
303 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
304 std::vector<unsigned char> &Sig) {
306 if (R->isSubClassOf("LLVMMatchType")) {
307 unsigned Number = R->getValueAsInt("Number");
308 assert(Number < ArgCodes.size() && "Invalid matching number!");
309 if (R->isSubClassOf("LLVMExtendedType"))
310 Sig.push_back(IIT_EXTEND_ARG);
311 else if (R->isSubClassOf("LLVMTruncatedType"))
312 Sig.push_back(IIT_TRUNC_ARG);
313 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
314 Sig.push_back(IIT_HALF_VEC_ARG);
315 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
316 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
317 Sig.push_back((Number << 3) | ArgCodes[Number]);
318 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
319 EncodeFixedValueType(VT, Sig);
322 else if (R->isSubClassOf("LLVMPointerTo"))
323 Sig.push_back(IIT_PTR_TO_ARG);
324 else if (R->isSubClassOf("LLVMVectorOfPointersToElt"))
325 Sig.push_back(IIT_VEC_OF_PTRS_TO_ELT);
327 Sig.push_back(IIT_ARG);
328 return Sig.push_back((Number << 3) | ArgCodes[Number]);
331 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
336 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
337 case MVT::vAny: ++Tmp; // FALL THROUGH.
338 case MVT::fAny: ++Tmp; // FALL THROUGH.
339 case MVT::iAny: ++Tmp; // FALL THROUGH.
341 // If this is an "any" valuetype, then the type is the type of the next
342 // type in the list specified to getIntrinsic().
343 Sig.push_back(IIT_ARG);
345 // Figure out what arg # this is consuming, and remember what kind it was.
346 unsigned ArgNo = ArgCodes.size();
347 ArgCodes.push_back(Tmp);
349 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
350 return Sig.push_back((ArgNo << 3) | Tmp);
354 unsigned AddrSpace = 0;
355 if (R->isSubClassOf("LLVMQualPointerType")) {
356 AddrSpace = R->getValueAsInt("AddrSpace");
357 assert(AddrSpace < 256 && "Address space exceeds 255");
360 Sig.push_back(IIT_ANYPTR);
361 Sig.push_back(AddrSpace);
363 Sig.push_back(IIT_PTR);
365 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
369 if (MVT(VT).isVector()) {
371 switch (VVT.getVectorNumElements()) {
372 default: PrintFatalError("unhandled vector type width in intrinsic!");
373 case 1: Sig.push_back(IIT_V1); break;
374 case 2: Sig.push_back(IIT_V2); break;
375 case 4: Sig.push_back(IIT_V4); break;
376 case 8: Sig.push_back(IIT_V8); break;
377 case 16: Sig.push_back(IIT_V16); break;
378 case 32: Sig.push_back(IIT_V32); break;
379 case 64: Sig.push_back(IIT_V64); break;
382 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
385 EncodeFixedValueType(VT, Sig);
388 #if defined(_MSC_VER) && !defined(__clang__)
389 #pragma optimize("",on)
392 /// ComputeFixedEncoding - If we can encode the type signature for this
393 /// intrinsic into 32 bits, return it. If not, return ~0U.
394 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
395 std::vector<unsigned char> &TypeSig) {
396 std::vector<unsigned char> ArgCodes;
398 if (Int.IS.RetVTs.empty())
399 TypeSig.push_back(IIT_Done);
400 else if (Int.IS.RetVTs.size() == 1 &&
401 Int.IS.RetVTs[0] == MVT::isVoid)
402 TypeSig.push_back(IIT_Done);
404 switch (Int.IS.RetVTs.size()) {
406 case 2: TypeSig.push_back(IIT_STRUCT2); break;
407 case 3: TypeSig.push_back(IIT_STRUCT3); break;
408 case 4: TypeSig.push_back(IIT_STRUCT4); break;
409 case 5: TypeSig.push_back(IIT_STRUCT5); break;
410 default: llvm_unreachable("Unhandled case in struct");
413 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
414 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
417 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
418 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
421 static void printIITEntry(raw_ostream &OS, unsigned char X) {
425 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
427 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
428 // capture it in this vector, otherwise store a ~0U.
429 std::vector<unsigned> FixedEncodings;
431 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
433 std::vector<unsigned char> TypeSig;
435 // Compute the unique argument type info.
436 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
437 // Get the signature for the intrinsic.
439 ComputeFixedEncoding(Ints[i], TypeSig);
441 // Check to see if we can encode it into a 32-bit word. We can only encode
442 // 8 nibbles into a 32-bit word.
443 if (TypeSig.size() <= 8) {
446 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
447 // If we had an unencodable argument, bail out.
448 if (TypeSig[i] > 15) {
452 Result = (Result << 4) | TypeSig[e-i-1];
455 // If this could be encoded into a 31-bit word, return it.
456 if (!Failed && (Result >> 31) == 0) {
457 FixedEncodings.push_back(Result);
462 // Otherwise, we're going to unique the sequence into the
463 // LongEncodingTable, and use its offset in the 32-bit table instead.
464 LongEncodingTable.add(TypeSig);
466 // This is a placehold that we'll replace after the table is laid out.
467 FixedEncodings.push_back(~0U);
470 LongEncodingTable.layout();
472 OS << "// Global intrinsic function declaration type table.\n";
473 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
475 OS << "static const unsigned IIT_Table[] = {\n ";
477 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
481 // If the entry fit in the table, just emit it.
482 if (FixedEncodings[i] != ~0U) {
483 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
488 ComputeFixedEncoding(Ints[i], TypeSig);
491 // Otherwise, emit the offset into the long encoding table. We emit it this
492 // way so that it is easier to read the offset in the .def file.
493 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
498 // Emit the shared table of register lists.
499 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
500 if (!LongEncodingTable.empty())
501 LongEncodingTable.emit(OS, printIITEntry);
502 OS << " 255\n};\n\n";
504 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
508 struct AttributeComparator {
509 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
510 // Sort throwing intrinsics after non-throwing intrinsics.
511 if (L->canThrow != R->canThrow)
514 if (L->isNoDuplicate != R->isNoDuplicate)
515 return R->isNoDuplicate;
517 if (L->isNoReturn != R->isNoReturn)
518 return R->isNoReturn;
520 if (L->isConvergent != R->isConvergent)
521 return R->isConvergent;
523 // Try to order by readonly/readnone attribute.
524 CodeGenIntrinsic::ModRefKind LK = L->ModRef;
525 CodeGenIntrinsic::ModRefKind RK = R->ModRef;
526 if (LK != RK) return (LK > RK);
528 // Order by argument attributes.
529 // This is reliable because each side is already sorted internally.
530 return (L->ArgumentAttributes < R->ArgumentAttributes);
533 } // End anonymous namespace
535 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
536 void IntrinsicEmitter::
537 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
538 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
539 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
541 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
542 << "Intrinsic::ID id) {\n";
544 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
546 // Compute the maximum number of attribute arguments and the map
547 typedef std::map<const CodeGenIntrinsic*, unsigned,
548 AttributeComparator> UniqAttrMapTy;
549 UniqAttrMapTy UniqAttributes;
550 unsigned maxArgAttrs = 0;
551 unsigned AttrNum = 0;
552 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
553 const CodeGenIntrinsic &intrinsic = Ints[i];
555 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
556 unsigned &N = UniqAttributes[&intrinsic];
558 assert(AttrNum < 256 && "Too many unique attributes for table!");
562 // Emit an array of AttributeSet. Most intrinsics will have at least one
563 // entry, for the function itself (index ~1), which is usually nounwind.
564 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
566 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
567 const CodeGenIntrinsic &intrinsic = Ints[i];
569 OS << " " << UniqAttributes[&intrinsic] << ", // "
570 << intrinsic.Name << "\n";
574 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
575 OS << " unsigned NumAttrs = 0;\n";
576 OS << " if (id != 0) {\n";
577 OS << " switch(IntrinsicsToAttributesMap[id - ";
579 OS << "Intrinsic::num_intrinsics";
583 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
584 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
585 E = UniqAttributes.end(); I != E; ++I) {
586 OS << " case " << I->second << ": {\n";
588 const CodeGenIntrinsic &intrinsic = *(I->first);
590 // Keep track of the number of attributes we're writing out.
591 unsigned numAttrs = 0;
593 // The argument attributes are alreadys sorted by argument index.
594 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
597 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
599 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
600 bool addComma = false;
603 switch (intrinsic.ArgumentAttributes[ai].second) {
604 case CodeGenIntrinsic::NoCapture:
607 OS << "Attribute::NoCapture";
610 case CodeGenIntrinsic::ReadOnly:
613 OS << "Attribute::ReadOnly";
616 case CodeGenIntrinsic::ReadNone:
619 OS << "Attribute::ReadNone";
625 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
627 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
628 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
632 if (!intrinsic.canThrow ||
633 intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
634 intrinsic.isNoReturn || intrinsic.isNoDuplicate ||
635 intrinsic.isConvergent) {
636 OS << " const Attribute::AttrKind Atts[] = {";
637 bool addComma = false;
638 if (!intrinsic.canThrow) {
639 OS << "Attribute::NoUnwind";
642 if (intrinsic.isNoReturn) {
645 OS << "Attribute::NoReturn";
648 if (intrinsic.isNoDuplicate) {
651 OS << "Attribute::NoDuplicate";
654 if (intrinsic.isConvergent) {
657 OS << "Attribute::Convergent";
661 switch (intrinsic.ModRef) {
662 case CodeGenIntrinsic::NoMem:
665 OS << "Attribute::ReadNone";
667 case CodeGenIntrinsic::ReadArgMem:
670 OS << "Attribute::ReadOnly,";
671 OS << "Attribute::ArgMemOnly";
673 case CodeGenIntrinsic::ReadMem:
676 OS << "Attribute::ReadOnly";
678 case CodeGenIntrinsic::ReadWriteArgMem:
681 OS << "Attribute::ArgMemOnly";
683 case CodeGenIntrinsic::ReadWriteMem:
687 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
688 << "AttributeSet::FunctionIndex, Atts);\n";
692 OS << " NumAttrs = " << numAttrs << ";\n";
696 OS << " return AttributeSet();\n";
703 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
705 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
708 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
709 void IntrinsicEmitter::
710 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
711 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
712 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
713 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
714 << "\"Unknown intrinsic.\");\n\n";
716 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
717 << " /* invalid */ FMRB_UnknownModRefBehavior,\n";
718 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
719 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
720 switch (Ints[i].ModRef) {
721 case CodeGenIntrinsic::NoMem:
722 OS << "FMRB_DoesNotAccessMemory,\n";
724 case CodeGenIntrinsic::ReadArgMem:
725 OS << "FMRB_OnlyReadsArgumentPointees,\n";
727 case CodeGenIntrinsic::ReadMem:
728 OS << "FMRB_OnlyReadsMemory,\n";
730 case CodeGenIntrinsic::ReadWriteArgMem:
731 OS << "FMRB_OnlyAccessesArgumentPointees,\n";
733 case CodeGenIntrinsic::ReadWriteMem:
734 OS << "FMRB_UnknownModRefBehavior,\n";
740 "static_cast<FunctionModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
741 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
744 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
745 /// same target, and we already checked it.
746 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
747 const std::string &TargetPrefix,
750 std::vector<StringMatcher::StringPair> Results;
752 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
753 E = BIM.end(); I != E; ++I) {
754 std::string ResultCode =
755 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
756 Results.emplace_back(I->first, ResultCode);
759 StringMatcher("BuiltinName", Results, OS).Emit();
763 void IntrinsicEmitter::
764 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
766 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
768 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
769 if (!Ints[i].GCCBuiltinName.empty()) {
770 // Get the map for this target prefix.
771 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
773 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
774 Ints[i].EnumName)).second)
775 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
776 "': duplicate GCC builtin name!");
780 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
781 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
782 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
783 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
784 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
787 OS << "static " << TargetPrefix << "Intrinsic::ID "
788 << "getIntrinsicForGCCBuiltin(const char "
789 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
791 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
792 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
795 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
796 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
798 // Note: this could emit significantly better code if we cared.
799 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
801 if (!I->first.empty())
802 OS << "if (TargetPrefix == \"" << I->first << "\") ";
804 OS << "/* Target Independent Builtins */ ";
807 // Emit the comparisons for this target prefix.
808 EmitTargetBuiltins(I->second, TargetPrefix, OS);
812 if (!TargetPrefix.empty())
813 OS << "(" << TargetPrefix << "Intrinsic::ID)";
814 OS << "Intrinsic::not_intrinsic;\n";
819 void IntrinsicEmitter::
820 EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
822 std::map<std::string, std::map<std::string, std::string>> TargetBuiltins;
824 for (const auto &Intrinsic : Ints) {
825 if (Intrinsic.MSBuiltinName.empty())
828 auto &Builtins = TargetBuiltins[Intrinsic.TargetPrefix];
829 if (!Builtins.insert(std::make_pair(Intrinsic.MSBuiltinName,
830 Intrinsic.EnumName)).second)
831 PrintFatalError("Intrinsic '" + Intrinsic.TheDef->getName() + "': "
832 "duplicate MS builtin name!");
835 OS << "// Get the LLVM intrinsic that corresponds to a MS builtin.\n"
836 "// This is used by the C front-end. The MS builtin name is passed\n"
837 "// in as a BuiltinName, and a target prefix (e.g. 'arm') is passed\n"
838 "// in as a TargetPrefix. The result is assigned to 'IntrinsicID'.\n"
839 "#ifdef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN\n";
841 OS << (TargetOnly ? "static " + TargetPrefix : "") << "Intrinsic::ID "
842 << (TargetOnly ? "" : "Intrinsic::")
843 << "getIntrinsicForMSBuiltin(const char *TP, const char *BN) {\n";
844 OS << " StringRef BuiltinName(BN);\n"
845 " StringRef TargetPrefix(TP);\n"
848 for (const auto &Builtins : TargetBuiltins) {
850 if (Builtins.first.empty())
851 OS << "/* Target Independent Builtins */ ";
853 OS << "if (TargetPrefix == \"" << Builtins.first << "\") ";
855 EmitTargetBuiltins(Builtins.second, TargetPrefix, OS);
860 if (!TargetPrefix.empty())
861 OS << "(" << TargetPrefix << "Intrinsic::ID)";
862 OS << "Intrinsic::not_intrinsic;\n";
868 void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
869 IntrinsicEmitter(RK, TargetOnly).run(OS);