1 //===- DemoteRegToStack.cpp - Move a virtual reg. to stack ------*- C++ -*-===//
3 // This file provide the function DemoteRegToStack(). This function takes a
4 // virtual register computed by an Instruction& X and replaces it with a slot in
5 // the stack frame, allocated via alloca. It returns the pointer to the
6 // AllocaInst inserted.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Transforms/Utils/DemoteRegToStack.h"
11 #include "llvm/Function.h"
12 #include "llvm/iMemory.h"
13 #include "llvm/iPHINode.h"
14 #include "llvm/iTerminators.h"
15 #include "llvm/Type.h"
16 #include "Support/hash_set"
19 //----------------------------------------------------------------------------
20 // function DemoteRegToStack()
22 //----------------------------------------------------------------------------
24 typedef hash_set<PHINode*> PhiSet;
25 typedef hash_set<PHINode*>::iterator PhiSetIterator;
27 // Helper function to push a phi *and* all its operands to the worklist!
28 // Do not push an instruction if it is already in the result set of Phis to go.
29 inline void PushOperandsOnWorkList(std::stack<Instruction*>& workList,
30 PhiSet& phisToGo, PHINode* phiN) {
31 for (User::op_iterator OI = phiN->op_begin(), OE = phiN->op_end();
33 if (Instruction* opI = dyn_cast<Instruction>(OI))
34 if (!isa<PHINode>(opI) ||
35 phisToGo.find(cast<PHINode>(opI)) == phisToGo.end())
39 void FindPhis(Instruction& X, PhiSet& phisToGo)
41 std::stack<Instruction*> workList;
44 // Handle the case that X itself is a Phi!
45 if (PHINode* phiX = dyn_cast<PHINode>(&X))
47 phisToGo.insert(phiX);
48 PushOperandsOnWorkList(workList, phisToGo, phiX);
51 // Now use a worklist to find all phis reachable from X, and
52 // (recursively) all phis reachable from operands of such phis.
53 for (Instruction* workI; !workList.empty(); workList.pop())
55 workI = workList.top();
56 for (Value::use_iterator UI=workI->use_begin(), UE=workI->use_end();
58 if (PHINode* phiN = dyn_cast<PHINode>(*UI))
59 if (phisToGo.find(phiN) == phisToGo.end())
60 { // Seeing this phi for the first time: it must go!
61 phisToGo.insert(phiN);
63 PushOperandsOnWorkList(workList, phisToGo, phiN);
69 // Create the Alloca for X
70 AllocaInst* CreateAllocaForX(Instruction& X)
72 Function* parentFunc = X.getParent()->getParent();
73 Instruction* entryInst = parentFunc->getEntryNode().begin();
74 return new AllocaInst(X.getType(), /*arraySize*/ NULL,
75 X.hasName()? X.getName()+std::string("OnStack")
80 // Insert loads before all uses of I, except uses in Phis
81 // since all such Phis *must* be deleted.
82 void LoadBeforeUses(Instruction* def, AllocaInst* XSlot)
84 for (unsigned nPhis = 0; def->use_size() - nPhis > 0; )
86 Instruction* useI = cast<Instruction>(def->use_back());
87 if (!isa<PHINode>(useI))
90 new LoadInst(XSlot, std::string("Load")+XSlot->getName(), useI);
91 useI->replaceUsesOfWith(def, loadI);
98 void AddLoadsAndStores(AllocaInst* XSlot, Instruction& X, PhiSet& phisToGo)
100 for (PhiSetIterator PI=phisToGo.begin(), PE=phisToGo.end(); PI != PE; ++PI)
104 // First, insert loads before all uses except uses in Phis.
105 // Do this first because new stores will appear as uses also!
106 LoadBeforeUses(pn, XSlot);
108 // For every incoming operand of the Phi, insert a store either
109 // just after the instruction defining the value or just before the
110 // predecessor of the Phi if the value is a formal, not an instruction.
112 for (unsigned i=0, N=pn->getNumIncomingValues(); i < N; ++i)
114 Value* phiOp = pn->getIncomingValue(i);
116 (!isa<PHINode>(phiOp) ||
117 phisToGo.find(cast<PHINode>(phiOp)) == phisToGo.end()))
118 { // This operand is not a phi that will be deleted: need to store.
119 assert(!isa<TerminatorInst>(phiOp));
121 Instruction* storeBefore;
122 if (Instruction* I = dyn_cast<Instruction>(phiOp))
123 { // phiOp is an instruction, store its result right after it.
124 assert(I->getNext() && "Non-terminator without successor?");
125 storeBefore = I->getNext();
128 { // If not, it must be a formal: store it at the end of the
129 // predecessor block of the Phi (*not* at function entry!).
130 storeBefore = pn->getIncomingBlock(i)->getTerminator();
133 // Create instr. to store the value of phiOp before `insertBefore'
134 StoreInst* storeI = new StoreInst(phiOp, XSlot, storeBefore);
140 void DeletePhis(PhiSet& phisToGo)
142 for (PhiSetIterator PI=phisToGo.begin(), PE=phisToGo.end(); PI != PE; ++PI)
144 assert((*PI)->use_size() == 0 && "This PHI should be DEAD!");
145 (*PI)->getParent()->getInstList().remove(*PI);
151 //----------------------------------------------------------------------------
152 // function DemoteRegToStack()
154 // This function takes a virtual register computed by an
155 // Instruction& X and replaces it with a slot in the stack frame,
156 // allocated via alloca. It has to:
157 // (1) Identify all Phi operations that have X as an operand and
158 // transitively other Phis that use such Phis;
159 // (2) Store all values merged with X via Phi operations to the stack slot;
160 // (3) Load the value from the stack slot just before any use of X or any
161 // of the Phis that were eliminated; and
162 // (4) Delete all the Phis, which should all now be dead.
164 // Returns the pointer to the alloca inserted to create a stack slot for X.
165 //----------------------------------------------------------------------------
167 AllocaInst* DemoteRegToStack(Instruction& X)
169 if (X.getType() == Type::VoidTy)
170 return NULL; // nothing to do!
172 // Find all Phis involving X or recursively using such Phis or Phis
173 // involving operands of such Phis (essentially all Phis in the "web" of X)
175 FindPhis(X, phisToGo);
177 // Create a stack slot to hold X
178 AllocaInst* XSlot = CreateAllocaForX(X);
180 // Insert loads before all uses of X and (*only then*) insert store after X
181 assert(X.getNext() && "Non-terminator (since non-void) with no successor?");
182 LoadBeforeUses(&X, XSlot);
183 StoreInst* storeI = new StoreInst(&X, XSlot, X.getNext());
185 // Do the same for all the phis that will be deleted
186 AddLoadsAndStores(XSlot, X, phisToGo);
188 // Delete the phis and return the alloca instruction
189 DeletePhis(phisToGo);