blob: ac6926fe9e74ef0a52b3eefb5885dd5aec8afc24 [file] [log] [blame]
Chris Lattner38cd27e2003-11-06 19:46:29 +00001//===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Vikram S. Advec864ab12002-12-10 13:07:58 +00009
Chad Rosier92a54f62013-02-05 18:23:10 +000010#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/DenseMap.h"
Nick Lewycky0b682452013-07-27 01:24:00 +000012#include "llvm/Analysis/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/Function.h"
14#include "llvm/IR/Instructions.h"
15#include "llvm/IR/Type.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/Transforms/Utils/Local.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000017using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000018
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000019/// DemoteRegToStack - This function takes a virtual register computed by an
20/// Instruction and replaces it with a slot in the stack frame, allocated via
21/// alloca. This allows the CFG to be changed around without fear of
22/// invalidating the SSA information for the value. It returns the pointer to
23/// the alloca inserted to create a stack slot for I.
Bill Wendlingaa9a3ea2012-02-17 02:12:54 +000024AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000025 Instruction *AllocaPoint) {
26 if (I.use_empty()) {
27 I.eraseFromParent();
28 return 0;
29 }
Jim Grosbache94f1de2010-06-16 22:41:09 +000030
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000031 // Create a stack slot to hold the value.
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000032 AllocaInst *Slot;
33 if (AllocaPoint) {
Owen Anderson4fdeba92009-07-15 23:53:25 +000034 Slot = new AllocaInst(I.getType(), 0,
Owen Andersonb6b25302009-07-14 23:09:55 +000035 I.getName()+".reg2mem", AllocaPoint);
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000036 } else {
37 Function *F = I.getParent()->getParent();
Owen Anderson4fdeba92009-07-15 23:53:25 +000038 Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem",
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000039 F->getEntryBlock().begin());
40 }
Jim Grosbache94f1de2010-06-16 22:41:09 +000041
Bill Wendling0a8fec22012-02-17 02:09:28 +000042 // Change all of the users of the instruction to read from the stack slot.
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000043 while (!I.use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000044 Instruction *U = cast<Instruction>(I.user_back());
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000045 if (PHINode *PN = dyn_cast<PHINode>(U)) {
46 // If this is a PHI node, we can't insert a load of the value before the
Bill Wendling0a8fec22012-02-17 02:09:28 +000047 // use. Instead insert the load in the predecessor block corresponding
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000048 // to the incoming value.
49 //
50 // Note that if there are multiple edges from a basic block to this PHI
Bill Wendling0a8fec22012-02-17 02:09:28 +000051 // node that we cannot have multiple loads. The problem is that the
52 // resulting PHI node will have multiple values (from each load) coming in
53 // from the same block, which is illegal SSA form. For this reason, we
54 // keep track of and reuse loads we insert.
Bill Wendlingaa9a3ea2012-02-17 02:12:54 +000055 DenseMap<BasicBlock*, Value*> Loads;
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000056 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
57 if (PN->getIncomingValue(i) == &I) {
Chris Lattnerc24019c2004-04-01 20:28:45 +000058 Value *&V = Loads[PN->getIncomingBlock(i)];
59 if (V == 0) {
60 // Insert the load into the predecessor block
Jim Grosbache94f1de2010-06-16 22:41:09 +000061 V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
Chris Lattnerc24019c2004-04-01 20:28:45 +000062 PN->getIncomingBlock(i)->getTerminator());
63 }
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000064 PN->setIncomingValue(i, V);
Chris Lattner4413e432003-09-20 14:36:23 +000065 }
Vikram S. Advec864ab12002-12-10 13:07:58 +000066
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000067 } else {
68 // If this is a normal instruction, just insert a load.
Chris Lattner16cd3562005-09-27 19:39:00 +000069 Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000070 U->replaceUsesOfWith(&I, V);
Vikram S. Advec864ab12002-12-10 13:07:58 +000071 }
Chris Lattner4413e432003-09-20 14:36:23 +000072 }
Chris Lattner38cd27e2003-11-06 19:46:29 +000073
Vikram S. Advec864ab12002-12-10 13:07:58 +000074
Bill Wendling0a8fec22012-02-17 02:09:28 +000075 // Insert stores of the computed value into the stack slot. We have to be
76 // careful if I is an invoke instruction, because we can't insert the store
77 // AFTER the terminator instruction.
Chris Lattner16cd3562005-09-27 19:39:00 +000078 BasicBlock::iterator InsertPt;
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000079 if (!isa<TerminatorInst>(I)) {
Chris Lattner16cd3562005-09-27 19:39:00 +000080 InsertPt = &I;
Chris Lattner20b07542005-10-04 00:44:01 +000081 ++InsertPt;
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000082 } else {
Chris Lattner16cd3562005-09-27 19:39:00 +000083 InvokeInst &II = cast<InvokeInst>(I);
Chad Rosier92a54f62013-02-05 18:23:10 +000084 if (II.getNormalDest()->getSinglePredecessor())
85 InsertPt = II.getNormalDest()->getFirstInsertionPt();
86 else {
87 // We cannot demote invoke instructions to the stack if their normal edge
88 // is critical. Therefore, split the critical edge and insert the store
89 // in the newly created basic block.
90 unsigned SuccNum = GetSuccessorNumber(I.getParent(), II.getNormalDest());
91 TerminatorInst *TI = &cast<TerminatorInst>(I);
92 assert (isCriticalEdge(TI, SuccNum) &&
93 "Expected a critical edge!");
94 BasicBlock *BB = SplitCriticalEdge(TI, SuccNum);
95 assert (BB && "Unable to split critical edge.");
96 InsertPt = BB->getFirstInsertionPt();
97 }
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000098 }
Vikram S. Advec864ab12002-12-10 13:07:58 +000099
Bill Wendling74964612011-11-07 19:38:34 +0000100 for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
Bill Wendling0a8fec22012-02-17 02:09:28 +0000101 /* empty */; // Don't insert before PHI nodes or landingpad instrs.
Chris Lattner16cd3562005-09-27 19:39:00 +0000102
Bill Wendling0a8fec22012-02-17 02:09:28 +0000103 new StoreInst(&I, Slot, InsertPt);
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +0000104 return Slot;
Vikram S. Advec864ab12002-12-10 13:07:58 +0000105}
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000106
Bill Wendling0a8fec22012-02-17 02:09:28 +0000107/// 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 Wendlingaa9a3ea2012-02-17 02:12:54 +0000110AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000111 if (P->use_empty()) {
Jim Grosbache94f1de2010-06-16 22:41:09 +0000112 P->eraseFromParent();
113 return 0;
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000114 }
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000115
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000116 // Create a stack slot to hold the value.
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000117 AllocaInst *Slot;
118 if (AllocaPoint) {
Owen Anderson4fdeba92009-07-15 23:53:25 +0000119 Slot = new AllocaInst(P->getType(), 0,
Owen Andersonb6b25302009-07-14 23:09:55 +0000120 P->getName()+".reg2mem", AllocaPoint);
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000121 } else {
122 Function *F = P->getParent()->getParent();
Owen Anderson4fdeba92009-07-15 23:53:25 +0000123 Slot = new AllocaInst(P->getType(), 0, P->getName()+".reg2mem",
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000124 F->getEntryBlock().begin());
125 }
Jim Grosbache94f1de2010-06-16 22:41:09 +0000126
Bill Wendling0a8fec22012-02-17 02:09:28 +0000127 // Iterate over each operand inserting a store in each predecessor.
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000128 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
129 if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
Jim Grosbache94f1de2010-06-16 22:41:09 +0000130 assert(II->getParent() != P->getIncomingBlock(i) &&
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000131 "Invoke edge not supported yet"); (void)II;
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000132 }
Jim Grosbache94f1de2010-06-16 22:41:09 +0000133 new StoreInst(P->getIncomingValue(i), Slot,
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000134 P->getIncomingBlock(i)->getTerminator());
135 }
Jim Grosbache94f1de2010-06-16 22:41:09 +0000136
Bill Wendling0a8fec22012-02-17 02:09:28 +0000137 // Insert a load in place of the PHI and replace all uses.
Bill Wendling76c65212013-01-08 10:51:32 +0000138 BasicBlock::iterator InsertPt = P;
139
140 for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
141 /* empty */; // Don't insert before PHI nodes or landingpad instrs.
142
143 Value *V = new LoadInst(Slot, P->getName()+".reload", InsertPt);
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000144 P->replaceAllUsesWith(V);
Jim Grosbache94f1de2010-06-16 22:41:09 +0000145
Bill Wendling0a8fec22012-02-17 02:09:28 +0000146 // Delete PHI.
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000147 P->eraseFromParent();
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000148 return Slot;
149}