[llvm-objdump] - Remove one overload of reportError. NFCI.
[llvm-complete.git] / utils / TableGen / IntrinsicEmitter.cpp
blob255d78e082115ea13482dbc7298d352eecd353ae
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.
11 //===----------------------------------------------------------------------===//
13 #include "CodeGenIntrinsics.h"
14 #include "CodeGenTarget.h"
15 #include "SequenceToOffsetTable.h"
16 #include "TableGenBackends.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/TableGen/Error.h"
19 #include "llvm/TableGen/Record.h"
20 #include "llvm/TableGen/StringMatcher.h"
21 #include "llvm/TableGen/TableGenBackend.h"
22 #include "llvm/TableGen/StringToOffsetTable.h"
23 #include <algorithm>
24 using namespace llvm;
26 namespace {
27 class IntrinsicEmitter {
28 RecordKeeper &Records;
29 bool TargetOnly;
30 std::string TargetPrefix;
32 public:
33 IntrinsicEmitter(RecordKeeper &R, bool T)
34 : Records(R), TargetOnly(T) {}
36 void run(raw_ostream &OS, bool Enums);
38 void EmitPrefix(raw_ostream &OS);
40 void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
41 void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
42 void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints,
43 raw_ostream &OS);
44 void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
45 raw_ostream &OS);
46 void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
47 void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
48 void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints, bool IsGCC,
49 raw_ostream &OS);
50 void EmitSuffix(raw_ostream &OS);
52 } // End anonymous namespace
54 //===----------------------------------------------------------------------===//
55 // IntrinsicEmitter Implementation
56 //===----------------------------------------------------------------------===//
58 void IntrinsicEmitter::run(raw_ostream &OS, bool Enums) {
59 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
61 CodeGenIntrinsicTable Ints(Records, TargetOnly);
63 if (TargetOnly && !Ints.empty())
64 TargetPrefix = Ints[0].TargetPrefix;
66 EmitPrefix(OS);
68 if (Enums) {
69 // Emit the enum information.
70 EmitEnumInfo(Ints, OS);
71 } else {
72 // Emit the target metadata.
73 EmitTargetInfo(Ints, OS);
75 // Emit the intrinsic ID -> name table.
76 EmitIntrinsicToNameTable(Ints, OS);
78 // Emit the intrinsic ID -> overload table.
79 EmitIntrinsicToOverloadTable(Ints, OS);
81 // Emit the intrinsic declaration generator.
82 EmitGenerator(Ints, OS);
84 // Emit the intrinsic parameter attributes.
85 EmitAttributes(Ints, OS);
87 // Emit code to translate GCC builtins into LLVM intrinsics.
88 EmitIntrinsicToBuiltinMap(Ints, true, OS);
90 // Emit code to translate MS builtins into LLVM intrinsics.
91 EmitIntrinsicToBuiltinMap(Ints, false, OS);
94 EmitSuffix(OS);
97 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
98 OS << "// VisualStudio defines setjmp as _setjmp\n"
99 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
100 " !defined(setjmp_undefined_for_msvc)\n"
101 "# pragma push_macro(\"setjmp\")\n"
102 "# undef setjmp\n"
103 "# define setjmp_undefined_for_msvc\n"
104 "#endif\n\n";
107 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
108 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
109 "// let's return it to _setjmp state\n"
110 "# pragma pop_macro(\"setjmp\")\n"
111 "# undef setjmp_undefined_for_msvc\n"
112 "#endif\n\n";
115 void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
116 raw_ostream &OS) {
117 OS << "// Enum values for Intrinsics.h\n";
118 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
119 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
120 OS << " " << Ints[i].EnumName;
121 OS << ((i != e-1) ? ", " : " ");
122 if (Ints[i].EnumName.size() < 40)
123 OS << std::string(40-Ints[i].EnumName.size(), ' ');
124 OS << " // " << Ints[i].Name << "\n";
126 OS << "#endif\n\n";
129 void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
130 raw_ostream &OS) {
131 OS << "// Target mapping\n";
132 OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n";
133 OS << "struct IntrinsicTargetInfo {\n"
134 << " llvm::StringLiteral Name;\n"
135 << " size_t Offset;\n"
136 << " size_t Count;\n"
137 << "};\n";
138 OS << "static constexpr IntrinsicTargetInfo TargetInfos[] = {\n";
139 for (auto Target : Ints.Targets)
140 OS << " {llvm::StringLiteral(\"" << Target.Name << "\"), " << Target.Offset
141 << ", " << Target.Count << "},\n";
142 OS << "};\n";
143 OS << "#endif\n\n";
146 void IntrinsicEmitter::EmitIntrinsicToNameTable(
147 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
148 OS << "// Intrinsic ID to name table\n";
149 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
150 OS << " // Note that entry #0 is the invalid intrinsic!\n";
151 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
152 OS << " \"" << Ints[i].Name << "\",\n";
153 OS << "#endif\n\n";
156 void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
157 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
158 OS << "// Intrinsic ID to overload bitset\n";
159 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
160 OS << "static const uint8_t OTable[] = {\n";
161 OS << " 0";
162 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
163 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
164 if ((i+1)%8 == 0)
165 OS << ",\n 0";
166 if (Ints[i].isOverloaded)
167 OS << " | (1<<" << (i+1)%8 << ')';
169 OS << "\n};\n\n";
170 // OTable contains a true bit at the position if the intrinsic is overloaded.
171 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
172 OS << "#endif\n\n";
176 // NOTE: This must be kept in synch with the copy in lib/IR/Function.cpp!
177 enum IIT_Info {
178 // Common values should be encoded with 0-15.
179 IIT_Done = 0,
180 IIT_I1 = 1,
181 IIT_I8 = 2,
182 IIT_I16 = 3,
183 IIT_I32 = 4,
184 IIT_I64 = 5,
185 IIT_F16 = 6,
186 IIT_F32 = 7,
187 IIT_F64 = 8,
188 IIT_V2 = 9,
189 IIT_V4 = 10,
190 IIT_V8 = 11,
191 IIT_V16 = 12,
192 IIT_V32 = 13,
193 IIT_PTR = 14,
194 IIT_ARG = 15,
196 // Values from 16+ are only encodable with the inefficient encoding.
197 IIT_V64 = 16,
198 IIT_MMX = 17,
199 IIT_TOKEN = 18,
200 IIT_METADATA = 19,
201 IIT_EMPTYSTRUCT = 20,
202 IIT_STRUCT2 = 21,
203 IIT_STRUCT3 = 22,
204 IIT_STRUCT4 = 23,
205 IIT_STRUCT5 = 24,
206 IIT_EXTEND_ARG = 25,
207 IIT_TRUNC_ARG = 26,
208 IIT_ANYPTR = 27,
209 IIT_V1 = 28,
210 IIT_VARARG = 29,
211 IIT_HALF_VEC_ARG = 30,
212 IIT_SAME_VEC_WIDTH_ARG = 31,
213 IIT_PTR_TO_ARG = 32,
214 IIT_PTR_TO_ELT = 33,
215 IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
216 IIT_I128 = 35,
217 IIT_V512 = 36,
218 IIT_V1024 = 37,
219 IIT_STRUCT6 = 38,
220 IIT_STRUCT7 = 39,
221 IIT_STRUCT8 = 40,
222 IIT_F128 = 41,
223 IIT_VEC_ELEMENT = 42
226 static void EncodeFixedValueType(MVT::SimpleValueType VT,
227 std::vector<unsigned char> &Sig) {
228 if (MVT(VT).isInteger()) {
229 unsigned BitWidth = MVT(VT).getSizeInBits();
230 switch (BitWidth) {
231 default: PrintFatalError("unhandled integer type width in intrinsic!");
232 case 1: return Sig.push_back(IIT_I1);
233 case 8: return Sig.push_back(IIT_I8);
234 case 16: return Sig.push_back(IIT_I16);
235 case 32: return Sig.push_back(IIT_I32);
236 case 64: return Sig.push_back(IIT_I64);
237 case 128: return Sig.push_back(IIT_I128);
241 switch (VT) {
242 default: PrintFatalError("unhandled MVT in intrinsic!");
243 case MVT::f16: return Sig.push_back(IIT_F16);
244 case MVT::f32: return Sig.push_back(IIT_F32);
245 case MVT::f64: return Sig.push_back(IIT_F64);
246 case MVT::f128: return Sig.push_back(IIT_F128);
247 case MVT::token: return Sig.push_back(IIT_TOKEN);
248 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
249 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
250 // MVT::OtherVT is used to mean the empty struct type here.
251 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
252 // MVT::isVoid is used to represent varargs here.
253 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
257 #if defined(_MSC_VER) && !defined(__clang__)
258 #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
259 #endif
261 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
262 unsigned &NextArgCode,
263 std::vector<unsigned char> &Sig,
264 ArrayRef<unsigned char> Mapping) {
266 if (R->isSubClassOf("LLVMMatchType")) {
267 unsigned Number = Mapping[R->getValueAsInt("Number")];
268 assert(Number < ArgCodes.size() && "Invalid matching number!");
269 if (R->isSubClassOf("LLVMExtendedType"))
270 Sig.push_back(IIT_EXTEND_ARG);
271 else if (R->isSubClassOf("LLVMTruncatedType"))
272 Sig.push_back(IIT_TRUNC_ARG);
273 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
274 Sig.push_back(IIT_HALF_VEC_ARG);
275 else if (R->isSubClassOf("LLVMScalarOrSameVectorWidth")) {
276 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
277 Sig.push_back((Number << 3) | ArgCodes[Number]);
278 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
279 EncodeFixedValueType(VT, Sig);
280 return;
282 else if (R->isSubClassOf("LLVMPointerTo"))
283 Sig.push_back(IIT_PTR_TO_ARG);
284 else if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
285 Sig.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT);
286 // Encode overloaded ArgNo
287 Sig.push_back(NextArgCode++);
288 // Encode LLVMMatchType<Number> ArgNo
289 Sig.push_back(Number);
290 return;
291 } else if (R->isSubClassOf("LLVMPointerToElt"))
292 Sig.push_back(IIT_PTR_TO_ELT);
293 else if (R->isSubClassOf("LLVMVectorElementType"))
294 Sig.push_back(IIT_VEC_ELEMENT);
295 else
296 Sig.push_back(IIT_ARG);
297 return Sig.push_back((Number << 3) | 7 /*IITDescriptor::AK_MatchType*/);
300 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
302 unsigned Tmp = 0;
303 switch (VT) {
304 default: break;
305 case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH;
306 case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH;
307 case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH;
308 case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH;
309 case MVT::Any: {
310 // If this is an "any" valuetype, then the type is the type of the next
311 // type in the list specified to getIntrinsic().
312 Sig.push_back(IIT_ARG);
314 // Figure out what arg # this is consuming, and remember what kind it was.
315 assert(NextArgCode < ArgCodes.size() && ArgCodes[NextArgCode] == Tmp &&
316 "Invalid or no ArgCode associated with overloaded VT!");
317 unsigned ArgNo = NextArgCode++;
319 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
320 return Sig.push_back((ArgNo << 3) | Tmp);
323 case MVT::iPTR: {
324 unsigned AddrSpace = 0;
325 if (R->isSubClassOf("LLVMQualPointerType")) {
326 AddrSpace = R->getValueAsInt("AddrSpace");
327 assert(AddrSpace < 256 && "Address space exceeds 255");
329 if (AddrSpace) {
330 Sig.push_back(IIT_ANYPTR);
331 Sig.push_back(AddrSpace);
332 } else {
333 Sig.push_back(IIT_PTR);
335 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, NextArgCode, Sig,
336 Mapping);
340 if (MVT(VT).isVector()) {
341 MVT VVT = VT;
342 switch (VVT.getVectorNumElements()) {
343 default: PrintFatalError("unhandled vector type width in intrinsic!");
344 case 1: Sig.push_back(IIT_V1); break;
345 case 2: Sig.push_back(IIT_V2); break;
346 case 4: Sig.push_back(IIT_V4); break;
347 case 8: Sig.push_back(IIT_V8); break;
348 case 16: Sig.push_back(IIT_V16); break;
349 case 32: Sig.push_back(IIT_V32); break;
350 case 64: Sig.push_back(IIT_V64); break;
351 case 512: Sig.push_back(IIT_V512); break;
352 case 1024: Sig.push_back(IIT_V1024); break;
355 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
358 EncodeFixedValueType(VT, Sig);
361 static void UpdateArgCodes(Record *R, std::vector<unsigned char> &ArgCodes,
362 unsigned int &NumInserted,
363 SmallVectorImpl<unsigned char> &Mapping) {
364 if (R->isSubClassOf("LLVMMatchType")) {
365 if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
366 ArgCodes.push_back(3 /*vAny*/);
367 ++NumInserted;
369 return;
372 unsigned Tmp = 0;
373 switch (getValueType(R->getValueAsDef("VT"))) {
374 default: break;
375 case MVT::iPTR:
376 UpdateArgCodes(R->getValueAsDef("ElTy"), ArgCodes, NumInserted, Mapping);
377 break;
378 case MVT::iPTRAny:
379 ++Tmp;
380 LLVM_FALLTHROUGH;
381 case MVT::vAny:
382 ++Tmp;
383 LLVM_FALLTHROUGH;
384 case MVT::fAny:
385 ++Tmp;
386 LLVM_FALLTHROUGH;
387 case MVT::iAny:
388 ++Tmp;
389 LLVM_FALLTHROUGH;
390 case MVT::Any:
391 unsigned OriginalIdx = ArgCodes.size() - NumInserted;
392 assert(OriginalIdx >= Mapping.size());
393 Mapping.resize(OriginalIdx+1);
394 Mapping[OriginalIdx] = ArgCodes.size();
395 ArgCodes.push_back(Tmp);
396 break;
400 #if defined(_MSC_VER) && !defined(__clang__)
401 #pragma optimize("",on)
402 #endif
404 /// ComputeFixedEncoding - If we can encode the type signature for this
405 /// intrinsic into 32 bits, return it. If not, return ~0U.
406 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
407 std::vector<unsigned char> &TypeSig) {
408 std::vector<unsigned char> ArgCodes;
410 // Add codes for any overloaded result VTs.
411 unsigned int NumInserted = 0;
412 SmallVector<unsigned char, 8> ArgMapping;
413 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
414 UpdateArgCodes(Int.IS.RetTypeDefs[i], ArgCodes, NumInserted, ArgMapping);
416 // Add codes for any overloaded operand VTs.
417 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
418 UpdateArgCodes(Int.IS.ParamTypeDefs[i], ArgCodes, NumInserted, ArgMapping);
420 unsigned NextArgCode = 0;
421 if (Int.IS.RetVTs.empty())
422 TypeSig.push_back(IIT_Done);
423 else if (Int.IS.RetVTs.size() == 1 &&
424 Int.IS.RetVTs[0] == MVT::isVoid)
425 TypeSig.push_back(IIT_Done);
426 else {
427 switch (Int.IS.RetVTs.size()) {
428 case 1: break;
429 case 2: TypeSig.push_back(IIT_STRUCT2); break;
430 case 3: TypeSig.push_back(IIT_STRUCT3); break;
431 case 4: TypeSig.push_back(IIT_STRUCT4); break;
432 case 5: TypeSig.push_back(IIT_STRUCT5); break;
433 case 6: TypeSig.push_back(IIT_STRUCT6); break;
434 case 7: TypeSig.push_back(IIT_STRUCT7); break;
435 case 8: TypeSig.push_back(IIT_STRUCT8); break;
436 default: llvm_unreachable("Unhandled case in struct");
439 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
440 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, NextArgCode, TypeSig,
441 ArgMapping);
444 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
445 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, NextArgCode, TypeSig,
446 ArgMapping);
449 static void printIITEntry(raw_ostream &OS, unsigned char X) {
450 OS << (unsigned)X;
453 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
454 raw_ostream &OS) {
455 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
456 // capture it in this vector, otherwise store a ~0U.
457 std::vector<unsigned> FixedEncodings;
459 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
461 std::vector<unsigned char> TypeSig;
463 // Compute the unique argument type info.
464 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
465 // Get the signature for the intrinsic.
466 TypeSig.clear();
467 ComputeFixedEncoding(Ints[i], TypeSig);
469 // Check to see if we can encode it into a 32-bit word. We can only encode
470 // 8 nibbles into a 32-bit word.
471 if (TypeSig.size() <= 8) {
472 bool Failed = false;
473 unsigned Result = 0;
474 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
475 // If we had an unencodable argument, bail out.
476 if (TypeSig[i] > 15) {
477 Failed = true;
478 break;
480 Result = (Result << 4) | TypeSig[e-i-1];
483 // If this could be encoded into a 31-bit word, return it.
484 if (!Failed && (Result >> 31) == 0) {
485 FixedEncodings.push_back(Result);
486 continue;
490 // Otherwise, we're going to unique the sequence into the
491 // LongEncodingTable, and use its offset in the 32-bit table instead.
492 LongEncodingTable.add(TypeSig);
494 // This is a placehold that we'll replace after the table is laid out.
495 FixedEncodings.push_back(~0U);
498 LongEncodingTable.layout();
500 OS << "// Global intrinsic function declaration type table.\n";
501 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
503 OS << "static const unsigned IIT_Table[] = {\n ";
505 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
506 if ((i & 7) == 7)
507 OS << "\n ";
509 // If the entry fit in the table, just emit it.
510 if (FixedEncodings[i] != ~0U) {
511 OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", ";
512 continue;
515 TypeSig.clear();
516 ComputeFixedEncoding(Ints[i], TypeSig);
519 // Otherwise, emit the offset into the long encoding table. We emit it this
520 // way so that it is easier to read the offset in the .def file.
521 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
524 OS << "0\n};\n\n";
526 // Emit the shared table of register lists.
527 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
528 if (!LongEncodingTable.empty())
529 LongEncodingTable.emit(OS, printIITEntry);
530 OS << " 255\n};\n\n";
532 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
535 namespace {
536 struct AttributeComparator {
537 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
538 // Sort throwing intrinsics after non-throwing intrinsics.
539 if (L->canThrow != R->canThrow)
540 return R->canThrow;
542 if (L->isNoDuplicate != R->isNoDuplicate)
543 return R->isNoDuplicate;
545 if (L->isNoReturn != R->isNoReturn)
546 return R->isNoReturn;
548 if (L->isWillReturn != R->isWillReturn)
549 return R->isWillReturn;
551 if (L->isCold != R->isCold)
552 return R->isCold;
554 if (L->isConvergent != R->isConvergent)
555 return R->isConvergent;
557 if (L->isSpeculatable != R->isSpeculatable)
558 return R->isSpeculatable;
560 if (L->hasSideEffects != R->hasSideEffects)
561 return R->hasSideEffects;
563 // Try to order by readonly/readnone attribute.
564 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
565 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
566 if (LK != RK) return (LK > RK);
567 // Order by argument attributes.
568 // This is reliable because each side is already sorted internally.
569 return (L->ArgumentAttributes < R->ArgumentAttributes);
572 } // End anonymous namespace
574 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
575 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
576 raw_ostream &OS) {
577 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
578 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
579 if (TargetOnly)
580 OS << "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix
581 << "Intrinsic::ID id) {\n";
582 else
583 OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
585 // Compute the maximum number of attribute arguments and the map
586 typedef std::map<const CodeGenIntrinsic*, unsigned,
587 AttributeComparator> UniqAttrMapTy;
588 UniqAttrMapTy UniqAttributes;
589 unsigned maxArgAttrs = 0;
590 unsigned AttrNum = 0;
591 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
592 const CodeGenIntrinsic &intrinsic = Ints[i];
593 maxArgAttrs =
594 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
595 unsigned &N = UniqAttributes[&intrinsic];
596 if (N) continue;
597 assert(AttrNum < 256 && "Too many unique attributes for table!");
598 N = ++AttrNum;
601 // Emit an array of AttributeList. Most intrinsics will have at least one
602 // entry, for the function itself (index ~1), which is usually nounwind.
603 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
605 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
606 const CodeGenIntrinsic &intrinsic = Ints[i];
608 OS << " " << UniqAttributes[&intrinsic] << ", // "
609 << intrinsic.Name << "\n";
611 OS << " };\n\n";
613 OS << " AttributeList AS[" << maxArgAttrs + 1 << "];\n";
614 OS << " unsigned NumAttrs = 0;\n";
615 OS << " if (id != 0) {\n";
616 OS << " switch(IntrinsicsToAttributesMap[id - ";
617 if (TargetOnly)
618 OS << "Intrinsic::num_intrinsics";
619 else
620 OS << "1";
621 OS << "]) {\n";
622 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
623 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
624 E = UniqAttributes.end(); I != E; ++I) {
625 OS << " case " << I->second << ": {\n";
627 const CodeGenIntrinsic &intrinsic = *(I->first);
629 // Keep track of the number of attributes we're writing out.
630 unsigned numAttrs = 0;
632 // The argument attributes are alreadys sorted by argument index.
633 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
634 if (ae) {
635 while (ai != ae) {
636 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
637 unsigned attrIdx = argNo + 1; // Must match AttributeList::FirstArgIndex
639 OS << " const Attribute::AttrKind AttrParam" << attrIdx << "[]= {";
640 bool addComma = false;
642 do {
643 switch (intrinsic.ArgumentAttributes[ai].second) {
644 case CodeGenIntrinsic::NoCapture:
645 if (addComma)
646 OS << ",";
647 OS << "Attribute::NoCapture";
648 addComma = true;
649 break;
650 case CodeGenIntrinsic::NoAlias:
651 if (addComma)
652 OS << ",";
653 OS << "Attribute::NoAlias";
654 addComma = true;
655 break;
656 case CodeGenIntrinsic::Returned:
657 if (addComma)
658 OS << ",";
659 OS << "Attribute::Returned";
660 addComma = true;
661 break;
662 case CodeGenIntrinsic::ReadOnly:
663 if (addComma)
664 OS << ",";
665 OS << "Attribute::ReadOnly";
666 addComma = true;
667 break;
668 case CodeGenIntrinsic::WriteOnly:
669 if (addComma)
670 OS << ",";
671 OS << "Attribute::WriteOnly";
672 addComma = true;
673 break;
674 case CodeGenIntrinsic::ReadNone:
675 if (addComma)
676 OS << ",";
677 OS << "Attribute::ReadNone";
678 addComma = true;
679 break;
680 case CodeGenIntrinsic::ImmArg:
681 if (addComma)
682 OS << ',';
683 OS << "Attribute::ImmArg";
684 addComma = true;
685 break;
688 ++ai;
689 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
690 OS << "};\n";
691 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
692 << attrIdx << ", AttrParam" << attrIdx << ");\n";
696 if (!intrinsic.canThrow ||
697 (intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem && !intrinsic.hasSideEffects) ||
698 intrinsic.isNoReturn || intrinsic.isWillReturn || intrinsic.isCold ||
699 intrinsic.isNoDuplicate || intrinsic.isConvergent ||
700 intrinsic.isSpeculatable) {
701 OS << " const Attribute::AttrKind Atts[] = {";
702 bool addComma = false;
703 if (!intrinsic.canThrow) {
704 OS << "Attribute::NoUnwind";
705 addComma = true;
707 if (intrinsic.isNoReturn) {
708 if (addComma)
709 OS << ",";
710 OS << "Attribute::NoReturn";
711 addComma = true;
713 if (intrinsic.isWillReturn) {
714 if (addComma)
715 OS << ",";
716 OS << "Attribute::WillReturn";
717 addComma = true;
719 if (intrinsic.isCold) {
720 if (addComma)
721 OS << ",";
722 OS << "Attribute::Cold";
723 addComma = true;
725 if (intrinsic.isNoDuplicate) {
726 if (addComma)
727 OS << ",";
728 OS << "Attribute::NoDuplicate";
729 addComma = true;
731 if (intrinsic.isConvergent) {
732 if (addComma)
733 OS << ",";
734 OS << "Attribute::Convergent";
735 addComma = true;
737 if (intrinsic.isSpeculatable) {
738 if (addComma)
739 OS << ",";
740 OS << "Attribute::Speculatable";
741 addComma = true;
744 switch (intrinsic.ModRef) {
745 case CodeGenIntrinsic::NoMem:
746 if (intrinsic.hasSideEffects)
747 break;
748 if (addComma)
749 OS << ",";
750 OS << "Attribute::ReadNone";
751 break;
752 case CodeGenIntrinsic::ReadArgMem:
753 if (addComma)
754 OS << ",";
755 OS << "Attribute::ReadOnly,";
756 OS << "Attribute::ArgMemOnly";
757 break;
758 case CodeGenIntrinsic::ReadMem:
759 if (addComma)
760 OS << ",";
761 OS << "Attribute::ReadOnly";
762 break;
763 case CodeGenIntrinsic::ReadInaccessibleMem:
764 if (addComma)
765 OS << ",";
766 OS << "Attribute::ReadOnly,";
767 OS << "Attribute::InaccessibleMemOnly";
768 break;
769 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem:
770 if (addComma)
771 OS << ",";
772 OS << "Attribute::ReadOnly,";
773 OS << "Attribute::InaccessibleMemOrArgMemOnly";
774 break;
775 case CodeGenIntrinsic::WriteArgMem:
776 if (addComma)
777 OS << ",";
778 OS << "Attribute::WriteOnly,";
779 OS << "Attribute::ArgMemOnly";
780 break;
781 case CodeGenIntrinsic::WriteMem:
782 if (addComma)
783 OS << ",";
784 OS << "Attribute::WriteOnly";
785 break;
786 case CodeGenIntrinsic::WriteInaccessibleMem:
787 if (addComma)
788 OS << ",";
789 OS << "Attribute::WriteOnly,";
790 OS << "Attribute::InaccessibleMemOnly";
791 break;
792 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem:
793 if (addComma)
794 OS << ",";
795 OS << "Attribute::WriteOnly,";
796 OS << "Attribute::InaccessibleMemOrArgMemOnly";
797 break;
798 case CodeGenIntrinsic::ReadWriteArgMem:
799 if (addComma)
800 OS << ",";
801 OS << "Attribute::ArgMemOnly";
802 break;
803 case CodeGenIntrinsic::ReadWriteInaccessibleMem:
804 if (addComma)
805 OS << ",";
806 OS << "Attribute::InaccessibleMemOnly";
807 break;
808 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem:
809 if (addComma)
810 OS << ",";
811 OS << "Attribute::InaccessibleMemOrArgMemOnly";
812 break;
813 case CodeGenIntrinsic::ReadWriteMem:
814 break;
816 OS << "};\n";
817 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
818 << "AttributeList::FunctionIndex, Atts);\n";
821 if (numAttrs) {
822 OS << " NumAttrs = " << numAttrs << ";\n";
823 OS << " break;\n";
824 OS << " }\n";
825 } else {
826 OS << " return AttributeList();\n";
827 OS << " }\n";
831 OS << " }\n";
832 OS << " }\n";
833 OS << " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n";
834 OS << "}\n";
835 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
838 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
839 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
840 StringRef CompilerName = (IsGCC ? "GCC" : "MS");
841 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
842 BIMTy BuiltinMap;
843 StringToOffsetTable Table;
844 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
845 const std::string &BuiltinName =
846 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
847 if (!BuiltinName.empty()) {
848 // Get the map for this target prefix.
849 std::map<std::string, std::string> &BIM =
850 BuiltinMap[Ints[i].TargetPrefix];
852 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
853 PrintFatalError(Ints[i].TheDef->getLoc(),
854 "Intrinsic '" + Ints[i].TheDef->getName() +
855 "': duplicate " + CompilerName + " builtin name!");
856 Table.GetOrAddStringOffset(BuiltinName);
860 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
861 OS << "// This is used by the C front-end. The builtin name is passed\n";
862 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
863 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
864 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
866 if (TargetOnly) {
867 OS << "static " << TargetPrefix << "Intrinsic::ID "
868 << "getIntrinsicFor" << CompilerName << "Builtin(const char "
869 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
870 } else {
871 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
872 << "Builtin(const char "
873 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
876 if (Table.Empty()) {
877 OS << " return ";
878 if (!TargetPrefix.empty())
879 OS << "(" << TargetPrefix << "Intrinsic::ID)";
880 OS << "Intrinsic::not_intrinsic;\n";
881 OS << "}\n";
882 OS << "#endif\n\n";
883 return;
886 OS << " static const char BuiltinNames[] = {\n";
887 Table.EmitCharArray(OS);
888 OS << " };\n\n";
890 OS << " struct BuiltinEntry {\n";
891 OS << " Intrinsic::ID IntrinID;\n";
892 OS << " unsigned StrTabOffset;\n";
893 OS << " const char *getName() const {\n";
894 OS << " return &BuiltinNames[StrTabOffset];\n";
895 OS << " }\n";
896 OS << " bool operator<(StringRef RHS) const {\n";
897 OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
898 OS << " }\n";
899 OS << " };\n";
901 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
903 // Note: this could emit significantly better code if we cared.
904 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
905 OS << " ";
906 if (!I->first.empty())
907 OS << "if (TargetPrefix == \"" << I->first << "\") ";
908 else
909 OS << "/* Target Independent Builtins */ ";
910 OS << "{\n";
912 // Emit the comparisons for this target prefix.
913 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n";
914 for (const auto &P : I->second) {
915 OS << " {Intrinsic::" << P.second << ", "
916 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
918 OS << " };\n";
919 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
920 OS << " std::end(" << I->first << "Names),\n";
921 OS << " BuiltinNameStr);\n";
922 OS << " if (I != std::end(" << I->first << "Names) &&\n";
923 OS << " I->getName() == BuiltinNameStr)\n";
924 OS << " return I->IntrinID;\n";
925 OS << " }\n";
927 OS << " return ";
928 if (!TargetPrefix.empty())
929 OS << "(" << TargetPrefix << "Intrinsic::ID)";
930 OS << "Intrinsic::not_intrinsic;\n";
931 OS << "}\n";
932 OS << "#endif\n\n";
935 void llvm::EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS,
936 bool TargetOnly) {
937 IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/true);
940 void llvm::EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS,
941 bool TargetOnly) {
942 IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/false);