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