LLVM 18.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
36namespace llvm {
37
38template <typename T, typename... TArgs> class AnalysisManager;
39template <class T> class ArrayRef;
40class AssumptionCache;
41class BinaryOperator;
42class CallBase;
43class DataLayout;
44class DominatorTree;
45class Function;
46class Instruction;
47struct LoopStandardAnalysisResults;
48class MDNode;
49class Pass;
50template <class T, unsigned n> class SmallSetVector;
51class TargetLibraryInfo;
52class Type;
53class Value;
54
55// NOTE: the explicit multiple argument versions of these functions are
56// deprecated.
57// Please use the SimplifyQuery versions in new code.
58
59/// Given operands for an Add, fold the result or return null.
60Value *simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW,
61 const SimplifyQuery &Q);
62
63/// Given operands for a Sub, fold the result or return null.
64Value *simplifySubInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW,
65 const SimplifyQuery &Q);
66
67/// Given operands for a Mul, fold the result or return null.
68Value *simplifyMulInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW,
69 const SimplifyQuery &Q);
70
71/// Given operands for an SDiv, fold the result or return null.
72Value *simplifySDivInst(Value *LHS, Value *RHS, bool IsExact,
73 const SimplifyQuery &Q);
74
75/// Given operands for a UDiv, fold the result or return null.
76Value *simplifyUDivInst(Value *LHS, Value *RHS, bool IsExact,
77 const SimplifyQuery &Q);
78
79/// Given operands for an SRem, fold the result or return null.
80Value *simplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
81
82/// Given operands for a URem, fold the result or return null.
83Value *simplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
84
85/// Given operand for an FNeg, fold the result or return null.
86Value *simplifyFNegInst(Value *Op, FastMathFlags FMF, const SimplifyQuery &Q);
87
88
89/// Given operands for an FAdd, fold the result or return null.
90Value *
91simplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
92 const SimplifyQuery &Q,
95
96/// Given operands for an FSub, fold the result or return null.
97Value *
98simplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
99 const SimplifyQuery &Q,
102
103/// Given operands for an FMul, fold the result or return null.
104Value *
105simplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
106 const SimplifyQuery &Q,
109
110/// Given operands for the multiplication of a FMA, fold the result or return
111/// null. In contrast to simplifyFMulInst, this function will not perform
112/// simplifications whose unrounded results differ when rounded to the argument
113/// type.
114Value *simplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF,
115 const SimplifyQuery &Q,
118
119/// Given operands for an FDiv, fold the result or return null.
120Value *
121simplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
122 const SimplifyQuery &Q,
125
126/// Given operands for an FRem, fold the result or return null.
127Value *
128simplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
129 const SimplifyQuery &Q,
132
133/// Given operands for a Shl, fold the result or return null.
134Value *simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
135 const SimplifyQuery &Q);
136
137/// Given operands for a LShr, fold the result or return null.
138Value *simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
139 const SimplifyQuery &Q);
140
141/// Given operands for a AShr, fold the result or return nulll.
142Value *simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
143 const SimplifyQuery &Q);
144
145/// Given operands for an And, fold the result or return null.
146Value *simplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
147
148/// Given operands for an Or, fold the result or return null.
149Value *simplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
150
151/// Given operands for an Xor, fold the result or return null.
152Value *simplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
153
154/// Given operands for an ICmpInst, fold the result or return null.
155Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
156 const SimplifyQuery &Q);
157
158/// Given operands for an FCmpInst, fold the result or return null.
159Value *simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
160 FastMathFlags FMF, const SimplifyQuery &Q);
161
162/// Given operands for a SelectInst, fold the result or return null.
163Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
164 const SimplifyQuery &Q);
165
166/// Given operands for a GetElementPtrInst, fold the result or return null.
167Value *simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef<Value *> Indices,
168 bool InBounds, const SimplifyQuery &Q);
169
170/// Given operands for an InsertValueInst, fold the result or return null.
171Value *simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
172 const SimplifyQuery &Q);
173
174/// Given operands for an InsertElement, fold the result or return null.
175Value *simplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx,
176 const SimplifyQuery &Q);
177
178/// Given operands for an ExtractValueInst, fold the result or return null.
179Value *simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
180 const SimplifyQuery &Q);
181
182/// Given operands for an ExtractElementInst, fold the result or return null.
183Value *simplifyExtractElementInst(Value *Vec, Value *Idx,
184 const SimplifyQuery &Q);
185
186/// Given operands for a CastInst, fold the result or return null.
187Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
188 const SimplifyQuery &Q);
189
190/// Given operands for a ShuffleVectorInst, fold the result or return null.
191/// See class ShuffleVectorInst for a description of the mask representation.
192Value *simplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef<int> Mask,
193 Type *RetTy, const SimplifyQuery &Q);
194
195//=== Helper functions for higher up the class hierarchy.
196
197/// Given operands for a CmpInst, fold the result or return null.
198Value *simplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
199 const SimplifyQuery &Q);
200
201/// Given operand for a UnaryOperator, fold the result or return null.
202Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q);
203
204/// Given operand for a UnaryOperator, fold the result or return null.
205/// Try to use FastMathFlags when folding the result.
206Value *simplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
207 const SimplifyQuery &Q);
208
209/// Given operands for a BinaryOperator, fold the result or return null.
210Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
211 const SimplifyQuery &Q);
212
213/// Given operands for a BinaryOperator, fold the result or return null.
214/// Try to use FastMathFlags when folding the result.
215Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, FastMathFlags FMF,
216 const SimplifyQuery &Q);
217
218/// Given a callsite, callee, and arguments, fold the result or return null.
219Value *simplifyCall(CallBase *Call, Value *Callee, ArrayRef<Value *> Args,
220 const SimplifyQuery &Q);
221
222/// Given a constrained FP intrinsic call, tries to compute its simplified
223/// version. Returns a simplified result or null.
224///
225/// This function provides an additional contract: it guarantees that if
226/// simplification succeeds that the intrinsic is side effect free. As a result,
227/// successful simplification can be used to delete the intrinsic not just
228/// replace its result.
229Value *simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q);
230
231/// Given an operand for a Freeze, see if we can fold the result.
232/// If not, this returns null.
233Value *simplifyFreezeInst(Value *Op, const SimplifyQuery &Q);
234
235/// Given a load instruction and its pointer operand, fold the result or return
236/// null.
237Value *simplifyLoadInst(LoadInst *LI, Value *PtrOp, const SimplifyQuery &Q);
238
239/// See if we can compute a simplified version of this instruction. If not,
240/// return null.
241Value *simplifyInstruction(Instruction *I, const SimplifyQuery &Q);
242
243/// Like \p simplifyInstruction but the operands of \p I are replaced with
244/// \p NewOps. Returns a simplified value, or null if none was found.
245Value *
246simplifyInstructionWithOperands(Instruction *I, ArrayRef<Value *> NewOps,
247 const SimplifyQuery &Q);
248
249/// See if V simplifies when its operand Op is replaced with RepOp. If not,
250/// return null.
251/// AllowRefinement specifies whether the simplification can be a refinement
252/// (e.g. 0 instead of poison), or whether it needs to be strictly identical.
253/// Op and RepOp can be assumed to not be poison when determining refinement.
254///
255/// If DropFlags is passed, then the replacement result is only valid if
256/// poison-generating flags/metadata on those instructions are dropped. This
257/// is only useful in conjunction with AllowRefinement=false.
258Value *
259simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
260 const SimplifyQuery &Q, bool AllowRefinement,
261 SmallVectorImpl<Instruction *> *DropFlags = nullptr);
262
263/// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
264///
265/// This first performs a normal RAUW of I with SimpleV. It then recursively
266/// attempts to simplify those users updated by the operation. The 'I'
267/// instruction must not be equal to the simplified value 'SimpleV'.
268/// If UnsimplifiedUsers is provided, instructions that could not be simplified
269/// are added to it.
270///
271/// The function returns true if any simplifications were performed.
273 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI = nullptr,
274 const DominatorTree *DT = nullptr, AssumptionCache *AC = nullptr,
275 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr);
276
277// These helper functions return a SimplifyQuery structure that contains as
278// many of the optional analysis we use as are currently valid. This is the
279// strongly preferred way of constructing SimplifyQuery in passes.
280const SimplifyQuery getBestSimplifyQuery(Pass &, Function &);
281template <class T, class... TArgs>
282const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &,
283 Function &);
284const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &,
285 const DataLayout &);
286} // end namespace llvm
287
288#endif
aarch64 AArch64 CCMP Pass
RelocType Type
Definition: COFFYAML.cpp:391
return RetTy
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define I(x, y, z)
Definition: MD5.cpp:58
#define T
const SmallVectorImpl< MachineOperand > & Cond
Value * RHS
Value * LHS
static constexpr uint32_t Opcode
Definition: aarch32.h:200
ExceptionBehavior
Exception behavior used for floating point operations.
Definition: FPEnv.h:38
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition: FPEnv.h:39
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Value * simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a AShr, fold the result or return nulll.
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.
Value * simplifyFreezeInst(Value *Op, const SimplifyQuery &Q)
Given an operand for a Freeze, see if we can fold the result.
Value * simplifySDivInst(Value *LHS, Value *RHS, bool IsExact, const SimplifyQuery &Q)
Given operands for an SDiv, fold the result or return null.
Value * simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q)
Given operand for a UnaryOperator, fold the result or return null.
Value * simplifyMulInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Mul, fold the result or return null.
Value * simplifyInstructionWithOperands(Instruction *I, ArrayRef< Value * > NewOps, const SimplifyQuery &Q)
Like simplifyInstruction but the operands of I are replaced with NewOps.
Value * simplifyCall(CallBase *Call, Value *Callee, ArrayRef< Value * > Args, const SimplifyQuery &Q)
Given a callsite, callee, and arguments, fold the result or return null.
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.
Value * simplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Or, fold the result or return null.
Value * simplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Xor, fold the result or return null.
Value * simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
Value * simplifySubInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Sub, fold the result or return null.
Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
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.
Value * simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Shl, fold the result or return null.
Value * simplifyFNegInst(Value *Op, FastMathFlags FMF, const SimplifyQuery &Q)
Given operand for an FNeg, fold the result or return null.
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.
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.
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.
Value * simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a LShr, fold the result or return null.
Value * simplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an And, fold the result or return null.
Value * simplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an ExtractValueInst, fold the result or return null.
Value * simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an InsertValueInst, fold the result or return null.
Value * simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
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.
Value * simplifyLoadInst(LoadInst *LI, Value *PtrOp, const SimplifyQuery &Q)
Given a load instruction and its pointer operand, fold the result or return null.
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.
Value * simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q)
Given a constrained FP intrinsic call, tries to compute its simplified version.
Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
Value * simplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
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.
ArrayRef(const T &OneElt) -> ArrayRef< T >
Value * simplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, const SimplifyQuery &Q)
Given operands for an InsertElement, fold the result or return null.
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.
Value * simplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an SRem, fold the result or return null.
const SimplifyQuery getBestSimplifyQuery(Pass &, Function &)
Value * simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FCmpInst, fold the result or return null.
Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, bool InBounds, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
Value * simplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a URem, fold the result or return null.
Value * simplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q)
Given operands for an ExtractElementInst, fold the result or return null.
Value * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.