cc734b3cb2ca4318ad962f3a803e23b4607568d3
[oota-llvm.git] / include / llvm / Analysis / InstructionSimplify.h
1 //===-- InstructionSimplify.h - Fold instrs into simpler forms --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
16 //
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.
22 //
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).
29 //
30 //===----------------------------------------------------------------------===//
31
32 #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
33 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
34
35 #include "llvm/IR/User.h"
36
37 namespace llvm {
38   template<typename T>
39   class ArrayRef;
40   class AssumptionCache;
41   class DominatorTree;
42   class Instruction;
43   class DataLayout;
44   class FastMathFlags;
45   class TargetLibraryInfo;
46   class Type;
47   class Value;
48
49   /// SimplifyAddInst - Given operands for an Add, see if we can
50   /// fold the result.  If not, this returns null.
51   Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
52                          const DataLayout *TD = nullptr,
53                          const TargetLibraryInfo *TLI = nullptr,
54                          const DominatorTree *DT = nullptr,
55                          AssumptionCache *AC = nullptr,
56                          const Instruction *CxtI = nullptr);
57
58   /// SimplifySubInst - Given operands for a Sub, see if we can
59   /// fold the result.  If not, this returns null.
60   Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
61                          const DataLayout *TD = nullptr,
62                          const TargetLibraryInfo *TLI = nullptr,
63                          const DominatorTree *DT = nullptr,
64                          AssumptionCache *AC = nullptr,
65                          const Instruction *CxtI = nullptr);
66
67   /// Given operands for an FAdd, see if we can fold the result.  If not, this
68   /// returns null.
69   Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
70                           const DataLayout *TD = nullptr,
71                           const TargetLibraryInfo *TLI = nullptr,
72                           const DominatorTree *DT = nullptr,
73                           AssumptionCache *AC = nullptr,
74                           const Instruction *CxtI = nullptr);
75
76   /// Given operands for an FSub, see if we can fold the result.  If not, this
77   /// returns null.
78   Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
79                           const DataLayout *TD = nullptr,
80                           const TargetLibraryInfo *TLI = nullptr,
81                           const DominatorTree *DT = nullptr,
82                           AssumptionCache *AC = nullptr,
83                           const Instruction *CxtI = nullptr);
84
85   /// Given operands for an FMul, see if we can fold the result.  If not, this
86   /// returns null.
87   Value *SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
88                           const DataLayout *TD = nullptr,
89                           const TargetLibraryInfo *TLI = nullptr,
90                           const DominatorTree *DT = nullptr,
91                           AssumptionCache *AC = nullptr,
92                           const Instruction *CxtI = nullptr);
93
94   /// SimplifyMulInst - Given operands for a Mul, see if we can
95   /// fold the result.  If not, this returns null.
96   Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
97                          const TargetLibraryInfo *TLI = nullptr,
98                          const DominatorTree *DT = nullptr,
99                          AssumptionCache *AC = nullptr,
100                          const Instruction *CxtI = nullptr);
101
102   /// SimplifySDivInst - Given operands for an SDiv, see if we can
103   /// fold the result.  If not, this returns null.
104   Value *SimplifySDivInst(Value *LHS, Value *RHS,
105                           const DataLayout *TD = nullptr,
106                           const TargetLibraryInfo *TLI = nullptr,
107                           const DominatorTree *DT = nullptr,
108                           AssumptionCache *AC = nullptr,
109                           const Instruction *CxtI = nullptr);
110
111   /// SimplifyUDivInst - Given operands for a UDiv, see if we can
112   /// fold the result.  If not, this returns null.
113   Value *SimplifyUDivInst(Value *LHS, Value *RHS,
114                           const DataLayout *TD = nullptr,
115                           const TargetLibraryInfo *TLI = nullptr,
116                           const DominatorTree *DT = nullptr,
117                           AssumptionCache *AC = nullptr,
118                           const Instruction *CxtI = nullptr);
119
120   /// SimplifyFDivInst - Given operands for an FDiv, see if we can
121   /// fold the result.  If not, this returns null.
122   Value *SimplifyFDivInst(Value *LHS, Value *RHS,
123                           const DataLayout *TD = nullptr,
124                           const TargetLibraryInfo *TLI = nullptr,
125                           const DominatorTree *DT = nullptr,
126                           AssumptionCache *AC = nullptr,
127                           const Instruction *CxtI = nullptr);
128
129   /// SimplifySRemInst - Given operands for an SRem, see if we can
130   /// fold the result.  If not, this returns null.
131   Value *SimplifySRemInst(Value *LHS, Value *RHS,
132                           const DataLayout *TD = nullptr,
133                           const TargetLibraryInfo *TLI = nullptr,
134                           const DominatorTree *DT = nullptr,
135                           AssumptionCache *AC = nullptr,
136                           const Instruction *CxtI = nullptr);
137
138   /// SimplifyURemInst - Given operands for a URem, see if we can
139   /// fold the result.  If not, this returns null.
140   Value *SimplifyURemInst(Value *LHS, Value *RHS,
141                           const DataLayout *TD = nullptr,
142                           const TargetLibraryInfo *TLI = nullptr,
143                           const DominatorTree *DT = nullptr,
144                           AssumptionCache *AC = nullptr,
145                           const Instruction *CxtI = nullptr);
146
147   /// SimplifyFRemInst - Given operands for an FRem, see if we can
148   /// fold the result.  If not, this returns null.
149   Value *SimplifyFRemInst(Value *LHS, Value *RHS,
150                           const DataLayout *TD = nullptr,
151                           const TargetLibraryInfo *TLI = nullptr,
152                           const DominatorTree *DT = nullptr,
153                           AssumptionCache *AC = nullptr,
154                           const Instruction *CxtI = nullptr);
155
156   /// SimplifyShlInst - Given operands for a Shl, see if we can
157   /// fold the result.  If not, this returns null.
158   Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
159                          const DataLayout *TD = nullptr,
160                          const TargetLibraryInfo *TLI = nullptr,
161                          const DominatorTree *DT = nullptr,
162                          AssumptionCache *AC = nullptr,
163                          const Instruction *CxtI = nullptr);
164
165   /// SimplifyLShrInst - Given operands for a LShr, see if we can
166   /// fold the result.  If not, this returns null.
167   Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
168                           const DataLayout *TD = nullptr,
169                           const TargetLibraryInfo *TLI = nullptr,
170                           const DominatorTree *DT = nullptr,
171                           AssumptionCache *AC = nullptr,
172                           const Instruction *CxtI = nullptr);
173
174   /// SimplifyAShrInst - Given operands for a AShr, see if we can
175   /// fold the result.  If not, this returns null.
176   Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
177                           const DataLayout *TD = nullptr,
178                           const TargetLibraryInfo *TLI = nullptr,
179                           const DominatorTree *DT = nullptr,
180                           AssumptionCache *AC = nullptr,
181                           const Instruction *CxtI = nullptr);
182
183   /// SimplifyAndInst - Given operands for an And, see if we can
184   /// fold the result.  If not, this returns null.
185   Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
186                          const TargetLibraryInfo *TLI = nullptr,
187                          const DominatorTree *DT = nullptr,
188                          AssumptionCache *AC = nullptr,
189                          const Instruction *CxtI = nullptr);
190
191   /// SimplifyOrInst - Given operands for an Or, see if we can
192   /// fold the result.  If not, this returns null.
193   Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
194                         const TargetLibraryInfo *TLI = nullptr,
195                         const DominatorTree *DT = nullptr,
196                         AssumptionCache *AC = nullptr,
197                         const Instruction *CxtI = nullptr);
198
199   /// SimplifyXorInst - Given operands for a Xor, see if we can
200   /// fold the result.  If not, this returns null.
201   Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
202                          const TargetLibraryInfo *TLI = nullptr,
203                          const DominatorTree *DT = nullptr,
204                          AssumptionCache *AC = nullptr,
205                          const Instruction *CxtI = nullptr);
206
207   /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
208   /// fold the result.  If not, this returns null.
209   Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
210                           const DataLayout *TD = nullptr,
211                           const TargetLibraryInfo *TLI = nullptr,
212                           const DominatorTree *DT = nullptr,
213                           AssumptionCache *AC = nullptr,
214                           Instruction *CxtI = nullptr);
215
216   /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
217   /// fold the result.  If not, this returns null.
218   Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
219                           const DataLayout *TD = nullptr,
220                           const TargetLibraryInfo *TLI = nullptr,
221                           const DominatorTree *DT = nullptr,
222                           AssumptionCache *AC = nullptr,
223                           const Instruction *CxtI = nullptr);
224
225   /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
226   /// the result.  If not, this returns null.
227   Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
228                             const DataLayout *TD = nullptr,
229                             const TargetLibraryInfo *TLI = nullptr,
230                             const DominatorTree *DT = nullptr,
231                             AssumptionCache *AC = nullptr,
232                             const Instruction *CxtI = nullptr);
233
234   /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
235   /// fold the result.  If not, this returns null.
236   Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *TD = nullptr,
237                          const TargetLibraryInfo *TLI = nullptr,
238                          const DominatorTree *DT = nullptr,
239                          AssumptionCache *AC = nullptr,
240                          const Instruction *CxtI = nullptr);
241
242   /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
243   /// can fold the result.  If not, this returns null.
244   Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
245                                  ArrayRef<unsigned> Idxs,
246                                  const DataLayout *TD = nullptr,
247                                  const TargetLibraryInfo *TLI = nullptr,
248                                  const DominatorTree *DT = nullptr,
249                                  AssumptionCache *AC = nullptr,
250                                  const Instruction *CxtI = nullptr);
251
252   /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold
253   /// the result.  If not, this returns null.
254   Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD = nullptr,
255                            const TargetLibraryInfo *TLI = nullptr,
256                            const DominatorTree *DT = nullptr,
257                            AssumptionCache *AC = nullptr,
258                            const Instruction *CxtI = nullptr);
259
260   //=== Helper functions for higher up the class hierarchy.
261
262
263   /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
264   /// fold the result.  If not, this returns null.
265   Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
266                          const DataLayout *TD = nullptr,
267                          const TargetLibraryInfo *TLI = nullptr,
268                          const DominatorTree *DT = nullptr,
269                          AssumptionCache *AC = nullptr,
270                          const Instruction *CxtI = nullptr);
271
272   /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
273   /// fold the result.  If not, this returns null.
274   Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
275                        const DataLayout *TD = nullptr,
276                        const TargetLibraryInfo *TLI = nullptr,
277                        const DominatorTree *DT = nullptr,
278                        AssumptionCache *AC = nullptr,
279                        const Instruction *CxtI = nullptr);
280   /// SimplifyFPBinOp - Given operands for a BinaryOperator, see if we can
281   /// fold the result.  If not, this returns null.
282   /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
283   /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
284   Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
285                          const FastMathFlags &FMF,
286                          const DataLayout *TD = nullptr,
287                          const TargetLibraryInfo *TLI = nullptr,
288                          const DominatorTree *DT = nullptr,
289                          AssumptionCache *AC = nullptr,
290                          const Instruction *CxtI = nullptr);
291
292   /// \brief Given a function and iterators over arguments, see if we can fold
293   /// the result.
294   ///
295   /// If this call could not be simplified returns null.
296   Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
297                       User::op_iterator ArgEnd, const DataLayout *TD = nullptr,
298                       const TargetLibraryInfo *TLI = nullptr,
299                       const DominatorTree *DT = nullptr,
300                       AssumptionCache *AC = nullptr,
301                       const Instruction *CxtI = nullptr);
302
303   /// \brief Given a function and set of arguments, see if we can fold the
304   /// result.
305   ///
306   /// If this call could not be simplified returns null.
307   Value *SimplifyCall(Value *V, ArrayRef<Value *> Args,
308                       const DataLayout *TD = nullptr,
309                       const TargetLibraryInfo *TLI = nullptr,
310                       const DominatorTree *DT = nullptr,
311                       AssumptionCache *AC = nullptr,
312                       const Instruction *CxtI = nullptr);
313
314   /// SimplifyInstruction - See if we can compute a simplified version of this
315   /// instruction.  If not, this returns null.
316   Value *SimplifyInstruction(Instruction *I, const DataLayout *TD = nullptr,
317                              const TargetLibraryInfo *TLI = nullptr,
318                              const DominatorTree *DT = nullptr,
319                              AssumptionCache *AC = nullptr);
320
321   /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses
322   /// recursively.
323   ///
324   /// This first performs a normal RAUW of I with SimpleV. It then recursively
325   /// attempts to simplify those users updated by the operation. The 'I'
326   /// instruction must not be equal to the simplified value 'SimpleV'.
327   ///
328   /// The function returns true if any simplifications were performed.
329   bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
330                                      const DataLayout *TD = nullptr,
331                                      const TargetLibraryInfo *TLI = nullptr,
332                                      const DominatorTree *DT = nullptr,
333                                      AssumptionCache *AC = nullptr);
334
335   /// \brief Recursively attempt to simplify an instruction.
336   ///
337   /// This routine uses SimplifyInstruction to simplify 'I', and if successful
338   /// replaces uses of 'I' with the simplified value. It then recurses on each
339   /// of the users impacted. It returns true if any simplifications were
340   /// performed.
341   bool recursivelySimplifyInstruction(Instruction *I,
342                                       const DataLayout *TD = nullptr,
343                                       const TargetLibraryInfo *TLI = nullptr,
344                                       const DominatorTree *DT = nullptr,
345                                       AssumptionCache *AC = nullptr);
346 } // end namespace llvm
347
348 #endif
349