1 //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
3 // The LLVM Compiler Infrastructure
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.
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"
24 /// DemoteRegToStack - This function takes a virtual register computed by an
25 /// Instruction and replaces it with a slot in the stack frame, allocated via
26 /// alloca. This allows the CFG to be changed around without fear of
27 /// invalidating the SSA information for the value. It returns the pointer to
28 /// the alloca inserted to create a stack slot for I.
30 AllocaInst* llvm::DemoteRegToStack(Instruction &I) {
31 if (I.use_empty()) return 0; // nothing to do!
33 // Create a stack slot to hold the value.
34 Function *F = I.getParent()->getParent();
35 AllocaInst *Slot = new AllocaInst(I.getType(), 0, I.getName(),
36 F->getEntryBlock().begin());
38 // Change all of the users of the instruction to read from the stack slot
40 while (!I.use_empty()) {
41 Instruction *U = cast<Instruction>(I.use_back());
42 if (PHINode *PN = dyn_cast<PHINode>(U)) {
43 // If this is a PHI node, we can't insert a load of the value before the
44 // use. Instead, insert the load in the predecessor block corresponding
45 // to the incoming value.
47 // Note that if there are multiple edges from a basic block to this PHI
48 // node that we'll insert multiple loads. Since DemoteRegToStack requires
49 // a mem2reg pass after it (to produce reasonable code), we don't care.
50 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
51 if (PN->getIncomingValue(i) == &I) {
52 // Insert the load into the predecessor block
53 Value *V = new LoadInst(Slot, I.getName()+".reload",
54 PN->getIncomingBlock(i)->getTerminator());
55 PN->setIncomingValue(i, V);
59 // If this is a normal instruction, just insert a load.
60 Value *V = new LoadInst(Slot, I.getName()+".reload", U);
61 U->replaceUsesOfWith(&I, V);
66 // Insert stores of the computed value into the stack slot. We have to be
67 // careful is I is an invoke instruction though, because we can't insert the
68 // store AFTER the terminator instruction.
69 if (!isa<TerminatorInst>(I)) {
70 BasicBlock::iterator InsertPt = &I;
71 for (++InsertPt; isa<PHINode>(InsertPt); ++InsertPt)
72 /* empty */; // Don't insert before any PHI nodes.
73 new StoreInst(&I, Slot, InsertPt);
75 // FIXME: We cannot yet demote invoke instructions to the stack, because
76 // doing so would require breaking critical edges. This should be fixed
79 "Cannot demote the value computed by an invoke instruction yet!");