LLVM 24.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"
19#include "llvm/ADT/StringRef.h"
22#include <optional>
23#include <string>
24#include <tuple>
25
26namespace llvm {
27
28class APInt;
29class AttributeList;
30class AttributeSet;
31class Constant;
32class Function;
33class FunctionType;
34class LLVMContext;
35class Module;
36class raw_ostream;
37class Type;
38
39/// This namespace contains an enum with a value for every intrinsic/builtin
40/// function known by LLVM. The enum values are returned by
41/// Function::getIntrinsicID().
42namespace Intrinsic {
43// Abstraction for the arguments of the noalias intrinsics
44static const int NoAliasScopeDeclScopeArg = 0;
45
46// Intrinsic ID type. This is an opaque typedef to facilitate splitting up
47// the enum into target-specific enums.
48typedef unsigned ID;
49
50enum IndependentIntrinsics : unsigned {
51 not_intrinsic = 0, // Must be zero
52
53// Get the intrinsic enums generated from Intrinsics.td
54#define GET_INTRINSIC_ENUM_VALUES
55#include "llvm/IR/IntrinsicEnums.inc"
56};
57
58/// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
59/// Note, this version is for intrinsics with no overloads. Use the other
60/// version of getName if overloads are required.
61LLVM_ABI StringRef getName(ID id);
62
63/// Return the LLVM name for an intrinsic, without encoded types for
64/// overloading, such as "llvm.ssa.copy".
65LLVM_ABI StringRef getBaseName(ID id);
66
67/// \returns the target feature expression required by an intrinsic.
69
70/// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx" or
71/// "llvm.ssa.copy.p0s_s.1". Note, this version of getName supports overloads.
72/// This is less efficient than the StringRef version of this function. If no
73/// overloads are required, it is safe to use this version, but better to use
74/// the StringRef version. If one of the types is based on an unnamed type, a
75/// function type will be computed. Providing FT will avoid this computation.
76LLVM_ABI std::string getName(ID Id, ArrayRef<Type *> OverloadTys, Module *M,
77 FunctionType *FT = nullptr);
78
79/// Return the LLVM name for an intrinsic. This is a special version only to
80/// be used by LLVMIntrinsicCopyOverloadedName. It only supports overloads
81/// based on named types.
82LLVM_ABI std::string getNameNoUnnamedTypes(ID Id, ArrayRef<Type *> OverloadTys);
83
84/// Return the function type for an intrinsic.
85LLVM_ABI FunctionType *getType(LLVMContext &Context, ID id,
86 ArrayRef<Type *> OverloadTys = {});
87
88/// Returns true if the intrinsic can be overloaded.
89LLVM_ABI bool isOverloaded(ID id);
90
91/// Returns true if the intrinsic is trivially scalarizable.
92/// This means that the intrinsic's argument types are all scalars for the
93/// scalar form and all vectors for the vector form.
95
96/// Returns true if the intrinsic has pretty printed immediate arguments.
98
99/// Returns the first default argument index and an ArrayRef of all
100/// default values for the trailing parameters of intrinsic IID.
101/// Returns {0, empty} if the intrinsic has no default arguments.
102///
103/// The defaults are stored contiguously starting at FirstDefault and
104/// extending to the last parameter (mirrors C++ default-argument
105/// rules).
106LLVM_ABI std::pair<unsigned, ArrayRef<uint64_t>> getAllDefaultArgValues(ID IID);
107
108/// isTargetIntrinsic - Returns true if IID is an intrinsic specific to a
109/// certain target. If it is a generic intrinsic false is returned.
111
113
114/// Return the attributes for an intrinsic.
116
117/// Return the function attributes for an intrinsic.
119
120/// Look up the Function declaration of the intrinsic \p id in the Module
121/// \p M. If it does not exist, add a declaration and return it. Otherwise,
122/// return the existing declaration.
123///
124/// The \p OverloadTys parameter is for intrinsics with overloaded types
125// (e.g., those using iAny, fAny, vAny, or pAny). For a declaration of an
126// overloaded intrinsic, OverloadTys must provide exactly one type for each
127// overloaded type in the intrinsic.
129 ArrayRef<Type *> OverloadTys = {});
130
131/// Look up the Function declaration of the intrinsic \p IID in the Module
132/// \p M. If it does not exist, add a declaration and return it. Otherwise,
133/// return the existing declaration.
134///
135/// This overload automatically resolves overloaded intrinsics based on the
136/// provided return type and argument types. For non-overloaded intrinsics,
137/// the return type and argument types are ignored.
138///
139/// \param M - The module to get or insert the intrinsic declaration.
140/// \param IID - The intrinsic ID.
141/// \param RetTy - The return type of the intrinsic.
142/// \param ArgTys - The argument types of the intrinsic.
144 ArrayRef<Type *> ArgTys);
145
146/// Look up the Function declaration of the intrinsic \p id in the Module
147/// \p M and return it if it exists. Otherwise, return nullptr. This version
148/// supports non-overloaded intrinsics.
150
151/// This version supports overloaded intrinsics.
153 ArrayRef<Type *> OverloadTys,
154 FunctionType *FT = nullptr);
155
156/// Map a Clang builtin name to an intrinsic ID.
158 StringRef BuiltinName);
159
160/// Map a MS builtin name to an intrinsic ID.
162 StringRef BuiltinName);
163
164/// Returns true if the intrinsic ID is for one of the "Constrained
165/// Floating-Point Intrinsics".
167
168/// Returns true if the intrinsic ID is for one of the "Constrained
169/// Floating-Point Intrinsics" that take rounding mode metadata.
171
172/// This is a type descriptor which explains the type requirements of an
173/// intrinsic. This is returned by getIntrinsicInfoTableEntries.
176 // Concrete types. Additional qualifiers listed in comments.
187 Integer, // Width of the integer in IntegerWidth.
188 Vector, // Width of the vector in VectorWidth.
189 Pointer, // Address space of the pointer in PointerAddressSpace.
190 Struct, // Number of elements in StructNumElements.
196
197 // Overloaded type.
198 Overloaded, // AnyKind and overload index in OverloadInfo.
199
200 // Fully dependent types. Overload index in OverloadInfo.
210
211 // Partially dependent types. Overload index (self and of the overload
212 // type it depends on) in OverloadInfo.
214
215 } Kind;
216
217 union {
218 unsigned IntegerWidth;
221 unsigned OverloadInfo;
223 };
224
225 // AnyKindVectorConstraint and AnyKindElementConstraint defined in
226 // Intrinsics.td
227#define GET_INTRINSIC_ANYKIND_ENUMS
228#include "llvm/IR/IntrinsicEnums.inc"
229
230 unsigned getOverloadIndex() const {
231 assert(Kind == Overloaded || Kind == Match || Kind == Extend ||
232 Kind == Trunc || Kind == SameVecWidth || Kind == VecElement ||
233 Kind == Subdivide2 || Kind == Subdivide4 ||
236 // Overload index is packed into byte[0] of OverloadInfo.
237 return OverloadInfo & 0xf;
238 }
239
240 std::pair<AnyKindVectorConstraint, AnyKindElementConstraint>
242 // Overload constraints are packed into byte[1] of OverloadInfo.
244 uint8_t AKEnumsPacked = OverloadInfo >> 8;
245 AnyKindVectorConstraint VC = (AnyKindVectorConstraint)(AKEnumsPacked >> 4);
246 AnyKindElementConstraint EC =
247 (AnyKindElementConstraint)(AKEnumsPacked & 0xf);
248 return {VC, EC};
249 }
250
251 // OneNthEltsVecArguments uses both a divisor N and a reference argument for
252 // the full-width vector to match.
253 unsigned getVectorDivisor() const {
255 return OverloadInfo >> 16;
256 }
257
258 unsigned getRefOverloadIndex() const {
260 return OverloadInfo >> 16;
261 }
262
264 IITDescriptor Result = {K, {Field}};
265 return Result;
266 }
267
268 static IITDescriptor get(IITDescriptorKind K, unsigned short Hi,
269 unsigned short Lo) {
270 unsigned Field = Hi << 16 | Lo;
271 IITDescriptor Result = {K, {Field}};
272 return Result;
273 }
274
275 static IITDescriptor getVector(unsigned Width, bool IsScalable) {
276 IITDescriptor Result = {Vector, {0}};
277 Result.VectorWidth = ElementCount::get(Width, IsScalable);
278 return Result;
279 }
280};
281
282/// Returns true if \p id has a struct return type.
283LLVM_ABI bool hasStructReturnType(ID id);
284
285/// Fill the IIT table descriptor for the intrinsic \p id into an array
286/// of IITDescriptors. Returns a tuple of 3 values:
287/// - ArrayRef for the descriptor table (for convenience).
288/// - Number of arguments.
289/// - if it's a variable argument intrinsic.
290///
291/// Note that for VarArg intrinsics, the last IIT `VarArg` token will be
292/// consumed and not a part of the returned ArrayRef.
293LLVM_ABI std::tuple<ArrayRef<IITDescriptor>, unsigned, bool>
295
296/// Returns true if \p FT is a valid function type for intrinsic \p ID. If
297/// `ID` is an overloaded intrinsic, the overload types are pushed into the
298/// OverloadTys vector.
299///
300/// Returns false if the given ID and function type combination is not a
301/// valid intrinsic call. Also prints the error message to indicate the reason
302/// of the mismatch to \p OS.
304 SmallVectorImpl<Type *> &OverloadTys,
305 raw_ostream &OS = nulls());
306
307/// Same as previous, but accepts a Function instead of ID and FunctionType.
309 SmallVectorImpl<Type *> &OverloadTys,
310 raw_ostream &OS = nulls());
311
312// Checks if the intrinsic name matches with its signature and if not
313// returns the declaration with the same signature and remangled name.
314// An existing GlobalValue with the wanted name but with a wrong prototype
315// or of the wrong kind will be renamed by adding ".renamed" to the name.
316LLVM_ABI std::optional<Function *> remangleIntrinsicFunction(Function *F);
317
318/// Returns the corresponding llvm.vector.interleaveN intrinsic for factor N.
320
321/// Returns the corresponding llvm.vector.deinterleaveN intrinsic for factor
322/// N.
324
325/// Print the argument info for the arguments with ArgInfo.
326LLVM_ABI void printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS,
327 const Constant *ImmArgVal);
328
329LLVM_ABI void printFPClassMask(raw_ostream &OS, const Constant *ImmArgVal);
330
331/// Returns true if \p Value satisfies the range constraints specified for
332/// argument \p ArgIdx of intrinsic \p IID.
333LLVM_ABI bool isImmArgValueInRangeSet(ID IID, unsigned ArgIdx,
334 const APInt &Value);
335
336} // namespace Intrinsic
337
338} // namespace llvm
339
340#endif // LLVM_IR_INTRINSICS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:215
#define F(x, y, z)
Definition MD5.cpp:54
#define T
OptimizedStructLayoutField Field
Class for arbitrary precision integers.
Definition APInt.h:78
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...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
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 printFPClassMask(raw_ostream &OS, const Constant *ImmArgVal)
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 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.
LLVM_ABI std::tuple< ArrayRef< IITDescriptor >, unsigned, bool > getIntrinsicInfoTableEntries(ID id, SmallVectorImpl< IITDescriptor > &T)
Fill the IIT table descriptor for the intrinsic id into an array of IITDescriptors.
static const int NoAliasScopeDeclScopeArg
Definition Intrinsics.h:44
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 bool isSignatureValid(Intrinsic::ID ID, FunctionType *FT, SmallVectorImpl< Type * > &OverloadTys, raw_ostream &OS=nulls())
Returns true if FT is a valid function type for intrinsic ID.
LLVM_ABI bool hasStructReturnType(ID id)
Returns true if id has a struct return type.
LLVM_ABI ID getIntrinsicForClangBuiltin(StringRef TargetPrefix, StringRef BuiltinName)
Map a Clang builtin name to an intrinsic ID.
LLVM_ABI std::pair< unsigned, ArrayRef< uint64_t > > getAllDefaultArgValues(ID IID)
Returns the first default argument index and an ArrayRef of all default values for the trailing param...
LLVM_ABI bool isTriviallyScalarizable(ID id)
Returns true if the intrinsic is trivially scalarizable.
LLVM_ABI bool isImmArgValueInRangeSet(ID IID, unsigned ArgIdx, const APInt &Value)
Returns true if Value satisfies the range constraints specified for argument ArgIdx of intrinsic IID.
LLVM_ABI bool isTargetIntrinsic(ID IID)
isTargetIntrinsic - Returns true if IID is an intrinsic specific to a certain target.
LLVM_ABI StringRef getRequiredTargetFeatures(ID id)
LLVM_ABI std::string getNameNoUnnamedTypes(ID Id, ArrayRef< Type * > OverloadTys)
Return the LLVM name for an intrinsic.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
ArrayRef(const T &OneElt) -> ArrayRef< T >
This is a type descriptor which explains the type requirements of an intrinsic.
Definition Intrinsics.h:174
enum llvm::Intrinsic::IITDescriptor::IITDescriptorKind Kind
static IITDescriptor get(IITDescriptorKind K, unsigned Field)
Definition Intrinsics.h:263
unsigned getRefOverloadIndex() const
Definition Intrinsics.h:258
unsigned getVectorDivisor() const
Definition Intrinsics.h:253
static IITDescriptor getVector(unsigned Width, bool IsScalable)
Definition Intrinsics.h:275
std::pair< AnyKindVectorConstraint, AnyKindElementConstraint > getOverloadConstraints() const
Definition Intrinsics.h:241
static IITDescriptor get(IITDescriptorKind K, unsigned short Hi, unsigned short Lo)
Definition Intrinsics.h:268
unsigned getOverloadIndex() const
Definition Intrinsics.h:230