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