1 //===-- InstructionSimplify.h - Fold instrs into simpler forms --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares routines for folding instructions into simpler forms
11 // that do not require creating new instructions. This does constant folding
12 // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
13 // returning a constant ("and i32 %x, 0" -> "0") or an already existing value
14 // ("and i32 %x, %x" -> "%x"). If the simplification is also an instruction
15 // then it dominates the original instruction.
17 // These routines implicitly resolve undef uses. The easiest way to be safe when
18 // using these routines to obtain simplified values for existing instructions is
19 // to always replace all uses of the instructions with the resulting simplified
20 // values. This will prevent other code from seeing the same undef uses and
21 // resolving them to different values.
23 // These routines are designed to tolerate moderately incomplete IR, such as
24 // instructions that are not connected to basic blocks yet. However, they do
25 // require that all the IR that they encounter be valid. In particular, they
26 // require that all non-constant values be defined in the same function, and the
27 // same call context of that function (and not split between caller and callee
28 // contexts of a directly recursive call, for example).
30 //===----------------------------------------------------------------------===//
32 #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
33 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
35 #include "llvm/IR/User.h"
39 template <typename T, typename... TArgs> class AnalysisManager;
40 template <class T> class ArrayRef;
41 class AssumptionCache;
44 class ImmutableCallSite;
47 struct LoopStandardAnalysisResults;
48 class OptimizationRemarkEmitter;
50 class TargetLibraryInfo;
54 struct SimplifyQuery {
56 const TargetLibraryInfo *TLI = nullptr;
57 const DominatorTree *DT = nullptr;
58 AssumptionCache *AC = nullptr;
59 const Instruction *CxtI = nullptr;
61 SimplifyQuery(const DataLayout &DL, const Instruction *CXTI = nullptr)
62 : DL(DL), CxtI(CXTI) {}
64 SimplifyQuery(const DataLayout &DL, const TargetLibraryInfo *TLI,
65 const DominatorTree *DT = nullptr,
66 AssumptionCache *AC = nullptr,
67 const Instruction *CXTI = nullptr)
68 : DL(DL), TLI(TLI), DT(DT), AC(AC), CxtI(CXTI) {}
69 SimplifyQuery getWithInstruction(Instruction *I) const {
70 SimplifyQuery Copy(*this);
76 // NOTE: the explicit multiple argument versions of these functions are
78 // Please use the SimplifyQuery versions in new code.
80 /// Given operands for an Add, fold the result or return null.
81 Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
82 const SimplifyQuery &Q);
84 /// Given operands for a Sub, fold the result or return null.
85 Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
86 const SimplifyQuery &Q);
88 /// Given operands for an FAdd, fold the result or return null.
89 Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
90 const SimplifyQuery &Q);
92 /// Given operands for an FSub, fold the result or return null.
93 Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
94 const SimplifyQuery &Q);
96 /// Given operands for an FMul, fold the result or return null.
97 Value *SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
98 const SimplifyQuery &Q);
100 /// Given operands for a Mul, fold the result or return null.
101 Value *SimplifyMulInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
103 /// Given operands for an SDiv, fold the result or return null.
104 Value *SimplifySDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
106 /// Given operands for a UDiv, fold the result or return null.
107 Value *SimplifyUDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
109 /// Given operands for an FDiv, fold the result or return null.
110 Value *SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
111 const SimplifyQuery &Q);
113 /// Given operands for an SRem, fold the result or return null.
114 Value *SimplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
116 /// Given operands for a URem, fold the result or return null.
117 Value *SimplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
119 /// Given operands for an FRem, fold the result or return null.
120 Value *SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
121 const SimplifyQuery &Q);
123 /// Given operands for a Shl, fold the result or return null.
124 Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
125 const SimplifyQuery &Q);
127 /// Given operands for a LShr, fold the result or return null.
128 Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
129 const SimplifyQuery &Q);
131 /// Given operands for a AShr, fold the result or return nulll.
132 Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
133 const SimplifyQuery &Q);
135 /// Given operands for an And, fold the result or return null.
136 Value *SimplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
138 /// Given operands for an Or, fold the result or return null.
139 Value *SimplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
141 /// Given operands for an Xor, fold the result or return null.
142 Value *SimplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
144 /// Given operands for an ICmpInst, fold the result or return null.
145 Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
146 const SimplifyQuery &Q);
148 /// Given operands for an FCmpInst, fold the result or return null.
149 Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
150 FastMathFlags FMF, const SimplifyQuery &Q);
152 /// Given operands for a SelectInst, fold the result or return null.
153 Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
154 const SimplifyQuery &Q);
156 /// Given operands for a GetElementPtrInst, fold the result or return null.
157 Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
158 const SimplifyQuery &Q);
160 /// Given operands for an InsertValueInst, fold the result or return null.
161 Value *SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
162 const SimplifyQuery &Q);
164 /// Given operands for an InsertElement, fold the result or return null.
165 Value *SimplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx,
166 const SimplifyQuery &Q);
168 /// Given operands for an ExtractValueInst, fold the result or return null.
169 Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
170 const SimplifyQuery &Q);
172 /// Given operands for an ExtractElementInst, fold the result or return null.
173 Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
174 const SimplifyQuery &Q);
176 /// Given operands for a CastInst, fold the result or return null.
177 Value *SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
178 const SimplifyQuery &Q);
180 /// Given operands for a ShuffleVectorInst, fold the result or return null.
181 Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask,
182 Type *RetTy, const SimplifyQuery &Q);
184 //=== Helper functions for higher up the class hierarchy.
186 /// Given operands for a CmpInst, fold the result or return null.
187 Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
188 const SimplifyQuery &Q);
190 /// Given operands for a BinaryOperator, fold the result or return null.
191 Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
192 const SimplifyQuery &Q);
194 /// Given operands for an FP BinaryOperator, fold the result or return null.
195 /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
196 /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
197 Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
198 FastMathFlags FMF, const SimplifyQuery &Q);
200 /// Given a callsite, fold the result or return null.
201 Value *SimplifyCall(ImmutableCallSite CS, const SimplifyQuery &Q);
203 /// Given a function and iterators over arguments, fold the result or return
205 Value *SimplifyCall(ImmutableCallSite CS, Value *V, User::op_iterator ArgBegin,
206 User::op_iterator ArgEnd, const SimplifyQuery &Q);
208 /// Given a function and set of arguments, fold the result or return null.
209 Value *SimplifyCall(ImmutableCallSite CS, Value *V, ArrayRef<Value *> Args,
210 const SimplifyQuery &Q);
212 /// See if we can compute a simplified version of this instruction. If not,
214 Value *SimplifyInstruction(Instruction *I, const SimplifyQuery &Q,
215 OptimizationRemarkEmitter *ORE = nullptr);
217 /// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
219 /// This first performs a normal RAUW of I with SimpleV. It then recursively
220 /// attempts to simplify those users updated by the operation. The 'I'
221 /// instruction must not be equal to the simplified value 'SimpleV'.
223 /// The function returns true if any simplifications were performed.
224 bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
225 const TargetLibraryInfo *TLI = nullptr,
226 const DominatorTree *DT = nullptr,
227 AssumptionCache *AC = nullptr);
229 /// Recursively attempt to simplify an instruction.
231 /// This routine uses SimplifyInstruction to simplify 'I', and if successful
232 /// replaces uses of 'I' with the simplified value. It then recurses on each
233 /// of the users impacted. It returns true if any simplifications were
235 bool recursivelySimplifyInstruction(Instruction *I,
236 const TargetLibraryInfo *TLI = nullptr,
237 const DominatorTree *DT = nullptr,
238 AssumptionCache *AC = nullptr);
240 // These helper functions return a SimplifyQuery structure that contains as
241 // many of the optional analysis we use as are currently valid. This is the
242 // strongly preferred way of constructing SimplifyQuery in passes.
243 const SimplifyQuery getBestSimplifyQuery(Pass &, Function &);
244 template <class T, class... TArgs>
245 const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &,
247 const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &,
249 } // end namespace llvm