1 //===-- InstructionSimplify.h - Fold instructions into simpler forms ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
20 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
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);
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);
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);
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);
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);
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);
58 /// SimplifySRemInst - Given operands for an SRem, see if we can
59 /// fold the result. If not, this returns null.
60 Value *SimplifySRemInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
61 const DominatorTree *DT = 0);
63 /// SimplifyURemInst - Given operands for a URem, see if we can
64 /// fold the result. If not, this returns null.
65 Value *SimplifyURemInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
66 const DominatorTree *DT = 0);
68 /// SimplifyFRemInst - Given operands for an FRem, see if we can
69 /// fold the result. If not, this returns null.
70 Value *SimplifyFRemInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
71 const DominatorTree *DT = 0);
73 /// SimplifyShlInst - Given operands for a Shl, see if we can
74 /// fold the result. If not, this returns null.
75 Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
76 const TargetData *TD = 0, const DominatorTree *DT = 0);
78 /// SimplifyLShrInst - Given operands for a LShr, see if we can
79 /// fold the result. If not, this returns null.
80 Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
81 const TargetData *TD = 0, const DominatorTree *DT=0);
83 /// SimplifyAShrInst - Given operands for a AShr, see if we can
84 /// fold the result. If not, this returns null.
85 Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
86 const TargetData *TD = 0,
87 const DominatorTree *DT = 0);
89 /// SimplifyAndInst - Given operands for an And, see if we can
90 /// fold the result. If not, this returns null.
91 Value *SimplifyAndInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
92 const DominatorTree *DT = 0);
94 /// SimplifyOrInst - Given operands for an Or, see if we can
95 /// fold the result. If not, this returns null.
96 Value *SimplifyOrInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
97 const DominatorTree *DT = 0);
99 /// SimplifyXorInst - Given operands for a Xor, see if we can
100 /// fold the result. If not, this returns null.
101 Value *SimplifyXorInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
102 const DominatorTree *DT = 0);
104 /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
105 /// fold the result. If not, this returns null.
106 Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
107 const TargetData *TD = 0,
108 const DominatorTree *DT = 0);
110 /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
111 /// fold the result. If not, this returns null.
112 Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
113 const TargetData *TD = 0,
114 const DominatorTree *DT = 0);
116 /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
117 /// the result. If not, this returns null.
118 Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
119 const TargetData *TD = 0,
120 const DominatorTree *DT = 0);
122 /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
123 /// fold the result. If not, this returns null.
124 Value *SimplifyGEPInst(Value * const *Ops, unsigned NumOps,
125 const TargetData *TD = 0, const DominatorTree *DT = 0);
127 //=== Helper functions for higher up the class hierarchy.
130 /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
131 /// fold the result. If not, this returns null.
132 Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
133 const TargetData *TD = 0, const DominatorTree *DT = 0);
135 /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
136 /// fold the result. If not, this returns null.
137 Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
138 const TargetData *TD = 0, const DominatorTree *DT = 0);
140 /// SimplifyInstruction - See if we can compute a simplified version of this
141 /// instruction. If not, this returns null.
142 Value *SimplifyInstruction(Instruction *I, const TargetData *TD = 0,
143 const DominatorTree *DT = 0);
146 /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
147 /// delete the From instruction. In addition to a basic RAUW, this does a
148 /// recursive simplification of the updated instructions. This catches
149 /// things where one simplification exposes other opportunities. This only
150 /// simplifies and deletes scalar operations, it does not change the CFG.
152 void ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
153 const TargetData *TD = 0,
154 const DominatorTree *DT = 0);
155 } // end namespace llvm