blob: 8e82a02caa6902caa0e7967b77823fa55cf509b4 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file provide the function DemoteRegToStack(). This function takes a
11// virtual register computed by an Instruction and replaces it with a slot in
12// the stack frame, allocated via alloca. It returns the pointer to the
13// 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.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Transforms/Utils/Local.h"
20#include "llvm/Function.h"
21#include "llvm/Instructions.h"
22#include "llvm/Type.h"
23#include <map>
24using namespace llvm;
25
26/// 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///
Owen Anderson140166d2009-07-15 23:53:25 +000032AllocaInst* llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000033 Instruction *AllocaPoint) {
34 if (I.use_empty()) {
35 I.eraseFromParent();
36 return 0;
37 }
Jim Grosbach116e6332010-06-16 22:41:09 +000038
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 // Create a stack slot to hold the value.
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000040 AllocaInst *Slot;
41 if (AllocaPoint) {
Owen Anderson140166d2009-07-15 23:53:25 +000042 Slot = new AllocaInst(I.getType(), 0,
Owen Anderson9f5b2aa2009-07-14 23:09:55 +000043 I.getName()+".reg2mem", AllocaPoint);
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000044 } else {
45 Function *F = I.getParent()->getParent();
Owen Anderson140166d2009-07-15 23:53:25 +000046 Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem",
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000047 F->getEntryBlock().begin());
48 }
Jim Grosbach116e6332010-06-16 22:41:09 +000049
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 // Change all of the users of the instruction to read from the stack slot
51 // instead.
52 while (!I.use_empty()) {
53 Instruction *U = cast<Instruction>(I.use_back());
54 if (PHINode *PN = dyn_cast<PHINode>(U)) {
55 // If this is a PHI node, we can't insert a load of the value before the
56 // use. Instead, insert the load in the predecessor block corresponding
57 // to the incoming value.
58 //
59 // Note that if there are multiple edges from a basic block to this PHI
60 // node that we cannot multiple loads. The problem is that the resultant
61 // PHI node will have multiple values (from each load) coming in from the
62 // same block, which is illegal SSA form. For this reason, we keep track
63 // and reuse loads we insert.
64 std::map<BasicBlock*, Value*> Loads;
65 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
66 if (PN->getIncomingValue(i) == &I) {
67 Value *&V = Loads[PN->getIncomingBlock(i)];
68 if (V == 0) {
69 // Insert the load into the predecessor block
Jim Grosbach116e6332010-06-16 22:41:09 +000070 V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 PN->getIncomingBlock(i)->getTerminator());
72 }
73 PN->setIncomingValue(i, V);
74 }
75
76 } else {
77 // If this is a normal instruction, just insert a load.
78 Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
79 U->replaceUsesOfWith(&I, V);
80 }
81 }
82
83
84 // Insert stores of the computed value into the stack slot. We have to be
85 // careful is I is an invoke instruction though, because we can't insert the
86 // store AFTER the terminator instruction.
87 BasicBlock::iterator InsertPt;
88 if (!isa<TerminatorInst>(I)) {
89 InsertPt = &I;
90 ++InsertPt;
91 } else {
92 // We cannot demote invoke instructions to the stack if their normal edge
93 // is critical.
94 InvokeInst &II = cast<InvokeInst>(I);
95 assert(II.getNormalDest()->getSinglePredecessor() &&
96 "Cannot demote invoke with a critical successor!");
97 InsertPt = II.getNormalDest()->begin();
98 }
99
100 for (; isa<PHINode>(InsertPt); ++InsertPt)
101 /* empty */; // Don't insert before any PHI nodes.
102 new StoreInst(&I, Slot, InsertPt);
103
104 return Slot;
105}
106
107
108/// DemotePHIToStack - This function takes a virtual register computed by a phi
109/// node and replaces it with a slot in the stack frame, allocated via alloca.
110/// The phi node is deleted and it returns the pointer to the alloca inserted.
Owen Anderson140166d2009-07-15 23:53:25 +0000111AllocaInst* llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 if (P->use_empty()) {
Jim Grosbach116e6332010-06-16 22:41:09 +0000113 P->eraseFromParent();
114 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 }
Anton Korobeynikov3224cda2007-10-21 23:05:16 +0000116
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 // Create a stack slot to hold the value.
Anton Korobeynikov3224cda2007-10-21 23:05:16 +0000118 AllocaInst *Slot;
119 if (AllocaPoint) {
Owen Anderson140166d2009-07-15 23:53:25 +0000120 Slot = new AllocaInst(P->getType(), 0,
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000121 P->getName()+".reg2mem", AllocaPoint);
Anton Korobeynikov3224cda2007-10-21 23:05:16 +0000122 } else {
123 Function *F = P->getParent()->getParent();
Owen Anderson140166d2009-07-15 23:53:25 +0000124 Slot = new AllocaInst(P->getType(), 0, P->getName()+".reg2mem",
Anton Korobeynikov3224cda2007-10-21 23:05:16 +0000125 F->getEntryBlock().begin());
126 }
Jim Grosbach116e6332010-06-16 22:41:09 +0000127
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 // Iterate over each operand, insert store in each predecessor.
129 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
130 if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
Jim Grosbach116e6332010-06-16 22:41:09 +0000131 assert(II->getParent() != P->getIncomingBlock(i) &&
Chris Lattner72290d52008-06-21 19:49:01 +0000132 "Invoke edge not supported yet"); II=II;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 }
Jim Grosbach116e6332010-06-16 22:41:09 +0000134 new StoreInst(P->getIncomingValue(i), Slot,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 P->getIncomingBlock(i)->getTerminator());
136 }
Jim Grosbach116e6332010-06-16 22:41:09 +0000137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 // Insert load in place of the phi and replace all uses.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 Value *V = new LoadInst(Slot, P->getName()+".reload", P);
140 P->replaceAllUsesWith(V);
Jim Grosbach116e6332010-06-16 22:41:09 +0000141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 // Delete phi.
143 P->eraseFromParent();
Jim Grosbach116e6332010-06-16 22:41:09 +0000144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 return Slot;
146}