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