xref: /freebsd-src/contrib/llvm-project/llvm/utils/TableGen/IntrinsicEmitter.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This tablegen backend emits information about intrinsic functions.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
13*0fca6ea1SDimitry Andric #include "Basic/CodeGenIntrinsics.h"
14*0fca6ea1SDimitry Andric #include "Basic/SequenceToOffsetTable.h"
1506c3fb27SDimitry Andric #include "llvm/ADT/STLExtras.h"
1606c3fb27SDimitry Andric #include "llvm/ADT/SmallVector.h"
170b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
1806c3fb27SDimitry Andric #include "llvm/ADT/StringRef.h"
1906c3fb27SDimitry Andric #include "llvm/ADT/Twine.h"
20480093f4SDimitry Andric #include "llvm/Support/CommandLine.h"
2106c3fb27SDimitry Andric #include "llvm/Support/ErrorHandling.h"
2206c3fb27SDimitry Andric #include "llvm/Support/ModRef.h"
2306c3fb27SDimitry Andric #include "llvm/Support/raw_ostream.h"
240b57cec5SDimitry Andric #include "llvm/TableGen/Error.h"
250b57cec5SDimitry Andric #include "llvm/TableGen/Record.h"
260b57cec5SDimitry Andric #include "llvm/TableGen/StringToOffsetTable.h"
27480093f4SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
280b57cec5SDimitry Andric #include <algorithm>
2906c3fb27SDimitry Andric #include <array>
3006c3fb27SDimitry Andric #include <cassert>
3106c3fb27SDimitry Andric #include <map>
3206c3fb27SDimitry Andric #include <optional>
3306c3fb27SDimitry Andric #include <string>
3406c3fb27SDimitry Andric #include <utility>
3506c3fb27SDimitry Andric #include <vector>
360b57cec5SDimitry Andric using namespace llvm;
370b57cec5SDimitry Andric 
38480093f4SDimitry Andric cl::OptionCategory GenIntrinsicCat("Options for -gen-intrinsic-enums");
39480093f4SDimitry Andric cl::opt<std::string>
40480093f4SDimitry Andric     IntrinsicPrefix("intrinsic-prefix",
41480093f4SDimitry Andric                     cl::desc("Generate intrinsics with this target prefix"),
42480093f4SDimitry Andric                     cl::value_desc("target prefix"), cl::cat(GenIntrinsicCat));
43480093f4SDimitry Andric 
440b57cec5SDimitry Andric namespace {
450b57cec5SDimitry Andric class IntrinsicEmitter {
460b57cec5SDimitry Andric   RecordKeeper &Records;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric public:
49480093f4SDimitry Andric   IntrinsicEmitter(RecordKeeper &R) : Records(R) {}
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   void run(raw_ostream &OS, bool Enums);
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
5406c3fb27SDimitry Andric   void EmitArgKind(raw_ostream &OS);
5506c3fb27SDimitry Andric   void EmitIITInfo(raw_ostream &OS);
560b57cec5SDimitry Andric   void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
570b57cec5SDimitry Andric   void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints,
580b57cec5SDimitry Andric                                 raw_ostream &OS);
590b57cec5SDimitry Andric   void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
600b57cec5SDimitry Andric                                     raw_ostream &OS);
610b57cec5SDimitry Andric   void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
620b57cec5SDimitry Andric   void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
63*0fca6ea1SDimitry Andric   void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints,
64*0fca6ea1SDimitry Andric                                  bool IsClang, raw_ostream &OS);
650b57cec5SDimitry Andric };
660b57cec5SDimitry Andric } // End anonymous namespace
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
690b57cec5SDimitry Andric // IntrinsicEmitter Implementation
700b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric void IntrinsicEmitter::run(raw_ostream &OS, bool Enums) {
730b57cec5SDimitry Andric   emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
740b57cec5SDimitry Andric 
75480093f4SDimitry Andric   CodeGenIntrinsicTable Ints(Records);
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   if (Enums) {
780b57cec5SDimitry Andric     // Emit the enum information.
790b57cec5SDimitry Andric     EmitEnumInfo(Ints, OS);
8006c3fb27SDimitry Andric 
8106c3fb27SDimitry Andric     // Emit ArgKind for Intrinsics.h.
8206c3fb27SDimitry Andric     EmitArgKind(OS);
830b57cec5SDimitry Andric   } else {
8406c3fb27SDimitry Andric     // Emit IIT_Info constants.
8506c3fb27SDimitry Andric     EmitIITInfo(OS);
8606c3fb27SDimitry Andric 
870b57cec5SDimitry Andric     // Emit the target metadata.
880b57cec5SDimitry Andric     EmitTargetInfo(Ints, OS);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric     // Emit the intrinsic ID -> name table.
910b57cec5SDimitry Andric     EmitIntrinsicToNameTable(Ints, OS);
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric     // Emit the intrinsic ID -> overload table.
940b57cec5SDimitry Andric     EmitIntrinsicToOverloadTable(Ints, OS);
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric     // Emit the intrinsic declaration generator.
970b57cec5SDimitry Andric     EmitGenerator(Ints, OS);
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric     // Emit the intrinsic parameter attributes.
1000b57cec5SDimitry Andric     EmitAttributes(Ints, OS);
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric     // Emit code to translate GCC builtins into LLVM intrinsics.
1030b57cec5SDimitry Andric     EmitIntrinsicToBuiltinMap(Ints, true, OS);
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric     // Emit code to translate MS builtins into LLVM intrinsics.
1060b57cec5SDimitry Andric     EmitIntrinsicToBuiltinMap(Ints, false, OS);
1070b57cec5SDimitry Andric   }
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
1110b57cec5SDimitry Andric                                     raw_ostream &OS) {
112480093f4SDimitry Andric   // Find the TargetSet for which to generate enums. There will be an initial
113480093f4SDimitry Andric   // set with an empty target prefix which will include target independent
114480093f4SDimitry Andric   // intrinsics like dbg.value.
115480093f4SDimitry Andric   const CodeGenIntrinsicTable::TargetSet *Set = nullptr;
116480093f4SDimitry Andric   for (const auto &Target : Ints.Targets) {
117480093f4SDimitry Andric     if (Target.Name == IntrinsicPrefix) {
118480093f4SDimitry Andric       Set = &Target;
119480093f4SDimitry Andric       break;
120480093f4SDimitry Andric     }
121480093f4SDimitry Andric   }
122480093f4SDimitry Andric   if (!Set) {
123480093f4SDimitry Andric     std::vector<std::string> KnownTargets;
124480093f4SDimitry Andric     for (const auto &Target : Ints.Targets)
125480093f4SDimitry Andric       if (!Target.Name.empty())
126480093f4SDimitry Andric         KnownTargets.push_back(Target.Name);
127480093f4SDimitry Andric     PrintFatalError("tried to generate intrinsics for unknown target " +
128480093f4SDimitry Andric                     IntrinsicPrefix +
129480093f4SDimitry Andric                     "\nKnown targets are: " + join(KnownTargets, ", ") + "\n");
130480093f4SDimitry Andric   }
131480093f4SDimitry Andric 
132480093f4SDimitry Andric   // Generate a complete header for target specific intrinsics.
13306c3fb27SDimitry Andric   if (IntrinsicPrefix.empty()) {
13406c3fb27SDimitry Andric     OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
13506c3fb27SDimitry Andric   } else {
136480093f4SDimitry Andric     std::string UpperPrefix = StringRef(IntrinsicPrefix).upper();
137480093f4SDimitry Andric     OS << "#ifndef LLVM_IR_INTRINSIC_" << UpperPrefix << "_ENUMS_H\n";
138480093f4SDimitry Andric     OS << "#define LLVM_IR_INTRINSIC_" << UpperPrefix << "_ENUMS_H\n\n";
139480093f4SDimitry Andric     OS << "namespace llvm {\n";
140480093f4SDimitry Andric     OS << "namespace Intrinsic {\n";
141480093f4SDimitry Andric     OS << "enum " << UpperPrefix << "Intrinsics : unsigned {\n";
142480093f4SDimitry Andric   }
143480093f4SDimitry Andric 
144480093f4SDimitry Andric   OS << "// Enum values for intrinsics\n";
145480093f4SDimitry Andric   for (unsigned i = Set->Offset, e = Set->Offset + Set->Count; i != e; ++i) {
1460b57cec5SDimitry Andric     OS << "    " << Ints[i].EnumName;
147480093f4SDimitry Andric 
148480093f4SDimitry Andric     // Assign a value to the first intrinsic in this target set so that all
149480093f4SDimitry Andric     // intrinsic ids are distinct.
150480093f4SDimitry Andric     if (i == Set->Offset)
151480093f4SDimitry Andric       OS << " = " << (Set->Offset + 1);
152480093f4SDimitry Andric 
153480093f4SDimitry Andric     OS << ", ";
1540b57cec5SDimitry Andric     if (Ints[i].EnumName.size() < 40)
155480093f4SDimitry Andric       OS.indent(40 - Ints[i].EnumName.size());
1560b57cec5SDimitry Andric     OS << " // " << Ints[i].Name << "\n";
1570b57cec5SDimitry Andric   }
158480093f4SDimitry Andric 
159480093f4SDimitry Andric   // Emit num_intrinsics into the target neutral enum.
160480093f4SDimitry Andric   if (IntrinsicPrefix.empty()) {
161480093f4SDimitry Andric     OS << "    num_intrinsics = " << (Ints.size() + 1) << "\n";
16206c3fb27SDimitry Andric     OS << "#endif\n\n";
163480093f4SDimitry Andric   } else {
164480093f4SDimitry Andric     OS << "}; // enum\n";
165480093f4SDimitry Andric     OS << "} // namespace Intrinsic\n";
166480093f4SDimitry Andric     OS << "} // namespace llvm\n\n";
167480093f4SDimitry Andric     OS << "#endif\n";
168480093f4SDimitry Andric   }
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric 
17106c3fb27SDimitry Andric void IntrinsicEmitter::EmitArgKind(raw_ostream &OS) {
17206c3fb27SDimitry Andric   if (!IntrinsicPrefix.empty())
17306c3fb27SDimitry Andric     return;
17406c3fb27SDimitry Andric   OS << "// llvm::Intrinsic::IITDescriptor::ArgKind\n";
17506c3fb27SDimitry Andric   OS << "#ifdef GET_INTRINSIC_ARGKIND\n";
17606c3fb27SDimitry Andric   if (auto RecArgKind = Records.getDef("ArgKind")) {
17706c3fb27SDimitry Andric     for (auto &RV : RecArgKind->getValues())
17806c3fb27SDimitry Andric       OS << "    AK_" << RV.getName() << " = " << *RV.getValue() << ",\n";
17906c3fb27SDimitry Andric   } else {
18006c3fb27SDimitry Andric     OS << "#error \"ArgKind is not defined\"\n";
18106c3fb27SDimitry Andric   }
18206c3fb27SDimitry Andric   OS << "#endif\n\n";
18306c3fb27SDimitry Andric }
18406c3fb27SDimitry Andric 
18506c3fb27SDimitry Andric void IntrinsicEmitter::EmitIITInfo(raw_ostream &OS) {
18606c3fb27SDimitry Andric   OS << "#ifdef GET_INTRINSIC_IITINFO\n";
18706c3fb27SDimitry Andric   std::array<StringRef, 256> RecsByNumber;
18806c3fb27SDimitry Andric   auto IIT_Base = Records.getAllDerivedDefinitionsIfDefined("IIT_Base");
18906c3fb27SDimitry Andric   for (auto Rec : IIT_Base) {
19006c3fb27SDimitry Andric     auto Number = Rec->getValueAsInt("Number");
19106c3fb27SDimitry Andric     assert(0 <= Number && Number < (int)RecsByNumber.size() &&
19206c3fb27SDimitry Andric            "IIT_Info.Number should be uint8_t");
19306c3fb27SDimitry Andric     assert(RecsByNumber[Number].empty() && "Duplicate IIT_Info.Number");
19406c3fb27SDimitry Andric     RecsByNumber[Number] = Rec->getName();
19506c3fb27SDimitry Andric   }
19606c3fb27SDimitry Andric   if (IIT_Base.size() > 0) {
19706c3fb27SDimitry Andric     for (unsigned I = 0, E = RecsByNumber.size(); I < E; ++I)
19806c3fb27SDimitry Andric       if (!RecsByNumber[I].empty())
19906c3fb27SDimitry Andric         OS << "  " << RecsByNumber[I] << " = " << I << ",\n";
20006c3fb27SDimitry Andric   } else {
20106c3fb27SDimitry Andric     OS << "#error \"class IIT_Base is not defined\"\n";
20206c3fb27SDimitry Andric   }
20306c3fb27SDimitry Andric   OS << "#endif\n\n";
20406c3fb27SDimitry Andric }
20506c3fb27SDimitry Andric 
2060b57cec5SDimitry Andric void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
2070b57cec5SDimitry Andric                                       raw_ostream &OS) {
2080b57cec5SDimitry Andric   OS << "// Target mapping\n";
2090b57cec5SDimitry Andric   OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n";
2100b57cec5SDimitry Andric   OS << "struct IntrinsicTargetInfo {\n"
2110b57cec5SDimitry Andric      << "  llvm::StringLiteral Name;\n"
2120b57cec5SDimitry Andric      << "  size_t Offset;\n"
2130b57cec5SDimitry Andric      << "  size_t Count;\n"
2140b57cec5SDimitry Andric      << "};\n";
2150b57cec5SDimitry Andric   OS << "static constexpr IntrinsicTargetInfo TargetInfos[] = {\n";
2165f757f3fSDimitry Andric   for (const auto &Target : Ints.Targets)
2170b57cec5SDimitry Andric     OS << "  {llvm::StringLiteral(\"" << Target.Name << "\"), " << Target.Offset
2180b57cec5SDimitry Andric        << ", " << Target.Count << "},\n";
2190b57cec5SDimitry Andric   OS << "};\n";
2200b57cec5SDimitry Andric   OS << "#endif\n\n";
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric void IntrinsicEmitter::EmitIntrinsicToNameTable(
2240b57cec5SDimitry Andric     const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
2250b57cec5SDimitry Andric   OS << "// Intrinsic ID to name table\n";
2260b57cec5SDimitry Andric   OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
2270b57cec5SDimitry Andric   OS << "  // Note that entry #0 is the invalid intrinsic!\n";
2280b57cec5SDimitry Andric   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
2290b57cec5SDimitry Andric     OS << "  \"" << Ints[i].Name << "\",\n";
2300b57cec5SDimitry Andric   OS << "#endif\n\n";
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
2340b57cec5SDimitry Andric     const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
2350b57cec5SDimitry Andric   OS << "// Intrinsic ID to overload bitset\n";
2360b57cec5SDimitry Andric   OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
2370b57cec5SDimitry Andric   OS << "static const uint8_t OTable[] = {\n";
2380b57cec5SDimitry Andric   OS << "  0";
2390b57cec5SDimitry Andric   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
2400b57cec5SDimitry Andric     // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
2410b57cec5SDimitry Andric     if ((i + 1) % 8 == 0)
2420b57cec5SDimitry Andric       OS << ",\n  0";
2430b57cec5SDimitry Andric     if (Ints[i].isOverloaded)
2440b57cec5SDimitry Andric       OS << " | (1<<" << (i + 1) % 8 << ')';
2450b57cec5SDimitry Andric   }
2460b57cec5SDimitry Andric   OS << "\n};\n\n";
2470b57cec5SDimitry Andric   // OTable contains a true bit at the position if the intrinsic is overloaded.
2480b57cec5SDimitry Andric   OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
2490b57cec5SDimitry Andric   OS << "#endif\n\n";
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric /// ComputeFixedEncoding - If we can encode the type signature for this
2530b57cec5SDimitry Andric /// intrinsic into 32 bits, return it.  If not, return ~0U.
2540b57cec5SDimitry Andric static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
2550b57cec5SDimitry Andric                                  std::vector<unsigned char> &TypeSig) {
25606c3fb27SDimitry Andric   if (auto *R = Int.TheDef->getValue("TypeSig")) {
25706c3fb27SDimitry Andric     for (auto &a : cast<ListInit>(R->getValue())->getValues()) {
25806c3fb27SDimitry Andric       for (auto &b : cast<ListInit>(a)->getValues())
25906c3fb27SDimitry Andric         TypeSig.push_back(cast<IntInit>(b)->getValue());
2600b57cec5SDimitry Andric     }
2610b57cec5SDimitry Andric   }
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric static void printIITEntry(raw_ostream &OS, unsigned char X) {
2650b57cec5SDimitry Andric   OS << (unsigned)X;
2660b57cec5SDimitry Andric }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
2690b57cec5SDimitry Andric                                      raw_ostream &OS) {
2700b57cec5SDimitry Andric   // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
2710b57cec5SDimitry Andric   // capture it in this vector, otherwise store a ~0U.
2720b57cec5SDimitry Andric   std::vector<unsigned> FixedEncodings;
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric   SequenceToOffsetTable<std::vector<unsigned char>> LongEncodingTable;
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   std::vector<unsigned char> TypeSig;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric   // Compute the unique argument type info.
2790b57cec5SDimitry Andric   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
2800b57cec5SDimitry Andric     // Get the signature for the intrinsic.
2810b57cec5SDimitry Andric     TypeSig.clear();
2820b57cec5SDimitry Andric     ComputeFixedEncoding(Ints[i], TypeSig);
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric     // Check to see if we can encode it into a 32-bit word.  We can only encode
2850b57cec5SDimitry Andric     // 8 nibbles into a 32-bit word.
2860b57cec5SDimitry Andric     if (TypeSig.size() <= 8) {
2870b57cec5SDimitry Andric       bool Failed = false;
2880b57cec5SDimitry Andric       unsigned Result = 0;
2890b57cec5SDimitry Andric       for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
2900b57cec5SDimitry Andric         // If we had an unencodable argument, bail out.
2910b57cec5SDimitry Andric         if (TypeSig[i] > 15) {
2920b57cec5SDimitry Andric           Failed = true;
2930b57cec5SDimitry Andric           break;
2940b57cec5SDimitry Andric         }
2950b57cec5SDimitry Andric         Result = (Result << 4) | TypeSig[e - i - 1];
2960b57cec5SDimitry Andric       }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric       // If this could be encoded into a 31-bit word, return it.
2990b57cec5SDimitry Andric       if (!Failed && (Result >> 31) == 0) {
3000b57cec5SDimitry Andric         FixedEncodings.push_back(Result);
3010b57cec5SDimitry Andric         continue;
3020b57cec5SDimitry Andric       }
3030b57cec5SDimitry Andric     }
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric     // Otherwise, we're going to unique the sequence into the
3060b57cec5SDimitry Andric     // LongEncodingTable, and use its offset in the 32-bit table instead.
3070b57cec5SDimitry Andric     LongEncodingTable.add(TypeSig);
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric     // This is a placehold that we'll replace after the table is laid out.
3100b57cec5SDimitry Andric     FixedEncodings.push_back(~0U);
3110b57cec5SDimitry Andric   }
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   LongEncodingTable.layout();
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric   OS << "// Global intrinsic function declaration type table.\n";
3160b57cec5SDimitry Andric   OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   OS << "static const unsigned IIT_Table[] = {\n  ";
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
3210b57cec5SDimitry Andric     if ((i & 7) == 7)
3220b57cec5SDimitry Andric       OS << "\n  ";
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric     // If the entry fit in the table, just emit it.
3250b57cec5SDimitry Andric     if (FixedEncodings[i] != ~0U) {
3260b57cec5SDimitry Andric       OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", ";
3270b57cec5SDimitry Andric       continue;
3280b57cec5SDimitry Andric     }
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric     TypeSig.clear();
3310b57cec5SDimitry Andric     ComputeFixedEncoding(Ints[i], TypeSig);
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric     // Otherwise, emit the offset into the long encoding table.  We emit it this
3340b57cec5SDimitry Andric     // way so that it is easier to read the offset in the .def file.
3350b57cec5SDimitry Andric     OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
3360b57cec5SDimitry Andric   }
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   OS << "0\n};\n\n";
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   // Emit the shared table of register lists.
3410b57cec5SDimitry Andric   OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
3420b57cec5SDimitry Andric   if (!LongEncodingTable.empty())
3430b57cec5SDimitry Andric     LongEncodingTable.emit(OS, printIITEntry);
3440b57cec5SDimitry Andric   OS << "  255\n};\n\n";
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric namespace {
350bdd1243dSDimitry Andric std::optional<bool> compareFnAttributes(const CodeGenIntrinsic *L,
351bdd1243dSDimitry Andric                                         const CodeGenIntrinsic *R) {
3520b57cec5SDimitry Andric   // Sort throwing intrinsics after non-throwing intrinsics.
3530b57cec5SDimitry Andric   if (L->canThrow != R->canThrow)
3540b57cec5SDimitry Andric     return R->canThrow;
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   if (L->isNoDuplicate != R->isNoDuplicate)
3570b57cec5SDimitry Andric     return R->isNoDuplicate;
3580b57cec5SDimitry Andric 
359fe6060f1SDimitry Andric   if (L->isNoMerge != R->isNoMerge)
360fe6060f1SDimitry Andric     return R->isNoMerge;
361fe6060f1SDimitry Andric 
3620b57cec5SDimitry Andric   if (L->isNoReturn != R->isNoReturn)
3630b57cec5SDimitry Andric     return R->isNoReturn;
3640b57cec5SDimitry Andric 
36581ad6265SDimitry Andric   if (L->isNoCallback != R->isNoCallback)
36681ad6265SDimitry Andric     return R->isNoCallback;
36781ad6265SDimitry Andric 
3685ffd83dbSDimitry Andric   if (L->isNoSync != R->isNoSync)
3695ffd83dbSDimitry Andric     return R->isNoSync;
3705ffd83dbSDimitry Andric 
3715ffd83dbSDimitry Andric   if (L->isNoFree != R->isNoFree)
3725ffd83dbSDimitry Andric     return R->isNoFree;
3735ffd83dbSDimitry Andric 
3740b57cec5SDimitry Andric   if (L->isWillReturn != R->isWillReturn)
3750b57cec5SDimitry Andric     return R->isWillReturn;
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric   if (L->isCold != R->isCold)
3780b57cec5SDimitry Andric     return R->isCold;
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   if (L->isConvergent != R->isConvergent)
3810b57cec5SDimitry Andric     return R->isConvergent;
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   if (L->isSpeculatable != R->isSpeculatable)
3840b57cec5SDimitry Andric     return R->isSpeculatable;
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   if (L->hasSideEffects != R->hasSideEffects)
3870b57cec5SDimitry Andric     return R->hasSideEffects;
3880b57cec5SDimitry Andric 
38906c3fb27SDimitry Andric   if (L->isStrictFP != R->isStrictFP)
39006c3fb27SDimitry Andric     return R->isStrictFP;
39106c3fb27SDimitry Andric 
3920b57cec5SDimitry Andric   // Try to order by readonly/readnone attribute.
393bdd1243dSDimitry Andric   uint32_t LK = L->ME.toIntValue();
394bdd1243dSDimitry Andric   uint32_t RK = R->ME.toIntValue();
395*0fca6ea1SDimitry Andric   if (LK != RK)
396*0fca6ea1SDimitry Andric     return (LK > RK);
397bdd1243dSDimitry Andric 
398bdd1243dSDimitry Andric   return std::nullopt;
399bdd1243dSDimitry Andric }
400bdd1243dSDimitry Andric 
401bdd1243dSDimitry Andric struct FnAttributeComparator {
402bdd1243dSDimitry Andric   bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
403bdd1243dSDimitry Andric     return compareFnAttributes(L, R).value_or(false);
404bdd1243dSDimitry Andric   }
405bdd1243dSDimitry Andric };
406bdd1243dSDimitry Andric 
407bdd1243dSDimitry Andric struct AttributeComparator {
408bdd1243dSDimitry Andric   bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
409bdd1243dSDimitry Andric     if (std::optional<bool> Res = compareFnAttributes(L, R))
410bdd1243dSDimitry Andric       return *Res;
411bdd1243dSDimitry Andric 
4120b57cec5SDimitry Andric     // Order by argument attributes.
4130b57cec5SDimitry Andric     // This is reliable because each side is already sorted internally.
4140b57cec5SDimitry Andric     return (L->ArgumentAttributes < R->ArgumentAttributes);
4150b57cec5SDimitry Andric   }
4160b57cec5SDimitry Andric };
4170b57cec5SDimitry Andric } // End anonymous namespace
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric /// EmitAttributes - This emits the Intrinsic::getAttributes method.
4200b57cec5SDimitry Andric void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
4210b57cec5SDimitry Andric                                       raw_ostream &OS) {
4220b57cec5SDimitry Andric   OS << "// Add parameter attributes that are not common to all intrinsics.\n";
4230b57cec5SDimitry Andric   OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
424bdd1243dSDimitry Andric 
425bdd1243dSDimitry Andric   // Compute unique argument attribute sets.
426bdd1243dSDimitry Andric   std::map<SmallVector<CodeGenIntrinsic::ArgAttribute, 0>, unsigned>
427bdd1243dSDimitry Andric       UniqArgAttributes;
428bdd1243dSDimitry Andric   OS << "static AttributeSet getIntrinsicArgAttributeSet("
429bdd1243dSDimitry Andric      << "LLVMContext &C, unsigned ID) {\n"
430bdd1243dSDimitry Andric      << "  switch (ID) {\n"
431bdd1243dSDimitry Andric      << "  default: llvm_unreachable(\"Invalid attribute set number\");\n";
432bdd1243dSDimitry Andric   for (const CodeGenIntrinsic &Int : Ints) {
433bdd1243dSDimitry Andric     for (auto &Attrs : Int.ArgumentAttributes) {
434bdd1243dSDimitry Andric       if (Attrs.empty())
435bdd1243dSDimitry Andric         continue;
436bdd1243dSDimitry Andric 
437bdd1243dSDimitry Andric       unsigned ID = UniqArgAttributes.size();
438bdd1243dSDimitry Andric       if (!UniqArgAttributes.try_emplace(Attrs, ID).second)
439bdd1243dSDimitry Andric         continue;
440bdd1243dSDimitry Andric 
441*0fca6ea1SDimitry Andric       assert(is_sorted(Attrs) && "Argument attributes are not sorted");
442bdd1243dSDimitry Andric 
443bdd1243dSDimitry Andric       OS << "  case " << ID << ":\n";
444bdd1243dSDimitry Andric       OS << "    return AttributeSet::get(C, {\n";
445bdd1243dSDimitry Andric       for (const CodeGenIntrinsic::ArgAttribute &Attr : Attrs) {
446bdd1243dSDimitry Andric         switch (Attr.Kind) {
447bdd1243dSDimitry Andric         case CodeGenIntrinsic::NoCapture:
448bdd1243dSDimitry Andric           OS << "      Attribute::get(C, Attribute::NoCapture),\n";
449bdd1243dSDimitry Andric           break;
450bdd1243dSDimitry Andric         case CodeGenIntrinsic::NoAlias:
451bdd1243dSDimitry Andric           OS << "      Attribute::get(C, Attribute::NoAlias),\n";
452bdd1243dSDimitry Andric           break;
453bdd1243dSDimitry Andric         case CodeGenIntrinsic::NoUndef:
454bdd1243dSDimitry Andric           OS << "      Attribute::get(C, Attribute::NoUndef),\n";
455bdd1243dSDimitry Andric           break;
456bdd1243dSDimitry Andric         case CodeGenIntrinsic::NonNull:
457bdd1243dSDimitry Andric           OS << "      Attribute::get(C, Attribute::NonNull),\n";
458bdd1243dSDimitry Andric           break;
459bdd1243dSDimitry Andric         case CodeGenIntrinsic::Returned:
460bdd1243dSDimitry Andric           OS << "      Attribute::get(C, Attribute::Returned),\n";
461bdd1243dSDimitry Andric           break;
462bdd1243dSDimitry Andric         case CodeGenIntrinsic::ReadOnly:
463bdd1243dSDimitry Andric           OS << "      Attribute::get(C, Attribute::ReadOnly),\n";
464bdd1243dSDimitry Andric           break;
465bdd1243dSDimitry Andric         case CodeGenIntrinsic::WriteOnly:
466bdd1243dSDimitry Andric           OS << "      Attribute::get(C, Attribute::WriteOnly),\n";
467bdd1243dSDimitry Andric           break;
468bdd1243dSDimitry Andric         case CodeGenIntrinsic::ReadNone:
469bdd1243dSDimitry Andric           OS << "      Attribute::get(C, Attribute::ReadNone),\n";
470bdd1243dSDimitry Andric           break;
471bdd1243dSDimitry Andric         case CodeGenIntrinsic::ImmArg:
472bdd1243dSDimitry Andric           OS << "      Attribute::get(C, Attribute::ImmArg),\n";
473bdd1243dSDimitry Andric           break;
474bdd1243dSDimitry Andric         case CodeGenIntrinsic::Alignment:
475*0fca6ea1SDimitry Andric           OS << "      Attribute::get(C, Attribute::Alignment, " << Attr.Value
476*0fca6ea1SDimitry Andric              << "),\n";
477bdd1243dSDimitry Andric           break;
47806c3fb27SDimitry Andric         case CodeGenIntrinsic::Dereferenceable:
47906c3fb27SDimitry Andric           OS << "      Attribute::get(C, Attribute::Dereferenceable, "
48006c3fb27SDimitry Andric              << Attr.Value << "),\n";
48106c3fb27SDimitry Andric           break;
482bdd1243dSDimitry Andric         }
483bdd1243dSDimitry Andric       }
484bdd1243dSDimitry Andric       OS << "    });\n";
485bdd1243dSDimitry Andric     }
486bdd1243dSDimitry Andric   }
487bdd1243dSDimitry Andric   OS << "  }\n";
488bdd1243dSDimitry Andric   OS << "}\n\n";
489bdd1243dSDimitry Andric 
490bdd1243dSDimitry Andric   // Compute unique function attribute sets.
491bdd1243dSDimitry Andric   std::map<const CodeGenIntrinsic *, unsigned, FnAttributeComparator>
492bdd1243dSDimitry Andric       UniqFnAttributes;
493bdd1243dSDimitry Andric   OS << "static AttributeSet getIntrinsicFnAttributeSet("
494bdd1243dSDimitry Andric      << "LLVMContext &C, unsigned ID) {\n"
495bdd1243dSDimitry Andric      << "  switch (ID) {\n"
496bdd1243dSDimitry Andric      << "  default: llvm_unreachable(\"Invalid attribute set number\");\n";
497bdd1243dSDimitry Andric   for (const CodeGenIntrinsic &Intrinsic : Ints) {
498bdd1243dSDimitry Andric     unsigned ID = UniqFnAttributes.size();
499bdd1243dSDimitry Andric     if (!UniqFnAttributes.try_emplace(&Intrinsic, ID).second)
500bdd1243dSDimitry Andric       continue;
501bdd1243dSDimitry Andric 
502bdd1243dSDimitry Andric     OS << "  case " << ID << ":\n"
503bdd1243dSDimitry Andric        << "    return AttributeSet::get(C, {\n";
504bdd1243dSDimitry Andric     if (!Intrinsic.canThrow)
505bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::NoUnwind),\n";
506bdd1243dSDimitry Andric     if (Intrinsic.isNoReturn)
507bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::NoReturn),\n";
508bdd1243dSDimitry Andric     if (Intrinsic.isNoCallback)
509bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::NoCallback),\n";
510bdd1243dSDimitry Andric     if (Intrinsic.isNoSync)
511bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::NoSync),\n";
512bdd1243dSDimitry Andric     if (Intrinsic.isNoFree)
513bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::NoFree),\n";
514bdd1243dSDimitry Andric     if (Intrinsic.isWillReturn)
515bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::WillReturn),\n";
516bdd1243dSDimitry Andric     if (Intrinsic.isCold)
517bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::Cold),\n";
518bdd1243dSDimitry Andric     if (Intrinsic.isNoDuplicate)
519bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::NoDuplicate),\n";
520bdd1243dSDimitry Andric     if (Intrinsic.isNoMerge)
521bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::NoMerge),\n";
522bdd1243dSDimitry Andric     if (Intrinsic.isConvergent)
523bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::Convergent),\n";
524bdd1243dSDimitry Andric     if (Intrinsic.isSpeculatable)
525bdd1243dSDimitry Andric       OS << "      Attribute::get(C, Attribute::Speculatable),\n";
52606c3fb27SDimitry Andric     if (Intrinsic.isStrictFP)
52706c3fb27SDimitry Andric       OS << "      Attribute::get(C, Attribute::StrictFP),\n";
528bdd1243dSDimitry Andric 
529bdd1243dSDimitry Andric     MemoryEffects ME = Intrinsic.ME;
530bdd1243dSDimitry Andric     // TODO: IntrHasSideEffects should affect not only readnone intrinsics.
531bdd1243dSDimitry Andric     if (ME.doesNotAccessMemory() && Intrinsic.hasSideEffects)
532bdd1243dSDimitry Andric       ME = MemoryEffects::unknown();
533bdd1243dSDimitry Andric     if (ME != MemoryEffects::unknown()) {
534bdd1243dSDimitry Andric       OS << "      Attribute::getWithMemoryEffects(C, "
535bdd1243dSDimitry Andric          << "MemoryEffects::createFromIntValue(" << ME.toIntValue() << ")),\n";
536bdd1243dSDimitry Andric     }
537bdd1243dSDimitry Andric     OS << "    });\n";
538bdd1243dSDimitry Andric   }
539bdd1243dSDimitry Andric   OS << "  }\n";
540bdd1243dSDimitry Andric   OS << "}\n\n";
5410b57cec5SDimitry Andric   OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric   // Compute the maximum number of attribute arguments and the map
544*0fca6ea1SDimitry Andric   typedef std::map<const CodeGenIntrinsic *, unsigned, AttributeComparator>
545*0fca6ea1SDimitry Andric       UniqAttrMapTy;
5460b57cec5SDimitry Andric   UniqAttrMapTy UniqAttributes;
5470b57cec5SDimitry Andric   unsigned maxArgAttrs = 0;
5480b57cec5SDimitry Andric   unsigned AttrNum = 0;
5490b57cec5SDimitry Andric   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
5500b57cec5SDimitry Andric     const CodeGenIntrinsic &intrinsic = Ints[i];
5510b57cec5SDimitry Andric     maxArgAttrs =
5520b57cec5SDimitry Andric         std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
5530b57cec5SDimitry Andric     unsigned &N = UniqAttributes[&intrinsic];
554*0fca6ea1SDimitry Andric     if (N)
555*0fca6ea1SDimitry Andric       continue;
5560b57cec5SDimitry Andric     N = ++AttrNum;
557d409305fSDimitry Andric     assert(N < 65536 && "Too many unique attributes for table!");
5580b57cec5SDimitry Andric   }
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric   // Emit an array of AttributeList.  Most intrinsics will have at least one
5610b57cec5SDimitry Andric   // entry, for the function itself (index ~1), which is usually nounwind.
562d409305fSDimitry Andric   OS << "  static const uint16_t IntrinsicsToAttributesMap[] = {\n";
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
5650b57cec5SDimitry Andric     const CodeGenIntrinsic &intrinsic = Ints[i];
5660b57cec5SDimitry Andric 
567*0fca6ea1SDimitry Andric     OS << "    " << UniqAttributes[&intrinsic] << ", // " << intrinsic.Name
568*0fca6ea1SDimitry Andric        << "\n";
5690b57cec5SDimitry Andric   }
5700b57cec5SDimitry Andric   OS << "  };\n\n";
5710b57cec5SDimitry Andric 
572bdd1243dSDimitry Andric   OS << "  std::pair<unsigned, AttributeSet> AS[" << maxArgAttrs + 1 << "];\n";
5730b57cec5SDimitry Andric   OS << "  unsigned NumAttrs = 0;\n";
5740b57cec5SDimitry Andric   OS << "  if (id != 0) {\n";
575480093f4SDimitry Andric   OS << "    switch(IntrinsicsToAttributesMap[id - 1]) {\n";
5760b57cec5SDimitry Andric   OS << "    default: llvm_unreachable(\"Invalid attribute number\");\n";
577fe6060f1SDimitry Andric   for (auto UniqAttribute : UniqAttributes) {
578fe6060f1SDimitry Andric     OS << "    case " << UniqAttribute.second << ": {\n";
5790b57cec5SDimitry Andric 
580fe6060f1SDimitry Andric     const CodeGenIntrinsic &Intrinsic = *(UniqAttribute.first);
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric     // Keep track of the number of attributes we're writing out.
5830b57cec5SDimitry Andric     unsigned numAttrs = 0;
5840b57cec5SDimitry Andric 
585bdd1243dSDimitry Andric     for (const auto &[AttrIdx, Attrs] :
586bdd1243dSDimitry Andric          enumerate(Intrinsic.ArgumentAttributes)) {
587bdd1243dSDimitry Andric       if (Attrs.empty())
588bdd1243dSDimitry Andric         continue;
5890b57cec5SDimitry Andric 
590bdd1243dSDimitry Andric       unsigned ID = UniqArgAttributes.find(Attrs)->second;
591bdd1243dSDimitry Andric       OS << "      AS[" << numAttrs++ << "] = {" << AttrIdx
592bdd1243dSDimitry Andric          << ", getIntrinsicArgAttributeSet(C, " << ID << ")};\n";
5930b57cec5SDimitry Andric     }
5940b57cec5SDimitry Andric 
595fe6060f1SDimitry Andric     if (!Intrinsic.canThrow ||
596bdd1243dSDimitry Andric         (Intrinsic.ME != MemoryEffects::unknown() &&
597fe6060f1SDimitry Andric          !Intrinsic.hasSideEffects) ||
59881ad6265SDimitry Andric         Intrinsic.isNoReturn || Intrinsic.isNoCallback || Intrinsic.isNoSync ||
59981ad6265SDimitry Andric         Intrinsic.isNoFree || Intrinsic.isWillReturn || Intrinsic.isCold ||
60081ad6265SDimitry Andric         Intrinsic.isNoDuplicate || Intrinsic.isNoMerge ||
60106c3fb27SDimitry Andric         Intrinsic.isConvergent || Intrinsic.isSpeculatable ||
60206c3fb27SDimitry Andric         Intrinsic.isStrictFP) {
603bdd1243dSDimitry Andric       unsigned ID = UniqFnAttributes.find(&Intrinsic)->second;
604bdd1243dSDimitry Andric       OS << "      AS[" << numAttrs++ << "] = {AttributeList::FunctionIndex, "
605bdd1243dSDimitry Andric          << "getIntrinsicFnAttributeSet(C, " << ID << ")};\n";
6060b57cec5SDimitry Andric     }
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric     if (numAttrs) {
6090b57cec5SDimitry Andric       OS << "      NumAttrs = " << numAttrs << ";\n";
6100b57cec5SDimitry Andric       OS << "      break;\n";
6110b57cec5SDimitry Andric       OS << "    }\n";
6120b57cec5SDimitry Andric     } else {
6130b57cec5SDimitry Andric       OS << "      return AttributeList();\n";
6140b57cec5SDimitry Andric       OS << "    }\n";
6150b57cec5SDimitry Andric     }
6160b57cec5SDimitry Andric   }
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric   OS << "    }\n";
6190b57cec5SDimitry Andric   OS << "  }\n";
620bdd1243dSDimitry Andric   OS << "  return AttributeList::get(C, ArrayRef(AS, NumAttrs));\n";
6210b57cec5SDimitry Andric   OS << "}\n";
6220b57cec5SDimitry Andric   OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
6230b57cec5SDimitry Andric }
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
62681ad6265SDimitry Andric     const CodeGenIntrinsicTable &Ints, bool IsClang, raw_ostream &OS) {
62781ad6265SDimitry Andric   StringRef CompilerName = (IsClang ? "Clang" : "MS");
62881ad6265SDimitry Andric   StringRef UpperCompilerName = (IsClang ? "CLANG" : "MS");
6290b57cec5SDimitry Andric   typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
6300b57cec5SDimitry Andric   BIMTy BuiltinMap;
6310b57cec5SDimitry Andric   StringToOffsetTable Table;
6320b57cec5SDimitry Andric   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
6330b57cec5SDimitry Andric     const std::string &BuiltinName =
63481ad6265SDimitry Andric         IsClang ? Ints[i].ClangBuiltinName : Ints[i].MSBuiltinName;
6350b57cec5SDimitry Andric     if (!BuiltinName.empty()) {
6360b57cec5SDimitry Andric       // Get the map for this target prefix.
6370b57cec5SDimitry Andric       std::map<std::string, std::string> &BIM =
6380b57cec5SDimitry Andric           BuiltinMap[Ints[i].TargetPrefix];
6390b57cec5SDimitry Andric 
640*0fca6ea1SDimitry Andric       if (!BIM.insert(std::pair(BuiltinName, Ints[i].EnumName)).second)
6410b57cec5SDimitry Andric         PrintFatalError(Ints[i].TheDef->getLoc(),
6420b57cec5SDimitry Andric                         "Intrinsic '" + Ints[i].TheDef->getName() +
6430b57cec5SDimitry Andric                             "': duplicate " + CompilerName + " builtin name!");
6440b57cec5SDimitry Andric       Table.GetOrAddStringOffset(BuiltinName);
6450b57cec5SDimitry Andric     }
6460b57cec5SDimitry Andric   }
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric   OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
6490b57cec5SDimitry Andric   OS << "// This is used by the C front-end.  The builtin name is passed\n";
6500b57cec5SDimitry Andric   OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
6510b57cec5SDimitry Andric   OS << "// in as TargetPrefix.  The result is assigned to 'IntrinsicID'.\n";
65281ad6265SDimitry Andric   OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << UpperCompilerName << "_BUILTIN\n";
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric   OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
6550b57cec5SDimitry Andric      << "Builtin(const char "
6560b57cec5SDimitry Andric      << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric   if (Table.Empty()) {
659480093f4SDimitry Andric     OS << "  return Intrinsic::not_intrinsic;\n";
6600b57cec5SDimitry Andric     OS << "}\n";
6610b57cec5SDimitry Andric     OS << "#endif\n\n";
6620b57cec5SDimitry Andric     return;
6630b57cec5SDimitry Andric   }
6640b57cec5SDimitry Andric 
6650b57cec5SDimitry Andric   OS << "  static const char BuiltinNames[] = {\n";
6660b57cec5SDimitry Andric   Table.EmitCharArray(OS);
6670b57cec5SDimitry Andric   OS << "  };\n\n";
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric   OS << "  struct BuiltinEntry {\n";
6700b57cec5SDimitry Andric   OS << "    Intrinsic::ID IntrinID;\n";
6710b57cec5SDimitry Andric   OS << "    unsigned StrTabOffset;\n";
6720b57cec5SDimitry Andric   OS << "    const char *getName() const {\n";
6730b57cec5SDimitry Andric   OS << "      return &BuiltinNames[StrTabOffset];\n";
6740b57cec5SDimitry Andric   OS << "    }\n";
6750b57cec5SDimitry Andric   OS << "    bool operator<(StringRef RHS) const {\n";
6760b57cec5SDimitry Andric   OS << "      return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
6770b57cec5SDimitry Andric   OS << "    }\n";
6780b57cec5SDimitry Andric   OS << "  };\n";
6790b57cec5SDimitry Andric 
6800b57cec5SDimitry Andric   OS << "  StringRef TargetPrefix(TargetPrefixStr);\n\n";
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric   // Note: this could emit significantly better code if we cared.
683fe6060f1SDimitry Andric   for (auto &I : BuiltinMap) {
6840b57cec5SDimitry Andric     OS << "  ";
685fe6060f1SDimitry Andric     if (!I.first.empty())
686fe6060f1SDimitry Andric       OS << "if (TargetPrefix == \"" << I.first << "\") ";
6870b57cec5SDimitry Andric     else
6880b57cec5SDimitry Andric       OS << "/* Target Independent Builtins */ ";
6890b57cec5SDimitry Andric     OS << "{\n";
6900b57cec5SDimitry Andric 
6910b57cec5SDimitry Andric     // Emit the comparisons for this target prefix.
692fe6060f1SDimitry Andric     OS << "    static const BuiltinEntry " << I.first << "Names[] = {\n";
693fe6060f1SDimitry Andric     for (const auto &P : I.second) {
6940b57cec5SDimitry Andric       OS << "      {Intrinsic::" << P.second << ", "
6950b57cec5SDimitry Andric          << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
6960b57cec5SDimitry Andric     }
6970b57cec5SDimitry Andric     OS << "    };\n";
698fe6060f1SDimitry Andric     OS << "    auto I = std::lower_bound(std::begin(" << I.first << "Names),\n";
699fe6060f1SDimitry Andric     OS << "                              std::end(" << I.first << "Names),\n";
7000b57cec5SDimitry Andric     OS << "                              BuiltinNameStr);\n";
701fe6060f1SDimitry Andric     OS << "    if (I != std::end(" << I.first << "Names) &&\n";
7020b57cec5SDimitry Andric     OS << "        I->getName() == BuiltinNameStr)\n";
7030b57cec5SDimitry Andric     OS << "      return I->IntrinID;\n";
7040b57cec5SDimitry Andric     OS << "  }\n";
7050b57cec5SDimitry Andric   }
7060b57cec5SDimitry Andric   OS << "  return ";
7070b57cec5SDimitry Andric   OS << "Intrinsic::not_intrinsic;\n";
7080b57cec5SDimitry Andric   OS << "}\n";
7090b57cec5SDimitry Andric   OS << "#endif\n\n";
7100b57cec5SDimitry Andric }
7110b57cec5SDimitry Andric 
71206c3fb27SDimitry Andric static void EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS) {
713480093f4SDimitry Andric   IntrinsicEmitter(RK).run(OS, /*Enums=*/true);
7140b57cec5SDimitry Andric }
7150b57cec5SDimitry Andric 
71606c3fb27SDimitry Andric static TableGen::Emitter::Opt X("gen-intrinsic-enums", EmitIntrinsicEnums,
71706c3fb27SDimitry Andric                                 "Generate intrinsic enums");
71806c3fb27SDimitry Andric 
71906c3fb27SDimitry Andric static void EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS) {
720480093f4SDimitry Andric   IntrinsicEmitter(RK).run(OS, /*Enums=*/false);
7210b57cec5SDimitry Andric }
72206c3fb27SDimitry Andric 
72306c3fb27SDimitry Andric static TableGen::Emitter::Opt Y("gen-intrinsic-impl", EmitIntrinsicImpl,
72406c3fb27SDimitry Andric                                 "Generate intrinsic information");
725