• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tablegen backend emits information about intrinsic functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
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"
24 #include <algorithm>
25 using namespace llvm;
26 
27 namespace {
28 class IntrinsicEmitter {
29   RecordKeeper &Records;
30   bool TargetOnly;
31   std::string TargetPrefix;
32 
33 public:
IntrinsicEmitter(RecordKeeper & R,bool T)34   IntrinsicEmitter(RecordKeeper &R, bool T)
35     : Records(R), TargetOnly(T) {}
36 
37   void run(raw_ostream &OS, bool Enums);
38 
39   void EmitPrefix(raw_ostream &OS);
40 
41   void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
42   void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
43   void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints,
44                                 raw_ostream &OS);
45   void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
46                                     raw_ostream &OS);
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,
50                                  raw_ostream &OS);
51   void EmitSuffix(raw_ostream &OS);
52 };
53 } // End anonymous namespace
54 
55 //===----------------------------------------------------------------------===//
56 // IntrinsicEmitter Implementation
57 //===----------------------------------------------------------------------===//
58 
run(raw_ostream & OS,bool Enums)59 void IntrinsicEmitter::run(raw_ostream &OS, bool Enums) {
60   emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
61 
62   CodeGenIntrinsicTable Ints(Records, TargetOnly);
63 
64   if (TargetOnly && !Ints.empty())
65     TargetPrefix = Ints[0].TargetPrefix;
66 
67   EmitPrefix(OS);
68 
69   if (Enums) {
70     // Emit the enum information.
71     EmitEnumInfo(Ints, OS);
72   } else {
73     // Emit the target metadata.
74     EmitTargetInfo(Ints, OS);
75 
76     // Emit the intrinsic ID -> name table.
77     EmitIntrinsicToNameTable(Ints, OS);
78 
79     // Emit the intrinsic ID -> overload table.
80     EmitIntrinsicToOverloadTable(Ints, OS);
81 
82     // Emit the intrinsic declaration generator.
83     EmitGenerator(Ints, OS);
84 
85     // Emit the intrinsic parameter attributes.
86     EmitAttributes(Ints, OS);
87 
88     // Emit code to translate GCC builtins into LLVM intrinsics.
89     EmitIntrinsicToBuiltinMap(Ints, true, OS);
90 
91     // Emit code to translate MS builtins into LLVM intrinsics.
92     EmitIntrinsicToBuiltinMap(Ints, false, OS);
93   }
94 
95   EmitSuffix(OS);
96 }
97 
EmitPrefix(raw_ostream & 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"
103         "#  undef setjmp\n"
104         "#  define setjmp_undefined_for_msvc\n"
105         "#endif\n\n";
106 }
107 
EmitSuffix(raw_ostream & OS)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"
113         "#endif\n\n";
114 }
115 
EmitEnumInfo(const CodeGenIntrinsicTable & Ints,raw_ostream & OS)116 void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
117                                     raw_ostream &OS) {
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";
126   }
127   OS << "#endif\n\n";
128 }
129 
EmitTargetInfo(const CodeGenIntrinsicTable & Ints,raw_ostream & OS)130 void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
131                                     raw_ostream &OS) {
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"
138      << "};\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";
143   OS << "};\n";
144   OS << "#endif\n\n";
145 }
146 
EmitIntrinsicToNameTable(const CodeGenIntrinsicTable & Ints,raw_ostream & OS)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";
154   OS << "#endif\n\n";
155 }
156 
EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable & Ints,raw_ostream & OS)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";
162   OS << "  0";
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.
165     if ((i+1)%8 == 0)
166       OS << ",\n  0";
167     if (Ints[i].isOverloaded)
168       OS << " | (1<<" << (i+1)%8 << ')';
169   }
170   OS << "\n};\n\n";
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";
173   OS << "#endif\n\n";
174 }
175 
176 
177 // NOTE: This must be kept in synch with the copy in lib/IR/Function.cpp!
178 enum IIT_Info {
179   // Common values should be encoded with 0-15.
180   IIT_Done = 0,
181   IIT_I1   = 1,
182   IIT_I8   = 2,
183   IIT_I16  = 3,
184   IIT_I32  = 4,
185   IIT_I64  = 5,
186   IIT_F16  = 6,
187   IIT_F32  = 7,
188   IIT_F64  = 8,
189   IIT_V2   = 9,
190   IIT_V4   = 10,
191   IIT_V8   = 11,
192   IIT_V16  = 12,
193   IIT_V32  = 13,
194   IIT_PTR  = 14,
195   IIT_ARG  = 15,
196 
197   // Values from 16+ are only encodable with the inefficient encoding.
198   IIT_V64  = 16,
199   IIT_MMX  = 17,
200   IIT_TOKEN = 18,
201   IIT_METADATA = 19,
202   IIT_EMPTYSTRUCT = 20,
203   IIT_STRUCT2 = 21,
204   IIT_STRUCT3 = 22,
205   IIT_STRUCT4 = 23,
206   IIT_STRUCT5 = 24,
207   IIT_EXTEND_ARG = 25,
208   IIT_TRUNC_ARG = 26,
209   IIT_ANYPTR = 27,
210   IIT_V1   = 28,
211   IIT_VARARG = 29,
212   IIT_HALF_VEC_ARG = 30,
213   IIT_SAME_VEC_WIDTH_ARG = 31,
214   IIT_PTR_TO_ARG = 32,
215   IIT_PTR_TO_ELT = 33,
216   IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
217   IIT_I128 = 35,
218   IIT_V512 = 36,
219   IIT_V1024 = 37,
220   IIT_STRUCT6 = 38,
221   IIT_STRUCT7 = 39,
222   IIT_STRUCT8 = 40,
223   IIT_F128 = 41
224 };
225 
EncodeFixedValueType(MVT::SimpleValueType VT,std::vector<unsigned char> & Sig)226 static void EncodeFixedValueType(MVT::SimpleValueType VT,
227                                  std::vector<unsigned char> &Sig) {
228   if (MVT(VT).isInteger()) {
229     unsigned BitWidth = MVT(VT).getSizeInBits();
230     switch (BitWidth) {
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);
238     }
239   }
240 
241   switch (VT) {
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);
254   }
255 }
256 
257 #if defined(_MSC_VER) && !defined(__clang__)
258 #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
259 #endif
260 
EncodeFixedType(Record * R,std::vector<unsigned char> & ArgCodes,std::vector<unsigned char> & Sig)261 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
262                             std::vector<unsigned char> &Sig) {
263 
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);
278       return;
279     }
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);
290       return;
291     } else if (R->isSubClassOf("LLVMPointerToElt"))
292       Sig.push_back(IIT_PTR_TO_ELT);
293     else
294       Sig.push_back(IIT_ARG);
295     return Sig.push_back((Number << 3) | ArgCodes[Number]);
296   }
297 
298   MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
299 
300   unsigned Tmp = 0;
301   switch (VT) {
302   default: break;
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;
307   case MVT::Any: {
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);
311 
312     // Figure out what arg # this is consuming, and remember what kind it was.
313     unsigned ArgNo = ArgCodes.size();
314     ArgCodes.push_back(Tmp);
315 
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);
318   }
319 
320   case MVT::iPTR: {
321     unsigned AddrSpace = 0;
322     if (R->isSubClassOf("LLVMQualPointerType")) {
323       AddrSpace = R->getValueAsInt("AddrSpace");
324       assert(AddrSpace < 256 && "Address space exceeds 255");
325     }
326     if (AddrSpace) {
327       Sig.push_back(IIT_ANYPTR);
328       Sig.push_back(AddrSpace);
329     } else {
330       Sig.push_back(IIT_PTR);
331     }
332     return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
333   }
334   }
335 
336   if (MVT(VT).isVector()) {
337     MVT VVT = VT;
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;
349     }
350 
351     return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
352   }
353 
354   EncodeFixedValueType(VT, Sig);
355 }
356 
357 #if defined(_MSC_VER) && !defined(__clang__)
358 #pragma optimize("",on)
359 #endif
360 
361 /// ComputeFixedEncoding - If we can encode the type signature for this
362 /// intrinsic into 32 bits, return it.  If not, return ~0U.
ComputeFixedEncoding(const CodeGenIntrinsic & Int,std::vector<unsigned char> & TypeSig)363 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
364                                  std::vector<unsigned char> &TypeSig) {
365   std::vector<unsigned char> ArgCodes;
366 
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);
372   else {
373     switch (Int.IS.RetVTs.size()) {
374       case 1: break;
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");
383     }
384 
385     for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
386       EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
387   }
388 
389   for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
390     EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
391 }
392 
printIITEntry(raw_ostream & OS,unsigned char X)393 static void printIITEntry(raw_ostream &OS, unsigned char X) {
394   OS << (unsigned)X;
395 }
396 
EmitGenerator(const CodeGenIntrinsicTable & Ints,raw_ostream & OS)397 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
398                                      raw_ostream &OS) {
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;
402 
403   SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
404 
405   std::vector<unsigned char> TypeSig;
406 
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.
410     TypeSig.clear();
411     ComputeFixedEncoding(Ints[i], TypeSig);
412 
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) {
416       bool Failed = false;
417       unsigned Result = 0;
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) {
421           Failed = true;
422           break;
423         }
424         Result = (Result << 4) | TypeSig[e-i-1];
425       }
426 
427       // If this could be encoded into a 31-bit word, return it.
428       if (!Failed && (Result >> 31) == 0) {
429         FixedEncodings.push_back(Result);
430         continue;
431       }
432     }
433 
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);
437 
438     // This is a placehold that we'll replace after the table is laid out.
439     FixedEncodings.push_back(~0U);
440   }
441 
442   LongEncodingTable.layout();
443 
444   OS << "// Global intrinsic function declaration type table.\n";
445   OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
446 
447   OS << "static const unsigned IIT_Table[] = {\n  ";
448 
449   for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
450     if ((i & 7) == 7)
451       OS << "\n  ";
452 
453     // If the entry fit in the table, just emit it.
454     if (FixedEncodings[i] != ~0U) {
455       OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", ";
456       continue;
457     }
458 
459     TypeSig.clear();
460     ComputeFixedEncoding(Ints[i], TypeSig);
461 
462 
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) << ", ";
466   }
467 
468   OS << "0\n};\n\n";
469 
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";
475 
476   OS << "#endif\n\n";  // End of GET_INTRINSIC_GENERATOR_GLOBAL
477 }
478 
479 namespace {
480 struct AttributeComparator {
operator ()__anond01cb58b0211::AttributeComparator481   bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
482     // Sort throwing intrinsics after non-throwing intrinsics.
483     if (L->canThrow != R->canThrow)
484       return R->canThrow;
485 
486     if (L->isNoDuplicate != R->isNoDuplicate)
487       return R->isNoDuplicate;
488 
489     if (L->isNoReturn != R->isNoReturn)
490       return R->isNoReturn;
491 
492     if (L->isConvergent != R->isConvergent)
493       return R->isConvergent;
494 
495     if (L->isSpeculatable != R->isSpeculatable)
496       return R->isSpeculatable;
497 
498     if (L->hasSideEffects != R->hasSideEffects)
499       return R->hasSideEffects;
500 
501     // Try to order by readonly/readnone attribute.
502     CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
503     CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
504     if (LK != RK) return (LK > RK);
505 
506     // Order by argument attributes.
507     // This is reliable because each side is already sorted internally.
508     return (L->ArgumentAttributes < R->ArgumentAttributes);
509   }
510 };
511 } // End anonymous namespace
512 
513 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
EmitAttributes(const CodeGenIntrinsicTable & Ints,raw_ostream & OS)514 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
515                                       raw_ostream &OS) {
516   OS << "// Add parameter attributes that are not common to all intrinsics.\n";
517   OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
518   if (TargetOnly)
519     OS << "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix
520        << "Intrinsic::ID id) {\n";
521   else
522     OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
523 
524   // Compute the maximum number of attribute arguments and the map
525   typedef std::map<const CodeGenIntrinsic*, unsigned,
526                    AttributeComparator> UniqAttrMapTy;
527   UniqAttrMapTy UniqAttributes;
528   unsigned maxArgAttrs = 0;
529   unsigned AttrNum = 0;
530   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
531     const CodeGenIntrinsic &intrinsic = Ints[i];
532     maxArgAttrs =
533       std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
534     unsigned &N = UniqAttributes[&intrinsic];
535     if (N) continue;
536     assert(AttrNum < 256 && "Too many unique attributes for table!");
537     N = ++AttrNum;
538   }
539 
540   // Emit an array of AttributeList.  Most intrinsics will have at least one
541   // entry, for the function itself (index ~1), which is usually nounwind.
542   OS << "  static const uint8_t IntrinsicsToAttributesMap[] = {\n";
543 
544   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
545     const CodeGenIntrinsic &intrinsic = Ints[i];
546 
547     OS << "    " << UniqAttributes[&intrinsic] << ", // "
548        << intrinsic.Name << "\n";
549   }
550   OS << "  };\n\n";
551 
552   OS << "  AttributeList AS[" << maxArgAttrs + 1 << "];\n";
553   OS << "  unsigned NumAttrs = 0;\n";
554   OS << "  if (id != 0) {\n";
555   OS << "    switch(IntrinsicsToAttributesMap[id - ";
556   if (TargetOnly)
557     OS << "Intrinsic::num_intrinsics";
558   else
559     OS << "1";
560   OS << "]) {\n";
561   OS << "    default: llvm_unreachable(\"Invalid attribute number\");\n";
562   for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
563        E = UniqAttributes.end(); I != E; ++I) {
564     OS << "    case " << I->second << ": {\n";
565 
566     const CodeGenIntrinsic &intrinsic = *(I->first);
567 
568     // Keep track of the number of attributes we're writing out.
569     unsigned numAttrs = 0;
570 
571     // The argument attributes are alreadys sorted by argument index.
572     unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
573     if (ae) {
574       while (ai != ae) {
575         unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
576         unsigned attrIdx = argNo + 1; // Must match AttributeList::FirstArgIndex
577 
578         OS << "      const Attribute::AttrKind AttrParam" << attrIdx << "[]= {";
579         bool addComma = false;
580 
581         do {
582           switch (intrinsic.ArgumentAttributes[ai].second) {
583           case CodeGenIntrinsic::NoCapture:
584             if (addComma)
585               OS << ",";
586             OS << "Attribute::NoCapture";
587             addComma = true;
588             break;
589           case CodeGenIntrinsic::Returned:
590             if (addComma)
591               OS << ",";
592             OS << "Attribute::Returned";
593             addComma = true;
594             break;
595           case CodeGenIntrinsic::ReadOnly:
596             if (addComma)
597               OS << ",";
598             OS << "Attribute::ReadOnly";
599             addComma = true;
600             break;
601           case CodeGenIntrinsic::WriteOnly:
602             if (addComma)
603               OS << ",";
604             OS << "Attribute::WriteOnly";
605             addComma = true;
606             break;
607           case CodeGenIntrinsic::ReadNone:
608             if (addComma)
609               OS << ",";
610             OS << "Attribute::ReadNone";
611             addComma = true;
612             break;
613           }
614 
615           ++ai;
616         } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
617         OS << "};\n";
618         OS << "      AS[" << numAttrs++ << "] = AttributeList::get(C, "
619            << attrIdx << ", AttrParam" << attrIdx << ");\n";
620       }
621     }
622 
623     if (!intrinsic.canThrow ||
624         intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
625         intrinsic.isNoReturn || intrinsic.isNoDuplicate ||
626         intrinsic.isConvergent || intrinsic.isSpeculatable) {
627       OS << "      const Attribute::AttrKind Atts[] = {";
628       bool addComma = false;
629       if (!intrinsic.canThrow) {
630         OS << "Attribute::NoUnwind";
631         addComma = true;
632       }
633       if (intrinsic.isNoReturn) {
634         if (addComma)
635           OS << ",";
636         OS << "Attribute::NoReturn";
637         addComma = true;
638       }
639       if (intrinsic.isNoDuplicate) {
640         if (addComma)
641           OS << ",";
642         OS << "Attribute::NoDuplicate";
643         addComma = true;
644       }
645       if (intrinsic.isConvergent) {
646         if (addComma)
647           OS << ",";
648         OS << "Attribute::Convergent";
649         addComma = true;
650       }
651       if (intrinsic.isSpeculatable) {
652         if (addComma)
653           OS << ",";
654         OS << "Attribute::Speculatable";
655         addComma = true;
656       }
657 
658       switch (intrinsic.ModRef) {
659       case CodeGenIntrinsic::NoMem:
660         if (addComma)
661           OS << ",";
662         OS << "Attribute::ReadNone";
663         break;
664       case CodeGenIntrinsic::ReadArgMem:
665         if (addComma)
666           OS << ",";
667         OS << "Attribute::ReadOnly,";
668         OS << "Attribute::ArgMemOnly";
669         break;
670       case CodeGenIntrinsic::ReadMem:
671         if (addComma)
672           OS << ",";
673         OS << "Attribute::ReadOnly";
674         break;
675       case CodeGenIntrinsic::ReadInaccessibleMem:
676         if (addComma)
677           OS << ",";
678         OS << "Attribute::ReadOnly,";
679         OS << "Attribute::InaccessibleMemOnly";
680         break;
681       case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem:
682         if (addComma)
683           OS << ",";
684         OS << "Attribute::ReadOnly,";
685         OS << "Attribute::InaccessibleMemOrArgMemOnly";
686         break;
687       case CodeGenIntrinsic::WriteArgMem:
688         if (addComma)
689           OS << ",";
690         OS << "Attribute::WriteOnly,";
691         OS << "Attribute::ArgMemOnly";
692         break;
693       case CodeGenIntrinsic::WriteMem:
694         if (addComma)
695           OS << ",";
696         OS << "Attribute::WriteOnly";
697         break;
698       case CodeGenIntrinsic::WriteInaccessibleMem:
699         if (addComma)
700           OS << ",";
701         OS << "Attribute::WriteOnly,";
702         OS << "Attribute::InaccessibleMemOnly";
703         break;
704       case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem:
705         if (addComma)
706           OS << ",";
707         OS << "Attribute::WriteOnly,";
708         OS << "Attribute::InaccessibleMemOrArgMemOnly";
709         break;
710       case CodeGenIntrinsic::ReadWriteArgMem:
711         if (addComma)
712           OS << ",";
713         OS << "Attribute::ArgMemOnly";
714         break;
715       case CodeGenIntrinsic::ReadWriteInaccessibleMem:
716         if (addComma)
717           OS << ",";
718         OS << "Attribute::InaccessibleMemOnly";
719         break;
720       case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem:
721         if (addComma)
722           OS << ",";
723         OS << "Attribute::InaccessibleMemOrArgMemOnly";
724         break;
725       case CodeGenIntrinsic::ReadWriteMem:
726         break;
727       }
728       OS << "};\n";
729       OS << "      AS[" << numAttrs++ << "] = AttributeList::get(C, "
730          << "AttributeList::FunctionIndex, Atts);\n";
731     }
732 
733     if (numAttrs) {
734       OS << "      NumAttrs = " << numAttrs << ";\n";
735       OS << "      break;\n";
736       OS << "      }\n";
737     } else {
738       OS << "      return AttributeList();\n";
739       OS << "      }\n";
740     }
741   }
742 
743   OS << "    }\n";
744   OS << "  }\n";
745   OS << "  return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n";
746   OS << "}\n";
747   OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
748 }
749 
EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable & Ints,bool IsGCC,raw_ostream & OS)750 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
751     const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
752   StringRef CompilerName = (IsGCC ? "GCC" : "MS");
753   typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
754   BIMTy BuiltinMap;
755   StringToOffsetTable Table;
756   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
757     const std::string &BuiltinName =
758         IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
759     if (!BuiltinName.empty()) {
760       // Get the map for this target prefix.
761       std::map<std::string, std::string> &BIM =
762           BuiltinMap[Ints[i].TargetPrefix];
763 
764       if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
765         PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
766                         "': duplicate " + CompilerName + " builtin name!");
767       Table.GetOrAddStringOffset(BuiltinName);
768     }
769   }
770 
771   OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
772   OS << "// This is used by the C front-end.  The builtin name is passed\n";
773   OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
774   OS << "// in as TargetPrefix.  The result is assigned to 'IntrinsicID'.\n";
775   OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
776 
777   if (TargetOnly) {
778     OS << "static " << TargetPrefix << "Intrinsic::ID "
779        << "getIntrinsicFor" << CompilerName << "Builtin(const char "
780        << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
781   } else {
782     OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
783        << "Builtin(const char "
784        << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
785   }
786 
787   if (Table.Empty()) {
788     OS << "  return ";
789     if (!TargetPrefix.empty())
790       OS << "(" << TargetPrefix << "Intrinsic::ID)";
791     OS << "Intrinsic::not_intrinsic;\n";
792     OS << "}\n";
793     OS << "#endif\n\n";
794     return;
795   }
796 
797   OS << "  static const char BuiltinNames[] = {\n";
798   Table.EmitCharArray(OS);
799   OS << "  };\n\n";
800 
801   OS << "  struct BuiltinEntry {\n";
802   OS << "    Intrinsic::ID IntrinID;\n";
803   OS << "    unsigned StrTabOffset;\n";
804   OS << "    const char *getName() const {\n";
805   OS << "      return &BuiltinNames[StrTabOffset];\n";
806   OS << "    }\n";
807   OS << "    bool operator<(StringRef RHS) const {\n";
808   OS << "      return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
809   OS << "    }\n";
810   OS << "  };\n";
811 
812   OS << "  StringRef TargetPrefix(TargetPrefixStr);\n\n";
813 
814   // Note: this could emit significantly better code if we cared.
815   for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
816     OS << "  ";
817     if (!I->first.empty())
818       OS << "if (TargetPrefix == \"" << I->first << "\") ";
819     else
820       OS << "/* Target Independent Builtins */ ";
821     OS << "{\n";
822 
823     // Emit the comparisons for this target prefix.
824     OS << "    static const BuiltinEntry " << I->first << "Names[] = {\n";
825     for (const auto &P : I->second) {
826       OS << "      {Intrinsic::" << P.second << ", "
827          << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
828     }
829     OS << "    };\n";
830     OS << "    auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
831     OS << "                              std::end(" << I->first << "Names),\n";
832     OS << "                              BuiltinNameStr);\n";
833     OS << "    if (I != std::end(" << I->first << "Names) &&\n";
834     OS << "        I->getName() == BuiltinNameStr)\n";
835     OS << "      return I->IntrinID;\n";
836     OS << "  }\n";
837   }
838   OS << "  return ";
839   if (!TargetPrefix.empty())
840     OS << "(" << TargetPrefix << "Intrinsic::ID)";
841   OS << "Intrinsic::not_intrinsic;\n";
842   OS << "}\n";
843   OS << "#endif\n\n";
844 }
845 
EmitIntrinsicEnums(RecordKeeper & RK,raw_ostream & OS,bool TargetOnly)846 void llvm::EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS,
847                               bool TargetOnly) {
848   IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/true);
849 }
850 
EmitIntrinsicImpl(RecordKeeper & RK,raw_ostream & OS,bool TargetOnly)851 void llvm::EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS,
852                              bool TargetOnly) {
853   IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/false);
854 }
855