1 //===- PromoteMemoryToRegister.cpp - Convert memory refs to regs ----------===//
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:
7 // %X = alloca int, uint 1 ret int 42
8 // store int 42, int *%X
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.
16 // After this, the code is transformed by...something magical :)
18 //===----------------------------------------------------------------------===//
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"
33 using cfg::DominanceFrontier;
36 //instance of the promoter -- to keep all the local method data.
37 // gets re-created for each method processed
41 vector<AllocaInst*> Allocas; // the alloca instruction..
42 map<Instruction *, int> AllocaLookup; //reverse mapping of above
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
48 //list of instructions to remove at end of pass :)
49 vector<Instruction *> killlist;
51 set<BasicBlock *> visited; //the basic blocks we've already visited
52 map<BasicBlock *, vector<PHINode *> > new_phinodes; //the phinodes we're adding
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);
61 // I do this so that I can force the deconstruction of the local variables
62 PromoteInstance(Method *M, DominanceFrontier & DF)
64 didchange=PromoteMethod(M, DF);
66 //This returns whether the pass changes anything
67 operator bool () { return didchange; }
70 class PromotePass : public MethodPass {
73 // runOnMethod - To run this pass, first we calculate the alloca instructions
74 // that are safe for promotion, then we promote each one.
76 virtual bool runOnMethod(Method *M)
78 PromoteInstance inst(M, getAnalysis<DominanceFrontier>());
83 // getAnalysisUsageInfo - We need dominance frontiers
85 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
86 Pass::AnalysisSet &Destroyed,
87 Pass::AnalysisSet &Provided) {
88 Requires.push_back(DominanceFrontier::ID);
93 // findSafeAllocas - Find allocas that are safe to promote
95 void PromoteInstance::findSafeAllocas(Method *M)
97 BasicBlock *BB = M->front(); // Get the entry node for the method
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()) {
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
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;
117 isSafe = false; break; // Not a load or store?
120 if (isSafe) // If all checks pass, add alloca to safe list
122 AllocaLookup[AI]=Allocas.size();
123 Allocas.push_back(AI);
130 // newPromoteMemoryToRegister - Provide an entry point to create this pass.
132 Pass *newPromoteMemoryToRegister() {
133 return new PromotePass();
137 bool PromoteInstance::PromoteMethod(Method *M, DominanceFrontier & DF) {
138 // Calculate the set of safe allocas
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());
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)
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)
153 if (MemAccessInst *MAI = dyn_cast<StoreInst>(*U)) {
154 WriteSets[i].push_back(MAI->getParent()); // jot down the basic-block it came from
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)
164 for (unsigned j = 0; j<WriteSets[i].size(); j++)
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)
171 if (queuePhiNode((BasicBlock *)*p, i))
172 PhiNodes[i].push_back((BasicBlock *)*p);
175 // perform iterative step
176 for (unsigned k = 0; k<PhiNodes[i].size(); k++)
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)
182 if (queuePhiNode((BasicBlock *)*p,i))
183 PhiNodes[i].push_back((BasicBlock*)*p);
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
193 CurrentValue.push_back(vector<Value *>(Allocas.size()));
195 traverse(f, NULL); // there is no predecessor of the root node
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)
203 Instruction * r = *i;
204 BasicBlock * o = r->getParent();
207 BasicBlock::InstListType & l = o->getInstList();
208 o->getInstList().remove(r);
212 return !Allocas.empty();
217 void PromoteInstance::traverse(BasicBlock *f, BasicBlock * predecessor)
219 vector<Value *> * tos = &CurrentValue.back(); //look at top-
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())
226 for (unsigned k = 0; k!=nd->second.size(); ++k)
229 //at this point we can assume that the array has phi nodes.. let's
230 // add the incoming data
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];
238 //don't revisit nodes
239 if (visited.find(f)!=visited.end())
244 BasicBlock::iterator i = f->begin();
245 //keep track of the value of each variable we're watching.. how?
248 Instruction * inst = *i; //get the instruction
249 //is this a write/read?
250 if (LoadInst * LI = dyn_cast<LoadInst>(inst))
252 // This is a bit weird...
253 Value * ptr = LI->getPointerOperand(); //of type value
254 if (AllocaInst * srcinstr = dyn_cast<AllocaInst>(ptr))
256 map<Instruction *, int>::iterator ai = AllocaLookup.find(srcinstr);
257 if (ai!=AllocaLookup.end())
259 if (Value *r = (*tos)[ai->second])
261 //walk the use list of this load and replace
263 LI->replaceAllUsesWith(r);
264 //now delete the instruction.. somehow..
265 killlist.push_back((Instruction *)LI);
270 else if (StoreInst * SI = dyn_cast<StoreInst>(inst))
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))
277 map<Instruction *, int>::iterator ai = AllocaLookup.find(srcinstr);
278 if (ai!=AllocaLookup.end())
280 //what value were we writing?
281 Value * writeval = SI->getOperand(0);
283 (*tos)[ai->second] = writeval;
284 //now delete it.. somehow?
285 killlist.push_back((Instruction *)SI);
290 else if (TerminatorInst * TI = dyn_cast<TerminatorInst>(inst))
292 // Recurse across our sucessors
293 for (unsigned i = 0; i!=TI->getNumSuccessors(); i++)
295 CurrentValue.push_back(CurrentValue.back());
296 traverse(TI->getSuccessor(i),f); //this node IS the predecessor
297 CurrentValue.pop_back();
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
308 bool PromoteInstance::queuePhiNode(BasicBlock *bb, int i /*the alloca*/)
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)
317 //we're not added any phi nodes to this basicblock yet
318 // create the phi-node array.
319 if (nd==new_phinodes.end())
321 new_phinodes[bb] = vector<PHINode *>(Allocas.size());
322 nd = new_phinodes.find(bb);
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");
330 //add the phi-node to the basic-block
331 bb->getInstList().push_front(ph);