1 //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Transforms/Utils/Local.h"
20 #include "llvm/Function.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Type.h"
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 AllocaInst* llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
32 Instruction *AllocaPoint) {
38 // Create a stack slot to hold the value.
41 Slot = new AllocaInst(I.getType(), 0,
42 I.getName()+".reg2mem", AllocaPoint);
44 Function *F = I.getParent()->getParent();
45 Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem",
46 F->getEntryBlock().begin());
49 // Change all of the users of the instruction to read from the stack slot.
50 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
54 // use. Instead insert the load in the predecessor block corresponding
55 // to the incoming value.
57 // Note that if there are multiple edges from a basic block to this PHI
58 // 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.
62 std::map<BasicBlock*, Value*> Loads;
63 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
64 if (PN->getIncomingValue(i) == &I) {
65 Value *&V = Loads[PN->getIncomingBlock(i)];
67 // Insert the load into the predecessor block
68 V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
69 PN->getIncomingBlock(i)->getTerminator());
71 PN->setIncomingValue(i, V);
75 // If this is a normal instruction, just insert a load.
76 Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
77 U->replaceUsesOfWith(&I, V);
82 // 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.
85 BasicBlock::iterator InsertPt;
86 if (!isa<TerminatorInst>(I)) {
90 // We cannot demote invoke instructions to the stack if their normal edge
92 InvokeInst &II = cast<InvokeInst>(I);
93 assert(II.getNormalDest()->getSinglePredecessor() &&
94 "Cannot demote invoke with a critical successor!");
95 InsertPt = II.getNormalDest()->begin();
98 for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
99 /* empty */; // Don't insert before PHI nodes or landingpad instrs.
101 new StoreInst(&I, Slot, InsertPt);
105 /// 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.
108 AllocaInst* llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
109 if (P->use_empty()) {
110 P->eraseFromParent();
114 // Create a stack slot to hold the value.
117 Slot = new AllocaInst(P->getType(), 0,
118 P->getName()+".reg2mem", AllocaPoint);
120 Function *F = P->getParent()->getParent();
121 Slot = new AllocaInst(P->getType(), 0, P->getName()+".reg2mem",
122 F->getEntryBlock().begin());
125 // Iterate over each operand inserting a store in each predecessor.
126 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
127 if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
128 assert(II->getParent() != P->getIncomingBlock(i) &&
129 "Invoke edge not supported yet"); (void)II;
131 new StoreInst(P->getIncomingValue(i), Slot,
132 P->getIncomingBlock(i)->getTerminator());
135 // Insert a load in place of the PHI and replace all uses.
136 Value *V = new LoadInst(Slot, P->getName()+".reload", P);
137 P->replaceAllUsesWith(V);
140 P->eraseFromParent();