1 //===- Intrinsics.h - LLVM 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 defines a set of enums which allow processing of intrinsic
10 // functions. Values of these enum types are returned by
11 // Function::getIntrinsicID.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_INTRINSICS_H
16 #define LLVM_IR_INTRINSICS_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/Optional.h"
32 /// This namespace contains an enum with a value for every intrinsic/builtin
33 /// function known by LLVM. The enum values are returned by
34 /// Function::getIntrinsicID().
37 not_intrinsic
= 0, // Must be zero
39 // Get the intrinsic enums generated from Intrinsics.td
40 #define GET_INTRINSIC_ENUM_VALUES
41 #include "llvm/IR/IntrinsicEnums.inc"
42 #undef GET_INTRINSIC_ENUM_VALUES
46 /// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
47 /// Note, this version is for intrinsics with no overloads. Use the other
48 /// version of getName if overloads are required.
49 StringRef
getName(ID id
);
51 /// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
52 /// Note, this version of getName supports overloads, but is less efficient
53 /// than the StringRef version of this function. If no overloads are
54 /// requried, it is safe to use this version, but better to use the StringRef
56 std::string
getName(ID id
, ArrayRef
<Type
*> Tys
);
58 /// Return the function type for an intrinsic.
59 FunctionType
*getType(LLVMContext
&Context
, ID id
,
60 ArrayRef
<Type
*> Tys
= None
);
62 /// Returns true if the intrinsic can be overloaded.
63 bool isOverloaded(ID id
);
65 /// Returns true if the intrinsic is a leaf, i.e. it does not make any calls
66 /// itself. Most intrinsics are leafs, the exceptions being the patchpoint
67 /// and statepoint intrinsics. These call (or invoke) their "target" argument.
70 /// Return the attributes for an intrinsic.
71 AttributeList
getAttributes(LLVMContext
&C
, ID id
);
73 /// Create or insert an LLVM Function declaration for an intrinsic, and return
76 /// The Tys parameter is for intrinsics with overloaded types (e.g., those
77 /// using iAny, fAny, vAny, or iPTRAny). For a declaration of an overloaded
78 /// intrinsic, Tys must provide exactly one type for each overloaded type in
80 Function
*getDeclaration(Module
*M
, ID id
, ArrayRef
<Type
*> Tys
= None
);
82 /// Looks up Name in NameTable via binary search. NameTable must be sorted
83 /// and all entries must start with "llvm.". If NameTable contains an exact
84 /// match for Name or a prefix of Name followed by a dot, its index in
85 /// NameTable is returned. Otherwise, -1 is returned.
86 int lookupLLVMIntrinsicByName(ArrayRef
<const char *> NameTable
,
89 /// Map a GCC builtin name to an intrinsic ID.
90 ID
getIntrinsicForGCCBuiltin(const char *Prefix
, StringRef BuiltinName
);
92 /// Map a MS builtin name to an intrinsic ID.
93 ID
getIntrinsicForMSBuiltin(const char *Prefix
, StringRef BuiltinName
);
95 /// This is a type descriptor which explains the type requirements of an
96 /// intrinsic. This is returned by getIntrinsicInfoTableEntries.
97 struct IITDescriptor
{
98 enum IITDescriptorKind
{
99 Void
, VarArg
, MMX
, Token
, Metadata
, Half
, Float
, Double
, Quad
,
100 Integer
, Vector
, Pointer
, Struct
,
101 Argument
, ExtendArgument
, TruncArgument
, HalfVecArgument
,
102 SameVecWidthArgument
, PtrToArgument
, PtrToElt
, VecOfAnyPtrsToElt
,
103 VecElementArgument
, ScalableVecArgument
, Subdivide2Argument
,
104 Subdivide4Argument
, VecOfBitcastsToInt
108 unsigned Integer_Width
;
109 unsigned Float_Width
;
110 unsigned Vector_Width
;
111 unsigned Pointer_AddressSpace
;
112 unsigned Struct_NumElements
;
113 unsigned Argument_Info
;
125 unsigned getArgumentNumber() const {
126 assert(Kind
== Argument
|| Kind
== ExtendArgument
||
127 Kind
== TruncArgument
|| Kind
== HalfVecArgument
||
128 Kind
== SameVecWidthArgument
|| Kind
== PtrToArgument
||
129 Kind
== PtrToElt
|| Kind
== VecElementArgument
||
130 Kind
== Subdivide2Argument
|| Kind
== Subdivide4Argument
||
131 Kind
== VecOfBitcastsToInt
);
132 return Argument_Info
>> 3;
134 ArgKind
getArgumentKind() const {
135 assert(Kind
== Argument
|| Kind
== ExtendArgument
||
136 Kind
== TruncArgument
|| Kind
== HalfVecArgument
||
137 Kind
== SameVecWidthArgument
|| Kind
== PtrToArgument
||
138 Kind
== VecElementArgument
|| Kind
== Subdivide2Argument
||
139 Kind
== Subdivide4Argument
|| Kind
== VecOfBitcastsToInt
);
140 return (ArgKind
)(Argument_Info
& 7);
143 // VecOfAnyPtrsToElt uses both an overloaded argument (for address space)
144 // and a reference argument (for matching vector width and element types)
145 unsigned getOverloadArgNumber() const {
146 assert(Kind
== VecOfAnyPtrsToElt
);
147 return Argument_Info
>> 16;
149 unsigned getRefArgNumber() const {
150 assert(Kind
== VecOfAnyPtrsToElt
);
151 return Argument_Info
& 0xFFFF;
154 static IITDescriptor
get(IITDescriptorKind K
, unsigned Field
) {
155 IITDescriptor Result
= { K
, { Field
} };
159 static IITDescriptor
get(IITDescriptorKind K
, unsigned short Hi
,
161 unsigned Field
= Hi
<< 16 | Lo
;
162 IITDescriptor Result
= {K
, {Field
}};
167 /// Return the IIT table descriptor for the specified intrinsic into an array
168 /// of IITDescriptors.
169 void getIntrinsicInfoTableEntries(ID id
, SmallVectorImpl
<IITDescriptor
> &T
);
171 enum MatchIntrinsicTypesResult
{
172 MatchIntrinsicTypes_Match
= 0,
173 MatchIntrinsicTypes_NoMatchRet
= 1,
174 MatchIntrinsicTypes_NoMatchArg
= 2,
177 /// Match the specified function type with the type constraints specified by
178 /// the .td file. If the given type is an overloaded type it is pushed to the
181 /// Returns false if the given type matches with the constraints, true
183 MatchIntrinsicTypesResult
184 matchIntrinsicSignature(FunctionType
*FTy
, ArrayRef
<IITDescriptor
> &Infos
,
185 SmallVectorImpl
<Type
*> &ArgTys
);
187 /// Verify if the intrinsic has variable arguments. This method is intended to
188 /// be called after all the fixed arguments have been matched first.
190 /// This method returns true on error.
191 bool matchIntrinsicVarArg(bool isVarArg
, ArrayRef
<IITDescriptor
> &Infos
);
193 // Checks if the intrinsic name matches with its signature and if not
194 // returns the declaration with the same signature and remangled name.
195 llvm::Optional
<Function
*> remangleIntrinsicFunction(Function
*F
);
197 } // End Intrinsic namespace
199 } // End llvm namespace