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