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,
226 static void EncodeFixedValueType(MVT::SimpleValueType VT
,
227 std::vector
<unsigned char> &Sig
) {
228 if (MVT(VT
).isInteger()) {
229 unsigned BitWidth
= MVT(VT
).getSizeInBits();
231 default: PrintFatalError("unhandled integer type width in intrinsic!");
232 case 1: return Sig
.push_back(IIT_I1
);
233 case 8: return Sig
.push_back(IIT_I8
);
234 case 16: return Sig
.push_back(IIT_I16
);
235 case 32: return Sig
.push_back(IIT_I32
);
236 case 64: return Sig
.push_back(IIT_I64
);
237 case 128: return Sig
.push_back(IIT_I128
);
242 default: PrintFatalError("unhandled MVT in intrinsic!");
243 case MVT::f16
: return Sig
.push_back(IIT_F16
);
244 case MVT::f32
: return Sig
.push_back(IIT_F32
);
245 case MVT::f64
: return Sig
.push_back(IIT_F64
);
246 case MVT::f128
: return Sig
.push_back(IIT_F128
);
247 case MVT::token
: return Sig
.push_back(IIT_TOKEN
);
248 case MVT::Metadata
: return Sig
.push_back(IIT_METADATA
);
249 case MVT::x86mmx
: return Sig
.push_back(IIT_MMX
);
250 // MVT::OtherVT is used to mean the empty struct type here.
251 case MVT::Other
: return Sig
.push_back(IIT_EMPTYSTRUCT
);
252 // MVT::isVoid is used to represent varargs here.
253 case MVT::isVoid
: return Sig
.push_back(IIT_VARARG
);
257 #if defined(_MSC_VER) && !defined(__clang__)
258 #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
261 static void EncodeFixedType(Record
*R
, std::vector
<unsigned char> &ArgCodes
,
262 unsigned &NextArgCode
,
263 std::vector
<unsigned char> &Sig
,
264 ArrayRef
<unsigned char> Mapping
) {
266 if (R
->isSubClassOf("LLVMMatchType")) {
267 unsigned Number
= Mapping
[R
->getValueAsInt("Number")];
268 assert(Number
< ArgCodes
.size() && "Invalid matching number!");
269 if (R
->isSubClassOf("LLVMExtendedType"))
270 Sig
.push_back(IIT_EXTEND_ARG
);
271 else if (R
->isSubClassOf("LLVMTruncatedType"))
272 Sig
.push_back(IIT_TRUNC_ARG
);
273 else if (R
->isSubClassOf("LLVMHalfElementsVectorType"))
274 Sig
.push_back(IIT_HALF_VEC_ARG
);
275 else if (R
->isSubClassOf("LLVMScalarOrSameVectorWidth")) {
276 Sig
.push_back(IIT_SAME_VEC_WIDTH_ARG
);
277 Sig
.push_back((Number
<< 3) | ArgCodes
[Number
]);
278 MVT::SimpleValueType VT
= getValueType(R
->getValueAsDef("ElTy"));
279 EncodeFixedValueType(VT
, Sig
);
282 else if (R
->isSubClassOf("LLVMPointerTo"))
283 Sig
.push_back(IIT_PTR_TO_ARG
);
284 else if (R
->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
285 Sig
.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT
);
286 // Encode overloaded ArgNo
287 Sig
.push_back(NextArgCode
++);
288 // Encode LLVMMatchType<Number> ArgNo
289 Sig
.push_back(Number
);
291 } else if (R
->isSubClassOf("LLVMPointerToElt"))
292 Sig
.push_back(IIT_PTR_TO_ELT
);
293 else if (R
->isSubClassOf("LLVMVectorElementType"))
294 Sig
.push_back(IIT_VEC_ELEMENT
);
296 Sig
.push_back(IIT_ARG
);
297 return Sig
.push_back((Number
<< 3) | 7 /*IITDescriptor::AK_MatchType*/);
300 MVT::SimpleValueType VT
= getValueType(R
->getValueAsDef("VT"));
305 case MVT::iPTRAny
: ++Tmp
; LLVM_FALLTHROUGH
;
306 case MVT::vAny
: ++Tmp
; LLVM_FALLTHROUGH
;
307 case MVT::fAny
: ++Tmp
; LLVM_FALLTHROUGH
;
308 case MVT::iAny
: ++Tmp
; LLVM_FALLTHROUGH
;
310 // If this is an "any" valuetype, then the type is the type of the next
311 // type in the list specified to getIntrinsic().
312 Sig
.push_back(IIT_ARG
);
314 // Figure out what arg # this is consuming, and remember what kind it was.
315 assert(NextArgCode
< ArgCodes
.size() && ArgCodes
[NextArgCode
] == Tmp
&&
316 "Invalid or no ArgCode associated with overloaded VT!");
317 unsigned ArgNo
= NextArgCode
++;
319 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
320 return Sig
.push_back((ArgNo
<< 3) | Tmp
);
324 unsigned AddrSpace
= 0;
325 if (R
->isSubClassOf("LLVMQualPointerType")) {
326 AddrSpace
= R
->getValueAsInt("AddrSpace");
327 assert(AddrSpace
< 256 && "Address space exceeds 255");
330 Sig
.push_back(IIT_ANYPTR
);
331 Sig
.push_back(AddrSpace
);
333 Sig
.push_back(IIT_PTR
);
335 return EncodeFixedType(R
->getValueAsDef("ElTy"), ArgCodes
, NextArgCode
, Sig
,
340 if (MVT(VT
).isVector()) {
342 switch (VVT
.getVectorNumElements()) {
343 default: PrintFatalError("unhandled vector type width in intrinsic!");
344 case 1: Sig
.push_back(IIT_V1
); break;
345 case 2: Sig
.push_back(IIT_V2
); break;
346 case 4: Sig
.push_back(IIT_V4
); break;
347 case 8: Sig
.push_back(IIT_V8
); break;
348 case 16: Sig
.push_back(IIT_V16
); break;
349 case 32: Sig
.push_back(IIT_V32
); break;
350 case 64: Sig
.push_back(IIT_V64
); break;
351 case 512: Sig
.push_back(IIT_V512
); break;
352 case 1024: Sig
.push_back(IIT_V1024
); break;
355 return EncodeFixedValueType(VVT
.getVectorElementType().SimpleTy
, Sig
);
358 EncodeFixedValueType(VT
, Sig
);
361 static void UpdateArgCodes(Record
*R
, std::vector
<unsigned char> &ArgCodes
,
362 unsigned int &NumInserted
,
363 SmallVectorImpl
<unsigned char> &Mapping
) {
364 if (R
->isSubClassOf("LLVMMatchType")) {
365 if (R
->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
366 ArgCodes
.push_back(3 /*vAny*/);
373 switch (getValueType(R
->getValueAsDef("VT"))) {
376 UpdateArgCodes(R
->getValueAsDef("ElTy"), ArgCodes
, NumInserted
, Mapping
);
391 unsigned OriginalIdx
= ArgCodes
.size() - NumInserted
;
392 assert(OriginalIdx
>= Mapping
.size());
393 Mapping
.resize(OriginalIdx
+1);
394 Mapping
[OriginalIdx
] = ArgCodes
.size();
395 ArgCodes
.push_back(Tmp
);
400 #if defined(_MSC_VER) && !defined(__clang__)
401 #pragma optimize("",on)
404 /// ComputeFixedEncoding - If we can encode the type signature for this
405 /// intrinsic into 32 bits, return it. If not, return ~0U.
406 static void ComputeFixedEncoding(const CodeGenIntrinsic
&Int
,
407 std::vector
<unsigned char> &TypeSig
) {
408 std::vector
<unsigned char> ArgCodes
;
410 // Add codes for any overloaded result VTs.
411 unsigned int NumInserted
= 0;
412 SmallVector
<unsigned char, 8> ArgMapping
;
413 for (unsigned i
= 0, e
= Int
.IS
.RetVTs
.size(); i
!= e
; ++i
)
414 UpdateArgCodes(Int
.IS
.RetTypeDefs
[i
], ArgCodes
, NumInserted
, ArgMapping
);
416 // Add codes for any overloaded operand VTs.
417 for (unsigned i
= 0, e
= Int
.IS
.ParamTypeDefs
.size(); i
!= e
; ++i
)
418 UpdateArgCodes(Int
.IS
.ParamTypeDefs
[i
], ArgCodes
, NumInserted
, ArgMapping
);
420 unsigned NextArgCode
= 0;
421 if (Int
.IS
.RetVTs
.empty())
422 TypeSig
.push_back(IIT_Done
);
423 else if (Int
.IS
.RetVTs
.size() == 1 &&
424 Int
.IS
.RetVTs
[0] == MVT::isVoid
)
425 TypeSig
.push_back(IIT_Done
);
427 switch (Int
.IS
.RetVTs
.size()) {
429 case 2: TypeSig
.push_back(IIT_STRUCT2
); break;
430 case 3: TypeSig
.push_back(IIT_STRUCT3
); break;
431 case 4: TypeSig
.push_back(IIT_STRUCT4
); break;
432 case 5: TypeSig
.push_back(IIT_STRUCT5
); break;
433 case 6: TypeSig
.push_back(IIT_STRUCT6
); break;
434 case 7: TypeSig
.push_back(IIT_STRUCT7
); break;
435 case 8: TypeSig
.push_back(IIT_STRUCT8
); break;
436 default: llvm_unreachable("Unhandled case in struct");
439 for (unsigned i
= 0, e
= Int
.IS
.RetVTs
.size(); i
!= e
; ++i
)
440 EncodeFixedType(Int
.IS
.RetTypeDefs
[i
], ArgCodes
, NextArgCode
, TypeSig
,
444 for (unsigned i
= 0, e
= Int
.IS
.ParamTypeDefs
.size(); i
!= e
; ++i
)
445 EncodeFixedType(Int
.IS
.ParamTypeDefs
[i
], ArgCodes
, NextArgCode
, TypeSig
,
449 static void printIITEntry(raw_ostream
&OS
, unsigned char X
) {
453 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable
&Ints
,
455 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
456 // capture it in this vector, otherwise store a ~0U.
457 std::vector
<unsigned> FixedEncodings
;
459 SequenceToOffsetTable
<std::vector
<unsigned char> > LongEncodingTable
;
461 std::vector
<unsigned char> TypeSig
;
463 // Compute the unique argument type info.
464 for (unsigned i
= 0, e
= Ints
.size(); i
!= e
; ++i
) {
465 // Get the signature for the intrinsic.
467 ComputeFixedEncoding(Ints
[i
], TypeSig
);
469 // Check to see if we can encode it into a 32-bit word. We can only encode
470 // 8 nibbles into a 32-bit word.
471 if (TypeSig
.size() <= 8) {
474 for (unsigned i
= 0, e
= TypeSig
.size(); i
!= e
; ++i
) {
475 // If we had an unencodable argument, bail out.
476 if (TypeSig
[i
] > 15) {
480 Result
= (Result
<< 4) | TypeSig
[e
-i
-1];
483 // If this could be encoded into a 31-bit word, return it.
484 if (!Failed
&& (Result
>> 31) == 0) {
485 FixedEncodings
.push_back(Result
);
490 // Otherwise, we're going to unique the sequence into the
491 // LongEncodingTable, and use its offset in the 32-bit table instead.
492 LongEncodingTable
.add(TypeSig
);
494 // This is a placehold that we'll replace after the table is laid out.
495 FixedEncodings
.push_back(~0U);
498 LongEncodingTable
.layout();
500 OS
<< "// Global intrinsic function declaration type table.\n";
501 OS
<< "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
503 OS
<< "static const unsigned IIT_Table[] = {\n ";
505 for (unsigned i
= 0, e
= FixedEncodings
.size(); i
!= e
; ++i
) {
509 // If the entry fit in the table, just emit it.
510 if (FixedEncodings
[i
] != ~0U) {
511 OS
<< "0x" << Twine::utohexstr(FixedEncodings
[i
]) << ", ";
516 ComputeFixedEncoding(Ints
[i
], TypeSig
);
519 // Otherwise, emit the offset into the long encoding table. We emit it this
520 // way so that it is easier to read the offset in the .def file.
521 OS
<< "(1U<<31) | " << LongEncodingTable
.get(TypeSig
) << ", ";
526 // Emit the shared table of register lists.
527 OS
<< "static const unsigned char IIT_LongEncodingTable[] = {\n";
528 if (!LongEncodingTable
.empty())
529 LongEncodingTable
.emit(OS
, printIITEntry
);
530 OS
<< " 255\n};\n\n";
532 OS
<< "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
536 struct AttributeComparator
{
537 bool operator()(const CodeGenIntrinsic
*L
, const CodeGenIntrinsic
*R
) const {
538 // Sort throwing intrinsics after non-throwing intrinsics.
539 if (L
->canThrow
!= R
->canThrow
)
542 if (L
->isNoDuplicate
!= R
->isNoDuplicate
)
543 return R
->isNoDuplicate
;
545 if (L
->isNoReturn
!= R
->isNoReturn
)
546 return R
->isNoReturn
;
548 if (L
->isWillReturn
!= R
->isWillReturn
)
549 return R
->isWillReturn
;
551 if (L
->isCold
!= R
->isCold
)
554 if (L
->isConvergent
!= R
->isConvergent
)
555 return R
->isConvergent
;
557 if (L
->isSpeculatable
!= R
->isSpeculatable
)
558 return R
->isSpeculatable
;
560 if (L
->hasSideEffects
!= R
->hasSideEffects
)
561 return R
->hasSideEffects
;
563 // Try to order by readonly/readnone attribute.
564 CodeGenIntrinsic::ModRefBehavior LK
= L
->ModRef
;
565 CodeGenIntrinsic::ModRefBehavior RK
= R
->ModRef
;
566 if (LK
!= RK
) return (LK
> RK
);
567 // Order by argument attributes.
568 // This is reliable because each side is already sorted internally.
569 return (L
->ArgumentAttributes
< R
->ArgumentAttributes
);
572 } // End anonymous namespace
574 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
575 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable
&Ints
,
577 OS
<< "// Add parameter attributes that are not common to all intrinsics.\n";
578 OS
<< "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
580 OS
<< "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix
581 << "Intrinsic::ID id) {\n";
583 OS
<< "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
585 // Compute the maximum number of attribute arguments and the map
586 typedef std::map
<const CodeGenIntrinsic
*, unsigned,
587 AttributeComparator
> UniqAttrMapTy
;
588 UniqAttrMapTy UniqAttributes
;
589 unsigned maxArgAttrs
= 0;
590 unsigned AttrNum
= 0;
591 for (unsigned i
= 0, e
= Ints
.size(); i
!= e
; ++i
) {
592 const CodeGenIntrinsic
&intrinsic
= Ints
[i
];
594 std::max(maxArgAttrs
, unsigned(intrinsic
.ArgumentAttributes
.size()));
595 unsigned &N
= UniqAttributes
[&intrinsic
];
597 assert(AttrNum
< 256 && "Too many unique attributes for table!");
601 // Emit an array of AttributeList. Most intrinsics will have at least one
602 // entry, for the function itself (index ~1), which is usually nounwind.
603 OS
<< " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
605 for (unsigned i
= 0, e
= Ints
.size(); i
!= e
; ++i
) {
606 const CodeGenIntrinsic
&intrinsic
= Ints
[i
];
608 OS
<< " " << UniqAttributes
[&intrinsic
] << ", // "
609 << intrinsic
.Name
<< "\n";
613 OS
<< " AttributeList AS[" << maxArgAttrs
+ 1 << "];\n";
614 OS
<< " unsigned NumAttrs = 0;\n";
615 OS
<< " if (id != 0) {\n";
616 OS
<< " switch(IntrinsicsToAttributesMap[id - ";
618 OS
<< "Intrinsic::num_intrinsics";
622 OS
<< " default: llvm_unreachable(\"Invalid attribute number\");\n";
623 for (UniqAttrMapTy::const_iterator I
= UniqAttributes
.begin(),
624 E
= UniqAttributes
.end(); I
!= E
; ++I
) {
625 OS
<< " case " << I
->second
<< ": {\n";
627 const CodeGenIntrinsic
&intrinsic
= *(I
->first
);
629 // Keep track of the number of attributes we're writing out.
630 unsigned numAttrs
= 0;
632 // The argument attributes are alreadys sorted by argument index.
633 unsigned ai
= 0, ae
= intrinsic
.ArgumentAttributes
.size();
636 unsigned argNo
= intrinsic
.ArgumentAttributes
[ai
].first
;
637 unsigned attrIdx
= argNo
+ 1; // Must match AttributeList::FirstArgIndex
639 OS
<< " const Attribute::AttrKind AttrParam" << attrIdx
<< "[]= {";
640 bool addComma
= false;
643 switch (intrinsic
.ArgumentAttributes
[ai
].second
) {
644 case CodeGenIntrinsic::NoCapture
:
647 OS
<< "Attribute::NoCapture";
650 case CodeGenIntrinsic::NoAlias
:
653 OS
<< "Attribute::NoAlias";
656 case CodeGenIntrinsic::Returned
:
659 OS
<< "Attribute::Returned";
662 case CodeGenIntrinsic::ReadOnly
:
665 OS
<< "Attribute::ReadOnly";
668 case CodeGenIntrinsic::WriteOnly
:
671 OS
<< "Attribute::WriteOnly";
674 case CodeGenIntrinsic::ReadNone
:
677 OS
<< "Attribute::ReadNone";
680 case CodeGenIntrinsic::ImmArg
:
683 OS
<< "Attribute::ImmArg";
689 } while (ai
!= ae
&& intrinsic
.ArgumentAttributes
[ai
].first
== argNo
);
691 OS
<< " AS[" << numAttrs
++ << "] = AttributeList::get(C, "
692 << attrIdx
<< ", AttrParam" << attrIdx
<< ");\n";
696 if (!intrinsic
.canThrow
||
697 (intrinsic
.ModRef
!= CodeGenIntrinsic::ReadWriteMem
&& !intrinsic
.hasSideEffects
) ||
698 intrinsic
.isNoReturn
|| intrinsic
.isWillReturn
|| intrinsic
.isCold
||
699 intrinsic
.isNoDuplicate
|| intrinsic
.isConvergent
||
700 intrinsic
.isSpeculatable
) {
701 OS
<< " const Attribute::AttrKind Atts[] = {";
702 bool addComma
= false;
703 if (!intrinsic
.canThrow
) {
704 OS
<< "Attribute::NoUnwind";
707 if (intrinsic
.isNoReturn
) {
710 OS
<< "Attribute::NoReturn";
713 if (intrinsic
.isWillReturn
) {
716 OS
<< "Attribute::WillReturn";
719 if (intrinsic
.isCold
) {
722 OS
<< "Attribute::Cold";
725 if (intrinsic
.isNoDuplicate
) {
728 OS
<< "Attribute::NoDuplicate";
731 if (intrinsic
.isConvergent
) {
734 OS
<< "Attribute::Convergent";
737 if (intrinsic
.isSpeculatable
) {
740 OS
<< "Attribute::Speculatable";
744 switch (intrinsic
.ModRef
) {
745 case CodeGenIntrinsic::NoMem
:
746 if (intrinsic
.hasSideEffects
)
750 OS
<< "Attribute::ReadNone";
752 case CodeGenIntrinsic::ReadArgMem
:
755 OS
<< "Attribute::ReadOnly,";
756 OS
<< "Attribute::ArgMemOnly";
758 case CodeGenIntrinsic::ReadMem
:
761 OS
<< "Attribute::ReadOnly";
763 case CodeGenIntrinsic::ReadInaccessibleMem
:
766 OS
<< "Attribute::ReadOnly,";
767 OS
<< "Attribute::InaccessibleMemOnly";
769 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem
:
772 OS
<< "Attribute::ReadOnly,";
773 OS
<< "Attribute::InaccessibleMemOrArgMemOnly";
775 case CodeGenIntrinsic::WriteArgMem
:
778 OS
<< "Attribute::WriteOnly,";
779 OS
<< "Attribute::ArgMemOnly";
781 case CodeGenIntrinsic::WriteMem
:
784 OS
<< "Attribute::WriteOnly";
786 case CodeGenIntrinsic::WriteInaccessibleMem
:
789 OS
<< "Attribute::WriteOnly,";
790 OS
<< "Attribute::InaccessibleMemOnly";
792 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem
:
795 OS
<< "Attribute::WriteOnly,";
796 OS
<< "Attribute::InaccessibleMemOrArgMemOnly";
798 case CodeGenIntrinsic::ReadWriteArgMem
:
801 OS
<< "Attribute::ArgMemOnly";
803 case CodeGenIntrinsic::ReadWriteInaccessibleMem
:
806 OS
<< "Attribute::InaccessibleMemOnly";
808 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem
:
811 OS
<< "Attribute::InaccessibleMemOrArgMemOnly";
813 case CodeGenIntrinsic::ReadWriteMem
:
817 OS
<< " AS[" << numAttrs
++ << "] = AttributeList::get(C, "
818 << "AttributeList::FunctionIndex, Atts);\n";
822 OS
<< " NumAttrs = " << numAttrs
<< ";\n";
826 OS
<< " return AttributeList();\n";
833 OS
<< " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n";
835 OS
<< "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
838 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
839 const CodeGenIntrinsicTable
&Ints
, bool IsGCC
, raw_ostream
&OS
) {
840 StringRef CompilerName
= (IsGCC
? "GCC" : "MS");
841 typedef std::map
<std::string
, std::map
<std::string
, std::string
>> BIMTy
;
843 StringToOffsetTable Table
;
844 for (unsigned i
= 0, e
= Ints
.size(); i
!= e
; ++i
) {
845 const std::string
&BuiltinName
=
846 IsGCC
? Ints
[i
].GCCBuiltinName
: Ints
[i
].MSBuiltinName
;
847 if (!BuiltinName
.empty()) {
848 // Get the map for this target prefix.
849 std::map
<std::string
, std::string
> &BIM
=
850 BuiltinMap
[Ints
[i
].TargetPrefix
];
852 if (!BIM
.insert(std::make_pair(BuiltinName
, Ints
[i
].EnumName
)).second
)
853 PrintFatalError(Ints
[i
].TheDef
->getLoc(),
854 "Intrinsic '" + Ints
[i
].TheDef
->getName() +
855 "': duplicate " + CompilerName
+ " builtin name!");
856 Table
.GetOrAddStringOffset(BuiltinName
);
860 OS
<< "// Get the LLVM intrinsic that corresponds to a builtin.\n";
861 OS
<< "// This is used by the C front-end. The builtin name is passed\n";
862 OS
<< "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
863 OS
<< "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
864 OS
<< "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName
<< "_BUILTIN\n";
867 OS
<< "static " << TargetPrefix
<< "Intrinsic::ID "
868 << "getIntrinsicFor" << CompilerName
<< "Builtin(const char "
869 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
871 OS
<< "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
872 << "Builtin(const char "
873 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
878 if (!TargetPrefix
.empty())
879 OS
<< "(" << TargetPrefix
<< "Intrinsic::ID)";
880 OS
<< "Intrinsic::not_intrinsic;\n";
886 OS
<< " static const char BuiltinNames[] = {\n";
887 Table
.EmitCharArray(OS
);
890 OS
<< " struct BuiltinEntry {\n";
891 OS
<< " Intrinsic::ID IntrinID;\n";
892 OS
<< " unsigned StrTabOffset;\n";
893 OS
<< " const char *getName() const {\n";
894 OS
<< " return &BuiltinNames[StrTabOffset];\n";
896 OS
<< " bool operator<(StringRef RHS) const {\n";
897 OS
<< " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
901 OS
<< " StringRef TargetPrefix(TargetPrefixStr);\n\n";
903 // Note: this could emit significantly better code if we cared.
904 for (BIMTy::iterator I
= BuiltinMap
.begin(), E
= BuiltinMap
.end();I
!= E
;++I
){
906 if (!I
->first
.empty())
907 OS
<< "if (TargetPrefix == \"" << I
->first
<< "\") ";
909 OS
<< "/* Target Independent Builtins */ ";
912 // Emit the comparisons for this target prefix.
913 OS
<< " static const BuiltinEntry " << I
->first
<< "Names[] = {\n";
914 for (const auto &P
: I
->second
) {
915 OS
<< " {Intrinsic::" << P
.second
<< ", "
916 << Table
.GetOrAddStringOffset(P
.first
) << "}, // " << P
.first
<< "\n";
919 OS
<< " auto I = std::lower_bound(std::begin(" << I
->first
<< "Names),\n";
920 OS
<< " std::end(" << I
->first
<< "Names),\n";
921 OS
<< " BuiltinNameStr);\n";
922 OS
<< " if (I != std::end(" << I
->first
<< "Names) &&\n";
923 OS
<< " I->getName() == BuiltinNameStr)\n";
924 OS
<< " return I->IntrinID;\n";
928 if (!TargetPrefix
.empty())
929 OS
<< "(" << TargetPrefix
<< "Intrinsic::ID)";
930 OS
<< "Intrinsic::not_intrinsic;\n";
935 void llvm::EmitIntrinsicEnums(RecordKeeper
&RK
, raw_ostream
&OS
,
937 IntrinsicEmitter(RK
, TargetOnly
).run(OS
, /*Enums=*/true);
940 void llvm::EmitIntrinsicImpl(RecordKeeper
&RK
, raw_ostream
&OS
,
942 IntrinsicEmitter(RK
, TargetOnly
).run(OS
, /*Enums=*/false);