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
227 static void EncodeFixedValueType(MVT::SimpleValueType VT
,
228 std::vector
<unsigned char> &Sig
) {
229 if (MVT(VT
).isInteger()) {
230 unsigned BitWidth
= MVT(VT
).getSizeInBits();
232 default: PrintFatalError("unhandled integer type width in intrinsic!");
233 case 1: return Sig
.push_back(IIT_I1
);
234 case 8: return Sig
.push_back(IIT_I8
);
235 case 16: return Sig
.push_back(IIT_I16
);
236 case 32: return Sig
.push_back(IIT_I32
);
237 case 64: return Sig
.push_back(IIT_I64
);
238 case 128: return Sig
.push_back(IIT_I128
);
243 default: PrintFatalError("unhandled MVT in intrinsic!");
244 case MVT::f16
: return Sig
.push_back(IIT_F16
);
245 case MVT::f32
: return Sig
.push_back(IIT_F32
);
246 case MVT::f64
: return Sig
.push_back(IIT_F64
);
247 case MVT::f128
: return Sig
.push_back(IIT_F128
);
248 case MVT::token
: return Sig
.push_back(IIT_TOKEN
);
249 case MVT::Metadata
: return Sig
.push_back(IIT_METADATA
);
250 case MVT::x86mmx
: return Sig
.push_back(IIT_MMX
);
251 // MVT::OtherVT is used to mean the empty struct type here.
252 case MVT::Other
: return Sig
.push_back(IIT_EMPTYSTRUCT
);
253 // MVT::isVoid is used to represent varargs here.
254 case MVT::isVoid
: return Sig
.push_back(IIT_VARARG
);
258 #if defined(_MSC_VER) && !defined(__clang__)
259 #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
262 static void EncodeFixedType(Record
*R
, std::vector
<unsigned char> &ArgCodes
,
263 unsigned &NextArgCode
,
264 std::vector
<unsigned char> &Sig
,
265 ArrayRef
<unsigned char> Mapping
) {
267 if (R
->isSubClassOf("LLVMMatchType")) {
268 unsigned Number
= Mapping
[R
->getValueAsInt("Number")];
269 assert(Number
< ArgCodes
.size() && "Invalid matching number!");
270 if (R
->isSubClassOf("LLVMExtendedType"))
271 Sig
.push_back(IIT_EXTEND_ARG
);
272 else if (R
->isSubClassOf("LLVMTruncatedType"))
273 Sig
.push_back(IIT_TRUNC_ARG
);
274 else if (R
->isSubClassOf("LLVMHalfElementsVectorType"))
275 Sig
.push_back(IIT_HALF_VEC_ARG
);
276 else if (R
->isSubClassOf("LLVMScalarOrSameVectorWidth")) {
277 Sig
.push_back(IIT_SAME_VEC_WIDTH_ARG
);
278 Sig
.push_back((Number
<< 3) | ArgCodes
[Number
]);
279 MVT::SimpleValueType VT
= getValueType(R
->getValueAsDef("ElTy"));
280 EncodeFixedValueType(VT
, Sig
);
283 else if (R
->isSubClassOf("LLVMPointerTo"))
284 Sig
.push_back(IIT_PTR_TO_ARG
);
285 else if (R
->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
286 Sig
.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT
);
287 // Encode overloaded ArgNo
288 Sig
.push_back(NextArgCode
++);
289 // Encode LLVMMatchType<Number> ArgNo
290 Sig
.push_back(Number
);
292 } else if (R
->isSubClassOf("LLVMPointerToElt"))
293 Sig
.push_back(IIT_PTR_TO_ELT
);
294 else if (R
->isSubClassOf("LLVMVectorElementType"))
295 Sig
.push_back(IIT_VEC_ELEMENT
);
297 Sig
.push_back(IIT_ARG
);
298 return Sig
.push_back((Number
<< 3) | 7 /*IITDescriptor::AK_MatchType*/);
301 MVT::SimpleValueType VT
= getValueType(R
->getValueAsDef("VT"));
306 case MVT::iPTRAny
: ++Tmp
; LLVM_FALLTHROUGH
;
307 case MVT::vAny
: ++Tmp
; LLVM_FALLTHROUGH
;
308 case MVT::fAny
: ++Tmp
; LLVM_FALLTHROUGH
;
309 case MVT::iAny
: ++Tmp
; LLVM_FALLTHROUGH
;
311 // If this is an "any" valuetype, then the type is the type of the next
312 // type in the list specified to getIntrinsic().
313 Sig
.push_back(IIT_ARG
);
315 // Figure out what arg # this is consuming, and remember what kind it was.
316 assert(NextArgCode
< ArgCodes
.size() && ArgCodes
[NextArgCode
] == Tmp
&&
317 "Invalid or no ArgCode associated with overloaded VT!");
318 unsigned ArgNo
= NextArgCode
++;
320 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
321 return Sig
.push_back((ArgNo
<< 3) | Tmp
);
325 unsigned AddrSpace
= 0;
326 if (R
->isSubClassOf("LLVMQualPointerType")) {
327 AddrSpace
= R
->getValueAsInt("AddrSpace");
328 assert(AddrSpace
< 256 && "Address space exceeds 255");
331 Sig
.push_back(IIT_ANYPTR
);
332 Sig
.push_back(AddrSpace
);
334 Sig
.push_back(IIT_PTR
);
336 return EncodeFixedType(R
->getValueAsDef("ElTy"), ArgCodes
, NextArgCode
, Sig
,
341 if (MVT(VT
).isVector()) {
343 if (VVT
.isScalableVector())
344 Sig
.push_back(IIT_SCALABLE_VEC
);
345 switch (VVT
.getVectorNumElements()) {
346 default: PrintFatalError("unhandled vector type width in intrinsic!");
347 case 1: Sig
.push_back(IIT_V1
); break;
348 case 2: Sig
.push_back(IIT_V2
); break;
349 case 4: Sig
.push_back(IIT_V4
); break;
350 case 8: Sig
.push_back(IIT_V8
); break;
351 case 16: Sig
.push_back(IIT_V16
); break;
352 case 32: Sig
.push_back(IIT_V32
); break;
353 case 64: Sig
.push_back(IIT_V64
); break;
354 case 512: Sig
.push_back(IIT_V512
); break;
355 case 1024: Sig
.push_back(IIT_V1024
); break;
358 return EncodeFixedValueType(VVT
.getVectorElementType().SimpleTy
, Sig
);
361 EncodeFixedValueType(VT
, Sig
);
364 static void UpdateArgCodes(Record
*R
, std::vector
<unsigned char> &ArgCodes
,
365 unsigned int &NumInserted
,
366 SmallVectorImpl
<unsigned char> &Mapping
) {
367 if (R
->isSubClassOf("LLVMMatchType")) {
368 if (R
->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
369 ArgCodes
.push_back(3 /*vAny*/);
376 switch (getValueType(R
->getValueAsDef("VT"))) {
379 UpdateArgCodes(R
->getValueAsDef("ElTy"), ArgCodes
, NumInserted
, Mapping
);
394 unsigned OriginalIdx
= ArgCodes
.size() - NumInserted
;
395 assert(OriginalIdx
>= Mapping
.size());
396 Mapping
.resize(OriginalIdx
+1);
397 Mapping
[OriginalIdx
] = ArgCodes
.size();
398 ArgCodes
.push_back(Tmp
);
403 #if defined(_MSC_VER) && !defined(__clang__)
404 #pragma optimize("",on)
407 /// ComputeFixedEncoding - If we can encode the type signature for this
408 /// intrinsic into 32 bits, return it. If not, return ~0U.
409 static void ComputeFixedEncoding(const CodeGenIntrinsic
&Int
,
410 std::vector
<unsigned char> &TypeSig
) {
411 std::vector
<unsigned char> ArgCodes
;
413 // Add codes for any overloaded result VTs.
414 unsigned int NumInserted
= 0;
415 SmallVector
<unsigned char, 8> ArgMapping
;
416 for (unsigned i
= 0, e
= Int
.IS
.RetVTs
.size(); i
!= e
; ++i
)
417 UpdateArgCodes(Int
.IS
.RetTypeDefs
[i
], ArgCodes
, NumInserted
, ArgMapping
);
419 // Add codes for any overloaded operand VTs.
420 for (unsigned i
= 0, e
= Int
.IS
.ParamTypeDefs
.size(); i
!= e
; ++i
)
421 UpdateArgCodes(Int
.IS
.ParamTypeDefs
[i
], ArgCodes
, NumInserted
, ArgMapping
);
423 unsigned NextArgCode
= 0;
424 if (Int
.IS
.RetVTs
.empty())
425 TypeSig
.push_back(IIT_Done
);
426 else if (Int
.IS
.RetVTs
.size() == 1 &&
427 Int
.IS
.RetVTs
[0] == MVT::isVoid
)
428 TypeSig
.push_back(IIT_Done
);
430 switch (Int
.IS
.RetVTs
.size()) {
432 case 2: TypeSig
.push_back(IIT_STRUCT2
); break;
433 case 3: TypeSig
.push_back(IIT_STRUCT3
); break;
434 case 4: TypeSig
.push_back(IIT_STRUCT4
); break;
435 case 5: TypeSig
.push_back(IIT_STRUCT5
); break;
436 case 6: TypeSig
.push_back(IIT_STRUCT6
); break;
437 case 7: TypeSig
.push_back(IIT_STRUCT7
); break;
438 case 8: TypeSig
.push_back(IIT_STRUCT8
); break;
439 default: llvm_unreachable("Unhandled case in struct");
442 for (unsigned i
= 0, e
= Int
.IS
.RetVTs
.size(); i
!= e
; ++i
)
443 EncodeFixedType(Int
.IS
.RetTypeDefs
[i
], ArgCodes
, NextArgCode
, TypeSig
,
447 for (unsigned i
= 0, e
= Int
.IS
.ParamTypeDefs
.size(); i
!= e
; ++i
)
448 EncodeFixedType(Int
.IS
.ParamTypeDefs
[i
], ArgCodes
, NextArgCode
, TypeSig
,
452 static void printIITEntry(raw_ostream
&OS
, unsigned char X
) {
456 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable
&Ints
,
458 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
459 // capture it in this vector, otherwise store a ~0U.
460 std::vector
<unsigned> FixedEncodings
;
462 SequenceToOffsetTable
<std::vector
<unsigned char> > LongEncodingTable
;
464 std::vector
<unsigned char> TypeSig
;
466 // Compute the unique argument type info.
467 for (unsigned i
= 0, e
= Ints
.size(); i
!= e
; ++i
) {
468 // Get the signature for the intrinsic.
470 ComputeFixedEncoding(Ints
[i
], TypeSig
);
472 // Check to see if we can encode it into a 32-bit word. We can only encode
473 // 8 nibbles into a 32-bit word.
474 if (TypeSig
.size() <= 8) {
477 for (unsigned i
= 0, e
= TypeSig
.size(); i
!= e
; ++i
) {
478 // If we had an unencodable argument, bail out.
479 if (TypeSig
[i
] > 15) {
483 Result
= (Result
<< 4) | TypeSig
[e
-i
-1];
486 // If this could be encoded into a 31-bit word, return it.
487 if (!Failed
&& (Result
>> 31) == 0) {
488 FixedEncodings
.push_back(Result
);
493 // Otherwise, we're going to unique the sequence into the
494 // LongEncodingTable, and use its offset in the 32-bit table instead.
495 LongEncodingTable
.add(TypeSig
);
497 // This is a placehold that we'll replace after the table is laid out.
498 FixedEncodings
.push_back(~0U);
501 LongEncodingTable
.layout();
503 OS
<< "// Global intrinsic function declaration type table.\n";
504 OS
<< "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
506 OS
<< "static const unsigned IIT_Table[] = {\n ";
508 for (unsigned i
= 0, e
= FixedEncodings
.size(); i
!= e
; ++i
) {
512 // If the entry fit in the table, just emit it.
513 if (FixedEncodings
[i
] != ~0U) {
514 OS
<< "0x" << Twine::utohexstr(FixedEncodings
[i
]) << ", ";
519 ComputeFixedEncoding(Ints
[i
], TypeSig
);
522 // Otherwise, emit the offset into the long encoding table. We emit it this
523 // way so that it is easier to read the offset in the .def file.
524 OS
<< "(1U<<31) | " << LongEncodingTable
.get(TypeSig
) << ", ";
529 // Emit the shared table of register lists.
530 OS
<< "static const unsigned char IIT_LongEncodingTable[] = {\n";
531 if (!LongEncodingTable
.empty())
532 LongEncodingTable
.emit(OS
, printIITEntry
);
533 OS
<< " 255\n};\n\n";
535 OS
<< "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
539 struct AttributeComparator
{
540 bool operator()(const CodeGenIntrinsic
*L
, const CodeGenIntrinsic
*R
) const {
541 // Sort throwing intrinsics after non-throwing intrinsics.
542 if (L
->canThrow
!= R
->canThrow
)
545 if (L
->isNoDuplicate
!= R
->isNoDuplicate
)
546 return R
->isNoDuplicate
;
548 if (L
->isNoReturn
!= R
->isNoReturn
)
549 return R
->isNoReturn
;
551 if (L
->isWillReturn
!= R
->isWillReturn
)
552 return R
->isWillReturn
;
554 if (L
->isCold
!= R
->isCold
)
557 if (L
->isConvergent
!= R
->isConvergent
)
558 return R
->isConvergent
;
560 if (L
->isSpeculatable
!= R
->isSpeculatable
)
561 return R
->isSpeculatable
;
563 if (L
->hasSideEffects
!= R
->hasSideEffects
)
564 return R
->hasSideEffects
;
566 // Try to order by readonly/readnone attribute.
567 CodeGenIntrinsic::ModRefBehavior LK
= L
->ModRef
;
568 CodeGenIntrinsic::ModRefBehavior RK
= R
->ModRef
;
569 if (LK
!= RK
) return (LK
> RK
);
570 // Order by argument attributes.
571 // This is reliable because each side is already sorted internally.
572 return (L
->ArgumentAttributes
< R
->ArgumentAttributes
);
575 } // End anonymous namespace
577 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
578 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable
&Ints
,
580 OS
<< "// Add parameter attributes that are not common to all intrinsics.\n";
581 OS
<< "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
583 OS
<< "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix
584 << "Intrinsic::ID id) {\n";
586 OS
<< "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
588 // Compute the maximum number of attribute arguments and the map
589 typedef std::map
<const CodeGenIntrinsic
*, unsigned,
590 AttributeComparator
> UniqAttrMapTy
;
591 UniqAttrMapTy UniqAttributes
;
592 unsigned maxArgAttrs
= 0;
593 unsigned AttrNum
= 0;
594 for (unsigned i
= 0, e
= Ints
.size(); i
!= e
; ++i
) {
595 const CodeGenIntrinsic
&intrinsic
= Ints
[i
];
597 std::max(maxArgAttrs
, unsigned(intrinsic
.ArgumentAttributes
.size()));
598 unsigned &N
= UniqAttributes
[&intrinsic
];
600 assert(AttrNum
< 256 && "Too many unique attributes for table!");
604 // Emit an array of AttributeList. Most intrinsics will have at least one
605 // entry, for the function itself (index ~1), which is usually nounwind.
606 OS
<< " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
608 for (unsigned i
= 0, e
= Ints
.size(); i
!= e
; ++i
) {
609 const CodeGenIntrinsic
&intrinsic
= Ints
[i
];
611 OS
<< " " << UniqAttributes
[&intrinsic
] << ", // "
612 << intrinsic
.Name
<< "\n";
616 OS
<< " AttributeList AS[" << maxArgAttrs
+ 1 << "];\n";
617 OS
<< " unsigned NumAttrs = 0;\n";
618 OS
<< " if (id != 0) {\n";
619 OS
<< " switch(IntrinsicsToAttributesMap[id - ";
621 OS
<< "Intrinsic::num_intrinsics";
625 OS
<< " default: llvm_unreachable(\"Invalid attribute number\");\n";
626 for (UniqAttrMapTy::const_iterator I
= UniqAttributes
.begin(),
627 E
= UniqAttributes
.end(); I
!= E
; ++I
) {
628 OS
<< " case " << I
->second
<< ": {\n";
630 const CodeGenIntrinsic
&intrinsic
= *(I
->first
);
632 // Keep track of the number of attributes we're writing out.
633 unsigned numAttrs
= 0;
635 // The argument attributes are alreadys sorted by argument index.
636 unsigned ai
= 0, ae
= intrinsic
.ArgumentAttributes
.size();
639 unsigned argNo
= intrinsic
.ArgumentAttributes
[ai
].first
;
640 unsigned attrIdx
= argNo
+ 1; // Must match AttributeList::FirstArgIndex
642 OS
<< " const Attribute::AttrKind AttrParam" << attrIdx
<< "[]= {";
643 bool addComma
= false;
646 switch (intrinsic
.ArgumentAttributes
[ai
].second
) {
647 case CodeGenIntrinsic::NoCapture
:
650 OS
<< "Attribute::NoCapture";
653 case CodeGenIntrinsic::NoAlias
:
656 OS
<< "Attribute::NoAlias";
659 case CodeGenIntrinsic::Returned
:
662 OS
<< "Attribute::Returned";
665 case CodeGenIntrinsic::ReadOnly
:
668 OS
<< "Attribute::ReadOnly";
671 case CodeGenIntrinsic::WriteOnly
:
674 OS
<< "Attribute::WriteOnly";
677 case CodeGenIntrinsic::ReadNone
:
680 OS
<< "Attribute::ReadNone";
683 case CodeGenIntrinsic::ImmArg
:
686 OS
<< "Attribute::ImmArg";
692 } while (ai
!= ae
&& intrinsic
.ArgumentAttributes
[ai
].first
== argNo
);
694 OS
<< " AS[" << numAttrs
++ << "] = AttributeList::get(C, "
695 << attrIdx
<< ", AttrParam" << attrIdx
<< ");\n";
699 if (!intrinsic
.canThrow
||
700 (intrinsic
.ModRef
!= CodeGenIntrinsic::ReadWriteMem
&& !intrinsic
.hasSideEffects
) ||
701 intrinsic
.isNoReturn
|| intrinsic
.isWillReturn
|| intrinsic
.isCold
||
702 intrinsic
.isNoDuplicate
|| intrinsic
.isConvergent
||
703 intrinsic
.isSpeculatable
) {
704 OS
<< " const Attribute::AttrKind Atts[] = {";
705 bool addComma
= false;
706 if (!intrinsic
.canThrow
) {
707 OS
<< "Attribute::NoUnwind";
710 if (intrinsic
.isNoReturn
) {
713 OS
<< "Attribute::NoReturn";
716 if (intrinsic
.isWillReturn
) {
719 OS
<< "Attribute::WillReturn";
722 if (intrinsic
.isCold
) {
725 OS
<< "Attribute::Cold";
728 if (intrinsic
.isNoDuplicate
) {
731 OS
<< "Attribute::NoDuplicate";
734 if (intrinsic
.isConvergent
) {
737 OS
<< "Attribute::Convergent";
740 if (intrinsic
.isSpeculatable
) {
743 OS
<< "Attribute::Speculatable";
747 switch (intrinsic
.ModRef
) {
748 case CodeGenIntrinsic::NoMem
:
749 if (intrinsic
.hasSideEffects
)
753 OS
<< "Attribute::ReadNone";
755 case CodeGenIntrinsic::ReadArgMem
:
758 OS
<< "Attribute::ReadOnly,";
759 OS
<< "Attribute::ArgMemOnly";
761 case CodeGenIntrinsic::ReadMem
:
764 OS
<< "Attribute::ReadOnly";
766 case CodeGenIntrinsic::ReadInaccessibleMem
:
769 OS
<< "Attribute::ReadOnly,";
770 OS
<< "Attribute::InaccessibleMemOnly";
772 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem
:
775 OS
<< "Attribute::ReadOnly,";
776 OS
<< "Attribute::InaccessibleMemOrArgMemOnly";
778 case CodeGenIntrinsic::WriteArgMem
:
781 OS
<< "Attribute::WriteOnly,";
782 OS
<< "Attribute::ArgMemOnly";
784 case CodeGenIntrinsic::WriteMem
:
787 OS
<< "Attribute::WriteOnly";
789 case CodeGenIntrinsic::WriteInaccessibleMem
:
792 OS
<< "Attribute::WriteOnly,";
793 OS
<< "Attribute::InaccessibleMemOnly";
795 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem
:
798 OS
<< "Attribute::WriteOnly,";
799 OS
<< "Attribute::InaccessibleMemOrArgMemOnly";
801 case CodeGenIntrinsic::ReadWriteArgMem
:
804 OS
<< "Attribute::ArgMemOnly";
806 case CodeGenIntrinsic::ReadWriteInaccessibleMem
:
809 OS
<< "Attribute::InaccessibleMemOnly";
811 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem
:
814 OS
<< "Attribute::InaccessibleMemOrArgMemOnly";
816 case CodeGenIntrinsic::ReadWriteMem
:
820 OS
<< " AS[" << numAttrs
++ << "] = AttributeList::get(C, "
821 << "AttributeList::FunctionIndex, Atts);\n";
825 OS
<< " NumAttrs = " << numAttrs
<< ";\n";
829 OS
<< " return AttributeList();\n";
836 OS
<< " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n";
838 OS
<< "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
841 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
842 const CodeGenIntrinsicTable
&Ints
, bool IsGCC
, raw_ostream
&OS
) {
843 StringRef CompilerName
= (IsGCC
? "GCC" : "MS");
844 typedef std::map
<std::string
, std::map
<std::string
, std::string
>> BIMTy
;
846 StringToOffsetTable Table
;
847 for (unsigned i
= 0, e
= Ints
.size(); i
!= e
; ++i
) {
848 const std::string
&BuiltinName
=
849 IsGCC
? Ints
[i
].GCCBuiltinName
: Ints
[i
].MSBuiltinName
;
850 if (!BuiltinName
.empty()) {
851 // Get the map for this target prefix.
852 std::map
<std::string
, std::string
> &BIM
=
853 BuiltinMap
[Ints
[i
].TargetPrefix
];
855 if (!BIM
.insert(std::make_pair(BuiltinName
, Ints
[i
].EnumName
)).second
)
856 PrintFatalError(Ints
[i
].TheDef
->getLoc(),
857 "Intrinsic '" + Ints
[i
].TheDef
->getName() +
858 "': duplicate " + CompilerName
+ " builtin name!");
859 Table
.GetOrAddStringOffset(BuiltinName
);
863 OS
<< "// Get the LLVM intrinsic that corresponds to a builtin.\n";
864 OS
<< "// This is used by the C front-end. The builtin name is passed\n";
865 OS
<< "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
866 OS
<< "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
867 OS
<< "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName
<< "_BUILTIN\n";
870 OS
<< "static " << TargetPrefix
<< "Intrinsic::ID "
871 << "getIntrinsicFor" << CompilerName
<< "Builtin(const char "
872 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
874 OS
<< "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
875 << "Builtin(const char "
876 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
881 if (!TargetPrefix
.empty())
882 OS
<< "(" << TargetPrefix
<< "Intrinsic::ID)";
883 OS
<< "Intrinsic::not_intrinsic;\n";
889 OS
<< " static const char BuiltinNames[] = {\n";
890 Table
.EmitCharArray(OS
);
893 OS
<< " struct BuiltinEntry {\n";
894 OS
<< " Intrinsic::ID IntrinID;\n";
895 OS
<< " unsigned StrTabOffset;\n";
896 OS
<< " const char *getName() const {\n";
897 OS
<< " return &BuiltinNames[StrTabOffset];\n";
899 OS
<< " bool operator<(StringRef RHS) const {\n";
900 OS
<< " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
904 OS
<< " StringRef TargetPrefix(TargetPrefixStr);\n\n";
906 // Note: this could emit significantly better code if we cared.
907 for (BIMTy::iterator I
= BuiltinMap
.begin(), E
= BuiltinMap
.end();I
!= E
;++I
){
909 if (!I
->first
.empty())
910 OS
<< "if (TargetPrefix == \"" << I
->first
<< "\") ";
912 OS
<< "/* Target Independent Builtins */ ";
915 // Emit the comparisons for this target prefix.
916 OS
<< " static const BuiltinEntry " << I
->first
<< "Names[] = {\n";
917 for (const auto &P
: I
->second
) {
918 OS
<< " {Intrinsic::" << P
.second
<< ", "
919 << Table
.GetOrAddStringOffset(P
.first
) << "}, // " << P
.first
<< "\n";
922 OS
<< " auto I = std::lower_bound(std::begin(" << I
->first
<< "Names),\n";
923 OS
<< " std::end(" << I
->first
<< "Names),\n";
924 OS
<< " BuiltinNameStr);\n";
925 OS
<< " if (I != std::end(" << I
->first
<< "Names) &&\n";
926 OS
<< " I->getName() == BuiltinNameStr)\n";
927 OS
<< " return I->IntrinID;\n";
931 if (!TargetPrefix
.empty())
932 OS
<< "(" << TargetPrefix
<< "Intrinsic::ID)";
933 OS
<< "Intrinsic::not_intrinsic;\n";
938 void llvm::EmitIntrinsicEnums(RecordKeeper
&RK
, raw_ostream
&OS
,
940 IntrinsicEmitter(RK
, TargetOnly
).run(OS
, /*Enums=*/true);
943 void llvm::EmitIntrinsicImpl(RecordKeeper
&RK
, raw_ostream
&OS
,
945 IntrinsicEmitter(RK
, TargetOnly
).run(OS
, /*Enums=*/false);