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 is trivially scalarizable.
87 /// This means that the intrinsic's argument types are all scalars for the
88 /// scalar form and all vectors for the vector form.
90
91 /// Returns true if the intrinsic has pretty printed immediate arguments.
93
94 /// isTargetIntrinsic - Returns true if IID is an intrinsic specific to a
95 /// certain target. If it is a generic intrinsic false is returned.
97
99
100 /// Return the attributes for an intrinsic.
102
103 /// Return the function attributes for an intrinsic.
105
106 /// Look up the Function declaration of the intrinsic \p id in the Module
107 /// \p M. If it does not exist, add a declaration and return it. Otherwise,
108 /// return the existing declaration.
109 ///
110 /// The \p OverloadTys parameter is for intrinsics with overloaded types
111 // (e.g., those using iAny, fAny, vAny, or pAny). For a declaration of an
112 // overloaded intrinsic, OverloadTys must provide exactly one type for each
113 // overloaded type in the intrinsic.
115 ArrayRef<Type *> OverloadTys = {});
116
117 /// Look up the Function declaration of the intrinsic \p IID in the Module
118 /// \p M. If it does not exist, add a declaration and return it. Otherwise,
119 /// return the existing declaration.
120 ///
121 /// This overload automatically resolves overloaded intrinsics based on the
122 /// provided return type and argument types. For non-overloaded intrinsics,
123 /// the return type and argument types are ignored.
124 ///
125 /// \param M - The module to get or insert the intrinsic declaration.
126 /// \param IID - The intrinsic ID.
127 /// \param RetTy - The return type of the intrinsic.
128 /// \param ArgTys - The argument types of the intrinsic.
130 ArrayRef<Type *> ArgTys);
131
132 /// Look up the Function declaration of the intrinsic \p id in the Module
133 /// \p M and return it if it exists. Otherwise, return nullptr. This version
134 /// supports non-overloaded intrinsics.
136
137 /// This version supports overloaded intrinsics.
139 ArrayRef<Type *> OverloadTys,
140 FunctionType *FT = nullptr);
141
142 /// Map a Clang builtin name to an intrinsic ID.
144 StringRef BuiltinName);
145
146 /// Map a MS builtin name to an intrinsic ID.
148 StringRef BuiltinName);
149
150 /// Returns true if the intrinsic ID is for one of the "Constrained
151 /// Floating-Point Intrinsics".
153
154 /// Returns true if the intrinsic ID is for one of the "Constrained
155 /// Floating-Point Intrinsics" that take rounding mode metadata.
157
158 /// This is a type descriptor which explains the type requirements of an
159 /// intrinsic. This is returned by getIntrinsicInfoTableEntries.
162 // Concrete types. Additional qualifiers listed in comments.
173 Integer, // Width of the integer in IntegerWidth.
174 Vector, // Width of the vector in VectorWidth.
175 Pointer, // Address space of the pointer in PointerAddressSpace.
176 Struct, // Number of elements in StructNumElements.
180
181 // Overloaded type.
182 Overloaded, // AnyKind and overload index in OverloadInfo.
183
184 // Fully dependent types. Overload index in OverloadInfo.
193
194 // Partially dependent types. Overload index (self and of the overload
195 // type it depends on) in OverloadInfo.
197
198 } Kind;
199
200 union {
201 unsigned IntegerWidth;
204 unsigned OverloadInfo;
206 };
207
208 // AK_% : Defined in Intrinsics.td
209 enum AnyKind {
210#define GET_INTRINSIC_ANYKIND
211#include "llvm/IR/IntrinsicEnums.inc"
212 };
213
214 unsigned getOverloadIndex() const {
215 assert(Kind == Overloaded || Kind == Extend || Kind == Trunc ||
219 // Overload index is packed into lower 5 bits.
220 return OverloadInfo & 0x1f;
221 }
222
224 // Overload kind is packed into upper 3 bits.
226 return (AnyKind)((OverloadInfo >> 5) & 0x7);
227 }
228
229 // OneNthEltsVecArguments uses both a divisor N and a reference argument for
230 // the full-width vector to match
231 unsigned getVectorDivisor() const {
233 return OverloadInfo >> 16;
234 }
235
236 unsigned getRefOverloadIndex() const {
238 return OverloadInfo >> 16;
239 }
240
242 IITDescriptor Result = { K, { Field } };
243 return Result;
244 }
245
246 static IITDescriptor get(IITDescriptorKind K, unsigned short Hi,
247 unsigned short Lo) {
248 unsigned Field = Hi << 16 | Lo;
249 IITDescriptor Result = {K, {Field}};
250 return Result;
251 }
252
253 static IITDescriptor getVector(unsigned Width, bool IsScalable) {
254 IITDescriptor Result = {Vector, {0}};
255 Result.VectorWidth = ElementCount::get(Width, IsScalable);
256 return Result;
257 }
258 };
259
260 /// Return the IIT table descriptor for the specified intrinsic into an array
261 /// of IITDescriptors.
264
270
271 /// Match the specified function type with the type constraints specified by
272 /// the .td file. If the given type is an overloaded type it is pushed to the
273 /// OverloadTys vector.
274 ///
275 /// Returns false if the given type matches with the constraints, true
276 /// otherwise.
279 SmallVectorImpl<Type *> &OverloadTys);
280
281 /// Verify if the intrinsic has variable arguments. This method is intended to
282 /// be called after all the fixed arguments have been matched first.
283 ///
284 /// This method returns true on error.
285 LLVM_ABI bool matchIntrinsicVarArg(bool isVarArg,
287
288 /// Gets the type arguments of an intrinsic call by matching type contraints
289 /// specified by the .td file. The overloaded types are pushed into the
290 /// OverloadTys vector.
291 ///
292 /// Returns false if the given ID and function type combination is not a
293 /// valid intrinsic call.
295 SmallVectorImpl<Type *> &OverloadTys);
296
297 /// Same as previous, but accepts a Function instead of ID and FunctionType.
299 SmallVectorImpl<Type *> &OverloadTys);
300
301 // Checks if the intrinsic name matches with its signature and if not
302 // returns the declaration with the same signature and remangled name.
303 // An existing GlobalValue with the wanted name but with a wrong prototype
304 // or of the wrong kind will be renamed by adding ".renamed" to the name.
305 LLVM_ABI std::optional<Function *> remangleIntrinsicFunction(Function *F);
306
307 /// Returns the corresponding llvm.vector.interleaveN intrinsic for factor N.
309
310 /// Returns the corresponding llvm.vector.deinterleaveN intrinsic for factor
311 /// N.
313
314 /// Print the argument info for the arguments with ArgInfo.
315 LLVM_ABI void printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS,
316 const Constant *ImmArgVal);
317
318 } // namespace Intrinsic
319
320 } // namespace llvm
321
322#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 Intrinsic::ID getDeinterleaveIntrinsicID(unsigned Factor)
Returns the corresponding llvm.vector.deinterleaveN intrinsic for factor N.
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:267
@ MatchIntrinsicTypes_NoMatchArg
Definition Intrinsics.h:268
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 MatchIntrinsicTypesResult matchIntrinsicSignature(FunctionType *FTy, ArrayRef< IITDescriptor > &Infos, SmallVectorImpl< Type * > &OverloadTys)
Match the specified function type with the type constraints specified by the .td file.
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 Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
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 isTriviallyScalarizable(ID id)
Returns true if the intrinsic is trivially scalarizable.
LLVM_ABI bool getIntrinsicSignature(Intrinsic::ID, FunctionType *FT, SmallVectorImpl< Type * > &OverloadTys)
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:160
enum llvm::Intrinsic::IITDescriptor::IITDescriptorKind Kind
static IITDescriptor get(IITDescriptorKind K, unsigned Field)
Definition Intrinsics.h:241
unsigned getRefOverloadIndex() const
Definition Intrinsics.h:236
unsigned getVectorDivisor() const
Definition Intrinsics.h:231
static IITDescriptor getVector(unsigned Width, bool IsScalable)
Definition Intrinsics.h:253
static IITDescriptor get(IITDescriptorKind K, unsigned short Hi, unsigned short Lo)
Definition Intrinsics.h:246
AnyKind getOverloadKind() const
Definition Intrinsics.h:223
unsigned getOverloadIndex() const
Definition Intrinsics.h:214