LLVM 23.0.0git
Intrinsics.h
Go to the documentation of this file.
1//===- Intrinsics.h - LLVM Intrinsic Function Handling ----------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_INTRINSICS_H
16#define LLVM_IR_INTRINSICS_H
17
18#include "llvm/ADT/ArrayRef.h"
21#include <optional>
22#include <string>
23
24namespace llvm {
25
26class Type;
27class FunctionType;
28class Function;
29class LLVMContext;
30class Module;
31class AttributeList;
32class AttributeSet;
33class raw_ostream;
34class Constant;
35
36/// This namespace contains an enum with a value for every intrinsic/builtin
37/// function known by LLVM. The enum values are returned by
38/// Function::getIntrinsicID().
39namespace Intrinsic {
40 // Abstraction for the arguments of the noalias intrinsics
41 static const int NoAliasScopeDeclScopeArg = 0;
42
43 // Intrinsic ID type. This is an opaque typedef to facilitate splitting up
44 // the enum into target-specific enums.
45 typedef unsigned ID;
46
47 enum IndependentIntrinsics : unsigned {
48 not_intrinsic = 0, // Must be zero
49
50 // Get the intrinsic enums generated from Intrinsics.td
51#define GET_INTRINSIC_ENUM_VALUES
52#include "llvm/IR/IntrinsicEnums.inc"
53 };
54
55 /// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
56 /// Note, this version is for intrinsics with no overloads. Use the other
57 /// version of getName if overloads are required.
58 LLVM_ABI StringRef getName(ID id);
59
60 /// Return the LLVM name for an intrinsic, without encoded types for
61 /// overloading, such as "llvm.ssa.copy".
62 LLVM_ABI StringRef getBaseName(ID id);
63
64 /// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx" or
65 /// "llvm.ssa.copy.p0s_s.1". Note, this version of getName supports overloads.
66 /// This is less efficient than the StringRef version of this function. If no
67 /// overloads are required, it is safe to use this version, but better to use
68 /// the StringRef version. If one of the types is based on an unnamed type, a
69 /// function type will be computed. Providing FT will avoid this computation.
70 LLVM_ABI std::string getName(ID Id, ArrayRef<Type *> OverloadTys, Module *M,
71 FunctionType *FT = nullptr);
72
73 /// Return the LLVM name for an intrinsic. This is a special version only to
74 /// be used by LLVMIntrinsicCopyOverloadedName. It only supports overloads
75 /// based on named types.
76 LLVM_ABI std::string getNameNoUnnamedTypes(ID Id,
77 ArrayRef<Type *> OverloadTys);
78
79 /// Return the function type for an intrinsic.
80 LLVM_ABI FunctionType *getType(LLVMContext &Context, ID id,
81 ArrayRef<Type *> OverloadTys = {});
82
83 /// Returns true if the intrinsic can be overloaded.
84 LLVM_ABI bool isOverloaded(ID id);
85
86 /// Returns true if the intrinsic has pretty printed immediate arguments.
88
89 /// isTargetIntrinsic - Returns true if IID is an intrinsic specific to a
90 /// certain target. If it is a generic intrinsic false is returned.
92
93 LLVM_ABI ID lookupIntrinsicID(StringRef Name);
94
95 /// Return the attributes for an intrinsic.
97
98 /// Return the function attributes for an intrinsic.
100
101 /// Look up the Function declaration of the intrinsic \p id in the Module
102 /// \p M. If it does not exist, add a declaration and return it. Otherwise,
103 /// return the existing declaration.
104 ///
105 /// The \p Tys parameter is for intrinsics with overloaded types (e.g., those
106 /// using iAny, fAny, vAny, or pAny). For a declaration of an overloaded
107 /// intrinsic, Tys must provide exactly one type for each overloaded type in
108 /// the intrinsic.
110 ArrayRef<Type *> Tys = {});
111
112 /// Look up the Function declaration of the intrinsic \p IID in the Module
113 /// \p M. If it does not exist, add a declaration and return it. Otherwise,
114 /// return the existing declaration.
115 ///
116 /// This overload automatically resolves overloaded intrinsics based on the
117 /// provided return type and argument types. For non-overloaded intrinsics,
118 /// the return type and argument types are ignored.
119 ///
120 /// \param M - The module to get or insert the intrinsic declaration.
121 /// \param IID - The intrinsic ID.
122 /// \param RetTy - The return type of the intrinsic.
123 /// \param ArgTys - The argument types of the intrinsic.
125 ArrayRef<Type *> ArgTys);
126
127 /// Look up the Function declaration of the intrinsic \p id in the Module
128 /// \p M and return it if it exists. Otherwise, return nullptr. This version
129 /// supports non-overloaded intrinsics.
131
132 /// This version supports overloaded intrinsics.
135 FunctionType *FT = nullptr);
136
137 /// Map a Clang builtin name to an intrinsic ID.
139 StringRef BuiltinName);
140
141 /// Map a MS builtin name to an intrinsic ID.
143 StringRef BuiltinName);
144
145 /// Returns true if the intrinsic ID is for one of the "Constrained
146 /// Floating-Point Intrinsics".
148
149 /// Returns true if the intrinsic ID is for one of the "Constrained
150 /// Floating-Point Intrinsics" that take rounding mode metadata.
152
153 /// This is a type descriptor which explains the type requirements of an
154 /// intrinsic. This is returned by getIntrinsicInfoTableEntries.
157 // Concrete types. Additional qualifiers listed in comments.
168 Integer, // Width of the integer in IntegerWidth.
169 Vector, // Width of the vector in VectorWidth.
170 Pointer, // Address space of the pointer in PointerAddressSpace.
171 Struct, // Number of elements in StructNumElements.
175
176 // Overloaded type.
177 Overloaded, // AnyKind and overload index in OverloadInfo.
178
179 // Fully dependent types. Overload index in OverloadInfo.
188
189 // Partially dependent types. Overload index (self and of the overload
190 // type it depends on) in OverloadInfo.
192
193 } Kind;
194
195 union {
196 unsigned IntegerWidth;
199 unsigned OverloadInfo;
201 };
202
203 // AK_% : Defined in Intrinsics.td
204 enum AnyKind {
205#define GET_INTRINSIC_ANYKIND
206#include "llvm/IR/IntrinsicEnums.inc"
207 };
208
209 unsigned getOverloadIndex() const {
210 assert(Kind == Overloaded || Kind == Extend || Kind == Trunc ||
214 // Overload index is packed into lower 5 bits.
215 return OverloadInfo & 0x1f;
216 }
217
219 // Overload kind is packed into upper 3 bits.
221 return (AnyKind)((OverloadInfo >> 5) & 0x7);
222 }
223
224 // OneNthEltsVecArguments uses both a divisor N and a reference argument for
225 // the full-width vector to match
226 unsigned getVectorDivisor() const {
228 return OverloadInfo >> 16;
229 }
230
231 unsigned getRefOverloadIndex() const {
233 return OverloadInfo >> 16;
234 }
235
237 IITDescriptor Result = { K, { Field } };
238 return Result;
239 }
240
241 static IITDescriptor get(IITDescriptorKind K, unsigned short Hi,
242 unsigned short Lo) {
243 unsigned Field = Hi << 16 | Lo;
244 IITDescriptor Result = {K, {Field}};
245 return Result;
246 }
247
248 static IITDescriptor getVector(unsigned Width, bool IsScalable) {
249 IITDescriptor Result = {Vector, {0}};
250 Result.VectorWidth = ElementCount::get(Width, IsScalable);
251 return Result;
252 }
253 };
254
255 /// Return the IIT table descriptor for the specified intrinsic into an array
256 /// of IITDescriptors.
259
265
266 /// Match the specified function type with the type constraints specified by
267 /// the .td file. If the given type is an overloaded type it is pushed to the
268 /// ArgTys vector.
269 ///
270 /// Returns false if the given type matches with the constraints, true
271 /// otherwise.
275
276 /// Verify if the intrinsic has variable arguments. This method is intended to
277 /// be called after all the fixed arguments have been matched first.
278 ///
279 /// This method returns true on error.
280 LLVM_ABI bool matchIntrinsicVarArg(bool isVarArg,
282
283 /// Gets the type arguments of an intrinsic call by matching type contraints
284 /// specified by the .td file. The overloaded types are pushed into the
285 /// AgTys vector.
286 ///
287 /// Returns false if the given ID and function type combination is not a
288 /// valid intrinsic call.
291
292 /// Same as previous, but accepts a Function instead of ID and FunctionType.
295
296 // Checks if the intrinsic name matches with its signature and if not
297 // returns the declaration with the same signature and remangled name.
298 // An existing GlobalValue with the wanted name but with a wrong prototype
299 // or of the wrong kind will be renamed by adding ".renamed" to the name.
300 LLVM_ABI std::optional<Function *> remangleIntrinsicFunction(Function *F);
301
302 /// Returns the corresponding llvm.vector.interleaveN intrinsic for factor N.
304
305 /// Returns the corresponding llvm.vector.deinterleaveN intrinsic for factor
306 /// N.
308
309 /// Print the argument info for the arguments with ArgInfo.
310 LLVM_ABI void printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS,
311 const Constant *ImmArgVal);
312
313 } // namespace Intrinsic
314
315 } // namespace llvm
316
317#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
#define F(x, y, z)
Definition MD5.cpp:54
Machine Check Debug Module
#define T
OptimizedStructLayoutField Field
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:407
This is an important base class in LLVM.
Definition Constant.h:43
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
Class to represent function types.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
LLVM_ABI Intrinsic::ID getDeinterleaveIntrinsicID(unsigned Factor)
Returns the corresponding llvm.vector.deinterleaveN intrinsic for factor N.
LLVM_ABI MatchIntrinsicTypesResult matchIntrinsicSignature(FunctionType *FTy, ArrayRef< IITDescriptor > &Infos, SmallVectorImpl< Type * > &ArgTys)
Match the specified function type with the type constraints specified by the .td file.
LLVM_ABI void getIntrinsicInfoTableEntries(ID id, SmallVectorImpl< IITDescriptor > &T)
Return the IIT table descriptor for the specified intrinsic into an array of IITDescriptors.
@ MatchIntrinsicTypes_NoMatchRet
Definition Intrinsics.h:262
@ MatchIntrinsicTypes_NoMatchArg
Definition Intrinsics.h:263
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
LLVM_ABI std::optional< Function * > remangleIntrinsicFunction(Function *F)
LLVM_ABI bool hasConstrainedFPRoundingModeOperand(ID QID)
Returns true if the intrinsic ID is for one of the "ConstrainedFloating-Point Intrinsics" that take r...
LLVM_ABI void printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Constant *ImmArgVal)
Print the argument info for the arguments with ArgInfo.
LLVM_ABI ID getIntrinsicForMSBuiltin(StringRef TargetPrefix, StringRef BuiltinName)
Map a MS builtin name to an intrinsic ID.
LLVM_ABI StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
LLVM_ABI bool isConstrainedFPIntrinsic(ID QID)
Returns true if the intrinsic ID is for one of the "ConstrainedFloating-Point Intrinsics".
LLVM_ABI ID lookupIntrinsicID(StringRef Name)
This does the actual lookup of an intrinsic ID which matches the given function name.
LLVM_ABI bool hasPrettyPrintedArgs(ID id)
Returns true if the intrinsic has pretty printed immediate arguments.
LLVM_ABI AttributeSet getFnAttributes(LLVMContext &C, ID id)
Return the function attributes for an intrinsic.
static const int NoAliasScopeDeclScopeArg
Definition Intrinsics.h:41
LLVM_ABI StringRef getBaseName(ID id)
Return the LLVM name for an intrinsic, without encoded types for overloading, such as "llvm....
LLVM_ABI Intrinsic::ID getInterleaveIntrinsicID(unsigned Factor)
Returns the corresponding llvm.vector.interleaveN intrinsic for factor N.
LLVM_ABI AttributeList getAttributes(LLVMContext &C, ID id, FunctionType *FT)
Return the attributes for an intrinsic.
LLVM_ABI bool isOverloaded(ID id)
Returns true if the intrinsic can be overloaded.
LLVM_ABI FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > OverloadTys={})
Return the function type for an intrinsic.
LLVM_ABI ID getIntrinsicForClangBuiltin(StringRef TargetPrefix, StringRef BuiltinName)
Map a Clang builtin name to an intrinsic ID.
LLVM_ABI bool getIntrinsicSignature(Intrinsic::ID, FunctionType *FT, SmallVectorImpl< Type * > &ArgTys)
Gets the type arguments of an intrinsic call by matching type contraints specified by the ....
LLVM_ABI bool isTargetIntrinsic(ID IID)
isTargetIntrinsic - Returns true if IID is an intrinsic specific to a certain target.
LLVM_ABI std::string getNameNoUnnamedTypes(ID Id, ArrayRef< Type * > OverloadTys)
Return the LLVM name for an intrinsic.
LLVM_ABI bool matchIntrinsicVarArg(bool isVarArg, ArrayRef< IITDescriptor > &Infos)
Verify if the intrinsic has variable arguments.
This is an optimization pass for GlobalISel generic memory operations.
ArrayRef(const T &OneElt) -> ArrayRef< T >
This is a type descriptor which explains the type requirements of an intrinsic.
Definition Intrinsics.h:155
enum llvm::Intrinsic::IITDescriptor::IITDescriptorKind Kind
static IITDescriptor get(IITDescriptorKind K, unsigned Field)
Definition Intrinsics.h:236
unsigned getRefOverloadIndex() const
Definition Intrinsics.h:231
unsigned getVectorDivisor() const
Definition Intrinsics.h:226
static IITDescriptor getVector(unsigned Width, bool IsScalable)
Definition Intrinsics.h:248
static IITDescriptor get(IITDescriptorKind K, unsigned short Hi, unsigned short Lo)
Definition Intrinsics.h:241
AnyKind getOverloadKind() const
Definition Intrinsics.h:218
unsigned getOverloadIndex() const
Definition Intrinsics.h:209