blob: 6d3d287defdb2fb2074ce7e3f85b636b76413f63 [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000010#include "llvm/ADT/DenseMap.h"
Nick Lewycky0b682452013-07-27 01:24:00 +000011#include "llvm/Analysis/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000012#include "llvm/IR/Function.h"
13#include "llvm/IR/Instructions.h"
14#include "llvm/IR/Type.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/Transforms/Utils/BasicBlockUtils.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
Matt Arsenault3c1fc762017-04-10 22:27:50 +000031 Function *F = I.getParent()->getParent();
32 const DataLayout &DL = F->getParent()->getDataLayout();
33
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000034 // Create a stack slot to hold the value.
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000035 AllocaInst *Slot;
36 if (AllocaPoint) {
Matt Arsenault3c1fc762017-04-10 22:27:50 +000037 Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,
Owen Andersonb6b25302009-07-14 23:09:55 +000038 I.getName()+".reg2mem", AllocaPoint);
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000039 } else {
Matt Arsenault3c1fc762017-04-10 22:27:50 +000040 Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,
41 I.getName() + ".reg2mem", &F->getEntryBlock().front());
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000042 }
Jim Grosbache94f1de2010-06-16 22:41:09 +000043
Akira Hatanaka8d3cb822015-02-09 06:38:23 +000044 // We cannot demote invoke instructions to the stack if their normal edge
45 // is critical. Therefore, split the critical edge and create a basic block
46 // into which the store can be inserted.
47 if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
48 if (!II->getNormalDest()->getSinglePredecessor()) {
49 unsigned SuccNum = GetSuccessorNumber(II->getParent(), II->getNormalDest());
50 assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
51 BasicBlock *BB = SplitCriticalEdge(II, SuccNum);
52 assert(BB && "Unable to split critical edge.");
53 (void)BB;
54 }
55 }
56
Bill Wendling0a8fec22012-02-17 02:09:28 +000057 // Change all of the users of the instruction to read from the stack slot.
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000058 while (!I.use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000059 Instruction *U = cast<Instruction>(I.user_back());
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000060 if (PHINode *PN = dyn_cast<PHINode>(U)) {
61 // If this is a PHI node, we can't insert a load of the value before the
Bill Wendling0a8fec22012-02-17 02:09:28 +000062 // use. Instead insert the load in the predecessor block corresponding
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000063 // to the incoming value.
64 //
65 // Note that if there are multiple edges from a basic block to this PHI
Bill Wendling0a8fec22012-02-17 02:09:28 +000066 // node that we cannot have multiple loads. The problem is that the
67 // resulting PHI node will have multiple values (from each load) coming in
68 // from the same block, which is illegal SSA form. For this reason, we
69 // keep track of and reuse loads we insert.
Bill Wendlingaa9a3ea2012-02-17 02:12:54 +000070 DenseMap<BasicBlock*, Value*> Loads;
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000071 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
72 if (PN->getIncomingValue(i) == &I) {
Chris Lattnerc24019c2004-04-01 20:28:45 +000073 Value *&V = Loads[PN->getIncomingBlock(i)];
Craig Topperf40110f2014-04-25 05:29:35 +000074 if (!V) {
Chris Lattnerc24019c2004-04-01 20:28:45 +000075 // Insert the load into the predecessor block
Jim Grosbache94f1de2010-06-16 22:41:09 +000076 V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
Chris Lattnerc24019c2004-04-01 20:28:45 +000077 PN->getIncomingBlock(i)->getTerminator());
78 }
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000079 PN->setIncomingValue(i, V);
Chris Lattner4413e432003-09-20 14:36:23 +000080 }
Vikram S. Advec864ab12002-12-10 13:07:58 +000081
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000082 } else {
83 // If this is a normal instruction, just insert a load.
Chris Lattner16cd3562005-09-27 19:39:00 +000084 Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000085 U->replaceUsesOfWith(&I, V);
Vikram S. Advec864ab12002-12-10 13:07:58 +000086 }
Chris Lattner4413e432003-09-20 14:36:23 +000087 }
Chris Lattner38cd27e2003-11-06 19:46:29 +000088
Bill Wendling0a8fec22012-02-17 02:09:28 +000089 // Insert stores of the computed value into the stack slot. We have to be
90 // careful if I is an invoke instruction, because we can't insert the store
91 // AFTER the terminator instruction.
Chris Lattner16cd3562005-09-27 19:39:00 +000092 BasicBlock::iterator InsertPt;
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000093 if (!isa<TerminatorInst>(I)) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000094 InsertPt = ++I.getIterator();
David Majnemereb518bd2015-08-04 08:21:40 +000095 for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
Akira Hatanaka8d3cb822015-02-09 06:38:23 +000096 /* empty */; // Don't insert before PHI nodes or landingpad instrs.
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000097 } else {
Chris Lattner16cd3562005-09-27 19:39:00 +000098 InvokeInst &II = cast<InvokeInst>(I);
Akira Hatanaka8d3cb822015-02-09 06:38:23 +000099 InsertPt = II.getNormalDest()->getFirstInsertionPt();
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +0000100 }
Vikram S. Advec864ab12002-12-10 13:07:58 +0000101
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000102 new StoreInst(&I, Slot, &*InsertPt);
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +0000103 return Slot;
Vikram S. Advec864ab12002-12-10 13:07:58 +0000104}
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000105
Bill Wendling0a8fec22012-02-17 02:09:28 +0000106/// DemotePHIToStack - This function takes a virtual register computed by a PHI
107/// node and replaces it with a slot in the stack frame allocated via alloca.
108/// The PHI node is deleted. It returns the pointer to the alloca inserted.
Bill Wendlingaa9a3ea2012-02-17 02:12:54 +0000109AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000110 if (P->use_empty()) {
Jim Grosbache94f1de2010-06-16 22:41:09 +0000111 P->eraseFromParent();
Craig Topperf40110f2014-04-25 05:29:35 +0000112 return nullptr;
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000113 }
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000114
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000115 const DataLayout &DL = P->getModule()->getDataLayout();
116
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000117 // Create a stack slot to hold the value.
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000118 AllocaInst *Slot;
119 if (AllocaPoint) {
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000120 Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,
Owen Andersonb6b25302009-07-14 23:09:55 +0000121 P->getName()+".reg2mem", AllocaPoint);
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000122 } else {
123 Function *F = P->getParent()->getParent();
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000124 Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,
125 P->getName() + ".reg2mem",
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000126 &F->getEntryBlock().front());
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +0000127 }
Jim Grosbache94f1de2010-06-16 22:41:09 +0000128
Bill Wendling0a8fec22012-02-17 02:09:28 +0000129 // Iterate over each operand inserting a store in each predecessor.
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000130 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
131 if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
Jim Grosbache94f1de2010-06-16 22:41:09 +0000132 assert(II->getParent() != P->getIncomingBlock(i) &&
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000133 "Invoke edge not supported yet"); (void)II;
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000134 }
Jim Grosbache94f1de2010-06-16 22:41:09 +0000135 new StoreInst(P->getIncomingValue(i), Slot,
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000136 P->getIncomingBlock(i)->getTerminator());
137 }
Jim Grosbache94f1de2010-06-16 22:41:09 +0000138
Bill Wendling0a8fec22012-02-17 02:09:28 +0000139 // Insert a load in place of the PHI and replace all uses.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000140 BasicBlock::iterator InsertPt = P->getIterator();
Bill Wendling76c65212013-01-08 10:51:32 +0000141
David Majnemerfd9f4772015-08-11 01:15:26 +0000142 for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
Bill Wendling76c65212013-01-08 10:51:32 +0000143 /* empty */; // Don't insert before PHI nodes or landingpad instrs.
144
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000145 Value *V = new LoadInst(Slot, P->getName() + ".reload", &*InsertPt);
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000146 P->replaceAllUsesWith(V);
Jim Grosbache94f1de2010-06-16 22:41:09 +0000147
Bill Wendling0a8fec22012-02-17 02:09:28 +0000148 // Delete PHI.
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000149 P->eraseFromParent();
Tanya Lattnerccecbcd2007-07-11 18:41:34 +0000150 return Slot;
151}