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 | |
34 | #include "llvm/Analysis/SimplifyQuery.h" |
35 | #include "llvm/IR/FPEnv.h" |
36 | #include "llvm/Support/Compiler.h" |
37 | |
38 | namespace llvm { |
39 | |
40 | template <typename T, typename... TArgs> class AnalysisManager; |
41 | template <class T> class ArrayRef; |
42 | class AssumptionCache; |
43 | class CallBase; |
44 | class DataLayout; |
45 | class DominatorTree; |
46 | class Function; |
47 | class Instruction; |
48 | class CmpPredicate; |
49 | class LoadInst; |
50 | struct LoopStandardAnalysisResults; |
51 | class Pass; |
52 | template <class T, unsigned n> class SmallSetVector; |
53 | class TargetLibraryInfo; |
54 | class Type; |
55 | class 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. |
62 | LLVM_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. |
66 | LLVM_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. |
70 | LLVM_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. |
74 | LLVM_ABI Value *simplifySDivInst(Value *LHS, Value *RHS, bool IsExact, |
75 | const SimplifyQuery &Q); |
76 | |
77 | /// Given operands for a UDiv, fold the result or return null. |
78 | LLVM_ABI Value *simplifyUDivInst(Value *LHS, Value *RHS, bool IsExact, |
79 | const SimplifyQuery &Q); |
80 | |
81 | /// Given operands for an SRem, fold the result or return null. |
82 | LLVM_ABI Value *simplifySRemInst(Value *LHS, Value *RHS, |
83 | const SimplifyQuery &Q); |
84 | |
85 | /// Given operands for a URem, fold the result or return null. |
86 | LLVM_ABI Value *simplifyURemInst(Value *LHS, Value *RHS, |
87 | const SimplifyQuery &Q); |
88 | |
89 | /// Given operand for an FNeg, fold the result or return null. |
90 | LLVM_ABI Value *simplifyFNegInst(Value *Op, FastMathFlags FMF, |
91 | const SimplifyQuery &Q); |
92 | |
93 | /// Given operands for an FAdd, fold the result or return null. |
94 | LLVM_ABI Value * |
95 | simplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF, |
96 | const SimplifyQuery &Q, |
97 | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
98 | RoundingMode Rounding = RoundingMode::NearestTiesToEven); |
99 | |
100 | /// Given operands for an FSub, fold the result or return null. |
101 | LLVM_ABI Value * |
102 | simplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF, |
103 | const SimplifyQuery &Q, |
104 | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
105 | RoundingMode Rounding = RoundingMode::NearestTiesToEven); |
106 | |
107 | /// Given operands for an FMul, fold the result or return null. |
108 | LLVM_ABI Value * |
109 | simplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, |
110 | const SimplifyQuery &Q, |
111 | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
112 | RoundingMode Rounding = RoundingMode::NearestTiesToEven); |
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. |
118 | LLVM_ABI Value * |
119 | simplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF, |
120 | const SimplifyQuery &Q, |
121 | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
122 | RoundingMode Rounding = RoundingMode::NearestTiesToEven); |
123 | |
124 | /// Given operands for an FDiv, fold the result or return null. |
125 | LLVM_ABI Value * |
126 | simplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF, |
127 | const SimplifyQuery &Q, |
128 | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
129 | RoundingMode Rounding = RoundingMode::NearestTiesToEven); |
130 | |
131 | /// Given operands for an FRem, fold the result or return null. |
132 | LLVM_ABI Value * |
133 | simplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF, |
134 | const SimplifyQuery &Q, |
135 | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
136 | RoundingMode Rounding = RoundingMode::NearestTiesToEven); |
137 | |
138 | /// Given operands for a Shl, fold the result or return null. |
139 | LLVM_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. |
143 | LLVM_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. |
147 | LLVM_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. |
151 | LLVM_ABI Value *simplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q); |
152 | |
153 | /// Given operands for an Or, fold the result or return null. |
154 | LLVM_ABI Value *simplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q); |
155 | |
156 | /// Given operands for an Xor, fold the result or return null. |
157 | LLVM_ABI Value *simplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q); |
158 | |
159 | /// Given operands for an ICmpInst, fold the result or return null. |
160 | LLVM_ABI Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, |
161 | const SimplifyQuery &Q); |
162 | |
163 | /// Given operands for an FCmpInst, fold the result or return null. |
164 | LLVM_ABI Value *simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, |
165 | FastMathFlags FMF, const SimplifyQuery &Q); |
166 | |
167 | /// Given operands for a SelectInst, fold the result or return null. |
168 | LLVM_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. |
172 | LLVM_ABI Value *simplifyGEPInst(Type *SrcTy, Value *Ptr, |
173 | ArrayRef<Value *> Indices, GEPNoWrapFlags NW, |
174 | const SimplifyQuery &Q); |
175 | |
176 | /// Given operands for an InsertValueInst, fold the result or return null. |
177 | LLVM_ABI Value *simplifyInsertValueInst(Value *Agg, Value *Val, |
178 | ArrayRef<unsigned> Idxs, |
179 | const SimplifyQuery &Q); |
180 | |
181 | /// Given operands for an InsertElement, fold the result or return null. |
182 | LLVM_ABI Value *simplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, |
183 | const SimplifyQuery &Q); |
184 | |
185 | /// Given operands for an ExtractValueInst, fold the result or return null. |
186 | LLVM_ABI Value *(Value *Agg, ArrayRef<unsigned> Idxs, |
187 | const SimplifyQuery &Q); |
188 | |
189 | /// Given operands for an ExtractElementInst, fold the result or return null. |
190 | LLVM_ABI Value *(Value *Vec, Value *Idx, |
191 | const SimplifyQuery &Q); |
192 | |
193 | /// Given operands for a CastInst, fold the result or return null. |
194 | LLVM_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 | LLVM_ABI Value *simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType, |
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. |
205 | LLVM_ABI Value *simplifyShuffleVectorInst(Value *Op0, Value *Op1, |
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. |
212 | LLVM_ABI Value *simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, |
213 | const SimplifyQuery &Q); |
214 | |
215 | /// Given operand for a UnaryOperator, fold the result or return null. |
216 | LLVM_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. |
221 | LLVM_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. |
225 | LLVM_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. |
230 | LLVM_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. |
234 | LLVM_ABI Value *simplifyCall(CallBase *Call, Value *Callee, |
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. |
244 | LLVM_ABI Value *simplifyConstrainedFPCall(CallBase *Call, |
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. |
249 | LLVM_ABI Value *simplifyFreezeInst(Value *Op, const SimplifyQuery &Q); |
250 | |
251 | /// Given a load instruction and its pointer operand, fold the result or return |
252 | /// null. |
253 | LLVM_ABI Value *simplifyLoadInst(LoadInst *LI, Value *PtrOp, |
254 | const SimplifyQuery &Q); |
255 | |
256 | /// See if we can compute a simplified version of this instruction. If not, |
257 | /// return null. |
258 | LLVM_ABI Value *simplifyInstruction(Instruction *I, const SimplifyQuery &Q); |
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. |
262 | LLVM_ABI Value *simplifyInstructionWithOperands(Instruction *I, |
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. |
275 | LLVM_ABI Value * |
276 | simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, |
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. |
289 | LLVM_ABI bool replaceAndRecursivelySimplify( |
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. |
297 | LLVM_ABI const SimplifyQuery getBestSimplifyQuery(Pass &, Function &); |
298 | template <class T, class... TArgs> |
299 | const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &, |
300 | Function &); |
301 | LLVM_ABI const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &, |
302 | const DataLayout &); |
303 | } // end namespace llvm |
304 | |
305 | #endif |
306 | |