LLVM 22.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.
199 Value *Op0, Value *Op1,
200 const SimplifyQuery &Q,
201 const CallBase *Call);
202
203/// Given operands for a ShuffleVectorInst, fold the result or return null.
204/// See class ShuffleVectorInst for a description of the mask representation.
206 ArrayRef<int> Mask, Type *RetTy,
207 const SimplifyQuery &Q);
208
209//=== Helper functions for higher up the class hierarchy.
210
211/// Given operands for a CmpInst, fold the result or return null.
213 const SimplifyQuery &Q);
214
215/// Given operand for a UnaryOperator, fold the result or return null.
216LLVM_ABI Value *simplifyUnOp(unsigned Opcode, Value *Op,
217 const SimplifyQuery &Q);
218
219/// Given operand for a UnaryOperator, fold the result or return null.
220/// Try to use FastMathFlags when folding the result.
221LLVM_ABI Value *simplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
222 const SimplifyQuery &Q);
223
224/// Given operands for a BinaryOperator, fold the result or return null.
225LLVM_ABI Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
226 const SimplifyQuery &Q);
227
228/// Given operands for a BinaryOperator, fold the result or return null.
229/// Try to use FastMathFlags when folding the result.
230LLVM_ABI Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
231 FastMathFlags FMF, const SimplifyQuery &Q);
232
233/// Given a callsite, callee, and arguments, fold the result or return null.
235 ArrayRef<Value *> Args, const SimplifyQuery &Q);
236
237/// Given a constrained FP intrinsic call, tries to compute its simplified
238/// version. Returns a simplified result or null.
239///
240/// This function provides an additional contract: it guarantees that if
241/// simplification succeeds that the intrinsic is side effect free. As a result,
242/// successful simplification can be used to delete the intrinsic not just
243/// replace its result.
245 const SimplifyQuery &Q);
246
247/// Given an operand for a Freeze, see if we can fold the result.
248/// If not, this returns null.
250
251/// Given a load instruction and its pointer operand, fold the result or return
252/// null.
254 const SimplifyQuery &Q);
255
256/// See if we can compute a simplified version of this instruction. If not,
257/// return null.
259
260/// Like \p simplifyInstruction but the operands of \p I are replaced with
261/// \p NewOps. Returns a simplified value, or null if none was found.
263 ArrayRef<Value *> NewOps,
264 const SimplifyQuery &Q);
265
266/// See if V simplifies when its operand Op is replaced with RepOp. If not,
267/// return null.
268/// AllowRefinement specifies whether the simplification can be a refinement
269/// (e.g. 0 instead of poison), or whether it needs to be strictly identical.
270/// Op and RepOp can be assumed to not be poison when determining refinement.
271///
272/// If DropFlags is passed, then the replacement result is only valid if
273/// poison-generating flags/metadata on those instructions are dropped. This
274/// is only useful in conjunction with AllowRefinement=false.
277 const SimplifyQuery &Q, bool AllowRefinement,
278 SmallVectorImpl<Instruction *> *DropFlags = nullptr);
279
280/// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
281///
282/// This first performs a normal RAUW of I with SimpleV. It then recursively
283/// attempts to simplify those users updated by the operation. The 'I'
284/// instruction must not be equal to the simplified value 'SimpleV'.
285/// If UnsimplifiedUsers is provided, instructions that could not be simplified
286/// are added to it.
287///
288/// The function returns true if any simplifications were performed.
290 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI = nullptr,
291 const DominatorTree *DT = nullptr, AssumptionCache *AC = nullptr,
292 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr);
293
294// These helper functions return a SimplifyQuery structure that contains as
295// many of the optional analysis we use as are currently valid. This is the
296// strongly preferred way of constructing SimplifyQuery in passes.
298template <class T, class... TArgs>
300 Function &);
302 const DataLayout &);
303} // end namespace llvm
304
305#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:58
#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:41
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:63
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:22
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:356
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:45
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...