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;
46 struct LoopStandardAnalysisResults;
47 class OptimizationRemarkEmitter;
49 class TargetLibraryInfo;
53 struct SimplifyQuery {
55 const TargetLibraryInfo *TLI = nullptr;
56 const DominatorTree *DT = nullptr;
57 AssumptionCache *AC = nullptr;
58 const Instruction *CxtI = nullptr;
60 SimplifyQuery(const DataLayout &DL, const Instruction *CXTI = nullptr)
61 : DL(DL), CxtI(CXTI) {}
63 SimplifyQuery(const DataLayout &DL, const TargetLibraryInfo *TLI,
64 const DominatorTree *DT = nullptr,
65 AssumptionCache *AC = nullptr,
66 const Instruction *CXTI = nullptr)
67 : DL(DL), TLI(TLI), DT(DT), AC(AC), CxtI(CXTI) {}
68 SimplifyQuery getWithInstruction(Instruction *I) const {
69 SimplifyQuery Copy(*this);
75 // NOTE: the explicit multiple argument versions of these functions are
77 // Please use the SimplifyQuery versions in new code.
79 /// Given operands for an Add, fold the result or return null.
80 Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
81 const SimplifyQuery &Q);
83 /// Given operands for a Sub, fold the result or return null.
84 Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
85 const SimplifyQuery &Q);
87 /// Given operands for an FAdd, fold the result or return null.
88 Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
89 const SimplifyQuery &Q);
91 /// Given operands for an FSub, fold the result or return null.
92 Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
93 const SimplifyQuery &Q);
95 /// Given operands for an FMul, fold the result or return null.
96 Value *SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
97 const SimplifyQuery &Q);
99 /// Given operands for a Mul, fold the result or return null.
100 Value *SimplifyMulInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
102 /// Given operands for an SDiv, fold the result or return null.
103 Value *SimplifySDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
105 /// Given operands for a UDiv, fold the result or return null.
106 Value *SimplifyUDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
108 /// Given operands for an FDiv, fold the result or return null.
109 Value *SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
110 const SimplifyQuery &Q);
112 /// Given operands for an SRem, fold the result or return null.
113 Value *SimplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
115 /// Given operands for a URem, fold the result or return null.
116 Value *SimplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
118 /// Given operands for an FRem, fold the result or return null.
119 Value *SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
120 const SimplifyQuery &Q);
122 /// Given operands for a Shl, fold the result or return null.
123 Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
124 const SimplifyQuery &Q);
126 /// Given operands for a LShr, fold the result or return null.
127 Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
128 const SimplifyQuery &Q);
130 /// Given operands for a AShr, fold the result or return nulll.
131 Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
132 const SimplifyQuery &Q);
134 /// Given operands for an And, fold the result or return null.
135 Value *SimplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
137 /// Given operands for an Or, fold the result or return null.
138 Value *SimplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
140 /// Given operands for an Xor, fold the result or return null.
141 Value *SimplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
143 /// Given operands for an ICmpInst, fold the result or return null.
144 Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
145 const SimplifyQuery &Q);
147 /// Given operands for an FCmpInst, fold the result or return null.
148 Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
149 FastMathFlags FMF, const SimplifyQuery &Q);
151 /// Given operands for a SelectInst, fold the result or return null.
152 Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
153 const SimplifyQuery &Q);
155 /// Given operands for a GetElementPtrInst, fold the result or return null.
156 Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
157 const SimplifyQuery &Q);
159 /// Given operands for an InsertValueInst, fold the result or return null.
160 Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
161 ArrayRef<unsigned> Idxs,
162 const SimplifyQuery &Q);
164 /// Given operands for an ExtractValueInst, fold the result or return null.
165 Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
166 const SimplifyQuery &Q);
168 /// Given operands for an ExtractElementInst, fold the result or return null.
169 Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
170 const SimplifyQuery &Q);
172 /// Given operands for a CastInst, fold the result or return null.
173 Value *SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
174 const SimplifyQuery &Q);
176 /// Given operands for a ShuffleVectorInst, fold the result or return null.
177 Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask,
178 Type *RetTy, const SimplifyQuery &Q);
180 //=== Helper functions for higher up the class hierarchy.
183 /// Given operands for a CmpInst, fold the result or return null.
184 Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
185 const SimplifyQuery &Q);
187 /// Given operands for a BinaryOperator, fold the result or return null.
188 Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
189 const SimplifyQuery &Q);
191 /// Given operands for an FP BinaryOperator, fold the result or return null.
192 /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
193 /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
194 Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
195 FastMathFlags FMF, const SimplifyQuery &Q);
197 /// Given a function and iterators over arguments, fold the result or return
199 Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
200 User::op_iterator ArgEnd, const SimplifyQuery &Q);
202 /// Given a function and set of arguments, fold the result or return null.
203 Value *SimplifyCall(Value *V, ArrayRef<Value *> Args, const SimplifyQuery &Q);
205 /// See if we can compute a simplified version of this instruction. If not,
207 Value *SimplifyInstruction(Instruction *I, const SimplifyQuery &Q,
208 OptimizationRemarkEmitter *ORE = nullptr);
210 /// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
212 /// This first performs a normal RAUW of I with SimpleV. It then recursively
213 /// attempts to simplify those users updated by the operation. The 'I'
214 /// instruction must not be equal to the simplified value 'SimpleV'.
216 /// The function returns true if any simplifications were performed.
217 bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
218 const TargetLibraryInfo *TLI = nullptr,
219 const DominatorTree *DT = nullptr,
220 AssumptionCache *AC = nullptr);
222 /// Recursively attempt to simplify an instruction.
224 /// This routine uses SimplifyInstruction to simplify 'I', and if successful
225 /// replaces uses of 'I' with the simplified value. It then recurses on each
226 /// of the users impacted. It returns true if any simplifications were
228 bool recursivelySimplifyInstruction(Instruction *I,
229 const TargetLibraryInfo *TLI = nullptr,
230 const DominatorTree *DT = nullptr,
231 AssumptionCache *AC = nullptr);
232 // These helper functions return a SimplifyQuery structure that contains as
233 // many of the optional analysis we use as are currently valid. This is the
234 // strongly preferred way of constructing SimplifyQuery in passes.
235 const SimplifyQuery getBestSimplifyQuery(Pass &, Function &);
236 template <class T, class... TArgs>
237 const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &,
239 const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &,
241 } // end namespace llvm