blob: c2483ab43c9e4eae86ab3994b9b7534b9733a151 [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//===----------------------------------------------------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00009//
Chris Lattnerab2b3282003-05-29 15:12:27 +000010// This file provide the function DemoteRegToStack(). This function takes a
Chris Lattnerc1a06232004-03-16 23:23:11 +000011// virtual register computed by an Instruction and replaces it with a slot in
Chris Lattnerab2b3282003-05-29 15:12:27 +000012// the stack frame, allocated via alloca. It returns the pointer to the
Chris Lattnerc1a06232004-03-16 23:23:11 +000013// AllocaInst inserted. After this function is called on an instruction, we are
14// guaranteed that the only user of the instruction is a store that is
15// immediately after it.
Chris Lattnerab2b3282003-05-29 15:12:27 +000016//
Vikram S. Adve83e3b652002-12-10 13:07:58 +000017//===----------------------------------------------------------------------===//
18
Chris Lattnera2dc7272004-03-14 02:13:38 +000019#include "llvm/Transforms/Utils/Local.h"
Vikram S. Adve83e3b652002-12-10 13:07:58 +000020#include "llvm/Function.h"
Chris Lattnerc1a06232004-03-16 23:23:11 +000021#include "llvm/Instructions.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000022#include "llvm/Type.h"
Chris Lattnerc68bace2004-04-01 20:28:45 +000023#include <map>
Chris Lattnerf7703df2004-01-09 06:12:26 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerc1a06232004-03-16 23:23:11 +000026/// DemoteRegToStack - This function takes a virtual register computed by an
27/// Instruction and replaces it with a slot in the stack frame, allocated via
28/// alloca. This allows the CFG to be changed around without fear of
29/// invalidating the SSA information for the value. It returns the pointer to
30/// the alloca inserted to create a stack slot for I.
Owen Anderson50dead02009-07-15 23:53:25 +000031AllocaInst* llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000032 Instruction *AllocaPoint) {
33 if (I.use_empty()) {
34 I.eraseFromParent();
35 return 0;
36 }
Jim Grosbachd0e84692010-06-16 22:41:09 +000037
Chris Lattnerc1a06232004-03-16 23:23:11 +000038 // Create a stack slot to hold the value.
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000039 AllocaInst *Slot;
40 if (AllocaPoint) {
Owen Anderson50dead02009-07-15 23:53:25 +000041 Slot = new AllocaInst(I.getType(), 0,
Owen Anderson9adc0ab2009-07-14 23:09:55 +000042 I.getName()+".reg2mem", AllocaPoint);
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000043 } else {
44 Function *F = I.getParent()->getParent();
Owen Anderson50dead02009-07-15 23:53:25 +000045 Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem",
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000046 F->getEntryBlock().begin());
47 }
Jim Grosbachd0e84692010-06-16 22:41:09 +000048
Bill Wendling1bc147b2012-02-17 02:09:28 +000049 // Change all of the users of the instruction to read from the stack slot.
Chris Lattnerc1a06232004-03-16 23:23:11 +000050 while (!I.use_empty()) {
51 Instruction *U = cast<Instruction>(I.use_back());
52 if (PHINode *PN = dyn_cast<PHINode>(U)) {
53 // If this is a PHI node, we can't insert a load of the value before the
Bill Wendling1bc147b2012-02-17 02:09:28 +000054 // use. Instead insert the load in the predecessor block corresponding
Chris Lattnerc1a06232004-03-16 23:23:11 +000055 // to the incoming value.
56 //
57 // Note that if there are multiple edges from a basic block to this PHI
Bill Wendling1bc147b2012-02-17 02:09:28 +000058 // node that we cannot have multiple loads. The problem is that the
59 // resulting PHI node will have multiple values (from each load) coming in
60 // from the same block, which is illegal SSA form. For this reason, we
61 // keep track of and reuse loads we insert.
Chris Lattnerc68bace2004-04-01 20:28:45 +000062 std::map<BasicBlock*, Value*> Loads;
Chris Lattnerc1a06232004-03-16 23:23:11 +000063 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
64 if (PN->getIncomingValue(i) == &I) {
Chris Lattnerc68bace2004-04-01 20:28:45 +000065 Value *&V = Loads[PN->getIncomingBlock(i)];
66 if (V == 0) {
67 // Insert the load into the predecessor block
Jim Grosbachd0e84692010-06-16 22:41:09 +000068 V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
Chris Lattnerc68bace2004-04-01 20:28:45 +000069 PN->getIncomingBlock(i)->getTerminator());
70 }
Chris Lattnerc1a06232004-03-16 23:23:11 +000071 PN->setIncomingValue(i, V);
Chris Lattnerab4536e2003-09-20 14:36:23 +000072 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000073
Chris Lattnerc1a06232004-03-16 23:23:11 +000074 } else {
75 // If this is a normal instruction, just insert a load.
Chris Lattner6d7277b2005-09-27 19:39:00 +000076 Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
Chris Lattnerc1a06232004-03-16 23:23:11 +000077 U->replaceUsesOfWith(&I, V);
Vikram S. Adve83e3b652002-12-10 13:07:58 +000078 }
Chris Lattnerab4536e2003-09-20 14:36:23 +000079 }
Chris Lattner0c7e8e12003-11-06 19:46:29 +000080
Vikram S. Adve83e3b652002-12-10 13:07:58 +000081
Bill Wendling1bc147b2012-02-17 02:09:28 +000082 // Insert stores of the computed value into the stack slot. We have to be
83 // careful if I is an invoke instruction, because we can't insert the store
84 // AFTER the terminator instruction.
Chris Lattner6d7277b2005-09-27 19:39:00 +000085 BasicBlock::iterator InsertPt;
Chris Lattnerc1a06232004-03-16 23:23:11 +000086 if (!isa<TerminatorInst>(I)) {
Chris Lattner6d7277b2005-09-27 19:39:00 +000087 InsertPt = &I;
Chris Lattnerab556982005-10-04 00:44:01 +000088 ++InsertPt;
Chris Lattnerc1a06232004-03-16 23:23:11 +000089 } else {
Chris Lattner6d7277b2005-09-27 19:39:00 +000090 // We cannot demote invoke instructions to the stack if their normal edge
91 // is critical.
92 InvokeInst &II = cast<InvokeInst>(I);
93 assert(II.getNormalDest()->getSinglePredecessor() &&
94 "Cannot demote invoke with a critical successor!");
95 InsertPt = II.getNormalDest()->begin();
Chris Lattnerc1a06232004-03-16 23:23:11 +000096 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000097
Bill Wendlingac101e52011-11-07 19:38:34 +000098 for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
Bill Wendling1bc147b2012-02-17 02:09:28 +000099 /* empty */; // Don't insert before PHI nodes or landingpad instrs.
Chris Lattner6d7277b2005-09-27 19:39:00 +0000100
Bill Wendling1bc147b2012-02-17 02:09:28 +0000101 new StoreInst(&I, Slot, InsertPt);
Chris Lattnerc1a06232004-03-16 23:23:11 +0000102 return Slot;
Vikram S. Adve83e3b652002-12-10 13:07:58 +0000103}
Tanya Lattner08d14d22007-07-11 18:41:34 +0000104
Bill Wendling1bc147b2012-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.
Owen Anderson50dead02009-07-15 23:53:25 +0000108AllocaInst* llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
Tanya Lattner08d14d22007-07-11 18:41:34 +0000109 if (P->use_empty()) {
Jim Grosbachd0e84692010-06-16 22:41:09 +0000110 P->eraseFromParent();
111 return 0;
Tanya Lattner08d14d22007-07-11 18:41:34 +0000112 }
Anton Korobeynikova024e8c2007-10-21 23:05:16 +0000113
Tanya Lattner08d14d22007-07-11 18:41:34 +0000114 // Create a stack slot to hold the value.
Anton Korobeynikova024e8c2007-10-21 23:05:16 +0000115 AllocaInst *Slot;
116 if (AllocaPoint) {
Owen Anderson50dead02009-07-15 23:53:25 +0000117 Slot = new AllocaInst(P->getType(), 0,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000118 P->getName()+".reg2mem", AllocaPoint);
Anton Korobeynikova024e8c2007-10-21 23:05:16 +0000119 } else {
120 Function *F = P->getParent()->getParent();
Owen Anderson50dead02009-07-15 23:53:25 +0000121 Slot = new AllocaInst(P->getType(), 0, P->getName()+".reg2mem",
Anton Korobeynikova024e8c2007-10-21 23:05:16 +0000122 F->getEntryBlock().begin());
123 }
Jim Grosbachd0e84692010-06-16 22:41:09 +0000124
Bill Wendling1bc147b2012-02-17 02:09:28 +0000125 // Iterate over each operand inserting a store in each predecessor.
Tanya Lattner08d14d22007-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 Grosbachd0e84692010-06-16 22:41:09 +0000128 assert(II->getParent() != P->getIncomingBlock(i) &&
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000129 "Invoke edge not supported yet"); (void)II;
Tanya Lattner08d14d22007-07-11 18:41:34 +0000130 }
Jim Grosbachd0e84692010-06-16 22:41:09 +0000131 new StoreInst(P->getIncomingValue(i), Slot,
Tanya Lattner08d14d22007-07-11 18:41:34 +0000132 P->getIncomingBlock(i)->getTerminator());
133 }
Jim Grosbachd0e84692010-06-16 22:41:09 +0000134
Bill Wendling1bc147b2012-02-17 02:09:28 +0000135 // Insert a load in place of the PHI and replace all uses.
Tanya Lattner08d14d22007-07-11 18:41:34 +0000136 Value *V = new LoadInst(Slot, P->getName()+".reload", P);
137 P->replaceAllUsesWith(V);
Jim Grosbachd0e84692010-06-16 22:41:09 +0000138
Bill Wendling1bc147b2012-02-17 02:09:28 +0000139 // Delete PHI.
Tanya Lattner08d14d22007-07-11 18:41:34 +0000140 P->eraseFromParent();
Tanya Lattner08d14d22007-07-11 18:41:34 +0000141 return Slot;
142}