InstructionSimplify.cpp revision 204792
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
197204792Srdivacky  // X icmp undef -> true/false.  For example, icmp ugt %X, undef -> false
198204792Srdivacky  // because X could be 0.
199204792Srdivacky  if (LHS == RHS || isa<UndefValue>(RHS))
200199481Srdivacky    return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
201199481Srdivacky
202199481Srdivacky  // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
203199481Srdivacky  // addresses never equal each other!  We already know that Op0 != Op1.
204199481Srdivacky  if ((isa<GlobalValue>(LHS) || isa<AllocaInst>(LHS) ||
205199481Srdivacky       isa<ConstantPointerNull>(LHS)) &&
206199481Srdivacky      (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
207199481Srdivacky       isa<ConstantPointerNull>(RHS)))
208199481Srdivacky    return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
209199481Srdivacky
210199481Srdivacky  // See if we are doing a comparison with a constant.
211199481Srdivacky  if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
212199481Srdivacky    // If we have an icmp le or icmp ge instruction, turn it into the
213199481Srdivacky    // appropriate icmp lt or icmp gt instruction.  This allows us to rely on
214199481Srdivacky    // them being folded in the code below.
215199481Srdivacky    switch (Pred) {
216199481Srdivacky    default: break;
217199481Srdivacky    case ICmpInst::ICMP_ULE:
218199481Srdivacky      if (CI->isMaxValue(false))                 // A <=u MAX -> TRUE
219199481Srdivacky        return ConstantInt::getTrue(CI->getContext());
220199481Srdivacky      break;
221199481Srdivacky    case ICmpInst::ICMP_SLE:
222199481Srdivacky      if (CI->isMaxValue(true))                  // A <=s MAX -> TRUE
223199481Srdivacky        return ConstantInt::getTrue(CI->getContext());
224199481Srdivacky      break;
225199481Srdivacky    case ICmpInst::ICMP_UGE:
226199481Srdivacky      if (CI->isMinValue(false))                 // A >=u MIN -> TRUE
227199481Srdivacky        return ConstantInt::getTrue(CI->getContext());
228199481Srdivacky      break;
229199481Srdivacky    case ICmpInst::ICMP_SGE:
230199481Srdivacky      if (CI->isMinValue(true))                  // A >=s MIN -> TRUE
231199481Srdivacky        return ConstantInt::getTrue(CI->getContext());
232199481Srdivacky      break;
233199481Srdivacky    }
234199481Srdivacky  }
235199481Srdivacky
236199481Srdivacky
237199481Srdivacky  return 0;
238199481Srdivacky}
239199481Srdivacky
240199481Srdivacky/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
241199481Srdivacky/// fold the result.  If not, this returns null.
242199481SrdivackyValue *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
243199481Srdivacky                              const TargetData *TD) {
244199481Srdivacky  CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
245199481Srdivacky  assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
246199481Srdivacky
247199481Srdivacky  if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
248199481Srdivacky    if (Constant *CRHS = dyn_cast<Constant>(RHS))
249199481Srdivacky      return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
250199481Srdivacky
251199481Srdivacky    // If we have a constant, make sure it is on the RHS.
252199481Srdivacky    std::swap(LHS, RHS);
253199481Srdivacky    Pred = CmpInst::getSwappedPredicate(Pred);
254199481Srdivacky  }
255199481Srdivacky
256199481Srdivacky  // Fold trivial predicates.
257199481Srdivacky  if (Pred == FCmpInst::FCMP_FALSE)
258199481Srdivacky    return ConstantInt::get(GetCompareTy(LHS), 0);
259199481Srdivacky  if (Pred == FCmpInst::FCMP_TRUE)
260199481Srdivacky    return ConstantInt::get(GetCompareTy(LHS), 1);
261199481Srdivacky
262199481Srdivacky  if (isa<UndefValue>(RHS))                  // fcmp pred X, undef -> undef
263199481Srdivacky    return UndefValue::get(GetCompareTy(LHS));
264199481Srdivacky
265199481Srdivacky  // fcmp x,x -> true/false.  Not all compares are foldable.
266199481Srdivacky  if (LHS == RHS) {
267199481Srdivacky    if (CmpInst::isTrueWhenEqual(Pred))
268199481Srdivacky      return ConstantInt::get(GetCompareTy(LHS), 1);
269199481Srdivacky    if (CmpInst::isFalseWhenEqual(Pred))
270199481Srdivacky      return ConstantInt::get(GetCompareTy(LHS), 0);
271199481Srdivacky  }
272199481Srdivacky
273199481Srdivacky  // Handle fcmp with constant RHS
274199481Srdivacky  if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
275199481Srdivacky    // If the constant is a nan, see if we can fold the comparison based on it.
276199481Srdivacky    if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
277199481Srdivacky      if (CFP->getValueAPF().isNaN()) {
278199481Srdivacky        if (FCmpInst::isOrdered(Pred))   // True "if ordered and foo"
279199481Srdivacky          return ConstantInt::getFalse(CFP->getContext());
280199481Srdivacky        assert(FCmpInst::isUnordered(Pred) &&
281199481Srdivacky               "Comparison must be either ordered or unordered!");
282199481Srdivacky        // True if unordered.
283199481Srdivacky        return ConstantInt::getTrue(CFP->getContext());
284199481Srdivacky      }
285204642Srdivacky      // Check whether the constant is an infinity.
286204642Srdivacky      if (CFP->getValueAPF().isInfinity()) {
287204642Srdivacky        if (CFP->getValueAPF().isNegative()) {
288204642Srdivacky          switch (Pred) {
289204642Srdivacky          case FCmpInst::FCMP_OLT:
290204642Srdivacky            // No value is ordered and less than negative infinity.
291204642Srdivacky            return ConstantInt::getFalse(CFP->getContext());
292204642Srdivacky          case FCmpInst::FCMP_UGE:
293204642Srdivacky            // All values are unordered with or at least negative infinity.
294204642Srdivacky            return ConstantInt::getTrue(CFP->getContext());
295204642Srdivacky          default:
296204642Srdivacky            break;
297204642Srdivacky          }
298204642Srdivacky        } else {
299204642Srdivacky          switch (Pred) {
300204642Srdivacky          case FCmpInst::FCMP_OGT:
301204642Srdivacky            // No value is ordered and greater than infinity.
302204642Srdivacky            return ConstantInt::getFalse(CFP->getContext());
303204642Srdivacky          case FCmpInst::FCMP_ULE:
304204642Srdivacky            // All values are unordered with and at most infinity.
305204642Srdivacky            return ConstantInt::getTrue(CFP->getContext());
306204642Srdivacky          default:
307204642Srdivacky            break;
308204642Srdivacky          }
309204642Srdivacky        }
310204642Srdivacky      }
311199481Srdivacky    }
312199481Srdivacky  }
313199481Srdivacky
314199481Srdivacky  return 0;
315199481Srdivacky}
316199481Srdivacky
317199989Srdivacky/// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
318199989Srdivacky/// fold the result.  If not, this returns null.
319199989SrdivackyValue *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps,
320199989Srdivacky                             const TargetData *TD) {
321199989Srdivacky  // getelementptr P -> P.
322199989Srdivacky  if (NumOps == 1)
323199989Srdivacky    return Ops[0];
324199989Srdivacky
325199989Srdivacky  // TODO.
326199989Srdivacky  //if (isa<UndefValue>(Ops[0]))
327199989Srdivacky  //  return UndefValue::get(GEP.getType());
328199989Srdivacky
329199989Srdivacky  // getelementptr P, 0 -> P.
330199989Srdivacky  if (NumOps == 2)
331199989Srdivacky    if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
332199989Srdivacky      if (C->isZero())
333199989Srdivacky        return Ops[0];
334199989Srdivacky
335199989Srdivacky  // Check to see if this is constant foldable.
336199989Srdivacky  for (unsigned i = 0; i != NumOps; ++i)
337199989Srdivacky    if (!isa<Constant>(Ops[i]))
338199989Srdivacky      return 0;
339199989Srdivacky
340199989Srdivacky  return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]),
341199989Srdivacky                                        (Constant *const*)Ops+1, NumOps-1);
342199989Srdivacky}
343199989Srdivacky
344199989Srdivacky
345199481Srdivacky//=== Helper functions for higher up the class hierarchy.
346199481Srdivacky
347199481Srdivacky/// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
348199481Srdivacky/// fold the result.  If not, this returns null.
349199481SrdivackyValue *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
350199481Srdivacky                           const TargetData *TD) {
351199481Srdivacky  switch (Opcode) {
352199481Srdivacky  case Instruction::And: return SimplifyAndInst(LHS, RHS, TD);
353199481Srdivacky  case Instruction::Or:  return SimplifyOrInst(LHS, RHS, TD);
354199481Srdivacky  default:
355199481Srdivacky    if (Constant *CLHS = dyn_cast<Constant>(LHS))
356199481Srdivacky      if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
357199481Srdivacky        Constant *COps[] = {CLHS, CRHS};
358199481Srdivacky        return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
359199481Srdivacky      }
360199481Srdivacky    return 0;
361199481Srdivacky  }
362199481Srdivacky}
363199481Srdivacky
364199481Srdivacky/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
365199481Srdivacky/// fold the result.
366199481SrdivackyValue *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
367199481Srdivacky                             const TargetData *TD) {
368199481Srdivacky  if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
369199481Srdivacky    return SimplifyICmpInst(Predicate, LHS, RHS, TD);
370199481Srdivacky  return SimplifyFCmpInst(Predicate, LHS, RHS, TD);
371199481Srdivacky}
372199481Srdivacky
373199481Srdivacky
374199481Srdivacky/// SimplifyInstruction - See if we can compute a simplified version of this
375199481Srdivacky/// instruction.  If not, this returns null.
376199481SrdivackyValue *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD) {
377199481Srdivacky  switch (I->getOpcode()) {
378199481Srdivacky  default:
379199481Srdivacky    return ConstantFoldInstruction(I, TD);
380199989Srdivacky  case Instruction::Add:
381199989Srdivacky    return SimplifyAddInst(I->getOperand(0), I->getOperand(1),
382199989Srdivacky                           cast<BinaryOperator>(I)->hasNoSignedWrap(),
383199989Srdivacky                           cast<BinaryOperator>(I)->hasNoUnsignedWrap(), TD);
384199481Srdivacky  case Instruction::And:
385199481Srdivacky    return SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD);
386199481Srdivacky  case Instruction::Or:
387199481Srdivacky    return SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD);
388199481Srdivacky  case Instruction::ICmp:
389199481Srdivacky    return SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
390199481Srdivacky                            I->getOperand(0), I->getOperand(1), TD);
391199481Srdivacky  case Instruction::FCmp:
392199481Srdivacky    return SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
393199481Srdivacky                            I->getOperand(0), I->getOperand(1), TD);
394199989Srdivacky  case Instruction::GetElementPtr: {
395199989Srdivacky    SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
396199989Srdivacky    return SimplifyGEPInst(&Ops[0], Ops.size(), TD);
397199481Srdivacky  }
398199989Srdivacky  }
399199481Srdivacky}
400199481Srdivacky
401199481Srdivacky/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
402199481Srdivacky/// delete the From instruction.  In addition to a basic RAUW, this does a
403199481Srdivacky/// recursive simplification of the newly formed instructions.  This catches
404199481Srdivacky/// things where one simplification exposes other opportunities.  This only
405199481Srdivacky/// simplifies and deletes scalar operations, it does not change the CFG.
406199481Srdivacky///
407199481Srdivackyvoid llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
408199481Srdivacky                                     const TargetData *TD) {
409199481Srdivacky  assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
410199481Srdivacky
411199481Srdivacky  // FromHandle - This keeps a weakvh on the from value so that we can know if
412199481Srdivacky  // it gets deleted out from under us in a recursive simplification.
413199481Srdivacky  WeakVH FromHandle(From);
414199481Srdivacky
415199481Srdivacky  while (!From->use_empty()) {
416199481Srdivacky    // Update the instruction to use the new value.
417199481Srdivacky    Use &U = From->use_begin().getUse();
418199481Srdivacky    Instruction *User = cast<Instruction>(U.getUser());
419199481Srdivacky    U = To;
420199481Srdivacky
421199481Srdivacky    // See if we can simplify it.
422199481Srdivacky    if (Value *V = SimplifyInstruction(User, TD)) {
423199481Srdivacky      // Recursively simplify this.
424199481Srdivacky      ReplaceAndSimplifyAllUses(User, V, TD);
425199481Srdivacky
426199481Srdivacky      // If the recursive simplification ended up revisiting and deleting 'From'
427199481Srdivacky      // then we're done.
428199481Srdivacky      if (FromHandle == 0)
429199481Srdivacky        return;
430199481Srdivacky    }
431199481Srdivacky  }
432199481Srdivacky  From->eraseFromParent();
433199481Srdivacky}
434199481Srdivacky
435