d39432d072bab704c35473ab84db8320f8a865ac
[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   /// SimplifyShlInst - Given operands for a Shl, see if we can
54   /// fold the result.  If not, this returns null.
55   Value *SimplifyShlInst(Value *Op0, Value *Op1, const TargetData *TD = 0,
56                          const DominatorTree *DT = 0);
57
58   /// SimplifyLShrInst - Given operands for a LShr, see if we can
59   /// fold the result.  If not, this returns null.
60   Value *SimplifyLShrInst(Value *Op0, Value *Op1, const TargetData *TD = 0,
61                           const DominatorTree *DT = 0);
62
63   /// SimplifyAShrInst - Given operands for a AShr, see if we can
64   /// fold the result.  If not, this returns null.
65   Value *SimplifyAShrInst(Value *Op0, Value *Op1, const TargetData *TD = 0,
66                           const DominatorTree *DT = 0);
67
68   /// SimplifyAndInst - Given operands for an And, see if we can
69   /// fold the result.  If not, this returns null.
70   Value *SimplifyAndInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
71                          const DominatorTree *DT = 0);
72
73   /// SimplifyOrInst - Given operands for an Or, see if we can
74   /// fold the result.  If not, this returns null.
75   Value *SimplifyOrInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
76                         const DominatorTree *DT = 0);
77
78   /// SimplifyXorInst - Given operands for a Xor, see if we can
79   /// fold the result.  If not, this returns null.
80   Value *SimplifyXorInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
81                          const DominatorTree *DT = 0);
82
83   /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
84   /// fold the result.  If not, this returns null.
85   Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
86                           const TargetData *TD = 0,
87                           const DominatorTree *DT = 0);
88
89   /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
90   /// fold the result.  If not, this returns null.
91   Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
92                           const TargetData *TD = 0,
93                           const DominatorTree *DT = 0);
94
95   /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
96   /// the result.  If not, this returns null.
97   Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
98                             const TargetData *TD = 0,
99                             const DominatorTree *DT = 0);
100
101   /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
102   /// fold the result.  If not, this returns null.
103   Value *SimplifyGEPInst(Value * const *Ops, unsigned NumOps,
104                          const TargetData *TD = 0, const DominatorTree *DT = 0);
105
106   //=== Helper functions for higher up the class hierarchy.
107
108
109   /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
110   /// fold the result.  If not, this returns null.
111   Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
112                          const TargetData *TD = 0, const DominatorTree *DT = 0);
113
114   /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
115   /// fold the result.  If not, this returns null.
116   Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
117                        const TargetData *TD = 0, const DominatorTree *DT = 0);
118
119   /// SimplifyInstruction - See if we can compute a simplified version of this
120   /// instruction.  If not, this returns null.
121   Value *SimplifyInstruction(Instruction *I, const TargetData *TD = 0,
122                              const DominatorTree *DT = 0);
123
124
125   /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
126   /// delete the From instruction.  In addition to a basic RAUW, this does a
127   /// recursive simplification of the updated instructions.  This catches
128   /// things where one simplification exposes other opportunities.  This only
129   /// simplifies and deletes scalar operations, it does not change the CFG.
130   ///
131   void ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
132                                  const TargetData *TD = 0,
133                                  const DominatorTree *DT = 0);
134 } // end namespace llvm
135
136 #endif
137