Two small cleanups/speedups:
[oota-llvm.git] / lib / Transforms / Utils / PromoteMemoryToRegister.cpp
1 //===- PromoteMemoryToRegister.cpp - Convert memory refs to regs ----------===//
2 //
3 // This file is used to promote memory references to be register references.  A
4 // simple example of the transformation performed by this function is:
5 //
6 //        FROM CODE                           TO CODE
7 //   %X = alloca int, uint 1                 ret int 42
8 //   store int 42, int *%X
9 //   %Y = load int* %X
10 //   ret int %Y
11 //
12 // The code is transformed by looping over all of the alloca instruction,
13 // calculating dominator frontiers, then inserting phi-nodes following the usual
14 // SSA construction algorithm.  This code does not modify the CFG of the
15 // function.
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/iTerminators.h"
24 #include "llvm/Function.h"
25 #include "llvm/Constant.h"
26 #include "llvm/Type.h"
27 #include "llvm/Support/CFG.h"
28 #include "Support/StringExtras.h"
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...
32 ///
33 bool isAllocaPromotable(const AllocaInst *AI, const TargetData &TD) {
34   // FIXME: If the memory unit is of pointer or integer type, we can permit
35   // assignments to subsections of the memory unit.
36
37   // Only allow direct loads and stores...
38   for (Value::use_const_iterator UI = AI->use_begin(), UE = AI->use_end();
39        UI != UE; ++UI)     // Loop over all of the uses of the alloca
40     if (!isa<LoadInst>(*UI))
41       if (const StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
42         if (SI->getOperand(0) == AI)
43           return false;   // Don't allow a store of the AI, only INTO the AI.
44       } else {
45         return false;   // Not a load or store?
46       }
47   
48   return true;
49 }
50
51
52 namespace {
53   struct PromoteMem2Reg {
54     const std::vector<AllocaInst*> &Allocas;        // the alloca instructions..
55     std::vector<unsigned> VersionNumbers;           // Current version counters
56     DominanceFrontier &DF;
57     const TargetData &TD;
58
59     std::map<Instruction*, unsigned>  AllocaLookup; // reverse mapping of above
60     
61     std::vector<std::vector<BasicBlock*> > PhiNodes;// Idx corresponds 2 Allocas
62     
63     // NewPhiNodes - The PhiNodes we're adding.
64     std::map<BasicBlock*, std::vector<PHINode*> > NewPhiNodes;
65
66     // Visited - The set of basic blocks the renamer has already visited.
67     std::set<BasicBlock*> Visited;
68
69   public:
70     PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominanceFrontier &df,
71                    const TargetData &td) : Allocas(A), DF(df), TD(td) {}
72
73     void run();
74
75   private:
76     void RenamePass(BasicBlock *BB, BasicBlock *Pred,
77                     std::vector<Value*> &IncVals);
78     bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx);
79   };
80 }  // end of anonymous namespace
81
82
83 void PromoteMem2Reg::run() {
84   Function &F = *DF.getRoot()->getParent();
85
86   VersionNumbers.resize(Allocas.size());
87
88   for (unsigned i = 0, e = Allocas.size(); i != e; ++i) {
89     assert(isAllocaPromotable(Allocas[i], TD) &&
90            "Cannot promote non-promotable alloca!");
91     assert(Allocas[i]->getParent()->getParent() == &F &&
92            "All allocas should be in the same function, which is same as DF!");
93     AllocaLookup[Allocas[i]] = i;
94   }
95
96   PhiNodes.resize(Allocas.size());
97   for (unsigned i = 0; i != Allocas.size(); ++i) {
98     AllocaInst *AI = Allocas[i];
99
100     // Calculate the set of write-locations for each alloca.  This is analogous
101     // to counting the number of 'redefinitions' of each variable.
102     std::vector<BasicBlock*> WriteSets;
103     for (Value::use_iterator U =AI->use_begin(), E = AI->use_end(); U != E; ++U)
104       if (StoreInst *SI = dyn_cast<StoreInst>(*U))
105         // jot down the basic-block it came from
106         WriteSets.push_back(SI->getParent());
107     
108     // Compute the locations where PhiNodes need to be inserted.  Look at the
109     // dominance frontier of EACH basic-block we have a write in.
110     //
111     for (unsigned j = 0; j != WriteSets.size(); j++) {
112       // Look up the DF for this write, add it to PhiNodes
113       DominanceFrontier::const_iterator it = DF.find(WriteSets[j]);
114       if (it != DF.end()) {
115         const DominanceFrontier::DomSetType &S = it->second;
116         for (DominanceFrontier::DomSetType::iterator P = S.begin(),PE = S.end();
117              P != PE; ++P)
118           QueuePhiNode(*P, i);
119       }
120     }
121     
122     // Perform iterative step
123     for (unsigned k = 0; k != PhiNodes[i].size(); k++) {
124       DominanceFrontier::const_iterator it = DF.find(PhiNodes[i][k]);
125       if (it != DF.end()) {
126         const DominanceFrontier::DomSetType     &S = it->second;
127         for (DominanceFrontier::DomSetType::iterator P = S.begin(),PE = S.end();
128              P != PE; ++P)
129           QueuePhiNode(*P, i);
130       }
131     }
132   }
133
134   // Set the incoming values for the basic block to be null values for all of
135   // the alloca's.  We do this in case there is a load of a value that has not
136   // been stored yet.  In this case, it will get this null value.
137   //
138   std::vector<Value *> Values(Allocas.size());
139   for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
140     Values[i] = Constant::getNullValue(Allocas[i]->getAllocatedType());
141
142   // Walks all basic blocks in the function performing the SSA rename algorithm
143   // and inserting the phi nodes we marked as necessary
144   //
145   RenamePass(F.begin(), 0, Values);
146   Visited.clear();
147
148   // Remove the allocas themselves from the function...
149   for (unsigned i = 0, e = Allocas.size(); i != e; ++i) {
150     Instruction *A = Allocas[i];
151
152     // If there are any uses of the alloca instructions left, they must be in
153     // sections of dead code that were not processed on the dominance frontier.
154     // Just delete the users now.
155     //
156     if (!A->use_empty())
157       A->replaceAllUsesWith(Constant::getNullValue(A->getType()));
158     A->getParent()->getInstList().erase(A);
159   }
160 }
161
162
163 // QueuePhiNode - queues a phi-node to be added to a basic-block for a specific
164 // Alloca returns true if there wasn't already a phi-node for that variable
165 //
166 bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo) {
167   // Look up the basic-block in question
168   std::vector<PHINode*> &BBPNs = NewPhiNodes[BB];
169   if (BBPNs.empty()) BBPNs.resize(Allocas.size());
170
171   // If the BB already has a phi node added for the i'th alloca then we're done!
172   if (BBPNs[AllocaNo]) return false;
173
174   // Create a PhiNode using the dereferenced type... and add the phi-node to the
175   // BasicBlock.
176   PHINode *PN = new PHINode(Allocas[AllocaNo]->getAllocatedType(),
177                             Allocas[AllocaNo]->getName() + "." +
178                                       utostr(VersionNumbers[AllocaNo]++),
179                             BB->begin());
180
181   // Add null incoming values for all predecessors.  This ensures that if one of
182   // the predecessors is not found in the depth-first traversal of the CFG (ie,
183   // because it is an unreachable predecessor), that all PHI nodes will have the
184   // correct number of entries for their predecessors.
185   Value *NullVal = Constant::getNullValue(PN->getType());
186
187   // This is necessary because adding incoming values to the PHI node adds uses
188   // to the basic blocks being used, which can invalidate the predecessor
189   // iterator!
190   std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
191   for (unsigned i = 0, e = Preds.size(); i != e; ++i)
192     PN->addIncoming(NullVal, Preds[i]);
193
194   BBPNs[AllocaNo] = PN;
195   PhiNodes[AllocaNo].push_back(BB);
196   return true;
197 }
198
199 void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
200                                 std::vector<Value*> &IncomingVals) {
201   // If this BB needs a PHI node, update the PHI node for each variable we need
202   // PHI nodes for.
203   std::map<BasicBlock*, std::vector<PHINode *> >::iterator
204     BBPNI = NewPhiNodes.find(BB);
205   if (BBPNI != NewPhiNodes.end()) {
206     std::vector<PHINode *> &BBPNs = BBPNI->second;
207     for (unsigned k = 0; k != BBPNs.size(); ++k)
208       if (PHINode *PN = BBPNs[k]) {
209         // The PHI node may have multiple entries for this predecessor.  We must
210         // make sure we update all of them.
211         for (unsigned i = 0, e = PN->getNumOperands(); i != e; i += 2) {
212           if (PN->getOperand(i+1) == Pred)
213             // At this point we can assume that the array has phi nodes.. let's
214             // update the incoming data.
215             PN->setOperand(i, IncomingVals[k]);
216         }
217         // also note that the active variable IS designated by the phi node
218         IncomingVals[k] = PN;
219       }
220   }
221
222   // don't revisit nodes
223   if (Visited.count(BB)) return;
224   
225   // mark as visited
226   Visited.insert(BB);
227
228   BasicBlock::iterator II = BB->begin();
229   while (1) {
230     Instruction *I = II++; // get the instruction, increment iterator
231
232     if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
233       if (AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand())) {
234         std::map<Instruction*, unsigned>::iterator AI = AllocaLookup.find(Src);
235         if (AI != AllocaLookup.end()) {
236           Value *V = IncomingVals[AI->second];
237
238           // walk the use list of this load and replace all uses with r
239           LI->replaceAllUsesWith(V);
240           BB->getInstList().erase(LI);
241         }
242       }
243     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
244       // Delete this instruction and mark the name as the current holder of the
245       // value
246       if (AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand())) {
247         std::map<Instruction *, unsigned>::iterator ai =AllocaLookup.find(Dest);
248         if (ai != AllocaLookup.end()) {
249           // what value were we writing?
250           IncomingVals[ai->second] = SI->getOperand(0);
251           BB->getInstList().erase(SI);
252         }
253       }
254       
255     } else if (TerminatorInst *TI = dyn_cast<TerminatorInst>(I)) {
256       // Recurse across our successors
257       for (unsigned i = 0; i != TI->getNumSuccessors(); i++) {
258         std::vector<Value*> OutgoingVals(IncomingVals);
259         RenamePass(TI->getSuccessor(i), BB, OutgoingVals);
260       }
261       break;
262     }
263   }
264 }
265
266 /// PromoteMemToReg - Promote the specified list of alloca instructions into
267 /// scalar registers, inserting PHI nodes as appropriate.  This function makes
268 /// use of DominanceFrontier information.  This function does not modify the CFG
269 /// of the function at all.  All allocas must be from the same function.
270 ///
271 void PromoteMemToReg(const std::vector<AllocaInst*> &Allocas,
272                      DominanceFrontier &DF, const TargetData &TD) {
273   // If there is nothing to do, bail out...
274   if (Allocas.empty()) return;
275   PromoteMem2Reg(Allocas, DF, TD).run();
276 }