SCCP.cpp revision 5e5529c74c6bc5f46e272658835dbb5d715b57ca
1//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
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 sparse conditional constant propagation and merging:
11//
12// Specifically, this:
13//   * Assumes values are constant unless proven otherwise
14//   * Assumes BasicBlocks are dead unless proven otherwise
15//   * Proves values to be constant, and replaces them with constants
16//   * Proves conditional branches to be unconditional
17//
18//===----------------------------------------------------------------------===//
19
20#define DEBUG_TYPE "sccp"
21#include "llvm/Transforms/Scalar.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/DenseSet.h"
24#include "llvm/ADT/PointerIntPair.h"
25#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/Analysis/ConstantFolding.h"
29#include "llvm/IR/Constants.h"
30#include "llvm/IR/DataLayout.h"
31#include "llvm/IR/DerivedTypes.h"
32#include "llvm/IR/Instructions.h"
33#include "llvm/InstVisitor.h"
34#include "llvm/Pass.h"
35#include "llvm/Support/CallSite.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
39#include "llvm/Target/TargetLibraryInfo.h"
40#include "llvm/Transforms/IPO.h"
41#include "llvm/Transforms/Utils/Local.h"
42#include <algorithm>
43using namespace llvm;
44
45STATISTIC(NumInstRemoved, "Number of instructions removed");
46STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
47
48STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP");
49STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");
50STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP");
51
52namespace {
53/// LatticeVal class - This class represents the different lattice values that
54/// an LLVM value may occupy.  It is a simple class with value semantics.
55///
56class LatticeVal {
57  enum LatticeValueTy {
58    /// undefined - This LLVM Value has no known value yet.
59    undefined,
60
61    /// constant - This LLVM Value has a specific constant value.
62    constant,
63
64    /// forcedconstant - This LLVM Value was thought to be undef until
65    /// ResolvedUndefsIn.  This is treated just like 'constant', but if merged
66    /// with another (different) constant, it goes to overdefined, instead of
67    /// asserting.
68    forcedconstant,
69
70    /// overdefined - This instruction is not known to be constant, and we know
71    /// it has a value.
72    overdefined
73  };
74
75  /// Val: This stores the current lattice value along with the Constant* for
76  /// the constant if this is a 'constant' or 'forcedconstant' value.
77  PointerIntPair<Constant *, 2, LatticeValueTy> Val;
78
79  LatticeValueTy getLatticeValue() const {
80    return Val.getInt();
81  }
82
83public:
84  LatticeVal() : Val(0, undefined) {}
85
86  bool isUndefined() const { return getLatticeValue() == undefined; }
87  bool isConstant() const {
88    return getLatticeValue() == constant || getLatticeValue() == forcedconstant;
89  }
90  bool isOverdefined() const { return getLatticeValue() == overdefined; }
91
92  Constant *getConstant() const {
93    assert(isConstant() && "Cannot get the constant of a non-constant!");
94    return Val.getPointer();
95  }
96
97  /// markOverdefined - Return true if this is a change in status.
98  bool markOverdefined() {
99    if (isOverdefined())
100      return false;
101
102    Val.setInt(overdefined);
103    return true;
104  }
105
106  /// markConstant - Return true if this is a change in status.
107  bool markConstant(Constant *V) {
108    if (getLatticeValue() == constant) { // Constant but not forcedconstant.
109      assert(getConstant() == V && "Marking constant with different value");
110      return false;
111    }
112
113    if (isUndefined()) {
114      Val.setInt(constant);
115      assert(V && "Marking constant with NULL");
116      Val.setPointer(V);
117    } else {
118      assert(getLatticeValue() == forcedconstant &&
119             "Cannot move from overdefined to constant!");
120      // Stay at forcedconstant if the constant is the same.
121      if (V == getConstant()) return false;
122
123      // Otherwise, we go to overdefined.  Assumptions made based on the
124      // forced value are possibly wrong.  Assuming this is another constant
125      // could expose a contradiction.
126      Val.setInt(overdefined);
127    }
128    return true;
129  }
130
131  /// getConstantInt - If this is a constant with a ConstantInt value, return it
132  /// otherwise return null.
133  ConstantInt *getConstantInt() const {
134    if (isConstant())
135      return dyn_cast<ConstantInt>(getConstant());
136    return 0;
137  }
138
139  void markForcedConstant(Constant *V) {
140    assert(isUndefined() && "Can't force a defined value!");
141    Val.setInt(forcedconstant);
142    Val.setPointer(V);
143  }
144};
145} // end anonymous namespace.
146
147
148namespace {
149
150//===----------------------------------------------------------------------===//
151//
152/// SCCPSolver - This class is a general purpose solver for Sparse Conditional
153/// Constant Propagation.
154///
155class SCCPSolver : public InstVisitor<SCCPSolver> {
156  const DataLayout *TD;
157  const TargetLibraryInfo *TLI;
158  SmallPtrSet<BasicBlock*, 8> BBExecutable; // The BBs that are executable.
159  DenseMap<Value*, LatticeVal> ValueState;  // The state each value is in.
160
161  /// StructValueState - This maintains ValueState for values that have
162  /// StructType, for example for formal arguments, calls, insertelement, etc.
163  ///
164  DenseMap<std::pair<Value*, unsigned>, LatticeVal> StructValueState;
165
166  /// GlobalValue - If we are tracking any values for the contents of a global
167  /// variable, we keep a mapping from the constant accessor to the element of
168  /// the global, to the currently known value.  If the value becomes
169  /// overdefined, it's entry is simply removed from this map.
170  DenseMap<GlobalVariable*, LatticeVal> TrackedGlobals;
171
172  /// TrackedRetVals - If we are tracking arguments into and the return
173  /// value out of a function, it will have an entry in this map, indicating
174  /// what the known return value for the function is.
175  DenseMap<Function*, LatticeVal> TrackedRetVals;
176
177  /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
178  /// that return multiple values.
179  DenseMap<std::pair<Function*, unsigned>, LatticeVal> TrackedMultipleRetVals;
180
181  /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
182  /// represented here for efficient lookup.
183  SmallPtrSet<Function*, 16> MRVFunctionsTracked;
184
185  /// TrackingIncomingArguments - This is the set of functions for whose
186  /// arguments we make optimistic assumptions about and try to prove as
187  /// constants.
188  SmallPtrSet<Function*, 16> TrackingIncomingArguments;
189
190  /// The reason for two worklists is that overdefined is the lowest state
191  /// on the lattice, and moving things to overdefined as fast as possible
192  /// makes SCCP converge much faster.
193  ///
194  /// By having a separate worklist, we accomplish this because everything
195  /// possibly overdefined will become overdefined at the soonest possible
196  /// point.
197  SmallVector<Value*, 64> OverdefinedInstWorkList;
198  SmallVector<Value*, 64> InstWorkList;
199
200
201  SmallVector<BasicBlock*, 64>  BBWorkList;  // The BasicBlock work list
202
203  /// KnownFeasibleEdges - Entries in this set are edges which have already had
204  /// PHI nodes retriggered.
205  typedef std::pair<BasicBlock*, BasicBlock*> Edge;
206  DenseSet<Edge> KnownFeasibleEdges;
207public:
208  SCCPSolver(const DataLayout *td, const TargetLibraryInfo *tli)
209    : TD(td), TLI(tli) {}
210
211  /// MarkBlockExecutable - This method can be used by clients to mark all of
212  /// the blocks that are known to be intrinsically live in the processed unit.
213  ///
214  /// This returns true if the block was not considered live before.
215  bool MarkBlockExecutable(BasicBlock *BB) {
216    if (!BBExecutable.insert(BB)) return false;
217    DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << "\n");
218    BBWorkList.push_back(BB);  // Add the block to the work list!
219    return true;
220  }
221
222  /// TrackValueOfGlobalVariable - Clients can use this method to
223  /// inform the SCCPSolver that it should track loads and stores to the
224  /// specified global variable if it can.  This is only legal to call if
225  /// performing Interprocedural SCCP.
226  void TrackValueOfGlobalVariable(GlobalVariable *GV) {
227    // We only track the contents of scalar globals.
228    if (GV->getType()->getElementType()->isSingleValueType()) {
229      LatticeVal &IV = TrackedGlobals[GV];
230      if (!isa<UndefValue>(GV->getInitializer()))
231        IV.markConstant(GV->getInitializer());
232    }
233  }
234
235  /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
236  /// and out of the specified function (which cannot have its address taken),
237  /// this method must be called.
238  void AddTrackedFunction(Function *F) {
239    // Add an entry, F -> undef.
240    if (StructType *STy = dyn_cast<StructType>(F->getReturnType())) {
241      MRVFunctionsTracked.insert(F);
242      for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
243        TrackedMultipleRetVals.insert(std::make_pair(std::make_pair(F, i),
244                                                     LatticeVal()));
245    } else
246      TrackedRetVals.insert(std::make_pair(F, LatticeVal()));
247  }
248
249  void AddArgumentTrackedFunction(Function *F) {
250    TrackingIncomingArguments.insert(F);
251  }
252
253  /// Solve - Solve for constants and executable blocks.
254  ///
255  void Solve();
256
257  /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
258  /// that branches on undef values cannot reach any of their successors.
259  /// However, this is not a safe assumption.  After we solve dataflow, this
260  /// method should be use to handle this.  If this returns true, the solver
261  /// should be rerun.
262  bool ResolvedUndefsIn(Function &F);
263
264  bool isBlockExecutable(BasicBlock *BB) const {
265    return BBExecutable.count(BB);
266  }
267
268  LatticeVal getLatticeValueFor(Value *V) const {
269    DenseMap<Value*, LatticeVal>::const_iterator I = ValueState.find(V);
270    assert(I != ValueState.end() && "V is not in valuemap!");
271    return I->second;
272  }
273
274  /// getTrackedRetVals - Get the inferred return value map.
275  ///
276  const DenseMap<Function*, LatticeVal> &getTrackedRetVals() {
277    return TrackedRetVals;
278  }
279
280  /// getTrackedGlobals - Get and return the set of inferred initializers for
281  /// global variables.
282  const DenseMap<GlobalVariable*, LatticeVal> &getTrackedGlobals() {
283    return TrackedGlobals;
284  }
285
286  void markOverdefined(Value *V) {
287    assert(!V->getType()->isStructTy() && "Should use other method");
288    markOverdefined(ValueState[V], V);
289  }
290
291  /// markAnythingOverdefined - Mark the specified value overdefined.  This
292  /// works with both scalars and structs.
293  void markAnythingOverdefined(Value *V) {
294    if (StructType *STy = dyn_cast<StructType>(V->getType()))
295      for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
296        markOverdefined(getStructValueState(V, i), V);
297    else
298      markOverdefined(V);
299  }
300
301private:
302  // markConstant - Make a value be marked as "constant".  If the value
303  // is not already a constant, add it to the instruction work list so that
304  // the users of the instruction are updated later.
305  //
306  void markConstant(LatticeVal &IV, Value *V, Constant *C) {
307    if (!IV.markConstant(C)) return;
308    DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
309    if (IV.isOverdefined())
310      OverdefinedInstWorkList.push_back(V);
311    else
312      InstWorkList.push_back(V);
313  }
314
315  void markConstant(Value *V, Constant *C) {
316    assert(!V->getType()->isStructTy() && "Should use other method");
317    markConstant(ValueState[V], V, C);
318  }
319
320  void markForcedConstant(Value *V, Constant *C) {
321    assert(!V->getType()->isStructTy() && "Should use other method");
322    LatticeVal &IV = ValueState[V];
323    IV.markForcedConstant(C);
324    DEBUG(dbgs() << "markForcedConstant: " << *C << ": " << *V << '\n');
325    if (IV.isOverdefined())
326      OverdefinedInstWorkList.push_back(V);
327    else
328      InstWorkList.push_back(V);
329  }
330
331
332  // markOverdefined - Make a value be marked as "overdefined". If the
333  // value is not already overdefined, add it to the overdefined instruction
334  // work list so that the users of the instruction are updated later.
335  void markOverdefined(LatticeVal &IV, Value *V) {
336    if (!IV.markOverdefined()) return;
337
338    DEBUG(dbgs() << "markOverdefined: ";
339          if (Function *F = dyn_cast<Function>(V))
340            dbgs() << "Function '" << F->getName() << "'\n";
341          else
342            dbgs() << *V << '\n');
343    // Only instructions go on the work list
344    OverdefinedInstWorkList.push_back(V);
345  }
346
347  void mergeInValue(LatticeVal &IV, Value *V, LatticeVal MergeWithV) {
348    if (IV.isOverdefined() || MergeWithV.isUndefined())
349      return;  // Noop.
350    if (MergeWithV.isOverdefined())
351      markOverdefined(IV, V);
352    else if (IV.isUndefined())
353      markConstant(IV, V, MergeWithV.getConstant());
354    else if (IV.getConstant() != MergeWithV.getConstant())
355      markOverdefined(IV, V);
356  }
357
358  void mergeInValue(Value *V, LatticeVal MergeWithV) {
359    assert(!V->getType()->isStructTy() && "Should use other method");
360    mergeInValue(ValueState[V], V, MergeWithV);
361  }
362
363
364  /// getValueState - Return the LatticeVal object that corresponds to the
365  /// value.  This function handles the case when the value hasn't been seen yet
366  /// by properly seeding constants etc.
367  LatticeVal &getValueState(Value *V) {
368    assert(!V->getType()->isStructTy() && "Should use getStructValueState");
369
370    std::pair<DenseMap<Value*, LatticeVal>::iterator, bool> I =
371      ValueState.insert(std::make_pair(V, LatticeVal()));
372    LatticeVal &LV = I.first->second;
373
374    if (!I.second)
375      return LV;  // Common case, already in the map.
376
377    if (Constant *C = dyn_cast<Constant>(V)) {
378      // Undef values remain undefined.
379      if (!isa<UndefValue>(V))
380        LV.markConstant(C);          // Constants are constant
381    }
382
383    // All others are underdefined by default.
384    return LV;
385  }
386
387  /// getStructValueState - Return the LatticeVal object that corresponds to the
388  /// value/field pair.  This function handles the case when the value hasn't
389  /// been seen yet by properly seeding constants etc.
390  LatticeVal &getStructValueState(Value *V, unsigned i) {
391    assert(V->getType()->isStructTy() && "Should use getValueState");
392    assert(i < cast<StructType>(V->getType())->getNumElements() &&
393           "Invalid element #");
394
395    std::pair<DenseMap<std::pair<Value*, unsigned>, LatticeVal>::iterator,
396              bool> I = StructValueState.insert(
397                        std::make_pair(std::make_pair(V, i), LatticeVal()));
398    LatticeVal &LV = I.first->second;
399
400    if (!I.second)
401      return LV;  // Common case, already in the map.
402
403    if (Constant *C = dyn_cast<Constant>(V)) {
404      Constant *Elt = C->getAggregateElement(i);
405
406      if (Elt == 0)
407        LV.markOverdefined();      // Unknown sort of constant.
408      else if (isa<UndefValue>(Elt))
409        ; // Undef values remain undefined.
410      else
411        LV.markConstant(Elt);      // Constants are constant.
412    }
413
414    // All others are underdefined by default.
415    return LV;
416  }
417
418
419  /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
420  /// work list if it is not already executable.
421  void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
422    if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
423      return;  // This edge is already known to be executable!
424
425    if (!MarkBlockExecutable(Dest)) {
426      // If the destination is already executable, we just made an *edge*
427      // feasible that wasn't before.  Revisit the PHI nodes in the block
428      // because they have potentially new operands.
429      DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
430            << " -> " << Dest->getName() << "\n");
431
432      PHINode *PN;
433      for (BasicBlock::iterator I = Dest->begin();
434           (PN = dyn_cast<PHINode>(I)); ++I)
435        visitPHINode(*PN);
436    }
437  }
438
439  // getFeasibleSuccessors - Return a vector of booleans to indicate which
440  // successors are reachable from a given terminator instruction.
441  //
442  void getFeasibleSuccessors(TerminatorInst &TI, SmallVector<bool, 16> &Succs);
443
444  // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
445  // block to the 'To' basic block is currently feasible.
446  //
447  bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
448
449  // OperandChangedState - This method is invoked on all of the users of an
450  // instruction that was just changed state somehow.  Based on this
451  // information, we need to update the specified user of this instruction.
452  //
453  void OperandChangedState(Instruction *I) {
454    if (BBExecutable.count(I->getParent()))   // Inst is executable?
455      visit(*I);
456  }
457
458private:
459  friend class InstVisitor<SCCPSolver>;
460
461  // visit implementations - Something changed in this instruction.  Either an
462  // operand made a transition, or the instruction is newly executable.  Change
463  // the value type of I to reflect these changes if appropriate.
464  void visitPHINode(PHINode &I);
465
466  // Terminators
467  void visitReturnInst(ReturnInst &I);
468  void visitTerminatorInst(TerminatorInst &TI);
469
470  void visitCastInst(CastInst &I);
471  void visitSelectInst(SelectInst &I);
472  void visitBinaryOperator(Instruction &I);
473  void visitCmpInst(CmpInst &I);
474  void visitExtractElementInst(ExtractElementInst &I);
475  void visitInsertElementInst(InsertElementInst &I);
476  void visitShuffleVectorInst(ShuffleVectorInst &I);
477  void visitExtractValueInst(ExtractValueInst &EVI);
478  void visitInsertValueInst(InsertValueInst &IVI);
479  void visitLandingPadInst(LandingPadInst &I) { markAnythingOverdefined(&I); }
480
481  // Instructions that cannot be folded away.
482  void visitStoreInst     (StoreInst &I);
483  void visitLoadInst      (LoadInst &I);
484  void visitGetElementPtrInst(GetElementPtrInst &I);
485  void visitCallInst      (CallInst &I) {
486    visitCallSite(&I);
487  }
488  void visitInvokeInst    (InvokeInst &II) {
489    visitCallSite(&II);
490    visitTerminatorInst(II);
491  }
492  void visitCallSite      (CallSite CS);
493  void visitResumeInst    (TerminatorInst &I) { /*returns void*/ }
494  void visitUnwindInst    (TerminatorInst &I) { /*returns void*/ }
495  void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
496  void visitFenceInst     (FenceInst &I) { /*returns void*/ }
497  void visitAtomicCmpXchgInst (AtomicCmpXchgInst &I) { markOverdefined(&I); }
498  void visitAtomicRMWInst (AtomicRMWInst &I) { markOverdefined(&I); }
499  void visitAllocaInst    (Instruction &I) { markOverdefined(&I); }
500  void visitVAArgInst     (Instruction &I) { markAnythingOverdefined(&I); }
501
502  void visitInstruction(Instruction &I) {
503    // If a new instruction is added to LLVM that we don't handle.
504    dbgs() << "SCCP: Don't know how to handle: " << I;
505    markAnythingOverdefined(&I);   // Just in case
506  }
507};
508
509} // end anonymous namespace
510
511
512// getFeasibleSuccessors - Return a vector of booleans to indicate which
513// successors are reachable from a given terminator instruction.
514//
515void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
516                                       SmallVector<bool, 16> &Succs) {
517  Succs.resize(TI.getNumSuccessors());
518  if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
519    if (BI->isUnconditional()) {
520      Succs[0] = true;
521      return;
522    }
523
524    LatticeVal BCValue = getValueState(BI->getCondition());
525    ConstantInt *CI = BCValue.getConstantInt();
526    if (CI == 0) {
527      // Overdefined condition variables, and branches on unfoldable constant
528      // conditions, mean the branch could go either way.
529      if (!BCValue.isUndefined())
530        Succs[0] = Succs[1] = true;
531      return;
532    }
533
534    // Constant condition variables mean the branch can only go a single way.
535    Succs[CI->isZero()] = true;
536    return;
537  }
538
539  if (isa<InvokeInst>(TI)) {
540    // Invoke instructions successors are always executable.
541    Succs[0] = Succs[1] = true;
542    return;
543  }
544
545  if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
546    if (!SI->getNumCases()) {
547      Succs[0] = true;
548      return;
549    }
550    LatticeVal SCValue = getValueState(SI->getCondition());
551    ConstantInt *CI = SCValue.getConstantInt();
552
553    if (CI == 0) {   // Overdefined or undefined condition?
554      // All destinations are executable!
555      if (!SCValue.isUndefined())
556        Succs.assign(TI.getNumSuccessors(), true);
557      return;
558    }
559
560    Succs[SI->findCaseValue(CI).getSuccessorIndex()] = true;
561    return;
562  }
563
564  // TODO: This could be improved if the operand is a [cast of a] BlockAddress.
565  if (isa<IndirectBrInst>(&TI)) {
566    // Just mark all destinations executable!
567    Succs.assign(TI.getNumSuccessors(), true);
568    return;
569  }
570
571#ifndef NDEBUG
572  dbgs() << "Unknown terminator instruction: " << TI << '\n';
573#endif
574  llvm_unreachable("SCCP: Don't know how to handle this terminator!");
575}
576
577
578// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
579// block to the 'To' basic block is currently feasible.
580//
581bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
582  assert(BBExecutable.count(To) && "Dest should always be alive!");
583
584  // Make sure the source basic block is executable!!
585  if (!BBExecutable.count(From)) return false;
586
587  // Check to make sure this edge itself is actually feasible now.
588  TerminatorInst *TI = From->getTerminator();
589  if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
590    if (BI->isUnconditional())
591      return true;
592
593    LatticeVal BCValue = getValueState(BI->getCondition());
594
595    // Overdefined condition variables mean the branch could go either way,
596    // undef conditions mean that neither edge is feasible yet.
597    ConstantInt *CI = BCValue.getConstantInt();
598    if (CI == 0)
599      return !BCValue.isUndefined();
600
601    // Constant condition variables mean the branch can only go a single way.
602    return BI->getSuccessor(CI->isZero()) == To;
603  }
604
605  // Invoke instructions successors are always executable.
606  if (isa<InvokeInst>(TI))
607    return true;
608
609  if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
610    if (SI->getNumCases() < 1)
611      return true;
612
613    LatticeVal SCValue = getValueState(SI->getCondition());
614    ConstantInt *CI = SCValue.getConstantInt();
615
616    if (CI == 0)
617      return !SCValue.isUndefined();
618
619    return SI->findCaseValue(CI).getCaseSuccessor() == To;
620  }
621
622  // Just mark all destinations executable!
623  // TODO: This could be improved if the operand is a [cast of a] BlockAddress.
624  if (isa<IndirectBrInst>(TI))
625    return true;
626
627#ifndef NDEBUG
628  dbgs() << "Unknown terminator instruction: " << *TI << '\n';
629#endif
630  llvm_unreachable(0);
631}
632
633// visit Implementations - Something changed in this instruction, either an
634// operand made a transition, or the instruction is newly executable.  Change
635// the value type of I to reflect these changes if appropriate.  This method
636// makes sure to do the following actions:
637//
638// 1. If a phi node merges two constants in, and has conflicting value coming
639//    from different branches, or if the PHI node merges in an overdefined
640//    value, then the PHI node becomes overdefined.
641// 2. If a phi node merges only constants in, and they all agree on value, the
642//    PHI node becomes a constant value equal to that.
643// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
644// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
645// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
646// 6. If a conditional branch has a value that is constant, make the selected
647//    destination executable
648// 7. If a conditional branch has a value that is overdefined, make all
649//    successors executable.
650//
651void SCCPSolver::visitPHINode(PHINode &PN) {
652  // If this PN returns a struct, just mark the result overdefined.
653  // TODO: We could do a lot better than this if code actually uses this.
654  if (PN.getType()->isStructTy())
655    return markAnythingOverdefined(&PN);
656
657  if (getValueState(&PN).isOverdefined())
658    return;  // Quick exit
659
660  // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
661  // and slow us down a lot.  Just mark them overdefined.
662  if (PN.getNumIncomingValues() > 64)
663    return markOverdefined(&PN);
664
665  // Look at all of the executable operands of the PHI node.  If any of them
666  // are overdefined, the PHI becomes overdefined as well.  If they are all
667  // constant, and they agree with each other, the PHI becomes the identical
668  // constant.  If they are constant and don't agree, the PHI is overdefined.
669  // If there are no executable operands, the PHI remains undefined.
670  //
671  Constant *OperandVal = 0;
672  for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
673    LatticeVal IV = getValueState(PN.getIncomingValue(i));
674    if (IV.isUndefined()) continue;  // Doesn't influence PHI node.
675
676    if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
677      continue;
678
679    if (IV.isOverdefined())    // PHI node becomes overdefined!
680      return markOverdefined(&PN);
681
682    if (OperandVal == 0) {   // Grab the first value.
683      OperandVal = IV.getConstant();
684      continue;
685    }
686
687    // There is already a reachable operand.  If we conflict with it,
688    // then the PHI node becomes overdefined.  If we agree with it, we
689    // can continue on.
690
691    // Check to see if there are two different constants merging, if so, the PHI
692    // node is overdefined.
693    if (IV.getConstant() != OperandVal)
694      return markOverdefined(&PN);
695  }
696
697  // If we exited the loop, this means that the PHI node only has constant
698  // arguments that agree with each other(and OperandVal is the constant) or
699  // OperandVal is null because there are no defined incoming arguments.  If
700  // this is the case, the PHI remains undefined.
701  //
702  if (OperandVal)
703    markConstant(&PN, OperandVal);      // Acquire operand value
704}
705
706void SCCPSolver::visitReturnInst(ReturnInst &I) {
707  if (I.getNumOperands() == 0) return;  // ret void
708
709  Function *F = I.getParent()->getParent();
710  Value *ResultOp = I.getOperand(0);
711
712  // If we are tracking the return value of this function, merge it in.
713  if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
714    DenseMap<Function*, LatticeVal>::iterator TFRVI =
715      TrackedRetVals.find(F);
716    if (TFRVI != TrackedRetVals.end()) {
717      mergeInValue(TFRVI->second, F, getValueState(ResultOp));
718      return;
719    }
720  }
721
722  // Handle functions that return multiple values.
723  if (!TrackedMultipleRetVals.empty()) {
724    if (StructType *STy = dyn_cast<StructType>(ResultOp->getType()))
725      if (MRVFunctionsTracked.count(F))
726        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
727          mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
728                       getStructValueState(ResultOp, i));
729
730  }
731}
732
733void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) {
734  SmallVector<bool, 16> SuccFeasible;
735  getFeasibleSuccessors(TI, SuccFeasible);
736
737  BasicBlock *BB = TI.getParent();
738
739  // Mark all feasible successors executable.
740  for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
741    if (SuccFeasible[i])
742      markEdgeExecutable(BB, TI.getSuccessor(i));
743}
744
745void SCCPSolver::visitCastInst(CastInst &I) {
746  LatticeVal OpSt = getValueState(I.getOperand(0));
747  if (OpSt.isOverdefined())          // Inherit overdefinedness of operand
748    markOverdefined(&I);
749  else if (OpSt.isConstant())        // Propagate constant value
750    markConstant(&I, ConstantExpr::getCast(I.getOpcode(),
751                                           OpSt.getConstant(), I.getType()));
752}
753
754
755void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
756  // If this returns a struct, mark all elements over defined, we don't track
757  // structs in structs.
758  if (EVI.getType()->isStructTy())
759    return markAnythingOverdefined(&EVI);
760
761  // If this is extracting from more than one level of struct, we don't know.
762  if (EVI.getNumIndices() != 1)
763    return markOverdefined(&EVI);
764
765  Value *AggVal = EVI.getAggregateOperand();
766  if (AggVal->getType()->isStructTy()) {
767    unsigned i = *EVI.idx_begin();
768    LatticeVal EltVal = getStructValueState(AggVal, i);
769    mergeInValue(getValueState(&EVI), &EVI, EltVal);
770  } else {
771    // Otherwise, must be extracting from an array.
772    return markOverdefined(&EVI);
773  }
774}
775
776void SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) {
777  StructType *STy = dyn_cast<StructType>(IVI.getType());
778  if (STy == 0)
779    return markOverdefined(&IVI);
780
781  // If this has more than one index, we can't handle it, drive all results to
782  // undef.
783  if (IVI.getNumIndices() != 1)
784    return markAnythingOverdefined(&IVI);
785
786  Value *Aggr = IVI.getAggregateOperand();
787  unsigned Idx = *IVI.idx_begin();
788
789  // Compute the result based on what we're inserting.
790  for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
791    // This passes through all values that aren't the inserted element.
792    if (i != Idx) {
793      LatticeVal EltVal = getStructValueState(Aggr, i);
794      mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
795      continue;
796    }
797
798    Value *Val = IVI.getInsertedValueOperand();
799    if (Val->getType()->isStructTy())
800      // We don't track structs in structs.
801      markOverdefined(getStructValueState(&IVI, i), &IVI);
802    else {
803      LatticeVal InVal = getValueState(Val);
804      mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
805    }
806  }
807}
808
809void SCCPSolver::visitSelectInst(SelectInst &I) {
810  // If this select returns a struct, just mark the result overdefined.
811  // TODO: We could do a lot better than this if code actually uses this.
812  if (I.getType()->isStructTy())
813    return markAnythingOverdefined(&I);
814
815  LatticeVal CondValue = getValueState(I.getCondition());
816  if (CondValue.isUndefined())
817    return;
818
819  if (ConstantInt *CondCB = CondValue.getConstantInt()) {
820    Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
821    mergeInValue(&I, getValueState(OpVal));
822    return;
823  }
824
825  // Otherwise, the condition is overdefined or a constant we can't evaluate.
826  // See if we can produce something better than overdefined based on the T/F
827  // value.
828  LatticeVal TVal = getValueState(I.getTrueValue());
829  LatticeVal FVal = getValueState(I.getFalseValue());
830
831  // select ?, C, C -> C.
832  if (TVal.isConstant() && FVal.isConstant() &&
833      TVal.getConstant() == FVal.getConstant())
834    return markConstant(&I, FVal.getConstant());
835
836  if (TVal.isUndefined())   // select ?, undef, X -> X.
837    return mergeInValue(&I, FVal);
838  if (FVal.isUndefined())   // select ?, X, undef -> X.
839    return mergeInValue(&I, TVal);
840  markOverdefined(&I);
841}
842
843// Handle Binary Operators.
844void SCCPSolver::visitBinaryOperator(Instruction &I) {
845  LatticeVal V1State = getValueState(I.getOperand(0));
846  LatticeVal V2State = getValueState(I.getOperand(1));
847
848  LatticeVal &IV = ValueState[&I];
849  if (IV.isOverdefined()) return;
850
851  if (V1State.isConstant() && V2State.isConstant())
852    return markConstant(IV, &I,
853                        ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
854                                          V2State.getConstant()));
855
856  // If something is undef, wait for it to resolve.
857  if (!V1State.isOverdefined() && !V2State.isOverdefined())
858    return;
859
860  // Otherwise, one of our operands is overdefined.  Try to produce something
861  // better than overdefined with some tricks.
862
863  // If this is an AND or OR with 0 or -1, it doesn't matter that the other
864  // operand is overdefined.
865  if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) {
866    LatticeVal *NonOverdefVal = 0;
867    if (!V1State.isOverdefined())
868      NonOverdefVal = &V1State;
869    else if (!V2State.isOverdefined())
870      NonOverdefVal = &V2State;
871
872    if (NonOverdefVal) {
873      if (NonOverdefVal->isUndefined()) {
874        // Could annihilate value.
875        if (I.getOpcode() == Instruction::And)
876          markConstant(IV, &I, Constant::getNullValue(I.getType()));
877        else if (VectorType *PT = dyn_cast<VectorType>(I.getType()))
878          markConstant(IV, &I, Constant::getAllOnesValue(PT));
879        else
880          markConstant(IV, &I,
881                       Constant::getAllOnesValue(I.getType()));
882        return;
883      }
884
885      if (I.getOpcode() == Instruction::And) {
886        // X and 0 = 0
887        if (NonOverdefVal->getConstant()->isNullValue())
888          return markConstant(IV, &I, NonOverdefVal->getConstant());
889      } else {
890        if (ConstantInt *CI = NonOverdefVal->getConstantInt())
891          if (CI->isAllOnesValue())     // X or -1 = -1
892            return markConstant(IV, &I, NonOverdefVal->getConstant());
893      }
894    }
895  }
896
897
898  markOverdefined(&I);
899}
900
901// Handle ICmpInst instruction.
902void SCCPSolver::visitCmpInst(CmpInst &I) {
903  LatticeVal V1State = getValueState(I.getOperand(0));
904  LatticeVal V2State = getValueState(I.getOperand(1));
905
906  LatticeVal &IV = ValueState[&I];
907  if (IV.isOverdefined()) return;
908
909  if (V1State.isConstant() && V2State.isConstant())
910    return markConstant(IV, &I, ConstantExpr::getCompare(I.getPredicate(),
911                                                         V1State.getConstant(),
912                                                        V2State.getConstant()));
913
914  // If operands are still undefined, wait for it to resolve.
915  if (!V1State.isOverdefined() && !V2State.isOverdefined())
916    return;
917
918  markOverdefined(&I);
919}
920
921void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
922  // TODO : SCCP does not handle vectors properly.
923  return markOverdefined(&I);
924
925#if 0
926  LatticeVal &ValState = getValueState(I.getOperand(0));
927  LatticeVal &IdxState = getValueState(I.getOperand(1));
928
929  if (ValState.isOverdefined() || IdxState.isOverdefined())
930    markOverdefined(&I);
931  else if(ValState.isConstant() && IdxState.isConstant())
932    markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(),
933                                                     IdxState.getConstant()));
934#endif
935}
936
937void SCCPSolver::visitInsertElementInst(InsertElementInst &I) {
938  // TODO : SCCP does not handle vectors properly.
939  return markOverdefined(&I);
940#if 0
941  LatticeVal &ValState = getValueState(I.getOperand(0));
942  LatticeVal &EltState = getValueState(I.getOperand(1));
943  LatticeVal &IdxState = getValueState(I.getOperand(2));
944
945  if (ValState.isOverdefined() || EltState.isOverdefined() ||
946      IdxState.isOverdefined())
947    markOverdefined(&I);
948  else if(ValState.isConstant() && EltState.isConstant() &&
949          IdxState.isConstant())
950    markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(),
951                                                    EltState.getConstant(),
952                                                    IdxState.getConstant()));
953  else if (ValState.isUndefined() && EltState.isConstant() &&
954           IdxState.isConstant())
955    markConstant(&I,ConstantExpr::getInsertElement(UndefValue::get(I.getType()),
956                                                   EltState.getConstant(),
957                                                   IdxState.getConstant()));
958#endif
959}
960
961void SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) {
962  // TODO : SCCP does not handle vectors properly.
963  return markOverdefined(&I);
964#if 0
965  LatticeVal &V1State   = getValueState(I.getOperand(0));
966  LatticeVal &V2State   = getValueState(I.getOperand(1));
967  LatticeVal &MaskState = getValueState(I.getOperand(2));
968
969  if (MaskState.isUndefined() ||
970      (V1State.isUndefined() && V2State.isUndefined()))
971    return;  // Undefined output if mask or both inputs undefined.
972
973  if (V1State.isOverdefined() || V2State.isOverdefined() ||
974      MaskState.isOverdefined()) {
975    markOverdefined(&I);
976  } else {
977    // A mix of constant/undef inputs.
978    Constant *V1 = V1State.isConstant() ?
979        V1State.getConstant() : UndefValue::get(I.getType());
980    Constant *V2 = V2State.isConstant() ?
981        V2State.getConstant() : UndefValue::get(I.getType());
982    Constant *Mask = MaskState.isConstant() ?
983      MaskState.getConstant() : UndefValue::get(I.getOperand(2)->getType());
984    markConstant(&I, ConstantExpr::getShuffleVector(V1, V2, Mask));
985  }
986#endif
987}
988
989// Handle getelementptr instructions.  If all operands are constants then we
990// can turn this into a getelementptr ConstantExpr.
991//
992void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
993  if (ValueState[&I].isOverdefined()) return;
994
995  SmallVector<Constant*, 8> Operands;
996  Operands.reserve(I.getNumOperands());
997
998  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
999    LatticeVal State = getValueState(I.getOperand(i));
1000    if (State.isUndefined())
1001      return;  // Operands are not resolved yet.
1002
1003    if (State.isOverdefined())
1004      return markOverdefined(&I);
1005
1006    assert(State.isConstant() && "Unknown state!");
1007    Operands.push_back(State.getConstant());
1008  }
1009
1010  Constant *Ptr = Operands[0];
1011  ArrayRef<Constant *> Indices(Operands.begin() + 1, Operands.end());
1012  markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Indices));
1013}
1014
1015void SCCPSolver::visitStoreInst(StoreInst &SI) {
1016  // If this store is of a struct, ignore it.
1017  if (SI.getOperand(0)->getType()->isStructTy())
1018    return;
1019
1020  if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1021    return;
1022
1023  GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
1024  DenseMap<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV);
1025  if (I == TrackedGlobals.end() || I->second.isOverdefined()) return;
1026
1027  // Get the value we are storing into the global, then merge it.
1028  mergeInValue(I->second, GV, getValueState(SI.getOperand(0)));
1029  if (I->second.isOverdefined())
1030    TrackedGlobals.erase(I);      // No need to keep tracking this!
1031}
1032
1033
1034// Handle load instructions.  If the operand is a constant pointer to a constant
1035// global, we can replace the load with the loaded constant value!
1036void SCCPSolver::visitLoadInst(LoadInst &I) {
1037  // If this load is of a struct, just mark the result overdefined.
1038  if (I.getType()->isStructTy())
1039    return markAnythingOverdefined(&I);
1040
1041  LatticeVal PtrVal = getValueState(I.getOperand(0));
1042  if (PtrVal.isUndefined()) return;   // The pointer is not resolved yet!
1043
1044  LatticeVal &IV = ValueState[&I];
1045  if (IV.isOverdefined()) return;
1046
1047  if (!PtrVal.isConstant() || I.isVolatile())
1048    return markOverdefined(IV, &I);
1049
1050  Constant *Ptr = PtrVal.getConstant();
1051
1052  // load null -> null
1053  if (isa<ConstantPointerNull>(Ptr) && I.getPointerAddressSpace() == 0)
1054    return markConstant(IV, &I, Constant::getNullValue(I.getType()));
1055
1056  // Transform load (constant global) into the value loaded.
1057  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
1058    if (!TrackedGlobals.empty()) {
1059      // If we are tracking this global, merge in the known value for it.
1060      DenseMap<GlobalVariable*, LatticeVal>::iterator It =
1061        TrackedGlobals.find(GV);
1062      if (It != TrackedGlobals.end()) {
1063        mergeInValue(IV, &I, It->second);
1064        return;
1065      }
1066    }
1067  }
1068
1069  // Transform load from a constant into a constant if possible.
1070  if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, TD))
1071    return markConstant(IV, &I, C);
1072
1073  // Otherwise we cannot say for certain what value this load will produce.
1074  // Bail out.
1075  markOverdefined(IV, &I);
1076}
1077
1078void SCCPSolver::visitCallSite(CallSite CS) {
1079  Function *F = CS.getCalledFunction();
1080  Instruction *I = CS.getInstruction();
1081
1082  // The common case is that we aren't tracking the callee, either because we
1083  // are not doing interprocedural analysis or the callee is indirect, or is
1084  // external.  Handle these cases first.
1085  if (F == 0 || F->isDeclaration()) {
1086CallOverdefined:
1087    // Void return and not tracking callee, just bail.
1088    if (I->getType()->isVoidTy()) return;
1089
1090    // Otherwise, if we have a single return value case, and if the function is
1091    // a declaration, maybe we can constant fold it.
1092    if (F && F->isDeclaration() && !I->getType()->isStructTy() &&
1093        canConstantFoldCallTo(F)) {
1094
1095      SmallVector<Constant*, 8> Operands;
1096      for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
1097           AI != E; ++AI) {
1098        LatticeVal State = getValueState(*AI);
1099
1100        if (State.isUndefined())
1101          return;  // Operands are not resolved yet.
1102        if (State.isOverdefined())
1103          return markOverdefined(I);
1104        assert(State.isConstant() && "Unknown state!");
1105        Operands.push_back(State.getConstant());
1106      }
1107
1108      // If we can constant fold this, mark the result of the call as a
1109      // constant.
1110      if (Constant *C = ConstantFoldCall(F, Operands, TLI))
1111        return markConstant(I, C);
1112    }
1113
1114    // Otherwise, we don't know anything about this call, mark it overdefined.
1115    return markAnythingOverdefined(I);
1116  }
1117
1118  // If this is a local function that doesn't have its address taken, mark its
1119  // entry block executable and merge in the actual arguments to the call into
1120  // the formal arguments of the function.
1121  if (!TrackingIncomingArguments.empty() && TrackingIncomingArguments.count(F)){
1122    MarkBlockExecutable(F->begin());
1123
1124    // Propagate information from this call site into the callee.
1125    CallSite::arg_iterator CAI = CS.arg_begin();
1126    for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1127         AI != E; ++AI, ++CAI) {
1128      // If this argument is byval, and if the function is not readonly, there
1129      // will be an implicit copy formed of the input aggregate.
1130      if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1131        markOverdefined(AI);
1132        continue;
1133      }
1134
1135      if (StructType *STy = dyn_cast<StructType>(AI->getType())) {
1136        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1137          LatticeVal CallArg = getStructValueState(*CAI, i);
1138          mergeInValue(getStructValueState(AI, i), AI, CallArg);
1139        }
1140      } else {
1141        mergeInValue(AI, getValueState(*CAI));
1142      }
1143    }
1144  }
1145
1146  // If this is a single/zero retval case, see if we're tracking the function.
1147  if (StructType *STy = dyn_cast<StructType>(F->getReturnType())) {
1148    if (!MRVFunctionsTracked.count(F))
1149      goto CallOverdefined;  // Not tracking this callee.
1150
1151    // If we are tracking this callee, propagate the result of the function
1152    // into this call site.
1153    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1154      mergeInValue(getStructValueState(I, i), I,
1155                   TrackedMultipleRetVals[std::make_pair(F, i)]);
1156  } else {
1157    DenseMap<Function*, LatticeVal>::iterator TFRVI = TrackedRetVals.find(F);
1158    if (TFRVI == TrackedRetVals.end())
1159      goto CallOverdefined;  // Not tracking this callee.
1160
1161    // If so, propagate the return value of the callee into this call result.
1162    mergeInValue(I, TFRVI->second);
1163  }
1164}
1165
1166void SCCPSolver::Solve() {
1167  // Process the work lists until they are empty!
1168  while (!BBWorkList.empty() || !InstWorkList.empty() ||
1169         !OverdefinedInstWorkList.empty()) {
1170    // Process the overdefined instruction's work list first, which drives other
1171    // things to overdefined more quickly.
1172    while (!OverdefinedInstWorkList.empty()) {
1173      Value *I = OverdefinedInstWorkList.pop_back_val();
1174
1175      DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n');
1176
1177      // "I" got into the work list because it either made the transition from
1178      // bottom to constant, or to overdefined.
1179      //
1180      // Anything on this worklist that is overdefined need not be visited
1181      // since all of its users will have already been marked as overdefined
1182      // Update all of the users of this instruction's value.
1183      //
1184      for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1185           UI != E; ++UI)
1186        if (Instruction *I = dyn_cast<Instruction>(*UI))
1187          OperandChangedState(I);
1188    }
1189
1190    // Process the instruction work list.
1191    while (!InstWorkList.empty()) {
1192      Value *I = InstWorkList.pop_back_val();
1193
1194      DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
1195
1196      // "I" got into the work list because it made the transition from undef to
1197      // constant.
1198      //
1199      // Anything on this worklist that is overdefined need not be visited
1200      // since all of its users will have already been marked as overdefined.
1201      // Update all of the users of this instruction's value.
1202      //
1203      if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
1204        for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1205             UI != E; ++UI)
1206          if (Instruction *I = dyn_cast<Instruction>(*UI))
1207            OperandChangedState(I);
1208    }
1209
1210    // Process the basic block work list.
1211    while (!BBWorkList.empty()) {
1212      BasicBlock *BB = BBWorkList.back();
1213      BBWorkList.pop_back();
1214
1215      DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
1216
1217      // Notify all instructions in this basic block that they are newly
1218      // executable.
1219      visit(BB);
1220    }
1221  }
1222}
1223
1224/// ResolvedUndefsIn - While solving the dataflow for a function, we assume
1225/// that branches on undef values cannot reach any of their successors.
1226/// However, this is not a safe assumption.  After we solve dataflow, this
1227/// method should be use to handle this.  If this returns true, the solver
1228/// should be rerun.
1229///
1230/// This method handles this by finding an unresolved branch and marking it one
1231/// of the edges from the block as being feasible, even though the condition
1232/// doesn't say it would otherwise be.  This allows SCCP to find the rest of the
1233/// CFG and only slightly pessimizes the analysis results (by marking one,
1234/// potentially infeasible, edge feasible).  This cannot usefully modify the
1235/// constraints on the condition of the branch, as that would impact other users
1236/// of the value.
1237///
1238/// This scan also checks for values that use undefs, whose results are actually
1239/// defined.  For example, 'zext i8 undef to i32' should produce all zeros
1240/// conservatively, as "(zext i8 X -> i32) & 0xFF00" must always return zero,
1241/// even if X isn't defined.
1242bool SCCPSolver::ResolvedUndefsIn(Function &F) {
1243  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1244    if (!BBExecutable.count(BB))
1245      continue;
1246
1247    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1248      // Look for instructions which produce undef values.
1249      if (I->getType()->isVoidTy()) continue;
1250
1251      if (StructType *STy = dyn_cast<StructType>(I->getType())) {
1252        // Only a few things that can be structs matter for undef.
1253
1254        // Tracked calls must never be marked overdefined in ResolvedUndefsIn.
1255        if (CallSite CS = CallSite(I))
1256          if (Function *F = CS.getCalledFunction())
1257            if (MRVFunctionsTracked.count(F))
1258              continue;
1259
1260        // extractvalue and insertvalue don't need to be marked; they are
1261        // tracked as precisely as their operands.
1262        if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I))
1263          continue;
1264
1265        // Send the results of everything else to overdefined.  We could be
1266        // more precise than this but it isn't worth bothering.
1267        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1268          LatticeVal &LV = getStructValueState(I, i);
1269          if (LV.isUndefined())
1270            markOverdefined(LV, I);
1271        }
1272        continue;
1273      }
1274
1275      LatticeVal &LV = getValueState(I);
1276      if (!LV.isUndefined()) continue;
1277
1278      // extractvalue is safe; check here because the argument is a struct.
1279      if (isa<ExtractValueInst>(I))
1280        continue;
1281
1282      // Compute the operand LatticeVals, for convenience below.
1283      // Anything taking a struct is conservatively assumed to require
1284      // overdefined markings.
1285      if (I->getOperand(0)->getType()->isStructTy()) {
1286        markOverdefined(I);
1287        return true;
1288      }
1289      LatticeVal Op0LV = getValueState(I->getOperand(0));
1290      LatticeVal Op1LV;
1291      if (I->getNumOperands() == 2) {
1292        if (I->getOperand(1)->getType()->isStructTy()) {
1293          markOverdefined(I);
1294          return true;
1295        }
1296
1297        Op1LV = getValueState(I->getOperand(1));
1298      }
1299      // If this is an instructions whose result is defined even if the input is
1300      // not fully defined, propagate the information.
1301      Type *ITy = I->getType();
1302      switch (I->getOpcode()) {
1303      case Instruction::Add:
1304      case Instruction::Sub:
1305      case Instruction::Trunc:
1306      case Instruction::FPTrunc:
1307      case Instruction::BitCast:
1308        break; // Any undef -> undef
1309      case Instruction::FSub:
1310      case Instruction::FAdd:
1311      case Instruction::FMul:
1312      case Instruction::FDiv:
1313      case Instruction::FRem:
1314        // Floating-point binary operation: be conservative.
1315        if (Op0LV.isUndefined() && Op1LV.isUndefined())
1316          markForcedConstant(I, Constant::getNullValue(ITy));
1317        else
1318          markOverdefined(I);
1319        return true;
1320      case Instruction::ZExt:
1321      case Instruction::SExt:
1322      case Instruction::FPToUI:
1323      case Instruction::FPToSI:
1324      case Instruction::FPExt:
1325      case Instruction::PtrToInt:
1326      case Instruction::IntToPtr:
1327      case Instruction::SIToFP:
1328      case Instruction::UIToFP:
1329        // undef -> 0; some outputs are impossible
1330        markForcedConstant(I, Constant::getNullValue(ITy));
1331        return true;
1332      case Instruction::Mul:
1333      case Instruction::And:
1334        // Both operands undef -> undef
1335        if (Op0LV.isUndefined() && Op1LV.isUndefined())
1336          break;
1337        // undef * X -> 0.   X could be zero.
1338        // undef & X -> 0.   X could be zero.
1339        markForcedConstant(I, Constant::getNullValue(ITy));
1340        return true;
1341
1342      case Instruction::Or:
1343        // Both operands undef -> undef
1344        if (Op0LV.isUndefined() && Op1LV.isUndefined())
1345          break;
1346        // undef | X -> -1.   X could be -1.
1347        markForcedConstant(I, Constant::getAllOnesValue(ITy));
1348        return true;
1349
1350      case Instruction::Xor:
1351        // undef ^ undef -> 0; strictly speaking, this is not strictly
1352        // necessary, but we try to be nice to people who expect this
1353        // behavior in simple cases
1354        if (Op0LV.isUndefined() && Op1LV.isUndefined()) {
1355          markForcedConstant(I, Constant::getNullValue(ITy));
1356          return true;
1357        }
1358        // undef ^ X -> undef
1359        break;
1360
1361      case Instruction::SDiv:
1362      case Instruction::UDiv:
1363      case Instruction::SRem:
1364      case Instruction::URem:
1365        // X / undef -> undef.  No change.
1366        // X % undef -> undef.  No change.
1367        if (Op1LV.isUndefined()) break;
1368
1369        // undef / X -> 0.   X could be maxint.
1370        // undef % X -> 0.   X could be 1.
1371        markForcedConstant(I, Constant::getNullValue(ITy));
1372        return true;
1373
1374      case Instruction::AShr:
1375        // X >>a undef -> undef.
1376        if (Op1LV.isUndefined()) break;
1377
1378        // undef >>a X -> all ones
1379        markForcedConstant(I, Constant::getAllOnesValue(ITy));
1380        return true;
1381      case Instruction::LShr:
1382      case Instruction::Shl:
1383        // X << undef -> undef.
1384        // X >> undef -> undef.
1385        if (Op1LV.isUndefined()) break;
1386
1387        // undef << X -> 0
1388        // undef >> X -> 0
1389        markForcedConstant(I, Constant::getNullValue(ITy));
1390        return true;
1391      case Instruction::Select:
1392        Op1LV = getValueState(I->getOperand(1));
1393        // undef ? X : Y  -> X or Y.  There could be commonality between X/Y.
1394        if (Op0LV.isUndefined()) {
1395          if (!Op1LV.isConstant())  // Pick the constant one if there is any.
1396            Op1LV = getValueState(I->getOperand(2));
1397        } else if (Op1LV.isUndefined()) {
1398          // c ? undef : undef -> undef.  No change.
1399          Op1LV = getValueState(I->getOperand(2));
1400          if (Op1LV.isUndefined())
1401            break;
1402          // Otherwise, c ? undef : x -> x.
1403        } else {
1404          // Leave Op1LV as Operand(1)'s LatticeValue.
1405        }
1406
1407        if (Op1LV.isConstant())
1408          markForcedConstant(I, Op1LV.getConstant());
1409        else
1410          markOverdefined(I);
1411        return true;
1412      case Instruction::Load:
1413        // A load here means one of two things: a load of undef from a global,
1414        // a load from an unknown pointer.  Either way, having it return undef
1415        // is okay.
1416        break;
1417      case Instruction::ICmp:
1418        // X == undef -> undef.  Other comparisons get more complicated.
1419        if (cast<ICmpInst>(I)->isEquality())
1420          break;
1421        markOverdefined(I);
1422        return true;
1423      case Instruction::Call:
1424      case Instruction::Invoke: {
1425        // There are two reasons a call can have an undef result
1426        // 1. It could be tracked.
1427        // 2. It could be constant-foldable.
1428        // Because of the way we solve return values, tracked calls must
1429        // never be marked overdefined in ResolvedUndefsIn.
1430        if (Function *F = CallSite(I).getCalledFunction())
1431          if (TrackedRetVals.count(F))
1432            break;
1433
1434        // If the call is constant-foldable, we mark it overdefined because
1435        // we do not know what return values are valid.
1436        markOverdefined(I);
1437        return true;
1438      }
1439      default:
1440        // If we don't know what should happen here, conservatively mark it
1441        // overdefined.
1442        markOverdefined(I);
1443        return true;
1444      }
1445    }
1446
1447    // Check to see if we have a branch or switch on an undefined value.  If so
1448    // we force the branch to go one way or the other to make the successor
1449    // values live.  It doesn't really matter which way we force it.
1450    TerminatorInst *TI = BB->getTerminator();
1451    if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1452      if (!BI->isConditional()) continue;
1453      if (!getValueState(BI->getCondition()).isUndefined())
1454        continue;
1455
1456      // If the input to SCCP is actually branch on undef, fix the undef to
1457      // false.
1458      if (isa<UndefValue>(BI->getCondition())) {
1459        BI->setCondition(ConstantInt::getFalse(BI->getContext()));
1460        markEdgeExecutable(BB, TI->getSuccessor(1));
1461        return true;
1462      }
1463
1464      // Otherwise, it is a branch on a symbolic value which is currently
1465      // considered to be undef.  Handle this by forcing the input value to the
1466      // branch to false.
1467      markForcedConstant(BI->getCondition(),
1468                         ConstantInt::getFalse(TI->getContext()));
1469      return true;
1470    }
1471
1472    if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
1473      if (!SI->getNumCases())
1474        continue;
1475      if (!getValueState(SI->getCondition()).isUndefined())
1476        continue;
1477
1478      // If the input to SCCP is actually switch on undef, fix the undef to
1479      // the first constant.
1480      if (isa<UndefValue>(SI->getCondition())) {
1481        SI->setCondition(SI->case_begin().getCaseValue());
1482        markEdgeExecutable(BB, SI->case_begin().getCaseSuccessor());
1483        return true;
1484      }
1485
1486      markForcedConstant(SI->getCondition(), SI->case_begin().getCaseValue());
1487      return true;
1488    }
1489  }
1490
1491  return false;
1492}
1493
1494
1495namespace {
1496  //===--------------------------------------------------------------------===//
1497  //
1498  /// SCCP Class - This class uses the SCCPSolver to implement a per-function
1499  /// Sparse Conditional Constant Propagator.
1500  ///
1501  struct SCCP : public FunctionPass {
1502    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1503      AU.addRequired<TargetLibraryInfo>();
1504    }
1505    static char ID; // Pass identification, replacement for typeid
1506    SCCP() : FunctionPass(ID) {
1507      initializeSCCPPass(*PassRegistry::getPassRegistry());
1508    }
1509
1510    // runOnFunction - Run the Sparse Conditional Constant Propagation
1511    // algorithm, and return true if the function was modified.
1512    //
1513    bool runOnFunction(Function &F);
1514  };
1515} // end anonymous namespace
1516
1517char SCCP::ID = 0;
1518INITIALIZE_PASS(SCCP, "sccp",
1519                "Sparse Conditional Constant Propagation", false, false)
1520
1521// createSCCPPass - This is the public interface to this file.
1522FunctionPass *llvm::createSCCPPass() {
1523  return new SCCP();
1524}
1525
1526static void DeleteInstructionInBlock(BasicBlock *BB) {
1527  DEBUG(dbgs() << "  BasicBlock Dead:" << *BB);
1528  ++NumDeadBlocks;
1529
1530  // Check to see if there are non-terminating instructions to delete.
1531  if (isa<TerminatorInst>(BB->begin()))
1532    return;
1533
1534  // Delete the instructions backwards, as it has a reduced likelihood of having
1535  // to update as many def-use and use-def chains.
1536  Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
1537  while (EndInst != BB->begin()) {
1538    // Delete the next to last instruction.
1539    BasicBlock::iterator I = EndInst;
1540    Instruction *Inst = --I;
1541    if (!Inst->use_empty())
1542      Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
1543    if (isa<LandingPadInst>(Inst)) {
1544      EndInst = Inst;
1545      continue;
1546    }
1547    BB->getInstList().erase(Inst);
1548    ++NumInstRemoved;
1549  }
1550}
1551
1552// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
1553// and return true if the function was modified.
1554//
1555bool SCCP::runOnFunction(Function &F) {
1556  DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n");
1557  const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
1558  const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
1559  SCCPSolver Solver(TD, TLI);
1560
1561  // Mark the first block of the function as being executable.
1562  Solver.MarkBlockExecutable(F.begin());
1563
1564  // Mark all arguments to the function as being overdefined.
1565  for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
1566    Solver.markAnythingOverdefined(AI);
1567
1568  // Solve for constants.
1569  bool ResolvedUndefs = true;
1570  while (ResolvedUndefs) {
1571    Solver.Solve();
1572    DEBUG(dbgs() << "RESOLVING UNDEFs\n");
1573    ResolvedUndefs = Solver.ResolvedUndefsIn(F);
1574  }
1575
1576  bool MadeChanges = false;
1577
1578  // If we decided that there are basic blocks that are dead in this function,
1579  // delete their contents now.  Note that we cannot actually delete the blocks,
1580  // as we cannot modify the CFG of the function.
1581
1582  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1583    if (!Solver.isBlockExecutable(BB)) {
1584      DeleteInstructionInBlock(BB);
1585      MadeChanges = true;
1586      continue;
1587    }
1588
1589    // Iterate over all of the instructions in a function, replacing them with
1590    // constants if we have found them to be of constant values.
1591    //
1592    for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1593      Instruction *Inst = BI++;
1594      if (Inst->getType()->isVoidTy() || isa<TerminatorInst>(Inst))
1595        continue;
1596
1597      // TODO: Reconstruct structs from their elements.
1598      if (Inst->getType()->isStructTy())
1599        continue;
1600
1601      LatticeVal IV = Solver.getLatticeValueFor(Inst);
1602      if (IV.isOverdefined())
1603        continue;
1604
1605      Constant *Const = IV.isConstant()
1606        ? IV.getConstant() : UndefValue::get(Inst->getType());
1607      DEBUG(dbgs() << "  Constant: " << *Const << " = " << *Inst);
1608
1609      // Replaces all of the uses of a variable with uses of the constant.
1610      Inst->replaceAllUsesWith(Const);
1611
1612      // Delete the instruction.
1613      Inst->eraseFromParent();
1614
1615      // Hey, we just changed something!
1616      MadeChanges = true;
1617      ++NumInstRemoved;
1618    }
1619  }
1620
1621  return MadeChanges;
1622}
1623
1624namespace {
1625  //===--------------------------------------------------------------------===//
1626  //
1627  /// IPSCCP Class - This class implements interprocedural Sparse Conditional
1628  /// Constant Propagation.
1629  ///
1630  struct IPSCCP : public ModulePass {
1631    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1632      AU.addRequired<TargetLibraryInfo>();
1633    }
1634    static char ID;
1635    IPSCCP() : ModulePass(ID) {
1636      initializeIPSCCPPass(*PassRegistry::getPassRegistry());
1637    }
1638    bool runOnModule(Module &M);
1639  };
1640} // end anonymous namespace
1641
1642char IPSCCP::ID = 0;
1643INITIALIZE_PASS_BEGIN(IPSCCP, "ipsccp",
1644                "Interprocedural Sparse Conditional Constant Propagation",
1645                false, false)
1646INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
1647INITIALIZE_PASS_END(IPSCCP, "ipsccp",
1648                "Interprocedural Sparse Conditional Constant Propagation",
1649                false, false)
1650
1651// createIPSCCPPass - This is the public interface to this file.
1652ModulePass *llvm::createIPSCCPPass() {
1653  return new IPSCCP();
1654}
1655
1656
1657static bool AddressIsTaken(const GlobalValue *GV) {
1658  // Delete any dead constantexpr klingons.
1659  GV->removeDeadConstantUsers();
1660
1661  for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end();
1662       UI != E; ++UI) {
1663    const User *U = *UI;
1664    if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
1665      if (SI->getOperand(0) == GV || SI->isVolatile())
1666        return true;  // Storing addr of GV.
1667    } else if (isa<InvokeInst>(U) || isa<CallInst>(U)) {
1668      // Make sure we are calling the function, not passing the address.
1669      ImmutableCallSite CS(cast<Instruction>(U));
1670      if (!CS.isCallee(UI))
1671        return true;
1672    } else if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
1673      if (LI->isVolatile())
1674        return true;
1675    } else if (isa<BlockAddress>(U)) {
1676      // blockaddress doesn't take the address of the function, it takes addr
1677      // of label.
1678    } else {
1679      return true;
1680    }
1681  }
1682  return false;
1683}
1684
1685bool IPSCCP::runOnModule(Module &M) {
1686  const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
1687  const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
1688  SCCPSolver Solver(TD, TLI);
1689
1690  // AddressTakenFunctions - This set keeps track of the address-taken functions
1691  // that are in the input.  As IPSCCP runs through and simplifies code,
1692  // functions that were address taken can end up losing their
1693  // address-taken-ness.  Because of this, we keep track of their addresses from
1694  // the first pass so we can use them for the later simplification pass.
1695  SmallPtrSet<Function*, 32> AddressTakenFunctions;
1696
1697  // Loop over all functions, marking arguments to those with their addresses
1698  // taken or that are external as overdefined.
1699  //
1700  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
1701    if (F->isDeclaration())
1702      continue;
1703
1704    // If this is a strong or ODR definition of this function, then we can
1705    // propagate information about its result into callsites of it.
1706    if (!F->mayBeOverridden())
1707      Solver.AddTrackedFunction(F);
1708
1709    // If this function only has direct calls that we can see, we can track its
1710    // arguments and return value aggressively, and can assume it is not called
1711    // unless we see evidence to the contrary.
1712    if (F->hasLocalLinkage()) {
1713      if (AddressIsTaken(F))
1714        AddressTakenFunctions.insert(F);
1715      else {
1716        Solver.AddArgumentTrackedFunction(F);
1717        continue;
1718      }
1719    }
1720
1721    // Assume the function is called.
1722    Solver.MarkBlockExecutable(F->begin());
1723
1724    // Assume nothing about the incoming arguments.
1725    for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1726         AI != E; ++AI)
1727      Solver.markAnythingOverdefined(AI);
1728  }
1729
1730  // Loop over global variables.  We inform the solver about any internal global
1731  // variables that do not have their 'addresses taken'.  If they don't have
1732  // their addresses taken, we can propagate constants through them.
1733  for (Module::global_iterator G = M.global_begin(), E = M.global_end();
1734       G != E; ++G)
1735    if (!G->isConstant() && G->hasLocalLinkage() && !AddressIsTaken(G))
1736      Solver.TrackValueOfGlobalVariable(G);
1737
1738  // Solve for constants.
1739  bool ResolvedUndefs = true;
1740  while (ResolvedUndefs) {
1741    Solver.Solve();
1742
1743    DEBUG(dbgs() << "RESOLVING UNDEFS\n");
1744    ResolvedUndefs = false;
1745    for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
1746      ResolvedUndefs |= Solver.ResolvedUndefsIn(*F);
1747  }
1748
1749  bool MadeChanges = false;
1750
1751  // Iterate over all of the instructions in the module, replacing them with
1752  // constants if we have found them to be of constant values.
1753  //
1754  SmallVector<BasicBlock*, 512> BlocksToErase;
1755
1756  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
1757    if (Solver.isBlockExecutable(F->begin())) {
1758      for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1759           AI != E; ++AI) {
1760        if (AI->use_empty() || AI->getType()->isStructTy()) continue;
1761
1762        // TODO: Could use getStructLatticeValueFor to find out if the entire
1763        // result is a constant and replace it entirely if so.
1764
1765        LatticeVal IV = Solver.getLatticeValueFor(AI);
1766        if (IV.isOverdefined()) continue;
1767
1768        Constant *CST = IV.isConstant() ?
1769        IV.getConstant() : UndefValue::get(AI->getType());
1770        DEBUG(dbgs() << "***  Arg " << *AI << " = " << *CST <<"\n");
1771
1772        // Replaces all of the uses of a variable with uses of the
1773        // constant.
1774        AI->replaceAllUsesWith(CST);
1775        ++IPNumArgsElimed;
1776      }
1777    }
1778
1779    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
1780      if (!Solver.isBlockExecutable(BB)) {
1781        DeleteInstructionInBlock(BB);
1782        MadeChanges = true;
1783
1784        TerminatorInst *TI = BB->getTerminator();
1785        for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1786          BasicBlock *Succ = TI->getSuccessor(i);
1787          if (!Succ->empty() && isa<PHINode>(Succ->begin()))
1788            TI->getSuccessor(i)->removePredecessor(BB);
1789        }
1790        if (!TI->use_empty())
1791          TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
1792        TI->eraseFromParent();
1793
1794        if (&*BB != &F->front())
1795          BlocksToErase.push_back(BB);
1796        else
1797          new UnreachableInst(M.getContext(), BB);
1798        continue;
1799      }
1800
1801      for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1802        Instruction *Inst = BI++;
1803        if (Inst->getType()->isVoidTy() || Inst->getType()->isStructTy())
1804          continue;
1805
1806        // TODO: Could use getStructLatticeValueFor to find out if the entire
1807        // result is a constant and replace it entirely if so.
1808
1809        LatticeVal IV = Solver.getLatticeValueFor(Inst);
1810        if (IV.isOverdefined())
1811          continue;
1812
1813        Constant *Const = IV.isConstant()
1814          ? IV.getConstant() : UndefValue::get(Inst->getType());
1815        DEBUG(dbgs() << "  Constant: " << *Const << " = " << *Inst);
1816
1817        // Replaces all of the uses of a variable with uses of the
1818        // constant.
1819        Inst->replaceAllUsesWith(Const);
1820
1821        // Delete the instruction.
1822        if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst))
1823          Inst->eraseFromParent();
1824
1825        // Hey, we just changed something!
1826        MadeChanges = true;
1827        ++IPNumInstRemoved;
1828      }
1829    }
1830
1831    // Now that all instructions in the function are constant folded, erase dead
1832    // blocks, because we can now use ConstantFoldTerminator to get rid of
1833    // in-edges.
1834    for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
1835      // If there are any PHI nodes in this successor, drop entries for BB now.
1836      BasicBlock *DeadBB = BlocksToErase[i];
1837      for (Value::use_iterator UI = DeadBB->use_begin(), UE = DeadBB->use_end();
1838           UI != UE; ) {
1839        // Grab the user and then increment the iterator early, as the user
1840        // will be deleted. Step past all adjacent uses from the same user.
1841        Instruction *I = dyn_cast<Instruction>(*UI);
1842        do { ++UI; } while (UI != UE && *UI == I);
1843
1844        // Ignore blockaddress users; BasicBlock's dtor will handle them.
1845        if (!I) continue;
1846
1847        bool Folded = ConstantFoldTerminator(I->getParent());
1848        if (!Folded) {
1849          // The constant folder may not have been able to fold the terminator
1850          // if this is a branch or switch on undef.  Fold it manually as a
1851          // branch to the first successor.
1852#ifndef NDEBUG
1853          if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
1854            assert(BI->isConditional() && isa<UndefValue>(BI->getCondition()) &&
1855                   "Branch should be foldable!");
1856          } else if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
1857            assert(isa<UndefValue>(SI->getCondition()) && "Switch should fold");
1858          } else {
1859            llvm_unreachable("Didn't fold away reference to block!");
1860          }
1861#endif
1862
1863          // Make this an uncond branch to the first successor.
1864          TerminatorInst *TI = I->getParent()->getTerminator();
1865          BranchInst::Create(TI->getSuccessor(0), TI);
1866
1867          // Remove entries in successor phi nodes to remove edges.
1868          for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i)
1869            TI->getSuccessor(i)->removePredecessor(TI->getParent());
1870
1871          // Remove the old terminator.
1872          TI->eraseFromParent();
1873        }
1874      }
1875
1876      // Finally, delete the basic block.
1877      F->getBasicBlockList().erase(DeadBB);
1878    }
1879    BlocksToErase.clear();
1880  }
1881
1882  // If we inferred constant or undef return values for a function, we replaced
1883  // all call uses with the inferred value.  This means we don't need to bother
1884  // actually returning anything from the function.  Replace all return
1885  // instructions with return undef.
1886  //
1887  // Do this in two stages: first identify the functions we should process, then
1888  // actually zap their returns.  This is important because we can only do this
1889  // if the address of the function isn't taken.  In cases where a return is the
1890  // last use of a function, the order of processing functions would affect
1891  // whether other functions are optimizable.
1892  SmallVector<ReturnInst*, 8> ReturnsToZap;
1893
1894  // TODO: Process multiple value ret instructions also.
1895  const DenseMap<Function*, LatticeVal> &RV = Solver.getTrackedRetVals();
1896  for (DenseMap<Function*, LatticeVal>::const_iterator I = RV.begin(),
1897       E = RV.end(); I != E; ++I) {
1898    Function *F = I->first;
1899    if (I->second.isOverdefined() || F->getReturnType()->isVoidTy())
1900      continue;
1901
1902    // We can only do this if we know that nothing else can call the function.
1903    if (!F->hasLocalLinkage() || AddressTakenFunctions.count(F))
1904      continue;
1905
1906    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1907      if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
1908        if (!isa<UndefValue>(RI->getOperand(0)))
1909          ReturnsToZap.push_back(RI);
1910  }
1911
1912  // Zap all returns which we've identified as zap to change.
1913  for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) {
1914    Function *F = ReturnsToZap[i]->getParent()->getParent();
1915    ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType()));
1916  }
1917
1918  // If we inferred constant or undef values for globals variables, we can
1919  // delete the global and any stores that remain to it.
1920  const DenseMap<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals();
1921  for (DenseMap<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(),
1922         E = TG.end(); I != E; ++I) {
1923    GlobalVariable *GV = I->first;
1924    assert(!I->second.isOverdefined() &&
1925           "Overdefined values should have been taken out of the map!");
1926    DEBUG(dbgs() << "Found that GV '" << GV->getName() << "' is constant!\n");
1927    while (!GV->use_empty()) {
1928      StoreInst *SI = cast<StoreInst>(GV->use_back());
1929      SI->eraseFromParent();
1930    }
1931    M.getGlobalList().erase(GV);
1932    ++IPNumGlobalConst;
1933  }
1934
1935  return MadeChanges;
1936}
1937