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