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