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 // These routines are designed to tolerate moderately incomplete IR, such as
23 // instructions that are not connected to basic blocks yet. However, they do
24 // require that all the IR that they encounter be valid. In particular, they
25 // require that all non-constant values be defined in the same function, and the
26 // same call context of that function (and not split between caller and callee
27 // contexts of a directly recursive call, for example).
28 //
29 // Additionally, these routines can't simplify to the instructions that are not
30 // def-reachable, meaning we can't just scan the basic block for instructions
31 // to simplify to.
32 //
33 //===----------------------------------------------------------------------===//
34 
35 #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
36 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
37 
38 #include "llvm/IR/Instruction.h"
39 #include "llvm/IR/Operator.h"
40 #include "llvm/IR/PatternMatch.h"
41 
42 namespace llvm {
43 
44 template <typename T, typename... TArgs> class AnalysisManager;
45 template <class T> class ArrayRef;
46 class AssumptionCache;
47 class BinaryOperator;
48 class CallBase;
49 class DataLayout;
50 class DominatorTree;
51 class Function;
52 struct LoopStandardAnalysisResults;
53 class MDNode;
54 class OptimizationRemarkEmitter;
55 class Pass;
56 template <class T, unsigned n> class SmallSetVector;
57 class TargetLibraryInfo;
58 class Type;
59 class Value;
60 
61 /// InstrInfoQuery provides an interface to query additional information for
62 /// instructions like metadata or keywords like nsw, which provides conservative
63 /// results if the users specified it is safe to use.
64 struct InstrInfoQuery {
65   InstrInfoQuery(bool UMD) : UseInstrInfo(UMD) {}
66   InstrInfoQuery() : UseInstrInfo(true) {}
67   bool UseInstrInfo = true;
68 
69   MDNode *getMetadata(const Instruction *I, unsigned KindID) const {
70     if (UseInstrInfo)
71       return I->getMetadata(KindID);
72     return nullptr;
73   }
74 
75   template <class InstT> bool hasNoUnsignedWrap(const InstT *Op) const {
76     if (UseInstrInfo)
77       return Op->hasNoUnsignedWrap();
78     return false;
79   }
80 
81   template <class InstT> bool hasNoSignedWrap(const InstT *Op) const {
82     if (UseInstrInfo)
83       return Op->hasNoSignedWrap();
84     return false;
85   }
86 
87   bool isExact(const BinaryOperator *Op) const {
88     if (UseInstrInfo && isa<PossiblyExactOperator>(Op))
89       return cast<PossiblyExactOperator>(Op)->isExact();
90     return false;
91   }
92 };
93 
94 struct SimplifyQuery {
95   const DataLayout &DL;
96   const TargetLibraryInfo *TLI = nullptr;
97   const DominatorTree *DT = nullptr;
98   AssumptionCache *AC = nullptr;
99   const Instruction *CxtI = nullptr;
100 
101   // Wrapper to query additional information for instructions like metadata or
102   // keywords like nsw, which provides conservative results if those cannot
103   // be safely used.
104   const InstrInfoQuery IIQ;
105 
106   /// Controls whether simplifications are allowed to constrain the range of
107   /// possible values for uses of undef. If it is false, simplifications are not
108   /// allowed to assume a particular value for a use of undef for example.
109   bool CanUseUndef = true;
110 
111   SimplifyQuery(const DataLayout &DL, const Instruction *CXTI = nullptr)
112       : DL(DL), CxtI(CXTI) {}
113 
114   SimplifyQuery(const DataLayout &DL, const TargetLibraryInfo *TLI,
115                 const DominatorTree *DT = nullptr,
116                 AssumptionCache *AC = nullptr,
117                 const Instruction *CXTI = nullptr, bool UseInstrInfo = true,
118                 bool CanUseUndef = true)
119       : DL(DL), TLI(TLI), DT(DT), AC(AC), CxtI(CXTI), IIQ(UseInstrInfo),
120         CanUseUndef(CanUseUndef) {}
121   SimplifyQuery getWithInstruction(Instruction *I) const {
122     SimplifyQuery Copy(*this);
123     Copy.CxtI = I;
124     return Copy;
125   }
126   SimplifyQuery getWithoutUndef() const {
127     SimplifyQuery Copy(*this);
128     Copy.CanUseUndef = false;
129     return Copy;
130   }
131 
132   /// If CanUseUndef is true, returns whether \p V is undef.
133   /// Otherwise always return false.
134   bool isUndefValue(Value *V) const {
135     if (!CanUseUndef)
136       return false;
137 
138     using namespace PatternMatch;
139     return match(V, m_Undef());
140   }
141 };
142 
143 // NOTE: the explicit multiple argument versions of these functions are
144 // deprecated.
145 // Please use the SimplifyQuery versions in new code.
146 
147 /// Given operand for an FNeg, fold the result or return null.
148 Value *SimplifyFNegInst(Value *Op, FastMathFlags FMF, const SimplifyQuery &Q);
149 
150 /// Given operands for an Add, fold the result or return null.
151 Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
152                        const SimplifyQuery &Q);
153 
154 /// Given operands for a Sub, fold the result or return null.
155 Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
156                        const SimplifyQuery &Q);
157 
158 /// Given operands for an FAdd, fold the result or return null.
159 Value *
160 SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
161                  const SimplifyQuery &Q,
162                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
163                  RoundingMode Rounding = RoundingMode::NearestTiesToEven);
164 
165 /// Given operands for an FSub, fold the result or return null.
166 Value *
167 SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
168                  const SimplifyQuery &Q,
169                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
170                  RoundingMode Rounding = RoundingMode::NearestTiesToEven);
171 
172 /// Given operands for an FMul, fold the result or return null.
173 Value *
174 SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
175                  const SimplifyQuery &Q,
176                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
177                  RoundingMode Rounding = RoundingMode::NearestTiesToEven);
178 
179 /// Given operands for the multiplication of a FMA, fold the result or return
180 /// null. In contrast to SimplifyFMulInst, this function will not perform
181 /// simplifications whose unrounded results differ when rounded to the argument
182 /// type.
183 Value *SimplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF,
184                        const SimplifyQuery &Q,
185                        fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
186                        RoundingMode Rounding = RoundingMode::NearestTiesToEven);
187 
188 /// Given operands for a Mul, fold the result or return null.
189 Value *SimplifyMulInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
190 
191 /// Given operands for an SDiv, fold the result or return null.
192 Value *SimplifySDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
193 
194 /// Given operands for a UDiv, fold the result or return null.
195 Value *SimplifyUDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
196 
197 /// Given operands for an FDiv, fold the result or return null.
198 Value *
199 SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
200                  const SimplifyQuery &Q,
201                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
202                  RoundingMode Rounding = RoundingMode::NearestTiesToEven);
203 
204 /// Given operands for an SRem, fold the result or return null.
205 Value *SimplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
206 
207 /// Given operands for a URem, fold the result or return null.
208 Value *SimplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
209 
210 /// Given operands for an FRem, fold the result or return null.
211 Value *
212 SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
213                  const SimplifyQuery &Q,
214                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
215                  RoundingMode Rounding = RoundingMode::NearestTiesToEven);
216 
217 /// Given operands for a Shl, fold the result or return null.
218 Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
219                        const SimplifyQuery &Q);
220 
221 /// Given operands for a LShr, fold the result or return null.
222 Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
223                         const SimplifyQuery &Q);
224 
225 /// Given operands for a AShr, fold the result or return nulll.
226 Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
227                         const SimplifyQuery &Q);
228 
229 /// Given operands for an And, fold the result or return null.
230 Value *SimplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
231 
232 /// Given operands for an Or, fold the result or return null.
233 Value *SimplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
234 
235 /// Given operands for an Xor, fold the result or return null.
236 Value *SimplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
237 
238 /// Given operands for an ICmpInst, fold the result or return null.
239 Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
240                         const SimplifyQuery &Q);
241 
242 /// Given operands for an FCmpInst, fold the result or return null.
243 Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
244                         FastMathFlags FMF, const SimplifyQuery &Q);
245 
246 /// Given operands for a SelectInst, fold the result or return null.
247 Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
248                           const SimplifyQuery &Q);
249 
250 /// Given operands for a GetElementPtrInst, fold the result or return null.
251 Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
252                        const SimplifyQuery &Q);
253 
254 /// Given operands for an InsertValueInst, fold the result or return null.
255 Value *SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
256                                const SimplifyQuery &Q);
257 
258 /// Given operands for an InsertElement, fold the result or return null.
259 Value *SimplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx,
260                                  const SimplifyQuery &Q);
261 
262 /// Given operands for an ExtractValueInst, fold the result or return null.
263 Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
264                                 const SimplifyQuery &Q);
265 
266 /// Given operands for an ExtractElementInst, fold the result or return null.
267 Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
268                                   const SimplifyQuery &Q);
269 
270 /// Given operands for a CastInst, fold the result or return null.
271 Value *SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
272                         const SimplifyQuery &Q);
273 
274 /// Given operands for a ShuffleVectorInst, fold the result or return null.
275 /// See class ShuffleVectorInst for a description of the mask representation.
276 Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef<int> Mask,
277                                  Type *RetTy, const SimplifyQuery &Q);
278 
279 //=== Helper functions for higher up the class hierarchy.
280 
281 /// Given operands for a CmpInst, fold the result or return null.
282 Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
283                        const SimplifyQuery &Q);
284 
285 /// Given operand for a UnaryOperator, fold the result or return null.
286 Value *SimplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q);
287 
288 /// Given operand for a UnaryOperator, fold the result or return null.
289 /// Try to use FastMathFlags when folding the result.
290 Value *SimplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
291                     const SimplifyQuery &Q);
292 
293 /// Given operands for a BinaryOperator, fold the result or return null.
294 Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
295                      const SimplifyQuery &Q);
296 
297 /// Given operands for a BinaryOperator, fold the result or return null.
298 /// Try to use FastMathFlags when folding the result.
299 Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, FastMathFlags FMF,
300                      const SimplifyQuery &Q);
301 
302 /// Given a callsite, fold the result or return null.
303 Value *SimplifyCall(CallBase *Call, const SimplifyQuery &Q);
304 
305 /// Given an operand for a Freeze, see if we can fold the result.
306 /// If not, this returns null.
307 Value *SimplifyFreezeInst(Value *Op, const SimplifyQuery &Q);
308 
309 /// See if we can compute a simplified version of this instruction. If not,
310 /// return null.
311 Value *SimplifyInstruction(Instruction *I, const SimplifyQuery &Q,
312                            OptimizationRemarkEmitter *ORE = nullptr);
313 
314 /// Like \p SimplifyInstruction but the operands of \p I are replaced with
315 /// \p NewOps. Returns a simplified value, or null if none was found.
316 Value *
317 SimplifyInstructionWithOperands(Instruction *I, ArrayRef<Value *> NewOps,
318                                 const SimplifyQuery &Q,
319                                 OptimizationRemarkEmitter *ORE = nullptr);
320 
321 /// See if V simplifies when its operand Op is replaced with RepOp. If not,
322 /// return null.
323 /// AllowRefinement specifies whether the simplification can be a refinement
324 /// (e.g. 0 instead of poison), or whether it needs to be strictly identical.
325 Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
326                               const SimplifyQuery &Q, bool AllowRefinement);
327 
328 /// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
329 ///
330 /// This first performs a normal RAUW of I with SimpleV. It then recursively
331 /// attempts to simplify those users updated by the operation. The 'I'
332 /// instruction must not be equal to the simplified value 'SimpleV'.
333 /// If UnsimplifiedUsers is provided, instructions that could not be simplified
334 /// are added to it.
335 ///
336 /// The function returns true if any simplifications were performed.
337 bool replaceAndRecursivelySimplify(
338     Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI = nullptr,
339     const DominatorTree *DT = nullptr, AssumptionCache *AC = nullptr,
340     SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr);
341 
342 // These helper functions return a SimplifyQuery structure that contains as
343 // many of the optional analysis we use as are currently valid.  This is the
344 // strongly preferred way of constructing SimplifyQuery in passes.
345 const SimplifyQuery getBestSimplifyQuery(Pass &, Function &);
346 template <class T, class... TArgs>
347 const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &,
348                                          Function &);
349 const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &,
350                                          const DataLayout &);
351 } // end namespace llvm
352 
353 #endif
354