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 // These routines implicitly resolve undef uses. The easiest way to be safe when
18 // using these routines to obtain simplified values for existing instructions is
19 // to always replace all uses of the instructions with the resulting simplified
20 // values. This will prevent other code from seeing the same undef uses and
21 // resolving them to different values.
23 //===----------------------------------------------------------------------===//
25 #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
26 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
28 #include "llvm/IR/User.h"
37 class TargetLibraryInfo;
41 /// SimplifyAddInst - Given operands for an Add, see if we can
42 /// fold the result. If not, this returns null.
43 Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
44 const DataLayout *TD = 0,
45 const TargetLibraryInfo *TLI = 0,
46 const DominatorTree *DT = 0);
48 /// SimplifySubInst - Given operands for a Sub, see if we can
49 /// fold the result. If not, this returns null.
50 Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
51 const DataLayout *TD = 0,
52 const TargetLibraryInfo *TLI = 0,
53 const DominatorTree *DT = 0);
55 /// Given operands for an FAdd, see if we can fold the result. If not, this
57 Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
58 const DataLayout *TD = 0,
59 const TargetLibraryInfo *TLI = 0,
60 const DominatorTree *DT = 0);
62 /// Given operands for an FSub, see if we can fold the result. If not, this
64 Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
65 const DataLayout *TD = 0,
66 const TargetLibraryInfo *TLI = 0,
67 const DominatorTree *DT = 0);
69 /// Given operands for an FMul, see if we can fold the result. If not, this
71 Value *SimplifyFMulInst(Value *LHS, Value *RHS,
73 const DataLayout *TD = 0,
74 const TargetLibraryInfo *TLI = 0,
75 const DominatorTree *DT = 0);
77 /// SimplifyMulInst - Given operands for a Mul, see if we can
78 /// fold the result. If not, this returns null.
79 Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
80 const TargetLibraryInfo *TLI = 0,
81 const DominatorTree *DT = 0);
83 /// SimplifySDivInst - Given operands for an SDiv, see if we can
84 /// fold the result. If not, this returns null.
85 Value *SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
86 const TargetLibraryInfo *TLI = 0,
87 const DominatorTree *DT = 0);
89 /// SimplifyUDivInst - Given operands for a UDiv, see if we can
90 /// fold the result. If not, this returns null.
91 Value *SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
92 const TargetLibraryInfo *TLI = 0,
93 const DominatorTree *DT = 0);
95 /// SimplifyFDivInst - Given operands for an FDiv, see if we can
96 /// fold the result. If not, this returns null.
97 Value *SimplifyFDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
98 const TargetLibraryInfo *TLI = 0,
99 const DominatorTree *DT = 0);
101 /// SimplifySRemInst - Given operands for an SRem, see if we can
102 /// fold the result. If not, this returns null.
103 Value *SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
104 const TargetLibraryInfo *TLI = 0,
105 const DominatorTree *DT = 0);
107 /// SimplifyURemInst - Given operands for a URem, see if we can
108 /// fold the result. If not, this returns null.
109 Value *SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
110 const TargetLibraryInfo *TLI = 0,
111 const DominatorTree *DT = 0);
113 /// SimplifyFRemInst - Given operands for an FRem, see if we can
114 /// fold the result. If not, this returns null.
115 Value *SimplifyFRemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
116 const TargetLibraryInfo *TLI = 0,
117 const DominatorTree *DT = 0);
119 /// SimplifyShlInst - Given operands for a Shl, see if we can
120 /// fold the result. If not, this returns null.
121 Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
122 const DataLayout *TD = 0,
123 const TargetLibraryInfo *TLI = 0,
124 const DominatorTree *DT = 0);
126 /// SimplifyLShrInst - Given operands for a LShr, see if we can
127 /// fold the result. If not, this returns null.
128 Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
129 const DataLayout *TD = 0,
130 const TargetLibraryInfo *TLI = 0,
131 const DominatorTree *DT = 0);
133 /// SimplifyAShrInst - Given operands for a AShr, see if we can
134 /// fold the result. If not, this returns null.
135 Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
136 const DataLayout *TD = 0,
137 const TargetLibraryInfo *TLI = 0,
138 const DominatorTree *DT = 0);
140 /// SimplifyAndInst - Given operands for an And, see if we can
141 /// fold the result. If not, this returns null.
142 Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
143 const TargetLibraryInfo *TLI = 0,
144 const DominatorTree *DT = 0);
146 /// SimplifyOrInst - Given operands for an Or, see if we can
147 /// fold the result. If not, this returns null.
148 Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
149 const TargetLibraryInfo *TLI = 0,
150 const DominatorTree *DT = 0);
152 /// SimplifyXorInst - Given operands for a Xor, see if we can
153 /// fold the result. If not, this returns null.
154 Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
155 const TargetLibraryInfo *TLI = 0,
156 const DominatorTree *DT = 0);
158 /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
159 /// fold the result. If not, this returns null.
160 Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
161 const DataLayout *TD = 0,
162 const TargetLibraryInfo *TLI = 0,
163 const DominatorTree *DT = 0);
165 /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
166 /// fold the result. If not, this returns null.
167 Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
168 const DataLayout *TD = 0,
169 const TargetLibraryInfo *TLI = 0,
170 const DominatorTree *DT = 0);
172 /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
173 /// the result. If not, this returns null.
174 Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
175 const DataLayout *TD = 0,
176 const TargetLibraryInfo *TLI = 0,
177 const DominatorTree *DT = 0);
179 /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
180 /// fold the result. If not, this returns null.
181 Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *TD = 0,
182 const TargetLibraryInfo *TLI = 0,
183 const DominatorTree *DT = 0);
185 /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
186 /// can fold the result. If not, this returns null.
187 Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
188 ArrayRef<unsigned> Idxs,
189 const DataLayout *TD = 0,
190 const TargetLibraryInfo *TLI = 0,
191 const DominatorTree *DT = 0);
193 /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold
194 /// the result. If not, this returns null.
195 Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD = 0,
196 const TargetLibraryInfo *TLI = 0,
197 const DominatorTree *DT = 0);
199 //=== Helper functions for higher up the class hierarchy.
202 /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
203 /// fold the result. If not, this returns null.
204 Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
205 const DataLayout *TD = 0,
206 const TargetLibraryInfo *TLI = 0,
207 const DominatorTree *DT = 0);
209 /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
210 /// fold the result. If not, this returns null.
211 Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
212 const DataLayout *TD = 0,
213 const TargetLibraryInfo *TLI = 0,
214 const DominatorTree *DT = 0);
216 /// \brief Given a function and iterators over arguments, see if we can fold
219 /// If this call could not be simplified returns null.
220 Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
221 User::op_iterator ArgEnd, const DataLayout *TD = 0,
222 const TargetLibraryInfo *TLI = 0,
223 const DominatorTree *DT = 0);
225 /// \brief Given a function and set of arguments, see if we can fold the
228 /// If this call could not be simplified returns null.
229 Value *SimplifyCall(Value *V, ArrayRef<Value *> Args,
230 const DataLayout *TD = 0,
231 const TargetLibraryInfo *TLI = 0,
232 const DominatorTree *DT = 0);
234 /// SimplifyInstruction - See if we can compute a simplified version of this
235 /// instruction. If not, this returns null.
236 Value *SimplifyInstruction(Instruction *I, const DataLayout *TD = 0,
237 const TargetLibraryInfo *TLI = 0,
238 const DominatorTree *DT = 0);
241 /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses
244 /// This first performs a normal RAUW of I with SimpleV. It then recursively
245 /// attempts to simplify those users updated by the operation. The 'I'
246 /// instruction must not be equal to the simplified value 'SimpleV'.
248 /// The function returns true if any simplifications were performed.
249 bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
250 const DataLayout *TD = 0,
251 const TargetLibraryInfo *TLI = 0,
252 const DominatorTree *DT = 0);
254 /// \brief Recursively attempt to simplify an instruction.
256 /// This routine uses SimplifyInstruction to simplify 'I', and if successful
257 /// replaces uses of 'I' with the simplified value. It then recurses on each
258 /// of the users impacted. It returns true if any simplifications were
260 bool recursivelySimplifyInstruction(Instruction *I,
261 const DataLayout *TD = 0,
262 const TargetLibraryInfo *TLI = 0,
263 const DominatorTree *DT = 0);
264 } // end namespace llvm