InstructionSimplify.h revision 618c1dbd293d15ee19f61b1156ab8086ad28311a
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 DominatorTree *DT = 0);
145
146  /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
147  /// fold the result.  If not, this returns null.
148  Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const TargetData *TD = 0,
149                         const DominatorTree *DT = 0);
150
151  /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
152  /// can fold the result.  If not, this returns null.
153  Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
154                                 ArrayRef<unsigned> Idxs,
155                                 const TargetData *TD = 0,
156                                 const DominatorTree *DT = 0);
157
158  //=== Helper functions for higher up the class hierarchy.
159
160
161  /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
162  /// fold the result.  If not, this returns null.
163  Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
164                         const TargetData *TD = 0,
165                         const TargetLibraryInfo *TLI = 0,
166                         const DominatorTree *DT = 0);
167
168  /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
169  /// fold the result.  If not, this returns null.
170  Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
171                       const TargetData *TD = 0,
172                       const TargetLibraryInfo *TLI = 0,
173                       const DominatorTree *DT = 0);
174
175  /// SimplifyInstruction - See if we can compute a simplified version of this
176  /// instruction.  If not, this returns null.
177  Value *SimplifyInstruction(Instruction *I, const TargetData *TD = 0,
178                             const TargetLibraryInfo *TLI = 0,
179                             const DominatorTree *DT = 0);
180
181
182  /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
183  /// delete the From instruction.  In addition to a basic RAUW, this does a
184  /// recursive simplification of the updated instructions.  This catches
185  /// things where one simplification exposes other opportunities.  This only
186  /// simplifies and deletes scalar operations, it does not change the CFG.
187  ///
188  void ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
189                                 const TargetData *TD = 0,
190                                 const TargetLibraryInfo *TLI = 0,
191                                 const DominatorTree *DT = 0);
192} // end namespace llvm
193
194#endif
195
196