Chris Lattner | 38cd27e | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 1 | //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame^] | 2 | // |
John Criswell | 482202a | 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. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame^] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame^] | 9 | // |
Chris Lattner | f789f29 | 2003-05-29 15:12:27 +0000 | [diff] [blame] | 10 | // This file provide the function DemoteRegToStack(). This function takes a |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 11 | // virtual register computed by an Instruction and replaces it with a slot in |
Chris Lattner | f789f29 | 2003-05-29 15:12:27 +0000 | [diff] [blame] | 12 | // the stack frame, allocated via alloca. It returns the pointer to the |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 13 | // AllocaInst inserted. After this function is called on an instruction, we are |
| 14 | // guaranteed that the only user of the instruction is a store that is |
| 15 | // immediately after it. |
Chris Lattner | f789f29 | 2003-05-29 15:12:27 +0000 | [diff] [blame] | 16 | // |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chris Lattner | 8eebc49 | 2004-03-14 02:13:38 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/Local.h" |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 20 | #include "llvm/Function.h" |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
Chris Lattner | 7b9020a | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 22 | #include "llvm/Type.h" |
Chris Lattner | c24019c | 2004-04-01 20:28:45 +0000 | [diff] [blame] | 23 | #include <map> |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 26 | /// DemoteRegToStack - This function takes a virtual register computed by an |
| 27 | /// Instruction and replaces it with a slot in the stack frame, allocated via |
| 28 | /// alloca. This allows the CFG to be changed around without fear of |
| 29 | /// invalidating the SSA information for the value. It returns the pointer to |
| 30 | /// the alloca inserted to create a stack slot for I. |
| 31 | /// |
| 32 | AllocaInst* llvm::DemoteRegToStack(Instruction &I) { |
| 33 | if (I.use_empty()) return 0; // nothing to do! |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 34 | |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 35 | // Create a stack slot to hold the value. |
| 36 | Function *F = I.getParent()->getParent(); |
| 37 | AllocaInst *Slot = new AllocaInst(I.getType(), 0, I.getName(), |
| 38 | F->getEntryBlock().begin()); |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 39 | |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 40 | // Change all of the users of the instruction to read from the stack slot |
| 41 | // instead. |
| 42 | while (!I.use_empty()) { |
| 43 | Instruction *U = cast<Instruction>(I.use_back()); |
| 44 | if (PHINode *PN = dyn_cast<PHINode>(U)) { |
| 45 | // If this is a PHI node, we can't insert a load of the value before the |
| 46 | // use. Instead, insert the load in the predecessor block corresponding |
| 47 | // to the incoming value. |
| 48 | // |
| 49 | // Note that if there are multiple edges from a basic block to this PHI |
Chris Lattner | c24019c | 2004-04-01 20:28:45 +0000 | [diff] [blame] | 50 | // node that we cannot multiple loads. The problem is that the resultant |
| 51 | // PHI node will have multiple values (from each load) coming in from the |
| 52 | // same block, which is illegal SSA form. For this reason, we keep track |
| 53 | // and reuse loads we insert. |
| 54 | std::map<BasicBlock*, Value*> Loads; |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 55 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 56 | if (PN->getIncomingValue(i) == &I) { |
Chris Lattner | c24019c | 2004-04-01 20:28:45 +0000 | [diff] [blame] | 57 | Value *&V = Loads[PN->getIncomingBlock(i)]; |
| 58 | if (V == 0) { |
| 59 | // Insert the load into the predecessor block |
| 60 | V = new LoadInst(Slot, I.getName()+".reload", |
| 61 | PN->getIncomingBlock(i)->getTerminator()); |
| 62 | } |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 63 | PN->setIncomingValue(i, V); |
Chris Lattner | 4413e43 | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 64 | } |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 65 | |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 66 | } else { |
| 67 | // If this is a normal instruction, just insert a load. |
| 68 | Value *V = new LoadInst(Slot, I.getName()+".reload", U); |
| 69 | U->replaceUsesOfWith(&I, V); |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 70 | } |
Chris Lattner | 4413e43 | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 71 | } |
Chris Lattner | 38cd27e | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 72 | |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 73 | |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 74 | // Insert stores of the computed value into the stack slot. We have to be |
| 75 | // careful is I is an invoke instruction though, because we can't insert the |
| 76 | // store AFTER the terminator instruction. |
| 77 | if (!isa<TerminatorInst>(I)) { |
| 78 | BasicBlock::iterator InsertPt = &I; |
| 79 | for (++InsertPt; isa<PHINode>(InsertPt); ++InsertPt) |
| 80 | /* empty */; // Don't insert before any PHI nodes. |
| 81 | new StoreInst(&I, Slot, InsertPt); |
| 82 | } else { |
| 83 | // FIXME: We cannot yet demote invoke instructions to the stack, because |
| 84 | // doing so would require breaking critical edges. This should be fixed |
| 85 | // eventually. |
| 86 | assert(0 && |
| 87 | "Cannot demote the value computed by an invoke instruction yet!"); |
| 88 | } |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 89 | |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 90 | return Slot; |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 91 | } |