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