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#include <tuple>
24
25namespace llvm {
26
27class Type;
28class FunctionType;
29class Function;
30class LLVMContext;
31class Module;
32class AttributeList;
33class AttributeSet;
34class raw_ostream;
35class Constant;
36
37/// This namespace contains an enum with a value for every intrinsic/builtin
38/// function known by LLVM. The enum values are returned by
39/// Function::getIntrinsicID().
40namespace Intrinsic {
41// Abstraction for the arguments of the noalias intrinsics
42static const int NoAliasScopeDeclScopeArg = 0;
43
44// Intrinsic ID type. This is an opaque typedef to facilitate splitting up
45// the enum into target-specific enums.
46typedef unsigned ID;
47
48enum IndependentIntrinsics : unsigned {
49 not_intrinsic = 0, // Must be zero
50
51// Get the intrinsic enums generated from Intrinsics.td
52#define GET_INTRINSIC_ENUM_VALUES
53#include "llvm/IR/IntrinsicEnums.inc"
54};
55
56/// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
57/// Note, this version is for intrinsics with no overloads. Use the other
58/// version of getName if overloads are required.
59LLVM_ABI StringRef getName(ID id);
60
61/// Return the LLVM name for an intrinsic, without encoded types for
62/// overloading, such as "llvm.ssa.copy".
63LLVM_ABI StringRef getBaseName(ID id);
64
65/// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx" or
66/// "llvm.ssa.copy.p0s_s.1". Note, this version of getName supports overloads.
67/// This is less efficient than the StringRef version of this function. If no
68/// overloads are required, it is safe to use this version, but better to use
69/// the StringRef version. If one of the types is based on an unnamed type, a
70/// function type will be computed. Providing FT will avoid this computation.
71LLVM_ABI std::string getName(ID Id, ArrayRef<Type *> OverloadTys, Module *M,
72 FunctionType *FT = nullptr);
73
74/// Return the LLVM name for an intrinsic. This is a special version only to
75/// be used by LLVMIntrinsicCopyOverloadedName. It only supports overloads
76/// based on named types.
77LLVM_ABI std::string getNameNoUnnamedTypes(ID Id, ArrayRef<Type *> OverloadTys);
78
79/// Return the function type for an intrinsic.
80LLVM_ABI FunctionType *getType(LLVMContext &Context, ID id,
81 ArrayRef<Type *> OverloadTys = {});
82
83/// Returns true if the intrinsic can be overloaded.
84LLVM_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/// Returns true if \p id has a struct return type.
262
263/// Fill the IIT table descriptor for the intrinsic \p id into an array
264/// of IITDescriptors. Returns a tuple of 3 values:
265/// - ArrayRef for the descriptor table (for convenience).
266/// - Number of arguments.
267/// - if it's a variable argument intrinsic.
268///
269/// Note that for VarArg intrinsics, the last IIT `VarArg` token will be
270/// consumed and not a part of the returned ArrayRef.
271LLVM_ABI std::tuple<ArrayRef<IITDescriptor>, unsigned, bool>
273
274/// Returns true if \p FT is a valid function type for intrinsic \p ID. If
275/// `ID` is an overloaded intrinsic, the overload types are pushed into the
276/// OverloadTys vector.
277///
278/// Returns false if the given ID and function type combination is not a
279/// valid intrinsic call. Also prints the error message to indicate the reason
280/// of the mismatch to \p OS.
282 SmallVectorImpl<Type *> &OverloadTys,
283 raw_ostream &OS = nulls());
284
285/// Same as previous, but accepts a Function instead of ID and FunctionType.
287 SmallVectorImpl<Type *> &OverloadTys,
288 raw_ostream &OS = nulls());
289
290// Checks if the intrinsic name matches with its signature and if not
291// returns the declaration with the same signature and remangled name.
292// An existing GlobalValue with the wanted name but with a wrong prototype
293// or of the wrong kind will be renamed by adding ".renamed" to the name.
294LLVM_ABI std::optional<Function *> remangleIntrinsicFunction(Function *F);
295
296/// Returns the corresponding llvm.vector.interleaveN intrinsic for factor N.
298
299/// Returns the corresponding llvm.vector.deinterleaveN intrinsic for factor
300/// N.
302
303/// Print the argument info for the arguments with ArgInfo.
304LLVM_ABI void printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS,
305 const Constant *ImmArgVal);
306
307} // namespace Intrinsic
308
309} // namespace llvm
310
311#endif // LLVM_IR_INTRINSICS_H
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
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:42
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 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 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: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