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"
40 class AssumptionCache;
45 class TargetLibraryInfo;
49 /// Given operands for an Add, fold the result or return null.
50 Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
52 const TargetLibraryInfo *TLI = nullptr,
53 const DominatorTree *DT = nullptr,
54 AssumptionCache *AC = nullptr,
55 const Instruction *CxtI = nullptr);
57 /// Given operands for a Sub, fold the result or return null.
58 Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
60 const TargetLibraryInfo *TLI = nullptr,
61 const DominatorTree *DT = nullptr,
62 AssumptionCache *AC = nullptr,
63 const Instruction *CxtI = nullptr);
65 /// Given operands for an FAdd, fold the result or return null.
66 Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
68 const TargetLibraryInfo *TLI = nullptr,
69 const DominatorTree *DT = nullptr,
70 AssumptionCache *AC = nullptr,
71 const Instruction *CxtI = nullptr);
73 /// Given operands for an FSub, fold the result or return null.
74 Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
76 const TargetLibraryInfo *TLI = nullptr,
77 const DominatorTree *DT = nullptr,
78 AssumptionCache *AC = nullptr,
79 const Instruction *CxtI = nullptr);
81 /// Given operands for an FMul, fold the result or return null.
82 Value *SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
84 const TargetLibraryInfo *TLI = nullptr,
85 const DominatorTree *DT = nullptr,
86 AssumptionCache *AC = nullptr,
87 const Instruction *CxtI = nullptr);
89 /// Given operands for a Mul, fold the result or return null.
90 Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout &DL,
91 const TargetLibraryInfo *TLI = nullptr,
92 const DominatorTree *DT = nullptr,
93 AssumptionCache *AC = nullptr,
94 const Instruction *CxtI = nullptr);
96 /// Given operands for an SDiv, fold the result or return null.
97 Value *SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout &DL,
98 const TargetLibraryInfo *TLI = nullptr,
99 const DominatorTree *DT = nullptr,
100 AssumptionCache *AC = nullptr,
101 const Instruction *CxtI = nullptr);
103 /// Given operands for a UDiv, fold the result or return null.
104 Value *SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout &DL,
105 const TargetLibraryInfo *TLI = nullptr,
106 const DominatorTree *DT = nullptr,
107 AssumptionCache *AC = nullptr,
108 const Instruction *CxtI = nullptr);
110 /// Given operands for an FDiv, fold the result or return null.
111 Value *SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
112 const DataLayout &DL,
113 const TargetLibraryInfo *TLI = nullptr,
114 const DominatorTree *DT = nullptr,
115 AssumptionCache *AC = nullptr,
116 const Instruction *CxtI = nullptr);
118 /// Given operands for an SRem, fold the result or return null.
119 Value *SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout &DL,
120 const TargetLibraryInfo *TLI = nullptr,
121 const DominatorTree *DT = nullptr,
122 AssumptionCache *AC = nullptr,
123 const Instruction *CxtI = nullptr);
125 /// Given operands for a URem, fold the result or return null.
126 Value *SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout &DL,
127 const TargetLibraryInfo *TLI = nullptr,
128 const DominatorTree *DT = nullptr,
129 AssumptionCache *AC = nullptr,
130 const Instruction *CxtI = nullptr);
132 /// Given operands for an FRem, fold the result or return null.
133 Value *SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
134 const DataLayout &DL,
135 const TargetLibraryInfo *TLI = nullptr,
136 const DominatorTree *DT = nullptr,
137 AssumptionCache *AC = nullptr,
138 const Instruction *CxtI = nullptr);
140 /// Given operands for a Shl, fold the result or return null.
141 Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
142 const DataLayout &DL,
143 const TargetLibraryInfo *TLI = nullptr,
144 const DominatorTree *DT = nullptr,
145 AssumptionCache *AC = nullptr,
146 const Instruction *CxtI = nullptr);
148 /// Given operands for a LShr, fold the result or return null.
149 Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
150 const DataLayout &DL,
151 const TargetLibraryInfo *TLI = nullptr,
152 const DominatorTree *DT = nullptr,
153 AssumptionCache *AC = nullptr,
154 const Instruction *CxtI = nullptr);
156 /// Given operands for a AShr, fold the result or return nulll.
157 Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
158 const DataLayout &DL,
159 const TargetLibraryInfo *TLI = nullptr,
160 const DominatorTree *DT = nullptr,
161 AssumptionCache *AC = nullptr,
162 const Instruction *CxtI = nullptr);
164 /// Given operands for an And, fold the result or return null.
165 Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout &DL,
166 const TargetLibraryInfo *TLI = nullptr,
167 const DominatorTree *DT = nullptr,
168 AssumptionCache *AC = nullptr,
169 const Instruction *CxtI = nullptr);
171 /// Given operands for an Or, fold the result or return null.
172 Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout &DL,
173 const TargetLibraryInfo *TLI = nullptr,
174 const DominatorTree *DT = nullptr,
175 AssumptionCache *AC = nullptr,
176 const Instruction *CxtI = nullptr);
178 /// Given operands for an Xor, fold the result or return null.
179 Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout &DL,
180 const TargetLibraryInfo *TLI = nullptr,
181 const DominatorTree *DT = nullptr,
182 AssumptionCache *AC = nullptr,
183 const Instruction *CxtI = nullptr);
185 /// Given operands for an ICmpInst, fold the result or return null.
186 Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
187 const DataLayout &DL,
188 const TargetLibraryInfo *TLI = nullptr,
189 const DominatorTree *DT = nullptr,
190 AssumptionCache *AC = nullptr,
191 const Instruction *CxtI = nullptr);
193 /// Given operands for an FCmpInst, fold the result or return null.
194 Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
195 FastMathFlags FMF, const DataLayout &DL,
196 const TargetLibraryInfo *TLI = nullptr,
197 const DominatorTree *DT = nullptr,
198 AssumptionCache *AC = nullptr,
199 const Instruction *CxtI = nullptr);
201 /// Given operands for a SelectInst, fold the result or return null.
202 Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
203 const DataLayout &DL,
204 const TargetLibraryInfo *TLI = nullptr,
205 const DominatorTree *DT = nullptr,
206 AssumptionCache *AC = nullptr,
207 const Instruction *CxtI = nullptr);
209 /// Given operands for a GetElementPtrInst, fold the result or return null.
210 Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
211 const DataLayout &DL,
212 const TargetLibraryInfo *TLI = nullptr,
213 const DominatorTree *DT = nullptr,
214 AssumptionCache *AC = nullptr,
215 const Instruction *CxtI = nullptr);
217 /// Given operands for an InsertValueInst, fold the result or return null.
218 Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
219 ArrayRef<unsigned> Idxs, const DataLayout &DL,
220 const TargetLibraryInfo *TLI = nullptr,
221 const DominatorTree *DT = nullptr,
222 AssumptionCache *AC = nullptr,
223 const Instruction *CxtI = nullptr);
225 /// Given operands for an ExtractValueInst, fold the result or return null.
226 Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
227 const DataLayout &DL,
228 const TargetLibraryInfo *TLI = nullptr,
229 const DominatorTree *DT = nullptr,
230 AssumptionCache *AC = nullptr,
231 const Instruction *CxtI = nullptr);
233 /// Given operands for an ExtractElementInst, fold the result or return null.
234 Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
235 const DataLayout &DL,
236 const TargetLibraryInfo *TLI = nullptr,
237 const DominatorTree *DT = nullptr,
238 AssumptionCache *AC = nullptr,
239 const Instruction *CxtI = nullptr);
241 /// Given operands for a CastInst, fold the result or return null.
242 Value *SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
243 const DataLayout &DL,
244 const TargetLibraryInfo *TLI = nullptr,
245 const DominatorTree *DT = nullptr,
246 AssumptionCache *AC = nullptr,
247 const Instruction *CxtI = nullptr);
249 //=== Helper functions for higher up the class hierarchy.
252 /// Given operands for a CmpInst, fold the result or return null.
253 Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
254 const DataLayout &DL,
255 const TargetLibraryInfo *TLI = nullptr,
256 const DominatorTree *DT = nullptr,
257 AssumptionCache *AC = nullptr,
258 const Instruction *CxtI = nullptr);
260 /// Given operands for a BinaryOperator, fold the result or return null.
261 Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
262 const DataLayout &DL,
263 const TargetLibraryInfo *TLI = nullptr,
264 const DominatorTree *DT = nullptr,
265 AssumptionCache *AC = nullptr,
266 const Instruction *CxtI = nullptr);
268 /// Given operands for an FP BinaryOperator, fold the result or return null.
269 /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
270 /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
271 Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
272 const FastMathFlags &FMF, const DataLayout &DL,
273 const TargetLibraryInfo *TLI = nullptr,
274 const DominatorTree *DT = nullptr,
275 AssumptionCache *AC = nullptr,
276 const Instruction *CxtI = nullptr);
278 /// Given a function and iterators over arguments, fold the result or return
280 Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
281 User::op_iterator ArgEnd, const DataLayout &DL,
282 const TargetLibraryInfo *TLI = nullptr,
283 const DominatorTree *DT = nullptr,
284 AssumptionCache *AC = nullptr,
285 const Instruction *CxtI = nullptr);
287 /// Given a function and set of arguments, fold the result or return null.
288 Value *SimplifyCall(Value *V, ArrayRef<Value *> Args, const DataLayout &DL,
289 const TargetLibraryInfo *TLI = nullptr,
290 const DominatorTree *DT = nullptr,
291 AssumptionCache *AC = nullptr,
292 const Instruction *CxtI = nullptr);
294 /// See if we can compute a simplified version of this instruction. If not,
296 Value *SimplifyInstruction(Instruction *I, const DataLayout &DL,
297 const TargetLibraryInfo *TLI = nullptr,
298 const DominatorTree *DT = nullptr,
299 AssumptionCache *AC = nullptr);
301 /// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
303 /// This first performs a normal RAUW of I with SimpleV. It then recursively
304 /// attempts to simplify those users updated by the operation. The 'I'
305 /// instruction must not be equal to the simplified value 'SimpleV'.
307 /// The function returns true if any simplifications were performed.
308 bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
309 const TargetLibraryInfo *TLI = nullptr,
310 const DominatorTree *DT = nullptr,
311 AssumptionCache *AC = nullptr);
313 /// Recursively attempt to simplify an instruction.
315 /// This routine uses SimplifyInstruction to simplify 'I', and if successful
316 /// replaces uses of 'I' with the simplified value. It then recurses on each
317 /// of the users impacted. It returns true if any simplifications were
319 bool recursivelySimplifyInstruction(Instruction *I,
320 const TargetLibraryInfo *TLI = nullptr,
321 const DominatorTree *DT = nullptr,
322 AssumptionCache *AC = nullptr);
323 } // end namespace llvm