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