[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / utils / TableGen / IntrinsicEmitter.cpp
blobe01f91c20456c37b845ae729e6e753d745cdb4c9
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,
224 IIT_SCALABLE_VEC = 43,
225 IIT_SUBDIVIDE2_ARG = 44,
226 IIT_SUBDIVIDE4_ARG = 45,
227 IIT_VEC_OF_BITCASTS_TO_INT = 46
230 static void EncodeFixedValueType(MVT::SimpleValueType VT,
231 std::vector<unsigned char> &Sig) {
232 if (MVT(VT).isInteger()) {
233 unsigned BitWidth = MVT(VT).getSizeInBits();
234 switch (BitWidth) {
235 default: PrintFatalError("unhandled integer type width in intrinsic!");
236 case 1: return Sig.push_back(IIT_I1);
237 case 8: return Sig.push_back(IIT_I8);
238 case 16: return Sig.push_back(IIT_I16);
239 case 32: return Sig.push_back(IIT_I32);
240 case 64: return Sig.push_back(IIT_I64);
241 case 128: return Sig.push_back(IIT_I128);
245 switch (VT) {
246 default: PrintFatalError("unhandled MVT in intrinsic!");
247 case MVT::f16: return Sig.push_back(IIT_F16);
248 case MVT::f32: return Sig.push_back(IIT_F32);
249 case MVT::f64: return Sig.push_back(IIT_F64);
250 case MVT::f128: return Sig.push_back(IIT_F128);
251 case MVT::token: return Sig.push_back(IIT_TOKEN);
252 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
253 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
254 // MVT::OtherVT is used to mean the empty struct type here.
255 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
256 // MVT::isVoid is used to represent varargs here.
257 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
261 #if defined(_MSC_VER) && !defined(__clang__)
262 #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
263 #endif
265 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
266 unsigned &NextArgCode,
267 std::vector<unsigned char> &Sig,
268 ArrayRef<unsigned char> Mapping) {
270 if (R->isSubClassOf("LLVMMatchType")) {
271 unsigned Number = Mapping[R->getValueAsInt("Number")];
272 assert(Number < ArgCodes.size() && "Invalid matching number!");
273 if (R->isSubClassOf("LLVMExtendedType"))
274 Sig.push_back(IIT_EXTEND_ARG);
275 else if (R->isSubClassOf("LLVMTruncatedType"))
276 Sig.push_back(IIT_TRUNC_ARG);
277 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
278 Sig.push_back(IIT_HALF_VEC_ARG);
279 else if (R->isSubClassOf("LLVMScalarOrSameVectorWidth")) {
280 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
281 Sig.push_back((Number << 3) | ArgCodes[Number]);
282 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
283 EncodeFixedValueType(VT, Sig);
284 return;
286 else if (R->isSubClassOf("LLVMPointerTo"))
287 Sig.push_back(IIT_PTR_TO_ARG);
288 else if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
289 Sig.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT);
290 // Encode overloaded ArgNo
291 Sig.push_back(NextArgCode++);
292 // Encode LLVMMatchType<Number> ArgNo
293 Sig.push_back(Number);
294 return;
295 } else if (R->isSubClassOf("LLVMPointerToElt"))
296 Sig.push_back(IIT_PTR_TO_ELT);
297 else if (R->isSubClassOf("LLVMVectorElementType"))
298 Sig.push_back(IIT_VEC_ELEMENT);
299 else if (R->isSubClassOf("LLVMSubdivide2VectorType"))
300 Sig.push_back(IIT_SUBDIVIDE2_ARG);
301 else if (R->isSubClassOf("LLVMSubdivide4VectorType"))
302 Sig.push_back(IIT_SUBDIVIDE4_ARG);
303 else if (R->isSubClassOf("LLVMVectorOfBitcastsToInt"))
304 Sig.push_back(IIT_VEC_OF_BITCASTS_TO_INT);
305 else
306 Sig.push_back(IIT_ARG);
307 return Sig.push_back((Number << 3) | 7 /*IITDescriptor::AK_MatchType*/);
310 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
312 unsigned Tmp = 0;
313 switch (VT) {
314 default: break;
315 case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH;
316 case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH;
317 case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH;
318 case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH;
319 case MVT::Any: {
320 // If this is an "any" valuetype, then the type is the type of the next
321 // type in the list specified to getIntrinsic().
322 Sig.push_back(IIT_ARG);
324 // Figure out what arg # this is consuming, and remember what kind it was.
325 assert(NextArgCode < ArgCodes.size() && ArgCodes[NextArgCode] == Tmp &&
326 "Invalid or no ArgCode associated with overloaded VT!");
327 unsigned ArgNo = NextArgCode++;
329 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
330 return Sig.push_back((ArgNo << 3) | Tmp);
333 case MVT::iPTR: {
334 unsigned AddrSpace = 0;
335 if (R->isSubClassOf("LLVMQualPointerType")) {
336 AddrSpace = R->getValueAsInt("AddrSpace");
337 assert(AddrSpace < 256 && "Address space exceeds 255");
339 if (AddrSpace) {
340 Sig.push_back(IIT_ANYPTR);
341 Sig.push_back(AddrSpace);
342 } else {
343 Sig.push_back(IIT_PTR);
345 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, NextArgCode, Sig,
346 Mapping);
350 if (MVT(VT).isVector()) {
351 MVT VVT = VT;
352 if (VVT.isScalableVector())
353 Sig.push_back(IIT_SCALABLE_VEC);
354 switch (VVT.getVectorNumElements()) {
355 default: PrintFatalError("unhandled vector type width in intrinsic!");
356 case 1: Sig.push_back(IIT_V1); break;
357 case 2: Sig.push_back(IIT_V2); break;
358 case 4: Sig.push_back(IIT_V4); break;
359 case 8: Sig.push_back(IIT_V8); break;
360 case 16: Sig.push_back(IIT_V16); break;
361 case 32: Sig.push_back(IIT_V32); break;
362 case 64: Sig.push_back(IIT_V64); break;
363 case 512: Sig.push_back(IIT_V512); break;
364 case 1024: Sig.push_back(IIT_V1024); break;
367 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
370 EncodeFixedValueType(VT, Sig);
373 static void UpdateArgCodes(Record *R, std::vector<unsigned char> &ArgCodes,
374 unsigned int &NumInserted,
375 SmallVectorImpl<unsigned char> &Mapping) {
376 if (R->isSubClassOf("LLVMMatchType")) {
377 if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
378 ArgCodes.push_back(3 /*vAny*/);
379 ++NumInserted;
381 return;
384 unsigned Tmp = 0;
385 switch (getValueType(R->getValueAsDef("VT"))) {
386 default: break;
387 case MVT::iPTR:
388 UpdateArgCodes(R->getValueAsDef("ElTy"), ArgCodes, NumInserted, Mapping);
389 break;
390 case MVT::iPTRAny:
391 ++Tmp;
392 LLVM_FALLTHROUGH;
393 case MVT::vAny:
394 ++Tmp;
395 LLVM_FALLTHROUGH;
396 case MVT::fAny:
397 ++Tmp;
398 LLVM_FALLTHROUGH;
399 case MVT::iAny:
400 ++Tmp;
401 LLVM_FALLTHROUGH;
402 case MVT::Any:
403 unsigned OriginalIdx = ArgCodes.size() - NumInserted;
404 assert(OriginalIdx >= Mapping.size());
405 Mapping.resize(OriginalIdx+1);
406 Mapping[OriginalIdx] = ArgCodes.size();
407 ArgCodes.push_back(Tmp);
408 break;
412 #if defined(_MSC_VER) && !defined(__clang__)
413 #pragma optimize("",on)
414 #endif
416 /// ComputeFixedEncoding - If we can encode the type signature for this
417 /// intrinsic into 32 bits, return it. If not, return ~0U.
418 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
419 std::vector<unsigned char> &TypeSig) {
420 std::vector<unsigned char> ArgCodes;
422 // Add codes for any overloaded result VTs.
423 unsigned int NumInserted = 0;
424 SmallVector<unsigned char, 8> ArgMapping;
425 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
426 UpdateArgCodes(Int.IS.RetTypeDefs[i], ArgCodes, NumInserted, ArgMapping);
428 // Add codes for any overloaded operand VTs.
429 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
430 UpdateArgCodes(Int.IS.ParamTypeDefs[i], ArgCodes, NumInserted, ArgMapping);
432 unsigned NextArgCode = 0;
433 if (Int.IS.RetVTs.empty())
434 TypeSig.push_back(IIT_Done);
435 else if (Int.IS.RetVTs.size() == 1 &&
436 Int.IS.RetVTs[0] == MVT::isVoid)
437 TypeSig.push_back(IIT_Done);
438 else {
439 switch (Int.IS.RetVTs.size()) {
440 case 1: break;
441 case 2: TypeSig.push_back(IIT_STRUCT2); break;
442 case 3: TypeSig.push_back(IIT_STRUCT3); break;
443 case 4: TypeSig.push_back(IIT_STRUCT4); break;
444 case 5: TypeSig.push_back(IIT_STRUCT5); break;
445 case 6: TypeSig.push_back(IIT_STRUCT6); break;
446 case 7: TypeSig.push_back(IIT_STRUCT7); break;
447 case 8: TypeSig.push_back(IIT_STRUCT8); break;
448 default: llvm_unreachable("Unhandled case in struct");
451 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
452 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, NextArgCode, TypeSig,
453 ArgMapping);
456 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
457 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, NextArgCode, TypeSig,
458 ArgMapping);
461 static void printIITEntry(raw_ostream &OS, unsigned char X) {
462 OS << (unsigned)X;
465 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
466 raw_ostream &OS) {
467 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
468 // capture it in this vector, otherwise store a ~0U.
469 std::vector<unsigned> FixedEncodings;
471 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
473 std::vector<unsigned char> TypeSig;
475 // Compute the unique argument type info.
476 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
477 // Get the signature for the intrinsic.
478 TypeSig.clear();
479 ComputeFixedEncoding(Ints[i], TypeSig);
481 // Check to see if we can encode it into a 32-bit word. We can only encode
482 // 8 nibbles into a 32-bit word.
483 if (TypeSig.size() <= 8) {
484 bool Failed = false;
485 unsigned Result = 0;
486 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
487 // If we had an unencodable argument, bail out.
488 if (TypeSig[i] > 15) {
489 Failed = true;
490 break;
492 Result = (Result << 4) | TypeSig[e-i-1];
495 // If this could be encoded into a 31-bit word, return it.
496 if (!Failed && (Result >> 31) == 0) {
497 FixedEncodings.push_back(Result);
498 continue;
502 // Otherwise, we're going to unique the sequence into the
503 // LongEncodingTable, and use its offset in the 32-bit table instead.
504 LongEncodingTable.add(TypeSig);
506 // This is a placehold that we'll replace after the table is laid out.
507 FixedEncodings.push_back(~0U);
510 LongEncodingTable.layout();
512 OS << "// Global intrinsic function declaration type table.\n";
513 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
515 OS << "static const unsigned IIT_Table[] = {\n ";
517 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
518 if ((i & 7) == 7)
519 OS << "\n ";
521 // If the entry fit in the table, just emit it.
522 if (FixedEncodings[i] != ~0U) {
523 OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", ";
524 continue;
527 TypeSig.clear();
528 ComputeFixedEncoding(Ints[i], TypeSig);
531 // Otherwise, emit the offset into the long encoding table. We emit it this
532 // way so that it is easier to read the offset in the .def file.
533 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
536 OS << "0\n};\n\n";
538 // Emit the shared table of register lists.
539 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
540 if (!LongEncodingTable.empty())
541 LongEncodingTable.emit(OS, printIITEntry);
542 OS << " 255\n};\n\n";
544 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
547 namespace {
548 struct AttributeComparator {
549 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
550 // Sort throwing intrinsics after non-throwing intrinsics.
551 if (L->canThrow != R->canThrow)
552 return R->canThrow;
554 if (L->isNoDuplicate != R->isNoDuplicate)
555 return R->isNoDuplicate;
557 if (L->isNoReturn != R->isNoReturn)
558 return R->isNoReturn;
560 if (L->isWillReturn != R->isWillReturn)
561 return R->isWillReturn;
563 if (L->isCold != R->isCold)
564 return R->isCold;
566 if (L->isConvergent != R->isConvergent)
567 return R->isConvergent;
569 if (L->isSpeculatable != R->isSpeculatable)
570 return R->isSpeculatable;
572 if (L->hasSideEffects != R->hasSideEffects)
573 return R->hasSideEffects;
575 // Try to order by readonly/readnone attribute.
576 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
577 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
578 if (LK != RK) return (LK > RK);
579 // Order by argument attributes.
580 // This is reliable because each side is already sorted internally.
581 return (L->ArgumentAttributes < R->ArgumentAttributes);
584 } // End anonymous namespace
586 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
587 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
588 raw_ostream &OS) {
589 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
590 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
591 if (TargetOnly)
592 OS << "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix
593 << "Intrinsic::ID id) {\n";
594 else
595 OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
597 // Compute the maximum number of attribute arguments and the map
598 typedef std::map<const CodeGenIntrinsic*, unsigned,
599 AttributeComparator> UniqAttrMapTy;
600 UniqAttrMapTy UniqAttributes;
601 unsigned maxArgAttrs = 0;
602 unsigned AttrNum = 0;
603 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
604 const CodeGenIntrinsic &intrinsic = Ints[i];
605 maxArgAttrs =
606 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
607 unsigned &N = UniqAttributes[&intrinsic];
608 if (N) continue;
609 assert(AttrNum < 256 && "Too many unique attributes for table!");
610 N = ++AttrNum;
613 // Emit an array of AttributeList. Most intrinsics will have at least one
614 // entry, for the function itself (index ~1), which is usually nounwind.
615 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
617 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
618 const CodeGenIntrinsic &intrinsic = Ints[i];
620 OS << " " << UniqAttributes[&intrinsic] << ", // "
621 << intrinsic.Name << "\n";
623 OS << " };\n\n";
625 OS << " AttributeList AS[" << maxArgAttrs + 1 << "];\n";
626 OS << " unsigned NumAttrs = 0;\n";
627 OS << " if (id != 0) {\n";
628 OS << " switch(IntrinsicsToAttributesMap[id - ";
629 if (TargetOnly)
630 OS << "Intrinsic::num_intrinsics";
631 else
632 OS << "1";
633 OS << "]) {\n";
634 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
635 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
636 E = UniqAttributes.end(); I != E; ++I) {
637 OS << " case " << I->second << ": {\n";
639 const CodeGenIntrinsic &intrinsic = *(I->first);
641 // Keep track of the number of attributes we're writing out.
642 unsigned numAttrs = 0;
644 // The argument attributes are alreadys sorted by argument index.
645 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
646 if (ae) {
647 while (ai != ae) {
648 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
649 unsigned attrIdx = argNo + 1; // Must match AttributeList::FirstArgIndex
651 OS << " const Attribute::AttrKind AttrParam" << attrIdx << "[]= {";
652 bool addComma = false;
654 do {
655 switch (intrinsic.ArgumentAttributes[ai].second) {
656 case CodeGenIntrinsic::NoCapture:
657 if (addComma)
658 OS << ",";
659 OS << "Attribute::NoCapture";
660 addComma = true;
661 break;
662 case CodeGenIntrinsic::NoAlias:
663 if (addComma)
664 OS << ",";
665 OS << "Attribute::NoAlias";
666 addComma = true;
667 break;
668 case CodeGenIntrinsic::Returned:
669 if (addComma)
670 OS << ",";
671 OS << "Attribute::Returned";
672 addComma = true;
673 break;
674 case CodeGenIntrinsic::ReadOnly:
675 if (addComma)
676 OS << ",";
677 OS << "Attribute::ReadOnly";
678 addComma = true;
679 break;
680 case CodeGenIntrinsic::WriteOnly:
681 if (addComma)
682 OS << ",";
683 OS << "Attribute::WriteOnly";
684 addComma = true;
685 break;
686 case CodeGenIntrinsic::ReadNone:
687 if (addComma)
688 OS << ",";
689 OS << "Attribute::ReadNone";
690 addComma = true;
691 break;
692 case CodeGenIntrinsic::ImmArg:
693 if (addComma)
694 OS << ',';
695 OS << "Attribute::ImmArg";
696 addComma = true;
697 break;
700 ++ai;
701 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
702 OS << "};\n";
703 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
704 << attrIdx << ", AttrParam" << attrIdx << ");\n";
708 if (!intrinsic.canThrow ||
709 (intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem && !intrinsic.hasSideEffects) ||
710 intrinsic.isNoReturn || intrinsic.isWillReturn || intrinsic.isCold ||
711 intrinsic.isNoDuplicate || intrinsic.isConvergent ||
712 intrinsic.isSpeculatable) {
713 OS << " const Attribute::AttrKind Atts[] = {";
714 bool addComma = false;
715 if (!intrinsic.canThrow) {
716 OS << "Attribute::NoUnwind";
717 addComma = true;
719 if (intrinsic.isNoReturn) {
720 if (addComma)
721 OS << ",";
722 OS << "Attribute::NoReturn";
723 addComma = true;
725 if (intrinsic.isWillReturn) {
726 if (addComma)
727 OS << ",";
728 OS << "Attribute::WillReturn";
729 addComma = true;
731 if (intrinsic.isCold) {
732 if (addComma)
733 OS << ",";
734 OS << "Attribute::Cold";
735 addComma = true;
737 if (intrinsic.isNoDuplicate) {
738 if (addComma)
739 OS << ",";
740 OS << "Attribute::NoDuplicate";
741 addComma = true;
743 if (intrinsic.isConvergent) {
744 if (addComma)
745 OS << ",";
746 OS << "Attribute::Convergent";
747 addComma = true;
749 if (intrinsic.isSpeculatable) {
750 if (addComma)
751 OS << ",";
752 OS << "Attribute::Speculatable";
753 addComma = true;
756 switch (intrinsic.ModRef) {
757 case CodeGenIntrinsic::NoMem:
758 if (intrinsic.hasSideEffects)
759 break;
760 if (addComma)
761 OS << ",";
762 OS << "Attribute::ReadNone";
763 break;
764 case CodeGenIntrinsic::ReadArgMem:
765 if (addComma)
766 OS << ",";
767 OS << "Attribute::ReadOnly,";
768 OS << "Attribute::ArgMemOnly";
769 break;
770 case CodeGenIntrinsic::ReadMem:
771 if (addComma)
772 OS << ",";
773 OS << "Attribute::ReadOnly";
774 break;
775 case CodeGenIntrinsic::ReadInaccessibleMem:
776 if (addComma)
777 OS << ",";
778 OS << "Attribute::ReadOnly,";
779 OS << "Attribute::InaccessibleMemOnly";
780 break;
781 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem:
782 if (addComma)
783 OS << ",";
784 OS << "Attribute::ReadOnly,";
785 OS << "Attribute::InaccessibleMemOrArgMemOnly";
786 break;
787 case CodeGenIntrinsic::WriteArgMem:
788 if (addComma)
789 OS << ",";
790 OS << "Attribute::WriteOnly,";
791 OS << "Attribute::ArgMemOnly";
792 break;
793 case CodeGenIntrinsic::WriteMem:
794 if (addComma)
795 OS << ",";
796 OS << "Attribute::WriteOnly";
797 break;
798 case CodeGenIntrinsic::WriteInaccessibleMem:
799 if (addComma)
800 OS << ",";
801 OS << "Attribute::WriteOnly,";
802 OS << "Attribute::InaccessibleMemOnly";
803 break;
804 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem:
805 if (addComma)
806 OS << ",";
807 OS << "Attribute::WriteOnly,";
808 OS << "Attribute::InaccessibleMemOrArgMemOnly";
809 break;
810 case CodeGenIntrinsic::ReadWriteArgMem:
811 if (addComma)
812 OS << ",";
813 OS << "Attribute::ArgMemOnly";
814 break;
815 case CodeGenIntrinsic::ReadWriteInaccessibleMem:
816 if (addComma)
817 OS << ",";
818 OS << "Attribute::InaccessibleMemOnly";
819 break;
820 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem:
821 if (addComma)
822 OS << ",";
823 OS << "Attribute::InaccessibleMemOrArgMemOnly";
824 break;
825 case CodeGenIntrinsic::ReadWriteMem:
826 break;
828 OS << "};\n";
829 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
830 << "AttributeList::FunctionIndex, Atts);\n";
833 if (numAttrs) {
834 OS << " NumAttrs = " << numAttrs << ";\n";
835 OS << " break;\n";
836 OS << " }\n";
837 } else {
838 OS << " return AttributeList();\n";
839 OS << " }\n";
843 OS << " }\n";
844 OS << " }\n";
845 OS << " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n";
846 OS << "}\n";
847 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
850 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
851 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
852 StringRef CompilerName = (IsGCC ? "GCC" : "MS");
853 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
854 BIMTy BuiltinMap;
855 StringToOffsetTable Table;
856 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
857 const std::string &BuiltinName =
858 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
859 if (!BuiltinName.empty()) {
860 // Get the map for this target prefix.
861 std::map<std::string, std::string> &BIM =
862 BuiltinMap[Ints[i].TargetPrefix];
864 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
865 PrintFatalError(Ints[i].TheDef->getLoc(),
866 "Intrinsic '" + Ints[i].TheDef->getName() +
867 "': duplicate " + CompilerName + " builtin name!");
868 Table.GetOrAddStringOffset(BuiltinName);
872 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
873 OS << "// This is used by the C front-end. The builtin name is passed\n";
874 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
875 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
876 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
878 if (TargetOnly) {
879 OS << "static " << TargetPrefix << "Intrinsic::ID "
880 << "getIntrinsicFor" << CompilerName << "Builtin(const char "
881 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
882 } else {
883 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
884 << "Builtin(const char "
885 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
888 if (Table.Empty()) {
889 OS << " return ";
890 if (!TargetPrefix.empty())
891 OS << "(" << TargetPrefix << "Intrinsic::ID)";
892 OS << "Intrinsic::not_intrinsic;\n";
893 OS << "}\n";
894 OS << "#endif\n\n";
895 return;
898 OS << " static const char BuiltinNames[] = {\n";
899 Table.EmitCharArray(OS);
900 OS << " };\n\n";
902 OS << " struct BuiltinEntry {\n";
903 OS << " Intrinsic::ID IntrinID;\n";
904 OS << " unsigned StrTabOffset;\n";
905 OS << " const char *getName() const {\n";
906 OS << " return &BuiltinNames[StrTabOffset];\n";
907 OS << " }\n";
908 OS << " bool operator<(StringRef RHS) const {\n";
909 OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
910 OS << " }\n";
911 OS << " };\n";
913 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
915 // Note: this could emit significantly better code if we cared.
916 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
917 OS << " ";
918 if (!I->first.empty())
919 OS << "if (TargetPrefix == \"" << I->first << "\") ";
920 else
921 OS << "/* Target Independent Builtins */ ";
922 OS << "{\n";
924 // Emit the comparisons for this target prefix.
925 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n";
926 for (const auto &P : I->second) {
927 OS << " {Intrinsic::" << P.second << ", "
928 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
930 OS << " };\n";
931 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
932 OS << " std::end(" << I->first << "Names),\n";
933 OS << " BuiltinNameStr);\n";
934 OS << " if (I != std::end(" << I->first << "Names) &&\n";
935 OS << " I->getName() == BuiltinNameStr)\n";
936 OS << " return I->IntrinID;\n";
937 OS << " }\n";
939 OS << " return ";
940 if (!TargetPrefix.empty())
941 OS << "(" << TargetPrefix << "Intrinsic::ID)";
942 OS << "Intrinsic::not_intrinsic;\n";
943 OS << "}\n";
944 OS << "#endif\n\n";
947 void llvm::EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS,
948 bool TargetOnly) {
949 IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/true);
952 void llvm::EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS,
953 bool TargetOnly) {
954 IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/false);