blob: a92a066469ca1e461dfec460302d754ed45dece2 [file] [log] [blame]
Chris Lattner38cd27e2003-11-06 19:46:29 +00001//===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
Vikram S. Advec864ab12002-12-10 13:07:58 +00002//
John Criswell482202a2003-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.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerf789f292003-05-29 15:12:27 +000010// This file provide the function DemoteRegToStack(). This function takes a
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000011// virtual register computed by an Instruction and replaces it with a slot in
Chris Lattnerf789f292003-05-29 15:12:27 +000012// the stack frame, allocated via alloca. It returns the pointer to the
Chris Lattnerbb1a2cc2004-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 Lattnerf789f292003-05-29 15:12:27 +000016//
Vikram S. Advec864ab12002-12-10 13:07:58 +000017//===----------------------------------------------------------------------===//
18
Chris Lattner8eebc492004-03-14 02:13:38 +000019#include "llvm/Transforms/Utils/Local.h"
Vikram S. Advec864ab12002-12-10 13:07:58 +000020#include "llvm/Function.h"
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000021#include "llvm/Instructions.h"
Chris Lattnerc24019c2004-04-01 20:28:45 +000022#include <map>
Chris Lattnerdf3c3422004-01-09 06:12:26 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000025/// DemoteRegToStack - This function takes a virtual register computed by an
26/// Instruction and replaces it with a slot in the stack frame, allocated via
27/// alloca. This allows the CFG to be changed around without fear of
28/// invalidating the SSA information for the value. It returns the pointer to
29/// the alloca inserted to create a stack slot for I.
30///
31AllocaInst* llvm::DemoteRegToStack(Instruction &I) {
32 if (I.use_empty()) return 0; // nothing to do!
Vikram S. Advec864ab12002-12-10 13:07:58 +000033
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000034 // Create a stack slot to hold the value.
35 Function *F = I.getParent()->getParent();
36 AllocaInst *Slot = new AllocaInst(I.getType(), 0, I.getName(),
37 F->getEntryBlock().begin());
Vikram S. Advec864ab12002-12-10 13:07:58 +000038
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000039 // Change all of the users of the instruction to read from the stack slot
40 // instead.
41 while (!I.use_empty()) {
42 Instruction *U = cast<Instruction>(I.use_back());
43 if (PHINode *PN = dyn_cast<PHINode>(U)) {
44 // If this is a PHI node, we can't insert a load of the value before the
45 // use. Instead, insert the load in the predecessor block corresponding
46 // to the incoming value.
47 //
48 // Note that if there are multiple edges from a basic block to this PHI
Chris Lattnerc24019c2004-04-01 20:28:45 +000049 // node that we cannot multiple loads. The problem is that the resultant
50 // PHI node will have multiple values (from each load) coming in from the
51 // same block, which is illegal SSA form. For this reason, we keep track
52 // and reuse loads we insert.
53 std::map<BasicBlock*, Value*> Loads;
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000054 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
55 if (PN->getIncomingValue(i) == &I) {
Chris Lattnerc24019c2004-04-01 20:28:45 +000056 Value *&V = Loads[PN->getIncomingBlock(i)];
57 if (V == 0) {
58 // Insert the load into the predecessor block
59 V = new LoadInst(Slot, I.getName()+".reload",
60 PN->getIncomingBlock(i)->getTerminator());
61 }
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000062 PN->setIncomingValue(i, V);
Chris Lattner4413e432003-09-20 14:36:23 +000063 }
Vikram S. Advec864ab12002-12-10 13:07:58 +000064
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000065 } else {
66 // If this is a normal instruction, just insert a load.
67 Value *V = new LoadInst(Slot, I.getName()+".reload", U);
68 U->replaceUsesOfWith(&I, V);
Vikram S. Advec864ab12002-12-10 13:07:58 +000069 }
Chris Lattner4413e432003-09-20 14:36:23 +000070 }
Chris Lattner38cd27e2003-11-06 19:46:29 +000071
Vikram S. Advec864ab12002-12-10 13:07:58 +000072
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000073 // Insert stores of the computed value into the stack slot. We have to be
74 // careful is I is an invoke instruction though, because we can't insert the
75 // store AFTER the terminator instruction.
76 if (!isa<TerminatorInst>(I)) {
77 BasicBlock::iterator InsertPt = &I;
78 for (++InsertPt; isa<PHINode>(InsertPt); ++InsertPt)
79 /* empty */; // Don't insert before any PHI nodes.
80 new StoreInst(&I, Slot, InsertPt);
81 } else {
82 // FIXME: We cannot yet demote invoke instructions to the stack, because
83 // doing so would require breaking critical edges. This should be fixed
84 // eventually.
85 assert(0 &&
86 "Cannot demote the value computed by an invoke instruction yet!");
87 }
Vikram S. Advec864ab12002-12-10 13:07:58 +000088
Chris Lattnerbb1a2cc2004-03-16 23:23:11 +000089 return Slot;
Vikram S. Advec864ab12002-12-10 13:07:58 +000090}