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