Added a slew of SimplifyInstruction floating-point optimizations, many of which take...
[oota-llvm.git] / include / llvm / Analysis / InstructionSimplify.h
1 //===-- InstructionSimplify.h - Fold instructions into simpler forms ------===//
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 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
20 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
21
22 namespace llvm {
23   template<typename T>
24   class ArrayRef;
25   class DominatorTree;
26   class Instruction;
27   class DataLayout;
28   class FastMathFlags;
29   class TargetLibraryInfo;
30   class Type;
31   class Value;
32
33   /// SimplifyAddInst - Given operands for an Add, see if we can
34   /// fold the result.  If not, this returns null.
35   Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
36                          const DataLayout *TD = 0,
37                          const TargetLibraryInfo *TLI = 0,
38                          const DominatorTree *DT = 0);
39
40   /// SimplifySubInst - Given operands for a Sub, see if we can
41   /// fold the result.  If not, this returns null.
42   Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
43                          const DataLayout *TD = 0,
44                          const TargetLibraryInfo *TLI = 0,
45                          const DominatorTree *DT = 0);
46
47   /// Given operands for an FAdd, see if we can fold the result.  If not, this
48   /// returns null.
49   Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
50                          const DataLayout *TD = 0,
51                          const TargetLibraryInfo *TLI = 0,
52                          const DominatorTree *DT = 0);
53
54   /// Given operands for an FSub, see if we can fold the result.  If not, this
55   /// returns null.
56   Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
57                          const DataLayout *TD = 0,
58                          const TargetLibraryInfo *TLI = 0,
59                          const DominatorTree *DT = 0);
60
61   /// Given operands for an FMul, see if we can fold the result.  If not, this
62   /// returns null.
63   Value *SimplifyFMulInst(Value *LHS, Value *RHS,
64                           FastMathFlags FMF,
65                           const DataLayout *TD = 0,
66                           const TargetLibraryInfo *TLI = 0,
67                           const DominatorTree *DT = 0);
68
69   /// SimplifyMulInst - Given operands for a Mul, see if we can
70   /// fold the result.  If not, this returns null.
71   Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
72                          const TargetLibraryInfo *TLI = 0,
73                          const DominatorTree *DT = 0);
74
75   /// SimplifySDivInst - Given operands for an SDiv, see if we can
76   /// fold the result.  If not, this returns null.
77   Value *SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
78                           const TargetLibraryInfo *TLI = 0,
79                           const DominatorTree *DT = 0);
80
81   /// SimplifyUDivInst - Given operands for a UDiv, see if we can
82   /// fold the result.  If not, this returns null.
83   Value *SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
84                           const TargetLibraryInfo *TLI = 0,
85                           const DominatorTree *DT = 0);
86
87   /// SimplifyFDivInst - Given operands for an FDiv, see if we can
88   /// fold the result.  If not, this returns null.
89   Value *SimplifyFDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
90                           const TargetLibraryInfo *TLI = 0,
91                           const DominatorTree *DT = 0);
92
93   /// SimplifySRemInst - Given operands for an SRem, see if we can
94   /// fold the result.  If not, this returns null.
95   Value *SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
96                           const TargetLibraryInfo *TLI = 0,
97                           const DominatorTree *DT = 0);
98
99   /// SimplifyURemInst - Given operands for a URem, see if we can
100   /// fold the result.  If not, this returns null.
101   Value *SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
102                           const TargetLibraryInfo *TLI = 0,
103                           const DominatorTree *DT = 0);
104
105   /// SimplifyFRemInst - Given operands for an FRem, see if we can
106   /// fold the result.  If not, this returns null.
107   Value *SimplifyFRemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
108                           const TargetLibraryInfo *TLI = 0,
109                           const DominatorTree *DT = 0);
110
111   /// SimplifyShlInst - Given operands for a Shl, see if we can
112   /// fold the result.  If not, this returns null.
113   Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
114                          const DataLayout *TD = 0,
115                          const TargetLibraryInfo *TLI = 0,
116                          const DominatorTree *DT = 0);
117
118   /// SimplifyLShrInst - Given operands for a LShr, see if we can
119   /// fold the result.  If not, this returns null.
120   Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
121                           const DataLayout *TD = 0,
122                           const TargetLibraryInfo *TLI = 0,
123                           const DominatorTree *DT = 0);
124
125   /// SimplifyAShrInst - Given operands for a AShr, see if we can
126   /// fold the result.  If not, this returns null.
127   Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
128                           const DataLayout *TD = 0,
129                           const TargetLibraryInfo *TLI = 0,
130                           const DominatorTree *DT = 0);
131
132   /// SimplifyAndInst - Given operands for an And, see if we can
133   /// fold the result.  If not, this returns null.
134   Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
135                          const TargetLibraryInfo *TLI = 0,
136                          const DominatorTree *DT = 0);
137
138   /// SimplifyOrInst - Given operands for an Or, see if we can
139   /// fold the result.  If not, this returns null.
140   Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
141                         const TargetLibraryInfo *TLI = 0,
142                         const DominatorTree *DT = 0);
143
144   /// SimplifyXorInst - Given operands for a Xor, see if we can
145   /// fold the result.  If not, this returns null.
146   Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
147                          const TargetLibraryInfo *TLI = 0,
148                          const DominatorTree *DT = 0);
149
150   /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
151   /// fold the result.  If not, this returns null.
152   Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
153                           const DataLayout *TD = 0,
154                           const TargetLibraryInfo *TLI = 0,
155                           const DominatorTree *DT = 0);
156
157   /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
158   /// fold the result.  If not, this returns null.
159   Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
160                           const DataLayout *TD = 0,
161                           const TargetLibraryInfo *TLI = 0,
162                           const DominatorTree *DT = 0);
163
164   /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
165   /// the result.  If not, this returns null.
166   Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
167                             const DataLayout *TD = 0,
168                             const TargetLibraryInfo *TLI = 0,
169                             const DominatorTree *DT = 0);
170
171   /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
172   /// fold the result.  If not, this returns null.
173   Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *TD = 0,
174                          const TargetLibraryInfo *TLI = 0,
175                          const DominatorTree *DT = 0);
176
177   /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
178   /// can fold the result.  If not, this returns null.
179   Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
180                                  ArrayRef<unsigned> Idxs,
181                                  const DataLayout *TD = 0,
182                                  const TargetLibraryInfo *TLI = 0,
183                                  const DominatorTree *DT = 0);
184
185   /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold
186   /// the result.  If not, this returns null.
187   Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD = 0,
188                            const TargetLibraryInfo *TLI = 0,
189                            const DominatorTree *DT = 0);
190
191   //=== Helper functions for higher up the class hierarchy.
192
193
194   /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
195   /// fold the result.  If not, this returns null.
196   Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
197                          const DataLayout *TD = 0,
198                          const TargetLibraryInfo *TLI = 0,
199                          const DominatorTree *DT = 0);
200
201   /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
202   /// fold the result.  If not, this returns null.
203   Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
204                        const DataLayout *TD = 0,
205                        const TargetLibraryInfo *TLI = 0,
206                        const DominatorTree *DT = 0);
207
208   /// SimplifyInstruction - See if we can compute a simplified version of this
209   /// instruction.  If not, this returns null.
210   Value *SimplifyInstruction(Instruction *I, const DataLayout *TD = 0,
211                              const TargetLibraryInfo *TLI = 0,
212                              const DominatorTree *DT = 0);
213
214
215   /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses
216   /// recursively.
217   ///
218   /// This first performs a normal RAUW of I with SimpleV. It then recursively
219   /// attempts to simplify those users updated by the operation. The 'I'
220   /// instruction must not be equal to the simplified value 'SimpleV'.
221   ///
222   /// The function returns true if any simplifications were performed.
223   bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
224                                      const DataLayout *TD = 0,
225                                      const TargetLibraryInfo *TLI = 0,
226                                      const DominatorTree *DT = 0);
227
228   /// \brief Recursively attempt to simplify an instruction.
229   ///
230   /// This routine uses SimplifyInstruction to simplify 'I', and if successful
231   /// replaces uses of 'I' with the simplified value. It then recurses on each
232   /// of the users impacted. It returns true if any simplifications were
233   /// performed.
234   bool recursivelySimplifyInstruction(Instruction *I,
235                                       const DataLayout *TD = 0,
236                                       const TargetLibraryInfo *TLI = 0,
237                                       const DominatorTree *DT = 0);
238 } // end namespace llvm
239
240 #endif
241