1 //===-- Intrinsics.cpp - Intrinsic Function Handling ------------*- C++ -*-===//
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 file implements functions required for supporting intrinsic functions.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/Intrinsics.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/ADT/StringTable.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/IntrinsicsAArch64.h"
18 #include "llvm/IR/IntrinsicsAMDGPU.h"
19 #include "llvm/IR/IntrinsicsARM.h"
20 #include "llvm/IR/IntrinsicsBPF.h"
21 #include "llvm/IR/IntrinsicsHexagon.h"
22 #include "llvm/IR/IntrinsicsLoongArch.h"
23 #include "llvm/IR/IntrinsicsMips.h"
24 #include "llvm/IR/IntrinsicsNVPTX.h"
25 #include "llvm/IR/IntrinsicsPowerPC.h"
26 #include "llvm/IR/IntrinsicsR600.h"
27 #include "llvm/IR/IntrinsicsRISCV.h"
28 #include "llvm/IR/IntrinsicsS390.h"
29 #include "llvm/IR/IntrinsicsVE.h"
30 #include "llvm/IR/IntrinsicsX86.h"
31 #include "llvm/IR/IntrinsicsXCore.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/Type.h"
37 /// Table of string intrinsic names indexed by enum value.
38 #define GET_INTRINSIC_NAME_TABLE
39 #include "llvm/IR/IntrinsicImpl.inc"
40 #undef GET_INTRINSIC_NAME_TABLE
42 StringRef
Intrinsic::getBaseName(ID id
) {
43 assert(id
< num_intrinsics
&& "Invalid intrinsic ID!");
44 return IntrinsicNameTable
[IntrinsicNameOffsetTable
[id
]];
47 StringRef
Intrinsic::getName(ID id
) {
48 assert(id
< num_intrinsics
&& "Invalid intrinsic ID!");
49 assert(!Intrinsic::isOverloaded(id
) &&
50 "This version of getName does not support overloading");
51 return getBaseName(id
);
54 /// Returns a stable mangling for the type specified for use in the name
55 /// mangling scheme used by 'any' types in intrinsic signatures. The mangling
56 /// of named types is simply their name. Manglings for unnamed types consist
57 /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
58 /// combined with the mangling of their component types. A vararg function
59 /// type will have a suffix of 'vararg'. Since function types can contain
60 /// other function types, we close a function type mangling with suffix 'f'
61 /// which can't be confused with it's prefix. This ensures we don't have
62 /// collisions between two unrelated function types. Otherwise, you might
63 /// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.)
64 /// The HasUnnamedType boolean is set if an unnamed type was encountered,
65 /// indicating that extra care must be taken to ensure a unique name.
66 static std::string
getMangledTypeStr(Type
*Ty
, bool &HasUnnamedType
) {
68 if (PointerType
*PTyp
= dyn_cast
<PointerType
>(Ty
)) {
69 Result
+= "p" + utostr(PTyp
->getAddressSpace());
70 } else if (ArrayType
*ATyp
= dyn_cast
<ArrayType
>(Ty
)) {
71 Result
+= "a" + utostr(ATyp
->getNumElements()) +
72 getMangledTypeStr(ATyp
->getElementType(), HasUnnamedType
);
73 } else if (StructType
*STyp
= dyn_cast
<StructType
>(Ty
)) {
74 if (!STyp
->isLiteral()) {
77 Result
+= STyp
->getName();
79 HasUnnamedType
= true;
82 for (auto *Elem
: STyp
->elements())
83 Result
+= getMangledTypeStr(Elem
, HasUnnamedType
);
85 // Ensure nested structs are distinguishable.
87 } else if (FunctionType
*FT
= dyn_cast
<FunctionType
>(Ty
)) {
88 Result
+= "f_" + getMangledTypeStr(FT
->getReturnType(), HasUnnamedType
);
89 for (size_t i
= 0; i
< FT
->getNumParams(); i
++)
90 Result
+= getMangledTypeStr(FT
->getParamType(i
), HasUnnamedType
);
93 // Ensure nested function types are distinguishable.
95 } else if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
)) {
96 ElementCount EC
= VTy
->getElementCount();
99 Result
+= "v" + utostr(EC
.getKnownMinValue()) +
100 getMangledTypeStr(VTy
->getElementType(), HasUnnamedType
);
101 } else if (TargetExtType
*TETy
= dyn_cast
<TargetExtType
>(Ty
)) {
103 Result
+= TETy
->getName();
104 for (Type
*ParamTy
: TETy
->type_params())
105 Result
+= "_" + getMangledTypeStr(ParamTy
, HasUnnamedType
);
106 for (unsigned IntParam
: TETy
->int_params())
107 Result
+= "_" + utostr(IntParam
);
108 // Ensure nested target extension types are distinguishable.
111 switch (Ty
->getTypeID()) {
113 llvm_unreachable("Unhandled type");
117 case Type::MetadataTyID
:
118 Result
+= "Metadata";
123 case Type::BFloatTyID
:
126 case Type::FloatTyID
:
129 case Type::DoubleTyID
:
132 case Type::X86_FP80TyID
:
135 case Type::FP128TyID
:
138 case Type::PPC_FP128TyID
:
141 case Type::X86_AMXTyID
:
144 case Type::IntegerTyID
:
145 Result
+= "i" + utostr(cast
<IntegerType
>(Ty
)->getBitWidth());
152 static std::string
getIntrinsicNameImpl(Intrinsic::ID Id
, ArrayRef
<Type
*> Tys
,
153 Module
*M
, FunctionType
*FT
,
154 bool EarlyModuleCheck
) {
156 assert(Id
< Intrinsic::num_intrinsics
&& "Invalid intrinsic ID!");
157 assert((Tys
.empty() || Intrinsic::isOverloaded(Id
)) &&
158 "This version of getName is for overloaded intrinsics only");
159 (void)EarlyModuleCheck
;
160 assert((!EarlyModuleCheck
|| M
||
161 !any_of(Tys
, [](Type
*T
) { return isa
<PointerType
>(T
); })) &&
162 "Intrinsic overloading on pointer types need to provide a Module");
163 bool HasUnnamedType
= false;
164 std::string
Result(Intrinsic::getBaseName(Id
));
166 Result
+= "." + getMangledTypeStr(Ty
, HasUnnamedType
);
167 if (HasUnnamedType
) {
168 assert(M
&& "unnamed types need a module");
170 FT
= Intrinsic::getType(M
->getContext(), Id
, Tys
);
172 assert((FT
== Intrinsic::getType(M
->getContext(), Id
, Tys
)) &&
173 "Provided FunctionType must match arguments");
174 return M
->getUniqueIntrinsicName(Result
, Id
, FT
);
179 std::string
Intrinsic::getName(ID Id
, ArrayRef
<Type
*> Tys
, Module
*M
,
181 assert(M
&& "We need to have a Module");
182 return getIntrinsicNameImpl(Id
, Tys
, M
, FT
, true);
185 std::string
Intrinsic::getNameNoUnnamedTypes(ID Id
, ArrayRef
<Type
*> Tys
) {
186 return getIntrinsicNameImpl(Id
, Tys
, nullptr, nullptr, false);
189 /// IIT_Info - These are enumerators that describe the entries returned by the
190 /// getIntrinsicInfoTableEntries function.
192 /// Defined in Intrinsics.td.
194 #define GET_INTRINSIC_IITINFO
195 #include "llvm/IR/IntrinsicImpl.inc"
196 #undef GET_INTRINSIC_IITINFO
200 DecodeIITType(unsigned &NextElt
, ArrayRef
<unsigned char> Infos
,
202 SmallVectorImpl
<Intrinsic::IITDescriptor
> &OutputTable
) {
203 using namespace Intrinsic
;
205 bool IsScalableVector
= (LastInfo
== IIT_SCALABLE_VEC
);
207 IIT_Info Info
= IIT_Info(Infos
[NextElt
++]);
208 unsigned StructElts
= 2;
212 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Void
, 0));
215 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::VarArg
, 0));
218 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::MMX
, 0));
221 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::AMX
, 0));
224 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Token
, 0));
227 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Metadata
, 0));
230 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Half
, 0));
233 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::BFloat
, 0));
236 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Float
, 0));
239 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Double
, 0));
242 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Quad
, 0));
245 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::PPCQuad
, 0));
248 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Integer
, 1));
251 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Integer
, 2));
254 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Integer
, 4));
256 case IIT_AARCH64_SVCOUNT
:
257 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::AArch64Svcount
, 0));
260 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Integer
, 8));
263 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Integer
, 16));
266 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Integer
, 32));
269 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Integer
, 64));
272 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Integer
, 128));
275 OutputTable
.push_back(IITDescriptor::getVector(1, IsScalableVector
));
276 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
279 OutputTable
.push_back(IITDescriptor::getVector(2, IsScalableVector
));
280 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
283 OutputTable
.push_back(IITDescriptor::getVector(3, IsScalableVector
));
284 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
287 OutputTable
.push_back(IITDescriptor::getVector(4, IsScalableVector
));
288 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
291 OutputTable
.push_back(IITDescriptor::getVector(6, IsScalableVector
));
292 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
295 OutputTable
.push_back(IITDescriptor::getVector(8, IsScalableVector
));
296 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
299 OutputTable
.push_back(IITDescriptor::getVector(10, IsScalableVector
));
300 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
303 OutputTable
.push_back(IITDescriptor::getVector(16, IsScalableVector
));
304 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
307 OutputTable
.push_back(IITDescriptor::getVector(32, IsScalableVector
));
308 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
311 OutputTable
.push_back(IITDescriptor::getVector(64, IsScalableVector
));
312 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
315 OutputTable
.push_back(IITDescriptor::getVector(128, IsScalableVector
));
316 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
319 OutputTable
.push_back(IITDescriptor::getVector(256, IsScalableVector
));
320 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
323 OutputTable
.push_back(IITDescriptor::getVector(512, IsScalableVector
));
324 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
327 OutputTable
.push_back(IITDescriptor::getVector(1024, IsScalableVector
));
328 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
331 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Pointer
, 10));
334 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Pointer
, 20));
337 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Pointer
, 0));
339 case IIT_ANYPTR
: // [ANYPTR addrspace]
340 OutputTable
.push_back(
341 IITDescriptor::get(IITDescriptor::Pointer
, Infos
[NextElt
++]));
344 unsigned ArgInfo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
345 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Argument
, ArgInfo
));
348 case IIT_EXTEND_ARG
: {
349 unsigned ArgInfo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
350 OutputTable
.push_back(
351 IITDescriptor::get(IITDescriptor::ExtendArgument
, ArgInfo
));
354 case IIT_TRUNC_ARG
: {
355 unsigned ArgInfo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
356 OutputTable
.push_back(
357 IITDescriptor::get(IITDescriptor::TruncArgument
, ArgInfo
));
360 case IIT_HALF_VEC_ARG
: {
361 unsigned ArgInfo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
362 OutputTable
.push_back(
363 IITDescriptor::get(IITDescriptor::HalfVecArgument
, ArgInfo
));
366 case IIT_SAME_VEC_WIDTH_ARG
: {
367 unsigned ArgInfo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
368 OutputTable
.push_back(
369 IITDescriptor::get(IITDescriptor::SameVecWidthArgument
, ArgInfo
));
372 case IIT_VEC_OF_ANYPTRS_TO_ELT
: {
373 unsigned short ArgNo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
374 unsigned short RefNo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
375 OutputTable
.push_back(
376 IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt
, ArgNo
, RefNo
));
379 case IIT_EMPTYSTRUCT
:
380 OutputTable
.push_back(IITDescriptor::get(IITDescriptor::Struct
, 0));
404 OutputTable
.push_back(
405 IITDescriptor::get(IITDescriptor::Struct
, StructElts
));
407 for (unsigned i
= 0; i
!= StructElts
; ++i
)
408 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
411 case IIT_SUBDIVIDE2_ARG
: {
412 unsigned ArgInfo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
413 OutputTable
.push_back(
414 IITDescriptor::get(IITDescriptor::Subdivide2Argument
, ArgInfo
));
417 case IIT_SUBDIVIDE4_ARG
: {
418 unsigned ArgInfo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
419 OutputTable
.push_back(
420 IITDescriptor::get(IITDescriptor::Subdivide4Argument
, ArgInfo
));
423 case IIT_VEC_ELEMENT
: {
424 unsigned ArgInfo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
425 OutputTable
.push_back(
426 IITDescriptor::get(IITDescriptor::VecElementArgument
, ArgInfo
));
429 case IIT_SCALABLE_VEC
: {
430 DecodeIITType(NextElt
, Infos
, Info
, OutputTable
);
433 case IIT_VEC_OF_BITCASTS_TO_INT
: {
434 unsigned ArgInfo
= (NextElt
== Infos
.size() ? 0 : Infos
[NextElt
++]);
435 OutputTable
.push_back(
436 IITDescriptor::get(IITDescriptor::VecOfBitcastsToInt
, ArgInfo
));
440 llvm_unreachable("unhandled");
443 #define GET_INTRINSIC_GENERATOR_GLOBAL
444 #include "llvm/IR/IntrinsicImpl.inc"
445 #undef GET_INTRINSIC_GENERATOR_GLOBAL
447 void Intrinsic::getIntrinsicInfoTableEntries(
448 ID id
, SmallVectorImpl
<IITDescriptor
> &T
) {
449 static_assert(sizeof(IIT_Table
[0]) == 2,
450 "Expect 16-bit entries in IIT_Table");
451 // Check to see if the intrinsic's type was expressible by the table.
452 uint16_t TableVal
= IIT_Table
[id
- 1];
454 // Decode the TableVal into an array of IITValues.
455 SmallVector
<unsigned char> IITValues
;
456 ArrayRef
<unsigned char> IITEntries
;
457 unsigned NextElt
= 0;
458 if (TableVal
>> 15) {
459 // This is an offset into the IIT_LongEncodingTable.
460 IITEntries
= IIT_LongEncodingTable
;
462 // Strip sentinel bit.
463 NextElt
= TableVal
& 0x7fff;
465 // If the entry was encoded into a single word in the table itself, decode
466 // it from an array of nibbles to an array of bytes.
468 IITValues
.push_back(TableVal
& 0xF);
472 IITEntries
= IITValues
;
476 // Okay, decode the table into the output vector of IITDescriptors.
477 DecodeIITType(NextElt
, IITEntries
, IIT_Done
, T
);
478 while (NextElt
!= IITEntries
.size() && IITEntries
[NextElt
] != 0)
479 DecodeIITType(NextElt
, IITEntries
, IIT_Done
, T
);
482 static Type
*DecodeFixedType(ArrayRef
<Intrinsic::IITDescriptor
> &Infos
,
483 ArrayRef
<Type
*> Tys
, LLVMContext
&Context
) {
484 using namespace Intrinsic
;
486 IITDescriptor D
= Infos
.front();
487 Infos
= Infos
.slice(1);
490 case IITDescriptor::Void
:
491 return Type::getVoidTy(Context
);
492 case IITDescriptor::VarArg
:
493 return Type::getVoidTy(Context
);
494 case IITDescriptor::MMX
:
495 return llvm::FixedVectorType::get(llvm::IntegerType::get(Context
, 64), 1);
496 case IITDescriptor::AMX
:
497 return Type::getX86_AMXTy(Context
);
498 case IITDescriptor::Token
:
499 return Type::getTokenTy(Context
);
500 case IITDescriptor::Metadata
:
501 return Type::getMetadataTy(Context
);
502 case IITDescriptor::Half
:
503 return Type::getHalfTy(Context
);
504 case IITDescriptor::BFloat
:
505 return Type::getBFloatTy(Context
);
506 case IITDescriptor::Float
:
507 return Type::getFloatTy(Context
);
508 case IITDescriptor::Double
:
509 return Type::getDoubleTy(Context
);
510 case IITDescriptor::Quad
:
511 return Type::getFP128Ty(Context
);
512 case IITDescriptor::PPCQuad
:
513 return Type::getPPC_FP128Ty(Context
);
514 case IITDescriptor::AArch64Svcount
:
515 return TargetExtType::get(Context
, "aarch64.svcount");
517 case IITDescriptor::Integer
:
518 return IntegerType::get(Context
, D
.Integer_Width
);
519 case IITDescriptor::Vector
:
520 return VectorType::get(DecodeFixedType(Infos
, Tys
, Context
),
522 case IITDescriptor::Pointer
:
523 return PointerType::get(Context
, D
.Pointer_AddressSpace
);
524 case IITDescriptor::Struct
: {
525 SmallVector
<Type
*, 8> Elts
;
526 for (unsigned i
= 0, e
= D
.Struct_NumElements
; i
!= e
; ++i
)
527 Elts
.push_back(DecodeFixedType(Infos
, Tys
, Context
));
528 return StructType::get(Context
, Elts
);
530 case IITDescriptor::Argument
:
531 return Tys
[D
.getArgumentNumber()];
532 case IITDescriptor::ExtendArgument
: {
533 Type
*Ty
= Tys
[D
.getArgumentNumber()];
534 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
535 return VectorType::getExtendedElementVectorType(VTy
);
537 return IntegerType::get(Context
, 2 * cast
<IntegerType
>(Ty
)->getBitWidth());
539 case IITDescriptor::TruncArgument
: {
540 Type
*Ty
= Tys
[D
.getArgumentNumber()];
541 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
542 return VectorType::getTruncatedElementVectorType(VTy
);
544 IntegerType
*ITy
= cast
<IntegerType
>(Ty
);
545 assert(ITy
->getBitWidth() % 2 == 0);
546 return IntegerType::get(Context
, ITy
->getBitWidth() / 2);
548 case IITDescriptor::Subdivide2Argument
:
549 case IITDescriptor::Subdivide4Argument
: {
550 Type
*Ty
= Tys
[D
.getArgumentNumber()];
551 VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
);
552 assert(VTy
&& "Expected an argument of Vector Type");
553 int SubDivs
= D
.Kind
== IITDescriptor::Subdivide2Argument
? 1 : 2;
554 return VectorType::getSubdividedVectorType(VTy
, SubDivs
);
556 case IITDescriptor::HalfVecArgument
:
557 return VectorType::getHalfElementsVectorType(
558 cast
<VectorType
>(Tys
[D
.getArgumentNumber()]));
559 case IITDescriptor::SameVecWidthArgument
: {
560 Type
*EltTy
= DecodeFixedType(Infos
, Tys
, Context
);
561 Type
*Ty
= Tys
[D
.getArgumentNumber()];
562 if (auto *VTy
= dyn_cast
<VectorType
>(Ty
))
563 return VectorType::get(EltTy
, VTy
->getElementCount());
566 case IITDescriptor::VecElementArgument
: {
567 Type
*Ty
= Tys
[D
.getArgumentNumber()];
568 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
569 return VTy
->getElementType();
570 llvm_unreachable("Expected an argument of Vector Type");
572 case IITDescriptor::VecOfBitcastsToInt
: {
573 Type
*Ty
= Tys
[D
.getArgumentNumber()];
574 VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
);
575 assert(VTy
&& "Expected an argument of Vector Type");
576 return VectorType::getInteger(VTy
);
578 case IITDescriptor::VecOfAnyPtrsToElt
:
579 // Return the overloaded type (which determines the pointers address space)
580 return Tys
[D
.getOverloadArgNumber()];
582 llvm_unreachable("unhandled");
585 FunctionType
*Intrinsic::getType(LLVMContext
&Context
, ID id
,
586 ArrayRef
<Type
*> Tys
) {
587 SmallVector
<IITDescriptor
, 8> Table
;
588 getIntrinsicInfoTableEntries(id
, Table
);
590 ArrayRef
<IITDescriptor
> TableRef
= Table
;
591 Type
*ResultTy
= DecodeFixedType(TableRef
, Tys
, Context
);
593 SmallVector
<Type
*, 8> ArgTys
;
594 while (!TableRef
.empty())
595 ArgTys
.push_back(DecodeFixedType(TableRef
, Tys
, Context
));
597 // DecodeFixedType returns Void for IITDescriptor::Void and
598 // IITDescriptor::VarArg If we see void type as the type of the last argument,
599 // it is vararg intrinsic
600 if (!ArgTys
.empty() && ArgTys
.back()->isVoidTy()) {
602 return FunctionType::get(ResultTy
, ArgTys
, true);
604 return FunctionType::get(ResultTy
, ArgTys
, false);
607 bool Intrinsic::isOverloaded(ID id
) {
608 #define GET_INTRINSIC_OVERLOAD_TABLE
609 #include "llvm/IR/IntrinsicImpl.inc"
610 #undef GET_INTRINSIC_OVERLOAD_TABLE
613 /// Table of per-target intrinsic name tables.
614 #define GET_INTRINSIC_TARGET_DATA
615 #include "llvm/IR/IntrinsicImpl.inc"
616 #undef GET_INTRINSIC_TARGET_DATA
618 bool Intrinsic::isTargetIntrinsic(Intrinsic::ID IID
) {
619 return IID
> TargetInfos
[0].Count
;
622 /// Looks up Name in NameTable via binary search. NameTable must be sorted
623 /// and all entries must start with "llvm.". If NameTable contains an exact
624 /// match for Name or a prefix of Name followed by a dot, its index in
625 /// NameTable is returned. Otherwise, -1 is returned.
626 static int lookupLLVMIntrinsicByName(ArrayRef
<unsigned> NameOffsetTable
,
627 StringRef Name
, StringRef Target
= "") {
628 assert(Name
.starts_with("llvm.") && "Unexpected intrinsic prefix");
629 assert(Name
.drop_front(5).starts_with(Target
) && "Unexpected target");
631 // Do successive binary searches of the dotted name components. For
632 // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
633 // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
634 // "llvm.gc.experimental.statepoint", and then we will stop as the range is
635 // size 1. During the search, we can skip the prefix that we already know is
636 // identical. By using strncmp we consider names with differing suffixes to
637 // be part of the equal range.
638 size_t CmpEnd
= 4; // Skip the "llvm" component.
640 CmpEnd
+= 1 + Target
.size(); // skip the .target component.
642 const unsigned *Low
= NameOffsetTable
.begin();
643 const unsigned *High
= NameOffsetTable
.end();
644 const unsigned *LastLow
= Low
;
645 while (CmpEnd
< Name
.size() && High
- Low
> 0) {
646 size_t CmpStart
= CmpEnd
;
647 CmpEnd
= Name
.find('.', CmpStart
+ 1);
648 CmpEnd
= CmpEnd
== StringRef::npos
? Name
.size() : CmpEnd
;
649 auto Cmp
= [CmpStart
, CmpEnd
](auto LHS
, auto RHS
) {
650 // `equal_range` requires the comparison to work with either side being an
651 // offset or the value. Detect which kind each side is to set up the
654 if constexpr (std::is_integral_v
<decltype(LHS
)>) {
655 LHSStr
= IntrinsicNameTable
[LHS
];
660 if constexpr (std::is_integral_v
<decltype(RHS
)>) {
661 RHSStr
= IntrinsicNameTable
[RHS
];
665 return strncmp(LHSStr
.data() + CmpStart
, RHSStr
.data() + CmpStart
,
666 CmpEnd
- CmpStart
) < 0;
669 std::tie(Low
, High
) = std::equal_range(Low
, High
, Name
.data(), Cmp
);
674 if (LastLow
== NameOffsetTable
.end())
676 StringRef NameFound
= IntrinsicNameTable
[*LastLow
];
677 if (Name
== NameFound
||
678 (Name
.starts_with(NameFound
) && Name
[NameFound
.size()] == '.'))
679 return LastLow
- NameOffsetTable
.begin();
683 /// Find the segment of \c IntrinsicNameOffsetTable for intrinsics with the same
684 /// target as \c Name, or the generic table if \c Name is not target specific.
686 /// Returns the relevant slice of \c IntrinsicNameOffsetTable and the target
688 static std::pair
<ArrayRef
<unsigned>, StringRef
>
689 findTargetSubtable(StringRef Name
) {
690 assert(Name
.starts_with("llvm."));
692 ArrayRef
<IntrinsicTargetInfo
> Targets(TargetInfos
);
693 // Drop "llvm." and take the first dotted component. That will be the target
694 // if this is target specific.
695 StringRef Target
= Name
.drop_front(5).split('.').first
;
696 auto It
= partition_point(
697 Targets
, [=](const IntrinsicTargetInfo
&TI
) { return TI
.Name
< Target
; });
698 // We've either found the target or just fall back to the generic set, which
700 const auto &TI
= It
!= Targets
.end() && It
->Name
== Target
? *It
: Targets
[0];
701 return {ArrayRef(&IntrinsicNameOffsetTable
[1] + TI
.Offset
, TI
.Count
),
705 /// This does the actual lookup of an intrinsic ID which matches the given
707 Intrinsic::ID
Intrinsic::lookupIntrinsicID(StringRef Name
) {
708 auto [NameOffsetTable
, Target
] = findTargetSubtable(Name
);
709 int Idx
= lookupLLVMIntrinsicByName(NameOffsetTable
, Name
, Target
);
711 return Intrinsic::not_intrinsic
;
713 // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
714 // an index into a sub-table.
715 int Adjust
= NameOffsetTable
.data() - IntrinsicNameOffsetTable
;
716 Intrinsic::ID ID
= static_cast<Intrinsic::ID
>(Idx
+ Adjust
);
718 // If the intrinsic is not overloaded, require an exact match. If it is
719 // overloaded, require either exact or prefix match.
720 const auto MatchSize
= IntrinsicNameTable
[NameOffsetTable
[Idx
]].size();
721 assert(Name
.size() >= MatchSize
&& "Expected either exact or prefix match");
722 bool IsExactMatch
= Name
.size() == MatchSize
;
723 return IsExactMatch
|| Intrinsic::isOverloaded(ID
) ? ID
724 : Intrinsic::not_intrinsic
;
727 /// This defines the "Intrinsic::getAttributes(ID id)" method.
728 #define GET_INTRINSIC_ATTRIBUTES
729 #include "llvm/IR/IntrinsicImpl.inc"
730 #undef GET_INTRINSIC_ATTRIBUTES
732 Function
*Intrinsic::getOrInsertDeclaration(Module
*M
, ID id
,
733 ArrayRef
<Type
*> Tys
) {
734 // There can never be multiple globals with the same name of different types,
735 // because intrinsics must be a specific type.
736 auto *FT
= getType(M
->getContext(), id
, Tys
);
737 return cast
<Function
>(
738 M
->getOrInsertFunction(
739 Tys
.empty() ? getName(id
) : getName(id
, Tys
, M
, FT
), FT
)
743 Function
*Intrinsic::getDeclarationIfExists(const Module
*M
, ID id
) {
744 return M
->getFunction(getName(id
));
747 Function
*Intrinsic::getDeclarationIfExists(Module
*M
, ID id
,
748 ArrayRef
<Type
*> Tys
,
750 return M
->getFunction(getName(id
, Tys
, M
, FT
));
753 // This defines the "Intrinsic::getIntrinsicForClangBuiltin()" method.
754 #define GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
755 #include "llvm/IR/IntrinsicImpl.inc"
756 #undef GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
758 // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
759 #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
760 #include "llvm/IR/IntrinsicImpl.inc"
761 #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
763 bool Intrinsic::isConstrainedFPIntrinsic(ID QID
) {
765 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
766 case Intrinsic::INTRINSIC:
767 #include "llvm/IR/ConstrainedOps.def"
775 bool Intrinsic::hasConstrainedFPRoundingModeOperand(Intrinsic::ID QID
) {
777 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
778 case Intrinsic::INTRINSIC: \
779 return ROUND_MODE == 1;
780 #include "llvm/IR/ConstrainedOps.def"
787 using DeferredIntrinsicMatchPair
=
788 std::pair
<Type
*, ArrayRef
<Intrinsic::IITDescriptor
>>;
791 matchIntrinsicType(Type
*Ty
, ArrayRef
<Intrinsic::IITDescriptor
> &Infos
,
792 SmallVectorImpl
<Type
*> &ArgTys
,
793 SmallVectorImpl
<DeferredIntrinsicMatchPair
> &DeferredChecks
,
794 bool IsDeferredCheck
) {
795 using namespace Intrinsic
;
797 // If we ran out of descriptors, there are too many arguments.
801 // Do this before slicing off the 'front' part
802 auto InfosRef
= Infos
;
803 auto DeferCheck
= [&DeferredChecks
, &InfosRef
](Type
*T
) {
804 DeferredChecks
.emplace_back(T
, InfosRef
);
808 IITDescriptor D
= Infos
.front();
809 Infos
= Infos
.slice(1);
812 case IITDescriptor::Void
:
813 return !Ty
->isVoidTy();
814 case IITDescriptor::VarArg
:
816 case IITDescriptor::MMX
: {
817 FixedVectorType
*VT
= dyn_cast
<FixedVectorType
>(Ty
);
818 return !VT
|| VT
->getNumElements() != 1 ||
819 !VT
->getElementType()->isIntegerTy(64);
821 case IITDescriptor::AMX
:
822 return !Ty
->isX86_AMXTy();
823 case IITDescriptor::Token
:
824 return !Ty
->isTokenTy();
825 case IITDescriptor::Metadata
:
826 return !Ty
->isMetadataTy();
827 case IITDescriptor::Half
:
828 return !Ty
->isHalfTy();
829 case IITDescriptor::BFloat
:
830 return !Ty
->isBFloatTy();
831 case IITDescriptor::Float
:
832 return !Ty
->isFloatTy();
833 case IITDescriptor::Double
:
834 return !Ty
->isDoubleTy();
835 case IITDescriptor::Quad
:
836 return !Ty
->isFP128Ty();
837 case IITDescriptor::PPCQuad
:
838 return !Ty
->isPPC_FP128Ty();
839 case IITDescriptor::Integer
:
840 return !Ty
->isIntegerTy(D
.Integer_Width
);
841 case IITDescriptor::AArch64Svcount
:
842 return !isa
<TargetExtType
>(Ty
) ||
843 cast
<TargetExtType
>(Ty
)->getName() != "aarch64.svcount";
844 case IITDescriptor::Vector
: {
845 VectorType
*VT
= dyn_cast
<VectorType
>(Ty
);
846 return !VT
|| VT
->getElementCount() != D
.Vector_Width
||
847 matchIntrinsicType(VT
->getElementType(), Infos
, ArgTys
,
848 DeferredChecks
, IsDeferredCheck
);
850 case IITDescriptor::Pointer
: {
851 PointerType
*PT
= dyn_cast
<PointerType
>(Ty
);
852 return !PT
|| PT
->getAddressSpace() != D
.Pointer_AddressSpace
;
855 case IITDescriptor::Struct
: {
856 StructType
*ST
= dyn_cast
<StructType
>(Ty
);
857 if (!ST
|| !ST
->isLiteral() || ST
->isPacked() ||
858 ST
->getNumElements() != D
.Struct_NumElements
)
861 for (unsigned i
= 0, e
= D
.Struct_NumElements
; i
!= e
; ++i
)
862 if (matchIntrinsicType(ST
->getElementType(i
), Infos
, ArgTys
,
863 DeferredChecks
, IsDeferredCheck
))
868 case IITDescriptor::Argument
:
869 // If this is the second occurrence of an argument,
870 // verify that the later instance matches the previous instance.
871 if (D
.getArgumentNumber() < ArgTys
.size())
872 return Ty
!= ArgTys
[D
.getArgumentNumber()];
874 if (D
.getArgumentNumber() > ArgTys
.size() ||
875 D
.getArgumentKind() == IITDescriptor::AK_MatchType
)
876 return IsDeferredCheck
|| DeferCheck(Ty
);
878 assert(D
.getArgumentNumber() == ArgTys
.size() && !IsDeferredCheck
&&
879 "Table consistency error");
880 ArgTys
.push_back(Ty
);
882 switch (D
.getArgumentKind()) {
883 case IITDescriptor::AK_Any
:
884 return false; // Success
885 case IITDescriptor::AK_AnyInteger
:
886 return !Ty
->isIntOrIntVectorTy();
887 case IITDescriptor::AK_AnyFloat
:
888 return !Ty
->isFPOrFPVectorTy();
889 case IITDescriptor::AK_AnyVector
:
890 return !isa
<VectorType
>(Ty
);
891 case IITDescriptor::AK_AnyPointer
:
892 return !isa
<PointerType
>(Ty
);
896 llvm_unreachable("all argument kinds not covered");
898 case IITDescriptor::ExtendArgument
: {
899 // If this is a forward reference, defer the check for later.
900 if (D
.getArgumentNumber() >= ArgTys
.size())
901 return IsDeferredCheck
|| DeferCheck(Ty
);
903 Type
*NewTy
= ArgTys
[D
.getArgumentNumber()];
904 if (VectorType
*VTy
= dyn_cast
<VectorType
>(NewTy
))
905 NewTy
= VectorType::getExtendedElementVectorType(VTy
);
906 else if (IntegerType
*ITy
= dyn_cast
<IntegerType
>(NewTy
))
907 NewTy
= IntegerType::get(ITy
->getContext(), 2 * ITy
->getBitWidth());
913 case IITDescriptor::TruncArgument
: {
914 // If this is a forward reference, defer the check for later.
915 if (D
.getArgumentNumber() >= ArgTys
.size())
916 return IsDeferredCheck
|| DeferCheck(Ty
);
918 Type
*NewTy
= ArgTys
[D
.getArgumentNumber()];
919 if (VectorType
*VTy
= dyn_cast
<VectorType
>(NewTy
))
920 NewTy
= VectorType::getTruncatedElementVectorType(VTy
);
921 else if (IntegerType
*ITy
= dyn_cast
<IntegerType
>(NewTy
))
922 NewTy
= IntegerType::get(ITy
->getContext(), ITy
->getBitWidth() / 2);
928 case IITDescriptor::HalfVecArgument
:
929 // If this is a forward reference, defer the check for later.
930 if (D
.getArgumentNumber() >= ArgTys
.size())
931 return IsDeferredCheck
|| DeferCheck(Ty
);
932 return !isa
<VectorType
>(ArgTys
[D
.getArgumentNumber()]) ||
933 VectorType::getHalfElementsVectorType(
934 cast
<VectorType
>(ArgTys
[D
.getArgumentNumber()])) != Ty
;
935 case IITDescriptor::SameVecWidthArgument
: {
936 if (D
.getArgumentNumber() >= ArgTys
.size()) {
937 // Defer check and subsequent check for the vector element type.
938 Infos
= Infos
.slice(1);
939 return IsDeferredCheck
|| DeferCheck(Ty
);
941 auto *ReferenceType
= dyn_cast
<VectorType
>(ArgTys
[D
.getArgumentNumber()]);
942 auto *ThisArgType
= dyn_cast
<VectorType
>(Ty
);
943 // Both must be vectors of the same number of elements or neither.
944 if ((ReferenceType
!= nullptr) != (ThisArgType
!= nullptr))
948 if (ReferenceType
->getElementCount() != ThisArgType
->getElementCount())
950 EltTy
= ThisArgType
->getElementType();
952 return matchIntrinsicType(EltTy
, Infos
, ArgTys
, DeferredChecks
,
955 case IITDescriptor::VecOfAnyPtrsToElt
: {
956 unsigned RefArgNumber
= D
.getRefArgNumber();
957 if (RefArgNumber
>= ArgTys
.size()) {
960 // If forward referencing, already add the pointer-vector type and
961 // defer the checks for later.
962 ArgTys
.push_back(Ty
);
963 return DeferCheck(Ty
);
966 if (!IsDeferredCheck
) {
967 assert(D
.getOverloadArgNumber() == ArgTys
.size() &&
968 "Table consistency error");
969 ArgTys
.push_back(Ty
);
972 // Verify the overloaded type "matches" the Ref type.
973 // i.e. Ty is a vector with the same width as Ref.
974 // Composed of pointers to the same element type as Ref.
975 auto *ReferenceType
= dyn_cast
<VectorType
>(ArgTys
[RefArgNumber
]);
976 auto *ThisArgVecTy
= dyn_cast
<VectorType
>(Ty
);
977 if (!ThisArgVecTy
|| !ReferenceType
||
978 (ReferenceType
->getElementCount() != ThisArgVecTy
->getElementCount()))
980 return !ThisArgVecTy
->getElementType()->isPointerTy();
982 case IITDescriptor::VecElementArgument
: {
983 if (D
.getArgumentNumber() >= ArgTys
.size())
984 return IsDeferredCheck
? true : DeferCheck(Ty
);
985 auto *ReferenceType
= dyn_cast
<VectorType
>(ArgTys
[D
.getArgumentNumber()]);
986 return !ReferenceType
|| Ty
!= ReferenceType
->getElementType();
988 case IITDescriptor::Subdivide2Argument
:
989 case IITDescriptor::Subdivide4Argument
: {
990 // If this is a forward reference, defer the check for later.
991 if (D
.getArgumentNumber() >= ArgTys
.size())
992 return IsDeferredCheck
|| DeferCheck(Ty
);
994 Type
*NewTy
= ArgTys
[D
.getArgumentNumber()];
995 if (auto *VTy
= dyn_cast
<VectorType
>(NewTy
)) {
996 int SubDivs
= D
.Kind
== IITDescriptor::Subdivide2Argument
? 1 : 2;
997 NewTy
= VectorType::getSubdividedVectorType(VTy
, SubDivs
);
1002 case IITDescriptor::VecOfBitcastsToInt
: {
1003 if (D
.getArgumentNumber() >= ArgTys
.size())
1004 return IsDeferredCheck
|| DeferCheck(Ty
);
1005 auto *ReferenceType
= dyn_cast
<VectorType
>(ArgTys
[D
.getArgumentNumber()]);
1006 auto *ThisArgVecTy
= dyn_cast
<VectorType
>(Ty
);
1007 if (!ThisArgVecTy
|| !ReferenceType
)
1009 return ThisArgVecTy
!= VectorType::getInteger(ReferenceType
);
1012 llvm_unreachable("unhandled");
1015 Intrinsic::MatchIntrinsicTypesResult
1016 Intrinsic::matchIntrinsicSignature(FunctionType
*FTy
,
1017 ArrayRef
<Intrinsic::IITDescriptor
> &Infos
,
1018 SmallVectorImpl
<Type
*> &ArgTys
) {
1019 SmallVector
<DeferredIntrinsicMatchPair
, 2> DeferredChecks
;
1020 if (matchIntrinsicType(FTy
->getReturnType(), Infos
, ArgTys
, DeferredChecks
,
1022 return MatchIntrinsicTypes_NoMatchRet
;
1024 unsigned NumDeferredReturnChecks
= DeferredChecks
.size();
1026 for (auto *Ty
: FTy
->params())
1027 if (matchIntrinsicType(Ty
, Infos
, ArgTys
, DeferredChecks
, false))
1028 return MatchIntrinsicTypes_NoMatchArg
;
1030 for (unsigned I
= 0, E
= DeferredChecks
.size(); I
!= E
; ++I
) {
1031 DeferredIntrinsicMatchPair
&Check
= DeferredChecks
[I
];
1032 if (matchIntrinsicType(Check
.first
, Check
.second
, ArgTys
, DeferredChecks
,
1034 return I
< NumDeferredReturnChecks
? MatchIntrinsicTypes_NoMatchRet
1035 : MatchIntrinsicTypes_NoMatchArg
;
1038 return MatchIntrinsicTypes_Match
;
1041 bool Intrinsic::matchIntrinsicVarArg(
1042 bool isVarArg
, ArrayRef
<Intrinsic::IITDescriptor
> &Infos
) {
1043 // If there are no descriptors left, then it can't be a vararg.
1047 // There should be only one descriptor remaining at this point.
1048 if (Infos
.size() != 1)
1051 // Check and verify the descriptor.
1052 IITDescriptor D
= Infos
.front();
1053 Infos
= Infos
.slice(1);
1054 if (D
.Kind
== IITDescriptor::VarArg
)
1060 bool Intrinsic::getIntrinsicSignature(Intrinsic::ID ID
, FunctionType
*FT
,
1061 SmallVectorImpl
<Type
*> &ArgTys
) {
1065 SmallVector
<Intrinsic::IITDescriptor
, 8> Table
;
1066 getIntrinsicInfoTableEntries(ID
, Table
);
1067 ArrayRef
<Intrinsic::IITDescriptor
> TableRef
= Table
;
1069 if (Intrinsic::matchIntrinsicSignature(FT
, TableRef
, ArgTys
) !=
1070 Intrinsic::MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match
) {
1073 if (Intrinsic::matchIntrinsicVarArg(FT
->isVarArg(), TableRef
))
1078 bool Intrinsic::getIntrinsicSignature(Function
*F
,
1079 SmallVectorImpl
<Type
*> &ArgTys
) {
1080 return getIntrinsicSignature(F
->getIntrinsicID(), F
->getFunctionType(),
1084 std::optional
<Function
*> Intrinsic::remangleIntrinsicFunction(Function
*F
) {
1085 SmallVector
<Type
*, 4> ArgTys
;
1086 if (!getIntrinsicSignature(F
, ArgTys
))
1087 return std::nullopt
;
1089 Intrinsic::ID ID
= F
->getIntrinsicID();
1090 StringRef Name
= F
->getName();
1091 std::string WantedName
=
1092 Intrinsic::getName(ID
, ArgTys
, F
->getParent(), F
->getFunctionType());
1093 if (Name
== WantedName
)
1094 return std::nullopt
;
1096 Function
*NewDecl
= [&] {
1097 if (auto *ExistingGV
= F
->getParent()->getNamedValue(WantedName
)) {
1098 if (auto *ExistingF
= dyn_cast
<Function
>(ExistingGV
))
1099 if (ExistingF
->getFunctionType() == F
->getFunctionType())
1102 // The name already exists, but is not a function or has the wrong
1103 // prototype. Make place for the new one by renaming the old version.
1104 // Either this old version will be removed later on or the module is
1105 // invalid and we'll get an error.
1106 ExistingGV
->setName(WantedName
+ ".renamed");
1108 return Intrinsic::getOrInsertDeclaration(F
->getParent(), ID
, ArgTys
);
1111 NewDecl
->setCallingConv(F
->getCallingConv());
1112 assert(NewDecl
->getFunctionType() == F
->getFunctionType() &&
1113 "Shouldn't change the signature");