Chris Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 1 | //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===// |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // 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 Lattner | ab2b328 | 2003-05-29 15:12:27 +0000 | [diff] [blame] | 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. |
| 14 | // |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/Transforms/Utils/DemoteRegToStack.h" |
| 18 | #include "llvm/Function.h" |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 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" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 26 | typedef hash_set<PHINode*> PhiSet; |
| 27 | typedef 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 Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 31 | static inline void PushOperandsOnWorkList(std::vector<Instruction*>& workList, |
| 32 | PhiSet& phisToGo, PHINode* phiN) { |
Chris Lattner | ab2b328 | 2003-05-29 15:12:27 +0000 | [diff] [blame] | 33 | for (User::op_iterator OI = phiN->op_begin(), OE = phiN->op_end(); |
Chris Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 34 | 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. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 41 | static void FindPhis(Instruction& X, PhiSet& phisToGo) { |
Chris Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 42 | std::vector<Instruction*> workList; |
| 43 | workList.push_back(&X); |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 44 | |
| 45 | // Handle the case that X itself is a Phi! |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 46 | if (PHINode* phiX = dyn_cast<PHINode>(&X)) { |
| 47 | phisToGo.insert(phiX); |
| 48 | PushOperandsOnWorkList(workList, phisToGo, phiX); |
| 49 | } |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 50 | |
| 51 | // Now use a worklist to find all phis reachable from X, and |
| 52 | // (recursively) all phis reachable from operands of such phis. |
Chris Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 53 | 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 Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 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); |
Chris Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 61 | workList.push_back(phiN); |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 62 | PushOperandsOnWorkList(workList, phisToGo, phiN); |
| 63 | } |
| 64 | } |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 68 | // Insert loads before all uses of I, except uses in Phis |
| 69 | // since all such Phis *must* be deleted. |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 70 | static void LoadBeforeUses(Instruction* def, AllocaInst* XSlot) { |
| 71 | for (unsigned nPhis = 0; def->use_size() - nPhis > 0; ) { |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 72 | Instruction* useI = cast<Instruction>(def->use_back()); |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 73 | 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. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 78 | ++nPhis; |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 79 | } |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 82 | static 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. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 86 | |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 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); |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 90 | |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 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. |
| 94 | // |
| 95 | for (unsigned i=0, N=pn->getNumIncomingValues(); i < N; ++i) { |
| 96 | Value* phiOp = pn->getIncomingValue(i); |
| 97 | if (phiOp != &X && |
Chris Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 98 | (!isa<PHINode>(phiOp) || !phisToGo.count(cast<PHINode>(phiOp)))) { |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 99 | // This operand is not a phi that will be deleted: need to store. |
| 100 | assert(!isa<TerminatorInst>(phiOp)); |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 101 | |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 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(); |
| 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. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 112 | |
| 113 | // Create instr. to store the value of phiOp before `insertBefore' |
| 114 | StoreInst* storeI = new StoreInst(phiOp, XSlot, storeBefore); |
| 115 | } |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 116 | } |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 117 | } |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 120 | //---------------------------------------------------------------------------- |
| 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 Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 134 | // |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 135 | AllocaInst* llvm::DemoteRegToStack(Instruction& X) { |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 136 | if (X.getType() == Type::VoidTy) |
Chris Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 137 | return 0; // nothing to do! |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 138 | |
| 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 Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 145 | Function* parentFunc = X.getParent()->getParent(); |
| 146 | AllocaInst *XSlot = new AllocaInst(X.getType(), 0, X.getName(), |
| 147 | parentFunc->getEntryBlock().begin()); |
| 148 | |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 149 | |
| 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 Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 159 | for (PhiSetIterator PI = phisToGo.begin(), E = phisToGo.end(); PI != E; ++PI) |
| 160 | (*PI)->getParent()->getInstList().erase(*PI); |
| 161 | |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 162 | return XSlot; |
| 163 | } |