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