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