xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/IntrinsicEmitter.cpp (revision e1c4c8dd8d2d10b6104f06856a77bd5b4813a801)
1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This tablegen backend emits information about intrinsic functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CodeGenIntrinsics.h"
14 #include "SequenceToOffsetTable.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/ModRef.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/TableGen/Error.h"
25 #include "llvm/TableGen/Record.h"
26 #include "llvm/TableGen/StringToOffsetTable.h"
27 #include "llvm/TableGen/TableGenBackend.h"
28 #include <algorithm>
29 #include <array>
30 #include <cassert>
31 #include <map>
32 #include <optional>
33 #include <string>
34 #include <utility>
35 #include <vector>
36 using namespace llvm;
37 
38 cl::OptionCategory GenIntrinsicCat("Options for -gen-intrinsic-enums");
39 cl::opt<std::string>
40     IntrinsicPrefix("intrinsic-prefix",
41                     cl::desc("Generate intrinsics with this target prefix"),
42                     cl::value_desc("target prefix"), cl::cat(GenIntrinsicCat));
43 
44 namespace {
45 class IntrinsicEmitter {
46   RecordKeeper &Records;
47 
48 public:
49   IntrinsicEmitter(RecordKeeper &R) : Records(R) {}
50 
51   void run(raw_ostream &OS, bool Enums);
52 
53   void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
54   void EmitArgKind(raw_ostream &OS);
55   void EmitIITInfo(raw_ostream &OS);
56   void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
57   void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints,
58                                 raw_ostream &OS);
59   void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
60                                     raw_ostream &OS);
61   void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
62   void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
63   void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints, bool IsClang,
64                                  raw_ostream &OS);
65 };
66 } // End anonymous namespace
67 
68 //===----------------------------------------------------------------------===//
69 // IntrinsicEmitter Implementation
70 //===----------------------------------------------------------------------===//
71 
72 void IntrinsicEmitter::run(raw_ostream &OS, bool Enums) {
73   emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
74 
75   CodeGenIntrinsicTable Ints(Records);
76 
77   if (Enums) {
78     // Emit the enum information.
79     EmitEnumInfo(Ints, OS);
80 
81     // Emit ArgKind for Intrinsics.h.
82     EmitArgKind(OS);
83   } else {
84     // Emit IIT_Info constants.
85     EmitIITInfo(OS);
86 
87     // Emit the target metadata.
88     EmitTargetInfo(Ints, OS);
89 
90     // Emit the intrinsic ID -> name table.
91     EmitIntrinsicToNameTable(Ints, OS);
92 
93     // Emit the intrinsic ID -> overload table.
94     EmitIntrinsicToOverloadTable(Ints, OS);
95 
96     // Emit the intrinsic declaration generator.
97     EmitGenerator(Ints, OS);
98 
99     // Emit the intrinsic parameter attributes.
100     EmitAttributes(Ints, OS);
101 
102     // Emit code to translate GCC builtins into LLVM intrinsics.
103     EmitIntrinsicToBuiltinMap(Ints, true, OS);
104 
105     // Emit code to translate MS builtins into LLVM intrinsics.
106     EmitIntrinsicToBuiltinMap(Ints, false, OS);
107   }
108 }
109 
110 void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
111                                     raw_ostream &OS) {
112   // Find the TargetSet for which to generate enums. There will be an initial
113   // set with an empty target prefix which will include target independent
114   // intrinsics like dbg.value.
115   const CodeGenIntrinsicTable::TargetSet *Set = nullptr;
116   for (const auto &Target : Ints.Targets) {
117     if (Target.Name == IntrinsicPrefix) {
118       Set = &Target;
119       break;
120     }
121   }
122   if (!Set) {
123     std::vector<std::string> KnownTargets;
124     for (const auto &Target : Ints.Targets)
125       if (!Target.Name.empty())
126         KnownTargets.push_back(Target.Name);
127     PrintFatalError("tried to generate intrinsics for unknown target " +
128                     IntrinsicPrefix +
129                     "\nKnown targets are: " + join(KnownTargets, ", ") + "\n");
130   }
131 
132   // Generate a complete header for target specific intrinsics.
133   if (IntrinsicPrefix.empty()) {
134     OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
135   } else {
136     std::string UpperPrefix = StringRef(IntrinsicPrefix).upper();
137     OS << "#ifndef LLVM_IR_INTRINSIC_" << UpperPrefix << "_ENUMS_H\n";
138     OS << "#define LLVM_IR_INTRINSIC_" << UpperPrefix << "_ENUMS_H\n\n";
139     OS << "namespace llvm {\n";
140     OS << "namespace Intrinsic {\n";
141     OS << "enum " << UpperPrefix << "Intrinsics : unsigned {\n";
142   }
143 
144   OS << "// Enum values for intrinsics\n";
145   for (unsigned i = Set->Offset, e = Set->Offset + Set->Count; i != e; ++i) {
146     OS << "    " << Ints[i].EnumName;
147 
148     // Assign a value to the first intrinsic in this target set so that all
149     // intrinsic ids are distinct.
150     if (i == Set->Offset)
151       OS << " = " << (Set->Offset + 1);
152 
153     OS << ", ";
154     if (Ints[i].EnumName.size() < 40)
155       OS.indent(40 - Ints[i].EnumName.size());
156     OS << " // " << Ints[i].Name << "\n";
157   }
158 
159   // Emit num_intrinsics into the target neutral enum.
160   if (IntrinsicPrefix.empty()) {
161     OS << "    num_intrinsics = " << (Ints.size() + 1) << "\n";
162     OS << "#endif\n\n";
163   } else {
164     OS << "}; // enum\n";
165     OS << "} // namespace Intrinsic\n";
166     OS << "} // namespace llvm\n\n";
167     OS << "#endif\n";
168   }
169 }
170 
171 void IntrinsicEmitter::EmitArgKind(raw_ostream &OS) {
172   if (!IntrinsicPrefix.empty())
173     return;
174   OS << "// llvm::Intrinsic::IITDescriptor::ArgKind\n";
175   OS << "#ifdef GET_INTRINSIC_ARGKIND\n";
176   if (auto RecArgKind = Records.getDef("ArgKind")) {
177     for (auto &RV : RecArgKind->getValues())
178       OS << "    AK_" << RV.getName() << " = " << *RV.getValue() << ",\n";
179   } else {
180     OS << "#error \"ArgKind is not defined\"\n";
181   }
182   OS << "#endif\n\n";
183 }
184 
185 void IntrinsicEmitter::EmitIITInfo(raw_ostream &OS) {
186   OS << "#ifdef GET_INTRINSIC_IITINFO\n";
187   std::array<StringRef, 256> RecsByNumber;
188   auto IIT_Base = Records.getAllDerivedDefinitionsIfDefined("IIT_Base");
189   for (auto Rec : IIT_Base) {
190     auto Number = Rec->getValueAsInt("Number");
191     assert(0 <= Number && Number < (int)RecsByNumber.size() &&
192            "IIT_Info.Number should be uint8_t");
193     assert(RecsByNumber[Number].empty() && "Duplicate IIT_Info.Number");
194     RecsByNumber[Number] = Rec->getName();
195   }
196   if (IIT_Base.size() > 0) {
197     for (unsigned I = 0, E = RecsByNumber.size(); I < E; ++I)
198       if (!RecsByNumber[I].empty())
199         OS << "  " << RecsByNumber[I] << " = " << I << ",\n";
200   } else {
201     OS << "#error \"class IIT_Base is not defined\"\n";
202   }
203   OS << "#endif\n\n";
204 }
205 
206 void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
207                                     raw_ostream &OS) {
208   OS << "// Target mapping\n";
209   OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n";
210   OS << "struct IntrinsicTargetInfo {\n"
211      << "  llvm::StringLiteral Name;\n"
212      << "  size_t Offset;\n"
213      << "  size_t Count;\n"
214      << "};\n";
215   OS << "static constexpr IntrinsicTargetInfo TargetInfos[] = {\n";
216   for (const auto &Target : Ints.Targets)
217     OS << "  {llvm::StringLiteral(\"" << Target.Name << "\"), " << Target.Offset
218        << ", " << Target.Count << "},\n";
219   OS << "};\n";
220   OS << "#endif\n\n";
221 }
222 
223 void IntrinsicEmitter::EmitIntrinsicToNameTable(
224     const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
225   OS << "// Intrinsic ID to name table\n";
226   OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
227   OS << "  // Note that entry #0 is the invalid intrinsic!\n";
228   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
229     OS << "  \"" << Ints[i].Name << "\",\n";
230   OS << "#endif\n\n";
231 }
232 
233 void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
234     const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
235   OS << "// Intrinsic ID to overload bitset\n";
236   OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
237   OS << "static const uint8_t OTable[] = {\n";
238   OS << "  0";
239   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
240     // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
241     if ((i+1)%8 == 0)
242       OS << ",\n  0";
243     if (Ints[i].isOverloaded)
244       OS << " | (1<<" << (i+1)%8 << ')';
245   }
246   OS << "\n};\n\n";
247   // OTable contains a true bit at the position if the intrinsic is overloaded.
248   OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
249   OS << "#endif\n\n";
250 }
251 
252 /// ComputeFixedEncoding - If we can encode the type signature for this
253 /// intrinsic into 32 bits, return it.  If not, return ~0U.
254 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
255                                  std::vector<unsigned char> &TypeSig) {
256   if (auto *R = Int.TheDef->getValue("TypeSig")) {
257     for (auto &a : cast<ListInit>(R->getValue())->getValues()) {
258       for (auto &b : cast<ListInit>(a)->getValues())
259         TypeSig.push_back(cast<IntInit>(b)->getValue());
260     }
261   }
262 }
263 
264 static void printIITEntry(raw_ostream &OS, unsigned char X) {
265   OS << (unsigned)X;
266 }
267 
268 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
269                                      raw_ostream &OS) {
270   // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
271   // capture it in this vector, otherwise store a ~0U.
272   std::vector<unsigned> FixedEncodings;
273 
274   SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
275 
276   std::vector<unsigned char> TypeSig;
277 
278   // Compute the unique argument type info.
279   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
280     // Get the signature for the intrinsic.
281     TypeSig.clear();
282     ComputeFixedEncoding(Ints[i], TypeSig);
283 
284     // Check to see if we can encode it into a 32-bit word.  We can only encode
285     // 8 nibbles into a 32-bit word.
286     if (TypeSig.size() <= 8) {
287       bool Failed = false;
288       unsigned Result = 0;
289       for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
290         // If we had an unencodable argument, bail out.
291         if (TypeSig[i] > 15) {
292           Failed = true;
293           break;
294         }
295         Result = (Result << 4) | TypeSig[e-i-1];
296       }
297 
298       // If this could be encoded into a 31-bit word, return it.
299       if (!Failed && (Result >> 31) == 0) {
300         FixedEncodings.push_back(Result);
301         continue;
302       }
303     }
304 
305     // Otherwise, we're going to unique the sequence into the
306     // LongEncodingTable, and use its offset in the 32-bit table instead.
307     LongEncodingTable.add(TypeSig);
308 
309     // This is a placehold that we'll replace after the table is laid out.
310     FixedEncodings.push_back(~0U);
311   }
312 
313   LongEncodingTable.layout();
314 
315   OS << "// Global intrinsic function declaration type table.\n";
316   OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
317 
318   OS << "static const unsigned IIT_Table[] = {\n  ";
319 
320   for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
321     if ((i & 7) == 7)
322       OS << "\n  ";
323 
324     // If the entry fit in the table, just emit it.
325     if (FixedEncodings[i] != ~0U) {
326       OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", ";
327       continue;
328     }
329 
330     TypeSig.clear();
331     ComputeFixedEncoding(Ints[i], TypeSig);
332 
333 
334     // Otherwise, emit the offset into the long encoding table.  We emit it this
335     // way so that it is easier to read the offset in the .def file.
336     OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
337   }
338 
339   OS << "0\n};\n\n";
340 
341   // Emit the shared table of register lists.
342   OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
343   if (!LongEncodingTable.empty())
344     LongEncodingTable.emit(OS, printIITEntry);
345   OS << "  255\n};\n\n";
346 
347   OS << "#endif\n\n";  // End of GET_INTRINSIC_GENERATOR_GLOBAL
348 }
349 
350 namespace {
351 std::optional<bool> compareFnAttributes(const CodeGenIntrinsic *L,
352                                         const CodeGenIntrinsic *R) {
353   // Sort throwing intrinsics after non-throwing intrinsics.
354   if (L->canThrow != R->canThrow)
355     return R->canThrow;
356 
357   if (L->isNoDuplicate != R->isNoDuplicate)
358     return R->isNoDuplicate;
359 
360   if (L->isNoMerge != R->isNoMerge)
361     return R->isNoMerge;
362 
363   if (L->isNoReturn != R->isNoReturn)
364     return R->isNoReturn;
365 
366   if (L->isNoCallback != R->isNoCallback)
367     return R->isNoCallback;
368 
369   if (L->isNoSync != R->isNoSync)
370     return R->isNoSync;
371 
372   if (L->isNoFree != R->isNoFree)
373     return R->isNoFree;
374 
375   if (L->isWillReturn != R->isWillReturn)
376     return R->isWillReturn;
377 
378   if (L->isCold != R->isCold)
379     return R->isCold;
380 
381   if (L->isConvergent != R->isConvergent)
382     return R->isConvergent;
383 
384   if (L->isSpeculatable != R->isSpeculatable)
385     return R->isSpeculatable;
386 
387   if (L->hasSideEffects != R->hasSideEffects)
388     return R->hasSideEffects;
389 
390   if (L->isStrictFP != R->isStrictFP)
391     return R->isStrictFP;
392 
393   // Try to order by readonly/readnone attribute.
394   uint32_t LK = L->ME.toIntValue();
395   uint32_t RK = R->ME.toIntValue();
396   if (LK != RK) return (LK > RK);
397 
398   return std::nullopt;
399 }
400 
401 struct FnAttributeComparator {
402   bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
403     return compareFnAttributes(L, R).value_or(false);
404   }
405 };
406 
407 struct AttributeComparator {
408   bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
409     if (std::optional<bool> Res = compareFnAttributes(L, R))
410       return *Res;
411 
412     // Order by argument attributes.
413     // This is reliable because each side is already sorted internally.
414     return (L->ArgumentAttributes < R->ArgumentAttributes);
415   }
416 };
417 } // End anonymous namespace
418 
419 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
420 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
421                                       raw_ostream &OS) {
422   OS << "// Add parameter attributes that are not common to all intrinsics.\n";
423   OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
424 
425   // Compute unique argument attribute sets.
426   std::map<SmallVector<CodeGenIntrinsic::ArgAttribute, 0>, unsigned>
427       UniqArgAttributes;
428   OS << "static AttributeSet getIntrinsicArgAttributeSet("
429      << "LLVMContext &C, unsigned ID) {\n"
430      << "  switch (ID) {\n"
431      << "  default: llvm_unreachable(\"Invalid attribute set number\");\n";
432   for (const CodeGenIntrinsic &Int : Ints) {
433     for (auto &Attrs : Int.ArgumentAttributes) {
434       if (Attrs.empty())
435         continue;
436 
437       unsigned ID = UniqArgAttributes.size();
438       if (!UniqArgAttributes.try_emplace(Attrs, ID).second)
439         continue;
440 
441       assert(is_sorted(Attrs) &&
442              "Argument attributes are not sorted");
443 
444       OS << "  case " << ID << ":\n";
445       OS << "    return AttributeSet::get(C, {\n";
446       for (const CodeGenIntrinsic::ArgAttribute &Attr : Attrs) {
447         switch (Attr.Kind) {
448         case CodeGenIntrinsic::NoCapture:
449           OS << "      Attribute::get(C, Attribute::NoCapture),\n";
450           break;
451         case CodeGenIntrinsic::NoAlias:
452           OS << "      Attribute::get(C, Attribute::NoAlias),\n";
453           break;
454         case CodeGenIntrinsic::NoUndef:
455           OS << "      Attribute::get(C, Attribute::NoUndef),\n";
456           break;
457         case CodeGenIntrinsic::NonNull:
458           OS << "      Attribute::get(C, Attribute::NonNull),\n";
459           break;
460         case CodeGenIntrinsic::Returned:
461           OS << "      Attribute::get(C, Attribute::Returned),\n";
462           break;
463         case CodeGenIntrinsic::ReadOnly:
464           OS << "      Attribute::get(C, Attribute::ReadOnly),\n";
465           break;
466         case CodeGenIntrinsic::WriteOnly:
467           OS << "      Attribute::get(C, Attribute::WriteOnly),\n";
468           break;
469         case CodeGenIntrinsic::ReadNone:
470           OS << "      Attribute::get(C, Attribute::ReadNone),\n";
471           break;
472         case CodeGenIntrinsic::ImmArg:
473           OS << "      Attribute::get(C, Attribute::ImmArg),\n";
474           break;
475         case CodeGenIntrinsic::Alignment:
476           OS << "      Attribute::get(C, Attribute::Alignment, "
477              << Attr.Value << "),\n";
478           break;
479         case CodeGenIntrinsic::Dereferenceable:
480           OS << "      Attribute::get(C, Attribute::Dereferenceable, "
481              << Attr.Value << "),\n";
482           break;
483         }
484       }
485       OS << "    });\n";
486     }
487   }
488   OS << "  }\n";
489   OS << "}\n\n";
490 
491   // Compute unique function attribute sets.
492   std::map<const CodeGenIntrinsic*, unsigned, FnAttributeComparator>
493       UniqFnAttributes;
494   OS << "static AttributeSet getIntrinsicFnAttributeSet("
495      << "LLVMContext &C, unsigned ID) {\n"
496      << "  switch (ID) {\n"
497      << "  default: llvm_unreachable(\"Invalid attribute set number\");\n";
498   for (const CodeGenIntrinsic &Intrinsic : Ints) {
499     unsigned ID = UniqFnAttributes.size();
500     if (!UniqFnAttributes.try_emplace(&Intrinsic, ID).second)
501       continue;
502 
503     OS << "  case " << ID << ":\n"
504        << "    return AttributeSet::get(C, {\n";
505     if (!Intrinsic.canThrow)
506       OS << "      Attribute::get(C, Attribute::NoUnwind),\n";
507     if (Intrinsic.isNoReturn)
508       OS << "      Attribute::get(C, Attribute::NoReturn),\n";
509     if (Intrinsic.isNoCallback)
510       OS << "      Attribute::get(C, Attribute::NoCallback),\n";
511     if (Intrinsic.isNoSync)
512       OS << "      Attribute::get(C, Attribute::NoSync),\n";
513     if (Intrinsic.isNoFree)
514       OS << "      Attribute::get(C, Attribute::NoFree),\n";
515     if (Intrinsic.isWillReturn)
516       OS << "      Attribute::get(C, Attribute::WillReturn),\n";
517     if (Intrinsic.isCold)
518       OS << "      Attribute::get(C, Attribute::Cold),\n";
519     if (Intrinsic.isNoDuplicate)
520       OS << "      Attribute::get(C, Attribute::NoDuplicate),\n";
521     if (Intrinsic.isNoMerge)
522       OS << "      Attribute::get(C, Attribute::NoMerge),\n";
523     if (Intrinsic.isConvergent)
524       OS << "      Attribute::get(C, Attribute::Convergent),\n";
525     if (Intrinsic.isSpeculatable)
526       OS << "      Attribute::get(C, Attribute::Speculatable),\n";
527     if (Intrinsic.isStrictFP)
528       OS << "      Attribute::get(C, Attribute::StrictFP),\n";
529 
530     MemoryEffects ME = Intrinsic.ME;
531     // TODO: IntrHasSideEffects should affect not only readnone intrinsics.
532     if (ME.doesNotAccessMemory() && Intrinsic.hasSideEffects)
533       ME = MemoryEffects::unknown();
534     if (ME != MemoryEffects::unknown()) {
535       OS << "      Attribute::getWithMemoryEffects(C, "
536          << "MemoryEffects::createFromIntValue(" << ME.toIntValue() << ")),\n";
537     }
538     OS << "    });\n";
539   }
540   OS << "  }\n";
541   OS << "}\n\n";
542   OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
543 
544   // Compute the maximum number of attribute arguments and the map
545   typedef std::map<const CodeGenIntrinsic*, unsigned,
546                    AttributeComparator> UniqAttrMapTy;
547   UniqAttrMapTy UniqAttributes;
548   unsigned maxArgAttrs = 0;
549   unsigned AttrNum = 0;
550   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
551     const CodeGenIntrinsic &intrinsic = Ints[i];
552     maxArgAttrs =
553       std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
554     unsigned &N = UniqAttributes[&intrinsic];
555     if (N) continue;
556     N = ++AttrNum;
557     assert(N < 65536 && "Too many unique attributes for table!");
558   }
559 
560   // Emit an array of AttributeList.  Most intrinsics will have at least one
561   // entry, for the function itself (index ~1), which is usually nounwind.
562   OS << "  static const uint16_t IntrinsicsToAttributesMap[] = {\n";
563 
564   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
565     const CodeGenIntrinsic &intrinsic = Ints[i];
566 
567     OS << "    " << UniqAttributes[&intrinsic] << ", // "
568        << intrinsic.Name << "\n";
569   }
570   OS << "  };\n\n";
571 
572   OS << "  std::pair<unsigned, AttributeSet> AS[" << maxArgAttrs + 1 << "];\n";
573   OS << "  unsigned NumAttrs = 0;\n";
574   OS << "  if (id != 0) {\n";
575   OS << "    switch(IntrinsicsToAttributesMap[id - 1]) {\n";
576   OS << "    default: llvm_unreachable(\"Invalid attribute number\");\n";
577   for (auto UniqAttribute : UniqAttributes) {
578     OS << "    case " << UniqAttribute.second << ": {\n";
579 
580     const CodeGenIntrinsic &Intrinsic = *(UniqAttribute.first);
581 
582     // Keep track of the number of attributes we're writing out.
583     unsigned numAttrs = 0;
584 
585     for (const auto &[AttrIdx, Attrs] :
586          enumerate(Intrinsic.ArgumentAttributes)) {
587       if (Attrs.empty())
588         continue;
589 
590       unsigned ID = UniqArgAttributes.find(Attrs)->second;
591       OS << "      AS[" << numAttrs++ << "] = {" << AttrIdx
592          << ", getIntrinsicArgAttributeSet(C, " << ID << ")};\n";
593     }
594 
595     if (!Intrinsic.canThrow ||
596         (Intrinsic.ME != MemoryEffects::unknown() &&
597          !Intrinsic.hasSideEffects) ||
598         Intrinsic.isNoReturn || Intrinsic.isNoCallback || Intrinsic.isNoSync ||
599         Intrinsic.isNoFree || Intrinsic.isWillReturn || Intrinsic.isCold ||
600         Intrinsic.isNoDuplicate || Intrinsic.isNoMerge ||
601         Intrinsic.isConvergent || Intrinsic.isSpeculatable ||
602         Intrinsic.isStrictFP) {
603       unsigned ID = UniqFnAttributes.find(&Intrinsic)->second;
604       OS << "      AS[" << numAttrs++ << "] = {AttributeList::FunctionIndex, "
605          << "getIntrinsicFnAttributeSet(C, " << ID << ")};\n";
606     }
607 
608     if (numAttrs) {
609       OS << "      NumAttrs = " << numAttrs << ";\n";
610       OS << "      break;\n";
611       OS << "    }\n";
612     } else {
613       OS << "      return AttributeList();\n";
614       OS << "    }\n";
615     }
616   }
617 
618   OS << "    }\n";
619   OS << "  }\n";
620   OS << "  return AttributeList::get(C, ArrayRef(AS, NumAttrs));\n";
621   OS << "}\n";
622   OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
623 }
624 
625 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
626     const CodeGenIntrinsicTable &Ints, bool IsClang, raw_ostream &OS) {
627   StringRef CompilerName = (IsClang ? "Clang" : "MS");
628   StringRef UpperCompilerName = (IsClang ? "CLANG" : "MS");
629   typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
630   BIMTy BuiltinMap;
631   StringToOffsetTable Table;
632   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
633     const std::string &BuiltinName =
634         IsClang ? Ints[i].ClangBuiltinName : Ints[i].MSBuiltinName;
635     if (!BuiltinName.empty()) {
636       // Get the map for this target prefix.
637       std::map<std::string, std::string> &BIM =
638           BuiltinMap[Ints[i].TargetPrefix];
639 
640       if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
641         PrintFatalError(Ints[i].TheDef->getLoc(),
642                         "Intrinsic '" + Ints[i].TheDef->getName() +
643                             "': duplicate " + CompilerName + " builtin name!");
644       Table.GetOrAddStringOffset(BuiltinName);
645     }
646   }
647 
648   OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
649   OS << "// This is used by the C front-end.  The builtin name is passed\n";
650   OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
651   OS << "// in as TargetPrefix.  The result is assigned to 'IntrinsicID'.\n";
652   OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << UpperCompilerName << "_BUILTIN\n";
653 
654   OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
655      << "Builtin(const char "
656      << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
657 
658   if (Table.Empty()) {
659     OS << "  return Intrinsic::not_intrinsic;\n";
660     OS << "}\n";
661     OS << "#endif\n\n";
662     return;
663   }
664 
665   OS << "  static const char BuiltinNames[] = {\n";
666   Table.EmitCharArray(OS);
667   OS << "  };\n\n";
668 
669   OS << "  struct BuiltinEntry {\n";
670   OS << "    Intrinsic::ID IntrinID;\n";
671   OS << "    unsigned StrTabOffset;\n";
672   OS << "    const char *getName() const {\n";
673   OS << "      return &BuiltinNames[StrTabOffset];\n";
674   OS << "    }\n";
675   OS << "    bool operator<(StringRef RHS) const {\n";
676   OS << "      return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
677   OS << "    }\n";
678   OS << "  };\n";
679 
680   OS << "  StringRef TargetPrefix(TargetPrefixStr);\n\n";
681 
682   // Note: this could emit significantly better code if we cared.
683   for (auto &I : BuiltinMap) {
684     OS << "  ";
685     if (!I.first.empty())
686       OS << "if (TargetPrefix == \"" << I.first << "\") ";
687     else
688       OS << "/* Target Independent Builtins */ ";
689     OS << "{\n";
690 
691     // Emit the comparisons for this target prefix.
692     OS << "    static const BuiltinEntry " << I.first << "Names[] = {\n";
693     for (const auto &P : I.second) {
694       OS << "      {Intrinsic::" << P.second << ", "
695          << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
696     }
697     OS << "    };\n";
698     OS << "    auto I = std::lower_bound(std::begin(" << I.first << "Names),\n";
699     OS << "                              std::end(" << I.first << "Names),\n";
700     OS << "                              BuiltinNameStr);\n";
701     OS << "    if (I != std::end(" << I.first << "Names) &&\n";
702     OS << "        I->getName() == BuiltinNameStr)\n";
703     OS << "      return I->IntrinID;\n";
704     OS << "  }\n";
705   }
706   OS << "  return ";
707   OS << "Intrinsic::not_intrinsic;\n";
708   OS << "}\n";
709   OS << "#endif\n\n";
710 }
711 
712 static void EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS) {
713   IntrinsicEmitter(RK).run(OS, /*Enums=*/true);
714 }
715 
716 static TableGen::Emitter::Opt X("gen-intrinsic-enums", EmitIntrinsicEnums,
717                                 "Generate intrinsic enums");
718 
719 static void EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS) {
720   IntrinsicEmitter(RK).run(OS, /*Enums=*/false);
721 }
722 
723 static TableGen::Emitter::Opt Y("gen-intrinsic-impl", EmitIntrinsicImpl,
724                                 "Generate intrinsic information");
725