InstructionSimplify.h revision 0aa85eb231dc76bcabcd35f6dc9a50536f607df3
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  class DominatorTree;
24  class Instruction;
25  class Value;
26  class TargetData;
27  class TargetLibraryInfo;
28  template<typename T>
29  class ArrayRef;
30
31  /// SimplifyAddInst - Given operands for an Add, see if we can
32  /// fold the result.  If not, this returns null.
33  Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
34                         const TargetData *TD = 0,
35                         const TargetLibraryInfo *TLI = 0,
36                         const DominatorTree *DT = 0);
37
38  /// SimplifySubInst - Given operands for a Sub, see if we can
39  /// fold the result.  If not, this returns null.
40  Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
41                         const TargetData *TD = 0,
42                         const TargetLibraryInfo *TLI = 0,
43                         const DominatorTree *DT = 0);
44
45  /// SimplifyMulInst - Given operands for a Mul, see if we can
46  /// fold the result.  If not, this returns null.
47  Value *SimplifyMulInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
48                         const TargetLibraryInfo *TLI = 0,
49                         const DominatorTree *DT = 0);
50
51  /// SimplifySDivInst - Given operands for an SDiv, see if we can
52  /// fold the result.  If not, this returns null.
53  Value *SimplifySDivInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
54                          const TargetLibraryInfo *TLI = 0,
55                          const DominatorTree *DT = 0);
56
57  /// SimplifyUDivInst - Given operands for a UDiv, see if we can
58  /// fold the result.  If not, this returns null.
59  Value *SimplifyUDivInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
60                          const TargetLibraryInfo *TLI = 0,
61                          const DominatorTree *DT = 0);
62
63  /// SimplifyFDivInst - Given operands for an FDiv, see if we can
64  /// fold the result.  If not, this returns null.
65  Value *SimplifyFDivInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
66                          const TargetLibraryInfo *TLI = 0,
67                          const DominatorTree *DT = 0);
68
69  /// SimplifySRemInst - Given operands for an SRem, see if we can
70  /// fold the result.  If not, this returns null.
71  Value *SimplifySRemInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
72                          const TargetLibraryInfo *TLI = 0,
73                          const DominatorTree *DT = 0);
74
75  /// SimplifyURemInst - Given operands for a URem, see if we can
76  /// fold the result.  If not, this returns null.
77  Value *SimplifyURemInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
78                          const TargetLibraryInfo *TLI = 0,
79                          const DominatorTree *DT = 0);
80
81  /// SimplifyFRemInst - Given operands for an FRem, see if we can
82  /// fold the result.  If not, this returns null.
83  Value *SimplifyFRemInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
84                          const TargetLibraryInfo *TLI = 0,
85                          const DominatorTree *DT = 0);
86
87  /// SimplifyShlInst - Given operands for a Shl, see if we can
88  /// fold the result.  If not, this returns null.
89  Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
90                         const TargetData *TD = 0,
91                         const TargetLibraryInfo *TLI = 0,
92                         const DominatorTree *DT = 0);
93
94  /// SimplifyLShrInst - Given operands for a LShr, see if we can
95  /// fold the result.  If not, this returns null.
96  Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
97                          const TargetData *TD = 0,
98                          const TargetLibraryInfo *TLI = 0,
99                          const DominatorTree *DT = 0);
100
101  /// SimplifyAShrInst - Given operands for a AShr, see if we can
102  /// fold the result.  If not, this returns null.
103  Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
104                          const TargetData *TD = 0,
105                          const TargetLibraryInfo *TLI = 0,
106                          const DominatorTree *DT = 0);
107
108  /// SimplifyAndInst - Given operands for an And, see if we can
109  /// fold the result.  If not, this returns null.
110  Value *SimplifyAndInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
111                         const TargetLibraryInfo *TLI = 0,
112                         const DominatorTree *DT = 0);
113
114  /// SimplifyOrInst - Given operands for an Or, see if we can
115  /// fold the result.  If not, this returns null.
116  Value *SimplifyOrInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
117                        const TargetLibraryInfo *TLI = 0,
118                        const DominatorTree *DT = 0);
119
120  /// SimplifyXorInst - Given operands for a Xor, see if we can
121  /// fold the result.  If not, this returns null.
122  Value *SimplifyXorInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
123                         const TargetLibraryInfo *TLI = 0,
124                         const DominatorTree *DT = 0);
125
126  /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
127  /// fold the result.  If not, this returns null.
128  Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
129                          const TargetData *TD = 0,
130                          const TargetLibraryInfo *TLI = 0,
131                          const DominatorTree *DT = 0);
132
133  /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
134  /// fold the result.  If not, this returns null.
135  Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
136                          const TargetData *TD = 0,
137                          const TargetLibraryInfo *TLI = 0,
138                          const DominatorTree *DT = 0);
139
140  /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
141  /// the result.  If not, this returns null.
142  Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
143                            const TargetData *TD = 0,
144                            const TargetLibraryInfo *TLI = 0,
145                            const DominatorTree *DT = 0);
146
147  /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
148  /// fold the result.  If not, this returns null.
149  Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const TargetData *TD = 0,
150                         const TargetLibraryInfo *TLI = 0,
151                         const DominatorTree *DT = 0);
152
153  /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
154  /// can fold the result.  If not, this returns null.
155  Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
156                                 ArrayRef<unsigned> Idxs,
157                                 const TargetData *TD = 0,
158                                 const TargetLibraryInfo *TLI = 0,
159                                 const DominatorTree *DT = 0);
160
161  //=== Helper functions for higher up the class hierarchy.
162
163
164  /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
165  /// fold the result.  If not, this returns null.
166  Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
167                         const TargetData *TD = 0,
168                         const TargetLibraryInfo *TLI = 0,
169                         const DominatorTree *DT = 0);
170
171  /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
172  /// fold the result.  If not, this returns null.
173  Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
174                       const TargetData *TD = 0,
175                       const TargetLibraryInfo *TLI = 0,
176                       const DominatorTree *DT = 0);
177
178  /// SimplifyInstruction - See if we can compute a simplified version of this
179  /// instruction.  If not, this returns null.
180  Value *SimplifyInstruction(Instruction *I, const TargetData *TD = 0,
181                             const TargetLibraryInfo *TLI = 0,
182                             const DominatorTree *DT = 0);
183
184
185  /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
186  /// delete the From instruction.  In addition to a basic RAUW, this does a
187  /// recursive simplification of the updated instructions.  This catches
188  /// things where one simplification exposes other opportunities.  This only
189  /// simplifies and deletes scalar operations, it does not change the CFG.
190  ///
191  void ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
192                                 const TargetData *TD = 0,
193                                 const TargetLibraryInfo *TLI = 0,
194                                 const DominatorTree *DT = 0);
195} // end namespace llvm
196
197#endif
198
199