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