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