LLVM 24.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 FastMathFlags FMF, 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 an AddrSpaceCastInst, fold the result or return null.
198LLVM_ABI Value *simplifyAddrSpaceCastInst(Value *Op, Type *Ty, bool IsNonNull,
199 const SimplifyQuery &Q);
200
201/// Given operands for an intrinsic, fold the result or return null. Context
202/// Function is passed as \p CxtF. \p ExBehavior and \p Rounding only apply to
203/// constrained FP intrinsics.
206 FastMathFlags FMF, const SimplifyQuery &Q,
207 Function *CxtF = nullptr,
210
211/// Given operands for a ShuffleVectorInst, fold the result or return null.
212/// See class ShuffleVectorInst for a description of the mask representation.
214 ArrayRef<int> Mask, Type *RetTy,
215 const SimplifyQuery &Q);
216
217//=== Helper functions for higher up the class hierarchy.
218
219/// Given operands for a CmpInst, fold the result or return null.
221 const SimplifyQuery &Q);
222
223/// Given operand for a UnaryOperator, fold the result or return null.
224LLVM_ABI Value *simplifyUnOp(unsigned Opcode, Value *Op,
225 const SimplifyQuery &Q);
226
227/// Given operand for a UnaryOperator, fold the result or return null.
228/// Try to use FastMathFlags when folding the result.
229LLVM_ABI Value *simplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
230 const SimplifyQuery &Q);
231
232/// Given operands for a BinaryOperator, fold the result or return null.
233LLVM_ABI Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
234 const SimplifyQuery &Q);
235
236/// Given operands for a BinaryOperator, fold the result or return null.
237/// Try to use FastMathFlags when folding the result.
238LLVM_ABI Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
239 FastMathFlags FMF, const SimplifyQuery &Q);
240
241/// Given a callsite, callee, and arguments, fold the result or return null.
243 ArrayRef<Value *> Args, const SimplifyQuery &Q);
244
245/// Given a constrained FP intrinsic call, tries to compute its simplified
246/// version. Returns a simplified result or null.
247///
248/// This function provides an additional contract: it guarantees that if
249/// simplification succeeds that the intrinsic is side effect free. As a result,
250/// successful simplification can be used to delete the intrinsic not just
251/// replace its result.
253 const SimplifyQuery &Q);
254
255/// Given an operand for a Freeze, see if we can fold the result.
256/// If not, this returns null.
258
259/// Given a load instruction and its pointer operand, fold the result or return
260/// null.
262 const SimplifyQuery &Q);
263
264/// See if we can compute a simplified version of this instruction. If not,
265/// return null.
267
268/// Like \p simplifyInstruction but the operands of \p I are replaced with
269/// \p NewOps. Returns a simplified value, or null if none was found.
271 ArrayRef<Value *> NewOps,
272 const SimplifyQuery &Q);
273
274/// See if V simplifies when its operand Op is replaced with RepOp. If not,
275/// return null.
276/// AllowRefinement specifies whether the simplification can be a refinement
277/// (e.g. 0 instead of poison), or whether it needs to be strictly identical.
278/// Op and RepOp can be assumed to not be poison when determining refinement.
279///
280/// If DropFlags is passed, then the replacement result is only valid if
281/// poison-generating flags/metadata on those instructions are dropped. This
282/// is only useful in conjunction with AllowRefinement=false.
285 const SimplifyQuery &Q, bool AllowRefinement,
286 SmallVectorImpl<Instruction *> *DropFlags = nullptr);
287
288/// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
289///
290/// This first performs a normal RAUW of I with SimpleV. It then recursively
291/// attempts to simplify those users updated by the operation. The 'I'
292/// instruction must not be equal to the simplified value 'SimpleV'.
293/// If UnsimplifiedUsers is provided, instructions that could not be simplified
294/// are added to it.
295///
296/// The function returns true if any simplifications were performed.
298 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI = nullptr,
299 const DominatorTree *DT = nullptr, AssumptionCache *AC = nullptr,
300 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr);
301
302// These helper functions return a SimplifyQuery structure that contains as
303// many of the optional analysis we use as are currently valid. This is the
304// strongly preferred way of constructing SimplifyQuery in passes.
306template <class T, class... TArgs>
308 Function &);
310 const DataLayout &);
311} // end namespace llvm
312
313#endif
#define LLVM_ABI
Definition Compiler.h:215
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.
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:122
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:345
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 * simplifyAddrSpaceCastInst(Value *Op, Type *Ty, bool IsNonNull, const SimplifyQuery &Q)
Given operands for an AddrSpaceCastInst, 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 * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for a SelectInst, 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 * simplifyIntrinsic(Intrinsic::ID IID, Type *ReturnType, ArrayRef< Value * > Args, FastMathFlags FMF, const SimplifyQuery &Q, Function *CxtF=nullptr, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an intrinsic, 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
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.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...