blob: 044cd1610aa3f1c8bdd4da1b551a1053445098f4 [file] [log] [blame]
Chris Lattner4413e432003-09-20 14:36:23 +00001//===- DemoteRegToStack.cpp - Move a virtual reg. to stack ----------------===//
Vikram S. Advec864ab12002-12-10 13:07:58 +00002//
Chris Lattnerf789f292003-05-29 15:12:27 +00003// 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.
7//
Vikram S. Advec864ab12002-12-10 13:07:58 +00008//===----------------------------------------------------------------------===//
9
10#include "llvm/Transforms/Utils/DemoteRegToStack.h"
11#include "llvm/Function.h"
Vikram S. Advec864ab12002-12-10 13:07:58 +000012#include "llvm/iMemory.h"
13#include "llvm/iPHINode.h"
14#include "llvm/iTerminators.h"
15#include "llvm/Type.h"
16#include "Support/hash_set"
17#include <stack>
18
Vikram S. Advec864ab12002-12-10 13:07:58 +000019typedef hash_set<PHINode*> PhiSet;
20typedef hash_set<PHINode*>::iterator PhiSetIterator;
21
22// Helper function to push a phi *and* all its operands to the worklist!
23// Do not push an instruction if it is already in the result set of Phis to go.
24inline void PushOperandsOnWorkList(std::stack<Instruction*>& workList,
Chris Lattnerf789f292003-05-29 15:12:27 +000025 PhiSet& phisToGo, PHINode* phiN) {
26 for (User::op_iterator OI = phiN->op_begin(), OE = phiN->op_end();
Vikram S. Advec864ab12002-12-10 13:07:58 +000027 OI != OE; ++OI)
Chris Lattnerf789f292003-05-29 15:12:27 +000028 if (Instruction* opI = dyn_cast<Instruction>(OI))
Vikram S. Advec864ab12002-12-10 13:07:58 +000029 if (!isa<PHINode>(opI) ||
30 phisToGo.find(cast<PHINode>(opI)) == phisToGo.end())
31 workList.push(opI);
32}
33
Chris Lattner4413e432003-09-20 14:36:23 +000034static void FindPhis(Instruction& X, PhiSet& phisToGo) {
Vikram S. Advec864ab12002-12-10 13:07:58 +000035 std::stack<Instruction*> workList;
36 workList.push(&X);
37
38 // Handle the case that X itself is a Phi!
Chris Lattner4413e432003-09-20 14:36:23 +000039 if (PHINode* phiX = dyn_cast<PHINode>(&X)) {
40 phisToGo.insert(phiX);
41 PushOperandsOnWorkList(workList, phisToGo, phiX);
42 }
Vikram S. Advec864ab12002-12-10 13:07:58 +000043
44 // Now use a worklist to find all phis reachable from X, and
45 // (recursively) all phis reachable from operands of such phis.
Chris Lattner4413e432003-09-20 14:36:23 +000046 for (Instruction* workI; !workList.empty(); workList.pop()) {
47 workI = workList.top();
48 for (Value::use_iterator UI=workI->use_begin(), UE=workI->use_end();
49 UI != UE; ++UI)
50 if (PHINode* phiN = dyn_cast<PHINode>(*UI))
51 if (phisToGo.find(phiN) == phisToGo.end()) {
52 // Seeing this phi for the first time: it must go!
53 phisToGo.insert(phiN);
54 workList.push(phiN);
55 PushOperandsOnWorkList(workList, phisToGo, phiN);
56 }
57 }
Vikram S. Advec864ab12002-12-10 13:07:58 +000058}
59
60
61// Create the Alloca for X
Chris Lattner4413e432003-09-20 14:36:23 +000062static AllocaInst* CreateAllocaForX(Instruction& X) {
Vikram S. Advec864ab12002-12-10 13:07:58 +000063 Function* parentFunc = X.getParent()->getParent();
Chris Lattner4413e432003-09-20 14:36:23 +000064 Instruction* entryInst = parentFunc->getEntryBlock().begin();
Vikram S. Advec864ab12002-12-10 13:07:58 +000065 return new AllocaInst(X.getType(), /*arraySize*/ NULL,
66 X.hasName()? X.getName()+std::string("OnStack")
67 : "DemotedTmp",
68 entryInst);
69}
70
71// Insert loads before all uses of I, except uses in Phis
72// since all such Phis *must* be deleted.
Chris Lattner4413e432003-09-20 14:36:23 +000073static void LoadBeforeUses(Instruction* def, AllocaInst* XSlot) {
74 for (unsigned nPhis = 0; def->use_size() - nPhis > 0; ) {
Vikram S. Advec864ab12002-12-10 13:07:58 +000075 Instruction* useI = cast<Instruction>(def->use_back());
Chris Lattner4413e432003-09-20 14:36:23 +000076 if (!isa<PHINode>(useI)) {
77 LoadInst* loadI =
78 new LoadInst(XSlot, std::string("Load")+XSlot->getName(), useI);
79 useI->replaceUsesOfWith(def, loadI);
80 } else
Vikram S. Advec864ab12002-12-10 13:07:58 +000081 ++nPhis;
Chris Lattner4413e432003-09-20 14:36:23 +000082 }
Vikram S. Advec864ab12002-12-10 13:07:58 +000083}
84
Chris Lattner4413e432003-09-20 14:36:23 +000085static void AddLoadsAndStores(AllocaInst* XSlot, Instruction& X,
86 PhiSet& phisToGo) {
87 for (PhiSetIterator PI=phisToGo.begin(), PE=phisToGo.end(); PI != PE; ++PI) {
88 PHINode* pn = *PI;
Vikram S. Advec864ab12002-12-10 13:07:58 +000089
Chris Lattner4413e432003-09-20 14:36:23 +000090 // First, insert loads before all uses except uses in Phis.
91 // Do this first because new stores will appear as uses also!
92 LoadBeforeUses(pn, XSlot);
Vikram S. Advec864ab12002-12-10 13:07:58 +000093
Chris Lattner4413e432003-09-20 14:36:23 +000094 // For every incoming operand of the Phi, insert a store either
95 // just after the instruction defining the value or just before the
96 // predecessor of the Phi if the value is a formal, not an instruction.
97 //
98 for (unsigned i=0, N=pn->getNumIncomingValues(); i < N; ++i) {
99 Value* phiOp = pn->getIncomingValue(i);
100 if (phiOp != &X &&
101 (!isa<PHINode>(phiOp) ||
102 phisToGo.find(cast<PHINode>(phiOp)) == phisToGo.end())) {
103 // This operand is not a phi that will be deleted: need to store.
104 assert(!isa<TerminatorInst>(phiOp));
Vikram S. Advec864ab12002-12-10 13:07:58 +0000105
Chris Lattner4413e432003-09-20 14:36:23 +0000106 Instruction* storeBefore;
107 if (Instruction* I = dyn_cast<Instruction>(phiOp)) {
108 // phiOp is an instruction, store its result right after it.
109 assert(I->getNext() && "Non-terminator without successor?");
110 storeBefore = I->getNext();
111 } else {
112 // If not, it must be a formal: store it at the end of the
113 // predecessor block of the Phi (*not* at function entry!).
114 storeBefore = pn->getIncomingBlock(i)->getTerminator();
Vikram S. Advec864ab12002-12-10 13:07:58 +0000115 }
Chris Lattner4413e432003-09-20 14:36:23 +0000116
117 // Create instr. to store the value of phiOp before `insertBefore'
118 StoreInst* storeI = new StoreInst(phiOp, XSlot, storeBefore);
119 }
Vikram S. Advec864ab12002-12-10 13:07:58 +0000120 }
Chris Lattner4413e432003-09-20 14:36:23 +0000121 }
Vikram S. Advec864ab12002-12-10 13:07:58 +0000122}
123
Chris Lattner4413e432003-09-20 14:36:23 +0000124static void DeletePhis(PhiSet& phisToGo) {
125 for (PhiSetIterator PI=phisToGo.begin(), PE=phisToGo.end(); PI != PE; ++PI) {
126 assert((*PI)->use_size() == 0 && "This PHI should be DEAD!");
127 (*PI)->getParent()->getInstList().remove(*PI);
128 delete *PI;
129 }
Vikram S. Advec864ab12002-12-10 13:07:58 +0000130 phisToGo.clear();
131}
132
133//----------------------------------------------------------------------------
134// function DemoteRegToStack()
135//
136// This function takes a virtual register computed by an
137// Instruction& X and replaces it with a slot in the stack frame,
138// allocated via alloca. It has to:
139// (1) Identify all Phi operations that have X as an operand and
140// transitively other Phis that use such Phis;
141// (2) Store all values merged with X via Phi operations to the stack slot;
142// (3) Load the value from the stack slot just before any use of X or any
143// of the Phis that were eliminated; and
144// (4) Delete all the Phis, which should all now be dead.
145//
146// Returns the pointer to the alloca inserted to create a stack slot for X.
147//----------------------------------------------------------------------------
148
Chris Lattner4413e432003-09-20 14:36:23 +0000149AllocaInst* DemoteRegToStack(Instruction& X) {
Vikram S. Advec864ab12002-12-10 13:07:58 +0000150 if (X.getType() == Type::VoidTy)
151 return NULL; // nothing to do!
152
153 // Find all Phis involving X or recursively using such Phis or Phis
154 // involving operands of such Phis (essentially all Phis in the "web" of X)
155 PhiSet phisToGo;
156 FindPhis(X, phisToGo);
157
158 // Create a stack slot to hold X
159 AllocaInst* XSlot = CreateAllocaForX(X);
160
161 // Insert loads before all uses of X and (*only then*) insert store after X
162 assert(X.getNext() && "Non-terminator (since non-void) with no successor?");
163 LoadBeforeUses(&X, XSlot);
164 StoreInst* storeI = new StoreInst(&X, XSlot, X.getNext());
165
166 // Do the same for all the phis that will be deleted
167 AddLoadsAndStores(XSlot, X, phisToGo);
168
169 // Delete the phis and return the alloca instruction
170 DeletePhis(phisToGo);
171 return XSlot;
172}