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