Added lower packed support for the extractelement operation.
[oota-llvm.git] / lib / Transforms / Scalar / SCCP.cpp
1 //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 // Notice that:
19 //   * This pass has a habit of making definitions be dead.  It is a good idea
20 //     to to run a DCE pass sometime after running this pass.
21 //
22 //===----------------------------------------------------------------------===//
23
24 #define DEBUG_TYPE "sccp"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Transforms/IPO.h"
27 #include "llvm/Constants.h"
28 #include "llvm/DerivedTypes.h"
29 #include "llvm/Instructions.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Support/InstVisitor.h"
32 #include "llvm/Transforms/Utils/Local.h"
33 #include "llvm/Support/CallSite.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/ADT/hash_map"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/ADT/STLExtras.h"
38 #include <algorithm>
39 #include <set>
40 using namespace llvm;
41
42 // LatticeVal class - This class represents the different lattice values that an
43 // instruction may occupy.  It is a simple class with value semantics.
44 //
45 namespace {
46
47 class LatticeVal {
48   enum {
49     undefined,           // This instruction has no known value
50     constant,            // This instruction has a constant value
51     overdefined          // This instruction has an unknown value
52   } LatticeValue;        // The current lattice position
53   Constant *ConstantVal; // If Constant value, the current value
54 public:
55   inline LatticeVal() : LatticeValue(undefined), ConstantVal(0) {}
56
57   // markOverdefined - Return true if this is a new status to be in...
58   inline bool markOverdefined() {
59     if (LatticeValue != overdefined) {
60       LatticeValue = overdefined;
61       return true;
62     }
63     return false;
64   }
65
66   // markConstant - Return true if this is a new status for us...
67   inline bool markConstant(Constant *V) {
68     if (LatticeValue != constant) {
69       LatticeValue = constant;
70       ConstantVal = V;
71       return true;
72     } else {
73       assert(ConstantVal == V && "Marking constant with different value");
74     }
75     return false;
76   }
77
78   inline bool isUndefined()   const { return LatticeValue == undefined; }
79   inline bool isConstant()    const { return LatticeValue == constant; }
80   inline bool isOverdefined() const { return LatticeValue == overdefined; }
81
82   inline Constant *getConstant() const {
83     assert(isConstant() && "Cannot get the constant of a non-constant!");
84     return ConstantVal;
85   }
86 };
87
88 } // end anonymous namespace
89
90
91 //===----------------------------------------------------------------------===//
92 //
93 /// SCCPSolver - This class is a general purpose solver for Sparse Conditional
94 /// Constant Propagation.
95 ///
96 class SCCPSolver : public InstVisitor<SCCPSolver> {
97   std::set<BasicBlock*>     BBExecutable;// The basic blocks that are executable
98   hash_map<Value*, LatticeVal> ValueState;  // The state each value is in...
99
100   /// GlobalValue - If we are tracking any values for the contents of a global
101   /// variable, we keep a mapping from the constant accessor to the element of
102   /// the global, to the currently known value.  If the value becomes
103   /// overdefined, it's entry is simply removed from this map.
104   hash_map<GlobalVariable*, LatticeVal> TrackedGlobals;
105
106   /// TrackedFunctionRetVals - If we are tracking arguments into and the return
107   /// value out of a function, it will have an entry in this map, indicating
108   /// what the known return value for the function is.
109   hash_map<Function*, LatticeVal> TrackedFunctionRetVals;
110
111   // The reason for two worklists is that overdefined is the lowest state
112   // on the lattice, and moving things to overdefined as fast as possible
113   // makes SCCP converge much faster.
114   // By having a separate worklist, we accomplish this because everything
115   // possibly overdefined will become overdefined at the soonest possible
116   // point.
117   std::vector<Value*> OverdefinedInstWorkList;
118   std::vector<Value*> InstWorkList;
119
120
121   std::vector<BasicBlock*>  BBWorkList;  // The BasicBlock work list
122
123   /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not
124   /// overdefined, despite the fact that the PHI node is overdefined.
125   std::multimap<PHINode*, Instruction*> UsersOfOverdefinedPHIs;
126
127   /// KnownFeasibleEdges - Entries in this set are edges which have already had
128   /// PHI nodes retriggered.
129   typedef std::pair<BasicBlock*,BasicBlock*> Edge;
130   std::set<Edge> KnownFeasibleEdges;
131 public:
132
133   /// MarkBlockExecutable - This method can be used by clients to mark all of
134   /// the blocks that are known to be intrinsically live in the processed unit.
135   void MarkBlockExecutable(BasicBlock *BB) {
136     DEBUG(std::cerr << "Marking Block Executable: " << BB->getName() << "\n");
137     BBExecutable.insert(BB);   // Basic block is executable!
138     BBWorkList.push_back(BB);  // Add the block to the work list!
139   }
140
141   /// TrackValueOfGlobalVariable - Clients can use this method to
142   /// inform the SCCPSolver that it should track loads and stores to the
143   /// specified global variable if it can.  This is only legal to call if
144   /// performing Interprocedural SCCP.
145   void TrackValueOfGlobalVariable(GlobalVariable *GV) {
146     const Type *ElTy = GV->getType()->getElementType();
147     if (ElTy->isFirstClassType()) {
148       LatticeVal &IV = TrackedGlobals[GV];
149       if (!isa<UndefValue>(GV->getInitializer()))
150         IV.markConstant(GV->getInitializer());
151     }
152   }
153
154   /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
155   /// and out of the specified function (which cannot have its address taken),
156   /// this method must be called.
157   void AddTrackedFunction(Function *F) {
158     assert(F->hasInternalLinkage() && "Can only track internal functions!");
159     // Add an entry, F -> undef.
160     TrackedFunctionRetVals[F];
161   }
162
163   /// Solve - Solve for constants and executable blocks.
164   ///
165   void Solve();
166
167   /// ResolveBranchesIn - While solving the dataflow for a function, we assume
168   /// that branches on undef values cannot reach any of their successors.
169   /// However, this is not a safe assumption.  After we solve dataflow, this
170   /// method should be use to handle this.  If this returns true, the solver
171   /// should be rerun.
172   bool ResolveBranchesIn(Function &F);
173
174   /// getExecutableBlocks - Once we have solved for constants, return the set of
175   /// blocks that is known to be executable.
176   std::set<BasicBlock*> &getExecutableBlocks() {
177     return BBExecutable;
178   }
179
180   /// getValueMapping - Once we have solved for constants, return the mapping of
181   /// LLVM values to LatticeVals.
182   hash_map<Value*, LatticeVal> &getValueMapping() {
183     return ValueState;
184   }
185
186   /// getTrackedFunctionRetVals - Get the inferred return value map.
187   ///
188   const hash_map<Function*, LatticeVal> &getTrackedFunctionRetVals() {
189     return TrackedFunctionRetVals;
190   }
191
192   /// getTrackedGlobals - Get and return the set of inferred initializers for
193   /// global variables.
194   const hash_map<GlobalVariable*, LatticeVal> &getTrackedGlobals() {
195     return TrackedGlobals;
196   }
197
198
199 private:
200   // markConstant - Make a value be marked as "constant".  If the value
201   // is not already a constant, add it to the instruction work list so that
202   // the users of the instruction are updated later.
203   //
204   inline void markConstant(LatticeVal &IV, Value *V, Constant *C) {
205     if (IV.markConstant(C)) {
206       DEBUG(std::cerr << "markConstant: " << *C << ": " << *V);
207       InstWorkList.push_back(V);
208     }
209   }
210   inline void markConstant(Value *V, Constant *C) {
211     markConstant(ValueState[V], V, C);
212   }
213
214   // markOverdefined - Make a value be marked as "overdefined". If the
215   // value is not already overdefined, add it to the overdefined instruction
216   // work list so that the users of the instruction are updated later.
217
218   inline void markOverdefined(LatticeVal &IV, Value *V) {
219     if (IV.markOverdefined()) {
220       DEBUG(std::cerr << "markOverdefined: ";
221             if (Function *F = dyn_cast<Function>(V))
222               std::cerr << "Function '" << F->getName() << "'\n";
223             else
224               std::cerr << *V);
225       // Only instructions go on the work list
226       OverdefinedInstWorkList.push_back(V);
227     }
228   }
229   inline void markOverdefined(Value *V) {
230     markOverdefined(ValueState[V], V);
231   }
232
233   inline void mergeInValue(LatticeVal &IV, Value *V, LatticeVal &MergeWithV) {
234     if (IV.isOverdefined() || MergeWithV.isUndefined())
235       return;  // Noop.
236     if (MergeWithV.isOverdefined())
237       markOverdefined(IV, V);
238     else if (IV.isUndefined())
239       markConstant(IV, V, MergeWithV.getConstant());
240     else if (IV.getConstant() != MergeWithV.getConstant())
241       markOverdefined(IV, V);
242   }
243
244   // getValueState - Return the LatticeVal object that corresponds to the value.
245   // This function is necessary because not all values should start out in the
246   // underdefined state... Argument's should be overdefined, and
247   // constants should be marked as constants.  If a value is not known to be an
248   // Instruction object, then use this accessor to get its value from the map.
249   //
250   inline LatticeVal &getValueState(Value *V) {
251     hash_map<Value*, LatticeVal>::iterator I = ValueState.find(V);
252     if (I != ValueState.end()) return I->second;  // Common case, in the map
253
254     if (Constant *CPV = dyn_cast<Constant>(V)) {
255       if (isa<UndefValue>(V)) {
256         // Nothing to do, remain undefined.
257       } else {
258         ValueState[CPV].markConstant(CPV);          // Constants are constant
259       }
260     }
261     // All others are underdefined by default...
262     return ValueState[V];
263   }
264
265   // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
266   // work list if it is not already executable...
267   //
268   void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
269     if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
270       return;  // This edge is already known to be executable!
271
272     if (BBExecutable.count(Dest)) {
273       DEBUG(std::cerr << "Marking Edge Executable: " << Source->getName()
274                       << " -> " << Dest->getName() << "\n");
275
276       // The destination is already executable, but we just made an edge
277       // feasible that wasn't before.  Revisit the PHI nodes in the block
278       // because they have potentially new operands.
279       for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
280         visitPHINode(*cast<PHINode>(I));
281
282     } else {
283       MarkBlockExecutable(Dest);
284     }
285   }
286
287   // getFeasibleSuccessors - Return a vector of booleans to indicate which
288   // successors are reachable from a given terminator instruction.
289   //
290   void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
291
292   // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
293   // block to the 'To' basic block is currently feasible...
294   //
295   bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
296
297   // OperandChangedState - This method is invoked on all of the users of an
298   // instruction that was just changed state somehow....  Based on this
299   // information, we need to update the specified user of this instruction.
300   //
301   void OperandChangedState(User *U) {
302     // Only instructions use other variable values!
303     Instruction &I = cast<Instruction>(*U);
304     if (BBExecutable.count(I.getParent()))   // Inst is executable?
305       visit(I);
306   }
307
308 private:
309   friend class InstVisitor<SCCPSolver>;
310
311   // visit implementations - Something changed in this instruction... Either an
312   // operand made a transition, or the instruction is newly executable.  Change
313   // the value type of I to reflect these changes if appropriate.
314   //
315   void visitPHINode(PHINode &I);
316
317   // Terminators
318   void visitReturnInst(ReturnInst &I);
319   void visitTerminatorInst(TerminatorInst &TI);
320
321   void visitCastInst(CastInst &I);
322   void visitSelectInst(SelectInst &I);
323   void visitBinaryOperator(Instruction &I);
324   void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
325   void visitExtractElementInst(ExtractElementInst &I);
326
327   // Instructions that cannot be folded away...
328   void visitStoreInst     (Instruction &I);
329   void visitLoadInst      (LoadInst &I);
330   void visitGetElementPtrInst(GetElementPtrInst &I);
331   void visitCallInst      (CallInst &I) { visitCallSite(CallSite::get(&I)); }
332   void visitInvokeInst    (InvokeInst &II) {
333     visitCallSite(CallSite::get(&II));
334     visitTerminatorInst(II);
335   }
336   void visitCallSite      (CallSite CS);
337   void visitUnwindInst    (TerminatorInst &I) { /*returns void*/ }
338   void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
339   void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
340   void visitVANextInst    (Instruction &I) { markOverdefined(&I); }
341   void visitVAArgInst     (Instruction &I) { markOverdefined(&I); }
342   void visitFreeInst      (Instruction &I) { /*returns void*/ }
343
344   void visitInstruction(Instruction &I) {
345     // If a new instruction is added to LLVM that we don't handle...
346     std::cerr << "SCCP: Don't know how to handle: " << I;
347     markOverdefined(&I);   // Just in case
348   }
349 };
350
351 // getFeasibleSuccessors - Return a vector of booleans to indicate which
352 // successors are reachable from a given terminator instruction.
353 //
354 void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
355                                        std::vector<bool> &Succs) {
356   Succs.resize(TI.getNumSuccessors());
357   if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
358     if (BI->isUnconditional()) {
359       Succs[0] = true;
360     } else {
361       LatticeVal &BCValue = getValueState(BI->getCondition());
362       if (BCValue.isOverdefined() ||
363           (BCValue.isConstant() && !isa<ConstantBool>(BCValue.getConstant()))) {
364         // Overdefined condition variables, and branches on unfoldable constant
365         // conditions, mean the branch could go either way.
366         Succs[0] = Succs[1] = true;
367       } else if (BCValue.isConstant()) {
368         // Constant condition variables mean the branch can only go a single way
369         Succs[BCValue.getConstant() == ConstantBool::False] = true;
370       }
371     }
372   } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
373     // Invoke instructions successors are always executable.
374     Succs[0] = Succs[1] = true;
375   } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
376     LatticeVal &SCValue = getValueState(SI->getCondition());
377     if (SCValue.isOverdefined() ||   // Overdefined condition?
378         (SCValue.isConstant() && !isa<ConstantInt>(SCValue.getConstant()))) {
379       // All destinations are executable!
380       Succs.assign(TI.getNumSuccessors(), true);
381     } else if (SCValue.isConstant()) {
382       Constant *CPV = SCValue.getConstant();
383       // Make sure to skip the "default value" which isn't a value
384       for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
385         if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
386           Succs[i] = true;
387           return;
388         }
389       }
390
391       // Constant value not equal to any of the branches... must execute
392       // default branch then...
393       Succs[0] = true;
394     }
395   } else {
396     std::cerr << "SCCP: Don't know how to handle: " << TI;
397     Succs.assign(TI.getNumSuccessors(), true);
398   }
399 }
400
401
402 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
403 // block to the 'To' basic block is currently feasible...
404 //
405 bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
406   assert(BBExecutable.count(To) && "Dest should always be alive!");
407
408   // Make sure the source basic block is executable!!
409   if (!BBExecutable.count(From)) return false;
410
411   // Check to make sure this edge itself is actually feasible now...
412   TerminatorInst *TI = From->getTerminator();
413   if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
414     if (BI->isUnconditional())
415       return true;
416     else {
417       LatticeVal &BCValue = getValueState(BI->getCondition());
418       if (BCValue.isOverdefined()) {
419         // Overdefined condition variables mean the branch could go either way.
420         return true;
421       } else if (BCValue.isConstant()) {
422         // Not branching on an evaluatable constant?
423         if (!isa<ConstantBool>(BCValue.getConstant())) return true;
424
425         // Constant condition variables mean the branch can only go a single way
426         return BI->getSuccessor(BCValue.getConstant() ==
427                                        ConstantBool::False) == To;
428       }
429       return false;
430     }
431   } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
432     // Invoke instructions successors are always executable.
433     return true;
434   } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
435     LatticeVal &SCValue = getValueState(SI->getCondition());
436     if (SCValue.isOverdefined()) {  // Overdefined condition?
437       // All destinations are executable!
438       return true;
439     } else if (SCValue.isConstant()) {
440       Constant *CPV = SCValue.getConstant();
441       if (!isa<ConstantInt>(CPV))
442         return true;  // not a foldable constant?
443
444       // Make sure to skip the "default value" which isn't a value
445       for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
446         if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
447           return SI->getSuccessor(i) == To;
448
449       // Constant value not equal to any of the branches... must execute
450       // default branch then...
451       return SI->getDefaultDest() == To;
452     }
453     return false;
454   } else {
455     std::cerr << "Unknown terminator instruction: " << *TI;
456     abort();
457   }
458 }
459
460 // visit Implementations - Something changed in this instruction... Either an
461 // operand made a transition, or the instruction is newly executable.  Change
462 // the value type of I to reflect these changes if appropriate.  This method
463 // makes sure to do the following actions:
464 //
465 // 1. If a phi node merges two constants in, and has conflicting value coming
466 //    from different branches, or if the PHI node merges in an overdefined
467 //    value, then the PHI node becomes overdefined.
468 // 2. If a phi node merges only constants in, and they all agree on value, the
469 //    PHI node becomes a constant value equal to that.
470 // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
471 // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
472 // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
473 // 6. If a conditional branch has a value that is constant, make the selected
474 //    destination executable
475 // 7. If a conditional branch has a value that is overdefined, make all
476 //    successors executable.
477 //
478 void SCCPSolver::visitPHINode(PHINode &PN) {
479   LatticeVal &PNIV = getValueState(&PN);
480   if (PNIV.isOverdefined()) {
481     // There may be instructions using this PHI node that are not overdefined
482     // themselves.  If so, make sure that they know that the PHI node operand
483     // changed.
484     std::multimap<PHINode*, Instruction*>::iterator I, E;
485     tie(I, E) = UsersOfOverdefinedPHIs.equal_range(&PN);
486     if (I != E) {
487       std::vector<Instruction*> Users;
488       Users.reserve(std::distance(I, E));
489       for (; I != E; ++I) Users.push_back(I->second);
490       while (!Users.empty()) {
491         visit(Users.back());
492         Users.pop_back();
493       }
494     }
495     return;  // Quick exit
496   }
497
498   // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
499   // and slow us down a lot.  Just mark them overdefined.
500   if (PN.getNumIncomingValues() > 64) {
501     markOverdefined(PNIV, &PN);
502     return;
503   }
504
505   // Look at all of the executable operands of the PHI node.  If any of them
506   // are overdefined, the PHI becomes overdefined as well.  If they are all
507   // constant, and they agree with each other, the PHI becomes the identical
508   // constant.  If they are constant and don't agree, the PHI is overdefined.
509   // If there are no executable operands, the PHI remains undefined.
510   //
511   Constant *OperandVal = 0;
512   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
513     LatticeVal &IV = getValueState(PN.getIncomingValue(i));
514     if (IV.isUndefined()) continue;  // Doesn't influence PHI node.
515
516     if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
517       if (IV.isOverdefined()) {   // PHI node becomes overdefined!
518         markOverdefined(PNIV, &PN);
519         return;
520       }
521
522       if (OperandVal == 0) {   // Grab the first value...
523         OperandVal = IV.getConstant();
524       } else {                // Another value is being merged in!
525         // There is already a reachable operand.  If we conflict with it,
526         // then the PHI node becomes overdefined.  If we agree with it, we
527         // can continue on.
528
529         // Check to see if there are two different constants merging...
530         if (IV.getConstant() != OperandVal) {
531           // Yes there is.  This means the PHI node is not constant.
532           // You must be overdefined poor PHI.
533           //
534           markOverdefined(PNIV, &PN);    // The PHI node now becomes overdefined
535           return;    // I'm done analyzing you
536         }
537       }
538     }
539   }
540
541   // If we exited the loop, this means that the PHI node only has constant
542   // arguments that agree with each other(and OperandVal is the constant) or
543   // OperandVal is null because there are no defined incoming arguments.  If
544   // this is the case, the PHI remains undefined.
545   //
546   if (OperandVal)
547     markConstant(PNIV, &PN, OperandVal);      // Acquire operand value
548 }
549
550 void SCCPSolver::visitReturnInst(ReturnInst &I) {
551   if (I.getNumOperands() == 0) return;  // Ret void
552
553   // If we are tracking the return value of this function, merge it in.
554   Function *F = I.getParent()->getParent();
555   if (F->hasInternalLinkage() && !TrackedFunctionRetVals.empty()) {
556     hash_map<Function*, LatticeVal>::iterator TFRVI =
557       TrackedFunctionRetVals.find(F);
558     if (TFRVI != TrackedFunctionRetVals.end() &&
559         !TFRVI->second.isOverdefined()) {
560       LatticeVal &IV = getValueState(I.getOperand(0));
561       mergeInValue(TFRVI->second, F, IV);
562     }
563   }
564 }
565
566
567 void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) {
568   std::vector<bool> SuccFeasible;
569   getFeasibleSuccessors(TI, SuccFeasible);
570
571   BasicBlock *BB = TI.getParent();
572
573   // Mark all feasible successors executable...
574   for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
575     if (SuccFeasible[i])
576       markEdgeExecutable(BB, TI.getSuccessor(i));
577 }
578
579 void SCCPSolver::visitCastInst(CastInst &I) {
580   Value *V = I.getOperand(0);
581   LatticeVal &VState = getValueState(V);
582   if (VState.isOverdefined())          // Inherit overdefinedness of operand
583     markOverdefined(&I);
584   else if (VState.isConstant())        // Propagate constant value
585     markConstant(&I, ConstantExpr::getCast(VState.getConstant(), I.getType()));
586 }
587
588 void SCCPSolver::visitSelectInst(SelectInst &I) {
589   LatticeVal &CondValue = getValueState(I.getCondition());
590   if (CondValue.isOverdefined())
591     markOverdefined(&I);
592   else if (CondValue.isConstant()) {
593     if (CondValue.getConstant() == ConstantBool::True) {
594       LatticeVal &Val = getValueState(I.getTrueValue());
595       if (Val.isOverdefined())
596         markOverdefined(&I);
597       else if (Val.isConstant())
598         markConstant(&I, Val.getConstant());
599     } else if (CondValue.getConstant() == ConstantBool::False) {
600       LatticeVal &Val = getValueState(I.getFalseValue());
601       if (Val.isOverdefined())
602         markOverdefined(&I);
603       else if (Val.isConstant())
604         markConstant(&I, Val.getConstant());
605     } else
606       markOverdefined(&I);
607   }
608 }
609
610 // Handle BinaryOperators and Shift Instructions...
611 void SCCPSolver::visitBinaryOperator(Instruction &I) {
612   LatticeVal &IV = ValueState[&I];
613   if (IV.isOverdefined()) return;
614
615   LatticeVal &V1State = getValueState(I.getOperand(0));
616   LatticeVal &V2State = getValueState(I.getOperand(1));
617
618   if (V1State.isOverdefined() || V2State.isOverdefined()) {
619     // If this is an AND or OR with 0 or -1, it doesn't matter that the other
620     // operand is overdefined.
621     if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) {
622       LatticeVal *NonOverdefVal = 0;
623       if (!V1State.isOverdefined()) {
624         NonOverdefVal = &V1State;
625       } else if (!V2State.isOverdefined()) {
626         NonOverdefVal = &V2State;
627       }
628
629       if (NonOverdefVal) {
630         if (NonOverdefVal->isUndefined()) {
631           // Could annihilate value.
632           if (I.getOpcode() == Instruction::And)
633             markConstant(IV, &I, Constant::getNullValue(I.getType()));
634           else
635             markConstant(IV, &I, ConstantInt::getAllOnesValue(I.getType()));
636           return;
637         } else {
638           if (I.getOpcode() == Instruction::And) {
639             if (NonOverdefVal->getConstant()->isNullValue()) {
640               markConstant(IV, &I, NonOverdefVal->getConstant());
641               return;      // X or 0 = -1
642             }
643           } else {
644             if (ConstantIntegral *CI =
645                      dyn_cast<ConstantIntegral>(NonOverdefVal->getConstant()))
646               if (CI->isAllOnesValue()) {
647                 markConstant(IV, &I, NonOverdefVal->getConstant());
648                 return;    // X or -1 = -1
649               }
650           }
651         }
652       }
653     }
654
655
656     // If both operands are PHI nodes, it is possible that this instruction has
657     // a constant value, despite the fact that the PHI node doesn't.  Check for
658     // this condition now.
659     if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0)))
660       if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1)))
661         if (PN1->getParent() == PN2->getParent()) {
662           // Since the two PHI nodes are in the same basic block, they must have
663           // entries for the same predecessors.  Walk the predecessor list, and
664           // if all of the incoming values are constants, and the result of
665           // evaluating this expression with all incoming value pairs is the
666           // same, then this expression is a constant even though the PHI node
667           // is not a constant!
668           LatticeVal Result;
669           for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
670             LatticeVal &In1 = getValueState(PN1->getIncomingValue(i));
671             BasicBlock *InBlock = PN1->getIncomingBlock(i);
672             LatticeVal &In2 =
673               getValueState(PN2->getIncomingValueForBlock(InBlock));
674
675             if (In1.isOverdefined() || In2.isOverdefined()) {
676               Result.markOverdefined();
677               break;  // Cannot fold this operation over the PHI nodes!
678             } else if (In1.isConstant() && In2.isConstant()) {
679               Constant *V = ConstantExpr::get(I.getOpcode(), In1.getConstant(),
680                                               In2.getConstant());
681               if (Result.isUndefined())
682                 Result.markConstant(V);
683               else if (Result.isConstant() && Result.getConstant() != V) {
684                 Result.markOverdefined();
685                 break;
686               }
687             }
688           }
689
690           // If we found a constant value here, then we know the instruction is
691           // constant despite the fact that the PHI nodes are overdefined.
692           if (Result.isConstant()) {
693             markConstant(IV, &I, Result.getConstant());
694             // Remember that this instruction is virtually using the PHI node
695             // operands.
696             UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I));
697             UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I));
698             return;
699           } else if (Result.isUndefined()) {
700             return;
701           }
702
703           // Okay, this really is overdefined now.  Since we might have
704           // speculatively thought that this was not overdefined before, and
705           // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
706           // make sure to clean out any entries that we put there, for
707           // efficiency.
708           std::multimap<PHINode*, Instruction*>::iterator It, E;
709           tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1);
710           while (It != E) {
711             if (It->second == &I) {
712               UsersOfOverdefinedPHIs.erase(It++);
713             } else
714               ++It;
715           }
716           tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2);
717           while (It != E) {
718             if (It->second == &I) {
719               UsersOfOverdefinedPHIs.erase(It++);
720             } else
721               ++It;
722           }
723         }
724
725     markOverdefined(IV, &I);
726   } else if (V1State.isConstant() && V2State.isConstant()) {
727     markConstant(IV, &I, ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
728                                            V2State.getConstant()));
729   }
730 }
731
732 void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
733   LatticeVal &ValState = getValueState(I.getOperand(0));
734   LatticeVal &IdxState = getValueState(I.getOperand(1));
735
736   if (ValState.isOverdefined() || IdxState.isOverdefined())
737     markOverdefined(&I);
738   else if(ValState.isConstant() && IdxState.isConstant())
739     markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(),
740                                                      IdxState.getConstant()));
741 }
742
743 // Handle getelementptr instructions... if all operands are constants then we
744 // can turn this into a getelementptr ConstantExpr.
745 //
746 void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
747   LatticeVal &IV = ValueState[&I];
748   if (IV.isOverdefined()) return;
749
750   std::vector<Constant*> Operands;
751   Operands.reserve(I.getNumOperands());
752
753   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
754     LatticeVal &State = getValueState(I.getOperand(i));
755     if (State.isUndefined())
756       return;  // Operands are not resolved yet...
757     else if (State.isOverdefined()) {
758       markOverdefined(IV, &I);
759       return;
760     }
761     assert(State.isConstant() && "Unknown state!");
762     Operands.push_back(State.getConstant());
763   }
764
765   Constant *Ptr = Operands[0];
766   Operands.erase(Operands.begin());  // Erase the pointer from idx list...
767
768   markConstant(IV, &I, ConstantExpr::getGetElementPtr(Ptr, Operands));
769 }
770
771 void SCCPSolver::visitStoreInst(Instruction &SI) {
772   if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
773     return;
774   GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
775   hash_map<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV);
776   if (I == TrackedGlobals.end() || I->second.isOverdefined()) return;
777
778   // Get the value we are storing into the global.
779   LatticeVal &PtrVal = getValueState(SI.getOperand(0));
780
781   mergeInValue(I->second, GV, PtrVal);
782   if (I->second.isOverdefined())
783     TrackedGlobals.erase(I);      // No need to keep tracking this!
784 }
785
786
787 // Handle load instructions.  If the operand is a constant pointer to a constant
788 // global, we can replace the load with the loaded constant value!
789 void SCCPSolver::visitLoadInst(LoadInst &I) {
790   LatticeVal &IV = ValueState[&I];
791   if (IV.isOverdefined()) return;
792
793   LatticeVal &PtrVal = getValueState(I.getOperand(0));
794   if (PtrVal.isUndefined()) return;   // The pointer is not resolved yet!
795   if (PtrVal.isConstant() && !I.isVolatile()) {
796     Value *Ptr = PtrVal.getConstant();
797     if (isa<ConstantPointerNull>(Ptr)) {
798       // load null -> null
799       markConstant(IV, &I, Constant::getNullValue(I.getType()));
800       return;
801     }
802
803     // Transform load (constant global) into the value loaded.
804     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
805       if (GV->isConstant()) {
806         if (!GV->isExternal()) {
807           markConstant(IV, &I, GV->getInitializer());
808           return;
809         }
810       } else if (!TrackedGlobals.empty()) {
811         // If we are tracking this global, merge in the known value for it.
812         hash_map<GlobalVariable*, LatticeVal>::iterator It =
813           TrackedGlobals.find(GV);
814         if (It != TrackedGlobals.end()) {
815           mergeInValue(IV, &I, It->second);
816           return;
817         }
818       }
819     }
820
821     // Transform load (constantexpr_GEP global, 0, ...) into the value loaded.
822     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
823       if (CE->getOpcode() == Instruction::GetElementPtr)
824     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
825       if (GV->isConstant() && !GV->isExternal())
826         if (Constant *V =
827              ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) {
828           markConstant(IV, &I, V);
829           return;
830         }
831   }
832
833   // Otherwise we cannot say for certain what value this load will produce.
834   // Bail out.
835   markOverdefined(IV, &I);
836 }
837
838 void SCCPSolver::visitCallSite(CallSite CS) {
839   Function *F = CS.getCalledFunction();
840
841   // If we are tracking this function, we must make sure to bind arguments as
842   // appropriate.
843   hash_map<Function*, LatticeVal>::iterator TFRVI =TrackedFunctionRetVals.end();
844   if (F && F->hasInternalLinkage())
845     TFRVI = TrackedFunctionRetVals.find(F);
846
847   if (TFRVI != TrackedFunctionRetVals.end()) {
848     // If this is the first call to the function hit, mark its entry block
849     // executable.
850     if (!BBExecutable.count(F->begin()))
851       MarkBlockExecutable(F->begin());
852
853     CallSite::arg_iterator CAI = CS.arg_begin();
854     for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
855          AI != E; ++AI, ++CAI) {
856       LatticeVal &IV = ValueState[AI];
857       if (!IV.isOverdefined())
858         mergeInValue(IV, AI, getValueState(*CAI));
859     }
860   }
861   Instruction *I = CS.getInstruction();
862   if (I->getType() == Type::VoidTy) return;
863
864   LatticeVal &IV = ValueState[I];
865   if (IV.isOverdefined()) return;
866
867   // Propagate the return value of the function to the value of the instruction.
868   if (TFRVI != TrackedFunctionRetVals.end()) {
869     mergeInValue(IV, I, TFRVI->second);
870     return;
871   }
872
873   if (F == 0 || !F->isExternal() || !canConstantFoldCallTo(F)) {
874     markOverdefined(IV, I);
875     return;
876   }
877
878   std::vector<Constant*> Operands;
879   Operands.reserve(I->getNumOperands()-1);
880
881   for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
882        AI != E; ++AI) {
883     LatticeVal &State = getValueState(*AI);
884     if (State.isUndefined())
885       return;  // Operands are not resolved yet...
886     else if (State.isOverdefined()) {
887       markOverdefined(IV, I);
888       return;
889     }
890     assert(State.isConstant() && "Unknown state!");
891     Operands.push_back(State.getConstant());
892   }
893
894   if (Constant *C = ConstantFoldCall(F, Operands))
895     markConstant(IV, I, C);
896   else
897     markOverdefined(IV, I);
898 }
899
900
901 void SCCPSolver::Solve() {
902   // Process the work lists until they are empty!
903   while (!BBWorkList.empty() || !InstWorkList.empty() ||
904          !OverdefinedInstWorkList.empty()) {
905     // Process the instruction work list...
906     while (!OverdefinedInstWorkList.empty()) {
907       Value *I = OverdefinedInstWorkList.back();
908       OverdefinedInstWorkList.pop_back();
909
910       DEBUG(std::cerr << "\nPopped off OI-WL: " << *I);
911
912       // "I" got into the work list because it either made the transition from
913       // bottom to constant
914       //
915       // Anything on this worklist that is overdefined need not be visited
916       // since all of its users will have already been marked as overdefined
917       // Update all of the users of this instruction's value...
918       //
919       for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
920            UI != E; ++UI)
921         OperandChangedState(*UI);
922     }
923     // Process the instruction work list...
924     while (!InstWorkList.empty()) {
925       Value *I = InstWorkList.back();
926       InstWorkList.pop_back();
927
928       DEBUG(std::cerr << "\nPopped off I-WL: " << *I);
929
930       // "I" got into the work list because it either made the transition from
931       // bottom to constant
932       //
933       // Anything on this worklist that is overdefined need not be visited
934       // since all of its users will have already been marked as overdefined.
935       // Update all of the users of this instruction's value...
936       //
937       if (!getValueState(I).isOverdefined())
938         for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
939              UI != E; ++UI)
940           OperandChangedState(*UI);
941     }
942
943     // Process the basic block work list...
944     while (!BBWorkList.empty()) {
945       BasicBlock *BB = BBWorkList.back();
946       BBWorkList.pop_back();
947
948       DEBUG(std::cerr << "\nPopped off BBWL: " << *BB);
949
950       // Notify all instructions in this basic block that they are newly
951       // executable.
952       visit(BB);
953     }
954   }
955 }
956
957 /// ResolveBranchesIn - While solving the dataflow for a function, we assume
958 /// that branches on undef values cannot reach any of their successors.
959 /// However, this is not a safe assumption.  After we solve dataflow, this
960 /// method should be use to handle this.  If this returns true, the solver
961 /// should be rerun.
962 bool SCCPSolver::ResolveBranchesIn(Function &F) {
963   bool BranchesResolved = false;
964   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
965     if (BBExecutable.count(BB)) {
966       TerminatorInst *TI = BB->getTerminator();
967       if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
968         if (BI->isConditional()) {
969           LatticeVal &BCValue = getValueState(BI->getCondition());
970           if (BCValue.isUndefined()) {
971             BI->setCondition(ConstantBool::True);
972             BranchesResolved = true;
973             visit(BI);
974           }
975         }
976       } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
977         LatticeVal &SCValue = getValueState(SI->getCondition());
978         if (SCValue.isUndefined()) {
979           const Type *CondTy = SI->getCondition()->getType();
980           SI->setCondition(Constant::getNullValue(CondTy));
981           BranchesResolved = true;
982           visit(SI);
983         }
984       }
985     }
986
987   return BranchesResolved;
988 }
989
990
991 namespace {
992   Statistic<> NumInstRemoved("sccp", "Number of instructions removed");
993   Statistic<> NumDeadBlocks ("sccp", "Number of basic blocks unreachable");
994
995   //===--------------------------------------------------------------------===//
996   //
997   /// SCCP Class - This class uses the SCCPSolver to implement a per-function
998   /// Sparse Conditional COnstant Propagator.
999   ///
1000   struct SCCP : public FunctionPass {
1001     // runOnFunction - Run the Sparse Conditional Constant Propagation
1002     // algorithm, and return true if the function was modified.
1003     //
1004     bool runOnFunction(Function &F);
1005
1006     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1007       AU.setPreservesCFG();
1008     }
1009   };
1010
1011   RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propagation");
1012 } // end anonymous namespace
1013
1014
1015 // createSCCPPass - This is the public interface to this file...
1016 FunctionPass *llvm::createSCCPPass() {
1017   return new SCCP();
1018 }
1019
1020
1021 // runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
1022 // and return true if the function was modified.
1023 //
1024 bool SCCP::runOnFunction(Function &F) {
1025   DEBUG(std::cerr << "SCCP on function '" << F.getName() << "'\n");
1026   SCCPSolver Solver;
1027
1028   // Mark the first block of the function as being executable.
1029   Solver.MarkBlockExecutable(F.begin());
1030
1031   // Mark all arguments to the function as being overdefined.
1032   hash_map<Value*, LatticeVal> &Values = Solver.getValueMapping();
1033   for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; ++AI)
1034     Values[AI].markOverdefined();
1035
1036   // Solve for constants.
1037   bool ResolvedBranches = true;
1038   while (ResolvedBranches) {
1039     Solver.Solve();
1040     DEBUG(std::cerr << "RESOLVING UNDEF BRANCHES\n");
1041     ResolvedBranches = Solver.ResolveBranchesIn(F);
1042   }
1043
1044   bool MadeChanges = false;
1045
1046   // If we decided that there are basic blocks that are dead in this function,
1047   // delete their contents now.  Note that we cannot actually delete the blocks,
1048   // as we cannot modify the CFG of the function.
1049   //
1050   std::set<BasicBlock*> &ExecutableBBs = Solver.getExecutableBlocks();
1051   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1052     if (!ExecutableBBs.count(BB)) {
1053       DEBUG(std::cerr << "  BasicBlock Dead:" << *BB);
1054       ++NumDeadBlocks;
1055
1056       // Delete the instructions backwards, as it has a reduced likelihood of
1057       // having to update as many def-use and use-def chains.
1058       std::vector<Instruction*> Insts;
1059       for (BasicBlock::iterator I = BB->begin(), E = BB->getTerminator();
1060            I != E; ++I)
1061         Insts.push_back(I);
1062       while (!Insts.empty()) {
1063         Instruction *I = Insts.back();
1064         Insts.pop_back();
1065         if (!I->use_empty())
1066           I->replaceAllUsesWith(UndefValue::get(I->getType()));
1067         BB->getInstList().erase(I);
1068         MadeChanges = true;
1069         ++NumInstRemoved;
1070       }
1071     } else {
1072       // Iterate over all of the instructions in a function, replacing them with
1073       // constants if we have found them to be of constant values.
1074       //
1075       for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1076         Instruction *Inst = BI++;
1077         if (Inst->getType() != Type::VoidTy) {
1078           LatticeVal &IV = Values[Inst];
1079           if (IV.isConstant() || IV.isUndefined() &&
1080               !isa<TerminatorInst>(Inst)) {
1081             Constant *Const = IV.isConstant()
1082               ? IV.getConstant() : UndefValue::get(Inst->getType());
1083             DEBUG(std::cerr << "  Constant: " << *Const << " = " << *Inst);
1084
1085             // Replaces all of the uses of a variable with uses of the constant.
1086             Inst->replaceAllUsesWith(Const);
1087
1088             // Delete the instruction.
1089             BB->getInstList().erase(Inst);
1090
1091             // Hey, we just changed something!
1092             MadeChanges = true;
1093             ++NumInstRemoved;
1094           }
1095         }
1096       }
1097     }
1098
1099   return MadeChanges;
1100 }
1101
1102 namespace {
1103   Statistic<> IPNumInstRemoved("ipsccp", "Number of instructions removed");
1104   Statistic<> IPNumDeadBlocks ("ipsccp", "Number of basic blocks unreachable");
1105   Statistic<> IPNumArgsElimed ("ipsccp",
1106                                "Number of arguments constant propagated");
1107   Statistic<> IPNumGlobalConst("ipsccp",
1108                                "Number of globals found to be constant");
1109
1110   //===--------------------------------------------------------------------===//
1111   //
1112   /// IPSCCP Class - This class implements interprocedural Sparse Conditional
1113   /// Constant Propagation.
1114   ///
1115   struct IPSCCP : public ModulePass {
1116     bool runOnModule(Module &M);
1117   };
1118
1119   RegisterOpt<IPSCCP>
1120   Y("ipsccp", "Interprocedural Sparse Conditional Constant Propagation");
1121 } // end anonymous namespace
1122
1123 // createIPSCCPPass - This is the public interface to this file...
1124 ModulePass *llvm::createIPSCCPPass() {
1125   return new IPSCCP();
1126 }
1127
1128
1129 static bool AddressIsTaken(GlobalValue *GV) {
1130   // Delete any dead constantexpr klingons.
1131   GV->removeDeadConstantUsers();
1132
1133   for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end();
1134        UI != E; ++UI)
1135     if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
1136       if (SI->getOperand(0) == GV || SI->isVolatile())
1137         return true;  // Storing addr of GV.
1138     } else if (isa<InvokeInst>(*UI) || isa<CallInst>(*UI)) {
1139       // Make sure we are calling the function, not passing the address.
1140       CallSite CS = CallSite::get(cast<Instruction>(*UI));
1141       for (CallSite::arg_iterator AI = CS.arg_begin(),
1142              E = CS.arg_end(); AI != E; ++AI)
1143         if (*AI == GV)
1144           return true;
1145     } else if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
1146       if (LI->isVolatile())
1147         return true;
1148     } else {
1149       return true;
1150     }
1151   return false;
1152 }
1153
1154 bool IPSCCP::runOnModule(Module &M) {
1155   SCCPSolver Solver;
1156
1157   // Loop over all functions, marking arguments to those with their addresses
1158   // taken or that are external as overdefined.
1159   //
1160   hash_map<Value*, LatticeVal> &Values = Solver.getValueMapping();
1161   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
1162     if (!F->hasInternalLinkage() || AddressIsTaken(F)) {
1163       if (!F->isExternal())
1164         Solver.MarkBlockExecutable(F->begin());
1165       for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1166            AI != E; ++AI)
1167         Values[AI].markOverdefined();
1168     } else {
1169       Solver.AddTrackedFunction(F);
1170     }
1171
1172   // Loop over global variables.  We inform the solver about any internal global
1173   // variables that do not have their 'addresses taken'.  If they don't have
1174   // their addresses taken, we can propagate constants through them.
1175   for (Module::global_iterator G = M.global_begin(), E = M.global_end();
1176        G != E; ++G)
1177     if (!G->isConstant() && G->hasInternalLinkage() && !AddressIsTaken(G))
1178       Solver.TrackValueOfGlobalVariable(G);
1179
1180   // Solve for constants.
1181   bool ResolvedBranches = true;
1182   while (ResolvedBranches) {
1183     Solver.Solve();
1184
1185     DEBUG(std::cerr << "RESOLVING UNDEF BRANCHES\n");
1186     ResolvedBranches = false;
1187     for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
1188       ResolvedBranches |= Solver.ResolveBranchesIn(*F);
1189   }
1190
1191   bool MadeChanges = false;
1192
1193   // Iterate over all of the instructions in the module, replacing them with
1194   // constants if we have found them to be of constant values.
1195   //
1196   std::set<BasicBlock*> &ExecutableBBs = Solver.getExecutableBlocks();
1197   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
1198     for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1199          AI != E; ++AI)
1200       if (!AI->use_empty()) {
1201         LatticeVal &IV = Values[AI];
1202         if (IV.isConstant() || IV.isUndefined()) {
1203           Constant *CST = IV.isConstant() ?
1204             IV.getConstant() : UndefValue::get(AI->getType());
1205           DEBUG(std::cerr << "***  Arg " << *AI << " = " << *CST <<"\n");
1206
1207           // Replaces all of the uses of a variable with uses of the
1208           // constant.
1209           AI->replaceAllUsesWith(CST);
1210           ++IPNumArgsElimed;
1211         }
1212       }
1213
1214     std::vector<BasicBlock*> BlocksToErase;
1215     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1216       if (!ExecutableBBs.count(BB)) {
1217         DEBUG(std::cerr << "  BasicBlock Dead:" << *BB);
1218         ++IPNumDeadBlocks;
1219
1220         // Delete the instructions backwards, as it has a reduced likelihood of
1221         // having to update as many def-use and use-def chains.
1222         std::vector<Instruction*> Insts;
1223         TerminatorInst *TI = BB->getTerminator();
1224         for (BasicBlock::iterator I = BB->begin(), E = TI; I != E; ++I)
1225           Insts.push_back(I);
1226
1227         while (!Insts.empty()) {
1228           Instruction *I = Insts.back();
1229           Insts.pop_back();
1230           if (!I->use_empty())
1231             I->replaceAllUsesWith(UndefValue::get(I->getType()));
1232           BB->getInstList().erase(I);
1233           MadeChanges = true;
1234           ++IPNumInstRemoved;
1235         }
1236
1237         for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1238           BasicBlock *Succ = TI->getSuccessor(i);
1239           if (Succ->begin() != Succ->end() && isa<PHINode>(Succ->begin()))
1240             TI->getSuccessor(i)->removePredecessor(BB);
1241         }
1242         if (!TI->use_empty())
1243           TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
1244         BB->getInstList().erase(TI);
1245
1246         if (&*BB != &F->front())
1247           BlocksToErase.push_back(BB);
1248         else
1249           new UnreachableInst(BB);
1250
1251       } else {
1252         for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1253           Instruction *Inst = BI++;
1254           if (Inst->getType() != Type::VoidTy) {
1255             LatticeVal &IV = Values[Inst];
1256             if (IV.isConstant() || IV.isUndefined() &&
1257                 !isa<TerminatorInst>(Inst)) {
1258               Constant *Const = IV.isConstant()
1259                 ? IV.getConstant() : UndefValue::get(Inst->getType());
1260               DEBUG(std::cerr << "  Constant: " << *Const << " = " << *Inst);
1261
1262               // Replaces all of the uses of a variable with uses of the
1263               // constant.
1264               Inst->replaceAllUsesWith(Const);
1265
1266               // Delete the instruction.
1267               if (!isa<TerminatorInst>(Inst) && !isa<CallInst>(Inst))
1268                 BB->getInstList().erase(Inst);
1269
1270               // Hey, we just changed something!
1271               MadeChanges = true;
1272               ++IPNumInstRemoved;
1273             }
1274           }
1275         }
1276       }
1277
1278     // Now that all instructions in the function are constant folded, erase dead
1279     // blocks, because we can now use ConstantFoldTerminator to get rid of
1280     // in-edges.
1281     for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
1282       // If there are any PHI nodes in this successor, drop entries for BB now.
1283       BasicBlock *DeadBB = BlocksToErase[i];
1284       while (!DeadBB->use_empty()) {
1285         Instruction *I = cast<Instruction>(DeadBB->use_back());
1286         bool Folded = ConstantFoldTerminator(I->getParent());
1287         assert(Folded && "Didn't fold away reference to block!");
1288       }
1289
1290       // Finally, delete the basic block.
1291       F->getBasicBlockList().erase(DeadBB);
1292     }
1293   }
1294
1295   // If we inferred constant or undef return values for a function, we replaced
1296   // all call uses with the inferred value.  This means we don't need to bother
1297   // actually returning anything from the function.  Replace all return
1298   // instructions with return undef.
1299   const hash_map<Function*, LatticeVal> &RV =Solver.getTrackedFunctionRetVals();
1300   for (hash_map<Function*, LatticeVal>::const_iterator I = RV.begin(),
1301          E = RV.end(); I != E; ++I)
1302     if (!I->second.isOverdefined() &&
1303         I->first->getReturnType() != Type::VoidTy) {
1304       Function *F = I->first;
1305       for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1306         if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
1307           if (!isa<UndefValue>(RI->getOperand(0)))
1308             RI->setOperand(0, UndefValue::get(F->getReturnType()));
1309     }
1310
1311   // If we infered constant or undef values for globals variables, we can delete
1312   // the global and any stores that remain to it.
1313   const hash_map<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals();
1314   for (hash_map<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(),
1315          E = TG.end(); I != E; ++I) {
1316     GlobalVariable *GV = I->first;
1317     assert(!I->second.isOverdefined() &&
1318            "Overdefined values should have been taken out of the map!");
1319     DEBUG(std::cerr << "Found that GV '" << GV->getName()<< "' is constant!\n");
1320     while (!GV->use_empty()) {
1321       StoreInst *SI = cast<StoreInst>(GV->use_back());
1322       SI->eraseFromParent();
1323     }
1324     M.getGlobalList().erase(GV);
1325     ++IPNumGlobalConst;
1326   }
1327
1328   return MadeChanges;
1329 }