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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 8 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 9 | #include "llvm/ADT/DenseMap.h" |
Nick Lewycky | 0b68245 | 2013-07-27 01:24:00 +0000 | [diff] [blame] | 10 | #include "llvm/Analysis/CFG.h" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 11 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 12 | #include "llvm/IR/Function.h" |
| 13 | #include "llvm/IR/Instructions.h" |
| 14 | #include "llvm/IR/Type.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 16 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 17 | |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 18 | /// DemoteRegToStack - This function takes a virtual register computed by an |
| 19 | /// Instruction and replaces it with a slot in the stack frame, allocated via |
| 20 | /// alloca. This allows the CFG to be changed around without fear of |
| 21 | /// invalidating the SSA information for the value. It returns the pointer to |
| 22 | /// the alloca inserted to create a stack slot for I. |
Bill Wendling | aa9a3ea | 2012-02-17 02:12:54 +0000 | [diff] [blame] | 23 | AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads, |
Anton Korobeynikov | 7499a3b | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 24 | Instruction *AllocaPoint) { |
| 25 | if (I.use_empty()) { |
| 26 | I.eraseFromParent(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 27 | return nullptr; |
Anton Korobeynikov | 7499a3b | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 28 | } |
Jim Grosbach | e94f1de | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 29 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 30 | Function *F = I.getParent()->getParent(); |
| 31 | const DataLayout &DL = F->getParent()->getDataLayout(); |
| 32 | |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 33 | // Create a stack slot to hold the value. |
Anton Korobeynikov | 7499a3b | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 34 | AllocaInst *Slot; |
| 35 | if (AllocaPoint) { |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 36 | Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr, |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 37 | I.getName()+".reg2mem", AllocaPoint); |
Anton Korobeynikov | 7499a3b | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 38 | } else { |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 39 | Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr, |
| 40 | I.getName() + ".reg2mem", &F->getEntryBlock().front()); |
Anton Korobeynikov | 7499a3b | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 41 | } |
Jim Grosbach | e94f1de | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 42 | |
Akira Hatanaka | 8d3cb82 | 2015-02-09 06:38:23 +0000 | [diff] [blame] | 43 | // We cannot demote invoke instructions to the stack if their normal edge |
| 44 | // is critical. Therefore, split the critical edge and create a basic block |
| 45 | // into which the store can be inserted. |
| 46 | if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
| 47 | if (!II->getNormalDest()->getSinglePredecessor()) { |
| 48 | unsigned SuccNum = GetSuccessorNumber(II->getParent(), II->getNormalDest()); |
| 49 | assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!"); |
| 50 | BasicBlock *BB = SplitCriticalEdge(II, SuccNum); |
| 51 | assert(BB && "Unable to split critical edge."); |
| 52 | (void)BB; |
| 53 | } |
| 54 | } |
| 55 | |
Bill Wendling | 0a8fec2 | 2012-02-17 02:09:28 +0000 | [diff] [blame] | 56 | // Change all of the users of the instruction to read from the stack slot. |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 57 | while (!I.use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 58 | Instruction *U = cast<Instruction>(I.user_back()); |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 59 | if (PHINode *PN = dyn_cast<PHINode>(U)) { |
| 60 | // If this is a PHI node, we can't insert a load of the value before the |
Bill Wendling | 0a8fec2 | 2012-02-17 02:09:28 +0000 | [diff] [blame] | 61 | // use. Instead insert the load in the predecessor block corresponding |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 62 | // to the incoming value. |
| 63 | // |
| 64 | // Note that if there are multiple edges from a basic block to this PHI |
Bill Wendling | 0a8fec2 | 2012-02-17 02:09:28 +0000 | [diff] [blame] | 65 | // node that we cannot have multiple loads. The problem is that the |
| 66 | // resulting PHI node will have multiple values (from each load) coming in |
| 67 | // from the same block, which is illegal SSA form. For this reason, we |
| 68 | // keep track of and reuse loads we insert. |
Bill Wendling | aa9a3ea | 2012-02-17 02:12:54 +0000 | [diff] [blame] | 69 | DenseMap<BasicBlock*, Value*> Loads; |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 70 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 71 | if (PN->getIncomingValue(i) == &I) { |
Chris Lattner | c24019c | 2004-04-01 20:28:45 +0000 | [diff] [blame] | 72 | Value *&V = Loads[PN->getIncomingBlock(i)]; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 73 | if (!V) { |
Chris Lattner | c24019c | 2004-04-01 20:28:45 +0000 | [diff] [blame] | 74 | // Insert the load into the predecessor block |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 75 | V = new LoadInst(I.getType(), Slot, I.getName() + ".reload", |
| 76 | VolatileLoads, |
Chris Lattner | c24019c | 2004-04-01 20:28:45 +0000 | [diff] [blame] | 77 | PN->getIncomingBlock(i)->getTerminator()); |
| 78 | } |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 79 | PN->setIncomingValue(i, V); |
Chris Lattner | 4413e43 | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 80 | } |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 81 | |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 82 | } else { |
| 83 | // If this is a normal instruction, just insert a load. |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 84 | Value *V = new LoadInst(I.getType(), Slot, I.getName() + ".reload", |
| 85 | VolatileLoads, U); |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 86 | U->replaceUsesOfWith(&I, V); |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 87 | } |
Chris Lattner | 4413e43 | 2003-09-20 14:36:23 +0000 | [diff] [blame] | 88 | } |
Chris Lattner | 38cd27e | 2003-11-06 19:46:29 +0000 | [diff] [blame] | 89 | |
Bill Wendling | 0a8fec2 | 2012-02-17 02:09:28 +0000 | [diff] [blame] | 90 | // Insert stores of the computed value into the stack slot. We have to be |
| 91 | // careful if I is an invoke instruction, because we can't insert the store |
| 92 | // AFTER the terminator instruction. |
Chris Lattner | 16cd356 | 2005-09-27 19:39:00 +0000 | [diff] [blame] | 93 | BasicBlock::iterator InsertPt; |
Chandler Carruth | 9ae926b | 2018-08-26 09:51:22 +0000 | [diff] [blame] | 94 | if (!I.isTerminator()) { |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 95 | InsertPt = ++I.getIterator(); |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 96 | for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt) |
Akira Hatanaka | 8d3cb82 | 2015-02-09 06:38:23 +0000 | [diff] [blame] | 97 | /* empty */; // Don't insert before PHI nodes or landingpad instrs. |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 98 | } else { |
Chris Lattner | 16cd356 | 2005-09-27 19:39:00 +0000 | [diff] [blame] | 99 | InvokeInst &II = cast<InvokeInst>(I); |
Akira Hatanaka | 8d3cb82 | 2015-02-09 06:38:23 +0000 | [diff] [blame] | 100 | InsertPt = II.getNormalDest()->getFirstInsertionPt(); |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 101 | } |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 102 | |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 103 | new StoreInst(&I, Slot, &*InsertPt); |
Chris Lattner | bb1a2cc | 2004-03-16 23:23:11 +0000 | [diff] [blame] | 104 | return Slot; |
Vikram S. Adve | c864ab1 | 2002-12-10 13:07:58 +0000 | [diff] [blame] | 105 | } |
Tanya Lattner | ccecbcd | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 106 | |
Bill Wendling | 0a8fec2 | 2012-02-17 02:09:28 +0000 | [diff] [blame] | 107 | /// DemotePHIToStack - This function takes a virtual register computed by a PHI |
| 108 | /// node and replaces it with a slot in the stack frame allocated via alloca. |
| 109 | /// The PHI node is deleted. It returns the pointer to the alloca inserted. |
Bill Wendling | aa9a3ea | 2012-02-17 02:12:54 +0000 | [diff] [blame] | 110 | AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) { |
Tanya Lattner | ccecbcd | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 111 | if (P->use_empty()) { |
Jim Grosbach | e94f1de | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 112 | P->eraseFromParent(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 113 | return nullptr; |
Tanya Lattner | ccecbcd | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 114 | } |
Anton Korobeynikov | 7499a3b | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 115 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 116 | const DataLayout &DL = P->getModule()->getDataLayout(); |
| 117 | |
Tanya Lattner | ccecbcd | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 118 | // Create a stack slot to hold the value. |
Anton Korobeynikov | 7499a3b | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 119 | AllocaInst *Slot; |
| 120 | if (AllocaPoint) { |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 121 | Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr, |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 122 | P->getName()+".reg2mem", AllocaPoint); |
Anton Korobeynikov | 7499a3b | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 123 | } else { |
| 124 | Function *F = P->getParent()->getParent(); |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 125 | Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr, |
| 126 | P->getName() + ".reg2mem", |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 127 | &F->getEntryBlock().front()); |
Anton Korobeynikov | 7499a3b | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 128 | } |
Jim Grosbach | e94f1de | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 129 | |
Bill Wendling | 0a8fec2 | 2012-02-17 02:09:28 +0000 | [diff] [blame] | 130 | // Iterate over each operand inserting a store in each predecessor. |
Tanya Lattner | ccecbcd | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 131 | for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) { |
| 132 | if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) { |
Jim Grosbach | e94f1de | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 133 | assert(II->getParent() != P->getIncomingBlock(i) && |
Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 134 | "Invoke edge not supported yet"); (void)II; |
Tanya Lattner | ccecbcd | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 135 | } |
Jim Grosbach | e94f1de | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 136 | new StoreInst(P->getIncomingValue(i), Slot, |
Tanya Lattner | ccecbcd | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 137 | P->getIncomingBlock(i)->getTerminator()); |
| 138 | } |
Jim Grosbach | e94f1de | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 139 | |
Bill Wendling | 0a8fec2 | 2012-02-17 02:09:28 +0000 | [diff] [blame] | 140 | // Insert a load in place of the PHI and replace all uses. |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 141 | BasicBlock::iterator InsertPt = P->getIterator(); |
Bill Wendling | 76c6521 | 2013-01-08 10:51:32 +0000 | [diff] [blame] | 142 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 143 | for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt) |
Bill Wendling | 76c6521 | 2013-01-08 10:51:32 +0000 | [diff] [blame] | 144 | /* empty */; // Don't insert before PHI nodes or landingpad instrs. |
| 145 | |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 146 | Value *V = |
| 147 | new LoadInst(P->getType(), Slot, P->getName() + ".reload", &*InsertPt); |
Tanya Lattner | ccecbcd | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 148 | P->replaceAllUsesWith(V); |
Jim Grosbach | e94f1de | 2010-06-16 22:41:09 +0000 | [diff] [blame] | 149 | |
Bill Wendling | 0a8fec2 | 2012-02-17 02:09:28 +0000 | [diff] [blame] | 150 | // Delete PHI. |
Tanya Lattner | ccecbcd | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 151 | P->eraseFromParent(); |
Tanya Lattner | ccecbcd | 2007-07-11 18:41:34 +0000 | [diff] [blame] | 152 | return Slot; |
| 153 | } |