blob: e35dca4de03100e03f4a87e8bf121ca34312a8dc [file] [log] [blame]
Chris Lattner0c7e8e12003-11-06 19:46:29 +00001//===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
Vikram S. Adve83e3b652002-12-10 13:07:58 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerab2b3282003-05-29 15:12:27 +000010// This file provide the function DemoteRegToStack(). This function takes a
11// virtual register computed by an Instruction& X and replaces it with a slot in
12// the stack frame, allocated via alloca. It returns the pointer to the
13// AllocaInst inserted.
14//
Vikram S. Adve83e3b652002-12-10 13:07:58 +000015//===----------------------------------------------------------------------===//
16
17#include "llvm/Transforms/Utils/DemoteRegToStack.h"
18#include "llvm/Function.h"
Vikram S. Adve83e3b652002-12-10 13:07:58 +000019#include "llvm/iMemory.h"
20#include "llvm/iPHINode.h"
21#include "llvm/iTerminators.h"
22#include "llvm/Type.h"
23#include "Support/hash_set"
Vikram S. Adve83e3b652002-12-10 13:07:58 +000024
Brian Gaeked0fde302003-11-11 22:41:34 +000025namespace llvm {
26
Vikram S. Adve83e3b652002-12-10 13:07:58 +000027typedef hash_set<PHINode*> PhiSet;
28typedef hash_set<PHINode*>::iterator PhiSetIterator;
29
30// Helper function to push a phi *and* all its operands to the worklist!
31// Do not push an instruction if it is already in the result set of Phis to go.
Chris Lattner0c7e8e12003-11-06 19:46:29 +000032inline void PushOperandsOnWorkList(std::vector<Instruction*>& workList,
Chris Lattnerab2b3282003-05-29 15:12:27 +000033 PhiSet& phisToGo, PHINode* phiN) {
34 for (User::op_iterator OI = phiN->op_begin(), OE = phiN->op_end();
Chris Lattner0c7e8e12003-11-06 19:46:29 +000035 OI != OE; ++OI) {
36 Instruction* opI = cast<Instruction>(OI);
37 if (!isa<PHINode>(opI) || !phisToGo.count(cast<PHINode>(opI)))
38 workList.push_back(opI);
39 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000040}
41
Chris Lattnerab4536e2003-09-20 14:36:23 +000042static void FindPhis(Instruction& X, PhiSet& phisToGo) {
Chris Lattner0c7e8e12003-11-06 19:46:29 +000043 std::vector<Instruction*> workList;
44 workList.push_back(&X);
Vikram S. Adve83e3b652002-12-10 13:07:58 +000045
46 // Handle the case that X itself is a Phi!
Chris Lattnerab4536e2003-09-20 14:36:23 +000047 if (PHINode* phiX = dyn_cast<PHINode>(&X)) {
48 phisToGo.insert(phiX);
49 PushOperandsOnWorkList(workList, phisToGo, phiX);
50 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000051
52 // Now use a worklist to find all phis reachable from X, and
53 // (recursively) all phis reachable from operands of such phis.
Chris Lattner0c7e8e12003-11-06 19:46:29 +000054 while (!workList.empty()) {
55 Instruction *I = workList.back();
56 workList.pop_back();
57 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E; ++UI)
Chris Lattnerab4536e2003-09-20 14:36:23 +000058 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);
Chris Lattner0c7e8e12003-11-06 19:46:29 +000062 workList.push_back(phiN);
Chris Lattnerab4536e2003-09-20 14:36:23 +000063 PushOperandsOnWorkList(workList, phisToGo, phiN);
64 }
65 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000066}
67
68
Vikram S. Adve83e3b652002-12-10 13:07:58 +000069// Insert loads before all uses of I, except uses in Phis
70// since all such Phis *must* be deleted.
Chris Lattnerab4536e2003-09-20 14:36:23 +000071static void LoadBeforeUses(Instruction* def, AllocaInst* XSlot) {
72 for (unsigned nPhis = 0; def->use_size() - nPhis > 0; ) {
Vikram S. Adve83e3b652002-12-10 13:07:58 +000073 Instruction* useI = cast<Instruction>(def->use_back());
Chris Lattnerab4536e2003-09-20 14:36:23 +000074 if (!isa<PHINode>(useI)) {
75 LoadInst* loadI =
76 new LoadInst(XSlot, std::string("Load")+XSlot->getName(), useI);
77 useI->replaceUsesOfWith(def, loadI);
78 } else
Vikram S. Adve83e3b652002-12-10 13:07:58 +000079 ++nPhis;
Chris Lattnerab4536e2003-09-20 14:36:23 +000080 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000081}
82
Chris Lattnerab4536e2003-09-20 14:36:23 +000083static void AddLoadsAndStores(AllocaInst* XSlot, Instruction& X,
84 PhiSet& phisToGo) {
85 for (PhiSetIterator PI=phisToGo.begin(), PE=phisToGo.end(); PI != PE; ++PI) {
86 PHINode* pn = *PI;
Vikram S. Adve83e3b652002-12-10 13:07:58 +000087
Chris Lattnerab4536e2003-09-20 14:36:23 +000088 // First, insert loads before all uses except uses in Phis.
89 // Do this first because new stores will appear as uses also!
90 LoadBeforeUses(pn, XSlot);
Vikram S. Adve83e3b652002-12-10 13:07:58 +000091
Chris Lattnerab4536e2003-09-20 14:36:23 +000092 // For every incoming operand of the Phi, insert a store either
93 // just after the instruction defining the value or just before the
94 // predecessor of the Phi if the value is a formal, not an instruction.
95 //
96 for (unsigned i=0, N=pn->getNumIncomingValues(); i < N; ++i) {
97 Value* phiOp = pn->getIncomingValue(i);
98 if (phiOp != &X &&
Chris Lattner0c7e8e12003-11-06 19:46:29 +000099 (!isa<PHINode>(phiOp) || !phisToGo.count(cast<PHINode>(phiOp)))) {
Chris Lattnerab4536e2003-09-20 14:36:23 +0000100 // This operand is not a phi that will be deleted: need to store.
101 assert(!isa<TerminatorInst>(phiOp));
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000102
Chris Lattnerab4536e2003-09-20 14:36:23 +0000103 Instruction* storeBefore;
104 if (Instruction* I = dyn_cast<Instruction>(phiOp)) {
105 // phiOp is an instruction, store its result right after it.
106 assert(I->getNext() && "Non-terminator without successor?");
107 storeBefore = I->getNext();
108 } else {
109 // If not, it must be a formal: store it at the end of the
110 // predecessor block of the Phi (*not* at function entry!).
111 storeBefore = pn->getIncomingBlock(i)->getTerminator();
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000112 }
Chris Lattnerab4536e2003-09-20 14:36:23 +0000113
114 // Create instr. to store the value of phiOp before `insertBefore'
115 StoreInst* storeI = new StoreInst(phiOp, XSlot, storeBefore);
116 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000117 }
Chris Lattnerab4536e2003-09-20 14:36:23 +0000118 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000119}
120
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000121//----------------------------------------------------------------------------
122// function DemoteRegToStack()
123//
124// This function takes a virtual register computed by an
125// Instruction& X and replaces it with a slot in the stack frame,
126// allocated via alloca. It has to:
127// (1) Identify all Phi operations that have X as an operand and
128// transitively other Phis that use such Phis;
129// (2) Store all values merged with X via Phi operations to the stack slot;
130// (3) Load the value from the stack slot just before any use of X or any
131// of the Phis that were eliminated; and
132// (4) Delete all the Phis, which should all now be dead.
133//
134// Returns the pointer to the alloca inserted to create a stack slot for X.
Chris Lattner0c7e8e12003-11-06 19:46:29 +0000135//
Chris Lattnerab4536e2003-09-20 14:36:23 +0000136AllocaInst* DemoteRegToStack(Instruction& X) {
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000137 if (X.getType() == Type::VoidTy)
Chris Lattner0c7e8e12003-11-06 19:46:29 +0000138 return 0; // nothing to do!
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000139
140 // Find all Phis involving X or recursively using such Phis or Phis
141 // involving operands of such Phis (essentially all Phis in the "web" of X)
142 PhiSet phisToGo;
143 FindPhis(X, phisToGo);
144
145 // Create a stack slot to hold X
Chris Lattner0c7e8e12003-11-06 19:46:29 +0000146 Function* parentFunc = X.getParent()->getParent();
147 AllocaInst *XSlot = new AllocaInst(X.getType(), 0, X.getName(),
148 parentFunc->getEntryBlock().begin());
149
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000150
151 // Insert loads before all uses of X and (*only then*) insert store after X
152 assert(X.getNext() && "Non-terminator (since non-void) with no successor?");
153 LoadBeforeUses(&X, XSlot);
154 StoreInst* storeI = new StoreInst(&X, XSlot, X.getNext());
155
156 // Do the same for all the phis that will be deleted
157 AddLoadsAndStores(XSlot, X, phisToGo);
158
159 // Delete the phis and return the alloca instruction
Chris Lattner0c7e8e12003-11-06 19:46:29 +0000160 for (PhiSetIterator PI = phisToGo.begin(), E = phisToGo.end(); PI != E; ++PI)
161 (*PI)->getParent()->getInstList().erase(*PI);
162
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000163 return XSlot;
164}
Brian Gaeked0fde302003-11-11 22:41:34 +0000165
166} // End llvm namespace