Implemented promote mem->reg pass.
[oota-llvm.git] / lib / Transforms / Utils / PromoteMemoryToRegister.cpp
1 //===- PromoteMemoryToRegister.cpp - Convert memory refs to regs ----------===//
2 //
3 // This pass is used to promote memory references to be register references.  A
4 // simple example of the transformation performed by this pass 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 // To do this transformation, a simple analysis is done to ensure it is safe.
13 // Currently this just loops over all alloca instructions, looking for
14 // instructions that are only used in simple load and stores.
15 //
16 // After this, the code is transformed by...something magical :)
17 //
18 //===----------------------------------------------------------------------===//
19
20 #include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
21 #include "llvm/Analysis/Dominators.h"
22 #include "llvm/iMemory.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Method.h"
25 #include "llvm/BasicBlock.h"
26 #include "llvm/Assembly/Writer.h"  // For debugging
27 #include "llvm/iPHINode.h"
28 #include "llvm/iTerminators.h"
29
30 using namespace std;
31
32
33 using cfg::DominanceFrontier;
34
35
36 //instance of the promoter -- to keep all the local method data.
37 // gets re-created for each method processed
38 class PromoteInstance
39 {
40         protected:
41         vector<AllocaInst*>                     Allocas;   // the alloca instruction..
42         map<Instruction *, int>                 AllocaLookup; //reverse mapping of above
43
44         vector<vector<BasicBlock *> >           WriteSets; // index corresponds to Allocas
45         vector<vector<BasicBlock *> >           PhiNodes;  // index corresponds to Allocas
46         vector<vector<Value *> >                CurrentValue; //the current value stack
47
48         //list of instructions to remove at end of pass :)
49         vector<Instruction *> killlist;
50
51         set<BasicBlock *>                       visited;        //the basic blocks we've already visited
52         map<BasicBlock *, vector<PHINode *> >   new_phinodes;   //the phinodes we're adding
53
54
55         void traverse(BasicBlock *f, BasicBlock * predecessor);
56         bool PromoteMethod(Method *M, DominanceFrontier & DF);
57         bool queuePhiNode(BasicBlock *bb, int alloca_index);
58         void findSafeAllocas(Method *M);
59         bool didchange;
60         public:
61         // I do this so that I can force the deconstruction of the local variables
62         PromoteInstance(Method *M, DominanceFrontier & DF)
63         {
64                 didchange=PromoteMethod(M, DF);
65         }
66         //This returns whether the pass changes anything
67         operator bool () { return didchange; }
68 };
69
70 class PromotePass : public MethodPass {
71         public:
72
73         // runOnMethod - To run this pass, first we calculate the alloca instructions
74         // that are safe for promotion, then we promote each one.
75         //
76         virtual bool runOnMethod(Method *M)
77         {
78                 PromoteInstance inst(M, getAnalysis<DominanceFrontier>());
79                 return (bool)inst;
80         }
81
82
83         // getAnalysisUsageInfo - We need dominance frontiers
84         //
85         virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
86         Pass::AnalysisSet &Destroyed,
87         Pass::AnalysisSet &Provided) {
88                 Requires.push_back(DominanceFrontier::ID);
89         }
90 };
91
92
93 // findSafeAllocas - Find allocas that are safe to promote
94 //
95 void PromoteInstance::findSafeAllocas(Method *M)  
96 {
97   BasicBlock *BB = M->front();  // Get the entry node for the method
98
99   // Look at all instructions in the entry node
100   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
101     if (AllocaInst *AI = dyn_cast<AllocaInst>(*I))       // Is it an alloca?
102       if (!AI->isArrayAllocation()) {
103         bool isSafe = true;
104         for (Value::use_iterator UI = AI->use_begin(), UE = AI->use_end();
105              UI != UE; ++UI) {   // Loop over all of the uses of the alloca
106
107           // Only allow nonindexed memory access instructions...
108           if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*UI)) {
109             if (MAI->hasIndices()) {  // indexed?
110               // Allow the access if there is only one index and the index is zero.
111               if (*MAI->idx_begin() != ConstantUInt::get(Type::UIntTy, 0) ||
112                   MAI->idx_begin()+1 != MAI->idx_end()) {
113                 isSafe = false; break;
114               }
115             }
116           } else {
117             isSafe = false; break;   // Not a load or store?
118           }
119         }
120         if (isSafe)              // If all checks pass, add alloca to safe list
121           {
122             AllocaLookup[AI]=Allocas.size();
123             Allocas.push_back(AI);
124           }
125       }
126 }
127
128
129
130 // newPromoteMemoryToRegister - Provide an entry point to create this pass.
131 //
132 Pass *newPromoteMemoryToRegister() {
133         return new PromotePass();
134 }
135
136
137 bool PromoteInstance::PromoteMethod(Method *M, DominanceFrontier & DF) {
138         // Calculate the set of safe allocas
139         findSafeAllocas(M);
140
141         // Add each alloca to the killlist
142         // note: killlist is destroyed MOST recently added to least recently.
143         killlist.assign(Allocas.begin(), Allocas.end());
144
145         // Calculate the set of write-locations for each alloca.
146         // this is analogous to counting the number of 'redefinitions' of each variable.
147         for (unsigned i = 0; i<Allocas.size(); ++i)
148         {
149                 AllocaInst * AI = Allocas[i];
150                 WriteSets.push_back(std::vector<BasicBlock *>()); //add a new set
151                 for (Value::use_iterator U = AI->use_begin();U!=AI->use_end();++U)
152                 {
153                         if (MemAccessInst *MAI = dyn_cast<StoreInst>(*U)) {
154                                 WriteSets[i].push_back(MAI->getParent()); // jot down the basic-block it came from
155                         }
156                 }
157         }
158
159         // Compute the locations where PhiNodes need to be inserted
160         // look at the dominance frontier of EACH basic-block we have a write in
161         PhiNodes.resize(Allocas.size());
162         for (unsigned i = 0; i<Allocas.size(); ++i)
163         {
164                 for (unsigned j = 0; j<WriteSets[i].size(); j++)
165                 {
166                         //look up the DF for this write, add it to PhiNodes
167                         DominanceFrontier::const_iterator it = DF.find(WriteSets[i][j]);
168                         DominanceFrontier::DomSetType     s = (*it).second;
169                         for (DominanceFrontier::DomSetType::iterator p = s.begin();p!=s.end(); ++p)
170                         {
171                                 if (queuePhiNode((BasicBlock *)*p, i))
172                                 PhiNodes[i].push_back((BasicBlock *)*p);
173                         }
174                 }
175                 // perform iterative step
176                 for (unsigned k = 0; k<PhiNodes[i].size(); k++)
177                 {
178                         DominanceFrontier::const_iterator it = DF.find(PhiNodes[i][k]);
179                         DominanceFrontier::DomSetType     s = it->second;
180                         for (DominanceFrontier::DomSetType::iterator p = s.begin(); p!=s.end(); ++p)
181                         {
182                                 if (queuePhiNode((BasicBlock *)*p,i))
183                                 PhiNodes[i].push_back((BasicBlock*)*p);
184                         }
185                 }
186         }
187
188         // Walks all basic blocks in the method
189         // performing the SSA rename algorithm
190         // and inserting the phi nodes we marked as necessary
191         BasicBlock * f = M->front(); //get root basic-block
192
193         CurrentValue.push_back(vector<Value *>(Allocas.size()));
194
195         traverse(f, NULL);  // there is no predecessor of the root node
196
197
198         // ** REMOVE EVERYTHING IN THE KILL-LIST **
199         // we need to kill 'uses' before root values
200         // so we should probably run through in reverse
201         for (vector<Instruction *>::reverse_iterator i = killlist.rbegin(); i!=killlist.rend(); ++i)
202         {
203                 Instruction * r = *i;
204                 BasicBlock * o = r->getParent();
205                 //now go find..
206
207                 BasicBlock::InstListType & l = o->getInstList();
208                 o->getInstList().remove(r);
209                 delete r;
210         }
211
212         return !Allocas.empty();
213 }
214
215
216
217 void PromoteInstance::traverse(BasicBlock *f, BasicBlock * predecessor)
218 {
219         vector<Value *> * tos = &CurrentValue.back(); //look at top-
220
221         //if this is a BB needing a phi node, lookup/create the phinode for
222         // each variable we need phinodes for.
223         map<BasicBlock *, vector<PHINode *> >::iterator nd = new_phinodes.find(f);
224         if (nd!=new_phinodes.end())
225         {
226                 for (unsigned k = 0; k!=nd->second.size(); ++k)
227                 if (nd->second[k])
228                 {
229                         //at this point we can assume that the array has phi nodes.. let's
230                         // add the incoming data
231                         if ((*tos)[k])
232                         nd->second[k]->addIncoming((*tos)[k],predecessor);
233                         //also note that the active variable IS designated by the phi node
234                         (*tos)[k] = nd->second[k];
235                 }
236         }
237
238         //don't revisit nodes
239         if (visited.find(f)!=visited.end())
240         return;
241         //mark as visited
242         visited.insert(f);
243
244         BasicBlock::iterator i = f->begin();
245         //keep track of the value of each variable we're watching.. how?
246         while(i!=f->end())
247         {
248                 Instruction * inst = *i; //get the instruction
249                 //is this a write/read?
250                 if (LoadInst * LI = dyn_cast<LoadInst>(inst))
251                 {
252                         // This is a bit weird...
253                         Value * ptr = LI->getPointerOperand(); //of type value
254                         if (AllocaInst * srcinstr = dyn_cast<AllocaInst>(ptr))
255                         {
256                                 map<Instruction *, int>::iterator ai = AllocaLookup.find(srcinstr);
257                                 if (ai!=AllocaLookup.end())
258                                 {
259                                         if (Value *r = (*tos)[ai->second])
260                                         {
261                                                 //walk the use list of this load and replace
262                                                 // all uses with r
263                                                 LI->replaceAllUsesWith(r);
264                                                 //now delete the instruction.. somehow..
265                                                 killlist.push_back((Instruction *)LI);
266                                         }
267                                 }
268                         }
269                 }
270                 else if (StoreInst * SI = dyn_cast<StoreInst>(inst))
271                 {
272                         // delete this instruction and mark the name as the
273                         // current holder of the value
274                         Value * ptr =  SI->getPointerOperand(); //of type value
275                         if (Instruction * srcinstr = dyn_cast<Instruction>(ptr))
276                         {
277                                 map<Instruction *, int>::iterator ai = AllocaLookup.find(srcinstr);
278                                 if (ai!=AllocaLookup.end())
279                                 {
280                                         //what value were we writing?
281                                         Value * writeval = SI->getOperand(0);
282                                         //write down...
283                                         (*tos)[ai->second] = writeval;
284                                         //now delete it.. somehow?
285                                         killlist.push_back((Instruction *)SI);
286                                 }
287                         }
288
289                 }
290                 else if (TerminatorInst * TI = dyn_cast<TerminatorInst>(inst))
291                 {
292                         // Recurse across our sucessors
293                         for (unsigned i = 0; i!=TI->getNumSuccessors(); i++)
294                         {
295                                 CurrentValue.push_back(CurrentValue.back());
296                                 traverse(TI->getSuccessor(i),f); //this node IS the predecessor
297                                 CurrentValue.pop_back();
298                         }
299                 }
300                 i++;
301         }
302 }
303
304 // queues a phi-node to be added to a basic-block for a specific Alloca
305 // returns true  if there wasn't already a phi-node for that variable
306
307
308 bool PromoteInstance::queuePhiNode(BasicBlock *bb, int i /*the alloca*/)
309 {
310         map<BasicBlock *, vector<PHINode *> >::iterator nd;
311         //look up the basic-block in question
312         nd = new_phinodes.find(bb);
313         //if the basic-block has no phi-nodes added, or at least none
314         //for the i'th alloca. then add.
315         if (nd==new_phinodes.end() || nd->second[i]==NULL)
316         {
317                 //we're not added any phi nodes to this basicblock yet
318                 // create the phi-node array.
319                 if (nd==new_phinodes.end())
320                 {
321                         new_phinodes[bb] = vector<PHINode *>(Allocas.size());
322                         nd = new_phinodes.find(bb);
323                 }
324
325                 //find the type the alloca returns
326                 const PointerType * pt = Allocas[i]->getType();
327                 //create a phi-node using the DEREFERENCED type
328                 PHINode * ph = new PHINode(pt->getElementType(), Allocas[i]->getName()+".mem2reg");
329                 nd->second[i] = ph;
330                 //add the phi-node to the basic-block
331                 bb->getInstList().push_front(ph);
332                 return true;
333         }
334         return false;
335 }