1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend emits information about intrinsic functions.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenIntrinsics.h"
15 #include "CodeGenTarget.h"
16 #include "SequenceToOffsetTable.h"
17 #include "TableGenBackends.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/TableGen/Record.h"
21 #include "llvm/TableGen/StringMatcher.h"
22 #include "llvm/TableGen/TableGenBackend.h"
23 #include "llvm/TableGen/StringToOffsetTable.h"
28 class IntrinsicEmitter {
29 RecordKeeper &Records;
31 std::string TargetPrefix;
34 IntrinsicEmitter(RecordKeeper &R, bool T)
35 : Records(R), TargetOnly(T) {}
37 void run(raw_ostream &OS, bool Enums);
39 void EmitPrefix(raw_ostream &OS);
41 void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
42 void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
43 void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints,
45 void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
47 void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
48 void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
49 void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints, bool IsGCC,
51 void EmitSuffix(raw_ostream &OS);
53 } // End anonymous namespace
55 //===----------------------------------------------------------------------===//
56 // IntrinsicEmitter Implementation
57 //===----------------------------------------------------------------------===//
59 void IntrinsicEmitter::run(raw_ostream &OS, bool Enums) {
60 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
62 CodeGenIntrinsicTable Ints(Records, TargetOnly);
64 if (TargetOnly && !Ints.empty())
65 TargetPrefix = Ints[0].TargetPrefix;
70 // Emit the enum information.
71 EmitEnumInfo(Ints, OS);
73 // Emit the target metadata.
74 EmitTargetInfo(Ints, OS);
76 // Emit the intrinsic ID -> name table.
77 EmitIntrinsicToNameTable(Ints, OS);
79 // Emit the intrinsic ID -> overload table.
80 EmitIntrinsicToOverloadTable(Ints, OS);
82 // Emit the intrinsic declaration generator.
83 EmitGenerator(Ints, OS);
85 // Emit the intrinsic parameter attributes.
86 EmitAttributes(Ints, OS);
88 // Emit code to translate GCC builtins into LLVM intrinsics.
89 EmitIntrinsicToBuiltinMap(Ints, true, OS);
91 // Emit code to translate MS builtins into LLVM intrinsics.
92 EmitIntrinsicToBuiltinMap(Ints, false, OS);
98 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
99 OS << "// VisualStudio defines setjmp as _setjmp\n"
100 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
101 " !defined(setjmp_undefined_for_msvc)\n"
102 "# pragma push_macro(\"setjmp\")\n"
104 "# define setjmp_undefined_for_msvc\n"
108 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
109 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
110 "// let's return it to _setjmp state\n"
111 "# pragma pop_macro(\"setjmp\")\n"
112 "# undef setjmp_undefined_for_msvc\n"
116 void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
118 OS << "// Enum values for Intrinsics.h\n";
119 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
120 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
121 OS << " " << Ints[i].EnumName;
122 OS << ((i != e-1) ? ", " : " ");
123 if (Ints[i].EnumName.size() < 40)
124 OS << std::string(40-Ints[i].EnumName.size(), ' ');
125 OS << " // " << Ints[i].Name << "\n";
130 void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
132 OS << "// Target mapping\n";
133 OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n";
134 OS << "struct IntrinsicTargetInfo {\n"
135 << " llvm::StringLiteral Name;\n"
136 << " size_t Offset;\n"
137 << " size_t Count;\n"
139 OS << "static constexpr IntrinsicTargetInfo TargetInfos[] = {\n";
140 for (auto Target : Ints.Targets)
141 OS << " {llvm::StringLiteral(\"" << Target.Name << "\"), " << Target.Offset
142 << ", " << Target.Count << "},\n";
147 void IntrinsicEmitter::EmitIntrinsicToNameTable(
148 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
149 OS << "// Intrinsic ID to name table\n";
150 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
151 OS << " // Note that entry #0 is the invalid intrinsic!\n";
152 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
153 OS << " \"" << Ints[i].Name << "\",\n";
157 void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
158 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
159 OS << "// Intrinsic ID to overload bitset\n";
160 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
161 OS << "static const uint8_t OTable[] = {\n";
163 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
164 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
167 if (Ints[i].isOverloaded)
168 OS << " | (1<<" << (i+1)%8 << ')';
171 // OTable contains a true bit at the position if the intrinsic is overloaded.
172 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
177 // NOTE: This must be kept in synch with the copy in lib/IR/Function.cpp!
179 // Common values should be encoded with 0-15.
197 // Values from 16+ are only encodable with the inefficient encoding.
202 IIT_EMPTYSTRUCT = 20,
212 IIT_HALF_VEC_ARG = 30,
213 IIT_SAME_VEC_WIDTH_ARG = 31,
216 IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
226 static void EncodeFixedValueType(MVT::SimpleValueType VT,
227 std::vector<unsigned char> &Sig) {
228 if (MVT(VT).isInteger()) {
229 unsigned BitWidth = MVT(VT).getSizeInBits();
231 default: PrintFatalError("unhandled integer type width in intrinsic!");
232 case 1: return Sig.push_back(IIT_I1);
233 case 8: return Sig.push_back(IIT_I8);
234 case 16: return Sig.push_back(IIT_I16);
235 case 32: return Sig.push_back(IIT_I32);
236 case 64: return Sig.push_back(IIT_I64);
237 case 128: return Sig.push_back(IIT_I128);
242 default: PrintFatalError("unhandled MVT in intrinsic!");
243 case MVT::f16: return Sig.push_back(IIT_F16);
244 case MVT::f32: return Sig.push_back(IIT_F32);
245 case MVT::f64: return Sig.push_back(IIT_F64);
246 case MVT::f128: return Sig.push_back(IIT_F128);
247 case MVT::token: return Sig.push_back(IIT_TOKEN);
248 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
249 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
250 // MVT::OtherVT is used to mean the empty struct type here.
251 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
252 // MVT::isVoid is used to represent varargs here.
253 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
257 #if defined(_MSC_VER) && !defined(__clang__)
258 #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
261 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
262 std::vector<unsigned char> &Sig) {
264 if (R->isSubClassOf("LLVMMatchType")) {
265 unsigned Number = R->getValueAsInt("Number");
266 assert(Number < ArgCodes.size() && "Invalid matching number!");
267 if (R->isSubClassOf("LLVMExtendedType"))
268 Sig.push_back(IIT_EXTEND_ARG);
269 else if (R->isSubClassOf("LLVMTruncatedType"))
270 Sig.push_back(IIT_TRUNC_ARG);
271 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
272 Sig.push_back(IIT_HALF_VEC_ARG);
273 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
274 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
275 Sig.push_back((Number << 3) | ArgCodes[Number]);
276 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
277 EncodeFixedValueType(VT, Sig);
280 else if (R->isSubClassOf("LLVMPointerTo"))
281 Sig.push_back(IIT_PTR_TO_ARG);
282 else if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
283 Sig.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT);
284 unsigned ArgNo = ArgCodes.size();
285 ArgCodes.push_back(3 /*vAny*/);
286 // Encode overloaded ArgNo
287 Sig.push_back(ArgNo);
288 // Encode LLVMMatchType<Number> ArgNo
289 Sig.push_back(Number);
291 } else if (R->isSubClassOf("LLVMPointerToElt"))
292 Sig.push_back(IIT_PTR_TO_ELT);
294 Sig.push_back(IIT_ARG);
295 return Sig.push_back((Number << 3) | ArgCodes[Number]);
298 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
303 case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH;
304 case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH;
305 case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH;
306 case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH;
308 // If this is an "any" valuetype, then the type is the type of the next
309 // type in the list specified to getIntrinsic().
310 Sig.push_back(IIT_ARG);
312 // Figure out what arg # this is consuming, and remember what kind it was.
313 unsigned ArgNo = ArgCodes.size();
314 ArgCodes.push_back(Tmp);
316 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
317 return Sig.push_back((ArgNo << 3) | Tmp);
321 unsigned AddrSpace = 0;
322 if (R->isSubClassOf("LLVMQualPointerType")) {
323 AddrSpace = R->getValueAsInt("AddrSpace");
324 assert(AddrSpace < 256 && "Address space exceeds 255");
327 Sig.push_back(IIT_ANYPTR);
328 Sig.push_back(AddrSpace);
330 Sig.push_back(IIT_PTR);
332 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
336 if (MVT(VT).isVector()) {
338 switch (VVT.getVectorNumElements()) {
339 default: PrintFatalError("unhandled vector type width in intrinsic!");
340 case 1: Sig.push_back(IIT_V1); break;
341 case 2: Sig.push_back(IIT_V2); break;
342 case 4: Sig.push_back(IIT_V4); break;
343 case 8: Sig.push_back(IIT_V8); break;
344 case 16: Sig.push_back(IIT_V16); break;
345 case 32: Sig.push_back(IIT_V32); break;
346 case 64: Sig.push_back(IIT_V64); break;
347 case 512: Sig.push_back(IIT_V512); break;
348 case 1024: Sig.push_back(IIT_V1024); break;
351 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
354 EncodeFixedValueType(VT, Sig);
357 #if defined(_MSC_VER) && !defined(__clang__)
358 #pragma optimize("",on)
361 /// ComputeFixedEncoding - If we can encode the type signature for this
362 /// intrinsic into 32 bits, return it. If not, return ~0U.
363 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
364 std::vector<unsigned char> &TypeSig) {
365 std::vector<unsigned char> ArgCodes;
367 if (Int.IS.RetVTs.empty())
368 TypeSig.push_back(IIT_Done);
369 else if (Int.IS.RetVTs.size() == 1 &&
370 Int.IS.RetVTs[0] == MVT::isVoid)
371 TypeSig.push_back(IIT_Done);
373 switch (Int.IS.RetVTs.size()) {
375 case 2: TypeSig.push_back(IIT_STRUCT2); break;
376 case 3: TypeSig.push_back(IIT_STRUCT3); break;
377 case 4: TypeSig.push_back(IIT_STRUCT4); break;
378 case 5: TypeSig.push_back(IIT_STRUCT5); break;
379 case 6: TypeSig.push_back(IIT_STRUCT6); break;
380 case 7: TypeSig.push_back(IIT_STRUCT7); break;
381 case 8: TypeSig.push_back(IIT_STRUCT8); break;
382 default: llvm_unreachable("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 CodeGenIntrinsicTable &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" << Twine::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
480 struct AttributeComparator {
481 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
482 // Sort throwing intrinsics after non-throwing intrinsics.
483 if (L->canThrow != R->canThrow)
486 if (L->isNoDuplicate != R->isNoDuplicate)
487 return R->isNoDuplicate;
489 if (L->isNoReturn != R->isNoReturn)
490 return R->isNoReturn;
492 if (L->isCold != R->isCold)
495 if (L->isConvergent != R->isConvergent)
496 return R->isConvergent;
498 if (L->isSpeculatable != R->isSpeculatable)
499 return R->isSpeculatable;
501 if (L->hasSideEffects != R->hasSideEffects)
502 return R->hasSideEffects;
504 // Try to order by readonly/readnone attribute.
505 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
506 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
507 if (LK != RK) return (LK > RK);
509 // Order by argument attributes.
510 // This is reliable because each side is already sorted internally.
511 return (L->ArgumentAttributes < R->ArgumentAttributes);
514 } // End anonymous namespace
516 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
517 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
519 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
520 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
522 OS << "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix
523 << "Intrinsic::ID id) {\n";
525 OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
527 // Compute the maximum number of attribute arguments and the map
528 typedef std::map<const CodeGenIntrinsic*, unsigned,
529 AttributeComparator> UniqAttrMapTy;
530 UniqAttrMapTy UniqAttributes;
531 unsigned maxArgAttrs = 0;
532 unsigned AttrNum = 0;
533 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
534 const CodeGenIntrinsic &intrinsic = Ints[i];
536 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
537 unsigned &N = UniqAttributes[&intrinsic];
539 assert(AttrNum < 256 && "Too many unique attributes for table!");
543 // Emit an array of AttributeList. Most intrinsics will have at least one
544 // entry, for the function itself (index ~1), which is usually nounwind.
545 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
547 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
548 const CodeGenIntrinsic &intrinsic = Ints[i];
550 OS << " " << UniqAttributes[&intrinsic] << ", // "
551 << intrinsic.Name << "\n";
555 OS << " AttributeList AS[" << maxArgAttrs + 1 << "];\n";
556 OS << " unsigned NumAttrs = 0;\n";
557 OS << " if (id != 0) {\n";
558 OS << " switch(IntrinsicsToAttributesMap[id - ";
560 OS << "Intrinsic::num_intrinsics";
564 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
565 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
566 E = UniqAttributes.end(); I != E; ++I) {
567 OS << " case " << I->second << ": {\n";
569 const CodeGenIntrinsic &intrinsic = *(I->first);
571 // Keep track of the number of attributes we're writing out.
572 unsigned numAttrs = 0;
574 // The argument attributes are alreadys sorted by argument index.
575 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
578 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
579 unsigned attrIdx = argNo + 1; // Must match AttributeList::FirstArgIndex
581 OS << " const Attribute::AttrKind AttrParam" << attrIdx << "[]= {";
582 bool addComma = false;
585 switch (intrinsic.ArgumentAttributes[ai].second) {
586 case CodeGenIntrinsic::NoCapture:
589 OS << "Attribute::NoCapture";
592 case CodeGenIntrinsic::Returned:
595 OS << "Attribute::Returned";
598 case CodeGenIntrinsic::ReadOnly:
601 OS << "Attribute::ReadOnly";
604 case CodeGenIntrinsic::WriteOnly:
607 OS << "Attribute::WriteOnly";
610 case CodeGenIntrinsic::ReadNone:
613 OS << "Attribute::ReadNone";
619 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
621 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
622 << attrIdx << ", AttrParam" << attrIdx << ");\n";
626 if (!intrinsic.canThrow ||
627 intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
628 intrinsic.isNoReturn || intrinsic.isCold || intrinsic.isNoDuplicate ||
629 intrinsic.isConvergent || intrinsic.isSpeculatable) {
630 OS << " const Attribute::AttrKind Atts[] = {";
631 bool addComma = false;
632 if (!intrinsic.canThrow) {
633 OS << "Attribute::NoUnwind";
636 if (intrinsic.isNoReturn) {
639 OS << "Attribute::NoReturn";
642 if (intrinsic.isCold) {
645 OS << "Attribute::Cold";
648 if (intrinsic.isNoDuplicate) {
651 OS << "Attribute::NoDuplicate";
654 if (intrinsic.isConvergent) {
657 OS << "Attribute::Convergent";
660 if (intrinsic.isSpeculatable) {
663 OS << "Attribute::Speculatable";
667 switch (intrinsic.ModRef) {
668 case CodeGenIntrinsic::NoMem:
671 OS << "Attribute::ReadNone";
673 case CodeGenIntrinsic::ReadArgMem:
676 OS << "Attribute::ReadOnly,";
677 OS << "Attribute::ArgMemOnly";
679 case CodeGenIntrinsic::ReadMem:
682 OS << "Attribute::ReadOnly";
684 case CodeGenIntrinsic::ReadInaccessibleMem:
687 OS << "Attribute::ReadOnly,";
688 OS << "Attribute::InaccessibleMemOnly";
690 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem:
693 OS << "Attribute::ReadOnly,";
694 OS << "Attribute::InaccessibleMemOrArgMemOnly";
696 case CodeGenIntrinsic::WriteArgMem:
699 OS << "Attribute::WriteOnly,";
700 OS << "Attribute::ArgMemOnly";
702 case CodeGenIntrinsic::WriteMem:
705 OS << "Attribute::WriteOnly";
707 case CodeGenIntrinsic::WriteInaccessibleMem:
710 OS << "Attribute::WriteOnly,";
711 OS << "Attribute::InaccessibleMemOnly";
713 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem:
716 OS << "Attribute::WriteOnly,";
717 OS << "Attribute::InaccessibleMemOrArgMemOnly";
719 case CodeGenIntrinsic::ReadWriteArgMem:
722 OS << "Attribute::ArgMemOnly";
724 case CodeGenIntrinsic::ReadWriteInaccessibleMem:
727 OS << "Attribute::InaccessibleMemOnly";
729 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem:
732 OS << "Attribute::InaccessibleMemOrArgMemOnly";
734 case CodeGenIntrinsic::ReadWriteMem:
738 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
739 << "AttributeList::FunctionIndex, Atts);\n";
743 OS << " NumAttrs = " << numAttrs << ";\n";
747 OS << " return AttributeList();\n";
754 OS << " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n";
756 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
759 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
760 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
761 StringRef CompilerName = (IsGCC ? "GCC" : "MS");
762 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
764 StringToOffsetTable Table;
765 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
766 const std::string &BuiltinName =
767 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
768 if (!BuiltinName.empty()) {
769 // Get the map for this target prefix.
770 std::map<std::string, std::string> &BIM =
771 BuiltinMap[Ints[i].TargetPrefix];
773 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
774 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
775 "': duplicate " + CompilerName + " builtin name!");
776 Table.GetOrAddStringOffset(BuiltinName);
780 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
781 OS << "// This is used by the C front-end. The 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_" << CompilerName << "_BUILTIN\n";
787 OS << "static " << TargetPrefix << "Intrinsic::ID "
788 << "getIntrinsicFor" << CompilerName << "Builtin(const char "
789 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
791 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
792 << "Builtin(const char "
793 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
798 if (!TargetPrefix.empty())
799 OS << "(" << TargetPrefix << "Intrinsic::ID)";
800 OS << "Intrinsic::not_intrinsic;\n";
806 OS << " static const char BuiltinNames[] = {\n";
807 Table.EmitCharArray(OS);
810 OS << " struct BuiltinEntry {\n";
811 OS << " Intrinsic::ID IntrinID;\n";
812 OS << " unsigned StrTabOffset;\n";
813 OS << " const char *getName() const {\n";
814 OS << " return &BuiltinNames[StrTabOffset];\n";
816 OS << " bool operator<(StringRef RHS) const {\n";
817 OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
821 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
823 // Note: this could emit significantly better code if we cared.
824 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
826 if (!I->first.empty())
827 OS << "if (TargetPrefix == \"" << I->first << "\") ";
829 OS << "/* Target Independent Builtins */ ";
832 // Emit the comparisons for this target prefix.
833 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n";
834 for (const auto &P : I->second) {
835 OS << " {Intrinsic::" << P.second << ", "
836 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
839 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
840 OS << " std::end(" << I->first << "Names),\n";
841 OS << " BuiltinNameStr);\n";
842 OS << " if (I != std::end(" << I->first << "Names) &&\n";
843 OS << " I->getName() == BuiltinNameStr)\n";
844 OS << " return I->IntrinID;\n";
848 if (!TargetPrefix.empty())
849 OS << "(" << TargetPrefix << "Intrinsic::ID)";
850 OS << "Intrinsic::not_intrinsic;\n";
855 void llvm::EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS,
857 IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/true);
860 void llvm::EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS,
862 IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/false);