1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
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
7 //===----------------------------------------------------------------------===//
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"
27 class IntrinsicEmitter
{
28 RecordKeeper
&Records
;
30 std::string TargetPrefix
;
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
,
44 void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable
&Ints
,
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
,
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
;
69 // Emit the enum information.
70 EmitEnumInfo(Ints
, OS
);
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
);
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"
103 "# define setjmp_undefined_for_msvc\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"
115 void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable
&Ints
,
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";
129 void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable
&Ints
,
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"
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";
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";
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";
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.
166 if (Ints
[i
].isOverloaded
)
167 OS
<< " | (1<<" << (i
+1)%8 << ')';
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";
176 // NOTE: This must be kept in synch with the copy in lib/IR/Function.cpp!
178 // Common values should be encoded with 0-15.
196 // Values from 16+ are only encodable with the inefficient encoding.
201 IIT_EMPTYSTRUCT
= 20,
211 IIT_HALF_VEC_ARG
= 30,
212 IIT_SAME_VEC_WIDTH_ARG
= 31,
215 IIT_VEC_OF_ANYPTRS_TO_ELT
= 34,
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();
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
);
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.
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
);
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
);
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
);
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"));
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
;
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
);
334 unsigned AddrSpace
= 0;
335 if (R
->isSubClassOf("LLVMQualPointerType")) {
336 AddrSpace
= R
->getValueAsInt("AddrSpace");
337 assert(AddrSpace
< 256 && "Address space exceeds 255");
340 Sig
.push_back(IIT_ANYPTR
);
341 Sig
.push_back(AddrSpace
);
343 Sig
.push_back(IIT_PTR
);
345 return EncodeFixedType(R
->getValueAsDef("ElTy"), ArgCodes
, NextArgCode
, Sig
,
350 if (MVT(VT
).isVector()) {
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*/);
385 switch (getValueType(R
->getValueAsDef("VT"))) {
388 UpdateArgCodes(R
->getValueAsDef("ElTy"), ArgCodes
, NumInserted
, Mapping
);
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
);
412 #if defined(_MSC_VER) && !defined(__clang__)
413 #pragma optimize("",on)
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
);
439 switch (Int
.IS
.RetVTs
.size()) {
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
,
456 for (unsigned i
= 0, e
= Int
.IS
.ParamTypeDefs
.size(); i
!= e
; ++i
)
457 EncodeFixedType(Int
.IS
.ParamTypeDefs
[i
], ArgCodes
, NextArgCode
, TypeSig
,
461 static void printIITEntry(raw_ostream
&OS
, unsigned char X
) {
465 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable
&Ints
,
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.
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) {
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) {
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
);
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
) {
521 // If the entry fit in the table, just emit it.
522 if (FixedEncodings
[i
] != ~0U) {
523 OS
<< "0x" << Twine::utohexstr(FixedEncodings
[i
]) << ", ";
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
) << ", ";
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
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
)
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
)
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
,
589 OS
<< "// Add parameter attributes that are not common to all intrinsics.\n";
590 OS
<< "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
592 OS
<< "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix
593 << "Intrinsic::ID id) {\n";
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
];
606 std::max(maxArgAttrs
, unsigned(intrinsic
.ArgumentAttributes
.size()));
607 unsigned &N
= UniqAttributes
[&intrinsic
];
609 assert(AttrNum
< 256 && "Too many unique attributes for table!");
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";
625 OS
<< " AttributeList AS[" << maxArgAttrs
+ 1 << "];\n";
626 OS
<< " unsigned NumAttrs = 0;\n";
627 OS
<< " if (id != 0) {\n";
628 OS
<< " switch(IntrinsicsToAttributesMap[id - ";
630 OS
<< "Intrinsic::num_intrinsics";
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();
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;
655 switch (intrinsic
.ArgumentAttributes
[ai
].second
) {
656 case CodeGenIntrinsic::NoCapture
:
659 OS
<< "Attribute::NoCapture";
662 case CodeGenIntrinsic::NoAlias
:
665 OS
<< "Attribute::NoAlias";
668 case CodeGenIntrinsic::Returned
:
671 OS
<< "Attribute::Returned";
674 case CodeGenIntrinsic::ReadOnly
:
677 OS
<< "Attribute::ReadOnly";
680 case CodeGenIntrinsic::WriteOnly
:
683 OS
<< "Attribute::WriteOnly";
686 case CodeGenIntrinsic::ReadNone
:
689 OS
<< "Attribute::ReadNone";
692 case CodeGenIntrinsic::ImmArg
:
695 OS
<< "Attribute::ImmArg";
701 } while (ai
!= ae
&& intrinsic
.ArgumentAttributes
[ai
].first
== argNo
);
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";
719 if (intrinsic
.isNoReturn
) {
722 OS
<< "Attribute::NoReturn";
725 if (intrinsic
.isWillReturn
) {
728 OS
<< "Attribute::WillReturn";
731 if (intrinsic
.isCold
) {
734 OS
<< "Attribute::Cold";
737 if (intrinsic
.isNoDuplicate
) {
740 OS
<< "Attribute::NoDuplicate";
743 if (intrinsic
.isConvergent
) {
746 OS
<< "Attribute::Convergent";
749 if (intrinsic
.isSpeculatable
) {
752 OS
<< "Attribute::Speculatable";
756 switch (intrinsic
.ModRef
) {
757 case CodeGenIntrinsic::NoMem
:
758 if (intrinsic
.hasSideEffects
)
762 OS
<< "Attribute::ReadNone";
764 case CodeGenIntrinsic::ReadArgMem
:
767 OS
<< "Attribute::ReadOnly,";
768 OS
<< "Attribute::ArgMemOnly";
770 case CodeGenIntrinsic::ReadMem
:
773 OS
<< "Attribute::ReadOnly";
775 case CodeGenIntrinsic::ReadInaccessibleMem
:
778 OS
<< "Attribute::ReadOnly,";
779 OS
<< "Attribute::InaccessibleMemOnly";
781 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem
:
784 OS
<< "Attribute::ReadOnly,";
785 OS
<< "Attribute::InaccessibleMemOrArgMemOnly";
787 case CodeGenIntrinsic::WriteArgMem
:
790 OS
<< "Attribute::WriteOnly,";
791 OS
<< "Attribute::ArgMemOnly";
793 case CodeGenIntrinsic::WriteMem
:
796 OS
<< "Attribute::WriteOnly";
798 case CodeGenIntrinsic::WriteInaccessibleMem
:
801 OS
<< "Attribute::WriteOnly,";
802 OS
<< "Attribute::InaccessibleMemOnly";
804 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem
:
807 OS
<< "Attribute::WriteOnly,";
808 OS
<< "Attribute::InaccessibleMemOrArgMemOnly";
810 case CodeGenIntrinsic::ReadWriteArgMem
:
813 OS
<< "Attribute::ArgMemOnly";
815 case CodeGenIntrinsic::ReadWriteInaccessibleMem
:
818 OS
<< "Attribute::InaccessibleMemOnly";
820 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem
:
823 OS
<< "Attribute::InaccessibleMemOrArgMemOnly";
825 case CodeGenIntrinsic::ReadWriteMem
:
829 OS
<< " AS[" << numAttrs
++ << "] = AttributeList::get(C, "
830 << "AttributeList::FunctionIndex, Atts);\n";
834 OS
<< " NumAttrs = " << numAttrs
<< ";\n";
838 OS
<< " return AttributeList();\n";
845 OS
<< " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\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
;
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";
879 OS
<< "static " << TargetPrefix
<< "Intrinsic::ID "
880 << "getIntrinsicFor" << CompilerName
<< "Builtin(const char "
881 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
883 OS
<< "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
884 << "Builtin(const char "
885 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
890 if (!TargetPrefix
.empty())
891 OS
<< "(" << TargetPrefix
<< "Intrinsic::ID)";
892 OS
<< "Intrinsic::not_intrinsic;\n";
898 OS
<< " static const char BuiltinNames[] = {\n";
899 Table
.EmitCharArray(OS
);
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";
908 OS
<< " bool operator<(StringRef RHS) const {\n";
909 OS
<< " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\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
){
918 if (!I
->first
.empty())
919 OS
<< "if (TargetPrefix == \"" << I
->first
<< "\") ";
921 OS
<< "/* Target Independent Builtins */ ";
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";
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";
940 if (!TargetPrefix
.empty())
941 OS
<< "(" << TargetPrefix
<< "Intrinsic::ID)";
942 OS
<< "Intrinsic::not_intrinsic;\n";
947 void llvm::EmitIntrinsicEnums(RecordKeeper
&RK
, raw_ostream
&OS
,
949 IntrinsicEmitter(RK
, TargetOnly
).run(OS
, /*Enums=*/true);
952 void llvm::EmitIntrinsicImpl(RecordKeeper
&RK
, raw_ostream
&OS
,
954 IntrinsicEmitter(RK
, TargetOnly
).run(OS
, /*Enums=*/false);