blob: f11b6099939cb8ac3492135ab35cf634d674ca7c [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();
Craig Topperf40110f2014-04-25 05:29:35 +000028 return nullptr;
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000029 }
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) {
Craig Topperf40110f2014-04-25 05:29:35 +000034 Slot = new AllocaInst(I.getType(), nullptr,
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();
Craig Topperf40110f2014-04-25 05:29:35 +000038 Slot = new AllocaInst(I.getType(), nullptr, I.getName()+".reg2mem",
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000039 F->getEntryBlock().begin());
40 }
Jim Grosbache94f1de2010-06-16 22:41:09 +000041
Akira Hatanaka8d3cb822015-02-09 06:38:23 +000042 // We cannot demote invoke instructions to the stack if their normal edge
43 // is critical. Therefore, split the critical edge and create a basic block
44 // into which the store can be inserted.
45 if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
46 if (!II->getNormalDest()->getSinglePredecessor()) {
47 unsigned SuccNum = GetSuccessorNumber(II->getParent(), II->getNormalDest());
48 assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
49 BasicBlock *BB = SplitCriticalEdge(II, SuccNum);
50 assert(BB && "Unable to split critical edge.");
51 (void)BB;
52 }
53 }
54
Bill Wendling0a8fec22012-02-17 02:09:28 +000055 // Change all of the users of the instruction to read from the stack slot.
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000056 while (!I.use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000057 Instruction *U = cast<Instruction>(I.user_back());
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000058 if (PHINode *PN = dyn_cast<PHINode>(U)) {
59 // If this is a PHI node, we can't insert a load of the value before the
Bill Wendling0a8fec22012-02-17 02:09:28 +000060 // use. Instead insert the load in the predecessor block corresponding
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000061 // to the incoming value.
62 //
63 // Note that if there are multiple edges from a basic block to this PHI
Bill Wendling0a8fec22012-02-17 02:09:28 +000064 // node that we cannot have multiple loads. The problem is that the
65 // resulting PHI node will have multiple values (from each load) coming in
66 // from the same block, which is illegal SSA form. For this reason, we
67 // keep track of and reuse loads we insert.
Bill Wendlingaa9a3ea2012-02-17 02:12:54 +000068 DenseMap<BasicBlock*, Value*> Loads;
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000069 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
70 if (PN->getIncomingValue(i) == &I) {
Chris Lattnerc24019c2004-04-01 20:28:45 +000071 Value *&V = Loads[PN->getIncomingBlock(i)];
Craig Topperf40110f2014-04-25 05:29:35 +000072 if (!V) {
Chris Lattnerc24019c2004-04-01 20:28:45 +000073 // Insert the load into the predecessor block
Jim Grosbache94f1de2010-06-16 22:41:09 +000074 V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
Chris Lattnerc24019c2004-04-01 20:28:45 +000075 PN->getIncomingBlock(i)->getTerminator());
76 }
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000077 PN->setIncomingValue(i, V);
Chris Lattner4413e432003-09-20 14:36:23 +000078 }
Vikram S. Advec864ab12002-12-10 13:07:58 +000079
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000080 } else {
81 // If this is a normal instruction, just insert a load.
Chris Lattner16cd3562005-09-27 19:39:00 +000082 Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000083 U->replaceUsesOfWith(&I, V);
Vikram S. Advec864ab12002-12-10 13:07:58 +000084 }
Chris Lattner4413e432003-09-20 14:36:23 +000085 }
Chris Lattner38cd27e2003-11-06 19:46:29 +000086
Bill Wendling0a8fec22012-02-17 02:09:28 +000087 // Insert stores of the computed value into the stack slot. We have to be
88 // careful if I is an invoke instruction, because we can't insert the store
89 // AFTER the terminator instruction.
Chris Lattner16cd3562005-09-27 19:39:00 +000090 BasicBlock::iterator InsertPt;
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000091 if (!isa<TerminatorInst>(I)) {
Chris Lattner16cd3562005-09-27 19:39:00 +000092 InsertPt = &I;
Chris Lattner20b07542005-10-04 00:44:01 +000093 ++InsertPt;
David Majnemereb518bd2015-08-04 08:21:40 +000094 for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
Akira Hatanaka8d3cb822015-02-09 06:38:23 +000095 /* empty */; // Don't insert before PHI nodes or landingpad instrs.
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000096 } else {
Chris Lattner16cd3562005-09-27 19:39:00 +000097 InvokeInst &II = cast<InvokeInst>(I);
Akira Hatanaka8d3cb822015-02-09 06:38:23 +000098 InsertPt = II.getNormalDest()->getFirstInsertionPt();
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000099 }
Vikram S. Advec864ab12002-12-10 13:07:58 +0000100
Bill Wendling0a8fec22012-02-17 02:09:28 +0000101 new StoreInst(&I, Slot, InsertPt);
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +0000102 return Slot;
Vikram S. Advec864ab12002-12-10 13:07:58 +0000103}
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000104
Bill Wendling0a8fec22012-02-17 02:09:28 +0000105/// 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.
Bill Wendlingaa9a3ea2012-02-17 02:12:54 +0000108AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000109 if (P->use_empty()) {
Jim Grosbache94f1de2010-06-16 22:41:09 +0000110 P->eraseFromParent();
Craig Topperf40110f2014-04-25 05:29:35 +0000111 return nullptr;
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000112 }
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000113
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000114 // Create a stack slot to hold the value.
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000115 AllocaInst *Slot;
116 if (AllocaPoint) {
Craig Topperf40110f2014-04-25 05:29:35 +0000117 Slot = new AllocaInst(P->getType(), nullptr,
Owen Andersonb6b25302009-07-14 23:09:55 +0000118 P->getName()+".reg2mem", AllocaPoint);
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000119 } else {
120 Function *F = P->getParent()->getParent();
Craig Topperf40110f2014-04-25 05:29:35 +0000121 Slot = new AllocaInst(P->getType(), nullptr, P->getName()+".reg2mem",
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000122 F->getEntryBlock().begin());
123 }
Jim Grosbache94f1de2010-06-16 22:41:09 +0000124
Bill Wendling0a8fec22012-02-17 02:09:28 +0000125 // Iterate over each operand inserting a store in each predecessor.
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000126 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
127 if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
Jim Grosbache94f1de2010-06-16 22:41:09 +0000128 assert(II->getParent() != P->getIncomingBlock(i) &&
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000129 "Invoke edge not supported yet"); (void)II;
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000130 }
Jim Grosbache94f1de2010-06-16 22:41:09 +0000131 new StoreInst(P->getIncomingValue(i), Slot,
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000132 P->getIncomingBlock(i)->getTerminator());
133 }
Jim Grosbache94f1de2010-06-16 22:41:09 +0000134
Bill Wendling0a8fec22012-02-17 02:09:28 +0000135 // Insert a load in place of the PHI and replace all uses.
Bill Wendling76c65212013-01-08 10:51:32 +0000136 BasicBlock::iterator InsertPt = P;
137
David Majnemer09e1fdb2015-08-06 21:13:51 +0000138 for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
Bill Wendling76c65212013-01-08 10:51:32 +0000139 /* empty */; // Don't insert before PHI nodes or landingpad instrs.
140
141 Value *V = new LoadInst(Slot, P->getName()+".reload", InsertPt);
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000142 P->replaceAllUsesWith(V);
Jim Grosbache94f1de2010-06-16 22:41:09 +0000143
Bill Wendling0a8fec22012-02-17 02:09:28 +0000144 // Delete PHI.
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000145 P->eraseFromParent();
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000146 return Slot;
147}