blob: 4dd8a66a5d6b4295a8cc228a4414599eea76c7fc [file] [log] [blame]
Chris Lattner9f3c25a2009-11-09 22:57:59 +00001//===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
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 implements routines for folding instructions into simpler forms
11// that do not require creating new instructions. For example, this does
12// constant folding, and can handle identities like (X&0)->0.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Analysis/InstructionSimplify.h"
17#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner40d8c282009-11-10 22:26:15 +000018#include "llvm/Support/ValueHandle.h"
Chris Lattner9f3c25a2009-11-09 22:57:59 +000019#include "llvm/Instructions.h"
Chris Lattnerd06094f2009-11-10 00:55:12 +000020#include "llvm/Support/PatternMatch.h"
Chris Lattner9f3c25a2009-11-09 22:57:59 +000021using namespace llvm;
Chris Lattnerd06094f2009-11-10 00:55:12 +000022using namespace llvm::PatternMatch;
Chris Lattner9f3c25a2009-11-09 22:57:59 +000023
Chris Lattner8aee8ef2009-11-27 17:42:22 +000024/// SimplifyAddInst - Given operands for an Add, see if we can
25/// fold the result. If not, this returns null.
26Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
27 const TargetData *TD) {
28 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
29 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
30 Constant *Ops[] = { CLHS, CRHS };
31 return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(),
32 Ops, 2, TD);
33 }
34
35 // Canonicalize the constant to the RHS.
36 std::swap(Op0, Op1);
37 }
38
39 if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
40 // X + undef -> undef
41 if (isa<UndefValue>(Op1C))
42 return Op1C;
43
44 // X + 0 --> X
45 if (Op1C->isNullValue())
46 return Op0;
47 }
48
49 // FIXME: Could pull several more out of instcombine.
50 return 0;
51}
52
Chris Lattnerd06094f2009-11-10 00:55:12 +000053/// SimplifyAndInst - Given operands for an And, see if we can
Chris Lattner9f3c25a2009-11-09 22:57:59 +000054/// fold the result. If not, this returns null.
Chris Lattner8aee8ef2009-11-27 17:42:22 +000055Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD) {
Chris Lattnerd06094f2009-11-10 00:55:12 +000056 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
57 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
58 Constant *Ops[] = { CLHS, CRHS };
59 return ConstantFoldInstOperands(Instruction::And, CLHS->getType(),
60 Ops, 2, TD);
61 }
62
63 // Canonicalize the constant to the RHS.
64 std::swap(Op0, Op1);
65 }
66
67 // X & undef -> 0
68 if (isa<UndefValue>(Op1))
69 return Constant::getNullValue(Op0->getType());
70
71 // X & X = X
72 if (Op0 == Op1)
73 return Op0;
74
75 // X & <0,0> = <0,0>
76 if (isa<ConstantAggregateZero>(Op1))
77 return Op1;
78
79 // X & <-1,-1> = X
80 if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1))
81 if (CP->isAllOnesValue())
82 return Op0;
83
84 if (ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1)) {
85 // X & 0 = 0
86 if (Op1CI->isZero())
87 return Op1CI;
88 // X & -1 = X
89 if (Op1CI->isAllOnesValue())
90 return Op0;
91 }
92
93 // A & ~A = ~A & A = 0
94 Value *A, *B;
Chris Lattner70ce6d02009-11-10 02:04:54 +000095 if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
96 (match(Op1, m_Not(m_Value(A))) && A == Op0))
Chris Lattnerd06094f2009-11-10 00:55:12 +000097 return Constant::getNullValue(Op0->getType());
98
99 // (A | ?) & A = A
100 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
101 (A == Op1 || B == Op1))
102 return Op1;
103
104 // A & (A | ?) = A
105 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
106 (A == Op0 || B == Op0))
107 return Op0;
108
Benjamin Kramer6844c8e2010-09-10 22:39:55 +0000109 // (A & B) & A -> A & B
110 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
111 (A == Op1 || B == Op1))
112 return Op0;
113
114 // A & (A & B) -> A & B
115 if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
116 (A == Op0 || B == Op0))
117 return Op1;
118
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000119 return 0;
120}
121
Chris Lattnerd06094f2009-11-10 00:55:12 +0000122/// SimplifyOrInst - Given operands for an Or, see if we can
123/// fold the result. If not, this returns null.
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000124Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD) {
Chris Lattnerd06094f2009-11-10 00:55:12 +0000125 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
126 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
127 Constant *Ops[] = { CLHS, CRHS };
128 return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(),
129 Ops, 2, TD);
130 }
131
132 // Canonicalize the constant to the RHS.
133 std::swap(Op0, Op1);
134 }
135
136 // X | undef -> -1
137 if (isa<UndefValue>(Op1))
138 return Constant::getAllOnesValue(Op0->getType());
139
140 // X | X = X
141 if (Op0 == Op1)
142 return Op0;
143
144 // X | <0,0> = X
145 if (isa<ConstantAggregateZero>(Op1))
146 return Op0;
147
148 // X | <-1,-1> = <-1,-1>
149 if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1))
150 if (CP->isAllOnesValue())
151 return Op1;
152
153 if (ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1)) {
154 // X | 0 = X
155 if (Op1CI->isZero())
156 return Op0;
157 // X | -1 = -1
158 if (Op1CI->isAllOnesValue())
159 return Op1CI;
160 }
161
162 // A | ~A = ~A | A = -1
163 Value *A, *B;
Chris Lattner70ce6d02009-11-10 02:04:54 +0000164 if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
165 (match(Op1, m_Not(m_Value(A))) && A == Op0))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000166 return Constant::getAllOnesValue(Op0->getType());
167
168 // (A & ?) | A = A
169 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
170 (A == Op1 || B == Op1))
171 return Op1;
172
173 // A | (A & ?) = A
174 if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
175 (A == Op0 || B == Op0))
176 return Op0;
177
Benjamin Kramer6844c8e2010-09-10 22:39:55 +0000178 // (A | B) | A -> A | B
179 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
180 (A == Op1 || B == Op1))
181 return Op0;
182
183 // A | (A | B) -> A | B
184 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
185 (A == Op0 || B == Op0))
186 return Op1;
187
Chris Lattnerd06094f2009-11-10 00:55:12 +0000188 return 0;
189}
190
191
Chris Lattner210c5d42009-11-09 23:55:12 +0000192static const Type *GetCompareTy(Value *Op) {
193 return CmpInst::makeCmpResultType(Op->getType());
194}
195
Duncan Sands3bbb0cc2010-11-09 17:25:51 +0000196/// ThreadCmpOverSelect - In the case of a comparison with a select instruction,
197/// try to simplify the comparison by seeing whether both branches of the select
198/// result in the same value. Returns the common value if so, otherwise returns
199/// null.
200static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
201 Value *RHS, const TargetData *TD) {
202 // Make sure the select is on the LHS.
203 if (!isa<SelectInst>(LHS)) {
204 std::swap(LHS, RHS);
205 Pred = CmpInst::getSwappedPredicate(Pred);
206 }
207 assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
208 SelectInst *SI = cast<SelectInst>(LHS);
209
210 // Now that we have "cmp select(cond, TV, FV), RHS", analyse it.
211 // Does "cmp TV, RHS" simplify?
212 if (Value *TCmp = SimplifyCmpInst(Pred, SI->getTrueValue(), RHS, TD))
213 // It does! Does "cmp FV, RHS" simplify?
214 if (Value *FCmp = SimplifyCmpInst(Pred, SI->getFalseValue(), RHS, TD))
215 // It does! If they simplified to the same value, then use it as the
216 // result of the original comparison.
217 if (TCmp == FCmp)
218 return TCmp;
219 return 0;
220}
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000221
Chris Lattner9dbb4292009-11-09 23:28:39 +0000222/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
223/// fold the result. If not, this returns null.
224Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
225 const TargetData *TD) {
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000226 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
Chris Lattner9dbb4292009-11-09 23:28:39 +0000227 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000228
Chris Lattnerd06094f2009-11-10 00:55:12 +0000229 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner8f73dea2009-11-09 23:06:58 +0000230 if (Constant *CRHS = dyn_cast<Constant>(RHS))
231 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Chris Lattnerd06094f2009-11-10 00:55:12 +0000232
233 // If we have a constant, make sure it is on the RHS.
234 std::swap(LHS, RHS);
235 Pred = CmpInst::getSwappedPredicate(Pred);
236 }
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000237
Chris Lattner210c5d42009-11-09 23:55:12 +0000238 // ITy - This is the return type of the compare we're considering.
239 const Type *ITy = GetCompareTy(LHS);
240
241 // icmp X, X -> true/false
Chris Lattnerc8e14b32010-03-03 19:46:03 +0000242 // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false
243 // because X could be 0.
244 if (LHS == RHS || isa<UndefValue>(RHS))
Chris Lattner210c5d42009-11-09 23:55:12 +0000245 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
Chris Lattner210c5d42009-11-09 23:55:12 +0000246
247 // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
248 // addresses never equal each other! We already know that Op0 != Op1.
249 if ((isa<GlobalValue>(LHS) || isa<AllocaInst>(LHS) ||
250 isa<ConstantPointerNull>(LHS)) &&
251 (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
252 isa<ConstantPointerNull>(RHS)))
253 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
254
255 // See if we are doing a comparison with a constant.
256 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
257 // If we have an icmp le or icmp ge instruction, turn it into the
258 // appropriate icmp lt or icmp gt instruction. This allows us to rely on
259 // them being folded in the code below.
260 switch (Pred) {
261 default: break;
262 case ICmpInst::ICMP_ULE:
263 if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
264 return ConstantInt::getTrue(CI->getContext());
265 break;
266 case ICmpInst::ICMP_SLE:
267 if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
268 return ConstantInt::getTrue(CI->getContext());
269 break;
270 case ICmpInst::ICMP_UGE:
271 if (CI->isMinValue(false)) // A >=u MIN -> TRUE
272 return ConstantInt::getTrue(CI->getContext());
273 break;
274 case ICmpInst::ICMP_SGE:
275 if (CI->isMinValue(true)) // A >=s MIN -> TRUE
276 return ConstantInt::getTrue(CI->getContext());
277 break;
278 }
Chris Lattner210c5d42009-11-09 23:55:12 +0000279 }
Duncan Sands1ac7c992010-11-07 16:12:23 +0000280
281 // If the comparison is with the result of a select instruction, check whether
282 // comparing with either branch of the select always yields the same value.
Duncan Sands3bbb0cc2010-11-09 17:25:51 +0000283 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
284 if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD))
285 return V;
Duncan Sands1ac7c992010-11-07 16:12:23 +0000286
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000287 return 0;
288}
289
Chris Lattner9dbb4292009-11-09 23:28:39 +0000290/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
291/// fold the result. If not, this returns null.
292Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
293 const TargetData *TD) {
294 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
295 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
296
Chris Lattnerd06094f2009-11-10 00:55:12 +0000297 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner9dbb4292009-11-09 23:28:39 +0000298 if (Constant *CRHS = dyn_cast<Constant>(RHS))
299 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Chris Lattnerd06094f2009-11-10 00:55:12 +0000300
301 // If we have a constant, make sure it is on the RHS.
302 std::swap(LHS, RHS);
303 Pred = CmpInst::getSwappedPredicate(Pred);
304 }
Chris Lattner9dbb4292009-11-09 23:28:39 +0000305
Chris Lattner210c5d42009-11-09 23:55:12 +0000306 // Fold trivial predicates.
307 if (Pred == FCmpInst::FCMP_FALSE)
308 return ConstantInt::get(GetCompareTy(LHS), 0);
309 if (Pred == FCmpInst::FCMP_TRUE)
310 return ConstantInt::get(GetCompareTy(LHS), 1);
311
Chris Lattner210c5d42009-11-09 23:55:12 +0000312 if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef
313 return UndefValue::get(GetCompareTy(LHS));
314
315 // fcmp x,x -> true/false. Not all compares are foldable.
316 if (LHS == RHS) {
317 if (CmpInst::isTrueWhenEqual(Pred))
318 return ConstantInt::get(GetCompareTy(LHS), 1);
319 if (CmpInst::isFalseWhenEqual(Pred))
320 return ConstantInt::get(GetCompareTy(LHS), 0);
321 }
322
323 // Handle fcmp with constant RHS
324 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
325 // If the constant is a nan, see if we can fold the comparison based on it.
326 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
327 if (CFP->getValueAPF().isNaN()) {
328 if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo"
329 return ConstantInt::getFalse(CFP->getContext());
330 assert(FCmpInst::isUnordered(Pred) &&
331 "Comparison must be either ordered or unordered!");
332 // True if unordered.
333 return ConstantInt::getTrue(CFP->getContext());
334 }
Dan Gohman6b617a72010-02-22 04:06:03 +0000335 // Check whether the constant is an infinity.
336 if (CFP->getValueAPF().isInfinity()) {
337 if (CFP->getValueAPF().isNegative()) {
338 switch (Pred) {
339 case FCmpInst::FCMP_OLT:
340 // No value is ordered and less than negative infinity.
341 return ConstantInt::getFalse(CFP->getContext());
342 case FCmpInst::FCMP_UGE:
343 // All values are unordered with or at least negative infinity.
344 return ConstantInt::getTrue(CFP->getContext());
345 default:
346 break;
347 }
348 } else {
349 switch (Pred) {
350 case FCmpInst::FCMP_OGT:
351 // No value is ordered and greater than infinity.
352 return ConstantInt::getFalse(CFP->getContext());
353 case FCmpInst::FCMP_ULE:
354 // All values are unordered with and at most infinity.
355 return ConstantInt::getTrue(CFP->getContext());
356 default:
357 break;
358 }
359 }
360 }
Chris Lattner210c5d42009-11-09 23:55:12 +0000361 }
362 }
363
Duncan Sands92826de2010-11-07 16:46:25 +0000364 // If the comparison is with the result of a select instruction, check whether
365 // comparing with either branch of the select always yields the same value.
Duncan Sands3bbb0cc2010-11-09 17:25:51 +0000366 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
367 if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD))
368 return V;
Duncan Sands92826de2010-11-07 16:46:25 +0000369
Chris Lattner9dbb4292009-11-09 23:28:39 +0000370 return 0;
371}
372
Chris Lattner04754262010-04-20 05:32:14 +0000373/// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
374/// the result. If not, this returns null.
375Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal,
376 const TargetData *TD) {
377 // select true, X, Y -> X
378 // select false, X, Y -> Y
379 if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal))
380 return CB->getZExtValue() ? TrueVal : FalseVal;
381
382 // select C, X, X -> X
383 if (TrueVal == FalseVal)
384 return TrueVal;
385
386 if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X
387 return FalseVal;
388 if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X
389 return TrueVal;
390 if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y
391 if (isa<Constant>(TrueVal))
392 return TrueVal;
393 return FalseVal;
394 }
395
396
397
398 return 0;
399}
400
401
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000402/// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
403/// fold the result. If not, this returns null.
404Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps,
405 const TargetData *TD) {
406 // getelementptr P -> P.
407 if (NumOps == 1)
408 return Ops[0];
409
410 // TODO.
411 //if (isa<UndefValue>(Ops[0]))
412 // return UndefValue::get(GEP.getType());
413
414 // getelementptr P, 0 -> P.
415 if (NumOps == 2)
416 if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
417 if (C->isZero())
418 return Ops[0];
419
420 // Check to see if this is constant foldable.
421 for (unsigned i = 0; i != NumOps; ++i)
422 if (!isa<Constant>(Ops[i]))
423 return 0;
424
425 return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]),
426 (Constant *const*)Ops+1, NumOps-1);
427}
428
429
Chris Lattnerd06094f2009-11-10 00:55:12 +0000430//=== Helper functions for higher up the class hierarchy.
Chris Lattner9dbb4292009-11-09 23:28:39 +0000431
Chris Lattnerd06094f2009-11-10 00:55:12 +0000432/// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
433/// fold the result. If not, this returns null.
434Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
435 const TargetData *TD) {
436 switch (Opcode) {
437 case Instruction::And: return SimplifyAndInst(LHS, RHS, TD);
438 case Instruction::Or: return SimplifyOrInst(LHS, RHS, TD);
439 default:
440 if (Constant *CLHS = dyn_cast<Constant>(LHS))
441 if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
442 Constant *COps[] = {CLHS, CRHS};
443 return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
444 }
445 return 0;
446 }
447}
Chris Lattner9dbb4292009-11-09 23:28:39 +0000448
449/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
450/// fold the result.
451Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
452 const TargetData *TD) {
453 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
454 return SimplifyICmpInst(Predicate, LHS, RHS, TD);
455 return SimplifyFCmpInst(Predicate, LHS, RHS, TD);
456}
457
Chris Lattnere3453782009-11-10 01:08:51 +0000458
459/// SimplifyInstruction - See if we can compute a simplified version of this
460/// instruction. If not, this returns null.
461Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD) {
462 switch (I->getOpcode()) {
463 default:
464 return ConstantFoldInstruction(I, TD);
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000465 case Instruction::Add:
Owen Anderson4e282de2010-09-16 20:51:41 +0000466 return SimplifyAddInst(I->getOperand(0), I->getOperand(1),
467 cast<BinaryOperator>(I)->hasNoSignedWrap(),
468 cast<BinaryOperator>(I)->hasNoUnsignedWrap(), TD);
Chris Lattnere3453782009-11-10 01:08:51 +0000469 case Instruction::And:
Owen Anderson4e282de2010-09-16 20:51:41 +0000470 return SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD);
Chris Lattnere3453782009-11-10 01:08:51 +0000471 case Instruction::Or:
Owen Anderson4e282de2010-09-16 20:51:41 +0000472 return SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD);
Chris Lattnere3453782009-11-10 01:08:51 +0000473 case Instruction::ICmp:
Owen Anderson4e282de2010-09-16 20:51:41 +0000474 return SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
475 I->getOperand(0), I->getOperand(1), TD);
Chris Lattnere3453782009-11-10 01:08:51 +0000476 case Instruction::FCmp:
Owen Anderson4e282de2010-09-16 20:51:41 +0000477 return SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
478 I->getOperand(0), I->getOperand(1), TD);
Chris Lattner04754262010-04-20 05:32:14 +0000479 case Instruction::Select:
Owen Anderson4e282de2010-09-16 20:51:41 +0000480 return SimplifySelectInst(I->getOperand(0), I->getOperand(1),
Chris Lattner04754262010-04-20 05:32:14 +0000481 I->getOperand(2), TD);
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000482 case Instruction::GetElementPtr: {
483 SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
Owen Anderson4e282de2010-09-16 20:51:41 +0000484 return SimplifyGEPInst(&Ops[0], Ops.size(), TD);
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000485 }
Chris Lattnere3453782009-11-10 01:08:51 +0000486 }
487}
488
Chris Lattner40d8c282009-11-10 22:26:15 +0000489/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
490/// delete the From instruction. In addition to a basic RAUW, this does a
491/// recursive simplification of the newly formed instructions. This catches
492/// things where one simplification exposes other opportunities. This only
493/// simplifies and deletes scalar operations, it does not change the CFG.
494///
495void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
496 const TargetData *TD) {
497 assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
498
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000499 // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that
500 // we can know if it gets deleted out from under us or replaced in a
501 // recursive simplification.
Chris Lattner40d8c282009-11-10 22:26:15 +0000502 WeakVH FromHandle(From);
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000503 WeakVH ToHandle(To);
Eli Friedmane2f93132010-07-15 05:09:31 +0000504
Chris Lattner40d8c282009-11-10 22:26:15 +0000505 while (!From->use_empty()) {
506 // Update the instruction to use the new value.
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000507 Use &TheUse = From->use_begin().getUse();
508 Instruction *User = cast<Instruction>(TheUse.getUser());
509 TheUse = To;
510
511 // Check to see if the instruction can be folded due to the operand
512 // replacement. For example changing (or X, Y) into (or X, -1) can replace
513 // the 'or' with -1.
514 Value *SimplifiedVal;
515 {
516 // Sanity check to make sure 'User' doesn't dangle across
517 // SimplifyInstruction.
518 AssertingVH<> UserHandle(User);
Chris Lattner40d8c282009-11-10 22:26:15 +0000519
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000520 SimplifiedVal = SimplifyInstruction(User, TD);
521 if (SimplifiedVal == 0) continue;
Chris Lattner40d8c282009-11-10 22:26:15 +0000522 }
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000523
524 // Recursively simplify this user to the new value.
525 ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD);
526 From = dyn_cast_or_null<Instruction>((Value*)FromHandle);
527 To = ToHandle;
528
529 assert(ToHandle && "To value deleted by recursive simplification?");
530
531 // If the recursive simplification ended up revisiting and deleting
532 // 'From' then we're done.
533 if (From == 0)
534 return;
Chris Lattner40d8c282009-11-10 22:26:15 +0000535 }
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000536
537 // If 'From' has value handles referring to it, do a real RAUW to update them.
538 From->replaceAllUsesWith(To);
539
Chris Lattner40d8c282009-11-10 22:26:15 +0000540 From->eraseFromParent();
541}
542