blob: b49b4d0c6aba31152f82f0beecd7655b57b3c82d [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
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000196
Chris Lattner9dbb4292009-11-09 23:28:39 +0000197/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
198/// fold the result. If not, this returns null.
199Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
200 const TargetData *TD) {
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000201 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
Chris Lattner9dbb4292009-11-09 23:28:39 +0000202 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000203
Chris Lattnerd06094f2009-11-10 00:55:12 +0000204 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner8f73dea2009-11-09 23:06:58 +0000205 if (Constant *CRHS = dyn_cast<Constant>(RHS))
206 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Chris Lattnerd06094f2009-11-10 00:55:12 +0000207
208 // If we have a constant, make sure it is on the RHS.
209 std::swap(LHS, RHS);
210 Pred = CmpInst::getSwappedPredicate(Pred);
211 }
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000212
Chris Lattner210c5d42009-11-09 23:55:12 +0000213 // ITy - This is the return type of the compare we're considering.
214 const Type *ITy = GetCompareTy(LHS);
215
216 // icmp X, X -> true/false
Chris Lattnerc8e14b32010-03-03 19:46:03 +0000217 // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false
218 // because X could be 0.
219 if (LHS == RHS || isa<UndefValue>(RHS))
Chris Lattner210c5d42009-11-09 23:55:12 +0000220 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
Chris Lattner210c5d42009-11-09 23:55:12 +0000221
222 // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
223 // addresses never equal each other! We already know that Op0 != Op1.
224 if ((isa<GlobalValue>(LHS) || isa<AllocaInst>(LHS) ||
225 isa<ConstantPointerNull>(LHS)) &&
226 (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
227 isa<ConstantPointerNull>(RHS)))
228 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
229
230 // See if we are doing a comparison with a constant.
231 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
232 // If we have an icmp le or icmp ge instruction, turn it into the
233 // appropriate icmp lt or icmp gt instruction. This allows us to rely on
234 // them being folded in the code below.
235 switch (Pred) {
236 default: break;
237 case ICmpInst::ICMP_ULE:
238 if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
239 return ConstantInt::getTrue(CI->getContext());
240 break;
241 case ICmpInst::ICMP_SLE:
242 if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
243 return ConstantInt::getTrue(CI->getContext());
244 break;
245 case ICmpInst::ICMP_UGE:
246 if (CI->isMinValue(false)) // A >=u MIN -> TRUE
247 return ConstantInt::getTrue(CI->getContext());
248 break;
249 case ICmpInst::ICMP_SGE:
250 if (CI->isMinValue(true)) // A >=s MIN -> TRUE
251 return ConstantInt::getTrue(CI->getContext());
252 break;
253 }
Chris Lattner210c5d42009-11-09 23:55:12 +0000254 }
255
256
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000257 return 0;
258}
259
Chris Lattner9dbb4292009-11-09 23:28:39 +0000260/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
261/// fold the result. If not, this returns null.
262Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
263 const TargetData *TD) {
264 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
265 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
266
Chris Lattnerd06094f2009-11-10 00:55:12 +0000267 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner9dbb4292009-11-09 23:28:39 +0000268 if (Constant *CRHS = dyn_cast<Constant>(RHS))
269 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Chris Lattnerd06094f2009-11-10 00:55:12 +0000270
271 // If we have a constant, make sure it is on the RHS.
272 std::swap(LHS, RHS);
273 Pred = CmpInst::getSwappedPredicate(Pred);
274 }
Chris Lattner9dbb4292009-11-09 23:28:39 +0000275
Chris Lattner210c5d42009-11-09 23:55:12 +0000276 // Fold trivial predicates.
277 if (Pred == FCmpInst::FCMP_FALSE)
278 return ConstantInt::get(GetCompareTy(LHS), 0);
279 if (Pred == FCmpInst::FCMP_TRUE)
280 return ConstantInt::get(GetCompareTy(LHS), 1);
281
Chris Lattner210c5d42009-11-09 23:55:12 +0000282 if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef
283 return UndefValue::get(GetCompareTy(LHS));
284
285 // fcmp x,x -> true/false. Not all compares are foldable.
286 if (LHS == RHS) {
287 if (CmpInst::isTrueWhenEqual(Pred))
288 return ConstantInt::get(GetCompareTy(LHS), 1);
289 if (CmpInst::isFalseWhenEqual(Pred))
290 return ConstantInt::get(GetCompareTy(LHS), 0);
291 }
292
293 // Handle fcmp with constant RHS
294 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
295 // If the constant is a nan, see if we can fold the comparison based on it.
296 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
297 if (CFP->getValueAPF().isNaN()) {
298 if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo"
299 return ConstantInt::getFalse(CFP->getContext());
300 assert(FCmpInst::isUnordered(Pred) &&
301 "Comparison must be either ordered or unordered!");
302 // True if unordered.
303 return ConstantInt::getTrue(CFP->getContext());
304 }
Dan Gohman6b617a72010-02-22 04:06:03 +0000305 // Check whether the constant is an infinity.
306 if (CFP->getValueAPF().isInfinity()) {
307 if (CFP->getValueAPF().isNegative()) {
308 switch (Pred) {
309 case FCmpInst::FCMP_OLT:
310 // No value is ordered and less than negative infinity.
311 return ConstantInt::getFalse(CFP->getContext());
312 case FCmpInst::FCMP_UGE:
313 // All values are unordered with or at least negative infinity.
314 return ConstantInt::getTrue(CFP->getContext());
315 default:
316 break;
317 }
318 } else {
319 switch (Pred) {
320 case FCmpInst::FCMP_OGT:
321 // No value is ordered and greater than infinity.
322 return ConstantInt::getFalse(CFP->getContext());
323 case FCmpInst::FCMP_ULE:
324 // All values are unordered with and at most infinity.
325 return ConstantInt::getTrue(CFP->getContext());
326 default:
327 break;
328 }
329 }
330 }
Chris Lattner210c5d42009-11-09 23:55:12 +0000331 }
332 }
333
Chris Lattner9dbb4292009-11-09 23:28:39 +0000334 return 0;
335}
336
Chris Lattner04754262010-04-20 05:32:14 +0000337/// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
338/// the result. If not, this returns null.
339Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal,
340 const TargetData *TD) {
341 // select true, X, Y -> X
342 // select false, X, Y -> Y
343 if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal))
344 return CB->getZExtValue() ? TrueVal : FalseVal;
345
346 // select C, X, X -> X
347 if (TrueVal == FalseVal)
348 return TrueVal;
349
350 if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X
351 return FalseVal;
352 if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X
353 return TrueVal;
354 if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y
355 if (isa<Constant>(TrueVal))
356 return TrueVal;
357 return FalseVal;
358 }
359
360
361
362 return 0;
363}
364
365
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000366/// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
367/// fold the result. If not, this returns null.
368Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps,
369 const TargetData *TD) {
370 // getelementptr P -> P.
371 if (NumOps == 1)
372 return Ops[0];
373
374 // TODO.
375 //if (isa<UndefValue>(Ops[0]))
376 // return UndefValue::get(GEP.getType());
377
378 // getelementptr P, 0 -> P.
379 if (NumOps == 2)
380 if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
381 if (C->isZero())
382 return Ops[0];
383
384 // Check to see if this is constant foldable.
385 for (unsigned i = 0; i != NumOps; ++i)
386 if (!isa<Constant>(Ops[i]))
387 return 0;
388
389 return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]),
390 (Constant *const*)Ops+1, NumOps-1);
391}
392
393
Chris Lattnerd06094f2009-11-10 00:55:12 +0000394//=== Helper functions for higher up the class hierarchy.
Chris Lattner9dbb4292009-11-09 23:28:39 +0000395
Chris Lattnerd06094f2009-11-10 00:55:12 +0000396/// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
397/// fold the result. If not, this returns null.
398Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
399 const TargetData *TD) {
400 switch (Opcode) {
401 case Instruction::And: return SimplifyAndInst(LHS, RHS, TD);
402 case Instruction::Or: return SimplifyOrInst(LHS, RHS, TD);
403 default:
404 if (Constant *CLHS = dyn_cast<Constant>(LHS))
405 if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
406 Constant *COps[] = {CLHS, CRHS};
407 return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
408 }
409 return 0;
410 }
411}
Chris Lattner9dbb4292009-11-09 23:28:39 +0000412
413/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
414/// fold the result.
415Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
416 const TargetData *TD) {
417 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
418 return SimplifyICmpInst(Predicate, LHS, RHS, TD);
419 return SimplifyFCmpInst(Predicate, LHS, RHS, TD);
420}
421
Chris Lattnere3453782009-11-10 01:08:51 +0000422
423/// SimplifyInstruction - See if we can compute a simplified version of this
424/// instruction. If not, this returns null.
425Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD) {
426 switch (I->getOpcode()) {
427 default:
428 return ConstantFoldInstruction(I, TD);
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000429 case Instruction::Add:
430 return SimplifyAddInst(I->getOperand(0), I->getOperand(1),
431 cast<BinaryOperator>(I)->hasNoSignedWrap(),
432 cast<BinaryOperator>(I)->hasNoUnsignedWrap(), TD);
Chris Lattnere3453782009-11-10 01:08:51 +0000433 case Instruction::And:
434 return SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD);
435 case Instruction::Or:
436 return SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD);
437 case Instruction::ICmp:
438 return SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
439 I->getOperand(0), I->getOperand(1), TD);
440 case Instruction::FCmp:
441 return SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
442 I->getOperand(0), I->getOperand(1), TD);
Chris Lattner04754262010-04-20 05:32:14 +0000443 case Instruction::Select:
444 return SimplifySelectInst(I->getOperand(0), I->getOperand(1),
445 I->getOperand(2), TD);
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000446 case Instruction::GetElementPtr: {
447 SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
448 return SimplifyGEPInst(&Ops[0], Ops.size(), TD);
449 }
Chris Lattnere3453782009-11-10 01:08:51 +0000450 }
451}
452
Chris Lattner40d8c282009-11-10 22:26:15 +0000453/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
454/// delete the From instruction. In addition to a basic RAUW, this does a
455/// recursive simplification of the newly formed instructions. This catches
456/// things where one simplification exposes other opportunities. This only
457/// simplifies and deletes scalar operations, it does not change the CFG.
458///
459void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
460 const TargetData *TD) {
461 assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
462
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000463 // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that
464 // we can know if it gets deleted out from under us or replaced in a
465 // recursive simplification.
Chris Lattner40d8c282009-11-10 22:26:15 +0000466 WeakVH FromHandle(From);
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000467 WeakVH ToHandle(To);
Eli Friedmane2f93132010-07-15 05:09:31 +0000468
Chris Lattner40d8c282009-11-10 22:26:15 +0000469 while (!From->use_empty()) {
470 // Update the instruction to use the new value.
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000471 Use &TheUse = From->use_begin().getUse();
472 Instruction *User = cast<Instruction>(TheUse.getUser());
473 TheUse = To;
474
475 // Check to see if the instruction can be folded due to the operand
476 // replacement. For example changing (or X, Y) into (or X, -1) can replace
477 // the 'or' with -1.
478 Value *SimplifiedVal;
479 {
480 // Sanity check to make sure 'User' doesn't dangle across
481 // SimplifyInstruction.
482 AssertingVH<> UserHandle(User);
Chris Lattner40d8c282009-11-10 22:26:15 +0000483
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000484 SimplifiedVal = SimplifyInstruction(User, TD);
485 if (SimplifiedVal == 0) continue;
Chris Lattner40d8c282009-11-10 22:26:15 +0000486 }
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000487
488 // Recursively simplify this user to the new value.
489 ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD);
490 From = dyn_cast_or_null<Instruction>((Value*)FromHandle);
491 To = ToHandle;
492
493 assert(ToHandle && "To value deleted by recursive simplification?");
494
495 // If the recursive simplification ended up revisiting and deleting
496 // 'From' then we're done.
497 if (From == 0)
498 return;
Chris Lattner40d8c282009-11-10 22:26:15 +0000499 }
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000500
501 // If 'From' has value handles referring to it, do a real RAUW to update them.
502 From->replaceAllUsesWith(To);
503
Chris Lattner40d8c282009-11-10 22:26:15 +0000504 From->eraseFromParent();
505}
506