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