Switch InsertedPHINodes back to SmallPtrSet now that the SmallPtrSet::erase
[oota-llvm.git] / lib / Transforms / Utils / PromoteMemoryToRegister.cpp
1 //===- PromoteMemoryToRegister.cpp - Convert allocas to registers ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file promote memory references to be register references.  It promotes
11 // alloca instructions which only have loads and stores as uses.  An alloca is
12 // transformed by using dominator frontiers to place PHI nodes, then traversing
13 // the function in depth-first order to rewrite loads and stores as appropriate.
14 // This is just the standard SSA construction algorithm to construct "pruned"
15 // SSA form.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/Analysis/Dominators.h"
25 #include "llvm/Analysis/AliasSetTracker.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/Support/CFG.h"
30 #include "llvm/Support/StableBasicBlockNumbering.h"
31 #include "llvm/Support/Compiler.h"
32 #include <algorithm>
33 using namespace llvm;
34
35 /// isAllocaPromotable - Return true if this alloca is legal for promotion.
36 /// This is true if there are only loads and stores to the alloca.
37 ///
38 bool llvm::isAllocaPromotable(const AllocaInst *AI, const TargetData &TD) {
39   // FIXME: If the memory unit is of pointer or integer type, we can permit
40   // assignments to subsections of the memory unit.
41
42   // Only allow direct loads and stores...
43   for (Value::use_const_iterator UI = AI->use_begin(), UE = AI->use_end();
44        UI != UE; ++UI)     // Loop over all of the uses of the alloca
45     if (isa<LoadInst>(*UI)) {
46       // noop
47     } else if (const StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
48       if (SI->getOperand(0) == AI)
49         return false;   // Don't allow a store OF the AI, only INTO the AI.
50     } else {
51       return false;   // Not a load or store.
52     }
53
54   return true;
55 }
56
57 namespace {
58   struct VISIBILITY_HIDDEN PromoteMem2Reg {
59     /// Allocas - The alloca instructions being promoted.
60     ///
61     std::vector<AllocaInst*> Allocas;
62     SmallVector<AllocaInst*, 16> &RetryList;
63     DominatorTree &DT;
64     DominanceFrontier &DF;
65     const TargetData &TD;
66
67     /// AST - An AliasSetTracker object to update.  If null, don't update it.
68     ///
69     AliasSetTracker *AST;
70
71     /// AllocaLookup - Reverse mapping of Allocas.
72     ///
73     std::map<AllocaInst*, unsigned>  AllocaLookup;
74
75     /// NewPhiNodes - The PhiNodes we're adding.
76     ///
77     std::map<BasicBlock*, std::vector<PHINode*> > NewPhiNodes;
78
79     /// PointerAllocaValues - If we are updating an AliasSetTracker, then for
80     /// each alloca that is of pointer type, we keep track of what to copyValue
81     /// to the inserted PHI nodes here.
82     ///
83     std::vector<Value*> PointerAllocaValues;
84
85     /// Visited - The set of basic blocks the renamer has already visited.
86     ///
87     SmallPtrSet<BasicBlock*, 16> Visited;
88
89     /// BBNumbers - Contains a stable numbering of basic blocks to avoid
90     /// non-determinstic behavior.
91     StableBasicBlockNumbering BBNumbers;
92
93   public:
94     PromoteMem2Reg(const std::vector<AllocaInst*> &A,
95                    SmallVector<AllocaInst*, 16> &Retry, DominatorTree &dt,
96                    DominanceFrontier &df, const TargetData &td,
97                    AliasSetTracker *ast)
98       : Allocas(A), RetryList(Retry), DT(dt), DF(df), TD(td), AST(ast) {}
99
100     void run();
101
102     /// properlyDominates - Return true if I1 properly dominates I2.
103     ///
104     bool properlyDominates(Instruction *I1, Instruction *I2) const {
105       if (InvokeInst *II = dyn_cast<InvokeInst>(I1))
106         I1 = II->getNormalDest()->begin();
107       return DT[I1->getParent()]->properlyDominates(DT[I2->getParent()]);
108     }
109     
110     /// dominates - Return true if BB1 dominates BB2 using the DominatorTree.
111     ///
112     bool dominates(BasicBlock *BB1, BasicBlock *BB2) const {
113       return DT[BB1]->dominates(DT[BB2]);
114     }
115
116   private:
117     void MarkDominatingPHILive(BasicBlock *BB, unsigned AllocaNum,
118                                SmallPtrSet<PHINode*, 16> &DeadPHINodes);
119     bool PromoteLocallyUsedAlloca(BasicBlock *BB, AllocaInst *AI);
120     void PromoteLocallyUsedAllocas(BasicBlock *BB,
121                                    const std::vector<AllocaInst*> &AIs);
122
123     void RenamePass(BasicBlock *BB, BasicBlock *Pred,
124                     std::vector<Value*> &IncVals);
125     bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version,
126                       SmallPtrSet<PHINode*, 16> &InsertedPHINodes);
127   };
128 }  // end of anonymous namespace
129
130 void PromoteMem2Reg::run() {
131   Function &F = *DF.getRoot()->getParent();
132
133   // LocallyUsedAllocas - Keep track of all of the alloca instructions which are
134   // only used in a single basic block.  These instructions can be efficiently
135   // promoted by performing a single linear scan over that one block.  Since
136   // individual basic blocks are sometimes large, we group together all allocas
137   // that are live in a single basic block by the basic block they are live in.
138   std::map<BasicBlock*, std::vector<AllocaInst*> > LocallyUsedAllocas;
139
140   if (AST) PointerAllocaValues.resize(Allocas.size());
141
142   for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
143     AllocaInst *AI = Allocas[AllocaNum];
144
145     assert(isAllocaPromotable(AI, TD) &&
146            "Cannot promote non-promotable alloca!");
147     assert(AI->getParent()->getParent() == &F &&
148            "All allocas should be in the same function, which is same as DF!");
149
150     if (AI->use_empty()) {
151       // If there are no uses of the alloca, just delete it now.
152       if (AST) AST->deleteValue(AI);
153       AI->eraseFromParent();
154
155       // Remove the alloca from the Allocas list, since it has been processed
156       Allocas[AllocaNum] = Allocas.back();
157       Allocas.pop_back();
158       --AllocaNum;
159       continue;
160     }
161
162     // Calculate the set of read and write-locations for each alloca.  This is
163     // analogous to finding the 'uses' and 'definitions' of each variable.
164     std::vector<BasicBlock*> DefiningBlocks;
165     std::vector<BasicBlock*> UsingBlocks;
166
167     StoreInst  *OnlyStore = 0;
168     BasicBlock *OnlyBlock = 0;
169     bool OnlyUsedInOneBlock = true;
170
171     // As we scan the uses of the alloca instruction, keep track of stores, and
172     // decide whether all of the loads and stores to the alloca are within the
173     // same basic block.
174     Value *AllocaPointerVal = 0;
175     for (Value::use_iterator U =AI->use_begin(), E = AI->use_end(); U != E;++U){
176       Instruction *User = cast<Instruction>(*U);
177       if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
178         // Remember the basic blocks which define new values for the alloca
179         DefiningBlocks.push_back(SI->getParent());
180         AllocaPointerVal = SI->getOperand(0);
181         OnlyStore = SI;
182       } else {
183         LoadInst *LI = cast<LoadInst>(User);
184         // Otherwise it must be a load instruction, keep track of variable reads
185         UsingBlocks.push_back(LI->getParent());
186         AllocaPointerVal = LI;
187       }
188
189       if (OnlyUsedInOneBlock) {
190         if (OnlyBlock == 0)
191           OnlyBlock = User->getParent();
192         else if (OnlyBlock != User->getParent())
193           OnlyUsedInOneBlock = false;
194       }
195     }
196
197     // If the alloca is only read and written in one basic block, just perform a
198     // linear sweep over the block to eliminate it.
199     if (OnlyUsedInOneBlock) {
200       LocallyUsedAllocas[OnlyBlock].push_back(AI);
201
202       // Remove the alloca from the Allocas list, since it will be processed.
203       Allocas[AllocaNum] = Allocas.back();
204       Allocas.pop_back();
205       --AllocaNum;
206       continue;
207     }
208
209     // If there is only a single store to this value, replace any loads of
210     // it that are directly dominated by the definition with the value stored.
211     if (DefiningBlocks.size() == 1) {
212       // Be aware of loads before the store.
213       std::set<BasicBlock*> ProcessedBlocks;
214       for (unsigned i = 0, e = UsingBlocks.size(); i != e; ++i)
215         // If the store dominates the block and if we haven't processed it yet,
216         // do so now.
217         if (dominates(OnlyStore->getParent(), UsingBlocks[i]))
218           if (ProcessedBlocks.insert(UsingBlocks[i]).second) {
219             BasicBlock *UseBlock = UsingBlocks[i];
220             
221             // If the use and store are in the same block, do a quick scan to
222             // verify that there are no uses before the store.
223             if (UseBlock == OnlyStore->getParent()) {
224               BasicBlock::iterator I = UseBlock->begin();
225               for (; &*I != OnlyStore; ++I) { // scan block for store.
226                 if (isa<LoadInst>(I) && I->getOperand(0) == AI)
227                   break;
228               }
229               if (&*I != OnlyStore) break;  // Do not handle this case.
230             }
231         
232             // Otherwise, if this is a different block or if all uses happen
233             // after the store, do a simple linear scan to replace loads with
234             // the stored value.
235             for (BasicBlock::iterator I = UseBlock->begin(),E = UseBlock->end();
236                  I != E; ) {
237               if (LoadInst *LI = dyn_cast<LoadInst>(I++)) {
238                 if (LI->getOperand(0) == AI) {
239                   LI->replaceAllUsesWith(OnlyStore->getOperand(0));
240                   if (AST && isa<PointerType>(LI->getType()))
241                     AST->deleteValue(LI);
242                   LI->eraseFromParent();
243                 }
244               }
245             }
246             
247             // Finally, remove this block from the UsingBlock set.
248             UsingBlocks[i] = UsingBlocks.back();
249             --i; --e;
250           }
251
252       // Finally, after the scan, check to see if the store is all that is left.
253       if (UsingBlocks.empty()) {
254         // The alloca has been processed, move on.
255         Allocas[AllocaNum] = Allocas.back();
256         Allocas.pop_back();
257         --AllocaNum;
258         continue;
259       }
260     }
261     
262     
263     if (AST)
264       PointerAllocaValues[AllocaNum] = AllocaPointerVal;
265
266     // If we haven't computed a numbering for the BB's in the function, do so
267     // now.
268     BBNumbers.compute(F);
269
270     // Compute the locations where PhiNodes need to be inserted.  Look at the
271     // dominance frontier of EACH basic-block we have a write in.
272     //
273     unsigned CurrentVersion = 0;
274     SmallPtrSet<PHINode*, 16> InsertedPHINodes;
275     std::vector<unsigned> DFBlocks;
276     while (!DefiningBlocks.empty()) {
277       BasicBlock *BB = DefiningBlocks.back();
278       DefiningBlocks.pop_back();
279
280       // Look up the DF for this write, add it to PhiNodes
281       DominanceFrontier::const_iterator it = DF.find(BB);
282       if (it != DF.end()) {
283         const DominanceFrontier::DomSetType &S = it->second;
284
285         // In theory we don't need the indirection through the DFBlocks vector.
286         // In practice, the order of calling QueuePhiNode would depend on the
287         // (unspecified) ordering of basic blocks in the dominance frontier,
288         // which would give PHI nodes non-determinstic subscripts.  Fix this by
289         // processing blocks in order of the occurance in the function.
290         for (DominanceFrontier::DomSetType::const_iterator P = S.begin(),
291              PE = S.end(); P != PE; ++P)
292           DFBlocks.push_back(BBNumbers.getNumber(*P));
293
294         // Sort by which the block ordering in the function.
295         std::sort(DFBlocks.begin(), DFBlocks.end());
296
297         for (unsigned i = 0, e = DFBlocks.size(); i != e; ++i) {
298           BasicBlock *BB = BBNumbers.getBlock(DFBlocks[i]);
299           if (QueuePhiNode(BB, AllocaNum, CurrentVersion, InsertedPHINodes))
300             DefiningBlocks.push_back(BB);
301         }
302         DFBlocks.clear();
303       }
304     }
305
306     // Now that we have inserted PHI nodes along the Iterated Dominance Frontier
307     // of the writes to the variable, scan through the reads of the variable,
308     // marking PHI nodes which are actually necessary as alive (by removing them
309     // from the InsertedPHINodes set).  This is not perfect: there may PHI
310     // marked alive because of loads which are dominated by stores, but there
311     // will be no unmarked PHI nodes which are actually used.
312     //
313     for (unsigned i = 0, e = UsingBlocks.size(); i != e; ++i)
314       MarkDominatingPHILive(UsingBlocks[i], AllocaNum, InsertedPHINodes);
315     UsingBlocks.clear();
316
317     // If there are any PHI nodes which are now known to be dead, remove them!
318     for (SmallPtrSet<PHINode*, 16>::iterator I = InsertedPHINodes.begin(),
319            E = InsertedPHINodes.end(); I != E; ++I) {
320       PHINode *PN = *I;
321       std::vector<PHINode*> &BBPNs = NewPhiNodes[PN->getParent()];
322       BBPNs[AllocaNum] = 0;
323
324       // Check to see if we just removed the last inserted PHI node from this
325       // basic block.  If so, remove the entry for the basic block.
326       bool HasOtherPHIs = false;
327       for (unsigned i = 0, e = BBPNs.size(); i != e; ++i)
328         if (BBPNs[i]) {
329           HasOtherPHIs = true;
330           break;
331         }
332       if (!HasOtherPHIs)
333         NewPhiNodes.erase(PN->getParent());
334
335       if (AST && isa<PointerType>(PN->getType()))
336         AST->deleteValue(PN);
337       PN->eraseFromParent();
338     }
339
340     // Keep the reverse mapping of the 'Allocas' array.
341     AllocaLookup[Allocas[AllocaNum]] = AllocaNum;
342   }
343
344   // Process all allocas which are only used in a single basic block.
345   for (std::map<BasicBlock*, std::vector<AllocaInst*> >::iterator I =
346          LocallyUsedAllocas.begin(), E = LocallyUsedAllocas.end(); I != E; ++I){
347     const std::vector<AllocaInst*> &LocAllocas = I->second;
348     assert(!LocAllocas.empty() && "empty alloca list??");
349
350     // It's common for there to only be one alloca in the list.  Handle it
351     // efficiently.
352     if (LocAllocas.size() == 1) {
353       // If we can do the quick promotion pass, do so now.
354       if (PromoteLocallyUsedAlloca(I->first, LocAllocas[0]))
355         RetryList.push_back(LocAllocas[0]);  // Failed, retry later.
356     } else {
357       // Locally promote anything possible.  Note that if this is unable to
358       // promote a particular alloca, it puts the alloca onto the Allocas vector
359       // for global processing.
360       PromoteLocallyUsedAllocas(I->first, LocAllocas);
361     }
362   }
363
364   if (Allocas.empty())
365     return; // All of the allocas must have been trivial!
366
367   // Set the incoming values for the basic block to be null values for all of
368   // the alloca's.  We do this in case there is a load of a value that has not
369   // been stored yet.  In this case, it will get this null value.
370   //
371   std::vector<Value *> Values(Allocas.size());
372   for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
373     Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
374
375   // Walks all basic blocks in the function performing the SSA rename algorithm
376   // and inserting the phi nodes we marked as necessary
377   //
378   RenamePass(F.begin(), 0, Values);
379
380   // The renamer uses the Visited set to avoid infinite loops.  Clear it now.
381   Visited.clear();
382
383   // Remove the allocas themselves from the function.
384   for (unsigned i = 0, e = Allocas.size(); i != e; ++i) {
385     Instruction *A = Allocas[i];
386
387     // If there are any uses of the alloca instructions left, they must be in
388     // sections of dead code that were not processed on the dominance frontier.
389     // Just delete the users now.
390     //
391     if (!A->use_empty())
392       A->replaceAllUsesWith(UndefValue::get(A->getType()));
393     if (AST) AST->deleteValue(A);
394     A->eraseFromParent();
395   }
396
397   
398   // Loop over all of the PHI nodes and see if there are any that we can get
399   // rid of because they merge all of the same incoming values.  This can
400   // happen due to undef values coming into the PHI nodes.  This process is
401   // iterative, because eliminating one PHI node can cause others to be removed.
402   bool EliminatedAPHI = true;
403   while (EliminatedAPHI) {
404     EliminatedAPHI = false;
405     
406     for (std::map<BasicBlock*, std::vector<PHINode *> >::iterator I =
407            NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E; ++I) {
408       std::vector<PHINode*> &PNs = I->second;
409       for (unsigned i = 0, e = PNs.size(); i != e; ++i) {
410         if (!PNs[i]) continue;
411
412         // If this PHI node merges one value and/or undefs, get the value.
413         if (Value *V = PNs[i]->hasConstantValue(true)) {
414           if (!isa<Instruction>(V) ||
415               properlyDominates(cast<Instruction>(V), PNs[i])) {
416             if (AST && isa<PointerType>(PNs[i]->getType()))
417               AST->deleteValue(PNs[i]);
418             PNs[i]->replaceAllUsesWith(V);
419             PNs[i]->eraseFromParent();
420             PNs[i] = 0;
421             EliminatedAPHI = true;
422             continue;
423           }
424         }
425       }
426     }
427   }
428   
429   // At this point, the renamer has added entries to PHI nodes for all reachable
430   // code.  Unfortunately, there may be unreachable blocks which the renamer
431   // hasn't traversed.  If this is the case, the PHI nodes may not
432   // have incoming values for all predecessors.  Loop over all PHI nodes we have
433   // created, inserting undef values if they are missing any incoming values.
434   //
435   for (std::map<BasicBlock*, std::vector<PHINode *> >::iterator I =
436          NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E; ++I) {
437
438     std::vector<BasicBlock*> Preds(pred_begin(I->first), pred_end(I->first));
439     std::vector<PHINode*> &PNs = I->second;
440     assert(!PNs.empty() && "Empty PHI node list??");
441     PHINode *SomePHI = 0;
442     for (unsigned i = 0, e = PNs.size(); i != e; ++i)
443       if (PNs[i]) {
444         SomePHI = PNs[i];
445         break;
446       }
447
448     // Only do work here if there the PHI nodes are missing incoming values.  We
449     // know that all PHI nodes that were inserted in a block will have the same
450     // number of incoming values, so we can just check any PHI node.
451     if (SomePHI && Preds.size() != SomePHI->getNumIncomingValues()) {
452       // Ok, now we know that all of the PHI nodes are missing entries for some
453       // basic blocks.  Start by sorting the incoming predecessors for efficient
454       // access.
455       std::sort(Preds.begin(), Preds.end());
456
457       // Now we loop through all BB's which have entries in SomePHI and remove
458       // them from the Preds list.
459       for (unsigned i = 0, e = SomePHI->getNumIncomingValues(); i != e; ++i) {
460         // Do a log(n) search of the Preds list for the entry we want.
461         std::vector<BasicBlock*>::iterator EntIt =
462           std::lower_bound(Preds.begin(), Preds.end(),
463                            SomePHI->getIncomingBlock(i));
464         assert(EntIt != Preds.end() && *EntIt == SomePHI->getIncomingBlock(i)&&
465                "PHI node has entry for a block which is not a predecessor!");
466
467         // Remove the entry
468         Preds.erase(EntIt);
469       }
470
471       // At this point, the blocks left in the preds list must have dummy
472       // entries inserted into every PHI nodes for the block.
473       for (unsigned i = 0, e = PNs.size(); i != e; ++i)
474         if (PHINode *PN = PNs[i]) {
475           Value *UndefVal = UndefValue::get(PN->getType());
476           for (unsigned pred = 0, e = Preds.size(); pred != e; ++pred)
477             PN->addIncoming(UndefVal, Preds[pred]);
478         }
479     }
480   }
481 }
482
483 // MarkDominatingPHILive - Mem2Reg wants to construct "pruned" SSA form, not
484 // "minimal" SSA form.  To do this, it inserts all of the PHI nodes on the IDF
485 // as usual (inserting the PHI nodes in the DeadPHINodes set), then processes
486 // each read of the variable.  For each block that reads the variable, this
487 // function is called, which removes used PHI nodes from the DeadPHINodes set.
488 // After all of the reads have been processed, any PHI nodes left in the
489 // DeadPHINodes set are removed.
490 //
491 void PromoteMem2Reg::MarkDominatingPHILive(BasicBlock *BB, unsigned AllocaNum,
492                                       SmallPtrSet<PHINode*, 16> &DeadPHINodes) {
493   // Scan the immediate dominators of this block looking for a block which has a
494   // PHI node for Alloca num.  If we find it, mark the PHI node as being alive!
495   for (DominatorTree::Node *N = DT[BB]; N; N = N->getIDom()) {
496     BasicBlock *DomBB = N->getBlock();
497     std::map<BasicBlock*, std::vector<PHINode*> >::iterator
498       I = NewPhiNodes.find(DomBB);
499     if (I != NewPhiNodes.end() && I->second[AllocaNum]) {
500       // Ok, we found an inserted PHI node which dominates this value.
501       PHINode *DominatingPHI = I->second[AllocaNum];
502
503       // Find out if we previously thought it was dead.  If so, mark it as being
504       // live by removing it from the DeadPHINodes set.
505       if (DeadPHINodes.erase(DominatingPHI)) {
506         // Now that we have marked the PHI node alive, also mark any PHI nodes
507         // which it might use as being alive as well.
508         for (pred_iterator PI = pred_begin(DomBB), PE = pred_end(DomBB);
509              PI != PE; ++PI)
510           MarkDominatingPHILive(*PI, AllocaNum, DeadPHINodes);
511       }
512     }
513   }
514 }
515
516 /// PromoteLocallyUsedAlloca - Many allocas are only used within a single basic
517 /// block.  If this is the case, avoid traversing the CFG and inserting a lot of
518 /// potentially useless PHI nodes by just performing a single linear pass over
519 /// the basic block using the Alloca.
520 ///
521 /// If we cannot promote this alloca (because it is read before it is written),
522 /// return true.  This is necessary in cases where, due to control flow, the
523 /// alloca is potentially undefined on some control flow paths.  e.g. code like
524 /// this is potentially correct:
525 ///
526 ///   for (...) { if (c) { A = undef; undef = B; } }
527 ///
528 /// ... so long as A is not used before undef is set.
529 ///
530 bool PromoteMem2Reg::PromoteLocallyUsedAlloca(BasicBlock *BB, AllocaInst *AI) {
531   assert(!AI->use_empty() && "There are no uses of the alloca!");
532
533   // Handle degenerate cases quickly.
534   if (AI->hasOneUse()) {
535     Instruction *U = cast<Instruction>(AI->use_back());
536     if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
537       // Must be a load of uninitialized value.
538       LI->replaceAllUsesWith(UndefValue::get(AI->getAllocatedType()));
539       if (AST && isa<PointerType>(LI->getType()))
540         AST->deleteValue(LI);
541     } else {
542       // Otherwise it must be a store which is never read.
543       assert(isa<StoreInst>(U));
544     }
545     BB->getInstList().erase(U);
546   } else {
547     // Uses of the uninitialized memory location shall get undef.
548     Value *CurVal = 0;
549
550     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
551       Instruction *Inst = I++;
552       if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
553         if (LI->getOperand(0) == AI) {
554           if (!CurVal) return true;  // Could not locally promote!
555
556           // Loads just returns the "current value"...
557           LI->replaceAllUsesWith(CurVal);
558           if (AST && isa<PointerType>(LI->getType()))
559             AST->deleteValue(LI);
560           BB->getInstList().erase(LI);
561         }
562       } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
563         if (SI->getOperand(1) == AI) {
564           // Store updates the "current value"...
565           CurVal = SI->getOperand(0);
566           BB->getInstList().erase(SI);
567         }
568       }
569     }
570   }
571
572   // After traversing the basic block, there should be no more uses of the
573   // alloca, remove it now.
574   assert(AI->use_empty() && "Uses of alloca from more than one BB??");
575   if (AST) AST->deleteValue(AI);
576   AI->getParent()->getInstList().erase(AI);
577   return false;
578 }
579
580 /// PromoteLocallyUsedAllocas - This method is just like
581 /// PromoteLocallyUsedAlloca, except that it processes multiple alloca
582 /// instructions in parallel.  This is important in cases where we have large
583 /// basic blocks, as we don't want to rescan the entire basic block for each
584 /// alloca which is locally used in it (which might be a lot).
585 void PromoteMem2Reg::
586 PromoteLocallyUsedAllocas(BasicBlock *BB, const std::vector<AllocaInst*> &AIs) {
587   std::map<AllocaInst*, Value*> CurValues;
588   for (unsigned i = 0, e = AIs.size(); i != e; ++i)
589     CurValues[AIs[i]] = 0; // Insert with null value
590
591   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
592     Instruction *Inst = I++;
593     if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
594       // Is this a load of an alloca we are tracking?
595       if (AllocaInst *AI = dyn_cast<AllocaInst>(LI->getOperand(0))) {
596         std::map<AllocaInst*, Value*>::iterator AIt = CurValues.find(AI);
597         if (AIt != CurValues.end()) {
598           // If loading an uninitialized value, allow the inter-block case to
599           // handle it.  Due to control flow, this might actually be ok.
600           if (AIt->second == 0) {  // Use of locally uninitialized value??
601             RetryList.push_back(AI);   // Retry elsewhere.
602             CurValues.erase(AIt);   // Stop tracking this here.
603             if (CurValues.empty()) return;
604           } else {
605             // Loads just returns the "current value"...
606             LI->replaceAllUsesWith(AIt->second);
607             if (AST && isa<PointerType>(LI->getType()))
608               AST->deleteValue(LI);
609             BB->getInstList().erase(LI);
610           }
611         }
612       }
613     } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
614       if (AllocaInst *AI = dyn_cast<AllocaInst>(SI->getOperand(1))) {
615         std::map<AllocaInst*, Value*>::iterator AIt = CurValues.find(AI);
616         if (AIt != CurValues.end()) {
617           // Store updates the "current value"...
618           AIt->second = SI->getOperand(0);
619           BB->getInstList().erase(SI);
620         }
621       }
622     }
623   }
624 }
625
626
627
628 // QueuePhiNode - queues a phi-node to be added to a basic-block for a specific
629 // Alloca returns true if there wasn't already a phi-node for that variable
630 //
631 bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo,
632                                   unsigned &Version,
633                                   SmallPtrSet<PHINode*, 16> &InsertedPHINodes) {
634   // Look up the basic-block in question.
635   std::vector<PHINode*> &BBPNs = NewPhiNodes[BB];
636   if (BBPNs.empty()) BBPNs.resize(Allocas.size());
637
638   // If the BB already has a phi node added for the i'th alloca then we're done!
639   if (BBPNs[AllocaNo]) return false;
640
641   // Create a PhiNode using the dereferenced type... and add the phi-node to the
642   // BasicBlock.
643   PHINode *PN = new PHINode(Allocas[AllocaNo]->getAllocatedType(),
644                             Allocas[AllocaNo]->getName() + "." +
645                                         utostr(Version++), BB->begin());
646   BBPNs[AllocaNo] = PN;
647   InsertedPHINodes.insert(PN);
648
649   if (AST && isa<PointerType>(PN->getType()))
650     AST->copyValue(PointerAllocaValues[AllocaNo], PN);
651
652   return true;
653 }
654
655
656 // RenamePass - Recursively traverse the CFG of the function, renaming loads and
657 // stores to the allocas which we are promoting.  IncomingVals indicates what
658 // value each Alloca contains on exit from the predecessor block Pred.
659 //
660 void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
661                                 std::vector<Value*> &IncomingVals) {
662
663   // If this BB needs a PHI node, update the PHI node for each variable we need
664   // PHI nodes for.
665   std::map<BasicBlock*, std::vector<PHINode *> >::iterator
666     BBPNI = NewPhiNodes.find(BB);
667   if (BBPNI != NewPhiNodes.end()) {
668     std::vector<PHINode *> &BBPNs = BBPNI->second;
669     for (unsigned k = 0; k != BBPNs.size(); ++k)
670       if (PHINode *PN = BBPNs[k]) {
671         // Add this incoming value to the PHI node.
672         PN->addIncoming(IncomingVals[k], Pred);
673
674         // The currently active variable for this block is now the PHI.
675         IncomingVals[k] = PN;
676       }
677   }
678
679   // don't revisit nodes
680   if (Visited.count(BB)) return;
681
682   // mark as visited
683   Visited.insert(BB);
684
685   for (BasicBlock::iterator II = BB->begin(); !isa<TerminatorInst>(II); ) {
686     Instruction *I = II++; // get the instruction, increment iterator
687
688     if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
689       if (AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand())) {
690         std::map<AllocaInst*, unsigned>::iterator AI = AllocaLookup.find(Src);
691         if (AI != AllocaLookup.end()) {
692           Value *V = IncomingVals[AI->second];
693
694           // walk the use list of this load and replace all uses with r
695           LI->replaceAllUsesWith(V);
696           if (AST && isa<PointerType>(LI->getType()))
697             AST->deleteValue(LI);
698           BB->getInstList().erase(LI);
699         }
700       }
701     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
702       // Delete this instruction and mark the name as the current holder of the
703       // value
704       if (AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand())) {
705         std::map<AllocaInst *, unsigned>::iterator ai = AllocaLookup.find(Dest);
706         if (ai != AllocaLookup.end()) {
707           // what value were we writing?
708           IncomingVals[ai->second] = SI->getOperand(0);
709           BB->getInstList().erase(SI);
710         }
711       }
712     }
713   }
714
715   // Recurse to our successors.
716   TerminatorInst *TI = BB->getTerminator();
717   for (unsigned i = 0; i != TI->getNumSuccessors(); i++) {
718     std::vector<Value*> OutgoingVals(IncomingVals);
719     RenamePass(TI->getSuccessor(i), BB, OutgoingVals);
720   }
721 }
722
723 /// PromoteMemToReg - Promote the specified list of alloca instructions into
724 /// scalar registers, inserting PHI nodes as appropriate.  This function makes
725 /// use of DominanceFrontier information.  This function does not modify the CFG
726 /// of the function at all.  All allocas must be from the same function.
727 ///
728 /// If AST is specified, the specified tracker is updated to reflect changes
729 /// made to the IR.
730 ///
731 void llvm::PromoteMemToReg(const std::vector<AllocaInst*> &Allocas,
732                            DominatorTree &DT, DominanceFrontier &DF,
733                            const TargetData &TD, AliasSetTracker *AST) {
734   // If there is nothing to do, bail out...
735   if (Allocas.empty()) return;
736
737   SmallVector<AllocaInst*, 16> RetryList;
738   PromoteMem2Reg(Allocas, RetryList, DT, DF, TD, AST).run();
739
740   // PromoteMem2Reg may not have been able to promote all of the allocas in one
741   // pass, run it again if needed.
742   std::vector<AllocaInst*> NewAllocas;
743   while (!RetryList.empty()) {
744     // If we need to retry some allocas, this is due to there being no store
745     // before a read in a local block.  To counteract this, insert a store of
746     // undef into the alloca right after the alloca itself.
747     for (unsigned i = 0, e = RetryList.size(); i != e; ++i) {
748       BasicBlock::iterator BBI = RetryList[i];
749
750       new StoreInst(UndefValue::get(RetryList[i]->getAllocatedType()),
751                     RetryList[i], ++BBI);
752     }
753
754     NewAllocas.assign(RetryList.begin(), RetryList.end());
755     RetryList.clear();
756     PromoteMem2Reg(NewAllocas, RetryList, DT, DF, TD, AST).run();
757     NewAllocas.clear();
758   }
759 }