LLVM 22.0.0git
ConstantFolding.h
Go to the documentation of this file.
1//===-- ConstantFolding.h - Fold instructions into constants ----*- 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 declares routines for folding instructions into constants when all
10// operands are constants, for example "sub i32 1, 0" -> "1".
11//
12// Also, to supplement the basic VMCore ConstantExpr simplifications,
13// this file declares some additional folding routines that can make use of
14// DataLayout information. These functions cannot go in VMCore due to library
15// dependency issues.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
20#define LLVM_ANALYSIS_CONSTANTFOLDING_H
21
23#include <stdint.h>
24
25namespace llvm {
26
27namespace Intrinsic {
28using ID = unsigned;
29}
30
31class APInt;
32template <typename T> class ArrayRef;
33class CallBase;
34class Constant;
36class DataLayout;
37class Function;
38class GlobalValue;
39class GlobalVariable;
40class Instruction;
42class Type;
43
44/// If this constant is a constant offset from a global, return the global and
45/// the constant. Because of constantexprs, this function is recursive.
46/// If the global is part of a dso_local_equivalent constant, return it through
47/// `Equiv` if it is provided.
48LLVM_ABI bool
50 const DataLayout &DL,
51 DSOLocalEquivalent **DSOEquiv = nullptr);
52
53/// ConstantFoldInstruction - Try to constant fold the specified instruction.
54/// If successful, the constant result is returned, if not, null is returned.
55/// Note that this fails if not all of the operands are constant. Otherwise,
56/// this function can only fail when attempting to fold instructions like loads
57/// and stores, which have no constant expression form.
60 const TargetLibraryInfo *TLI = nullptr);
61
62/// ConstantFoldConstant - Fold the constant using the specified DataLayout.
63/// This function always returns a non-null constant: Either the folding result,
64/// or the original constant if further folding is not possible.
66 const TargetLibraryInfo *TLI = nullptr);
67
68/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
69/// specified operands. If successful, the constant result is returned, if not,
70/// null is returned. Note that this function can fail when attempting to
71/// fold instructions like loads and stores, which have no constant expression
72/// form.
73///
74/// In some cases, constant folding may return one value chosen from a set of
75/// multiple legal return values. For example, the exact bit pattern of NaN
76/// results is not guaranteed. Using such a result is usually only valid if
77/// all uses of the original operation are replaced by the constant-folded
78/// result. The \p AllowNonDeterministic parameter controls whether this is
79/// allowed.
82 const TargetLibraryInfo *TLI = nullptr, bool AllowNonDeterministic = true);
83
84/// Attempt to constant fold a compare instruction (icmp/fcmp) with the
85/// specified operands. Returns null or a constant expression of the specified
86/// operands on failure.
87/// Denormal inputs may be flushed based on the denormal handling mode.
89 unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL,
90 const TargetLibraryInfo *TLI = nullptr, const Instruction *I = nullptr);
91
92/// Attempt to constant fold a unary operation with the specified operand.
93/// Returns null on failure.
95 const DataLayout &DL);
96
97/// Attempt to constant fold a binary operation with the specified operands.
98/// Returns null or a constant expression of the specified operands on failure.
100 Constant *RHS,
101 const DataLayout &DL);
102
103/// Attempt to constant fold a floating point binary operation with the
104/// specified operands, applying the denormal handling mod to the operands.
105/// Returns null or a constant expression of the specified operands on failure.
108 const DataLayout &DL, const Instruction *I,
109 bool AllowNonDeterministic = true);
110
111/// Attempt to flush float point constant according to denormal mode set in the
112/// instruction's parent function attributes. If so, return a zero with the
113/// correct sign, otherwise return the original constant. Inputs and outputs to
114/// floating point instructions can have their mode set separately, so the
115/// direction is also needed.
116///
117/// If the calling function's "denormal-fp-math" input mode is "dynamic" for the
118/// floating-point type, returns nullptr for denormal inputs.
120 bool IsOutput);
121
122/// Attempt to constant fold a select instruction with the specified
123/// operands. The constant result is returned if successful; if not, null is
124/// returned.
126 Constant *V2);
127
128/// Attempt to constant fold a cast with the specified operand. If it
129/// fails, it returns a constant expression of the specified operand.
131 Type *DestTy, const DataLayout &DL);
132
133/// Constant fold a zext, sext or trunc, depending on IsSigned and whether the
134/// DestTy is wider or narrower than C. Returns nullptr on failure.
136 bool IsSigned, const DataLayout &DL);
137
138/// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
139/// instruction with the specified operands and indices. The constant result is
140/// returned if successful; if not, null is returned.
142 Constant *Val,
143 ArrayRef<unsigned> Idxs);
144
145/// Attempt to constant fold an extractvalue instruction with the
146/// specified operands and indices. The constant result is returned if
147/// successful; if not, null is returned.
149 ArrayRef<unsigned> Idxs);
150
151/// Attempt to constant fold an insertelement instruction with the
152/// specified operands and indices. The constant result is returned if
153/// successful; if not, null is returned.
155 Constant *Elt,
156 Constant *Idx);
157
158/// Attempt to constant fold an extractelement instruction with the
159/// specified operands and indices. The constant result is returned if
160/// successful; if not, null is returned.
162 Constant *Idx);
163
164/// Attempt to constant fold a shufflevector instruction with the
165/// specified operands and mask. See class ShuffleVectorInst for a description
166/// of the mask representation. The constant result is returned if successful;
167/// if not, null is returned.
169 Constant *V2,
170 ArrayRef<int> Mask);
171
172/// Extract value of C at the given Offset reinterpreted as Ty. If bits past
173/// the end of C are accessed, they are assumed to be poison.
175 const APInt &Offset,
176 const DataLayout &DL);
177
178/// Extract value of C reinterpreted as Ty. Same as previous API with zero
179/// offset.
181 const DataLayout &DL);
182
183/// Return the value that a load from C with offset Offset would produce if it
184/// is constant and determinable. If this is not determinable, return null.
187 const DataLayout &DL);
188
189/// Return the value that a load from C would produce if it is constant and
190/// determinable. If this is not determinable, return null.
192 const DataLayout &DL);
193
194/// If C is a uniform value where all bits are the same (either all zero, all
195/// ones, all undef or all poison), return the corresponding uniform value in
196/// the new type. If the value is not uniform or the result cannot be
197/// represented, return null.
199 const DataLayout &DL);
200
201/// canConstantFoldCallTo - Return true if its even possible to fold a call to
202/// the specified function.
204
205/// ConstantFoldCall - Attempt to constant fold a call to the specified function
206/// with the specified arguments, returning null if unsuccessful.
209 const TargetLibraryInfo *TLI = nullptr,
210 bool AllowNonDeterministic = true);
211
213 Constant *RHS, Type *Ty,
215
216/// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
217/// returning null if unsuccessful. Can cast pointer to pointer or pointer to
218/// integer and vice versa if their sizes are equal.
220 const DataLayout &DL);
221
222/// Check whether the given call has no side-effects.
223/// Specifically checks for math routimes which sometimes set errno.
225 const TargetLibraryInfo *TLI);
226
228 uint64_t Offset);
229
231 bool NNeg = false;
232 bool NUW = false;
233 bool NSW = false;
234};
235
236/// Try to cast C to InvC losslessly, satisfying CastOp(InvC) equals C, or
237/// CastOp(InvC) is a refined value of undefined C. Will try best to
238/// preserve the flags.
240 unsigned CastOp, const DataLayout &DL,
241 PreservedCastFlags *Flags = nullptr);
242
245 PreservedCastFlags *Flags = nullptr);
246
248 const DataLayout &DL,
249 PreservedCastFlags *Flags = nullptr);
250} // namespace llvm
251
252#endif
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_ABI
Definition Compiler.h:213
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
mir Rename Register Operands
const SmallVectorImpl< MachineOperand > & Cond
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This is an important base class in LLVM.
Definition Constant.h:43
Wrapper for a function that represents a value that functionally represents the original function.
Definition Constants.h:952
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
This provides a helper for copying FMF from an instruction or setting specified flags.
Definition IRBuilder.h:93
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
CallInst * Call
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.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
LLVM_ABI Constant * ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS, Constant *RHS, Type *Ty, Instruction *FMFSource)
LLVM_ABI Constant * ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, const DataLayout &DL)
ConstantFoldLoadThroughBitcast - try to cast constant to destination type returning null if unsuccess...
LLVM_ABI Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
LLVM_ABI Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I, bool AllowNonDeterministic=true)
Attempt to constant fold a floating point binary operation with the specified operands,...
LLVM_ABI bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
LLVM_ABI Constant * ConstantFoldInstruction(const Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
LLVM_ABI bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL, DSOLocalEquivalent **DSOEquiv=nullptr)
If this constant is a constant offset from a global, return the global and the constant.
LLVM_ABI bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
LLVM_ABI Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
LLVM_ABI Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
LLVM_ABI Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
LLVM_ABI Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, const DataLayout &DL)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
LLVM_ABI Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
LLVM_ABI Constant * FlushFPConstant(Constant *Operand, const Instruction *I, bool IsOutput)
Attempt to flush float point constant according to denormal mode set in the instruction's parent func...
LLVM_ABI Constant * getLosslessUnsignedTrunc(Constant *C, Type *DestTy, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
LLVM_ABI Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices.
LLVM_ABI Constant * getLosslessSignedTrunc(Constant *C, Type *DestTy, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_ABI Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices.
DWARFExpression::Operation Op
LLVM_ABI Constant * getLosslessInvCast(Constant *C, Type *InvCastTo, unsigned CastOp, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
Try to cast C to InvC losslessly, satisfying CastOp(InvC) equals C, or CastOp(InvC) is a refined valu...
LLVM_ABI Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
LLVM_ABI Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
LLVM_ABI Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, ArrayRef< int > Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and mask.
LLVM_ABI Constant * ConstantFoldInstOperands(const Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...