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.
250 IIT_EMPTYSTRUCT = 19,
260 IIT_HALF_VEC_ARG = 29,
261 IIT_SAME_VEC_WIDTH_ARG = 30,
266 static void EncodeFixedValueType(MVT::SimpleValueType VT,
267 std::vector<unsigned char> &Sig) {
268 if (MVT(VT).isInteger()) {
269 unsigned BitWidth = MVT(VT).getSizeInBits();
271 default: PrintFatalError("unhandled integer type width in intrinsic!");
272 case 1: return Sig.push_back(IIT_I1);
273 case 8: return Sig.push_back(IIT_I8);
274 case 16: return Sig.push_back(IIT_I16);
275 case 32: return Sig.push_back(IIT_I32);
276 case 64: return Sig.push_back(IIT_I64);
281 default: PrintFatalError("unhandled MVT in intrinsic!");
282 case MVT::f16: return Sig.push_back(IIT_F16);
283 case MVT::f32: return Sig.push_back(IIT_F32);
284 case MVT::f64: return Sig.push_back(IIT_F64);
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);
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 << 2) | 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);
321 Sig.push_back(IIT_ARG);
322 return Sig.push_back((Number << 2) | ArgCodes[Number]);
325 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
330 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
331 case MVT::vAny: ++Tmp; // FALL THROUGH.
332 case MVT::fAny: ++Tmp; // FALL THROUGH.
334 // If this is an "any" valuetype, then the type is the type of the next
335 // type in the list specified to getIntrinsic().
336 Sig.push_back(IIT_ARG);
338 // Figure out what arg # this is consuming, and remember what kind it was.
339 unsigned ArgNo = ArgCodes.size();
340 ArgCodes.push_back(Tmp);
342 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
343 return Sig.push_back((ArgNo << 2) | Tmp);
347 unsigned AddrSpace = 0;
348 if (R->isSubClassOf("LLVMQualPointerType")) {
349 AddrSpace = R->getValueAsInt("AddrSpace");
350 assert(AddrSpace < 256 && "Address space exceeds 255");
353 Sig.push_back(IIT_ANYPTR);
354 Sig.push_back(AddrSpace);
356 Sig.push_back(IIT_PTR);
358 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
362 if (MVT(VT).isVector()) {
364 switch (VVT.getVectorNumElements()) {
365 default: PrintFatalError("unhandled vector type width in intrinsic!");
366 case 1: Sig.push_back(IIT_V1); break;
367 case 2: Sig.push_back(IIT_V2); break;
368 case 4: Sig.push_back(IIT_V4); break;
369 case 8: Sig.push_back(IIT_V8); break;
370 case 16: Sig.push_back(IIT_V16); break;
371 case 32: Sig.push_back(IIT_V32); break;
372 case 64: Sig.push_back(IIT_V64); break;
375 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
378 EncodeFixedValueType(VT, Sig);
382 #pragma optimize("",on)
385 /// ComputeFixedEncoding - If we can encode the type signature for this
386 /// intrinsic into 32 bits, return it. If not, return ~0U.
387 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
388 std::vector<unsigned char> &TypeSig) {
389 std::vector<unsigned char> ArgCodes;
391 if (Int.IS.RetVTs.empty())
392 TypeSig.push_back(IIT_Done);
393 else if (Int.IS.RetVTs.size() == 1 &&
394 Int.IS.RetVTs[0] == MVT::isVoid)
395 TypeSig.push_back(IIT_Done);
397 switch (Int.IS.RetVTs.size()) {
399 case 2: TypeSig.push_back(IIT_STRUCT2); break;
400 case 3: TypeSig.push_back(IIT_STRUCT3); break;
401 case 4: TypeSig.push_back(IIT_STRUCT4); break;
402 case 5: TypeSig.push_back(IIT_STRUCT5); break;
403 default: llvm_unreachable("Unhandled case in struct");
406 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
407 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
410 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
411 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
414 static void printIITEntry(raw_ostream &OS, unsigned char X) {
418 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
420 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
421 // capture it in this vector, otherwise store a ~0U.
422 std::vector<unsigned> FixedEncodings;
424 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
426 std::vector<unsigned char> TypeSig;
428 // Compute the unique argument type info.
429 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
430 // Get the signature for the intrinsic.
432 ComputeFixedEncoding(Ints[i], TypeSig);
434 // Check to see if we can encode it into a 32-bit word. We can only encode
435 // 8 nibbles into a 32-bit word.
436 if (TypeSig.size() <= 8) {
439 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
440 // If we had an unencodable argument, bail out.
441 if (TypeSig[i] > 15) {
445 Result = (Result << 4) | TypeSig[e-i-1];
448 // If this could be encoded into a 31-bit word, return it.
449 if (!Failed && (Result >> 31) == 0) {
450 FixedEncodings.push_back(Result);
455 // Otherwise, we're going to unique the sequence into the
456 // LongEncodingTable, and use its offset in the 32-bit table instead.
457 LongEncodingTable.add(TypeSig);
459 // This is a placehold that we'll replace after the table is laid out.
460 FixedEncodings.push_back(~0U);
463 LongEncodingTable.layout();
465 OS << "// Global intrinsic function declaration type table.\n";
466 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
468 OS << "static const unsigned IIT_Table[] = {\n ";
470 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
474 // If the entry fit in the table, just emit it.
475 if (FixedEncodings[i] != ~0U) {
476 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
481 ComputeFixedEncoding(Ints[i], TypeSig);
484 // Otherwise, emit the offset into the long encoding table. We emit it this
485 // way so that it is easier to read the offset in the .def file.
486 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
491 // Emit the shared table of register lists.
492 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
493 if (!LongEncodingTable.empty())
494 LongEncodingTable.emit(OS, printIITEntry);
495 OS << " 255\n};\n\n";
497 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
508 static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
509 switch (intrinsic.ModRef) {
510 case CodeGenIntrinsic::NoMem:
512 case CodeGenIntrinsic::ReadArgMem:
513 case CodeGenIntrinsic::ReadMem:
515 case CodeGenIntrinsic::ReadWriteArgMem:
516 case CodeGenIntrinsic::ReadWriteMem:
519 llvm_unreachable("bad mod-ref kind");
523 struct AttributeComparator {
524 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
525 // Sort throwing intrinsics after non-throwing intrinsics.
526 if (L->canThrow != R->canThrow)
529 if (L->isNoDuplicate != R->isNoDuplicate)
530 return R->isNoDuplicate;
532 if (L->isNoReturn != R->isNoReturn)
533 return R->isNoReturn;
535 // Try to order by readonly/readnone attribute.
536 ModRefKind LK = getModRefKind(*L);
537 ModRefKind RK = getModRefKind(*R);
538 if (LK != RK) return (LK > RK);
540 // Order by argument attributes.
541 // This is reliable because each side is already sorted internally.
542 return (L->ArgumentAttributes < R->ArgumentAttributes);
545 } // End anonymous namespace
547 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
548 void IntrinsicEmitter::
549 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
550 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
551 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
553 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
554 << "Intrinsic::ID id) {\n";
556 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
558 // Compute the maximum number of attribute arguments and the map
559 typedef std::map<const CodeGenIntrinsic*, unsigned,
560 AttributeComparator> UniqAttrMapTy;
561 UniqAttrMapTy UniqAttributes;
562 unsigned maxArgAttrs = 0;
563 unsigned AttrNum = 0;
564 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
565 const CodeGenIntrinsic &intrinsic = Ints[i];
567 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
568 unsigned &N = UniqAttributes[&intrinsic];
570 assert(AttrNum < 256 && "Too many unique attributes for table!");
574 // Emit an array of AttributeSet. Most intrinsics will have at least one
575 // entry, for the function itself (index ~1), which is usually nounwind.
576 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
578 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
579 const CodeGenIntrinsic &intrinsic = Ints[i];
581 OS << " " << UniqAttributes[&intrinsic] << ", // "
582 << intrinsic.Name << "\n";
586 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
587 OS << " unsigned NumAttrs = 0;\n";
588 OS << " if (id != 0) {\n";
589 OS << " switch(IntrinsicsToAttributesMap[id - ";
591 OS << "Intrinsic::num_intrinsics";
595 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
596 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
597 E = UniqAttributes.end(); I != E; ++I) {
598 OS << " case " << I->second << ": {\n";
600 const CodeGenIntrinsic &intrinsic = *(I->first);
602 // Keep track of the number of attributes we're writing out.
603 unsigned numAttrs = 0;
605 // The argument attributes are alreadys sorted by argument index.
606 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
609 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
611 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
612 bool addComma = false;
615 switch (intrinsic.ArgumentAttributes[ai].second) {
616 case CodeGenIntrinsic::NoCapture:
619 OS << "Attribute::NoCapture";
622 case CodeGenIntrinsic::ReadOnly:
625 OS << "Attribute::ReadOnly";
628 case CodeGenIntrinsic::ReadNone:
631 OS << "Attributes::ReadNone";
637 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
639 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
640 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
644 ModRefKind modRef = getModRefKind(intrinsic);
646 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn ||
647 intrinsic.isNoDuplicate) {
648 OS << " const Attribute::AttrKind Atts[] = {";
649 bool addComma = false;
650 if (!intrinsic.canThrow) {
651 OS << "Attribute::NoUnwind";
654 if (intrinsic.isNoReturn) {
657 OS << "Attribute::NoReturn";
660 if (intrinsic.isNoDuplicate) {
663 OS << "Attribute::NoDuplicate";
668 case MRK_none: break;
672 OS << "Attribute::ReadOnly";
677 OS << "Attribute::ReadNone";
681 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
682 << "AttributeSet::FunctionIndex, Atts);\n";
686 OS << " NumAttrs = " << numAttrs << ";\n";
690 OS << " return AttributeSet();\n";
697 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
699 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
702 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
703 void IntrinsicEmitter::
704 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
705 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
706 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
707 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
708 << "\"Unknown intrinsic.\");\n\n";
710 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
711 << " /* invalid */ UnknownModRefBehavior,\n";
712 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
713 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
714 switch (Ints[i].ModRef) {
715 case CodeGenIntrinsic::NoMem:
716 OS << "DoesNotAccessMemory,\n";
718 case CodeGenIntrinsic::ReadArgMem:
719 OS << "OnlyReadsArgumentPointees,\n";
721 case CodeGenIntrinsic::ReadMem:
722 OS << "OnlyReadsMemory,\n";
724 case CodeGenIntrinsic::ReadWriteArgMem:
725 OS << "OnlyAccessesArgumentPointees,\n";
727 case CodeGenIntrinsic::ReadWriteMem:
728 OS << "UnknownModRefBehavior,\n";
733 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
734 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
737 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
738 /// same target, and we already checked it.
739 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
740 const std::string &TargetPrefix,
743 std::vector<StringMatcher::StringPair> Results;
745 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
746 E = BIM.end(); I != E; ++I) {
747 std::string ResultCode =
748 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
749 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
752 StringMatcher("BuiltinName", Results, OS).Emit();
756 void IntrinsicEmitter::
757 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
759 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
761 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
762 if (!Ints[i].GCCBuiltinName.empty()) {
763 // Get the map for this target prefix.
764 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
766 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
767 Ints[i].EnumName)).second)
768 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
769 "': duplicate GCC builtin name!");
773 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
774 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
775 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
776 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
777 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
780 OS << "static " << TargetPrefix << "Intrinsic::ID "
781 << "getIntrinsicForGCCBuiltin(const char "
782 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
784 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
785 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
788 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
789 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
791 // Note: this could emit significantly better code if we cared.
792 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
794 if (!I->first.empty())
795 OS << "if (TargetPrefix == \"" << I->first << "\") ";
797 OS << "/* Target Independent Builtins */ ";
800 // Emit the comparisons for this target prefix.
801 EmitTargetBuiltins(I->second, TargetPrefix, OS);
805 if (!TargetPrefix.empty())
806 OS << "(" << TargetPrefix << "Intrinsic::ID)";
807 OS << "Intrinsic::not_intrinsic;\n";
812 void IntrinsicEmitter::
813 EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
815 std::map<std::string, std::map<std::string, std::string>> TargetBuiltins;
817 for (const auto &Intrinsic : Ints) {
818 if (Intrinsic.MSBuiltinName.empty())
821 auto &Builtins = TargetBuiltins[Intrinsic.TargetPrefix];
822 if (!Builtins.insert(std::make_pair(Intrinsic.MSBuiltinName,
823 Intrinsic.EnumName)).second)
824 PrintFatalError("Intrinsic '" + Intrinsic.TheDef->getName() + "': "
825 "duplicate MS builtin name!");
828 OS << "// Get the LLVM intrinsic that corresponds to a MS builtin.\n"
829 "// This is used by the C front-end. The MS builtin name is passed\n"
830 "// in as a BuiltinName, and a target prefix (e.g. 'arm') is passed\n"
831 "// in as a TargetPrefix. The result is assigned to 'IntrinsicID'.\n"
832 "#ifdef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN\n";
834 OS << (TargetOnly ? "static " + TargetPrefix : "") << "Intrinsic::ID "
835 << (TargetOnly ? "" : "Intrinsic::")
836 << "getIntrinsicForMSBuiltin(const char *TP, const char *BN) {\n";
837 OS << " StringRef BuiltinName(BN);\n"
838 " StringRef TargetPrefix(TP);\n"
841 for (const auto &Builtins : TargetBuiltins) {
843 if (Builtins.first.empty())
844 OS << "/* Target Independent Builtins */ ";
846 OS << "if (TargetPrefix == \"" << Builtins.first << "\") ";
848 EmitTargetBuiltins(Builtins.second, TargetPrefix, OS);
853 if (!TargetPrefix.empty())
854 OS << "(" << TargetPrefix << "Intrinsic::ID)";
855 OS << "Intrinsic::not_intrinsic;\n";
861 void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
862 IntrinsicEmitter(RK, TargetOnly).run(OS);