23dd2865a06120dbf64cb77da08b9868873de3d0
[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/iPHINode.h"
24 #include "llvm/iTerminators.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Function.h"
27 #include "llvm/BasicBlock.h"
28 #include "llvm/ConstantVals.h"
29
30 using namespace std;
31
32 namespace {
33
34 // instance of the promoter -- to keep all the local function data.
35 // gets re-created for each function processed
36 class PromoteInstance {
37 protected:
38   vector<AllocaInst*>          Allocas;      // the alloca instruction..
39   map<Instruction*, unsigned>  AllocaLookup; // reverse mapping of above
40   
41   vector<vector<BasicBlock*> > WriteSets;    // index corresponds to Allocas
42   vector<vector<BasicBlock*> > PhiNodes;     // index corresponds to Allocas
43   vector<vector<Value*> >      CurrentValue; // the current value stack
44   
45   //list of instructions to remove at end of pass :)
46   vector<Instruction *>        KillList;
47
48   set<BasicBlock*> visited;       // the basic blocks we've already visited
49   map<BasicBlock*, vector<PHINode*> > NewPhiNodes; // the phinodes we're adding
50
51   void traverse(BasicBlock *f, BasicBlock * predecessor);
52   bool PromoteFunction(Function *F, DominanceFrontier &DF);
53   bool QueuePhiNode(BasicBlock *bb, unsigned alloca_index);
54   void findSafeAllocas(Function *M);
55   bool didchange;
56 public:
57   // I do this so that I can force the deconstruction of the local variables
58   PromoteInstance(Function *F, DominanceFrontier &DF) {
59     didchange = PromoteFunction(F, DF);
60   }
61   //This returns whether the pass changes anything
62   operator bool () { return didchange; }
63 };
64
65 }  // end of anonymous namespace
66
67
68
69 // findSafeAllocas - Find allocas that are safe to promote
70 //
71 void PromoteInstance::findSafeAllocas(Function *F) {
72   BasicBlock *BB = F->getEntryNode();  // Get the entry node for the function
73
74   // Look at all instructions in the entry node
75   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
76     if (AllocaInst *AI = dyn_cast<AllocaInst>(*I))       // Is it an alloca?
77       if (!AI->isArrayAllocation()) {
78         bool isSafe = true;
79         for (Value::use_iterator UI = AI->use_begin(), UE = AI->use_end();
80              UI != UE; ++UI) {   // Loop over all of the uses of the alloca
81
82           // Only allow nonindexed memory access instructions...
83           if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*UI)) {
84             if (MAI->hasIndices()) {  // indexed?
85               // Allow the access if there is only one index and the index is
86               // zero.
87               if (*MAI->idx_begin() != ConstantUInt::get(Type::UIntTy, 0) ||
88                   MAI->idx_begin()+1 != MAI->idx_end()) {
89                 isSafe = false;
90                 break;
91               }
92             }
93           } else {
94             isSafe = false; break;   // Not a load or store?
95           }
96         }
97         if (isSafe) {            // If all checks pass, add alloca to safe list
98           AllocaLookup[AI] = Allocas.size();
99           Allocas.push_back(AI);
100         }
101       }
102 }
103
104
105
106 bool PromoteInstance::PromoteFunction(Function *F, DominanceFrontier &DF) {
107   // Calculate the set of safe allocas
108   findSafeAllocas(F);
109
110   // Add each alloca to the KillList.  Note: KillList is destroyed MOST recently
111   // added to least recently.
112   KillList.assign(Allocas.begin(), Allocas.end());
113
114   // Calculate the set of write-locations for each alloca.  This is analogous to
115   // counting the number of 'redefinitions' of each variable.
116   WriteSets.resize(Allocas.size());
117   for (unsigned i = 0; i != Allocas.size(); ++i) {
118     AllocaInst *AI = Allocas[i];
119     for (Value::use_iterator U =AI->use_begin(), E = AI->use_end(); U != E; ++U)
120       if (StoreInst *SI = dyn_cast<StoreInst>(*U))
121         // jot down the basic-block it came from
122         WriteSets[i].push_back(SI->getParent());
123   }
124
125   // Compute the locations where PhiNodes need to be inserted.  Look at the
126   // dominance frontier of EACH basic-block we have a write in
127   //
128   PhiNodes.resize(Allocas.size());
129   for (unsigned i = 0; i != Allocas.size(); ++i) {
130     for (unsigned j = 0; j != WriteSets[i].size(); j++) {
131       // Look up the DF for this write, add it to PhiNodes
132       DominanceFrontier::const_iterator it = DF.find(WriteSets[i][j]);
133       DominanceFrontier::DomSetType     S = it->second;
134       for (DominanceFrontier::DomSetType::iterator P = S.begin(), PE = S.end();
135            P != PE; ++P)
136         QueuePhiNode(*P, i);
137     }
138     
139     // Perform iterative step
140     for (unsigned k = 0; k != PhiNodes[i].size(); k++) {
141       DominanceFrontier::const_iterator it = DF.find(PhiNodes[i][k]);
142       DominanceFrontier::DomSetType     S = it->second;
143       for (DominanceFrontier::DomSetType::iterator P = S.begin(), PE = S.end();
144            P != PE; ++P)
145         QueuePhiNode(*P, i);
146     }
147   }
148
149   // Set the incoming values for the basic block to be null values for all of
150   // the alloca's.  We do this in case there is a load of a value that has not
151   // been stored yet.  In this case, it will get this null value.
152   //
153   CurrentValue.push_back(vector<Value *>(Allocas.size()));
154   for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
155     CurrentValue[0][i] =
156       Constant::getNullValue(Allocas[i]->getType()->getElementType());
157
158   // Walks all basic blocks in the function performing the SSA rename algorithm
159   // and inserting the phi nodes we marked as necessary
160   //
161   traverse(F->front(), 0);  // there is no predecessor of the root node
162
163   // Remove all instructions marked by being placed in the KillList...
164   //
165   while (!KillList.empty()) {
166     Instruction *I = KillList.back();
167     KillList.pop_back();
168
169     //now go find..
170     I->getParent()->getInstList().remove(I);
171     delete I;
172   }
173
174   return !Allocas.empty();
175 }
176
177
178 // QueuePhiNode - queues a phi-node to be added to a basic-block for a specific
179 // Alloca returns true if there wasn't already a phi-node for that variable
180 //
181 bool PromoteInstance::QueuePhiNode(BasicBlock *BB, unsigned i /*the alloca*/) {
182   // Look up the basic-block in question
183   vector<PHINode*> &BBPNs = NewPhiNodes[BB];
184   if (BBPNs.empty()) BBPNs.resize(Allocas.size());
185
186   // If the BB already has a phi node added for the i'th alloca then we're done!
187   if (BBPNs[i]) return false;
188
189   // Create a phi-node using the dereferenced type...
190   PHINode *PN = new PHINode(Allocas[i]->getType()->getElementType(),
191                             Allocas[i]->getName()+".mem2reg");
192   BBPNs[i] = PN;
193
194   // Add the phi-node to the basic-block
195   BB->getInstList().push_front(PN);
196
197   PhiNodes[i].push_back(BB);
198   return true;
199 }
200
201 void PromoteInstance::traverse(BasicBlock *BB, BasicBlock *Pred) {
202   vector<Value *> &TOS = CurrentValue.back(); // look at top
203
204   // If this is a BB needing a phi node, lookup/create the phinode for each
205   // variable we need phinodes for.
206   vector<PHINode *> &BBPNs = NewPhiNodes[BB];
207   for (unsigned k = 0; k != BBPNs.size(); ++k)
208     if (PHINode *PN = BBPNs[k]) {
209       // at this point we can assume that the array has phi nodes.. let's add
210       // the incoming data
211       PN->addIncoming(TOS[k], Pred);
212
213       // also note that the active variable IS designated by the phi node
214       TOS[k] = PN;
215     }
216
217   // don't revisit nodes
218   if (visited.count(BB)) return;
219   
220   // mark as visited
221   visited.insert(BB);
222
223   // keep track of the value of each variable we're watching.. how?
224   for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) {
225     Instruction *I = *II; //get the instruction
226
227     if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
228       Value *Ptr = LI->getPointerOperand();
229
230       if (AllocaInst *Src = dyn_cast<AllocaInst>(Ptr)) {
231         map<Instruction*, unsigned>::iterator ai = AllocaLookup.find(Src);
232         if (ai != AllocaLookup.end()) {
233           Value *V = TOS[ai->second];
234
235           // walk the use list of this load and replace all uses with r
236           LI->replaceAllUsesWith(V);
237           KillList.push_back(LI); // Mark the load to be deleted
238         }
239       }
240     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
241       // delete this instruction and mark the name as the current holder of the
242       // value
243       Value *Ptr = SI->getPointerOperand();
244       if (AllocaInst *Dest = dyn_cast<AllocaInst>(Ptr)) {
245         map<Instruction *, unsigned>::iterator ai = AllocaLookup.find(Dest);
246         if (ai != AllocaLookup.end()) {
247           // what value were we writing?
248           TOS[ai->second] = SI->getOperand(0);
249           KillList.push_back(SI);  // Mark the store to be deleted
250         }
251       }
252       
253     } else if (TerminatorInst *TI = dyn_cast<TerminatorInst>(I)) {
254       // Recurse across our successors
255       for (unsigned i = 0; i != TI->getNumSuccessors(); i++) {
256         CurrentValue.push_back(CurrentValue.back());
257         traverse(TI->getSuccessor(i), BB); // This node becomes the predecessor
258         CurrentValue.pop_back();
259       }
260     }
261   }
262 }
263
264
265 namespace {
266   struct PromotePass : public FunctionPass {
267
268     // runOnFunction - To run this pass, first we calculate the alloca
269     // instructions that are safe for promotion, then we promote each one.
270     //
271     virtual bool runOnFunction(Function *F) {
272       return (bool)PromoteInstance(F, getAnalysis<DominanceFrontier>());
273     }
274
275     // getAnalysisUsage - We need dominance frontiers
276     //
277     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
278       AU.addRequired(DominanceFrontier::ID);
279     }
280   };
281 }
282   
283
284 // createPromoteMemoryToRegister - Provide an entry point to create this pass.
285 //
286 Pass *createPromoteMemoryToRegister() {
287   return new PromotePass();
288 }