LLVM API Documentation

PromoteMemoryToRegister.cpp
Go to the documentation of this file.
00001 //===- PromoteMemoryToRegister.cpp - Convert allocas to registers ---------===//
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 promotes memory references to be register references.  It promotes
00011 // alloca instructions which only have loads and stores as uses.  An alloca is
00012 // transformed by using iterated dominator frontiers to place PHI nodes, then
00013 // traversing the function in depth-first order to rewrite loads and stores as
00014 // appropriate.
00015 //
00016 // The algorithm used here is based on:
00017 //
00018 //   Sreedhar and Gao. A linear time algorithm for placing phi-nodes.
00019 //   In Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of
00020 //   Programming Languages
00021 //   POPL '95. ACM, New York, NY, 62-73.
00022 //
00023 // It has been modified to not explicitly use the DJ graph data structure and to
00024 // directly compute pruned SSA using per-variable liveness information.
00025 //
00026 //===----------------------------------------------------------------------===//
00027 
00028 #define DEBUG_TYPE "mem2reg"
00029 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
00030 #include "llvm/ADT/DenseMap.h"
00031 #include "llvm/ADT/Hashing.h"
00032 #include "llvm/ADT/STLExtras.h"
00033 #include "llvm/ADT/SmallPtrSet.h"
00034 #include "llvm/ADT/SmallVector.h"
00035 #include "llvm/ADT/Statistic.h"
00036 #include "llvm/Analysis/AliasSetTracker.h"
00037 #include "llvm/Analysis/Dominators.h"
00038 #include "llvm/Analysis/InstructionSimplify.h"
00039 #include "llvm/Analysis/ValueTracking.h"
00040 #include "llvm/DIBuilder.h"
00041 #include "llvm/DebugInfo.h"
00042 #include "llvm/IR/Constants.h"
00043 #include "llvm/IR/DerivedTypes.h"
00044 #include "llvm/IR/Function.h"
00045 #include "llvm/IR/Instructions.h"
00046 #include "llvm/IR/IntrinsicInst.h"
00047 #include "llvm/IR/Metadata.h"
00048 #include "llvm/Support/CFG.h"
00049 #include "llvm/Transforms/Utils/Local.h"
00050 #include <algorithm>
00051 #include <queue>
00052 using namespace llvm;
00053 
00054 STATISTIC(NumLocalPromoted, "Number of alloca's promoted within one block");
00055 STATISTIC(NumSingleStore,   "Number of alloca's promoted with a single store");
00056 STATISTIC(NumDeadAlloca,    "Number of dead alloca's removed");
00057 STATISTIC(NumPHIInsert,     "Number of PHI nodes inserted");
00058 
00059 namespace llvm {
00060 template<>
00061 struct DenseMapInfo<std::pair<BasicBlock*, unsigned> > {
00062   typedef std::pair<BasicBlock*, unsigned> EltTy;
00063   static inline EltTy getEmptyKey() {
00064     return EltTy(reinterpret_cast<BasicBlock*>(-1), ~0U);
00065   }
00066   static inline EltTy getTombstoneKey() {
00067     return EltTy(reinterpret_cast<BasicBlock*>(-2), 0U);
00068   }
00069   static unsigned getHashValue(const std::pair<BasicBlock*, unsigned> &Val) {
00070     using llvm::hash_value;
00071     return static_cast<unsigned>(hash_value(Val));
00072   }
00073   static bool isEqual(const EltTy &LHS, const EltTy &RHS) {
00074     return LHS == RHS;
00075   }
00076 };
00077 }
00078 
00079 /// isAllocaPromotable - Return true if this alloca is legal for promotion.
00080 /// This is true if there are only loads and stores to the alloca.
00081 ///
00082 bool llvm::isAllocaPromotable(const AllocaInst *AI) {
00083   // FIXME: If the memory unit is of pointer or integer type, we can permit
00084   // assignments to subsections of the memory unit.
00085 
00086   // Only allow direct and non-volatile loads and stores...
00087   for (Value::const_use_iterator UI = AI->use_begin(), UE = AI->use_end();
00088        UI != UE; ++UI) {   // Loop over all of the uses of the alloca
00089     const User *U = *UI;
00090     if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
00091       // Note that atomic loads can be transformed; atomic semantics do
00092       // not have any meaning for a local alloca.
00093       if (LI->isVolatile())
00094         return false;
00095     } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
00096       if (SI->getOperand(0) == AI)
00097         return false;   // Don't allow a store OF the AI, only INTO the AI.
00098       // Note that atomic stores can be transformed; atomic semantics do
00099       // not have any meaning for a local alloca.
00100       if (SI->isVolatile())
00101         return false;
00102     } else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
00103       if (II->getIntrinsicID() != Intrinsic::lifetime_start &&
00104           II->getIntrinsicID() != Intrinsic::lifetime_end)
00105         return false;
00106     } else if (const BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
00107       if (BCI->getType() != Type::getInt8PtrTy(U->getContext()))
00108         return false;
00109       if (!onlyUsedByLifetimeMarkers(BCI))
00110         return false;
00111     } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
00112       if (GEPI->getType() != Type::getInt8PtrTy(U->getContext()))
00113         return false;
00114       if (!GEPI->hasAllZeroIndices())
00115         return false;
00116       if (!onlyUsedByLifetimeMarkers(GEPI))
00117         return false;
00118     } else {
00119       return false;
00120     }
00121   }
00122 
00123   return true;
00124 }
00125 
00126 namespace {
00127   struct AllocaInfo;
00128 
00129   // Data package used by RenamePass()
00130   class RenamePassData {
00131   public:
00132     typedef std::vector<Value *> ValVector;
00133     
00134     RenamePassData() : BB(NULL), Pred(NULL), Values() {}
00135     RenamePassData(BasicBlock *B, BasicBlock *P,
00136                    const ValVector &V) : BB(B), Pred(P), Values(V) {}
00137     BasicBlock *BB;
00138     BasicBlock *Pred;
00139     ValVector Values;
00140     
00141     void swap(RenamePassData &RHS) {
00142       std::swap(BB, RHS.BB);
00143       std::swap(Pred, RHS.Pred);
00144       Values.swap(RHS.Values);
00145     }
00146   };
00147   
00148   /// LargeBlockInfo - This assigns and keeps a per-bb relative ordering of
00149   /// load/store instructions in the block that directly load or store an alloca.
00150   ///
00151   /// This functionality is important because it avoids scanning large basic
00152   /// blocks multiple times when promoting many allocas in the same block.
00153   class LargeBlockInfo {
00154     /// InstNumbers - For each instruction that we track, keep the index of the
00155     /// instruction.  The index starts out as the number of the instruction from
00156     /// the start of the block.
00157     DenseMap<const Instruction *, unsigned> InstNumbers;
00158   public:
00159     
00160     /// isInterestingInstruction - This code only looks at accesses to allocas.
00161     static bool isInterestingInstruction(const Instruction *I) {
00162       return (isa<LoadInst>(I) && isa<AllocaInst>(I->getOperand(0))) ||
00163              (isa<StoreInst>(I) && isa<AllocaInst>(I->getOperand(1)));
00164     }
00165     
00166     /// getInstructionIndex - Get or calculate the index of the specified
00167     /// instruction.
00168     unsigned getInstructionIndex(const Instruction *I) {
00169       assert(isInterestingInstruction(I) &&
00170              "Not a load/store to/from an alloca?");
00171       
00172       // If we already have this instruction number, return it.
00173       DenseMap<const Instruction *, unsigned>::iterator It = InstNumbers.find(I);
00174       if (It != InstNumbers.end()) return It->second;
00175       
00176       // Scan the whole block to get the instruction.  This accumulates
00177       // information for every interesting instruction in the block, in order to
00178       // avoid gratuitus rescans.
00179       const BasicBlock *BB = I->getParent();
00180       unsigned InstNo = 0;
00181       for (BasicBlock::const_iterator BBI = BB->begin(), E = BB->end();
00182            BBI != E; ++BBI)
00183         if (isInterestingInstruction(BBI))
00184           InstNumbers[BBI] = InstNo++;
00185       It = InstNumbers.find(I);
00186       
00187       assert(It != InstNumbers.end() && "Didn't insert instruction?");
00188       return It->second;
00189     }
00190     
00191     void deleteValue(const Instruction *I) {
00192       InstNumbers.erase(I);
00193     }
00194     
00195     void clear() {
00196       InstNumbers.clear();
00197     }
00198   };
00199 
00200   struct PromoteMem2Reg {
00201     /// Allocas - The alloca instructions being promoted.
00202     ///
00203     std::vector<AllocaInst*> Allocas;
00204     DominatorTree &DT;
00205     DIBuilder *DIB;
00206 
00207     /// AST - An AliasSetTracker object to update.  If null, don't update it.
00208     ///
00209     AliasSetTracker *AST;
00210     
00211     /// AllocaLookup - Reverse mapping of Allocas.
00212     ///
00213     DenseMap<AllocaInst*, unsigned>  AllocaLookup;
00214 
00215     /// NewPhiNodes - The PhiNodes we're adding.  That map is used to simplify
00216     /// some Phi nodes as we iterate over it, so it should have deterministic
00217     /// iterators.  We could use a MapVector, but since we already maintain a
00218     /// map from BasicBlock* to a stable numbering (BBNumbers), the DenseMap is
00219     /// more efficient (also supports removal).
00220     ///
00221     DenseMap<std::pair<unsigned, unsigned>, PHINode*> NewPhiNodes;
00222     
00223     /// PhiToAllocaMap - For each PHI node, keep track of which entry in Allocas
00224     /// it corresponds to.
00225     DenseMap<PHINode*, unsigned> PhiToAllocaMap;
00226     
00227     /// PointerAllocaValues - If we are updating an AliasSetTracker, then for
00228     /// each alloca that is of pointer type, we keep track of what to copyValue
00229     /// to the inserted PHI nodes here.
00230     ///
00231     std::vector<Value*> PointerAllocaValues;
00232 
00233     /// AllocaDbgDeclares - For each alloca, we keep track of the dbg.declare
00234     /// intrinsic that describes it, if any, so that we can convert it to a
00235     /// dbg.value intrinsic if the alloca gets promoted.
00236     SmallVector<DbgDeclareInst*, 8> AllocaDbgDeclares;
00237 
00238     /// Visited - The set of basic blocks the renamer has already visited.
00239     ///
00240     SmallPtrSet<BasicBlock*, 16> Visited;
00241 
00242     /// BBNumbers - Contains a stable numbering of basic blocks to avoid
00243     /// non-determinstic behavior.
00244     DenseMap<BasicBlock*, unsigned> BBNumbers;
00245 
00246     /// DomLevels - Maps DomTreeNodes to their level in the dominator tree.
00247     DenseMap<DomTreeNode*, unsigned> DomLevels;
00248 
00249     /// BBNumPreds - Lazily compute the number of predecessors a block has.
00250     DenseMap<const BasicBlock*, unsigned> BBNumPreds;
00251   public:
00252     PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominatorTree &dt,
00253                    AliasSetTracker *ast)
00254       : Allocas(A), DT(dt), DIB(0), AST(ast) {}
00255     ~PromoteMem2Reg() {
00256       delete DIB;
00257     }
00258 
00259     void run();
00260 
00261     /// dominates - Return true if BB1 dominates BB2 using the DominatorTree.
00262     ///
00263     bool dominates(BasicBlock *BB1, BasicBlock *BB2) const {
00264       return DT.dominates(BB1, BB2);
00265     }
00266 
00267   private:
00268     void RemoveFromAllocasList(unsigned &AllocaIdx) {
00269       Allocas[AllocaIdx] = Allocas.back();
00270       Allocas.pop_back();
00271       --AllocaIdx;
00272     }
00273 
00274     unsigned getNumPreds(const BasicBlock *BB) {
00275       unsigned &NP = BBNumPreds[BB];
00276       if (NP == 0)
00277         NP = std::distance(pred_begin(BB), pred_end(BB))+1;
00278       return NP-1;
00279     }
00280 
00281     void DetermineInsertionPoint(AllocaInst *AI, unsigned AllocaNum,
00282                                  AllocaInfo &Info);
00283     void ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info, 
00284                              const SmallPtrSet<BasicBlock*, 32> &DefBlocks,
00285                              SmallPtrSet<BasicBlock*, 32> &LiveInBlocks);
00286     
00287     void RewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
00288                                   LargeBlockInfo &LBI);
00289     void PromoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info,
00290                                   LargeBlockInfo &LBI);
00291     
00292     void RenamePass(BasicBlock *BB, BasicBlock *Pred,
00293                     RenamePassData::ValVector &IncVals,
00294                     std::vector<RenamePassData> &Worklist);
00295     bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version);
00296   };
00297   
00298   struct AllocaInfo {
00299     SmallVector<BasicBlock*, 32> DefiningBlocks;
00300     SmallVector<BasicBlock*, 32> UsingBlocks;
00301     
00302     StoreInst  *OnlyStore;
00303     BasicBlock *OnlyBlock;
00304     bool OnlyUsedInOneBlock;
00305     
00306     Value *AllocaPointerVal;
00307     DbgDeclareInst *DbgDeclare;
00308     
00309     void clear() {
00310       DefiningBlocks.clear();
00311       UsingBlocks.clear();
00312       OnlyStore = 0;
00313       OnlyBlock = 0;
00314       OnlyUsedInOneBlock = true;
00315       AllocaPointerVal = 0;
00316       DbgDeclare = 0;
00317     }
00318     
00319     /// AnalyzeAlloca - Scan the uses of the specified alloca, filling in our
00320     /// ivars.
00321     void AnalyzeAlloca(AllocaInst *AI) {
00322       clear();
00323 
00324       // As we scan the uses of the alloca instruction, keep track of stores,
00325       // and decide whether all of the loads and stores to the alloca are within
00326       // the same basic block.
00327       for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
00328            UI != E;)  {
00329         Instruction *User = cast<Instruction>(*UI++);
00330 
00331         if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
00332           // Remember the basic blocks which define new values for the alloca
00333           DefiningBlocks.push_back(SI->getParent());
00334           AllocaPointerVal = SI->getOperand(0);
00335           OnlyStore = SI;
00336         } else {
00337           LoadInst *LI = cast<LoadInst>(User);
00338           // Otherwise it must be a load instruction, keep track of variable
00339           // reads.
00340           UsingBlocks.push_back(LI->getParent());
00341           AllocaPointerVal = LI;
00342         }
00343         
00344         if (OnlyUsedInOneBlock) {
00345           if (OnlyBlock == 0)
00346             OnlyBlock = User->getParent();
00347           else if (OnlyBlock != User->getParent())
00348             OnlyUsedInOneBlock = false;
00349         }
00350       }
00351       
00352       DbgDeclare = FindAllocaDbgDeclare(AI);
00353     }
00354   };
00355 
00356   typedef std::pair<DomTreeNode*, unsigned> DomTreeNodePair;
00357 
00358   struct DomTreeNodeCompare {
00359     bool operator()(const DomTreeNodePair &LHS, const DomTreeNodePair &RHS) {
00360       return LHS.second < RHS.second;
00361     }
00362   };
00363 }  // end of anonymous namespace
00364 
00365 static void removeLifetimeIntrinsicUsers(AllocaInst *AI) {
00366   // Knowing that this alloca is promotable, we know that it's safe to kill all
00367   // instructions except for load and store.
00368 
00369   for (Value::use_iterator UI = AI->use_begin(), UE = AI->use_end();
00370        UI != UE;) {
00371     Instruction *I = cast<Instruction>(*UI);
00372     ++UI;
00373     if (isa<LoadInst>(I) || isa<StoreInst>(I))
00374       continue;
00375 
00376     if (!I->getType()->isVoidTy()) {
00377       // The only users of this bitcast/GEP instruction are lifetime intrinsics.
00378       // Follow the use/def chain to erase them now instead of leaving it for
00379       // dead code elimination later.
00380       for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
00381            UI != UE;) {
00382         Instruction *Inst = cast<Instruction>(*UI);
00383         ++UI;
00384         Inst->eraseFromParent();
00385       }
00386     }
00387     I->eraseFromParent();
00388   }
00389 }
00390 
00391 void PromoteMem2Reg::run() {
00392   Function &F = *DT.getRoot()->getParent();
00393 
00394   if (AST) PointerAllocaValues.resize(Allocas.size());
00395   AllocaDbgDeclares.resize(Allocas.size());
00396 
00397   AllocaInfo Info;
00398   LargeBlockInfo LBI;
00399 
00400   for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
00401     AllocaInst *AI = Allocas[AllocaNum];
00402 
00403     assert(isAllocaPromotable(AI) &&
00404            "Cannot promote non-promotable alloca!");
00405     assert(AI->getParent()->getParent() == &F &&
00406            "All allocas should be in the same function, which is same as DF!");
00407 
00408     removeLifetimeIntrinsicUsers(AI);
00409 
00410     if (AI->use_empty()) {
00411       // If there are no uses of the alloca, just delete it now.
00412       if (AST) AST->deleteValue(AI);
00413       AI->eraseFromParent();
00414 
00415       // Remove the alloca from the Allocas list, since it has been processed
00416       RemoveFromAllocasList(AllocaNum);
00417       ++NumDeadAlloca;
00418       continue;
00419     }
00420     
00421     // Calculate the set of read and write-locations for each alloca.  This is
00422     // analogous to finding the 'uses' and 'definitions' of each variable.
00423     Info.AnalyzeAlloca(AI);
00424 
00425     // If there is only a single store to this value, replace any loads of
00426     // it that are directly dominated by the definition with the value stored.
00427     if (Info.DefiningBlocks.size() == 1) {
00428       RewriteSingleStoreAlloca(AI, Info, LBI);
00429 
00430       // Finally, after the scan, check to see if the store is all that is left.
00431       if (Info.UsingBlocks.empty()) {
00432         // Record debuginfo for the store and remove the declaration's 
00433         // debuginfo.
00434         if (DbgDeclareInst *DDI = Info.DbgDeclare) {
00435           if (!DIB)
00436             DIB = new DIBuilder(*DDI->getParent()->getParent()->getParent());
00437           ConvertDebugDeclareToDebugValue(DDI, Info.OnlyStore, *DIB);
00438           DDI->eraseFromParent();
00439         }
00440         // Remove the (now dead) store and alloca.
00441         Info.OnlyStore->eraseFromParent();
00442         LBI.deleteValue(Info.OnlyStore);
00443 
00444         if (AST) AST->deleteValue(AI);
00445         AI->eraseFromParent();
00446         LBI.deleteValue(AI);
00447         
00448         // The alloca has been processed, move on.
00449         RemoveFromAllocasList(AllocaNum);
00450         
00451         ++NumSingleStore;
00452         continue;
00453       }
00454     }
00455     
00456     // If the alloca is only read and written in one basic block, just perform a
00457     // linear sweep over the block to eliminate it.
00458     if (Info.OnlyUsedInOneBlock) {
00459       PromoteSingleBlockAlloca(AI, Info, LBI);
00460       
00461       // Finally, after the scan, check to see if the stores are all that is
00462       // left.
00463       if (Info.UsingBlocks.empty()) {
00464         
00465         // Remove the (now dead) stores and alloca.
00466         while (!AI->use_empty()) {
00467           StoreInst *SI = cast<StoreInst>(AI->use_back());
00468           // Record debuginfo for the store before removing it.
00469           if (DbgDeclareInst *DDI = Info.DbgDeclare) {
00470             if (!DIB)
00471               DIB = new DIBuilder(*SI->getParent()->getParent()->getParent());
00472             ConvertDebugDeclareToDebugValue(DDI, SI, *DIB);
00473           }
00474           SI->eraseFromParent();
00475           LBI.deleteValue(SI);
00476         }
00477         
00478         if (AST) AST->deleteValue(AI);
00479         AI->eraseFromParent();
00480         LBI.deleteValue(AI);
00481         
00482         // The alloca has been processed, move on.
00483         RemoveFromAllocasList(AllocaNum);
00484         
00485         // The alloca's debuginfo can be removed as well.
00486         if (DbgDeclareInst *DDI = Info.DbgDeclare)
00487           DDI->eraseFromParent();
00488 
00489         ++NumLocalPromoted;
00490         continue;
00491       }
00492     }
00493 
00494     // If we haven't computed dominator tree levels, do so now.
00495     if (DomLevels.empty()) {
00496       SmallVector<DomTreeNode*, 32> Worklist;
00497 
00498       DomTreeNode *Root = DT.getRootNode();
00499       DomLevels[Root] = 0;
00500       Worklist.push_back(Root);
00501 
00502       while (!Worklist.empty()) {
00503         DomTreeNode *Node = Worklist.pop_back_val();
00504         unsigned ChildLevel = DomLevels[Node] + 1;
00505         for (DomTreeNode::iterator CI = Node->begin(), CE = Node->end();
00506              CI != CE; ++CI) {
00507           DomLevels[*CI] = ChildLevel;
00508           Worklist.push_back(*CI);
00509         }
00510       }
00511     }
00512 
00513     // If we haven't computed a numbering for the BB's in the function, do so
00514     // now.
00515     if (BBNumbers.empty()) {
00516       unsigned ID = 0;
00517       for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
00518         BBNumbers[I] = ID++;
00519     }
00520 
00521     // If we have an AST to keep updated, remember some pointer value that is
00522     // stored into the alloca.
00523     if (AST)
00524       PointerAllocaValues[AllocaNum] = Info.AllocaPointerVal;
00525       
00526     // Remember the dbg.declare intrinsic describing this alloca, if any.
00527     if (Info.DbgDeclare) AllocaDbgDeclares[AllocaNum] = Info.DbgDeclare;
00528     
00529     // Keep the reverse mapping of the 'Allocas' array for the rename pass.
00530     AllocaLookup[Allocas[AllocaNum]] = AllocaNum;
00531 
00532     // At this point, we're committed to promoting the alloca using IDF's, and
00533     // the standard SSA construction algorithm.  Determine which blocks need PHI
00534     // nodes and see if we can optimize out some work by avoiding insertion of
00535     // dead phi nodes.
00536     DetermineInsertionPoint(AI, AllocaNum, Info);
00537   }
00538 
00539   if (Allocas.empty())
00540     return; // All of the allocas must have been trivial!
00541 
00542   LBI.clear();
00543   
00544   
00545   // Set the incoming values for the basic block to be null values for all of
00546   // the alloca's.  We do this in case there is a load of a value that has not
00547   // been stored yet.  In this case, it will get this null value.
00548   //
00549   RenamePassData::ValVector Values(Allocas.size());
00550   for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
00551     Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
00552 
00553   // Walks all basic blocks in the function performing the SSA rename algorithm
00554   // and inserting the phi nodes we marked as necessary
00555   //
00556   std::vector<RenamePassData> RenamePassWorkList;
00557   RenamePassWorkList.push_back(RenamePassData(F.begin(), 0, Values));
00558   do {
00559     RenamePassData RPD;
00560     RPD.swap(RenamePassWorkList.back());
00561     RenamePassWorkList.pop_back();
00562     // RenamePass may add new worklist entries.
00563     RenamePass(RPD.BB, RPD.Pred, RPD.Values, RenamePassWorkList);
00564   } while (!RenamePassWorkList.empty());
00565   
00566   // The renamer uses the Visited set to avoid infinite loops.  Clear it now.
00567   Visited.clear();
00568 
00569   // Remove the allocas themselves from the function.
00570   for (unsigned i = 0, e = Allocas.size(); i != e; ++i) {
00571     Instruction *A = Allocas[i];
00572 
00573     // If there are any uses of the alloca instructions left, they must be in
00574     // unreachable basic blocks that were not processed by walking the dominator
00575     // tree. Just delete the users now.
00576     if (!A->use_empty())
00577       A->replaceAllUsesWith(UndefValue::get(A->getType()));
00578     if (AST) AST->deleteValue(A);
00579     A->eraseFromParent();
00580   }
00581 
00582   // Remove alloca's dbg.declare instrinsics from the function.
00583   for (unsigned i = 0, e = AllocaDbgDeclares.size(); i != e; ++i)
00584     if (DbgDeclareInst *DDI = AllocaDbgDeclares[i])
00585       DDI->eraseFromParent();
00586 
00587   // Loop over all of the PHI nodes and see if there are any that we can get
00588   // rid of because they merge all of the same incoming values.  This can
00589   // happen due to undef values coming into the PHI nodes.  This process is
00590   // iterative, because eliminating one PHI node can cause others to be removed.
00591   bool EliminatedAPHI = true;
00592   while (EliminatedAPHI) {
00593     EliminatedAPHI = false;
00594     
00595     // Iterating over NewPhiNodes is deterministic, so it is safe to try to
00596     // simplify and RAUW them as we go.  If it was not, we could add uses to
00597     // the values we replace with in a non deterministic order, thus creating
00598     // non deterministic def->use chains.
00599     for (DenseMap<std::pair<unsigned, unsigned>, PHINode*>::iterator I =
00600            NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E;) {
00601       PHINode *PN = I->second;
00602 
00603       // If this PHI node merges one value and/or undefs, get the value.
00604       if (Value *V = SimplifyInstruction(PN, 0, 0, &DT)) {
00605         if (AST && PN->getType()->isPointerTy())
00606           AST->deleteValue(PN);
00607         PN->replaceAllUsesWith(V);
00608         PN->eraseFromParent();
00609         NewPhiNodes.erase(I++);
00610         EliminatedAPHI = true;
00611         continue;
00612       }
00613       ++I;
00614     }
00615   }
00616   
00617   // At this point, the renamer has added entries to PHI nodes for all reachable
00618   // code.  Unfortunately, there may be unreachable blocks which the renamer
00619   // hasn't traversed.  If this is the case, the PHI nodes may not
00620   // have incoming values for all predecessors.  Loop over all PHI nodes we have
00621   // created, inserting undef values if they are missing any incoming values.
00622   //
00623   for (DenseMap<std::pair<unsigned, unsigned>, PHINode*>::iterator I =
00624          NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E; ++I) {
00625     // We want to do this once per basic block.  As such, only process a block
00626     // when we find the PHI that is the first entry in the block.
00627     PHINode *SomePHI = I->second;
00628     BasicBlock *BB = SomePHI->getParent();
00629     if (&BB->front() != SomePHI)
00630       continue;
00631 
00632     // Only do work here if there the PHI nodes are missing incoming values.  We
00633     // know that all PHI nodes that were inserted in a block will have the same
00634     // number of incoming values, so we can just check any of them.
00635     if (SomePHI->getNumIncomingValues() == getNumPreds(BB))
00636       continue;
00637 
00638     // Get the preds for BB.
00639     SmallVector<BasicBlock*, 16> Preds(pred_begin(BB), pred_end(BB));
00640     
00641     // Ok, now we know that all of the PHI nodes are missing entries for some
00642     // basic blocks.  Start by sorting the incoming predecessors for efficient
00643     // access.
00644     std::sort(Preds.begin(), Preds.end());
00645     
00646     // Now we loop through all BB's which have entries in SomePHI and remove
00647     // them from the Preds list.
00648     for (unsigned i = 0, e = SomePHI->getNumIncomingValues(); i != e; ++i) {
00649       // Do a log(n) search of the Preds list for the entry we want.
00650       SmallVector<BasicBlock*, 16>::iterator EntIt =
00651         std::lower_bound(Preds.begin(), Preds.end(),
00652                          SomePHI->getIncomingBlock(i));
00653       assert(EntIt != Preds.end() && *EntIt == SomePHI->getIncomingBlock(i)&&
00654              "PHI node has entry for a block which is not a predecessor!");
00655 
00656       // Remove the entry
00657       Preds.erase(EntIt);
00658     }
00659 
00660     // At this point, the blocks left in the preds list must have dummy
00661     // entries inserted into every PHI nodes for the block.  Update all the phi
00662     // nodes in this block that we are inserting (there could be phis before
00663     // mem2reg runs).
00664     unsigned NumBadPreds = SomePHI->getNumIncomingValues();
00665     BasicBlock::iterator BBI = BB->begin();
00666     while ((SomePHI = dyn_cast<PHINode>(BBI++)) &&
00667            SomePHI->getNumIncomingValues() == NumBadPreds) {
00668       Value *UndefVal = UndefValue::get(SomePHI->getType());
00669       for (unsigned pred = 0, e = Preds.size(); pred != e; ++pred)
00670         SomePHI->addIncoming(UndefVal, Preds[pred]);
00671     }
00672   }
00673         
00674   NewPhiNodes.clear();
00675 }
00676 
00677 
00678 /// ComputeLiveInBlocks - Determine which blocks the value is live in.  These
00679 /// are blocks which lead to uses.  Knowing this allows us to avoid inserting
00680 /// PHI nodes into blocks which don't lead to uses (thus, the inserted phi nodes
00681 /// would be dead).
00682 void PromoteMem2Reg::
00683 ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info, 
00684                     const SmallPtrSet<BasicBlock*, 32> &DefBlocks,
00685                     SmallPtrSet<BasicBlock*, 32> &LiveInBlocks) {
00686   
00687   // To determine liveness, we must iterate through the predecessors of blocks
00688   // where the def is live.  Blocks are added to the worklist if we need to
00689   // check their predecessors.  Start with all the using blocks.
00690   SmallVector<BasicBlock*, 64> LiveInBlockWorklist(Info.UsingBlocks.begin(),
00691                                                    Info.UsingBlocks.end());
00692   
00693   // If any of the using blocks is also a definition block, check to see if the
00694   // definition occurs before or after the use.  If it happens before the use,
00695   // the value isn't really live-in.
00696   for (unsigned i = 0, e = LiveInBlockWorklist.size(); i != e; ++i) {
00697     BasicBlock *BB = LiveInBlockWorklist[i];
00698     if (!DefBlocks.count(BB)) continue;
00699     
00700     // Okay, this is a block that both uses and defines the value.  If the first
00701     // reference to the alloca is a def (store), then we know it isn't live-in.
00702     for (BasicBlock::iterator I = BB->begin(); ; ++I) {
00703       if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
00704         if (SI->getOperand(1) != AI) continue;
00705         
00706         // We found a store to the alloca before a load.  The alloca is not
00707         // actually live-in here.
00708         LiveInBlockWorklist[i] = LiveInBlockWorklist.back();
00709         LiveInBlockWorklist.pop_back();
00710         --i, --e;
00711         break;
00712       }
00713       
00714       if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
00715         if (LI->getOperand(0) != AI) continue;
00716         
00717         // Okay, we found a load before a store to the alloca.  It is actually
00718         // live into this block.
00719         break;
00720       }
00721     }
00722   }
00723   
00724   // Now that we have a set of blocks where the phi is live-in, recursively add
00725   // their predecessors until we find the full region the value is live.
00726   while (!LiveInBlockWorklist.empty()) {
00727     BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
00728     
00729     // The block really is live in here, insert it into the set.  If already in
00730     // the set, then it has already been processed.
00731     if (!LiveInBlocks.insert(BB))
00732       continue;
00733     
00734     // Since the value is live into BB, it is either defined in a predecessor or
00735     // live into it to.  Add the preds to the worklist unless they are a
00736     // defining block.
00737     for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
00738       BasicBlock *P = *PI;
00739       
00740       // The value is not live into a predecessor if it defines the value.
00741       if (DefBlocks.count(P))
00742         continue;
00743       
00744       // Otherwise it is, add to the worklist.
00745       LiveInBlockWorklist.push_back(P);
00746     }
00747   }
00748 }
00749 
00750 /// DetermineInsertionPoint - At this point, we're committed to promoting the
00751 /// alloca using IDF's, and the standard SSA construction algorithm.  Determine
00752 /// which blocks need phi nodes and see if we can optimize out some work by
00753 /// avoiding insertion of dead phi nodes.
00754 void PromoteMem2Reg::DetermineInsertionPoint(AllocaInst *AI, unsigned AllocaNum,
00755                                              AllocaInfo &Info) {
00756   // Unique the set of defining blocks for efficient lookup.
00757   SmallPtrSet<BasicBlock*, 32> DefBlocks;
00758   DefBlocks.insert(Info.DefiningBlocks.begin(), Info.DefiningBlocks.end());
00759 
00760   // Determine which blocks the value is live in.  These are blocks which lead
00761   // to uses.
00762   SmallPtrSet<BasicBlock*, 32> LiveInBlocks;
00763   ComputeLiveInBlocks(AI, Info, DefBlocks, LiveInBlocks);
00764 
00765   // Use a priority queue keyed on dominator tree level so that inserted nodes
00766   // are handled from the bottom of the dominator tree upwards.
00767   typedef std::priority_queue<DomTreeNodePair, SmallVector<DomTreeNodePair, 32>,
00768                               DomTreeNodeCompare> IDFPriorityQueue;
00769   IDFPriorityQueue PQ;
00770 
00771   for (SmallPtrSet<BasicBlock*, 32>::const_iterator I = DefBlocks.begin(),
00772        E = DefBlocks.end(); I != E; ++I) {
00773     if (DomTreeNode *Node = DT.getNode(*I))
00774       PQ.push(std::make_pair(Node, DomLevels[Node]));
00775   }
00776 
00777   SmallVector<std::pair<unsigned, BasicBlock*>, 32> DFBlocks;
00778   SmallPtrSet<DomTreeNode*, 32> Visited;
00779   SmallVector<DomTreeNode*, 32> Worklist;
00780   while (!PQ.empty()) {
00781     DomTreeNodePair RootPair = PQ.top();
00782     PQ.pop();
00783     DomTreeNode *Root = RootPair.first;
00784     unsigned RootLevel = RootPair.second;
00785 
00786     // Walk all dominator tree children of Root, inspecting their CFG edges with
00787     // targets elsewhere on the dominator tree. Only targets whose level is at
00788     // most Root's level are added to the iterated dominance frontier of the
00789     // definition set.
00790 
00791     Worklist.clear();
00792     Worklist.push_back(Root);
00793 
00794     while (!Worklist.empty()) {
00795       DomTreeNode *Node = Worklist.pop_back_val();
00796       BasicBlock *BB = Node->getBlock();
00797 
00798       for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
00799            ++SI) {
00800         DomTreeNode *SuccNode = DT.getNode(*SI);
00801 
00802         // Quickly skip all CFG edges that are also dominator tree edges instead
00803         // of catching them below.
00804         if (SuccNode->getIDom() == Node)
00805           continue;
00806 
00807         unsigned SuccLevel = DomLevels[SuccNode];
00808         if (SuccLevel > RootLevel)
00809           continue;
00810 
00811         if (!Visited.insert(SuccNode))
00812           continue;
00813 
00814         BasicBlock *SuccBB = SuccNode->getBlock();
00815         if (!LiveInBlocks.count(SuccBB))
00816           continue;
00817 
00818         DFBlocks.push_back(std::make_pair(BBNumbers[SuccBB], SuccBB));
00819         if (!DefBlocks.count(SuccBB))
00820           PQ.push(std::make_pair(SuccNode, SuccLevel));
00821       }
00822 
00823       for (DomTreeNode::iterator CI = Node->begin(), CE = Node->end(); CI != CE;
00824            ++CI) {
00825         if (!Visited.count(*CI))
00826           Worklist.push_back(*CI);
00827       }
00828     }
00829   }
00830 
00831   if (DFBlocks.size() > 1)
00832     std::sort(DFBlocks.begin(), DFBlocks.end());
00833 
00834   unsigned CurrentVersion = 0;
00835   for (unsigned i = 0, e = DFBlocks.size(); i != e; ++i)
00836     QueuePhiNode(DFBlocks[i].second, AllocaNum, CurrentVersion);
00837 }
00838 
00839 /// RewriteSingleStoreAlloca - If there is only a single store to this value,
00840 /// replace any loads of it that are directly dominated by the definition with
00841 /// the value stored.
00842 void PromoteMem2Reg::RewriteSingleStoreAlloca(AllocaInst *AI,
00843                                               AllocaInfo &Info,
00844                                               LargeBlockInfo &LBI) {
00845   StoreInst *OnlyStore = Info.OnlyStore;
00846   bool StoringGlobalVal = !isa<Instruction>(OnlyStore->getOperand(0));
00847   BasicBlock *StoreBB = OnlyStore->getParent();
00848   int StoreIndex = -1;
00849 
00850   // Clear out UsingBlocks.  We will reconstruct it here if needed.
00851   Info.UsingBlocks.clear();
00852   
00853   for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E; ) {
00854     Instruction *UserInst = cast<Instruction>(*UI++);
00855     if (!isa<LoadInst>(UserInst)) {
00856       assert(UserInst == OnlyStore && "Should only have load/stores");
00857       continue;
00858     }
00859     LoadInst *LI = cast<LoadInst>(UserInst);
00860     
00861     // Okay, if we have a load from the alloca, we want to replace it with the
00862     // only value stored to the alloca.  We can do this if the value is
00863     // dominated by the store.  If not, we use the rest of the mem2reg machinery
00864     // to insert the phi nodes as needed.
00865     if (!StoringGlobalVal) {  // Non-instructions are always dominated.
00866       if (LI->getParent() == StoreBB) {
00867         // If we have a use that is in the same block as the store, compare the
00868         // indices of the two instructions to see which one came first.  If the
00869         // load came before the store, we can't handle it.
00870         if (StoreIndex == -1)
00871           StoreIndex = LBI.getInstructionIndex(OnlyStore);
00872 
00873         if (unsigned(StoreIndex) > LBI.getInstructionIndex(LI)) {
00874           // Can't handle this load, bail out.
00875           Info.UsingBlocks.push_back(StoreBB);
00876           continue;
00877         }
00878         
00879       } else if (LI->getParent() != StoreBB &&
00880                  !dominates(StoreBB, LI->getParent())) {
00881         // If the load and store are in different blocks, use BB dominance to
00882         // check their relationships.  If the store doesn't dom the use, bail
00883         // out.
00884         Info.UsingBlocks.push_back(LI->getParent());
00885         continue;
00886       }
00887     }
00888     
00889     // Otherwise, we *can* safely rewrite this load.
00890     Value *ReplVal = OnlyStore->getOperand(0);
00891     // If the replacement value is the load, this must occur in unreachable
00892     // code.
00893     if (ReplVal == LI)
00894       ReplVal = UndefValue::get(LI->getType());
00895     LI->replaceAllUsesWith(ReplVal);
00896     if (AST && LI->getType()->isPointerTy())
00897       AST->deleteValue(LI);
00898     LI->eraseFromParent();
00899     LBI.deleteValue(LI);
00900   }
00901 }
00902 
00903 namespace {
00904 
00905 /// StoreIndexSearchPredicate - This is a helper predicate used to search by the
00906 /// first element of a pair.
00907 struct StoreIndexSearchPredicate {
00908   bool operator()(const std::pair<unsigned, StoreInst*> &LHS,
00909                   const std::pair<unsigned, StoreInst*> &RHS) {
00910     return LHS.first < RHS.first;
00911   }
00912 };
00913 
00914 }
00915 
00916 /// PromoteSingleBlockAlloca - Many allocas are only used within a single basic
00917 /// block.  If this is the case, avoid traversing the CFG and inserting a lot of
00918 /// potentially useless PHI nodes by just performing a single linear pass over
00919 /// the basic block using the Alloca.
00920 ///
00921 /// If we cannot promote this alloca (because it is read before it is written),
00922 /// return true.  This is necessary in cases where, due to control flow, the
00923 /// alloca is potentially undefined on some control flow paths.  e.g. code like
00924 /// this is potentially correct:
00925 ///
00926 ///   for (...) { if (c) { A = undef; undef = B; } }
00927 ///
00928 /// ... so long as A is not used before undef is set.
00929 ///
00930 void PromoteMem2Reg::PromoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info,
00931                                               LargeBlockInfo &LBI) {
00932   // The trickiest case to handle is when we have large blocks. Because of this,
00933   // this code is optimized assuming that large blocks happen.  This does not
00934   // significantly pessimize the small block case.  This uses LargeBlockInfo to
00935   // make it efficient to get the index of various operations in the block.
00936   
00937   // Clear out UsingBlocks.  We will reconstruct it here if needed.
00938   Info.UsingBlocks.clear();
00939   
00940   // Walk the use-def list of the alloca, getting the locations of all stores.
00941   typedef SmallVector<std::pair<unsigned, StoreInst*>, 64> StoresByIndexTy;
00942   StoresByIndexTy StoresByIndex;
00943   
00944   for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
00945        UI != E; ++UI) 
00946     if (StoreInst *SI = dyn_cast<StoreInst>(*UI))
00947       StoresByIndex.push_back(std::make_pair(LBI.getInstructionIndex(SI), SI));
00948 
00949   // If there are no stores to the alloca, just replace any loads with undef.
00950   if (StoresByIndex.empty()) {
00951     for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) 
00952       if (LoadInst *LI = dyn_cast<LoadInst>(*UI++)) {
00953         LI->replaceAllUsesWith(UndefValue::get(LI->getType()));
00954         if (AST && LI->getType()->isPointerTy())
00955           AST->deleteValue(LI);
00956         LBI.deleteValue(LI);
00957         LI->eraseFromParent();
00958       }
00959     return;
00960   }
00961   
00962   // Sort the stores by their index, making it efficient to do a lookup with a
00963   // binary search.
00964   std::sort(StoresByIndex.begin(), StoresByIndex.end());
00965   
00966   // Walk all of the loads from this alloca, replacing them with the nearest
00967   // store above them, if any.
00968   for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) {
00969     LoadInst *LI = dyn_cast<LoadInst>(*UI++);
00970     if (!LI) continue;
00971     
00972     unsigned LoadIdx = LBI.getInstructionIndex(LI);
00973     
00974     // Find the nearest store that has a lower than this load. 
00975     StoresByIndexTy::iterator I = 
00976       std::lower_bound(StoresByIndex.begin(), StoresByIndex.end(),
00977                        std::pair<unsigned, StoreInst*>(LoadIdx, static_cast<StoreInst*>(0)),
00978                        StoreIndexSearchPredicate());
00979     
00980     // If there is no store before this load, then we can't promote this load.
00981     if (I == StoresByIndex.begin()) {
00982       // Can't handle this load, bail out.
00983       Info.UsingBlocks.push_back(LI->getParent());
00984       continue;
00985     }
00986       
00987     // Otherwise, there was a store before this load, the load takes its value.
00988     --I;
00989     LI->replaceAllUsesWith(I->second->getOperand(0));
00990     if (AST && LI->getType()->isPointerTy())
00991       AST->deleteValue(LI);
00992     LI->eraseFromParent();
00993     LBI.deleteValue(LI);
00994   }
00995 }
00996 
00997 // QueuePhiNode - queues a phi-node to be added to a basic-block for a specific
00998 // Alloca returns true if there wasn't already a phi-node for that variable
00999 //
01000 bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo,
01001                                   unsigned &Version) {
01002   // Look up the basic-block in question.
01003   PHINode *&PN = NewPhiNodes[std::make_pair(BBNumbers[BB], AllocaNo)];
01004 
01005   // If the BB already has a phi node added for the i'th alloca then we're done!
01006   if (PN) return false;
01007 
01008   // Create a PhiNode using the dereferenced type... and add the phi-node to the
01009   // BasicBlock.
01010   PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(), getNumPreds(BB),
01011                        Allocas[AllocaNo]->getName() + "." + Twine(Version++), 
01012                        BB->begin());
01013   ++NumPHIInsert;
01014   PhiToAllocaMap[PN] = AllocaNo;
01015 
01016   if (AST && PN->getType()->isPointerTy())
01017     AST->copyValue(PointerAllocaValues[AllocaNo], PN);
01018 
01019   return true;
01020 }
01021 
01022 // RenamePass - Recursively traverse the CFG of the function, renaming loads and
01023 // stores to the allocas which we are promoting.  IncomingVals indicates what
01024 // value each Alloca contains on exit from the predecessor block Pred.
01025 //
01026 void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
01027                                 RenamePassData::ValVector &IncomingVals,
01028                                 std::vector<RenamePassData> &Worklist) {
01029 NextIteration:
01030   // If we are inserting any phi nodes into this BB, they will already be in the
01031   // block.
01032   if (PHINode *APN = dyn_cast<PHINode>(BB->begin())) {
01033     // If we have PHI nodes to update, compute the number of edges from Pred to
01034     // BB.
01035     if (PhiToAllocaMap.count(APN)) {
01036       // We want to be able to distinguish between PHI nodes being inserted by
01037       // this invocation of mem2reg from those phi nodes that already existed in
01038       // the IR before mem2reg was run.  We determine that APN is being inserted
01039       // because it is missing incoming edges.  All other PHI nodes being
01040       // inserted by this pass of mem2reg will have the same number of incoming
01041       // operands so far.  Remember this count.
01042       unsigned NewPHINumOperands = APN->getNumOperands();
01043       
01044       unsigned NumEdges = 0;
01045       for (succ_iterator I = succ_begin(Pred), E = succ_end(Pred); I != E; ++I)
01046         if (*I == BB)
01047           ++NumEdges;
01048       assert(NumEdges && "Must be at least one edge from Pred to BB!");
01049       
01050       // Add entries for all the phis.
01051       BasicBlock::iterator PNI = BB->begin();
01052       do {
01053         unsigned AllocaNo = PhiToAllocaMap[APN];
01054         
01055         // Add N incoming values to the PHI node.
01056         for (unsigned i = 0; i != NumEdges; ++i)
01057           APN->addIncoming(IncomingVals[AllocaNo], Pred);
01058         
01059         // The currently active variable for this block is now the PHI.
01060         IncomingVals[AllocaNo] = APN;
01061         
01062         // Get the next phi node.
01063         ++PNI;
01064         APN = dyn_cast<PHINode>(PNI);
01065         if (APN == 0) break;
01066         
01067         // Verify that it is missing entries.  If not, it is not being inserted
01068         // by this mem2reg invocation so we want to ignore it.
01069       } while (APN->getNumOperands() == NewPHINumOperands);
01070     }
01071   }
01072   
01073   // Don't revisit blocks.
01074   if (!Visited.insert(BB)) return;
01075 
01076   for (BasicBlock::iterator II = BB->begin(); !isa<TerminatorInst>(II); ) {
01077     Instruction *I = II++; // get the instruction, increment iterator
01078 
01079     if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
01080       AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand());
01081       if (!Src) continue;
01082   
01083       DenseMap<AllocaInst*, unsigned>::iterator AI = AllocaLookup.find(Src);
01084       if (AI == AllocaLookup.end()) continue;
01085 
01086       Value *V = IncomingVals[AI->second];
01087 
01088       // Anything using the load now uses the current value.
01089       LI->replaceAllUsesWith(V);
01090       if (AST && LI->getType()->isPointerTy())
01091         AST->deleteValue(LI);
01092       BB->getInstList().erase(LI);
01093     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
01094       // Delete this instruction and mark the name as the current holder of the
01095       // value
01096       AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand());
01097       if (!Dest) continue;
01098       
01099       DenseMap<AllocaInst *, unsigned>::iterator ai = AllocaLookup.find(Dest);
01100       if (ai == AllocaLookup.end())
01101         continue;
01102       
01103       // what value were we writing?
01104       IncomingVals[ai->second] = SI->getOperand(0);
01105       // Record debuginfo for the store before removing it.
01106       if (DbgDeclareInst *DDI = AllocaDbgDeclares[ai->second]) {
01107         if (!DIB)
01108           DIB = new DIBuilder(*SI->getParent()->getParent()->getParent());
01109         ConvertDebugDeclareToDebugValue(DDI, SI, *DIB);
01110       }
01111       BB->getInstList().erase(SI);
01112     }
01113   }
01114 
01115   // 'Recurse' to our successors.
01116   succ_iterator I = succ_begin(BB), E = succ_end(BB);
01117   if (I == E) return;
01118 
01119   // Keep track of the successors so we don't visit the same successor twice
01120   SmallPtrSet<BasicBlock*, 8> VisitedSuccs;
01121 
01122   // Handle the first successor without using the worklist.
01123   VisitedSuccs.insert(*I);
01124   Pred = BB;
01125   BB = *I;
01126   ++I;
01127 
01128   for (; I != E; ++I)
01129     if (VisitedSuccs.insert(*I))
01130       Worklist.push_back(RenamePassData(*I, Pred, IncomingVals));
01131 
01132   goto NextIteration;
01133 }
01134 
01135 /// PromoteMemToReg - Promote the specified list of alloca instructions into
01136 /// scalar registers, inserting PHI nodes as appropriate.  This function does
01137 /// not modify the CFG of the function at all.  All allocas must be from the
01138 /// same function.
01139 ///
01140 /// If AST is specified, the specified tracker is updated to reflect changes
01141 /// made to the IR.
01142 ///
01143 void llvm::PromoteMemToReg(const std::vector<AllocaInst*> &Allocas,
01144                            DominatorTree &DT, AliasSetTracker *AST) {
01145   // If there is nothing to do, bail out...
01146   if (Allocas.empty()) return;
01147 
01148   PromoteMem2Reg(Allocas, DT, AST).run();
01149 }