LLVM API Documentation

SCCP.cpp
Go to the documentation of this file.
00001 //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements sparse conditional constant propagation and merging:
00011 //
00012 // Specifically, this:
00013 //   * Assumes values are constant unless proven otherwise
00014 //   * Assumes BasicBlocks are dead unless proven otherwise
00015 //   * Proves values to be constant, and replaces them with constants
00016 //   * Proves conditional branches to be unconditional
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #define DEBUG_TYPE "sccp"
00021 #include "llvm/Transforms/Scalar.h"
00022 #include "llvm/ADT/DenseMap.h"
00023 #include "llvm/ADT/DenseSet.h"
00024 #include "llvm/ADT/PointerIntPair.h"
00025 #include "llvm/ADT/SmallPtrSet.h"
00026 #include "llvm/ADT/SmallVector.h"
00027 #include "llvm/ADT/Statistic.h"
00028 #include "llvm/Analysis/ConstantFolding.h"
00029 #include "llvm/IR/Constants.h"
00030 #include "llvm/IR/DataLayout.h"
00031 #include "llvm/IR/DerivedTypes.h"
00032 #include "llvm/IR/Instructions.h"
00033 #include "llvm/InstVisitor.h"
00034 #include "llvm/Pass.h"
00035 #include "llvm/Support/CallSite.h"
00036 #include "llvm/Support/Debug.h"
00037 #include "llvm/Support/ErrorHandling.h"
00038 #include "llvm/Support/raw_ostream.h"
00039 #include "llvm/Target/TargetLibraryInfo.h"
00040 #include "llvm/Transforms/IPO.h"
00041 #include "llvm/Transforms/Utils/Local.h"
00042 #include <algorithm>
00043 using namespace llvm;
00044 
00045 STATISTIC(NumInstRemoved, "Number of instructions removed");
00046 STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
00047 
00048 STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP");
00049 STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");
00050 STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP");
00051 
00052 namespace {
00053 /// LatticeVal class - This class represents the different lattice values that
00054 /// an LLVM value may occupy.  It is a simple class with value semantics.
00055 ///
00056 class LatticeVal {
00057   enum LatticeValueTy {
00058     /// undefined - This LLVM Value has no known value yet.
00059     undefined,
00060 
00061     /// constant - This LLVM Value has a specific constant value.
00062     constant,
00063 
00064     /// forcedconstant - This LLVM Value was thought to be undef until
00065     /// ResolvedUndefsIn.  This is treated just like 'constant', but if merged
00066     /// with another (different) constant, it goes to overdefined, instead of
00067     /// asserting.
00068     forcedconstant,
00069 
00070     /// overdefined - This instruction is not known to be constant, and we know
00071     /// it has a value.
00072     overdefined
00073   };
00074 
00075   /// Val: This stores the current lattice value along with the Constant* for
00076   /// the constant if this is a 'constant' or 'forcedconstant' value.
00077   PointerIntPair<Constant *, 2, LatticeValueTy> Val;
00078 
00079   LatticeValueTy getLatticeValue() const {
00080     return Val.getInt();
00081   }
00082 
00083 public:
00084   LatticeVal() : Val(0, undefined) {}
00085 
00086   bool isUndefined() const { return getLatticeValue() == undefined; }
00087   bool isConstant() const {
00088     return getLatticeValue() == constant || getLatticeValue() == forcedconstant;
00089   }
00090   bool isOverdefined() const { return getLatticeValue() == overdefined; }
00091 
00092   Constant *getConstant() const {
00093     assert(isConstant() && "Cannot get the constant of a non-constant!");
00094     return Val.getPointer();
00095   }
00096 
00097   /// markOverdefined - Return true if this is a change in status.
00098   bool markOverdefined() {
00099     if (isOverdefined())
00100       return false;
00101 
00102     Val.setInt(overdefined);
00103     return true;
00104   }
00105 
00106   /// markConstant - Return true if this is a change in status.
00107   bool markConstant(Constant *V) {
00108     if (getLatticeValue() == constant) { // Constant but not forcedconstant.
00109       assert(getConstant() == V && "Marking constant with different value");
00110       return false;
00111     }
00112 
00113     if (isUndefined()) {
00114       Val.setInt(constant);
00115       assert(V && "Marking constant with NULL");
00116       Val.setPointer(V);
00117     } else {
00118       assert(getLatticeValue() == forcedconstant &&
00119              "Cannot move from overdefined to constant!");
00120       // Stay at forcedconstant if the constant is the same.
00121       if (V == getConstant()) return false;
00122 
00123       // Otherwise, we go to overdefined.  Assumptions made based on the
00124       // forced value are possibly wrong.  Assuming this is another constant
00125       // could expose a contradiction.
00126       Val.setInt(overdefined);
00127     }
00128     return true;
00129   }
00130 
00131   /// getConstantInt - If this is a constant with a ConstantInt value, return it
00132   /// otherwise return null.
00133   ConstantInt *getConstantInt() const {
00134     if (isConstant())
00135       return dyn_cast<ConstantInt>(getConstant());
00136     return 0;
00137   }
00138 
00139   void markForcedConstant(Constant *V) {
00140     assert(isUndefined() && "Can't force a defined value!");
00141     Val.setInt(forcedconstant);
00142     Val.setPointer(V);
00143   }
00144 };
00145 } // end anonymous namespace.
00146 
00147 
00148 namespace {
00149 
00150 //===----------------------------------------------------------------------===//
00151 //
00152 /// SCCPSolver - This class is a general purpose solver for Sparse Conditional
00153 /// Constant Propagation.
00154 ///
00155 class SCCPSolver : public InstVisitor<SCCPSolver> {
00156   const DataLayout *TD;
00157   const TargetLibraryInfo *TLI;
00158   SmallPtrSet<BasicBlock*, 8> BBExecutable; // The BBs that are executable.
00159   DenseMap<Value*, LatticeVal> ValueState;  // The state each value is in.
00160 
00161   /// StructValueState - This maintains ValueState for values that have
00162   /// StructType, for example for formal arguments, calls, insertelement, etc.
00163   ///
00164   DenseMap<std::pair<Value*, unsigned>, LatticeVal> StructValueState;
00165 
00166   /// GlobalValue - If we are tracking any values for the contents of a global
00167   /// variable, we keep a mapping from the constant accessor to the element of
00168   /// the global, to the currently known value.  If the value becomes
00169   /// overdefined, it's entry is simply removed from this map.
00170   DenseMap<GlobalVariable*, LatticeVal> TrackedGlobals;
00171 
00172   /// TrackedRetVals - If we are tracking arguments into and the return
00173   /// value out of a function, it will have an entry in this map, indicating
00174   /// what the known return value for the function is.
00175   DenseMap<Function*, LatticeVal> TrackedRetVals;
00176 
00177   /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
00178   /// that return multiple values.
00179   DenseMap<std::pair<Function*, unsigned>, LatticeVal> TrackedMultipleRetVals;
00180 
00181   /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
00182   /// represented here for efficient lookup.
00183   SmallPtrSet<Function*, 16> MRVFunctionsTracked;
00184 
00185   /// TrackingIncomingArguments - This is the set of functions for whose
00186   /// arguments we make optimistic assumptions about and try to prove as
00187   /// constants.
00188   SmallPtrSet<Function*, 16> TrackingIncomingArguments;
00189 
00190   /// The reason for two worklists is that overdefined is the lowest state
00191   /// on the lattice, and moving things to overdefined as fast as possible
00192   /// makes SCCP converge much faster.
00193   ///
00194   /// By having a separate worklist, we accomplish this because everything
00195   /// possibly overdefined will become overdefined at the soonest possible
00196   /// point.
00197   SmallVector<Value*, 64> OverdefinedInstWorkList;
00198   SmallVector<Value*, 64> InstWorkList;
00199 
00200 
00201   SmallVector<BasicBlock*, 64>  BBWorkList;  // The BasicBlock work list
00202 
00203   /// KnownFeasibleEdges - Entries in this set are edges which have already had
00204   /// PHI nodes retriggered.
00205   typedef std::pair<BasicBlock*, BasicBlock*> Edge;
00206   DenseSet<Edge> KnownFeasibleEdges;
00207 public:
00208   SCCPSolver(const DataLayout *td, const TargetLibraryInfo *tli)
00209     : TD(td), TLI(tli) {}
00210 
00211   /// MarkBlockExecutable - This method can be used by clients to mark all of
00212   /// the blocks that are known to be intrinsically live in the processed unit.
00213   ///
00214   /// This returns true if the block was not considered live before.
00215   bool MarkBlockExecutable(BasicBlock *BB) {
00216     if (!BBExecutable.insert(BB)) return false;
00217     DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << "\n");
00218     BBWorkList.push_back(BB);  // Add the block to the work list!
00219     return true;
00220   }
00221 
00222   /// TrackValueOfGlobalVariable - Clients can use this method to
00223   /// inform the SCCPSolver that it should track loads and stores to the
00224   /// specified global variable if it can.  This is only legal to call if
00225   /// performing Interprocedural SCCP.
00226   void TrackValueOfGlobalVariable(GlobalVariable *GV) {
00227     // We only track the contents of scalar globals.
00228     if (GV->getType()->getElementType()->isSingleValueType()) {
00229       LatticeVal &IV = TrackedGlobals[GV];
00230       if (!isa<UndefValue>(GV->getInitializer()))
00231         IV.markConstant(GV->getInitializer());
00232     }
00233   }
00234 
00235   /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
00236   /// and out of the specified function (which cannot have its address taken),
00237   /// this method must be called.
00238   void AddTrackedFunction(Function *F) {
00239     // Add an entry, F -> undef.
00240     if (StructType *STy = dyn_cast<StructType>(F->getReturnType())) {
00241       MRVFunctionsTracked.insert(F);
00242       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
00243         TrackedMultipleRetVals.insert(std::make_pair(std::make_pair(F, i),
00244                                                      LatticeVal()));
00245     } else
00246       TrackedRetVals.insert(std::make_pair(F, LatticeVal()));
00247   }
00248 
00249   void AddArgumentTrackedFunction(Function *F) {
00250     TrackingIncomingArguments.insert(F);
00251   }
00252 
00253   /// Solve - Solve for constants and executable blocks.
00254   ///
00255   void Solve();
00256 
00257   /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
00258   /// that branches on undef values cannot reach any of their successors.
00259   /// However, this is not a safe assumption.  After we solve dataflow, this
00260   /// method should be use to handle this.  If this returns true, the solver
00261   /// should be rerun.
00262   bool ResolvedUndefsIn(Function &F);
00263 
00264   bool isBlockExecutable(BasicBlock *BB) const {
00265     return BBExecutable.count(BB);
00266   }
00267 
00268   LatticeVal getLatticeValueFor(Value *V) const {
00269     DenseMap<Value*, LatticeVal>::const_iterator I = ValueState.find(V);
00270     assert(I != ValueState.end() && "V is not in valuemap!");
00271     return I->second;
00272   }
00273 
00274   /// getTrackedRetVals - Get the inferred return value map.
00275   ///
00276   const DenseMap<Function*, LatticeVal> &getTrackedRetVals() {
00277     return TrackedRetVals;
00278   }
00279 
00280   /// getTrackedGlobals - Get and return the set of inferred initializers for
00281   /// global variables.
00282   const DenseMap<GlobalVariable*, LatticeVal> &getTrackedGlobals() {
00283     return TrackedGlobals;
00284   }
00285 
00286   void markOverdefined(Value *V) {
00287     assert(!V->getType()->isStructTy() && "Should use other method");
00288     markOverdefined(ValueState[V], V);
00289   }
00290 
00291   /// markAnythingOverdefined - Mark the specified value overdefined.  This
00292   /// works with both scalars and structs.
00293   void markAnythingOverdefined(Value *V) {
00294     if (StructType *STy = dyn_cast<StructType>(V->getType()))
00295       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
00296         markOverdefined(getStructValueState(V, i), V);
00297     else
00298       markOverdefined(V);
00299   }
00300 
00301 private:
00302   // markConstant - Make a value be marked as "constant".  If the value
00303   // is not already a constant, add it to the instruction work list so that
00304   // the users of the instruction are updated later.
00305   //
00306   void markConstant(LatticeVal &IV, Value *V, Constant *C) {
00307     if (!IV.markConstant(C)) return;
00308     DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
00309     if (IV.isOverdefined())
00310       OverdefinedInstWorkList.push_back(V);
00311     else
00312       InstWorkList.push_back(V);
00313   }
00314 
00315   void markConstant(Value *V, Constant *C) {
00316     assert(!V->getType()->isStructTy() && "Should use other method");
00317     markConstant(ValueState[V], V, C);
00318   }
00319 
00320   void markForcedConstant(Value *V, Constant *C) {
00321     assert(!V->getType()->isStructTy() && "Should use other method");
00322     LatticeVal &IV = ValueState[V];
00323     IV.markForcedConstant(C);
00324     DEBUG(dbgs() << "markForcedConstant: " << *C << ": " << *V << '\n');
00325     if (IV.isOverdefined())
00326       OverdefinedInstWorkList.push_back(V);
00327     else
00328       InstWorkList.push_back(V);
00329   }
00330 
00331 
00332   // markOverdefined - Make a value be marked as "overdefined". If the
00333   // value is not already overdefined, add it to the overdefined instruction
00334   // work list so that the users of the instruction are updated later.
00335   void markOverdefined(LatticeVal &IV, Value *V) {
00336     if (!IV.markOverdefined()) return;
00337 
00338     DEBUG(dbgs() << "markOverdefined: ";
00339           if (Function *F = dyn_cast<Function>(V))
00340             dbgs() << "Function '" << F->getName() << "'\n";
00341           else
00342             dbgs() << *V << '\n');
00343     // Only instructions go on the work list
00344     OverdefinedInstWorkList.push_back(V);
00345   }
00346 
00347   void mergeInValue(LatticeVal &IV, Value *V, LatticeVal MergeWithV) {
00348     if (IV.isOverdefined() || MergeWithV.isUndefined())
00349       return;  // Noop.
00350     if (MergeWithV.isOverdefined())
00351       markOverdefined(IV, V);
00352     else if (IV.isUndefined())
00353       markConstant(IV, V, MergeWithV.getConstant());
00354     else if (IV.getConstant() != MergeWithV.getConstant())
00355       markOverdefined(IV, V);
00356   }
00357 
00358   void mergeInValue(Value *V, LatticeVal MergeWithV) {
00359     assert(!V->getType()->isStructTy() && "Should use other method");
00360     mergeInValue(ValueState[V], V, MergeWithV);
00361   }
00362 
00363 
00364   /// getValueState - Return the LatticeVal object that corresponds to the
00365   /// value.  This function handles the case when the value hasn't been seen yet
00366   /// by properly seeding constants etc.
00367   LatticeVal &getValueState(Value *V) {
00368     assert(!V->getType()->isStructTy() && "Should use getStructValueState");
00369 
00370     std::pair<DenseMap<Value*, LatticeVal>::iterator, bool> I =
00371       ValueState.insert(std::make_pair(V, LatticeVal()));
00372     LatticeVal &LV = I.first->second;
00373 
00374     if (!I.second)
00375       return LV;  // Common case, already in the map.
00376 
00377     if (Constant *C = dyn_cast<Constant>(V)) {
00378       // Undef values remain undefined.
00379       if (!isa<UndefValue>(V))
00380         LV.markConstant(C);          // Constants are constant
00381     }
00382 
00383     // All others are underdefined by default.
00384     return LV;
00385   }
00386 
00387   /// getStructValueState - Return the LatticeVal object that corresponds to the
00388   /// value/field pair.  This function handles the case when the value hasn't
00389   /// been seen yet by properly seeding constants etc.
00390   LatticeVal &getStructValueState(Value *V, unsigned i) {
00391     assert(V->getType()->isStructTy() && "Should use getValueState");
00392     assert(i < cast<StructType>(V->getType())->getNumElements() &&
00393            "Invalid element #");
00394 
00395     std::pair<DenseMap<std::pair<Value*, unsigned>, LatticeVal>::iterator,
00396               bool> I = StructValueState.insert(
00397                         std::make_pair(std::make_pair(V, i), LatticeVal()));
00398     LatticeVal &LV = I.first->second;
00399 
00400     if (!I.second)
00401       return LV;  // Common case, already in the map.
00402 
00403     if (Constant *C = dyn_cast<Constant>(V)) {
00404       Constant *Elt = C->getAggregateElement(i);
00405 
00406       if (Elt == 0)
00407         LV.markOverdefined();      // Unknown sort of constant.
00408       else if (isa<UndefValue>(Elt))
00409         ; // Undef values remain undefined.
00410       else
00411         LV.markConstant(Elt);      // Constants are constant.
00412     }
00413 
00414     // All others are underdefined by default.
00415     return LV;
00416   }
00417 
00418 
00419   /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
00420   /// work list if it is not already executable.
00421   void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
00422     if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
00423       return;  // This edge is already known to be executable!
00424 
00425     if (!MarkBlockExecutable(Dest)) {
00426       // If the destination is already executable, we just made an *edge*
00427       // feasible that wasn't before.  Revisit the PHI nodes in the block
00428       // because they have potentially new operands.
00429       DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
00430             << " -> " << Dest->getName() << "\n");
00431 
00432       PHINode *PN;
00433       for (BasicBlock::iterator I = Dest->begin();
00434            (PN = dyn_cast<PHINode>(I)); ++I)
00435         visitPHINode(*PN);
00436     }
00437   }
00438 
00439   // getFeasibleSuccessors - Return a vector of booleans to indicate which
00440   // successors are reachable from a given terminator instruction.
00441   //
00442   void getFeasibleSuccessors(TerminatorInst &TI, SmallVector<bool, 16> &Succs);
00443 
00444   // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
00445   // block to the 'To' basic block is currently feasible.
00446   //
00447   bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
00448 
00449   // OperandChangedState - This method is invoked on all of the users of an
00450   // instruction that was just changed state somehow.  Based on this
00451   // information, we need to update the specified user of this instruction.
00452   //
00453   void OperandChangedState(Instruction *I) {
00454     if (BBExecutable.count(I->getParent()))   // Inst is executable?
00455       visit(*I);
00456   }
00457 
00458 private:
00459   friend class InstVisitor<SCCPSolver>;
00460 
00461   // visit implementations - Something changed in this instruction.  Either an
00462   // operand made a transition, or the instruction is newly executable.  Change
00463   // the value type of I to reflect these changes if appropriate.
00464   void visitPHINode(PHINode &I);
00465 
00466   // Terminators
00467   void visitReturnInst(ReturnInst &I);
00468   void visitTerminatorInst(TerminatorInst &TI);
00469 
00470   void visitCastInst(CastInst &I);
00471   void visitSelectInst(SelectInst &I);
00472   void visitBinaryOperator(Instruction &I);
00473   void visitCmpInst(CmpInst &I);
00474   void visitExtractElementInst(ExtractElementInst &I);
00475   void visitInsertElementInst(InsertElementInst &I);
00476   void visitShuffleVectorInst(ShuffleVectorInst &I);
00477   void visitExtractValueInst(ExtractValueInst &EVI);
00478   void visitInsertValueInst(InsertValueInst &IVI);
00479   void visitLandingPadInst(LandingPadInst &I) { markAnythingOverdefined(&I); }
00480 
00481   // Instructions that cannot be folded away.
00482   void visitStoreInst     (StoreInst &I);
00483   void visitLoadInst      (LoadInst &I);
00484   void visitGetElementPtrInst(GetElementPtrInst &I);
00485   void visitCallInst      (CallInst &I) {
00486     visitCallSite(&I);
00487   }
00488   void visitInvokeInst    (InvokeInst &II) {
00489     visitCallSite(&II);
00490     visitTerminatorInst(II);
00491   }
00492   void visitCallSite      (CallSite CS);
00493   void visitResumeInst    (TerminatorInst &I) { /*returns void*/ }
00494   void visitUnwindInst    (TerminatorInst &I) { /*returns void*/ }
00495   void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
00496   void visitFenceInst     (FenceInst &I) { /*returns void*/ }
00497   void visitAtomicCmpXchgInst (AtomicCmpXchgInst &I) { markOverdefined(&I); }
00498   void visitAtomicRMWInst (AtomicRMWInst &I) { markOverdefined(&I); }
00499   void visitAllocaInst    (Instruction &I) { markOverdefined(&I); }
00500   void visitVAArgInst     (Instruction &I) { markAnythingOverdefined(&I); }
00501 
00502   void visitInstruction(Instruction &I) {
00503     // If a new instruction is added to LLVM that we don't handle.
00504     dbgs() << "SCCP: Don't know how to handle: " << I;
00505     markAnythingOverdefined(&I);   // Just in case
00506   }
00507 };
00508 
00509 } // end anonymous namespace
00510 
00511 
00512 // getFeasibleSuccessors - Return a vector of booleans to indicate which
00513 // successors are reachable from a given terminator instruction.
00514 //
00515 void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
00516                                        SmallVector<bool, 16> &Succs) {
00517   Succs.resize(TI.getNumSuccessors());
00518   if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
00519     if (BI->isUnconditional()) {
00520       Succs[0] = true;
00521       return;
00522     }
00523 
00524     LatticeVal BCValue = getValueState(BI->getCondition());
00525     ConstantInt *CI = BCValue.getConstantInt();
00526     if (CI == 0) {
00527       // Overdefined condition variables, and branches on unfoldable constant
00528       // conditions, mean the branch could go either way.
00529       if (!BCValue.isUndefined())
00530         Succs[0] = Succs[1] = true;
00531       return;
00532     }
00533 
00534     // Constant condition variables mean the branch can only go a single way.
00535     Succs[CI->isZero()] = true;
00536     return;
00537   }
00538 
00539   if (isa<InvokeInst>(TI)) {
00540     // Invoke instructions successors are always executable.
00541     Succs[0] = Succs[1] = true;
00542     return;
00543   }
00544 
00545   if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
00546     if (!SI->getNumCases()) {
00547       Succs[0] = true;
00548       return;
00549     }
00550     LatticeVal SCValue = getValueState(SI->getCondition());
00551     ConstantInt *CI = SCValue.getConstantInt();
00552 
00553     if (CI == 0) {   // Overdefined or undefined condition?
00554       // All destinations are executable!
00555       if (!SCValue.isUndefined())
00556         Succs.assign(TI.getNumSuccessors(), true);
00557       return;
00558     }
00559 
00560     Succs[SI->findCaseValue(CI).getSuccessorIndex()] = true;
00561     return;
00562   }
00563 
00564   // TODO: This could be improved if the operand is a [cast of a] BlockAddress.
00565   if (isa<IndirectBrInst>(&TI)) {
00566     // Just mark all destinations executable!
00567     Succs.assign(TI.getNumSuccessors(), true);
00568     return;
00569   }
00570 
00571 #ifndef NDEBUG
00572   dbgs() << "Unknown terminator instruction: " << TI << '\n';
00573 #endif
00574   llvm_unreachable("SCCP: Don't know how to handle this terminator!");
00575 }
00576 
00577 
00578 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
00579 // block to the 'To' basic block is currently feasible.
00580 //
00581 bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
00582   assert(BBExecutable.count(To) && "Dest should always be alive!");
00583 
00584   // Make sure the source basic block is executable!!
00585   if (!BBExecutable.count(From)) return false;
00586 
00587   // Check to make sure this edge itself is actually feasible now.
00588   TerminatorInst *TI = From->getTerminator();
00589   if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
00590     if (BI->isUnconditional())
00591       return true;
00592 
00593     LatticeVal BCValue = getValueState(BI->getCondition());
00594 
00595     // Overdefined condition variables mean the branch could go either way,
00596     // undef conditions mean that neither edge is feasible yet.
00597     ConstantInt *CI = BCValue.getConstantInt();
00598     if (CI == 0)
00599       return !BCValue.isUndefined();
00600 
00601     // Constant condition variables mean the branch can only go a single way.
00602     return BI->getSuccessor(CI->isZero()) == To;
00603   }
00604 
00605   // Invoke instructions successors are always executable.
00606   if (isa<InvokeInst>(TI))
00607     return true;
00608 
00609   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
00610     if (SI->getNumCases() < 1)
00611       return true;
00612 
00613     LatticeVal SCValue = getValueState(SI->getCondition());
00614     ConstantInt *CI = SCValue.getConstantInt();
00615 
00616     if (CI == 0)
00617       return !SCValue.isUndefined();
00618 
00619     return SI->findCaseValue(CI).getCaseSuccessor() == To;
00620   }
00621 
00622   // Just mark all destinations executable!
00623   // TODO: This could be improved if the operand is a [cast of a] BlockAddress.
00624   if (isa<IndirectBrInst>(TI))
00625     return true;
00626 
00627 #ifndef NDEBUG
00628   dbgs() << "Unknown terminator instruction: " << *TI << '\n';
00629 #endif
00630   llvm_unreachable(0);
00631 }
00632 
00633 // visit Implementations - Something changed in this instruction, either an
00634 // operand made a transition, or the instruction is newly executable.  Change
00635 // the value type of I to reflect these changes if appropriate.  This method
00636 // makes sure to do the following actions:
00637 //
00638 // 1. If a phi node merges two constants in, and has conflicting value coming
00639 //    from different branches, or if the PHI node merges in an overdefined
00640 //    value, then the PHI node becomes overdefined.
00641 // 2. If a phi node merges only constants in, and they all agree on value, the
00642 //    PHI node becomes a constant value equal to that.
00643 // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
00644 // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
00645 // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
00646 // 6. If a conditional branch has a value that is constant, make the selected
00647 //    destination executable
00648 // 7. If a conditional branch has a value that is overdefined, make all
00649 //    successors executable.
00650 //
00651 void SCCPSolver::visitPHINode(PHINode &PN) {
00652   // If this PN returns a struct, just mark the result overdefined.
00653   // TODO: We could do a lot better than this if code actually uses this.
00654   if (PN.getType()->isStructTy())
00655     return markAnythingOverdefined(&PN);
00656 
00657   if (getValueState(&PN).isOverdefined())
00658     return;  // Quick exit
00659 
00660   // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
00661   // and slow us down a lot.  Just mark them overdefined.
00662   if (PN.getNumIncomingValues() > 64)
00663     return markOverdefined(&PN);
00664 
00665   // Look at all of the executable operands of the PHI node.  If any of them
00666   // are overdefined, the PHI becomes overdefined as well.  If they are all
00667   // constant, and they agree with each other, the PHI becomes the identical
00668   // constant.  If they are constant and don't agree, the PHI is overdefined.
00669   // If there are no executable operands, the PHI remains undefined.
00670   //
00671   Constant *OperandVal = 0;
00672   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
00673     LatticeVal IV = getValueState(PN.getIncomingValue(i));
00674     if (IV.isUndefined()) continue;  // Doesn't influence PHI node.
00675 
00676     if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
00677       continue;
00678 
00679     if (IV.isOverdefined())    // PHI node becomes overdefined!
00680       return markOverdefined(&PN);
00681 
00682     if (OperandVal == 0) {   // Grab the first value.
00683       OperandVal = IV.getConstant();
00684       continue;
00685     }
00686 
00687     // There is already a reachable operand.  If we conflict with it,
00688     // then the PHI node becomes overdefined.  If we agree with it, we
00689     // can continue on.
00690 
00691     // Check to see if there are two different constants merging, if so, the PHI
00692     // node is overdefined.
00693     if (IV.getConstant() != OperandVal)
00694       return markOverdefined(&PN);
00695   }
00696 
00697   // If we exited the loop, this means that the PHI node only has constant
00698   // arguments that agree with each other(and OperandVal is the constant) or
00699   // OperandVal is null because there are no defined incoming arguments.  If
00700   // this is the case, the PHI remains undefined.
00701   //
00702   if (OperandVal)
00703     markConstant(&PN, OperandVal);      // Acquire operand value
00704 }
00705 
00706 void SCCPSolver::visitReturnInst(ReturnInst &I) {
00707   if (I.getNumOperands() == 0) return;  // ret void
00708 
00709   Function *F = I.getParent()->getParent();
00710   Value *ResultOp = I.getOperand(0);
00711 
00712   // If we are tracking the return value of this function, merge it in.
00713   if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
00714     DenseMap<Function*, LatticeVal>::iterator TFRVI =
00715       TrackedRetVals.find(F);
00716     if (TFRVI != TrackedRetVals.end()) {
00717       mergeInValue(TFRVI->second, F, getValueState(ResultOp));
00718       return;
00719     }
00720   }
00721 
00722   // Handle functions that return multiple values.
00723   if (!TrackedMultipleRetVals.empty()) {
00724     if (StructType *STy = dyn_cast<StructType>(ResultOp->getType()))
00725       if (MRVFunctionsTracked.count(F))
00726         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
00727           mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
00728                        getStructValueState(ResultOp, i));
00729 
00730   }
00731 }
00732 
00733 void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) {
00734   SmallVector<bool, 16> SuccFeasible;
00735   getFeasibleSuccessors(TI, SuccFeasible);
00736 
00737   BasicBlock *BB = TI.getParent();
00738 
00739   // Mark all feasible successors executable.
00740   for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
00741     if (SuccFeasible[i])
00742       markEdgeExecutable(BB, TI.getSuccessor(i));
00743 }
00744 
00745 void SCCPSolver::visitCastInst(CastInst &I) {
00746   LatticeVal OpSt = getValueState(I.getOperand(0));
00747   if (OpSt.isOverdefined())          // Inherit overdefinedness of operand
00748     markOverdefined(&I);
00749   else if (OpSt.isConstant())        // Propagate constant value
00750     markConstant(&I, ConstantExpr::getCast(I.getOpcode(),
00751                                            OpSt.getConstant(), I.getType()));
00752 }
00753 
00754 
00755 void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
00756   // If this returns a struct, mark all elements over defined, we don't track
00757   // structs in structs.
00758   if (EVI.getType()->isStructTy())
00759     return markAnythingOverdefined(&EVI);
00760 
00761   // If this is extracting from more than one level of struct, we don't know.
00762   if (EVI.getNumIndices() != 1)
00763     return markOverdefined(&EVI);
00764 
00765   Value *AggVal = EVI.getAggregateOperand();
00766   if (AggVal->getType()->isStructTy()) {
00767     unsigned i = *EVI.idx_begin();
00768     LatticeVal EltVal = getStructValueState(AggVal, i);
00769     mergeInValue(getValueState(&EVI), &EVI, EltVal);
00770   } else {
00771     // Otherwise, must be extracting from an array.
00772     return markOverdefined(&EVI);
00773   }
00774 }
00775 
00776 void SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) {
00777   StructType *STy = dyn_cast<StructType>(IVI.getType());
00778   if (STy == 0)
00779     return markOverdefined(&IVI);
00780 
00781   // If this has more than one index, we can't handle it, drive all results to
00782   // undef.
00783   if (IVI.getNumIndices() != 1)
00784     return markAnythingOverdefined(&IVI);
00785 
00786   Value *Aggr = IVI.getAggregateOperand();
00787   unsigned Idx = *IVI.idx_begin();
00788 
00789   // Compute the result based on what we're inserting.
00790   for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
00791     // This passes through all values that aren't the inserted element.
00792     if (i != Idx) {
00793       LatticeVal EltVal = getStructValueState(Aggr, i);
00794       mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
00795       continue;
00796     }
00797 
00798     Value *Val = IVI.getInsertedValueOperand();
00799     if (Val->getType()->isStructTy())
00800       // We don't track structs in structs.
00801       markOverdefined(getStructValueState(&IVI, i), &IVI);
00802     else {
00803       LatticeVal InVal = getValueState(Val);
00804       mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
00805     }
00806   }
00807 }
00808 
00809 void SCCPSolver::visitSelectInst(SelectInst &I) {
00810   // If this select returns a struct, just mark the result overdefined.
00811   // TODO: We could do a lot better than this if code actually uses this.
00812   if (I.getType()->isStructTy())
00813     return markAnythingOverdefined(&I);
00814 
00815   LatticeVal CondValue = getValueState(I.getCondition());
00816   if (CondValue.isUndefined())
00817     return;
00818 
00819   if (ConstantInt *CondCB = CondValue.getConstantInt()) {
00820     Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
00821     mergeInValue(&I, getValueState(OpVal));
00822     return;
00823   }
00824 
00825   // Otherwise, the condition is overdefined or a constant we can't evaluate.
00826   // See if we can produce something better than overdefined based on the T/F
00827   // value.
00828   LatticeVal TVal = getValueState(I.getTrueValue());
00829   LatticeVal FVal = getValueState(I.getFalseValue());
00830 
00831   // select ?, C, C -> C.
00832   if (TVal.isConstant() && FVal.isConstant() &&
00833       TVal.getConstant() == FVal.getConstant())
00834     return markConstant(&I, FVal.getConstant());
00835 
00836   if (TVal.isUndefined())   // select ?, undef, X -> X.
00837     return mergeInValue(&I, FVal);
00838   if (FVal.isUndefined())   // select ?, X, undef -> X.
00839     return mergeInValue(&I, TVal);
00840   markOverdefined(&I);
00841 }
00842 
00843 // Handle Binary Operators.
00844 void SCCPSolver::visitBinaryOperator(Instruction &I) {
00845   LatticeVal V1State = getValueState(I.getOperand(0));
00846   LatticeVal V2State = getValueState(I.getOperand(1));
00847 
00848   LatticeVal &IV = ValueState[&I];
00849   if (IV.isOverdefined()) return;
00850 
00851   if (V1State.isConstant() && V2State.isConstant())
00852     return markConstant(IV, &I,
00853                         ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
00854                                           V2State.getConstant()));
00855 
00856   // If something is undef, wait for it to resolve.
00857   if (!V1State.isOverdefined() && !V2State.isOverdefined())
00858     return;
00859 
00860   // Otherwise, one of our operands is overdefined.  Try to produce something
00861   // better than overdefined with some tricks.
00862 
00863   // If this is an AND or OR with 0 or -1, it doesn't matter that the other
00864   // operand is overdefined.
00865   if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) {
00866     LatticeVal *NonOverdefVal = 0;
00867     if (!V1State.isOverdefined())
00868       NonOverdefVal = &V1State;
00869     else if (!V2State.isOverdefined())
00870       NonOverdefVal = &V2State;
00871 
00872     if (NonOverdefVal) {
00873       if (NonOverdefVal->isUndefined()) {
00874         // Could annihilate value.
00875         if (I.getOpcode() == Instruction::And)
00876           markConstant(IV, &I, Constant::getNullValue(I.getType()));
00877         else if (VectorType *PT = dyn_cast<VectorType>(I.getType()))
00878           markConstant(IV, &I, Constant::getAllOnesValue(PT));
00879         else
00880           markConstant(IV, &I,
00881                        Constant::getAllOnesValue(I.getType()));
00882         return;
00883       }
00884 
00885       if (I.getOpcode() == Instruction::And) {
00886         // X and 0 = 0
00887         if (NonOverdefVal->getConstant()->isNullValue())
00888           return markConstant(IV, &I, NonOverdefVal->getConstant());
00889       } else {
00890         if (ConstantInt *CI = NonOverdefVal->getConstantInt())
00891           if (CI->isAllOnesValue())     // X or -1 = -1
00892             return markConstant(IV, &I, NonOverdefVal->getConstant());
00893       }
00894     }
00895   }
00896 
00897 
00898   markOverdefined(&I);
00899 }
00900 
00901 // Handle ICmpInst instruction.
00902 void SCCPSolver::visitCmpInst(CmpInst &I) {
00903   LatticeVal V1State = getValueState(I.getOperand(0));
00904   LatticeVal V2State = getValueState(I.getOperand(1));
00905 
00906   LatticeVal &IV = ValueState[&I];
00907   if (IV.isOverdefined()) return;
00908 
00909   if (V1State.isConstant() && V2State.isConstant())
00910     return markConstant(IV, &I, ConstantExpr::getCompare(I.getPredicate(),
00911                                                          V1State.getConstant(),
00912                                                         V2State.getConstant()));
00913 
00914   // If operands are still undefined, wait for it to resolve.
00915   if (!V1State.isOverdefined() && !V2State.isOverdefined())
00916     return;
00917 
00918   markOverdefined(&I);
00919 }
00920 
00921 void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
00922   // TODO : SCCP does not handle vectors properly.
00923   return markOverdefined(&I);
00924 
00925 #if 0
00926   LatticeVal &ValState = getValueState(I.getOperand(0));
00927   LatticeVal &IdxState = getValueState(I.getOperand(1));
00928 
00929   if (ValState.isOverdefined() || IdxState.isOverdefined())
00930     markOverdefined(&I);
00931   else if(ValState.isConstant() && IdxState.isConstant())
00932     markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(),
00933                                                      IdxState.getConstant()));
00934 #endif
00935 }
00936 
00937 void SCCPSolver::visitInsertElementInst(InsertElementInst &I) {
00938   // TODO : SCCP does not handle vectors properly.
00939   return markOverdefined(&I);
00940 #if 0
00941   LatticeVal &ValState = getValueState(I.getOperand(0));
00942   LatticeVal &EltState = getValueState(I.getOperand(1));
00943   LatticeVal &IdxState = getValueState(I.getOperand(2));
00944 
00945   if (ValState.isOverdefined() || EltState.isOverdefined() ||
00946       IdxState.isOverdefined())
00947     markOverdefined(&I);
00948   else if(ValState.isConstant() && EltState.isConstant() &&
00949           IdxState.isConstant())
00950     markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(),
00951                                                     EltState.getConstant(),
00952                                                     IdxState.getConstant()));
00953   else if (ValState.isUndefined() && EltState.isConstant() &&
00954            IdxState.isConstant())
00955     markConstant(&I,ConstantExpr::getInsertElement(UndefValue::get(I.getType()),
00956                                                    EltState.getConstant(),
00957                                                    IdxState.getConstant()));
00958 #endif
00959 }
00960 
00961 void SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) {
00962   // TODO : SCCP does not handle vectors properly.
00963   return markOverdefined(&I);
00964 #if 0
00965   LatticeVal &V1State   = getValueState(I.getOperand(0));
00966   LatticeVal &V2State   = getValueState(I.getOperand(1));
00967   LatticeVal &MaskState = getValueState(I.getOperand(2));
00968 
00969   if (MaskState.isUndefined() ||
00970       (V1State.isUndefined() && V2State.isUndefined()))
00971     return;  // Undefined output if mask or both inputs undefined.
00972 
00973   if (V1State.isOverdefined() || V2State.isOverdefined() ||
00974       MaskState.isOverdefined()) {
00975     markOverdefined(&I);
00976   } else {
00977     // A mix of constant/undef inputs.
00978     Constant *V1 = V1State.isConstant() ?
00979         V1State.getConstant() : UndefValue::get(I.getType());
00980     Constant *V2 = V2State.isConstant() ?
00981         V2State.getConstant() : UndefValue::get(I.getType());
00982     Constant *Mask = MaskState.isConstant() ?
00983       MaskState.getConstant() : UndefValue::get(I.getOperand(2)->getType());
00984     markConstant(&I, ConstantExpr::getShuffleVector(V1, V2, Mask));
00985   }
00986 #endif
00987 }
00988 
00989 // Handle getelementptr instructions.  If all operands are constants then we
00990 // can turn this into a getelementptr ConstantExpr.
00991 //
00992 void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
00993   if (ValueState[&I].isOverdefined()) return;
00994 
00995   SmallVector<Constant*, 8> Operands;
00996   Operands.reserve(I.getNumOperands());
00997 
00998   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
00999     LatticeVal State = getValueState(I.getOperand(i));
01000     if (State.isUndefined())
01001       return;  // Operands are not resolved yet.
01002 
01003     if (State.isOverdefined())
01004       return markOverdefined(&I);
01005 
01006     assert(State.isConstant() && "Unknown state!");
01007     Operands.push_back(State.getConstant());
01008   }
01009 
01010   Constant *Ptr = Operands[0];
01011   ArrayRef<Constant *> Indices(Operands.begin() + 1, Operands.end());
01012   markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Indices));
01013 }
01014 
01015 void SCCPSolver::visitStoreInst(StoreInst &SI) {
01016   // If this store is of a struct, ignore it.
01017   if (SI.getOperand(0)->getType()->isStructTy())
01018     return;
01019 
01020   if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
01021     return;
01022 
01023   GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
01024   DenseMap<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV);
01025   if (I == TrackedGlobals.end() || I->second.isOverdefined()) return;
01026 
01027   // Get the value we are storing into the global, then merge it.
01028   mergeInValue(I->second, GV, getValueState(SI.getOperand(0)));
01029   if (I->second.isOverdefined())
01030     TrackedGlobals.erase(I);      // No need to keep tracking this!
01031 }
01032 
01033 
01034 // Handle load instructions.  If the operand is a constant pointer to a constant
01035 // global, we can replace the load with the loaded constant value!
01036 void SCCPSolver::visitLoadInst(LoadInst &I) {
01037   // If this load is of a struct, just mark the result overdefined.
01038   if (I.getType()->isStructTy())
01039     return markAnythingOverdefined(&I);
01040 
01041   LatticeVal PtrVal = getValueState(I.getOperand(0));
01042   if (PtrVal.isUndefined()) return;   // The pointer is not resolved yet!
01043 
01044   LatticeVal &IV = ValueState[&I];
01045   if (IV.isOverdefined()) return;
01046 
01047   if (!PtrVal.isConstant() || I.isVolatile())
01048     return markOverdefined(IV, &I);
01049 
01050   Constant *Ptr = PtrVal.getConstant();
01051 
01052   // load null -> null
01053   if (isa<ConstantPointerNull>(Ptr) && I.getPointerAddressSpace() == 0)
01054     return markConstant(IV, &I, Constant::getNullValue(I.getType()));
01055 
01056   // Transform load (constant global) into the value loaded.
01057   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
01058     if (!TrackedGlobals.empty()) {
01059       // If we are tracking this global, merge in the known value for it.
01060       DenseMap<GlobalVariable*, LatticeVal>::iterator It =
01061         TrackedGlobals.find(GV);
01062       if (It != TrackedGlobals.end()) {
01063         mergeInValue(IV, &I, It->second);
01064         return;
01065       }
01066     }
01067   }
01068 
01069   // Transform load from a constant into a constant if possible.
01070   if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, TD))
01071     return markConstant(IV, &I, C);
01072 
01073   // Otherwise we cannot say for certain what value this load will produce.
01074   // Bail out.
01075   markOverdefined(IV, &I);
01076 }
01077 
01078 void SCCPSolver::visitCallSite(CallSite CS) {
01079   Function *F = CS.getCalledFunction();
01080   Instruction *I = CS.getInstruction();
01081 
01082   // The common case is that we aren't tracking the callee, either because we
01083   // are not doing interprocedural analysis or the callee is indirect, or is
01084   // external.  Handle these cases first.
01085   if (F == 0 || F->isDeclaration()) {
01086 CallOverdefined:
01087     // Void return and not tracking callee, just bail.
01088     if (I->getType()->isVoidTy()) return;
01089 
01090     // Otherwise, if we have a single return value case, and if the function is
01091     // a declaration, maybe we can constant fold it.
01092     if (F && F->isDeclaration() && !I->getType()->isStructTy() &&
01093         canConstantFoldCallTo(F)) {
01094 
01095       SmallVector<Constant*, 8> Operands;
01096       for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
01097            AI != E; ++AI) {
01098         LatticeVal State = getValueState(*AI);
01099 
01100         if (State.isUndefined())
01101           return;  // Operands are not resolved yet.
01102         if (State.isOverdefined())
01103           return markOverdefined(I);
01104         assert(State.isConstant() && "Unknown state!");
01105         Operands.push_back(State.getConstant());
01106       }
01107 
01108       // If we can constant fold this, mark the result of the call as a
01109       // constant.
01110       if (Constant *C = ConstantFoldCall(F, Operands, TLI))
01111         return markConstant(I, C);
01112     }
01113 
01114     // Otherwise, we don't know anything about this call, mark it overdefined.
01115     return markAnythingOverdefined(I);
01116   }
01117 
01118   // If this is a local function that doesn't have its address taken, mark its
01119   // entry block executable and merge in the actual arguments to the call into
01120   // the formal arguments of the function.
01121   if (!TrackingIncomingArguments.empty() && TrackingIncomingArguments.count(F)){
01122     MarkBlockExecutable(F->begin());
01123 
01124     // Propagate information from this call site into the callee.
01125     CallSite::arg_iterator CAI = CS.arg_begin();
01126     for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
01127          AI != E; ++AI, ++CAI) {
01128       // If this argument is byval, and if the function is not readonly, there
01129       // will be an implicit copy formed of the input aggregate.
01130       if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
01131         markOverdefined(AI);
01132         continue;
01133       }
01134 
01135       if (StructType *STy = dyn_cast<StructType>(AI->getType())) {
01136         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
01137           LatticeVal CallArg = getStructValueState(*CAI, i);
01138           mergeInValue(getStructValueState(AI, i), AI, CallArg);
01139         }
01140       } else {
01141         mergeInValue(AI, getValueState(*CAI));
01142       }
01143     }
01144   }
01145 
01146   // If this is a single/zero retval case, see if we're tracking the function.
01147   if (StructType *STy = dyn_cast<StructType>(F->getReturnType())) {
01148     if (!MRVFunctionsTracked.count(F))
01149       goto CallOverdefined;  // Not tracking this callee.
01150 
01151     // If we are tracking this callee, propagate the result of the function
01152     // into this call site.
01153     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
01154       mergeInValue(getStructValueState(I, i), I,
01155                    TrackedMultipleRetVals[std::make_pair(F, i)]);
01156   } else {
01157     DenseMap<Function*, LatticeVal>::iterator TFRVI = TrackedRetVals.find(F);
01158     if (TFRVI == TrackedRetVals.end())
01159       goto CallOverdefined;  // Not tracking this callee.
01160 
01161     // If so, propagate the return value of the callee into this call result.
01162     mergeInValue(I, TFRVI->second);
01163   }
01164 }
01165 
01166 void SCCPSolver::Solve() {
01167   // Process the work lists until they are empty!
01168   while (!BBWorkList.empty() || !InstWorkList.empty() ||
01169          !OverdefinedInstWorkList.empty()) {
01170     // Process the overdefined instruction's work list first, which drives other
01171     // things to overdefined more quickly.
01172     while (!OverdefinedInstWorkList.empty()) {
01173       Value *I = OverdefinedInstWorkList.pop_back_val();
01174 
01175       DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n');
01176 
01177       // "I" got into the work list because it either made the transition from
01178       // bottom to constant, or to overdefined.
01179       //
01180       // Anything on this worklist that is overdefined need not be visited
01181       // since all of its users will have already been marked as overdefined
01182       // Update all of the users of this instruction's value.
01183       //
01184       for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
01185            UI != E; ++UI)
01186         if (Instruction *I = dyn_cast<Instruction>(*UI))
01187           OperandChangedState(I);
01188     }
01189 
01190     // Process the instruction work list.
01191     while (!InstWorkList.empty()) {
01192       Value *I = InstWorkList.pop_back_val();
01193 
01194       DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
01195 
01196       // "I" got into the work list because it made the transition from undef to
01197       // constant.
01198       //
01199       // Anything on this worklist that is overdefined need not be visited
01200       // since all of its users will have already been marked as overdefined.
01201       // Update all of the users of this instruction's value.
01202       //
01203       if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
01204         for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
01205              UI != E; ++UI)
01206           if (Instruction *I = dyn_cast<Instruction>(*UI))
01207             OperandChangedState(I);
01208     }
01209 
01210     // Process the basic block work list.
01211     while (!BBWorkList.empty()) {
01212       BasicBlock *BB = BBWorkList.back();
01213       BBWorkList.pop_back();
01214 
01215       DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
01216 
01217       // Notify all instructions in this basic block that they are newly
01218       // executable.
01219       visit(BB);
01220     }
01221   }
01222 }
01223 
01224 /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
01225 /// that branches on undef values cannot reach any of their successors.
01226 /// However, this is not a safe assumption.  After we solve dataflow, this
01227 /// method should be use to handle this.  If this returns true, the solver
01228 /// should be rerun.
01229 ///
01230 /// This method handles this by finding an unresolved branch and marking it one
01231 /// of the edges from the block as being feasible, even though the condition
01232 /// doesn't say it would otherwise be.  This allows SCCP to find the rest of the
01233 /// CFG and only slightly pessimizes the analysis results (by marking one,
01234 /// potentially infeasible, edge feasible).  This cannot usefully modify the
01235 /// constraints on the condition of the branch, as that would impact other users
01236 /// of the value.
01237 ///
01238 /// This scan also checks for values that use undefs, whose results are actually
01239 /// defined.  For example, 'zext i8 undef to i32' should produce all zeros
01240 /// conservatively, as "(zext i8 X -> i32) & 0xFF00" must always return zero,
01241 /// even if X isn't defined.
01242 bool SCCPSolver::ResolvedUndefsIn(Function &F) {
01243   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
01244     if (!BBExecutable.count(BB))
01245       continue;
01246 
01247     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
01248       // Look for instructions which produce undef values.
01249       if (I->getType()->isVoidTy()) continue;
01250 
01251       if (StructType *STy = dyn_cast<StructType>(I->getType())) {
01252         // Only a few things that can be structs matter for undef.
01253 
01254         // Tracked calls must never be marked overdefined in ResolvedUndefsIn.
01255         if (CallSite CS = CallSite(I))
01256           if (Function *F = CS.getCalledFunction())
01257             if (MRVFunctionsTracked.count(F))
01258               continue;
01259 
01260         // extractvalue and insertvalue don't need to be marked; they are
01261         // tracked as precisely as their operands.
01262         if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I))
01263           continue;
01264 
01265         // Send the results of everything else to overdefined.  We could be
01266         // more precise than this but it isn't worth bothering.
01267         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
01268           LatticeVal &LV = getStructValueState(I, i);
01269           if (LV.isUndefined())
01270             markOverdefined(LV, I);
01271         }
01272         continue;
01273       }
01274 
01275       LatticeVal &LV = getValueState(I);
01276       if (!LV.isUndefined()) continue;
01277 
01278       // extractvalue is safe; check here because the argument is a struct.
01279       if (isa<ExtractValueInst>(I))
01280         continue;
01281 
01282       // Compute the operand LatticeVals, for convenience below.
01283       // Anything taking a struct is conservatively assumed to require
01284       // overdefined markings.
01285       if (I->getOperand(0)->getType()->isStructTy()) {
01286         markOverdefined(I);
01287         return true;
01288       }
01289       LatticeVal Op0LV = getValueState(I->getOperand(0));
01290       LatticeVal Op1LV;
01291       if (I->getNumOperands() == 2) {
01292         if (I->getOperand(1)->getType()->isStructTy()) {
01293           markOverdefined(I);
01294           return true;
01295         }
01296 
01297         Op1LV = getValueState(I->getOperand(1));
01298       }
01299       // If this is an instructions whose result is defined even if the input is
01300       // not fully defined, propagate the information.
01301       Type *ITy = I->getType();
01302       switch (I->getOpcode()) {
01303       case Instruction::Add:
01304       case Instruction::Sub:
01305       case Instruction::Trunc:
01306       case Instruction::FPTrunc:
01307       case Instruction::BitCast:
01308         break; // Any undef -> undef
01309       case Instruction::FSub:
01310       case Instruction::FAdd:
01311       case Instruction::FMul:
01312       case Instruction::FDiv:
01313       case Instruction::FRem:
01314         // Floating-point binary operation: be conservative.
01315         if (Op0LV.isUndefined() && Op1LV.isUndefined())
01316           markForcedConstant(I, Constant::getNullValue(ITy));
01317         else
01318           markOverdefined(I);
01319         return true;
01320       case Instruction::ZExt:
01321       case Instruction::SExt:
01322       case Instruction::FPToUI:
01323       case Instruction::FPToSI:
01324       case Instruction::FPExt:
01325       case Instruction::PtrToInt:
01326       case Instruction::IntToPtr:
01327       case Instruction::SIToFP:
01328       case Instruction::UIToFP:
01329         // undef -> 0; some outputs are impossible
01330         markForcedConstant(I, Constant::getNullValue(ITy));
01331         return true;
01332       case Instruction::Mul:
01333       case Instruction::And:
01334         // Both operands undef -> undef
01335         if (Op0LV.isUndefined() && Op1LV.isUndefined())
01336           break;
01337         // undef * X -> 0.   X could be zero.
01338         // undef & X -> 0.   X could be zero.
01339         markForcedConstant(I, Constant::getNullValue(ITy));
01340         return true;
01341 
01342       case Instruction::Or:
01343         // Both operands undef -> undef
01344         if (Op0LV.isUndefined() && Op1LV.isUndefined())
01345           break;
01346         // undef | X -> -1.   X could be -1.
01347         markForcedConstant(I, Constant::getAllOnesValue(ITy));
01348         return true;
01349 
01350       case Instruction::Xor:
01351         // undef ^ undef -> 0; strictly speaking, this is not strictly
01352         // necessary, but we try to be nice to people who expect this
01353         // behavior in simple cases
01354         if (Op0LV.isUndefined() && Op1LV.isUndefined()) {
01355           markForcedConstant(I, Constant::getNullValue(ITy));
01356           return true;
01357         }
01358         // undef ^ X -> undef
01359         break;
01360 
01361       case Instruction::SDiv:
01362       case Instruction::UDiv:
01363       case Instruction::SRem:
01364       case Instruction::URem:
01365         // X / undef -> undef.  No change.
01366         // X % undef -> undef.  No change.
01367         if (Op1LV.isUndefined()) break;
01368 
01369         // undef / X -> 0.   X could be maxint.
01370         // undef % X -> 0.   X could be 1.
01371         markForcedConstant(I, Constant::getNullValue(ITy));
01372         return true;
01373 
01374       case Instruction::AShr:
01375         // X >>a undef -> undef.
01376         if (Op1LV.isUndefined()) break;
01377 
01378         // undef >>a X -> all ones
01379         markForcedConstant(I, Constant::getAllOnesValue(ITy));
01380         return true;
01381       case Instruction::LShr:
01382       case Instruction::Shl:
01383         // X << undef -> undef.
01384         // X >> undef -> undef.
01385         if (Op1LV.isUndefined()) break;
01386 
01387         // undef << X -> 0
01388         // undef >> X -> 0
01389         markForcedConstant(I, Constant::getNullValue(ITy));
01390         return true;
01391       case Instruction::Select:
01392         Op1LV = getValueState(I->getOperand(1));
01393         // undef ? X : Y  -> X or Y.  There could be commonality between X/Y.
01394         if (Op0LV.isUndefined()) {
01395           if (!Op1LV.isConstant())  // Pick the constant one if there is any.
01396             Op1LV = getValueState(I->getOperand(2));
01397         } else if (Op1LV.isUndefined()) {
01398           // c ? undef : undef -> undef.  No change.
01399           Op1LV = getValueState(I->getOperand(2));
01400           if (Op1LV.isUndefined())
01401             break;
01402           // Otherwise, c ? undef : x -> x.
01403         } else {
01404           // Leave Op1LV as Operand(1)'s LatticeValue.
01405         }
01406 
01407         if (Op1LV.isConstant())
01408           markForcedConstant(I, Op1LV.getConstant());
01409         else
01410           markOverdefined(I);
01411         return true;
01412       case Instruction::Load:
01413         // A load here means one of two things: a load of undef from a global,
01414         // a load from an unknown pointer.  Either way, having it return undef
01415         // is okay.
01416         break;
01417       case Instruction::ICmp:
01418         // X == undef -> undef.  Other comparisons get more complicated.
01419         if (cast<ICmpInst>(I)->isEquality())
01420           break;
01421         markOverdefined(I);
01422         return true;
01423       case Instruction::Call:
01424       case Instruction::Invoke: {
01425         // There are two reasons a call can have an undef result
01426         // 1. It could be tracked.
01427         // 2. It could be constant-foldable.
01428         // Because of the way we solve return values, tracked calls must
01429         // never be marked overdefined in ResolvedUndefsIn.
01430         if (Function *F = CallSite(I).getCalledFunction())
01431           if (TrackedRetVals.count(F))
01432             break;
01433 
01434         // If the call is constant-foldable, we mark it overdefined because
01435         // we do not know what return values are valid.
01436         markOverdefined(I);
01437         return true;
01438       }
01439       default:
01440         // If we don't know what should happen here, conservatively mark it
01441         // overdefined.
01442         markOverdefined(I);
01443         return true;
01444       }
01445     }
01446 
01447     // Check to see if we have a branch or switch on an undefined value.  If so
01448     // we force the branch to go one way or the other to make the successor
01449     // values live.  It doesn't really matter which way we force it.
01450     TerminatorInst *TI = BB->getTerminator();
01451     if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
01452       if (!BI->isConditional()) continue;
01453       if (!getValueState(BI->getCondition()).isUndefined())
01454         continue;
01455 
01456       // If the input to SCCP is actually branch on undef, fix the undef to
01457       // false.
01458       if (isa<UndefValue>(BI->getCondition())) {
01459         BI->setCondition(ConstantInt::getFalse(BI->getContext()));
01460         markEdgeExecutable(BB, TI->getSuccessor(1));
01461         return true;
01462       }
01463 
01464       // Otherwise, it is a branch on a symbolic value which is currently
01465       // considered to be undef.  Handle this by forcing the input value to the
01466       // branch to false.
01467       markForcedConstant(BI->getCondition(),
01468                          ConstantInt::getFalse(TI->getContext()));
01469       return true;
01470     }
01471 
01472     if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
01473       if (!SI->getNumCases())
01474         continue;
01475       if (!getValueState(SI->getCondition()).isUndefined())
01476         continue;
01477 
01478       // If the input to SCCP is actually switch on undef, fix the undef to
01479       // the first constant.
01480       if (isa<UndefValue>(SI->getCondition())) {
01481         SI->setCondition(SI->case_begin().getCaseValue());
01482         markEdgeExecutable(BB, SI->case_begin().getCaseSuccessor());
01483         return true;
01484       }
01485 
01486       markForcedConstant(SI->getCondition(), SI->case_begin().getCaseValue());
01487       return true;
01488     }
01489   }
01490 
01491   return false;
01492 }
01493 
01494 
01495 namespace {
01496   //===--------------------------------------------------------------------===//
01497   //
01498   /// SCCP Class - This class uses the SCCPSolver to implement a per-function
01499   /// Sparse Conditional Constant Propagator.
01500   ///
01501   struct SCCP : public FunctionPass {
01502     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
01503       AU.addRequired<TargetLibraryInfo>();
01504     }
01505     static char ID; // Pass identification, replacement for typeid
01506     SCCP() : FunctionPass(ID) {
01507       initializeSCCPPass(*PassRegistry::getPassRegistry());
01508     }
01509 
01510     // runOnFunction - Run the Sparse Conditional Constant Propagation
01511     // algorithm, and return true if the function was modified.
01512     //
01513     bool runOnFunction(Function &F);
01514   };
01515 } // end anonymous namespace
01516 
01517 char SCCP::ID = 0;
01518 INITIALIZE_PASS(SCCP, "sccp",
01519                 "Sparse Conditional Constant Propagation", false, false)
01520 
01521 // createSCCPPass - This is the public interface to this file.
01522 FunctionPass *llvm::createSCCPPass() {
01523   return new SCCP();
01524 }
01525 
01526 static void DeleteInstructionInBlock(BasicBlock *BB) {
01527   DEBUG(dbgs() << "  BasicBlock Dead:" << *BB);
01528   ++NumDeadBlocks;
01529 
01530   // Check to see if there are non-terminating instructions to delete.
01531   if (isa<TerminatorInst>(BB->begin()))
01532     return;
01533 
01534   // Delete the instructions backwards, as it has a reduced likelihood of having
01535   // to update as many def-use and use-def chains.
01536   Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
01537   while (EndInst != BB->begin()) {
01538     // Delete the next to last instruction.
01539     BasicBlock::iterator I = EndInst;
01540     Instruction *Inst = --I;
01541     if (!Inst->use_empty())
01542       Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
01543     if (isa<LandingPadInst>(Inst)) {
01544       EndInst = Inst;
01545       continue;
01546     }
01547     BB->getInstList().erase(Inst);
01548     ++NumInstRemoved;
01549   }
01550 }
01551 
01552 // runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
01553 // and return true if the function was modified.
01554 //
01555 bool SCCP::runOnFunction(Function &F) {
01556   DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n");
01557   const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
01558   const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
01559   SCCPSolver Solver(TD, TLI);
01560 
01561   // Mark the first block of the function as being executable.
01562   Solver.MarkBlockExecutable(F.begin());
01563 
01564   // Mark all arguments to the function as being overdefined.
01565   for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
01566     Solver.markAnythingOverdefined(AI);
01567 
01568   // Solve for constants.
01569   bool ResolvedUndefs = true;
01570   while (ResolvedUndefs) {
01571     Solver.Solve();
01572     DEBUG(dbgs() << "RESOLVING UNDEFs\n");
01573     ResolvedUndefs = Solver.ResolvedUndefsIn(F);
01574   }
01575 
01576   bool MadeChanges = false;
01577 
01578   // If we decided that there are basic blocks that are dead in this function,
01579   // delete their contents now.  Note that we cannot actually delete the blocks,
01580   // as we cannot modify the CFG of the function.
01581 
01582   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
01583     if (!Solver.isBlockExecutable(BB)) {
01584       DeleteInstructionInBlock(BB);
01585       MadeChanges = true;
01586       continue;
01587     }
01588 
01589     // Iterate over all of the instructions in a function, replacing them with
01590     // constants if we have found them to be of constant values.
01591     //
01592     for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
01593       Instruction *Inst = BI++;
01594       if (Inst->getType()->isVoidTy() || isa<TerminatorInst>(Inst))
01595         continue;
01596 
01597       // TODO: Reconstruct structs from their elements.
01598       if (Inst->getType()->isStructTy())
01599         continue;
01600 
01601       LatticeVal IV = Solver.getLatticeValueFor(Inst);
01602       if (IV.isOverdefined())
01603         continue;
01604 
01605       Constant *Const = IV.isConstant()
01606         ? IV.getConstant() : UndefValue::get(Inst->getType());
01607       DEBUG(dbgs() << "  Constant: " << *Const << " = " << *Inst);
01608 
01609       // Replaces all of the uses of a variable with uses of the constant.
01610       Inst->replaceAllUsesWith(Const);
01611 
01612       // Delete the instruction.
01613       Inst->eraseFromParent();
01614 
01615       // Hey, we just changed something!
01616       MadeChanges = true;
01617       ++NumInstRemoved;
01618     }
01619   }
01620 
01621   return MadeChanges;
01622 }
01623 
01624 namespace {
01625   //===--------------------------------------------------------------------===//
01626   //
01627   /// IPSCCP Class - This class implements interprocedural Sparse Conditional
01628   /// Constant Propagation.
01629   ///
01630   struct IPSCCP : public ModulePass {
01631     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
01632       AU.addRequired<TargetLibraryInfo>();
01633     }
01634     static char ID;
01635     IPSCCP() : ModulePass(ID) {
01636       initializeIPSCCPPass(*PassRegistry::getPassRegistry());
01637     }
01638     bool runOnModule(Module &M);
01639   };
01640 } // end anonymous namespace
01641 
01642 char IPSCCP::ID = 0;
01643 INITIALIZE_PASS_BEGIN(IPSCCP, "ipsccp",
01644                 "Interprocedural Sparse Conditional Constant Propagation",
01645                 false, false)
01646 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
01647 INITIALIZE_PASS_END(IPSCCP, "ipsccp",
01648                 "Interprocedural Sparse Conditional Constant Propagation",
01649                 false, false)
01650 
01651 // createIPSCCPPass - This is the public interface to this file.
01652 ModulePass *llvm::createIPSCCPPass() {
01653   return new IPSCCP();
01654 }
01655 
01656 
01657 static bool AddressIsTaken(const GlobalValue *GV) {
01658   // Delete any dead constantexpr klingons.
01659   GV->removeDeadConstantUsers();
01660 
01661   for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end();
01662        UI != E; ++UI) {
01663     const User *U = *UI;
01664     if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
01665       if (SI->getOperand(0) == GV || SI->isVolatile())
01666         return true;  // Storing addr of GV.
01667     } else if (isa<InvokeInst>(U) || isa<CallInst>(U)) {
01668       // Make sure we are calling the function, not passing the address.
01669       ImmutableCallSite CS(cast<Instruction>(U));
01670       if (!CS.isCallee(UI))
01671         return true;
01672     } else if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
01673       if (LI->isVolatile())
01674         return true;
01675     } else if (isa<BlockAddress>(U)) {
01676       // blockaddress doesn't take the address of the function, it takes addr
01677       // of label.
01678     } else {
01679       return true;
01680     }
01681   }
01682   return false;
01683 }
01684 
01685 bool IPSCCP::runOnModule(Module &M) {
01686   const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
01687   const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
01688   SCCPSolver Solver(TD, TLI);
01689 
01690   // AddressTakenFunctions - This set keeps track of the address-taken functions
01691   // that are in the input.  As IPSCCP runs through and simplifies code,
01692   // functions that were address taken can end up losing their
01693   // address-taken-ness.  Because of this, we keep track of their addresses from
01694   // the first pass so we can use them for the later simplification pass.
01695   SmallPtrSet<Function*, 32> AddressTakenFunctions;
01696 
01697   // Loop over all functions, marking arguments to those with their addresses
01698   // taken or that are external as overdefined.
01699   //
01700   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
01701     if (F->isDeclaration())
01702       continue;
01703 
01704     // If this is a strong or ODR definition of this function, then we can
01705     // propagate information about its result into callsites of it.
01706     if (!F->mayBeOverridden())
01707       Solver.AddTrackedFunction(F);
01708 
01709     // If this function only has direct calls that we can see, we can track its
01710     // arguments and return value aggressively, and can assume it is not called
01711     // unless we see evidence to the contrary.
01712     if (F->hasLocalLinkage()) {
01713       if (AddressIsTaken(F))
01714         AddressTakenFunctions.insert(F);
01715       else {
01716         Solver.AddArgumentTrackedFunction(F);
01717         continue;
01718       }
01719     }
01720 
01721     // Assume the function is called.
01722     Solver.MarkBlockExecutable(F->begin());
01723 
01724     // Assume nothing about the incoming arguments.
01725     for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
01726          AI != E; ++AI)
01727       Solver.markAnythingOverdefined(AI);
01728   }
01729 
01730   // Loop over global variables.  We inform the solver about any internal global
01731   // variables that do not have their 'addresses taken'.  If they don't have
01732   // their addresses taken, we can propagate constants through them.
01733   for (Module::global_iterator G = M.global_begin(), E = M.global_end();
01734        G != E; ++G)
01735     if (!G->isConstant() && G->hasLocalLinkage() && !AddressIsTaken(G))
01736       Solver.TrackValueOfGlobalVariable(G);
01737 
01738   // Solve for constants.
01739   bool ResolvedUndefs = true;
01740   while (ResolvedUndefs) {
01741     Solver.Solve();
01742 
01743     DEBUG(dbgs() << "RESOLVING UNDEFS\n");
01744     ResolvedUndefs = false;
01745     for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
01746       ResolvedUndefs |= Solver.ResolvedUndefsIn(*F);
01747   }
01748 
01749   bool MadeChanges = false;
01750 
01751   // Iterate over all of the instructions in the module, replacing them with
01752   // constants if we have found them to be of constant values.
01753   //
01754   SmallVector<BasicBlock*, 512> BlocksToErase;
01755 
01756   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
01757     if (Solver.isBlockExecutable(F->begin())) {
01758       for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
01759            AI != E; ++AI) {
01760         if (AI->use_empty() || AI->getType()->isStructTy()) continue;
01761 
01762         // TODO: Could use getStructLatticeValueFor to find out if the entire
01763         // result is a constant and replace it entirely if so.
01764 
01765         LatticeVal IV = Solver.getLatticeValueFor(AI);
01766         if (IV.isOverdefined()) continue;
01767 
01768         Constant *CST = IV.isConstant() ?
01769         IV.getConstant() : UndefValue::get(AI->getType());
01770         DEBUG(dbgs() << "***  Arg " << *AI << " = " << *CST <<"\n");
01771 
01772         // Replaces all of the uses of a variable with uses of the
01773         // constant.
01774         AI->replaceAllUsesWith(CST);
01775         ++IPNumArgsElimed;
01776       }
01777     }
01778 
01779     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
01780       if (!Solver.isBlockExecutable(BB)) {
01781         DeleteInstructionInBlock(BB);
01782         MadeChanges = true;
01783 
01784         TerminatorInst *TI = BB->getTerminator();
01785         for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
01786           BasicBlock *Succ = TI->getSuccessor(i);
01787           if (!Succ->empty() && isa<PHINode>(Succ->begin()))
01788             TI->getSuccessor(i)->removePredecessor(BB);
01789         }
01790         if (!TI->use_empty())
01791           TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
01792         TI->eraseFromParent();
01793 
01794         if (&*BB != &F->front())
01795           BlocksToErase.push_back(BB);
01796         else
01797           new UnreachableInst(M.getContext(), BB);
01798         continue;
01799       }
01800 
01801       for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
01802         Instruction *Inst = BI++;
01803         if (Inst->getType()->isVoidTy() || Inst->getType()->isStructTy())
01804           continue;
01805 
01806         // TODO: Could use getStructLatticeValueFor to find out if the entire
01807         // result is a constant and replace it entirely if so.
01808 
01809         LatticeVal IV = Solver.getLatticeValueFor(Inst);
01810         if (IV.isOverdefined())
01811           continue;
01812 
01813         Constant *Const = IV.isConstant()
01814           ? IV.getConstant() : UndefValue::get(Inst->getType());
01815         DEBUG(dbgs() << "  Constant: " << *Const << " = " << *Inst);
01816 
01817         // Replaces all of the uses of a variable with uses of the
01818         // constant.
01819         Inst->replaceAllUsesWith(Const);
01820 
01821         // Delete the instruction.
01822         if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst))
01823           Inst->eraseFromParent();
01824 
01825         // Hey, we just changed something!
01826         MadeChanges = true;
01827         ++IPNumInstRemoved;
01828       }
01829     }
01830 
01831     // Now that all instructions in the function are constant folded, erase dead
01832     // blocks, because we can now use ConstantFoldTerminator to get rid of
01833     // in-edges.
01834     for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
01835       // If there are any PHI nodes in this successor, drop entries for BB now.
01836       BasicBlock *DeadBB = BlocksToErase[i];
01837       for (Value::use_iterator UI = DeadBB->use_begin(), UE = DeadBB->use_end();
01838            UI != UE; ) {
01839         // Grab the user and then increment the iterator early, as the user
01840         // will be deleted. Step past all adjacent uses from the same user.
01841         Instruction *I = dyn_cast<Instruction>(*UI);
01842         do { ++UI; } while (UI != UE && *UI == I);
01843 
01844         // Ignore blockaddress users; BasicBlock's dtor will handle them.
01845         if (!I) continue;
01846 
01847         bool Folded = ConstantFoldTerminator(I->getParent());
01848         if (!Folded) {
01849           // The constant folder may not have been able to fold the terminator
01850           // if this is a branch or switch on undef.  Fold it manually as a
01851           // branch to the first successor.
01852 #ifndef NDEBUG
01853           if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
01854             assert(BI->isConditional() && isa<UndefValue>(BI->getCondition()) &&
01855                    "Branch should be foldable!");
01856           } else if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
01857             assert(isa<UndefValue>(SI->getCondition()) && "Switch should fold");
01858           } else {
01859             llvm_unreachable("Didn't fold away reference to block!");
01860           }
01861 #endif
01862 
01863           // Make this an uncond branch to the first successor.
01864           TerminatorInst *TI = I->getParent()->getTerminator();
01865           BranchInst::Create(TI->getSuccessor(0), TI);
01866 
01867           // Remove entries in successor phi nodes to remove edges.
01868           for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i)
01869             TI->getSuccessor(i)->removePredecessor(TI->getParent());
01870 
01871           // Remove the old terminator.
01872           TI->eraseFromParent();
01873         }
01874       }
01875 
01876       // Finally, delete the basic block.
01877       F->getBasicBlockList().erase(DeadBB);
01878     }
01879     BlocksToErase.clear();
01880   }
01881 
01882   // If we inferred constant or undef return values for a function, we replaced
01883   // all call uses with the inferred value.  This means we don't need to bother
01884   // actually returning anything from the function.  Replace all return
01885   // instructions with return undef.
01886   //
01887   // Do this in two stages: first identify the functions we should process, then
01888   // actually zap their returns.  This is important because we can only do this
01889   // if the address of the function isn't taken.  In cases where a return is the
01890   // last use of a function, the order of processing functions would affect
01891   // whether other functions are optimizable.
01892   SmallVector<ReturnInst*, 8> ReturnsToZap;
01893 
01894   // TODO: Process multiple value ret instructions also.
01895   const DenseMap<Function*, LatticeVal> &RV = Solver.getTrackedRetVals();
01896   for (DenseMap<Function*, LatticeVal>::const_iterator I = RV.begin(),
01897        E = RV.end(); I != E; ++I) {
01898     Function *F = I->first;
01899     if (I->second.isOverdefined() || F->getReturnType()->isVoidTy())
01900       continue;
01901 
01902     // We can only do this if we know that nothing else can call the function.
01903     if (!F->hasLocalLinkage() || AddressTakenFunctions.count(F))
01904       continue;
01905 
01906     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
01907       if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
01908         if (!isa<UndefValue>(RI->getOperand(0)))
01909           ReturnsToZap.push_back(RI);
01910   }
01911 
01912   // Zap all returns which we've identified as zap to change.
01913   for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) {
01914     Function *F = ReturnsToZap[i]->getParent()->getParent();
01915     ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType()));
01916   }
01917 
01918   // If we inferred constant or undef values for globals variables, we can
01919   // delete the global and any stores that remain to it.
01920   const DenseMap<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals();
01921   for (DenseMap<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(),
01922          E = TG.end(); I != E; ++I) {
01923     GlobalVariable *GV = I->first;
01924     assert(!I->second.isOverdefined() &&
01925            "Overdefined values should have been taken out of the map!");
01926     DEBUG(dbgs() << "Found that GV '" << GV->getName() << "' is constant!\n");
01927     while (!GV->use_empty()) {
01928       StoreInst *SI = cast<StoreInst>(GV->use_back());
01929       SI->eraseFromParent();
01930     }
01931     M.getGlobalList().erase(GV);
01932     ++IPNumGlobalConst;
01933   }
01934 
01935   return MadeChanges;
01936 }