Chris Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 1 | //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | ab2b328 | 2003-05-29 15:12:27 +0000 | [diff] [blame] | 10 | // This file provide the function DemoteRegToStack(). This function takes a |
Chris Lattner | c1a0623 | 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 | ab2b328 | 2003-05-29 15:12:27 +0000 | [diff] [blame] | 12 | // the stack frame, allocated via alloca. It returns the pointer to the |
Chris Lattner | c1a0623 | 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 | ab2b328 | 2003-05-29 15:12:27 +0000 | [diff] [blame] | 16 | // |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chris Lattner | a2dc727 | 2004-03-14 02:13:38 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/Local.h" |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 20 | #include "llvm/Function.h" |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 22 | #include "llvm/Type.h" |
Chris Lattner | c68bace | 2004-04-01 20:28:45 +0000 | [diff] [blame] | 23 | #include <map> |
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 | |
Chris Lattner | c1a0623 | 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. |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 31 | AllocaInst* llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads, |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 32 | Instruction *AllocaPoint) { |
| 33 | if (I.use_empty()) { |
| 34 | I.eraseFromParent(); |
| 35 | return 0; |
| 36 | } |
Jim Grosbach | d0e8469 | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 37 | |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 38 | // Create a stack slot to hold the value. |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 39 | AllocaInst *Slot; |
| 40 | if (AllocaPoint) { |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 41 | Slot = new AllocaInst(I.getType(), 0, |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 42 | I.getName()+".reg2mem", AllocaPoint); |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 43 | } else { |
| 44 | Function *F = I.getParent()->getParent(); |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 45 | Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem", |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 46 | F->getEntryBlock().begin()); |
| 47 | } |
Jim Grosbach | d0e8469 | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 48 | |
Bill Wendling | 1bc147b | 2012-02-17 02:09:28 +0000 | [diff] [blame^] | 49 | // Change all of the users of the instruction to read from the stack slot. |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 50 | while (!I.use_empty()) { |
| 51 | Instruction *U = cast<Instruction>(I.use_back()); |
| 52 | if (PHINode *PN = dyn_cast<PHINode>(U)) { |
| 53 | // If this is a PHI node, we can't insert a load of the value before the |
Bill Wendling | 1bc147b | 2012-02-17 02:09:28 +0000 | [diff] [blame^] | 54 | // use. Instead insert the load in the predecessor block corresponding |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 55 | // to the incoming value. |
| 56 | // |
| 57 | // Note that if there are multiple edges from a basic block to this PHI |
Bill Wendling | 1bc147b | 2012-02-17 02:09:28 +0000 | [diff] [blame^] | 58 | // node that we cannot have multiple loads. The problem is that the |
| 59 | // resulting PHI node will have multiple values (from each load) coming in |
| 60 | // from the same block, which is illegal SSA form. For this reason, we |
| 61 | // keep track of and reuse loads we insert. |
Chris Lattner | c68bace | 2004-04-01 20:28:45 +0000 | [diff] [blame] | 62 | std::map<BasicBlock*, Value*> Loads; |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 63 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 64 | if (PN->getIncomingValue(i) == &I) { |
Chris Lattner | c68bace | 2004-04-01 20:28:45 +0000 | [diff] [blame] | 65 | Value *&V = Loads[PN->getIncomingBlock(i)]; |
| 66 | if (V == 0) { |
| 67 | // Insert the load into the predecessor block |
Jim Grosbach | d0e8469 | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 68 | V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, |
Chris Lattner | c68bace | 2004-04-01 20:28:45 +0000 | [diff] [blame] | 69 | PN->getIncomingBlock(i)->getTerminator()); |
| 70 | } |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 71 | PN->setIncomingValue(i, V); |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 72 | } |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 73 | |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 74 | } else { |
| 75 | // If this is a normal instruction, just insert a load. |
Chris Lattner | 6d7277b | 2005-09-27 19:39:00 +0000 | [diff] [blame] | 76 | Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U); |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 77 | U->replaceUsesOfWith(&I, V); |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 78 | } |
Chris Lattner | ab4536e | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 79 | } |
Chris Lattner | 0c7e8e1 | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 80 | |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 81 | |
Bill Wendling | 1bc147b | 2012-02-17 02:09:28 +0000 | [diff] [blame^] | 82 | // Insert stores of the computed value into the stack slot. We have to be |
| 83 | // careful if I is an invoke instruction, because we can't insert the store |
| 84 | // AFTER the terminator instruction. |
Chris Lattner | 6d7277b | 2005-09-27 19:39:00 +0000 | [diff] [blame] | 85 | BasicBlock::iterator InsertPt; |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 86 | if (!isa<TerminatorInst>(I)) { |
Chris Lattner | 6d7277b | 2005-09-27 19:39:00 +0000 | [diff] [blame] | 87 | InsertPt = &I; |
Chris Lattner | ab55698 | 2005-10-04 00:44:01 +0000 | [diff] [blame] | 88 | ++InsertPt; |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 89 | } else { |
Chris Lattner | 6d7277b | 2005-09-27 19:39:00 +0000 | [diff] [blame] | 90 | // We cannot demote invoke instructions to the stack if their normal edge |
| 91 | // is critical. |
| 92 | InvokeInst &II = cast<InvokeInst>(I); |
| 93 | assert(II.getNormalDest()->getSinglePredecessor() && |
| 94 | "Cannot demote invoke with a critical successor!"); |
| 95 | InsertPt = II.getNormalDest()->begin(); |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 96 | } |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 97 | |
Bill Wendling | ac101e5 | 2011-11-07 19:38:34 +0000 | [diff] [blame] | 98 | for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt) |
Bill Wendling | 1bc147b | 2012-02-17 02:09:28 +0000 | [diff] [blame^] | 99 | /* empty */; // Don't insert before PHI nodes or landingpad instrs. |
Chris Lattner | 6d7277b | 2005-09-27 19:39:00 +0000 | [diff] [blame] | 100 | |
Bill Wendling | 1bc147b | 2012-02-17 02:09:28 +0000 | [diff] [blame^] | 101 | new StoreInst(&I, Slot, InsertPt); |
Chris Lattner | c1a0623 | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 102 | return Slot; |
Vikram S. Adve | 83e3b65 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 103 | } |
Tanya Lattner | 08d14d2 | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 104 | |
Bill Wendling | 1bc147b | 2012-02-17 02:09:28 +0000 | [diff] [blame^] | 105 | /// DemotePHIToStack - This function takes a virtual register computed by a PHI |
| 106 | /// node and replaces it with a slot in the stack frame allocated via alloca. |
| 107 | /// The PHI node is deleted. It returns the pointer to the alloca inserted. |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 108 | AllocaInst* llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) { |
Tanya Lattner | 08d14d2 | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 109 | if (P->use_empty()) { |
Jim Grosbach | d0e8469 | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 110 | P->eraseFromParent(); |
| 111 | return 0; |
Tanya Lattner | 08d14d2 | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 112 | } |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 113 | |
Tanya Lattner | 08d14d2 | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 114 | // Create a stack slot to hold the value. |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 115 | AllocaInst *Slot; |
| 116 | if (AllocaPoint) { |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 117 | Slot = new AllocaInst(P->getType(), 0, |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 118 | P->getName()+".reg2mem", AllocaPoint); |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 119 | } else { |
| 120 | Function *F = P->getParent()->getParent(); |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 121 | Slot = new AllocaInst(P->getType(), 0, P->getName()+".reg2mem", |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 122 | F->getEntryBlock().begin()); |
| 123 | } |
Jim Grosbach | d0e8469 | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 124 | |
Bill Wendling | 1bc147b | 2012-02-17 02:09:28 +0000 | [diff] [blame^] | 125 | // Iterate over each operand inserting a store in each predecessor. |
Tanya Lattner | 08d14d2 | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 126 | for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) { |
| 127 | if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) { |
Jim Grosbach | d0e8469 | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 128 | assert(II->getParent() != P->getIncomingBlock(i) && |
Jeffrey Yasskin | 8e68c38 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 129 | "Invoke edge not supported yet"); (void)II; |
Tanya Lattner | 08d14d2 | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 130 | } |
Jim Grosbach | d0e8469 | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 131 | new StoreInst(P->getIncomingValue(i), Slot, |
Tanya Lattner | 08d14d2 | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 132 | P->getIncomingBlock(i)->getTerminator()); |
| 133 | } |
Jim Grosbach | d0e8469 | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 134 | |
Bill Wendling | 1bc147b | 2012-02-17 02:09:28 +0000 | [diff] [blame^] | 135 | // Insert a load in place of the PHI and replace all uses. |
Tanya Lattner | 08d14d2 | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 136 | Value *V = new LoadInst(Slot, P->getName()+".reload", P); |
| 137 | P->replaceAllUsesWith(V); |
Jim Grosbach | d0e8469 | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 138 | |
Bill Wendling | 1bc147b | 2012-02-17 02:09:28 +0000 | [diff] [blame^] | 139 | // Delete PHI. |
Tanya Lattner | 08d14d2 | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 140 | P->eraseFromParent(); |
Tanya Lattner | 08d14d2 | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 141 | return Slot; |
| 142 | } |