blob: df332b289d0d4aa4a2c97f46c65d40963fdc3272 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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.
31///
Chris Lattner6d7277b2005-09-27 19:39:00 +000032AllocaInst* llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads) {
Chris Lattnerc1a06232004-03-16 23:23:11 +000033 if (I.use_empty()) return 0; // nothing to do!
Vikram S. Adve83e3b652002-12-10 13:07:58 +000034
Chris Lattnerc1a06232004-03-16 23:23:11 +000035 // Create a stack slot to hold the value.
36 Function *F = I.getParent()->getParent();
37 AllocaInst *Slot = new AllocaInst(I.getType(), 0, I.getName(),
38 F->getEntryBlock().begin());
Vikram S. Adve83e3b652002-12-10 13:07:58 +000039
Chris Lattnerc1a06232004-03-16 23:23:11 +000040 // Change all of the users of the instruction to read from the stack slot
41 // instead.
42 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
46 // use. Instead, insert the load in the predecessor block corresponding
47 // to the incoming value.
48 //
49 // Note that if there are multiple edges from a basic block to this PHI
Chris Lattnerc68bace2004-04-01 20:28:45 +000050 // node that we cannot multiple loads. The problem is that the resultant
51 // PHI node will have multiple values (from each load) coming in from the
52 // same block, which is illegal SSA form. For this reason, we keep track
53 // and reuse loads we insert.
54 std::map<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
Chris Lattner6d7277b2005-09-27 19:39:00 +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
Chris Lattnerc1a06232004-03-16 23:23:11 +000074 // Insert stores of the computed value into the stack slot. We have to be
75 // careful is I is an invoke instruction though, because we can't insert the
76 // store 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 // We cannot demote invoke instructions to the stack if their normal edge
83 // is critical.
84 InvokeInst &II = cast<InvokeInst>(I);
85 assert(II.getNormalDest()->getSinglePredecessor() &&
86 "Cannot demote invoke with a critical successor!");
87 InsertPt = II.getNormalDest()->begin();
Chris Lattnerc1a06232004-03-16 23:23:11 +000088 }
Vikram S. Adve83e3b652002-12-10 13:07:58 +000089
Chris Lattnerab556982005-10-04 00:44:01 +000090 for (; isa<PHINode>(InsertPt); ++InsertPt)
Chris Lattner6d7277b2005-09-27 19:39:00 +000091 /* empty */; // Don't insert before any PHI nodes.
92 new StoreInst(&I, Slot, InsertPt);
93
Chris Lattnerc1a06232004-03-16 23:23:11 +000094 return Slot;
Vikram S. Adve83e3b652002-12-10 13:07:58 +000095}
Tanya Lattner08d14d22007-07-11 18:41:34 +000096
97
98/// DemotePHIToStack - This function takes a virtual register computed by a phi
99/// node and replaces it with a slot in the stack frame, allocated via alloca.
100/// The phi node is deleted and it returns the pointer to the alloca inserted.
101AllocaInst* llvm::DemotePHIToStack(PHINode *P) {
102 if (P->use_empty()) {
103 P->eraseFromParent();
104 return 0;
105 }
106
107 // Create a stack slot to hold the value.
108 Function *F = P->getParent()->getParent();
109 AllocaInst *Slot = new AllocaInst(P->getType(), 0, P->getName(),
110 F->getEntryBlock().begin());
111
112 // Iterate over each operand, insert store in each predecessor.
113 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
114 if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
115 assert(II->getParent() != P->getIncomingBlock(i) &&
116 "Invoke edge not supported yet");
117 }
118 new StoreInst(P->getIncomingValue(i), Slot,
119 P->getIncomingBlock(i)->getTerminator());
120 }
121
122 // Insert load in place of the phi and replace all uses.
123 BasicBlock::iterator InsertPt;
124 for (InsertPt = P->getParent()->getInstList().begin();
125 isa<PHINode>(InsertPt); ++InsertPt);
126 Value *V = new LoadInst(Slot, P->getName()+".reload", P);
127 P->replaceAllUsesWith(V);
128
129 // Delete phi.
130 P->eraseFromParent();
131
132 return Slot;
133}