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:
34   IntrinsicEmitter(RecordKeeper &R, bool T)
35     : Records(R), TargetOnly(T) {}
36 
37   void run(raw_ostream &OS);
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 
59 void IntrinsicEmitter::run(raw_ostream &OS) {
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   // Emit the enum information.
70   EmitEnumInfo(Ints, OS);
71 
72   // Emit the target metadata.
73   EmitTargetInfo(Ints, OS);
74 
75   // Emit the intrinsic ID -> name table.
76   EmitIntrinsicToNameTable(Ints, OS);
77 
78   // Emit the intrinsic ID -> overload table.
79   EmitIntrinsicToOverloadTable(Ints, OS);
80 
81   // Emit the intrinsic declaration generator.
82   EmitGenerator(Ints, OS);
83 
84   // Emit the intrinsic parameter attributes.
85   EmitAttributes(Ints, OS);
86 
87   // Emit code to translate GCC builtins into LLVM intrinsics.
88   EmitIntrinsicToBuiltinMap(Ints, true, OS);
89 
90   // Emit code to translate MS builtins into LLVM intrinsics.
91   EmitIntrinsicToBuiltinMap(Ints, false, OS);
92 
93   EmitSuffix(OS);
94 }
95 
96 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
97   OS << "// VisualStudio defines setjmp as _setjmp\n"
98         "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
99         "                         !defined(setjmp_undefined_for_msvc)\n"
100         "#  pragma push_macro(\"setjmp\")\n"
101         "#  undef setjmp\n"
102         "#  define setjmp_undefined_for_msvc\n"
103         "#endif\n\n";
104 }
105 
106 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
107   OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
108         "// let's return it to _setjmp state\n"
109         "#  pragma pop_macro(\"setjmp\")\n"
110         "#  undef setjmp_undefined_for_msvc\n"
111         "#endif\n\n";
112 }
113 
114 void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
115                                     raw_ostream &OS) {
116   OS << "// Enum values for Intrinsics.h\n";
117   OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
118   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
119     OS << "    " << Ints[i].EnumName;
120     OS << ((i != e-1) ? ", " : "  ");
121     if (Ints[i].EnumName.size() < 40)
122       OS << std::string(40-Ints[i].EnumName.size(), ' ');
123     OS << " // " << Ints[i].Name << "\n";
124   }
125   OS << "#endif\n\n";
126 }
127 
128 void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
129                                     raw_ostream &OS) {
130   OS << "// Target mapping\n";
131   OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n";
132   OS << "struct IntrinsicTargetInfo {\n"
133      << "  llvm::StringLiteral Name;\n"
134      << "  size_t Offset;\n"
135      << "  size_t Count;\n"
136      << "};\n";
137   OS << "static constexpr IntrinsicTargetInfo TargetInfos[] = {\n";
138   for (auto Target : Ints.Targets)
139     OS << "  {llvm::StringLiteral(\"" << Target.Name << "\"), " << Target.Offset
140        << ", " << Target.Count << "},\n";
141   OS << "};\n";
142   OS << "#endif\n\n";
143 }
144 
145 void IntrinsicEmitter::EmitIntrinsicToNameTable(
146     const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
147   OS << "// Intrinsic ID to name table\n";
148   OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
149   OS << "  // Note that entry #0 is the invalid intrinsic!\n";
150   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
151     OS << "  \"" << Ints[i].Name << "\",\n";
152   OS << "#endif\n\n";
153 }
154 
155 void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
156     const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
157   OS << "// Intrinsic ID to overload bitset\n";
158   OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
159   OS << "static const uint8_t OTable[] = {\n";
160   OS << "  0";
161   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
162     // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
163     if ((i+1)%8 == 0)
164       OS << ",\n  0";
165     if (Ints[i].isOverloaded)
166       OS << " | (1<<" << (i+1)%8 << ')';
167   }
168   OS << "\n};\n\n";
169   // OTable contains a true bit at the position if the intrinsic is overloaded.
170   OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
171   OS << "#endif\n\n";
172 }
173 
174 
175 // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
176 enum IIT_Info {
177   // Common values should be encoded with 0-15.
178   IIT_Done = 0,
179   IIT_I1   = 1,
180   IIT_I8   = 2,
181   IIT_I16  = 3,
182   IIT_I32  = 4,
183   IIT_I64  = 5,
184   IIT_F16  = 6,
185   IIT_F32  = 7,
186   IIT_F64  = 8,
187   IIT_V2   = 9,
188   IIT_V4   = 10,
189   IIT_V8   = 11,
190   IIT_V16  = 12,
191   IIT_V32  = 13,
192   IIT_PTR  = 14,
193   IIT_ARG  = 15,
194 
195   // Values from 16+ are only encodable with the inefficient encoding.
196   IIT_V64  = 16,
197   IIT_MMX  = 17,
198   IIT_TOKEN = 18,
199   IIT_METADATA = 19,
200   IIT_EMPTYSTRUCT = 20,
201   IIT_STRUCT2 = 21,
202   IIT_STRUCT3 = 22,
203   IIT_STRUCT4 = 23,
204   IIT_STRUCT5 = 24,
205   IIT_EXTEND_ARG = 25,
206   IIT_TRUNC_ARG = 26,
207   IIT_ANYPTR = 27,
208   IIT_V1   = 28,
209   IIT_VARARG = 29,
210   IIT_HALF_VEC_ARG = 30,
211   IIT_SAME_VEC_WIDTH_ARG = 31,
212   IIT_PTR_TO_ARG = 32,
213   IIT_PTR_TO_ELT = 33,
214   IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
215   IIT_I128 = 35,
216   IIT_V512 = 36,
217   IIT_V1024 = 37
218 };
219 
220 static void EncodeFixedValueType(MVT::SimpleValueType VT,
221                                  std::vector<unsigned char> &Sig) {
222   if (MVT(VT).isInteger()) {
223     unsigned BitWidth = MVT(VT).getSizeInBits();
224     switch (BitWidth) {
225     default: PrintFatalError("unhandled integer type width in intrinsic!");
226     case 1: return Sig.push_back(IIT_I1);
227     case 8: return Sig.push_back(IIT_I8);
228     case 16: return Sig.push_back(IIT_I16);
229     case 32: return Sig.push_back(IIT_I32);
230     case 64: return Sig.push_back(IIT_I64);
231     case 128: return Sig.push_back(IIT_I128);
232     }
233   }
234 
235   switch (VT) {
236   default: PrintFatalError("unhandled MVT in intrinsic!");
237   case MVT::f16: return Sig.push_back(IIT_F16);
238   case MVT::f32: return Sig.push_back(IIT_F32);
239   case MVT::f64: return Sig.push_back(IIT_F64);
240   case MVT::token: return Sig.push_back(IIT_TOKEN);
241   case MVT::Metadata: return Sig.push_back(IIT_METADATA);
242   case MVT::x86mmx: return Sig.push_back(IIT_MMX);
243   // MVT::OtherVT is used to mean the empty struct type here.
244   case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
245   // MVT::isVoid is used to represent varargs here.
246   case MVT::isVoid: return Sig.push_back(IIT_VARARG);
247   }
248 }
249 
250 #if defined(_MSC_VER) && !defined(__clang__)
251 #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
252 #endif
253 
254 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
255                             std::vector<unsigned char> &Sig) {
256 
257   if (R->isSubClassOf("LLVMMatchType")) {
258     unsigned Number = R->getValueAsInt("Number");
259     assert(Number < ArgCodes.size() && "Invalid matching number!");
260     if (R->isSubClassOf("LLVMExtendedType"))
261       Sig.push_back(IIT_EXTEND_ARG);
262     else if (R->isSubClassOf("LLVMTruncatedType"))
263       Sig.push_back(IIT_TRUNC_ARG);
264     else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
265       Sig.push_back(IIT_HALF_VEC_ARG);
266     else if (R->isSubClassOf("LLVMVectorSameWidth")) {
267       Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
268       Sig.push_back((Number << 3) | ArgCodes[Number]);
269       MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
270       EncodeFixedValueType(VT, Sig);
271       return;
272     }
273     else if (R->isSubClassOf("LLVMPointerTo"))
274       Sig.push_back(IIT_PTR_TO_ARG);
275     else if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
276       Sig.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT);
277       unsigned ArgNo = ArgCodes.size();
278       ArgCodes.push_back(3 /*vAny*/);
279       // Encode overloaded ArgNo
280       Sig.push_back(ArgNo);
281       // Encode LLVMMatchType<Number> ArgNo
282       Sig.push_back(Number);
283       return;
284     } else if (R->isSubClassOf("LLVMPointerToElt"))
285       Sig.push_back(IIT_PTR_TO_ELT);
286     else
287       Sig.push_back(IIT_ARG);
288     return Sig.push_back((Number << 3) | ArgCodes[Number]);
289   }
290 
291   MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
292 
293   unsigned Tmp = 0;
294   switch (VT) {
295   default: break;
296   case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH;
297   case MVT::vAny: ++Tmp;    LLVM_FALLTHROUGH;
298   case MVT::fAny: ++Tmp;    LLVM_FALLTHROUGH;
299   case MVT::iAny: ++Tmp;    LLVM_FALLTHROUGH;
300   case MVT::Any: {
301     // If this is an "any" valuetype, then the type is the type of the next
302     // type in the list specified to getIntrinsic().
303     Sig.push_back(IIT_ARG);
304 
305     // Figure out what arg # this is consuming, and remember what kind it was.
306     unsigned ArgNo = ArgCodes.size();
307     ArgCodes.push_back(Tmp);
308 
309     // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
310     return Sig.push_back((ArgNo << 3) | Tmp);
311   }
312 
313   case MVT::iPTR: {
314     unsigned AddrSpace = 0;
315     if (R->isSubClassOf("LLVMQualPointerType")) {
316       AddrSpace = R->getValueAsInt("AddrSpace");
317       assert(AddrSpace < 256 && "Address space exceeds 255");
318     }
319     if (AddrSpace) {
320       Sig.push_back(IIT_ANYPTR);
321       Sig.push_back(AddrSpace);
322     } else {
323       Sig.push_back(IIT_PTR);
324     }
325     return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
326   }
327   }
328 
329   if (MVT(VT).isVector()) {
330     MVT VVT = VT;
331     switch (VVT.getVectorNumElements()) {
332     default: PrintFatalError("unhandled vector type width in intrinsic!");
333     case 1: Sig.push_back(IIT_V1); break;
334     case 2: Sig.push_back(IIT_V2); break;
335     case 4: Sig.push_back(IIT_V4); break;
336     case 8: Sig.push_back(IIT_V8); break;
337     case 16: Sig.push_back(IIT_V16); break;
338     case 32: Sig.push_back(IIT_V32); break;
339     case 64: Sig.push_back(IIT_V64); break;
340     case 512: Sig.push_back(IIT_V512); break;
341     case 1024: Sig.push_back(IIT_V1024); break;
342     }
343 
344     return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
345   }
346 
347   EncodeFixedValueType(VT, Sig);
348 }
349 
350 #if defined(_MSC_VER) && !defined(__clang__)
351 #pragma optimize("",on)
352 #endif
353 
354 /// ComputeFixedEncoding - If we can encode the type signature for this
355 /// intrinsic into 32 bits, return it.  If not, return ~0U.
356 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
357                                  std::vector<unsigned char> &TypeSig) {
358   std::vector<unsigned char> ArgCodes;
359 
360   if (Int.IS.RetVTs.empty())
361     TypeSig.push_back(IIT_Done);
362   else if (Int.IS.RetVTs.size() == 1 &&
363            Int.IS.RetVTs[0] == MVT::isVoid)
364     TypeSig.push_back(IIT_Done);
365   else {
366     switch (Int.IS.RetVTs.size()) {
367       case 1: break;
368       case 2: TypeSig.push_back(IIT_STRUCT2); break;
369       case 3: TypeSig.push_back(IIT_STRUCT3); break;
370       case 4: TypeSig.push_back(IIT_STRUCT4); break;
371       case 5: TypeSig.push_back(IIT_STRUCT5); break;
372       default: llvm_unreachable("Unhandled case in struct");
373     }
374 
375     for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
376       EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
377   }
378 
379   for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
380     EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
381 }
382 
383 static void printIITEntry(raw_ostream &OS, unsigned char X) {
384   OS << (unsigned)X;
385 }
386 
387 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
388                                      raw_ostream &OS) {
389   // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
390   // capture it in this vector, otherwise store a ~0U.
391   std::vector<unsigned> FixedEncodings;
392 
393   SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
394 
395   std::vector<unsigned char> TypeSig;
396 
397   // Compute the unique argument type info.
398   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
399     // Get the signature for the intrinsic.
400     TypeSig.clear();
401     ComputeFixedEncoding(Ints[i], TypeSig);
402 
403     // Check to see if we can encode it into a 32-bit word.  We can only encode
404     // 8 nibbles into a 32-bit word.
405     if (TypeSig.size() <= 8) {
406       bool Failed = false;
407       unsigned Result = 0;
408       for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
409         // If we had an unencodable argument, bail out.
410         if (TypeSig[i] > 15) {
411           Failed = true;
412           break;
413         }
414         Result = (Result << 4) | TypeSig[e-i-1];
415       }
416 
417       // If this could be encoded into a 31-bit word, return it.
418       if (!Failed && (Result >> 31) == 0) {
419         FixedEncodings.push_back(Result);
420         continue;
421       }
422     }
423 
424     // Otherwise, we're going to unique the sequence into the
425     // LongEncodingTable, and use its offset in the 32-bit table instead.
426     LongEncodingTable.add(TypeSig);
427 
428     // This is a placehold that we'll replace after the table is laid out.
429     FixedEncodings.push_back(~0U);
430   }
431 
432   LongEncodingTable.layout();
433 
434   OS << "// Global intrinsic function declaration type table.\n";
435   OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
436 
437   OS << "static const unsigned IIT_Table[] = {\n  ";
438 
439   for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
440     if ((i & 7) == 7)
441       OS << "\n  ";
442 
443     // If the entry fit in the table, just emit it.
444     if (FixedEncodings[i] != ~0U) {
445       OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
446       continue;
447     }
448 
449     TypeSig.clear();
450     ComputeFixedEncoding(Ints[i], TypeSig);
451 
452 
453     // Otherwise, emit the offset into the long encoding table.  We emit it this
454     // way so that it is easier to read the offset in the .def file.
455     OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
456   }
457 
458   OS << "0\n};\n\n";
459 
460   // Emit the shared table of register lists.
461   OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
462   if (!LongEncodingTable.empty())
463     LongEncodingTable.emit(OS, printIITEntry);
464   OS << "  255\n};\n\n";
465 
466   OS << "#endif\n\n";  // End of GET_INTRINSIC_GENERATOR_GLOBAL
467 }
468 
469 namespace {
470 struct AttributeComparator {
471   bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
472     // Sort throwing intrinsics after non-throwing intrinsics.
473     if (L->canThrow != R->canThrow)
474       return R->canThrow;
475 
476     if (L->isNoDuplicate != R->isNoDuplicate)
477       return R->isNoDuplicate;
478 
479     if (L->isNoReturn != R->isNoReturn)
480       return R->isNoReturn;
481 
482     if (L->isConvergent != R->isConvergent)
483       return R->isConvergent;
484 
485     if (L->isSpeculatable != R->isSpeculatable)
486       return R->isSpeculatable;
487 
488     if (L->hasSideEffects != R->hasSideEffects)
489       return R->hasSideEffects;
490 
491     // Try to order by readonly/readnone attribute.
492     CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
493     CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
494     if (LK != RK) return (LK > RK);
495 
496     // Order by argument attributes.
497     // This is reliable because each side is already sorted internally.
498     return (L->ArgumentAttributes < R->ArgumentAttributes);
499   }
500 };
501 } // End anonymous namespace
502 
503 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
504 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
505                                       raw_ostream &OS) {
506   OS << "// Add parameter attributes that are not common to all intrinsics.\n";
507   OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
508   if (TargetOnly)
509     OS << "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix
510        << "Intrinsic::ID id) {\n";
511   else
512     OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
513 
514   // Compute the maximum number of attribute arguments and the map
515   typedef std::map<const CodeGenIntrinsic*, unsigned,
516                    AttributeComparator> UniqAttrMapTy;
517   UniqAttrMapTy UniqAttributes;
518   unsigned maxArgAttrs = 0;
519   unsigned AttrNum = 0;
520   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
521     const CodeGenIntrinsic &intrinsic = Ints[i];
522     maxArgAttrs =
523       std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
524     unsigned &N = UniqAttributes[&intrinsic];
525     if (N) continue;
526     assert(AttrNum < 256 && "Too many unique attributes for table!");
527     N = ++AttrNum;
528   }
529 
530   // Emit an array of AttributeList.  Most intrinsics will have at least one
531   // entry, for the function itself (index ~1), which is usually nounwind.
532   OS << "  static const uint8_t IntrinsicsToAttributesMap[] = {\n";
533 
534   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
535     const CodeGenIntrinsic &intrinsic = Ints[i];
536 
537     OS << "    " << UniqAttributes[&intrinsic] << ", // "
538        << intrinsic.Name << "\n";
539   }
540   OS << "  };\n\n";
541 
542   OS << "  AttributeList AS[" << maxArgAttrs + 1 << "];\n";
543   OS << "  unsigned NumAttrs = 0;\n";
544   OS << "  if (id != 0) {\n";
545   OS << "    switch(IntrinsicsToAttributesMap[id - ";
546   if (TargetOnly)
547     OS << "Intrinsic::num_intrinsics";
548   else
549     OS << "1";
550   OS << "]) {\n";
551   OS << "    default: llvm_unreachable(\"Invalid attribute number\");\n";
552   for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
553        E = UniqAttributes.end(); I != E; ++I) {
554     OS << "    case " << I->second << ": {\n";
555 
556     const CodeGenIntrinsic &intrinsic = *(I->first);
557 
558     // Keep track of the number of attributes we're writing out.
559     unsigned numAttrs = 0;
560 
561     // The argument attributes are alreadys sorted by argument index.
562     unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
563     if (ae) {
564       while (ai != ae) {
565         unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
566         unsigned attrIdx = argNo + 1; // Must match AttributeList::FirstArgIndex
567 
568         OS << "      const Attribute::AttrKind AttrParam" << attrIdx << "[]= {";
569         bool addComma = false;
570 
571         do {
572           switch (intrinsic.ArgumentAttributes[ai].second) {
573           case CodeGenIntrinsic::NoCapture:
574             if (addComma)
575               OS << ",";
576             OS << "Attribute::NoCapture";
577             addComma = true;
578             break;
579           case CodeGenIntrinsic::Returned:
580             if (addComma)
581               OS << ",";
582             OS << "Attribute::Returned";
583             addComma = true;
584             break;
585           case CodeGenIntrinsic::ReadOnly:
586             if (addComma)
587               OS << ",";
588             OS << "Attribute::ReadOnly";
589             addComma = true;
590             break;
591           case CodeGenIntrinsic::WriteOnly:
592             if (addComma)
593               OS << ",";
594             OS << "Attribute::WriteOnly";
595             addComma = true;
596             break;
597           case CodeGenIntrinsic::ReadNone:
598             if (addComma)
599               OS << ",";
600             OS << "Attribute::ReadNone";
601             addComma = true;
602             break;
603           }
604 
605           ++ai;
606         } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
607         OS << "};\n";
608         OS << "      AS[" << numAttrs++ << "] = AttributeList::get(C, "
609            << attrIdx << ", AttrParam" << attrIdx << ");\n";
610       }
611     }
612 
613     if (!intrinsic.canThrow ||
614         intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
615         intrinsic.isNoReturn || intrinsic.isNoDuplicate ||
616         intrinsic.isConvergent || intrinsic.isSpeculatable) {
617       OS << "      const Attribute::AttrKind Atts[] = {";
618       bool addComma = false;
619       if (!intrinsic.canThrow) {
620         OS << "Attribute::NoUnwind";
621         addComma = true;
622       }
623       if (intrinsic.isNoReturn) {
624         if (addComma)
625           OS << ",";
626         OS << "Attribute::NoReturn";
627         addComma = true;
628       }
629       if (intrinsic.isNoDuplicate) {
630         if (addComma)
631           OS << ",";
632         OS << "Attribute::NoDuplicate";
633         addComma = true;
634       }
635       if (intrinsic.isConvergent) {
636         if (addComma)
637           OS << ",";
638         OS << "Attribute::Convergent";
639         addComma = true;
640       }
641       if (intrinsic.isSpeculatable) {
642         if (addComma)
643           OS << ",";
644         OS << "Attribute::Speculatable";
645         addComma = true;
646       }
647 
648       switch (intrinsic.ModRef) {
649       case CodeGenIntrinsic::NoMem:
650         if (addComma)
651           OS << ",";
652         OS << "Attribute::ReadNone";
653         break;
654       case CodeGenIntrinsic::ReadArgMem:
655         if (addComma)
656           OS << ",";
657         OS << "Attribute::ReadOnly,";
658         OS << "Attribute::ArgMemOnly";
659         break;
660       case CodeGenIntrinsic::ReadMem:
661         if (addComma)
662           OS << ",";
663         OS << "Attribute::ReadOnly";
664         break;
665       case CodeGenIntrinsic::ReadInaccessibleMem:
666         if (addComma)
667           OS << ",";
668         OS << "Attribute::ReadOnly,";
669         OS << "Attribute::InaccessibleMemOnly";
670         break;
671       case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem:
672         if (addComma)
673           OS << ",";
674         OS << "Attribute::ReadOnly,";
675         OS << "Attribute::InaccessibleMemOrArgMemOnly";
676         break;
677       case CodeGenIntrinsic::WriteArgMem:
678         if (addComma)
679           OS << ",";
680         OS << "Attribute::WriteOnly,";
681         OS << "Attribute::ArgMemOnly";
682         break;
683       case CodeGenIntrinsic::WriteMem:
684         if (addComma)
685           OS << ",";
686         OS << "Attribute::WriteOnly";
687         break;
688       case CodeGenIntrinsic::WriteInaccessibleMem:
689         if (addComma)
690           OS << ",";
691         OS << "Attribute::WriteOnly,";
692         OS << "Attribute::InaccessibleMemOnly";
693         break;
694       case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem:
695         if (addComma)
696           OS << ",";
697         OS << "Attribute::WriteOnly,";
698         OS << "Attribute::InaccessibleMemOrArgOnly";
699         break;
700       case CodeGenIntrinsic::ReadWriteArgMem:
701         if (addComma)
702           OS << ",";
703         OS << "Attribute::ArgMemOnly";
704         break;
705       case CodeGenIntrinsic::ReadWriteInaccessibleMem:
706         if (addComma)
707           OS << ",";
708         OS << "Attribute::InaccessibleMemOnly";
709         break;
710       case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem:
711         if (addComma)
712           OS << ",";
713         OS << "Attribute::InaccessibleMemOrArgMemOnly";
714       case CodeGenIntrinsic::ReadWriteMem:
715         break;
716       }
717       OS << "};\n";
718       OS << "      AS[" << numAttrs++ << "] = AttributeList::get(C, "
719          << "AttributeList::FunctionIndex, Atts);\n";
720     }
721 
722     if (numAttrs) {
723       OS << "      NumAttrs = " << numAttrs << ";\n";
724       OS << "      break;\n";
725       OS << "      }\n";
726     } else {
727       OS << "      return AttributeList();\n";
728       OS << "      }\n";
729     }
730   }
731 
732   OS << "    }\n";
733   OS << "  }\n";
734   OS << "  return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n";
735   OS << "}\n";
736   OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
737 }
738 
739 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
740     const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
741   StringRef CompilerName = (IsGCC ? "GCC" : "MS");
742   typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
743   BIMTy BuiltinMap;
744   StringToOffsetTable Table;
745   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
746     const std::string &BuiltinName =
747         IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
748     if (!BuiltinName.empty()) {
749       // Get the map for this target prefix.
750       std::map<std::string, std::string> &BIM =
751           BuiltinMap[Ints[i].TargetPrefix];
752 
753       if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
754         PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
755                         "': duplicate " + CompilerName + " builtin name!");
756       Table.GetOrAddStringOffset(BuiltinName);
757     }
758   }
759 
760   OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
761   OS << "// This is used by the C front-end.  The builtin name is passed\n";
762   OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
763   OS << "// in as TargetPrefix.  The result is assigned to 'IntrinsicID'.\n";
764   OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
765 
766   if (TargetOnly) {
767     OS << "static " << TargetPrefix << "Intrinsic::ID "
768        << "getIntrinsicFor" << CompilerName << "Builtin(const char "
769        << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
770   } else {
771     OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
772        << "Builtin(const char "
773        << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
774   }
775 
776   if (Table.Empty()) {
777     OS << "  return ";
778     if (!TargetPrefix.empty())
779       OS << "(" << TargetPrefix << "Intrinsic::ID)";
780     OS << "Intrinsic::not_intrinsic;\n";
781     OS << "}\n";
782     OS << "#endif\n\n";
783     return;
784   }
785 
786   OS << "  static const char BuiltinNames[] = {\n";
787   Table.EmitCharArray(OS);
788   OS << "  };\n\n";
789 
790   OS << "  struct BuiltinEntry {\n";
791   OS << "    Intrinsic::ID IntrinID;\n";
792   OS << "    unsigned StrTabOffset;\n";
793   OS << "    const char *getName() const {\n";
794   OS << "      return &BuiltinNames[StrTabOffset];\n";
795   OS << "    }\n";
796   OS << "    bool operator<(StringRef RHS) const {\n";
797   OS << "      return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
798   OS << "    }\n";
799   OS << "  };\n";
800 
801   OS << "  StringRef TargetPrefix(TargetPrefixStr);\n\n";
802 
803   // Note: this could emit significantly better code if we cared.
804   for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
805     OS << "  ";
806     if (!I->first.empty())
807       OS << "if (TargetPrefix == \"" << I->first << "\") ";
808     else
809       OS << "/* Target Independent Builtins */ ";
810     OS << "{\n";
811 
812     // Emit the comparisons for this target prefix.
813     OS << "    static const BuiltinEntry " << I->first << "Names[] = {\n";
814     for (const auto &P : I->second) {
815       OS << "      {Intrinsic::" << P.second << ", "
816          << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
817     }
818     OS << "    };\n";
819     OS << "    auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
820     OS << "                              std::end(" << I->first << "Names),\n";
821     OS << "                              BuiltinNameStr);\n";
822     OS << "    if (I != std::end(" << I->first << "Names) &&\n";
823     OS << "        I->getName() == BuiltinNameStr)\n";
824     OS << "      return I->IntrinID;\n";
825     OS << "  }\n";
826   }
827   OS << "  return ";
828   if (!TargetPrefix.empty())
829     OS << "(" << TargetPrefix << "Intrinsic::ID)";
830   OS << "Intrinsic::not_intrinsic;\n";
831   OS << "}\n";
832   OS << "#endif\n\n";
833 }
834 
835 void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
836   IntrinsicEmitter(RK, TargetOnly).run(OS);
837 }
838