blob: db525cdc24d80d0f4c7866d72258ebbffe76966f [file] [log] [blame]
Chris Lattner0c7e8e12003-11-06 19:46:29 +00001//===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Vikram S. Adve83e3b652002-12-10 13:07:58 +00009
Chad Rosier1e454872013-02-05 18:23:10 +000010#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnera2dc7272004-03-14 02:13:38 +000011#include "llvm/Transforms/Utils/Local.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000012#include "llvm/ADT/DenseMap.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000013#include "llvm/IR/Function.h"
14#include "llvm/IR/Instructions.h"
15#include "llvm/IR/Type.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000016using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000017
Chris Lattnerc1a06232004-03-16 23:23:11 +000018/// 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 Wendling3106b8b2012-02-17 02:12:54 +000023AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000024 Instruction *AllocaPoint) {
25 if (I.use_empty()) {
26 I.eraseFromParent();
27 return 0;
28 }
Jim Grosbachd0e84692010-06-16 22:41:09 +000029
Chris Lattnerc1a06232004-03-16 23:23:11 +000030 // Create a stack slot to hold the value.
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000031 AllocaInst *Slot;
32 if (AllocaPoint) {
Owen Anderson50dead02009-07-15 23:53:25 +000033 Slot = new AllocaInst(I.getType(), 0,
Owen Anderson9adc0ab2009-07-14 23:09:55 +000034 I.getName()+".reg2mem", AllocaPoint);
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000035 } else {
36 Function *F = I.getParent()->getParent();
Owen Anderson50dead02009-07-15 23:53:25 +000037 Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem",
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000038 F->getEntryBlock().begin());
39 }
Jim Grosbachd0e84692010-06-16 22:41:09 +000040
Bill Wendling1bc147b2012-02-17 02:09:28 +000041 // Change all of the users of the instruction to read from the stack slot.
Chris Lattnerc1a06232004-03-16 23:23:11 +000042 while (!I.use_empty()) {
43 Instruction *U = cast<Instruction>(I.use_back());
44 if (PHINode *PN = dyn_cast<PHINode>(U)) {
45 // If this is a PHI node, we can't insert a load of the value before the
Bill Wendling1bc147b2012-02-17 02:09:28 +000046 // use. Instead insert the load in the predecessor block corresponding
Chris Lattnerc1a06232004-03-16 23:23:11 +000047 // to the incoming value.
48 //
49 // Note that if there are multiple edges from a basic block to this PHI
Bill Wendling1bc147b2012-02-17 02:09:28 +000050 // node that we cannot have multiple loads. The problem is that the
51 // resulting PHI node will have multiple values (from each load) coming in
52 // from the same block, which is illegal SSA form. For this reason, we
53 // keep track of and reuse loads we insert.
Bill Wendling3106b8b2012-02-17 02:12:54 +000054 DenseMap<BasicBlock*, Value*> Loads;
Chris Lattnerc1a06232004-03-16 23:23:11 +000055 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
56 if (PN->getIncomingValue(i) == &I) {
Chris Lattnerc68bace2004-04-01 20:28:45 +000057 Value *&V = Loads[PN->getIncomingBlock(i)];
58 if (V == 0) {
59 // Insert the load into the predecessor block
Jim Grosbachd0e84692010-06-16 22:41:09 +000060 V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
Chris Lattnerc68bace2004-04-01 20:28:45 +000061 PN->getIncomingBlock(i)->getTerminator());
62 }
Chris Lattnerc1a06232004-03-16 23:23:11 +000063 PN->setIncomingValue(i, V);
Chris Lattnerab4536e2003-09-20 14:36:23 +000064 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000065
Chris Lattnerc1a06232004-03-16 23:23:11 +000066 } else {
67 // If this is a normal instruction, just insert a load.
Chris Lattner6d7277b2005-09-27 19:39:00 +000068 Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
Chris Lattnerc1a06232004-03-16 23:23:11 +000069 U->replaceUsesOfWith(&I, V);
Vikram S. Adve83e3b652002-12-10 13:07:58 +000070 }
Chris Lattnerab4536e2003-09-20 14:36:23 +000071 }
Chris Lattner0c7e8e12003-11-06 19:46:29 +000072
Vikram S. Adve83e3b652002-12-10 13:07:58 +000073
Bill Wendling1bc147b2012-02-17 02:09:28 +000074 // Insert stores of the computed value into the stack slot. We have to be
75 // careful if I is an invoke instruction, because we can't insert the store
76 // AFTER the terminator instruction.
Chris Lattner6d7277b2005-09-27 19:39:00 +000077 BasicBlock::iterator InsertPt;
Chris Lattnerc1a06232004-03-16 23:23:11 +000078 if (!isa<TerminatorInst>(I)) {
Chris Lattner6d7277b2005-09-27 19:39:00 +000079 InsertPt = &I;
Chris Lattnerab556982005-10-04 00:44:01 +000080 ++InsertPt;
Chris Lattnerc1a06232004-03-16 23:23:11 +000081 } else {
Chris Lattner6d7277b2005-09-27 19:39:00 +000082 InvokeInst &II = cast<InvokeInst>(I);
Chad Rosier1e454872013-02-05 18:23:10 +000083 if (II.getNormalDest()->getSinglePredecessor())
84 InsertPt = II.getNormalDest()->getFirstInsertionPt();
85 else {
86 // We cannot demote invoke instructions to the stack if their normal edge
87 // is critical. Therefore, split the critical edge and insert the store
88 // in the newly created basic block.
89 unsigned SuccNum = GetSuccessorNumber(I.getParent(), II.getNormalDest());
90 TerminatorInst *TI = &cast<TerminatorInst>(I);
91 assert (isCriticalEdge(TI, SuccNum) &&
92 "Expected a critical edge!");
93 BasicBlock *BB = SplitCriticalEdge(TI, SuccNum);
94 assert (BB && "Unable to split critical edge.");
95 InsertPt = BB->getFirstInsertionPt();
96 }
Chris Lattnerc1a06232004-03-16 23:23:11 +000097 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000098
Bill Wendlingac101e52011-11-07 19:38:34 +000099 for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
Bill Wendling1bc147b2012-02-17 02:09:28 +0000100 /* empty */; // Don't insert before PHI nodes or landingpad instrs.
Chris Lattner6d7277b2005-09-27 19:39:00 +0000101
Bill Wendling1bc147b2012-02-17 02:09:28 +0000102 new StoreInst(&I, Slot, InsertPt);
Chris Lattnerc1a06232004-03-16 23:23:11 +0000103 return Slot;
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000104}
Tanya Lattner08d14d22007-07-11 18:41:34 +0000105
Bill Wendling1bc147b2012-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 Wendling3106b8b2012-02-17 02:12:54 +0000109AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
Tanya Lattner08d14d22007-07-11 18:41:34 +0000110 if (P->use_empty()) {
Jim Grosbachd0e84692010-06-16 22:41:09 +0000111 P->eraseFromParent();
112 return 0;
Tanya Lattner08d14d22007-07-11 18:41:34 +0000113 }
Anton Korobeynikova024e8c2007-10-21 23:05:16 +0000114
Tanya Lattner08d14d22007-07-11 18:41:34 +0000115 // Create a stack slot to hold the value.
Anton Korobeynikova024e8c2007-10-21 23:05:16 +0000116 AllocaInst *Slot;
117 if (AllocaPoint) {
Owen Anderson50dead02009-07-15 23:53:25 +0000118 Slot = new AllocaInst(P->getType(), 0,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000119 P->getName()+".reg2mem", AllocaPoint);
Anton Korobeynikova024e8c2007-10-21 23:05:16 +0000120 } else {
121 Function *F = P->getParent()->getParent();
Owen Anderson50dead02009-07-15 23:53:25 +0000122 Slot = new AllocaInst(P->getType(), 0, P->getName()+".reg2mem",
Anton Korobeynikova024e8c2007-10-21 23:05:16 +0000123 F->getEntryBlock().begin());
124 }
Jim Grosbachd0e84692010-06-16 22:41:09 +0000125
Bill Wendling1bc147b2012-02-17 02:09:28 +0000126 // Iterate over each operand inserting a store in each predecessor.
Tanya Lattner08d14d22007-07-11 18:41:34 +0000127 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
128 if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
Jim Grosbachd0e84692010-06-16 22:41:09 +0000129 assert(II->getParent() != P->getIncomingBlock(i) &&
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000130 "Invoke edge not supported yet"); (void)II;
Tanya Lattner08d14d22007-07-11 18:41:34 +0000131 }
Jim Grosbachd0e84692010-06-16 22:41:09 +0000132 new StoreInst(P->getIncomingValue(i), Slot,
Tanya Lattner08d14d22007-07-11 18:41:34 +0000133 P->getIncomingBlock(i)->getTerminator());
134 }
Jim Grosbachd0e84692010-06-16 22:41:09 +0000135
Bill Wendling1bc147b2012-02-17 02:09:28 +0000136 // Insert a load in place of the PHI and replace all uses.
Bill Wendling3cc48a02013-01-08 10:51:32 +0000137 BasicBlock::iterator InsertPt = P;
138
139 for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
140 /* empty */; // Don't insert before PHI nodes or landingpad instrs.
141
142 Value *V = new LoadInst(Slot, P->getName()+".reload", InsertPt);
Tanya Lattner08d14d22007-07-11 18:41:34 +0000143 P->replaceAllUsesWith(V);
Jim Grosbachd0e84692010-06-16 22:41:09 +0000144
Bill Wendling1bc147b2012-02-17 02:09:28 +0000145 // Delete PHI.
Tanya Lattner08d14d22007-07-11 18:41:34 +0000146 P->eraseFromParent();
Tanya Lattner08d14d22007-07-11 18:41:34 +0000147 return Slot;
148}