LLVM 23.0.0git
InstructionSimplify.h
Go to the documentation of this file.
1//===-- InstructionSimplify.h - Fold instrs into simpler forms --*- 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 simpler forms
10// that do not require creating new instructions. This does constant folding
11// ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
12// returning a constant ("and i32 %x, 0" -> "0") or an already existing value
13// ("and i32 %x, %x" -> "%x"). If the simplification is also an instruction
14// then it dominates the original instruction.
15//
16// These routines implicitly resolve undef uses. The easiest way to be safe when
17// using these routines to obtain simplified values for existing instructions is
18// to always replace all uses of the instructions with the resulting simplified
19// values. This will prevent other code from seeing the same undef uses and
20// resolving them to different values.
21//
22// They require that all the IR that they encounter be valid and inserted into a
23// parent function.
24//
25// Additionally, these routines can't simplify to the instructions that are not
26// def-reachable, meaning we can't just scan the basic block for instructions
27// to simplify to.
28//
29//===----------------------------------------------------------------------===//
30
31#ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
32#define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
33
35#include "llvm/IR/FPEnv.h"
37
38namespace llvm {
39
40template <typename T, typename... TArgs> class AnalysisManager;
41template <class T> class ArrayRef;
42class AssumptionCache;
43class CallBase;
44class DataLayout;
45class DominatorTree;
46class Function;
47class Instruction;
48class CmpPredicate;
49class LoadInst;
51class Pass;
52template <class T, unsigned n> class SmallSetVector;
54class Type;
55class Value;
56
57// NOTE: the explicit multiple argument versions of these functions are
58// deprecated.
59// Please use the SimplifyQuery versions in new code.
60
61/// Given operands for an Add, fold the result or return null.
62LLVM_ABI Value *simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW,
63 const SimplifyQuery &Q);
64
65/// Given operands for a Sub, fold the result or return null.
66LLVM_ABI Value *simplifySubInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW,
67 const SimplifyQuery &Q);
68
69/// Given operands for a Mul, fold the result or return null.
70LLVM_ABI Value *simplifyMulInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW,
71 const SimplifyQuery &Q);
72
73/// Given operands for an SDiv, fold the result or return null.
75 const SimplifyQuery &Q);
76
77/// Given operands for a UDiv, fold the result or return null.
79 const SimplifyQuery &Q);
80
81/// Given operands for an SRem, fold the result or return null.
83 const SimplifyQuery &Q);
84
85/// Given operands for a URem, fold the result or return null.
87 const SimplifyQuery &Q);
88
89/// Given operand for an FNeg, fold the result or return null.
91 const SimplifyQuery &Q);
92
93/// Given operands for an FAdd, fold the result or return null.
96 const SimplifyQuery &Q,
99
100/// Given operands for an FSub, fold the result or return null.
103 const SimplifyQuery &Q,
106
107/// Given operands for an FMul, fold the result or return null.
110 const SimplifyQuery &Q,
113
114/// Given operands for the multiplication of a FMA, fold the result or return
115/// null. In contrast to simplifyFMulInst, this function will not perform
116/// simplifications whose unrounded results differ when rounded to the argument
117/// type.
120 const SimplifyQuery &Q,
123
124/// Given operands for an FDiv, fold the result or return null.
127 const SimplifyQuery &Q,
130
131/// Given operands for an FRem, fold the result or return null.
134 const SimplifyQuery &Q,
137
138/// Given operands for a Shl, fold the result or return null.
139LLVM_ABI Value *simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
140 const SimplifyQuery &Q);
141
142/// Given operands for a LShr, fold the result or return null.
143LLVM_ABI Value *simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
144 const SimplifyQuery &Q);
145
146/// Given operands for a AShr, fold the result or return nulll.
147LLVM_ABI Value *simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
148 const SimplifyQuery &Q);
149
150/// Given operands for an And, fold the result or return null.
152
153/// Given operands for an Or, fold the result or return null.
155
156/// Given operands for an Xor, fold the result or return null.
158
159/// Given operands for an ICmpInst, fold the result or return null.
161 const SimplifyQuery &Q);
162
163/// Given operands for an FCmpInst, fold the result or return null.
165 FastMathFlags FMF, const SimplifyQuery &Q);
166
167/// Given operands for a SelectInst, fold the result or return null.
168LLVM_ABI Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
169 const SimplifyQuery &Q);
170
171/// Given operands for a GetElementPtrInst, fold the result or return null.
174 const SimplifyQuery &Q);
175
176/// Given operands for an InsertValueInst, fold the result or return null.
179 const SimplifyQuery &Q);
180
181/// Given operands for an InsertElement, fold the result or return null.
183 const SimplifyQuery &Q);
184
185/// Given operands for an ExtractValueInst, fold the result or return null.
187 const SimplifyQuery &Q);
188
189/// Given operands for an ExtractElementInst, fold the result or return null.
191 const SimplifyQuery &Q);
192
193/// Given operands for a CastInst, fold the result or return null.
194LLVM_ABI Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
195 const SimplifyQuery &Q);
196
197/// Given operands for a BinaryIntrinsic, fold the result or return null.
198/// The \p `Call` argument is optional and may be null.
200 Value *Op0, Value *Op1,
201 const SimplifyQuery &Q,
202 const CallBase *Call);
203
204/// Given operands for a ShuffleVectorInst, fold the result or return null.
205/// See class ShuffleVectorInst for a description of the mask representation.
207 ArrayRef<int> Mask, Type *RetTy,
208 const SimplifyQuery &Q);
209
210//=== Helper functions for higher up the class hierarchy.
211
212/// Given operands for a CmpInst, fold the result or return null.
214 const SimplifyQuery &Q);
215
216/// Given operand for a UnaryOperator, fold the result or return null.
217LLVM_ABI Value *simplifyUnOp(unsigned Opcode, Value *Op,
218 const SimplifyQuery &Q);
219
220/// Given operand for a UnaryOperator, fold the result or return null.
221/// Try to use FastMathFlags when folding the result.
222LLVM_ABI Value *simplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
223 const SimplifyQuery &Q);
224
225/// Given operands for a BinaryOperator, fold the result or return null.
226LLVM_ABI Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
227 const SimplifyQuery &Q);
228
229/// Given operands for a BinaryOperator, fold the result or return null.
230/// Try to use FastMathFlags when folding the result.
231LLVM_ABI Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
232 FastMathFlags FMF, const SimplifyQuery &Q);
233
234/// Given a callsite, callee, and arguments, fold the result or return null.
236 ArrayRef<Value *> Args, const SimplifyQuery &Q);
237
238/// Given a constrained FP intrinsic call, tries to compute its simplified
239/// version. Returns a simplified result or null.
240///
241/// This function provides an additional contract: it guarantees that if
242/// simplification succeeds that the intrinsic is side effect free. As a result,
243/// successful simplification can be used to delete the intrinsic not just
244/// replace its result.
246 const SimplifyQuery &Q);
247
248/// Given an operand for a Freeze, see if we can fold the result.
249/// If not, this returns null.
251
252/// Given a load instruction and its pointer operand, fold the result or return
253/// null.
255 const SimplifyQuery &Q);
256
257/// See if we can compute a simplified version of this instruction. If not,
258/// return null.
260
261/// Like \p simplifyInstruction but the operands of \p I are replaced with
262/// \p NewOps. Returns a simplified value, or null if none was found.
264 ArrayRef<Value *> NewOps,
265 const SimplifyQuery &Q);
266
267/// See if V simplifies when its operand Op is replaced with RepOp. If not,
268/// return null.
269/// AllowRefinement specifies whether the simplification can be a refinement
270/// (e.g. 0 instead of poison), or whether it needs to be strictly identical.
271/// Op and RepOp can be assumed to not be poison when determining refinement.
272///
273/// If DropFlags is passed, then the replacement result is only valid if
274/// poison-generating flags/metadata on those instructions are dropped. This
275/// is only useful in conjunction with AllowRefinement=false.
278 const SimplifyQuery &Q, bool AllowRefinement,
279 SmallVectorImpl<Instruction *> *DropFlags = nullptr);
280
281/// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
282///
283/// This first performs a normal RAUW of I with SimpleV. It then recursively
284/// attempts to simplify those users updated by the operation. The 'I'
285/// instruction must not be equal to the simplified value 'SimpleV'.
286/// If UnsimplifiedUsers is provided, instructions that could not be simplified
287/// are added to it.
288///
289/// The function returns true if any simplifications were performed.
291 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI = nullptr,
292 const DominatorTree *DT = nullptr, AssumptionCache *AC = nullptr,
293 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr);
294
295// These helper functions return a SimplifyQuery structure that contains as
296// many of the optional analysis we use as are currently valid. This is the
297// strongly preferred way of constructing SimplifyQuery in passes.
299template <class T, class... TArgs>
301 Function &);
303 const DataLayout &);
304} // end namespace llvm
305
306#endif
#define LLVM_ABI
Definition Compiler.h:213
This file contains the declarations of entities that describe floating point environment and related ...
#define I(x, y, z)
Definition MD5.cpp:57
#define T
const SmallVectorImpl< MachineOperand > & Cond
Value * RHS
Value * LHS
A container for analyses that lazily runs them and caches their results.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A cache of @llvm.assume calls within a function.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
Represents flags for the getelementptr instruction/expression.
An instruction for reading from memory.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:339
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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:46
LLVM Value Representation.
Definition Value.h:75
CallInst * Call
ExceptionBehavior
Exception behavior used for floating point operations.
Definition FPEnv.h:39
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI Value * simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a AShr, fold the result or return nulll.
LLVM_ABI Value * simplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FMul, fold the result or return null.
LLVM_ABI Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, GEPNoWrapFlags NW, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
LLVM_ABI Value * simplifyFreezeInst(Value *Op, const SimplifyQuery &Q)
Given an operand for a Freeze, see if we can fold the result.
LLVM_ABI Value * simplifySDivInst(Value *LHS, Value *RHS, bool IsExact, const SimplifyQuery &Q)
Given operands for an SDiv, fold the result or return null.
LLVM_ABI Value * simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q)
Given operand for a UnaryOperator, fold the result or return null.
LLVM_ABI Value * simplifyMulInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Mul, fold the result or return null.
LLVM_ABI Value * simplifyInstructionWithOperands(Instruction *I, ArrayRef< Value * > NewOps, const SimplifyQuery &Q)
Like simplifyInstruction but the operands of I are replaced with NewOps.
LLVM_ABI Value * simplifyCall(CallBase *Call, Value *Callee, ArrayRef< Value * > Args, const SimplifyQuery &Q)
Given a callsite, callee, and arguments, fold the result or return null.
LLVM_ABI Value * simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FCmpInst, fold the result or return null.
LLVM_ABI Value * simplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef< int > Mask, Type *RetTy, const SimplifyQuery &Q)
Given operands for a ShuffleVectorInst, fold the result or return null.
LLVM_ABI Value * simplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Or, fold the result or return null.
LLVM_ABI Value * simplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Xor, fold the result or return null.
LLVM_ABI Value * simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
LLVM_ABI Value * simplifySubInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Sub, fold the result or return null.
LLVM_ABI Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
LLVM_ABI bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, SmallSetVector< Instruction *, 8 > *UnsimplifiedUsers=nullptr)
Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
LLVM_ABI Value * simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Shl, fold the result or return null.
LLVM_ABI Value * simplifyFNegInst(Value *Op, FastMathFlags FMF, const SimplifyQuery &Q)
Given operand for an FNeg, fold the result or return null.
LLVM_ABI Value * simplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FSub, fold the result or return null.
LLVM_ABI Value * simplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FRem, fold the result or return null.
LLVM_ABI Value * simplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FAdd, fold the result or return null.
LLVM_ABI Value * simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a LShr, fold the result or return null.
LLVM_ABI Value * simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
LLVM_ABI Value * simplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an And, fold the result or return null.
LLVM_ABI Value * simplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an ExtractValueInst, fold the result or return null.
LLVM_ABI Value * simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an InsertValueInst, fold the result or return null.
LLVM_ABI Value * simplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FDiv, fold the result or return null.
LLVM_ABI Value * simplifyLoadInst(LoadInst *LI, Value *PtrOp, const SimplifyQuery &Q)
Given a load instruction and its pointer operand, fold the result or return null.
LLVM_ABI Value * simplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for the multiplication of a FMA, fold the result or return null.
LLVM_ABI Value * simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q)
Given a constrained FP intrinsic call, tries to compute its simplified version.
LLVM_ABI Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
LLVM_ABI Value * simplifyUDivInst(Value *LHS, Value *RHS, bool IsExact, const SimplifyQuery &Q)
Given operands for a UDiv, fold the result or return null.
DWARFExpression::Operation Op
LLVM_ABI Value * simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType, Value *Op0, Value *Op1, const SimplifyQuery &Q, const CallBase *Call)
Given operands for a BinaryIntrinsic, fold the result or return null.
RoundingMode
Rounding mode.
@ NearestTiesToEven
roundTiesToEven.
LLVM_ABI Value * simplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, const SimplifyQuery &Q)
Given operands for an InsertElement, fold the result or return null.
LLVM_ABI Value * simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, const SimplifyQuery &Q, bool AllowRefinement, SmallVectorImpl< Instruction * > *DropFlags=nullptr)
See if V simplifies when its operand Op is replaced with RepOp.
LLVM_ABI Value * simplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an SRem, fold the result or return null.
LLVM_ABI Value * simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
LLVM_ABI const SimplifyQuery getBestSimplifyQuery(Pass &, Function &)
LLVM_ABI Value * simplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a URem, fold the result or return null.
LLVM_ABI Value * simplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q)
Given operands for an ExtractElementInst, fold the result or return null.
LLVM_ABI Value * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...