blob: ec0b091f94c542959309e1a0bd5a2c6f3540d397 [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"
Chris Lattnerf7703df2004-01-09 06:12:26 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Vikram S. Adve83e3b652002-12-10 13:07:58 +000026typedef hash_set<PHINode*> PhiSet;
27typedef hash_set<PHINode*>::iterator PhiSetIterator;
28
29// Helper function to push a phi *and* all its operands to the worklist!
30// Do not push an instruction if it is already in the result set of Phis to go.
Chris Lattnerf7703df2004-01-09 06:12:26 +000031static inline void PushOperandsOnWorkList(std::vector<Instruction*>& workList,
32 PhiSet& phisToGo, PHINode* phiN) {
Chris Lattnerab2b3282003-05-29 15:12:27 +000033 for (User::op_iterator OI = phiN->op_begin(), OE = phiN->op_end();
Chris Lattner0c7e8e12003-11-06 19:46:29 +000034 OI != OE; ++OI) {
35 Instruction* opI = cast<Instruction>(OI);
36 if (!isa<PHINode>(opI) || !phisToGo.count(cast<PHINode>(opI)))
37 workList.push_back(opI);
38 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000039}
40
Chris Lattnerab4536e2003-09-20 14:36:23 +000041static void FindPhis(Instruction& X, PhiSet& phisToGo) {
Chris Lattner0c7e8e12003-11-06 19:46:29 +000042 std::vector<Instruction*> workList;
43 workList.push_back(&X);
Vikram S. Adve83e3b652002-12-10 13:07:58 +000044
45 // Handle the case that X itself is a Phi!
Chris Lattnerab4536e2003-09-20 14:36:23 +000046 if (PHINode* phiX = dyn_cast<PHINode>(&X)) {
47 phisToGo.insert(phiX);
48 PushOperandsOnWorkList(workList, phisToGo, phiX);
49 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000050
51 // Now use a worklist to find all phis reachable from X, and
52 // (recursively) all phis reachable from operands of such phis.
Chris Lattner0c7e8e12003-11-06 19:46:29 +000053 while (!workList.empty()) {
54 Instruction *I = workList.back();
55 workList.pop_back();
56 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E; ++UI)
Chris Lattnerab4536e2003-09-20 14:36:23 +000057 if (PHINode* phiN = dyn_cast<PHINode>(*UI))
58 if (phisToGo.find(phiN) == phisToGo.end()) {
59 // Seeing this phi for the first time: it must go!
60 phisToGo.insert(phiN);
Chris Lattner0c7e8e12003-11-06 19:46:29 +000061 workList.push_back(phiN);
Chris Lattnerab4536e2003-09-20 14:36:23 +000062 PushOperandsOnWorkList(workList, phisToGo, phiN);
63 }
64 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000065}
66
67
Vikram S. Adve83e3b652002-12-10 13:07:58 +000068// Insert loads before all uses of I, except uses in Phis
69// since all such Phis *must* be deleted.
Chris Lattnerab4536e2003-09-20 14:36:23 +000070static void LoadBeforeUses(Instruction* def, AllocaInst* XSlot) {
71 for (unsigned nPhis = 0; def->use_size() - nPhis > 0; ) {
Vikram S. Adve83e3b652002-12-10 13:07:58 +000072 Instruction* useI = cast<Instruction>(def->use_back());
Chris Lattnerab4536e2003-09-20 14:36:23 +000073 if (!isa<PHINode>(useI)) {
74 LoadInst* loadI =
75 new LoadInst(XSlot, std::string("Load")+XSlot->getName(), useI);
76 useI->replaceUsesOfWith(def, loadI);
77 } else
Vikram S. Adve83e3b652002-12-10 13:07:58 +000078 ++nPhis;
Chris Lattnerab4536e2003-09-20 14:36:23 +000079 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000080}
81
Chris Lattnerab4536e2003-09-20 14:36:23 +000082static void AddLoadsAndStores(AllocaInst* XSlot, Instruction& X,
83 PhiSet& phisToGo) {
84 for (PhiSetIterator PI=phisToGo.begin(), PE=phisToGo.end(); PI != PE; ++PI) {
85 PHINode* pn = *PI;
Vikram S. Adve83e3b652002-12-10 13:07:58 +000086
Chris Lattnerab4536e2003-09-20 14:36:23 +000087 // First, insert loads before all uses except uses in Phis.
88 // Do this first because new stores will appear as uses also!
89 LoadBeforeUses(pn, XSlot);
Vikram S. Adve83e3b652002-12-10 13:07:58 +000090
Chris Lattnerab4536e2003-09-20 14:36:23 +000091 // For every incoming operand of the Phi, insert a store either
92 // just after the instruction defining the value or just before the
93 // predecessor of the Phi if the value is a formal, not an instruction.
94 //
95 for (unsigned i=0, N=pn->getNumIncomingValues(); i < N; ++i) {
96 Value* phiOp = pn->getIncomingValue(i);
97 if (phiOp != &X &&
Chris Lattner0c7e8e12003-11-06 19:46:29 +000098 (!isa<PHINode>(phiOp) || !phisToGo.count(cast<PHINode>(phiOp)))) {
Chris Lattnerab4536e2003-09-20 14:36:23 +000099 // This operand is not a phi that will be deleted: need to store.
100 assert(!isa<TerminatorInst>(phiOp));
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000101
Chris Lattnerab4536e2003-09-20 14:36:23 +0000102 Instruction* storeBefore;
103 if (Instruction* I = dyn_cast<Instruction>(phiOp)) {
104 // phiOp is an instruction, store its result right after it.
105 assert(I->getNext() && "Non-terminator without successor?");
106 storeBefore = I->getNext();
107 } else {
108 // If not, it must be a formal: store it at the end of the
109 // predecessor block of the Phi (*not* at function entry!).
110 storeBefore = pn->getIncomingBlock(i)->getTerminator();
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000111 }
Chris Lattnerab4536e2003-09-20 14:36:23 +0000112
113 // Create instr. to store the value of phiOp before `insertBefore'
114 StoreInst* storeI = new StoreInst(phiOp, XSlot, storeBefore);
115 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000116 }
Chris Lattnerab4536e2003-09-20 14:36:23 +0000117 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000118}
119
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000120//----------------------------------------------------------------------------
121// function DemoteRegToStack()
122//
123// This function takes a virtual register computed by an
124// Instruction& X and replaces it with a slot in the stack frame,
125// allocated via alloca. It has to:
126// (1) Identify all Phi operations that have X as an operand and
127// transitively other Phis that use such Phis;
128// (2) Store all values merged with X via Phi operations to the stack slot;
129// (3) Load the value from the stack slot just before any use of X or any
130// of the Phis that were eliminated; and
131// (4) Delete all the Phis, which should all now be dead.
132//
133// Returns the pointer to the alloca inserted to create a stack slot for X.
Chris Lattner0c7e8e12003-11-06 19:46:29 +0000134//
Chris Lattnerf7703df2004-01-09 06:12:26 +0000135AllocaInst* llvm::DemoteRegToStack(Instruction& X) {
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000136 if (X.getType() == Type::VoidTy)
Chris Lattner0c7e8e12003-11-06 19:46:29 +0000137 return 0; // nothing to do!
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000138
139 // Find all Phis involving X or recursively using such Phis or Phis
140 // involving operands of such Phis (essentially all Phis in the "web" of X)
141 PhiSet phisToGo;
142 FindPhis(X, phisToGo);
143
144 // Create a stack slot to hold X
Chris Lattner0c7e8e12003-11-06 19:46:29 +0000145 Function* parentFunc = X.getParent()->getParent();
146 AllocaInst *XSlot = new AllocaInst(X.getType(), 0, X.getName(),
147 parentFunc->getEntryBlock().begin());
148
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000149
150 // Insert loads before all uses of X and (*only then*) insert store after X
151 assert(X.getNext() && "Non-terminator (since non-void) with no successor?");
152 LoadBeforeUses(&X, XSlot);
153 StoreInst* storeI = new StoreInst(&X, XSlot, X.getNext());
154
155 // Do the same for all the phis that will be deleted
156 AddLoadsAndStores(XSlot, X, phisToGo);
157
158 // Delete the phis and return the alloca instruction
Chris Lattner0c7e8e12003-11-06 19:46:29 +0000159 for (PhiSetIterator PI = phisToGo.begin(), E = phisToGo.end(); PI != E; ++PI)
160 (*PI)->getParent()->getInstList().erase(*PI);
161
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000162 return XSlot;
163}