InstructionSimplify.cpp revision 204642
1199481Srdivacky//===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
2199481Srdivacky//
3199481Srdivacky//                     The LLVM Compiler Infrastructure
4199481Srdivacky//
5199481Srdivacky// This file is distributed under the University of Illinois Open Source
6199481Srdivacky// License. See LICENSE.TXT for details.
7199481Srdivacky//
8199481Srdivacky//===----------------------------------------------------------------------===//
9199481Srdivacky//
10199481Srdivacky// This file implements routines for folding instructions into simpler forms
11199481Srdivacky// that do not require creating new instructions.  For example, this does
12199481Srdivacky// constant folding, and can handle identities like (X&0)->0.
13199481Srdivacky//
14199481Srdivacky//===----------------------------------------------------------------------===//
15199481Srdivacky
16199481Srdivacky#include "llvm/Analysis/InstructionSimplify.h"
17199481Srdivacky#include "llvm/Analysis/ConstantFolding.h"
18199481Srdivacky#include "llvm/Support/ValueHandle.h"
19199481Srdivacky#include "llvm/Instructions.h"
20199481Srdivacky#include "llvm/Support/PatternMatch.h"
21199481Srdivackyusing namespace llvm;
22199481Srdivackyusing namespace llvm::PatternMatch;
23199481Srdivacky
24199989Srdivacky/// SimplifyAddInst - Given operands for an Add, see if we can
25199481Srdivacky/// fold the result.  If not, this returns null.
26199989SrdivackyValue *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
27199481Srdivacky                             const TargetData *TD) {
28199481Srdivacky  if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
29199481Srdivacky    if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
30199481Srdivacky      Constant *Ops[] = { CLHS, CRHS };
31199989Srdivacky      return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(),
32199989Srdivacky                                      Ops, 2, TD);
33199989Srdivacky    }
34199989Srdivacky
35199989Srdivacky    // Canonicalize the constant to the RHS.
36199989Srdivacky    std::swap(Op0, Op1);
37199989Srdivacky  }
38199989Srdivacky
39199989Srdivacky  if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
40199989Srdivacky    // X + undef -> undef
41199989Srdivacky    if (isa<UndefValue>(Op1C))
42199989Srdivacky      return Op1C;
43199989Srdivacky
44199989Srdivacky    // X + 0 --> X
45199989Srdivacky    if (Op1C->isNullValue())
46199989Srdivacky      return Op0;
47199989Srdivacky  }
48199989Srdivacky
49199989Srdivacky  // FIXME: Could pull several more out of instcombine.
50199989Srdivacky  return 0;
51199989Srdivacky}
52199989Srdivacky
53199989Srdivacky/// SimplifyAndInst - Given operands for an And, see if we can
54199989Srdivacky/// fold the result.  If not, this returns null.
55199989SrdivackyValue *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD) {
56199989Srdivacky  if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
57199989Srdivacky    if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
58199989Srdivacky      Constant *Ops[] = { CLHS, CRHS };
59199481Srdivacky      return ConstantFoldInstOperands(Instruction::And, CLHS->getType(),
60199481Srdivacky                                      Ops, 2, TD);
61199481Srdivacky    }
62199481Srdivacky
63199481Srdivacky    // Canonicalize the constant to the RHS.
64199481Srdivacky    std::swap(Op0, Op1);
65199481Srdivacky  }
66199481Srdivacky
67199481Srdivacky  // X & undef -> 0
68199481Srdivacky  if (isa<UndefValue>(Op1))
69199481Srdivacky    return Constant::getNullValue(Op0->getType());
70199481Srdivacky
71199481Srdivacky  // X & X = X
72199481Srdivacky  if (Op0 == Op1)
73199481Srdivacky    return Op0;
74199481Srdivacky
75199481Srdivacky  // X & <0,0> = <0,0>
76199481Srdivacky  if (isa<ConstantAggregateZero>(Op1))
77199481Srdivacky    return Op1;
78199481Srdivacky
79199481Srdivacky  // X & <-1,-1> = X
80199481Srdivacky  if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1))
81199481Srdivacky    if (CP->isAllOnesValue())
82199481Srdivacky      return Op0;
83199481Srdivacky
84199481Srdivacky  if (ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1)) {
85199481Srdivacky    // X & 0 = 0
86199481Srdivacky    if (Op1CI->isZero())
87199481Srdivacky      return Op1CI;
88199481Srdivacky    // X & -1 = X
89199481Srdivacky    if (Op1CI->isAllOnesValue())
90199481Srdivacky      return Op0;
91199481Srdivacky  }
92199481Srdivacky
93199481Srdivacky  // A & ~A  =  ~A & A  =  0
94199481Srdivacky  Value *A, *B;
95199481Srdivacky  if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
96199481Srdivacky      (match(Op1, m_Not(m_Value(A))) && A == Op0))
97199481Srdivacky    return Constant::getNullValue(Op0->getType());
98199481Srdivacky
99199481Srdivacky  // (A | ?) & A = A
100199481Srdivacky  if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
101199481Srdivacky      (A == Op1 || B == Op1))
102199481Srdivacky    return Op1;
103199481Srdivacky
104199481Srdivacky  // A & (A | ?) = A
105199481Srdivacky  if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
106199481Srdivacky      (A == Op0 || B == Op0))
107199481Srdivacky    return Op0;
108199481Srdivacky
109199481Srdivacky  return 0;
110199481Srdivacky}
111199481Srdivacky
112199481Srdivacky/// SimplifyOrInst - Given operands for an Or, see if we can
113199481Srdivacky/// fold the result.  If not, this returns null.
114199989SrdivackyValue *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD) {
115199481Srdivacky  if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
116199481Srdivacky    if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
117199481Srdivacky      Constant *Ops[] = { CLHS, CRHS };
118199481Srdivacky      return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(),
119199481Srdivacky                                      Ops, 2, TD);
120199481Srdivacky    }
121199481Srdivacky
122199481Srdivacky    // Canonicalize the constant to the RHS.
123199481Srdivacky    std::swap(Op0, Op1);
124199481Srdivacky  }
125199481Srdivacky
126199481Srdivacky  // X | undef -> -1
127199481Srdivacky  if (isa<UndefValue>(Op1))
128199481Srdivacky    return Constant::getAllOnesValue(Op0->getType());
129199481Srdivacky
130199481Srdivacky  // X | X = X
131199481Srdivacky  if (Op0 == Op1)
132199481Srdivacky    return Op0;
133199481Srdivacky
134199481Srdivacky  // X | <0,0> = X
135199481Srdivacky  if (isa<ConstantAggregateZero>(Op1))
136199481Srdivacky    return Op0;
137199481Srdivacky
138199481Srdivacky  // X | <-1,-1> = <-1,-1>
139199481Srdivacky  if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1))
140199481Srdivacky    if (CP->isAllOnesValue())
141199481Srdivacky      return Op1;
142199481Srdivacky
143199481Srdivacky  if (ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1)) {
144199481Srdivacky    // X | 0 = X
145199481Srdivacky    if (Op1CI->isZero())
146199481Srdivacky      return Op0;
147199481Srdivacky    // X | -1 = -1
148199481Srdivacky    if (Op1CI->isAllOnesValue())
149199481Srdivacky      return Op1CI;
150199481Srdivacky  }
151199481Srdivacky
152199481Srdivacky  // A | ~A  =  ~A | A  =  -1
153199481Srdivacky  Value *A, *B;
154199481Srdivacky  if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
155199481Srdivacky      (match(Op1, m_Not(m_Value(A))) && A == Op0))
156199481Srdivacky    return Constant::getAllOnesValue(Op0->getType());
157199481Srdivacky
158199481Srdivacky  // (A & ?) | A = A
159199481Srdivacky  if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
160199481Srdivacky      (A == Op1 || B == Op1))
161199481Srdivacky    return Op1;
162199481Srdivacky
163199481Srdivacky  // A | (A & ?) = A
164199481Srdivacky  if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
165199481Srdivacky      (A == Op0 || B == Op0))
166199481Srdivacky    return Op0;
167199481Srdivacky
168199481Srdivacky  return 0;
169199481Srdivacky}
170199481Srdivacky
171199481Srdivacky
172199481Srdivackystatic const Type *GetCompareTy(Value *Op) {
173199481Srdivacky  return CmpInst::makeCmpResultType(Op->getType());
174199481Srdivacky}
175199481Srdivacky
176199481Srdivacky
177199481Srdivacky/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
178199481Srdivacky/// fold the result.  If not, this returns null.
179199481SrdivackyValue *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
180199481Srdivacky                              const TargetData *TD) {
181199481Srdivacky  CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
182199481Srdivacky  assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
183199481Srdivacky
184199481Srdivacky  if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
185199481Srdivacky    if (Constant *CRHS = dyn_cast<Constant>(RHS))
186199481Srdivacky      return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
187199481Srdivacky
188199481Srdivacky    // If we have a constant, make sure it is on the RHS.
189199481Srdivacky    std::swap(LHS, RHS);
190199481Srdivacky    Pred = CmpInst::getSwappedPredicate(Pred);
191199481Srdivacky  }
192199481Srdivacky
193199481Srdivacky  // ITy - This is the return type of the compare we're considering.
194199481Srdivacky  const Type *ITy = GetCompareTy(LHS);
195199481Srdivacky
196199481Srdivacky  // icmp X, X -> true/false
197199481Srdivacky  if (LHS == RHS)
198199481Srdivacky    return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
199199481Srdivacky
200199481Srdivacky  if (isa<UndefValue>(RHS))                  // X icmp undef -> undef
201199481Srdivacky    return UndefValue::get(ITy);
202199481Srdivacky
203199481Srdivacky  // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
204199481Srdivacky  // addresses never equal each other!  We already know that Op0 != Op1.
205199481Srdivacky  if ((isa<GlobalValue>(LHS) || isa<AllocaInst>(LHS) ||
206199481Srdivacky       isa<ConstantPointerNull>(LHS)) &&
207199481Srdivacky      (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
208199481Srdivacky       isa<ConstantPointerNull>(RHS)))
209199481Srdivacky    return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
210199481Srdivacky
211199481Srdivacky  // See if we are doing a comparison with a constant.
212199481Srdivacky  if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
213199481Srdivacky    // If we have an icmp le or icmp ge instruction, turn it into the
214199481Srdivacky    // appropriate icmp lt or icmp gt instruction.  This allows us to rely on
215199481Srdivacky    // them being folded in the code below.
216199481Srdivacky    switch (Pred) {
217199481Srdivacky    default: break;
218199481Srdivacky    case ICmpInst::ICMP_ULE:
219199481Srdivacky      if (CI->isMaxValue(false))                 // A <=u MAX -> TRUE
220199481Srdivacky        return ConstantInt::getTrue(CI->getContext());
221199481Srdivacky      break;
222199481Srdivacky    case ICmpInst::ICMP_SLE:
223199481Srdivacky      if (CI->isMaxValue(true))                  // A <=s MAX -> TRUE
224199481Srdivacky        return ConstantInt::getTrue(CI->getContext());
225199481Srdivacky      break;
226199481Srdivacky    case ICmpInst::ICMP_UGE:
227199481Srdivacky      if (CI->isMinValue(false))                 // A >=u MIN -> TRUE
228199481Srdivacky        return ConstantInt::getTrue(CI->getContext());
229199481Srdivacky      break;
230199481Srdivacky    case ICmpInst::ICMP_SGE:
231199481Srdivacky      if (CI->isMinValue(true))                  // A >=s MIN -> TRUE
232199481Srdivacky        return ConstantInt::getTrue(CI->getContext());
233199481Srdivacky      break;
234199481Srdivacky    }
235199481Srdivacky  }
236199481Srdivacky
237199481Srdivacky
238199481Srdivacky  return 0;
239199481Srdivacky}
240199481Srdivacky
241199481Srdivacky/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
242199481Srdivacky/// fold the result.  If not, this returns null.
243199481SrdivackyValue *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
244199481Srdivacky                              const TargetData *TD) {
245199481Srdivacky  CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
246199481Srdivacky  assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
247199481Srdivacky
248199481Srdivacky  if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
249199481Srdivacky    if (Constant *CRHS = dyn_cast<Constant>(RHS))
250199481Srdivacky      return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
251199481Srdivacky
252199481Srdivacky    // If we have a constant, make sure it is on the RHS.
253199481Srdivacky    std::swap(LHS, RHS);
254199481Srdivacky    Pred = CmpInst::getSwappedPredicate(Pred);
255199481Srdivacky  }
256199481Srdivacky
257199481Srdivacky  // Fold trivial predicates.
258199481Srdivacky  if (Pred == FCmpInst::FCMP_FALSE)
259199481Srdivacky    return ConstantInt::get(GetCompareTy(LHS), 0);
260199481Srdivacky  if (Pred == FCmpInst::FCMP_TRUE)
261199481Srdivacky    return ConstantInt::get(GetCompareTy(LHS), 1);
262199481Srdivacky
263199481Srdivacky  if (isa<UndefValue>(RHS))                  // fcmp pred X, undef -> undef
264199481Srdivacky    return UndefValue::get(GetCompareTy(LHS));
265199481Srdivacky
266199481Srdivacky  // fcmp x,x -> true/false.  Not all compares are foldable.
267199481Srdivacky  if (LHS == RHS) {
268199481Srdivacky    if (CmpInst::isTrueWhenEqual(Pred))
269199481Srdivacky      return ConstantInt::get(GetCompareTy(LHS), 1);
270199481Srdivacky    if (CmpInst::isFalseWhenEqual(Pred))
271199481Srdivacky      return ConstantInt::get(GetCompareTy(LHS), 0);
272199481Srdivacky  }
273199481Srdivacky
274199481Srdivacky  // Handle fcmp with constant RHS
275199481Srdivacky  if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
276199481Srdivacky    // If the constant is a nan, see if we can fold the comparison based on it.
277199481Srdivacky    if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
278199481Srdivacky      if (CFP->getValueAPF().isNaN()) {
279199481Srdivacky        if (FCmpInst::isOrdered(Pred))   // True "if ordered and foo"
280199481Srdivacky          return ConstantInt::getFalse(CFP->getContext());
281199481Srdivacky        assert(FCmpInst::isUnordered(Pred) &&
282199481Srdivacky               "Comparison must be either ordered or unordered!");
283199481Srdivacky        // True if unordered.
284199481Srdivacky        return ConstantInt::getTrue(CFP->getContext());
285199481Srdivacky      }
286204642Srdivacky      // Check whether the constant is an infinity.
287204642Srdivacky      if (CFP->getValueAPF().isInfinity()) {
288204642Srdivacky        if (CFP->getValueAPF().isNegative()) {
289204642Srdivacky          switch (Pred) {
290204642Srdivacky          case FCmpInst::FCMP_OLT:
291204642Srdivacky            // No value is ordered and less than negative infinity.
292204642Srdivacky            return ConstantInt::getFalse(CFP->getContext());
293204642Srdivacky          case FCmpInst::FCMP_UGE:
294204642Srdivacky            // All values are unordered with or at least negative infinity.
295204642Srdivacky            return ConstantInt::getTrue(CFP->getContext());
296204642Srdivacky          default:
297204642Srdivacky            break;
298204642Srdivacky          }
299204642Srdivacky        } else {
300204642Srdivacky          switch (Pred) {
301204642Srdivacky          case FCmpInst::FCMP_OGT:
302204642Srdivacky            // No value is ordered and greater than infinity.
303204642Srdivacky            return ConstantInt::getFalse(CFP->getContext());
304204642Srdivacky          case FCmpInst::FCMP_ULE:
305204642Srdivacky            // All values are unordered with and at most infinity.
306204642Srdivacky            return ConstantInt::getTrue(CFP->getContext());
307204642Srdivacky          default:
308204642Srdivacky            break;
309204642Srdivacky          }
310204642Srdivacky        }
311204642Srdivacky      }
312199481Srdivacky    }
313199481Srdivacky  }
314199481Srdivacky
315199481Srdivacky  return 0;
316199481Srdivacky}
317199481Srdivacky
318199989Srdivacky/// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
319199989Srdivacky/// fold the result.  If not, this returns null.
320199989SrdivackyValue *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps,
321199989Srdivacky                             const TargetData *TD) {
322199989Srdivacky  // getelementptr P -> P.
323199989Srdivacky  if (NumOps == 1)
324199989Srdivacky    return Ops[0];
325199989Srdivacky
326199989Srdivacky  // TODO.
327199989Srdivacky  //if (isa<UndefValue>(Ops[0]))
328199989Srdivacky  //  return UndefValue::get(GEP.getType());
329199989Srdivacky
330199989Srdivacky  // getelementptr P, 0 -> P.
331199989Srdivacky  if (NumOps == 2)
332199989Srdivacky    if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
333199989Srdivacky      if (C->isZero())
334199989Srdivacky        return Ops[0];
335199989Srdivacky
336199989Srdivacky  // Check to see if this is constant foldable.
337199989Srdivacky  for (unsigned i = 0; i != NumOps; ++i)
338199989Srdivacky    if (!isa<Constant>(Ops[i]))
339199989Srdivacky      return 0;
340199989Srdivacky
341199989Srdivacky  return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]),
342199989Srdivacky                                        (Constant *const*)Ops+1, NumOps-1);
343199989Srdivacky}
344199989Srdivacky
345199989Srdivacky
346199481Srdivacky//=== Helper functions for higher up the class hierarchy.
347199481Srdivacky
348199481Srdivacky/// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
349199481Srdivacky/// fold the result.  If not, this returns null.
350199481SrdivackyValue *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
351199481Srdivacky                           const TargetData *TD) {
352199481Srdivacky  switch (Opcode) {
353199481Srdivacky  case Instruction::And: return SimplifyAndInst(LHS, RHS, TD);
354199481Srdivacky  case Instruction::Or:  return SimplifyOrInst(LHS, RHS, TD);
355199481Srdivacky  default:
356199481Srdivacky    if (Constant *CLHS = dyn_cast<Constant>(LHS))
357199481Srdivacky      if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
358199481Srdivacky        Constant *COps[] = {CLHS, CRHS};
359199481Srdivacky        return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
360199481Srdivacky      }
361199481Srdivacky    return 0;
362199481Srdivacky  }
363199481Srdivacky}
364199481Srdivacky
365199481Srdivacky/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
366199481Srdivacky/// fold the result.
367199481SrdivackyValue *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
368199481Srdivacky                             const TargetData *TD) {
369199481Srdivacky  if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
370199481Srdivacky    return SimplifyICmpInst(Predicate, LHS, RHS, TD);
371199481Srdivacky  return SimplifyFCmpInst(Predicate, LHS, RHS, TD);
372199481Srdivacky}
373199481Srdivacky
374199481Srdivacky
375199481Srdivacky/// SimplifyInstruction - See if we can compute a simplified version of this
376199481Srdivacky/// instruction.  If not, this returns null.
377199481SrdivackyValue *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD) {
378199481Srdivacky  switch (I->getOpcode()) {
379199481Srdivacky  default:
380199481Srdivacky    return ConstantFoldInstruction(I, TD);
381199989Srdivacky  case Instruction::Add:
382199989Srdivacky    return SimplifyAddInst(I->getOperand(0), I->getOperand(1),
383199989Srdivacky                           cast<BinaryOperator>(I)->hasNoSignedWrap(),
384199989Srdivacky                           cast<BinaryOperator>(I)->hasNoUnsignedWrap(), TD);
385199481Srdivacky  case Instruction::And:
386199481Srdivacky    return SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD);
387199481Srdivacky  case Instruction::Or:
388199481Srdivacky    return SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD);
389199481Srdivacky  case Instruction::ICmp:
390199481Srdivacky    return SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
391199481Srdivacky                            I->getOperand(0), I->getOperand(1), TD);
392199481Srdivacky  case Instruction::FCmp:
393199481Srdivacky    return SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
394199481Srdivacky                            I->getOperand(0), I->getOperand(1), TD);
395199989Srdivacky  case Instruction::GetElementPtr: {
396199989Srdivacky    SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
397199989Srdivacky    return SimplifyGEPInst(&Ops[0], Ops.size(), TD);
398199481Srdivacky  }
399199989Srdivacky  }
400199481Srdivacky}
401199481Srdivacky
402199481Srdivacky/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
403199481Srdivacky/// delete the From instruction.  In addition to a basic RAUW, this does a
404199481Srdivacky/// recursive simplification of the newly formed instructions.  This catches
405199481Srdivacky/// things where one simplification exposes other opportunities.  This only
406199481Srdivacky/// simplifies and deletes scalar operations, it does not change the CFG.
407199481Srdivacky///
408199481Srdivackyvoid llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
409199481Srdivacky                                     const TargetData *TD) {
410199481Srdivacky  assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
411199481Srdivacky
412199481Srdivacky  // FromHandle - This keeps a weakvh on the from value so that we can know if
413199481Srdivacky  // it gets deleted out from under us in a recursive simplification.
414199481Srdivacky  WeakVH FromHandle(From);
415199481Srdivacky
416199481Srdivacky  while (!From->use_empty()) {
417199481Srdivacky    // Update the instruction to use the new value.
418199481Srdivacky    Use &U = From->use_begin().getUse();
419199481Srdivacky    Instruction *User = cast<Instruction>(U.getUser());
420199481Srdivacky    U = To;
421199481Srdivacky
422199481Srdivacky    // See if we can simplify it.
423199481Srdivacky    if (Value *V = SimplifyInstruction(User, TD)) {
424199481Srdivacky      // Recursively simplify this.
425199481Srdivacky      ReplaceAndSimplifyAllUses(User, V, TD);
426199481Srdivacky
427199481Srdivacky      // If the recursive simplification ended up revisiting and deleting 'From'
428199481Srdivacky      // then we're done.
429199481Srdivacky      if (FromHandle == 0)
430199481Srdivacky        return;
431199481Srdivacky    }
432199481Srdivacky  }
433199481Srdivacky  From->eraseFromParent();
434199481Srdivacky}
435199481Srdivacky
436