1 //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // 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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Utils/DemoteRegToStack.h"
18 #include "llvm/Function.h"
19 #include "llvm/iMemory.h"
20 #include "llvm/iPHINode.h"
21 #include "llvm/iTerminators.h"
22 #include "llvm/Type.h"
23 #include "Support/hash_set"
26 typedef hash_set<PHINode*> PhiSet;
27 typedef hash_set<PHINode*>::iterator PhiSetIterator;
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.
31 static inline void PushOperandsOnWorkList(std::vector<Instruction*>& workList,
32 PhiSet& phisToGo, PHINode* phiN) {
33 for (User::op_iterator OI = phiN->op_begin(), OE = phiN->op_end();
35 Instruction* opI = cast<Instruction>(OI);
36 if (!isa<PHINode>(opI) || !phisToGo.count(cast<PHINode>(opI)))
37 workList.push_back(opI);
41 static void FindPhis(Instruction& X, PhiSet& phisToGo) {
42 std::vector<Instruction*> workList;
43 workList.push_back(&X);
45 // Handle the case that X itself is a Phi!
46 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 while (!workList.empty()) {
54 Instruction *I = workList.back();
56 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E; ++UI)
57 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);
61 workList.push_back(phiN);
62 PushOperandsOnWorkList(workList, phisToGo, phiN);
68 // Insert loads before all uses of I, except uses in Phis
69 // since all such Phis *must* be deleted.
70 static void LoadBeforeUses(Instruction* def, AllocaInst* XSlot) {
71 for (unsigned nPhis = 0; def->use_size() - nPhis > 0; ) {
72 Instruction* useI = cast<Instruction>(def->use_back());
73 if (!isa<PHINode>(useI)) {
75 new LoadInst(XSlot, std::string("Load")+XSlot->getName(), useI);
76 useI->replaceUsesOfWith(def, loadI);
82 static void AddLoadsAndStores(AllocaInst* XSlot, Instruction& X,
84 for (PhiSetIterator PI=phisToGo.begin(), PE=phisToGo.end(); PI != PE; ++PI) {
87 // 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);
91 // 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.
95 for (unsigned i=0, N=pn->getNumIncomingValues(); i < N; ++i) {
96 Value* phiOp = pn->getIncomingValue(i);
98 (!isa<PHINode>(phiOp) || !phisToGo.count(cast<PHINode>(phiOp)))) {
99 // This operand is not a phi that will be deleted: need to store.
100 assert(!isa<TerminatorInst>(phiOp));
102 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();
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();
113 // Create instr. to store the value of phiOp before `insertBefore'
114 StoreInst* storeI = new StoreInst(phiOp, XSlot, storeBefore);
120 //----------------------------------------------------------------------------
121 // function DemoteRegToStack()
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.
133 // Returns the pointer to the alloca inserted to create a stack slot for X.
135 AllocaInst* llvm::DemoteRegToStack(Instruction& X) {
136 if (X.getType() == Type::VoidTy)
137 return 0; // nothing to do!
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)
142 FindPhis(X, phisToGo);
144 // Create a stack slot to hold X
145 Function* parentFunc = X.getParent()->getParent();
146 AllocaInst *XSlot = new AllocaInst(X.getType(), 0, X.getName(),
147 parentFunc->getEntryBlock().begin());
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());
155 // Do the same for all the phis that will be deleted
156 AddLoadsAndStores(XSlot, X, phisToGo);
158 // Delete the phis and return the alloca instruction
159 for (PhiSetIterator PI = phisToGo.begin(), E = phisToGo.end(); PI != E; ++PI)
160 (*PI)->getParent()->getInstList().erase(*PI);