Move EVER MORE stuff over to LLVMContext.
[oota-llvm.git] / lib / Transforms / Utils / DemoteRegToStack.cpp
1 //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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>
24 using 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 ///
32 AllocaInst* llvm::DemoteRegToStack(LLVMContext &Context, 
33                                    Instruction &I, bool VolatileLoads,
34                                    Instruction *AllocaPoint) {
35   if (I.use_empty()) {
36     I.eraseFromParent();
37     return 0;
38   }
39   
40   // Create a stack slot to hold the value.
41   AllocaInst *Slot;
42   if (AllocaPoint) {
43     Slot = new AllocaInst(Context, I.getType(), 0,
44                           I.getName()+".reg2mem", AllocaPoint);
45   } else {
46     Function *F = I.getParent()->getParent();
47     Slot = new AllocaInst(Context, I.getType(), 0, I.getName()+".reg2mem",
48                           F->getEntryBlock().begin());
49   }
50   
51   // Change all of the users of the instruction to read from the stack slot
52   // instead.
53   while (!I.use_empty()) {
54     Instruction *U = cast<Instruction>(I.use_back());
55     if (PHINode *PN = dyn_cast<PHINode>(U)) {
56       // If this is a PHI node, we can't insert a load of the value before the
57       // use.  Instead, insert the load in the predecessor block corresponding
58       // to the incoming value.
59       //
60       // Note that if there are multiple edges from a basic block to this PHI
61       // node that we cannot multiple loads.  The problem is that the resultant
62       // PHI node will have multiple values (from each load) coming in from the
63       // same block, which is illegal SSA form.  For this reason, we keep track
64       // and reuse loads we insert.
65       std::map<BasicBlock*, Value*> Loads;
66       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
67         if (PN->getIncomingValue(i) == &I) {
68           Value *&V = Loads[PN->getIncomingBlock(i)];
69           if (V == 0) {
70             // Insert the load into the predecessor block
71             V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, 
72                              PN->getIncomingBlock(i)->getTerminator());
73           }
74           PN->setIncomingValue(i, V);
75         }
76
77     } else {
78       // If this is a normal instruction, just insert a load.
79       Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
80       U->replaceUsesOfWith(&I, V);
81     }
82   }
83
84
85   // Insert stores of the computed value into the stack slot.  We have to be
86   // careful is I is an invoke instruction though, because we can't insert the
87   // store AFTER the terminator instruction.
88   BasicBlock::iterator InsertPt;
89   if (!isa<TerminatorInst>(I)) {
90     InsertPt = &I;
91     ++InsertPt;
92   } else {
93     // We cannot demote invoke instructions to the stack if their normal edge
94     // is critical.
95     InvokeInst &II = cast<InvokeInst>(I);
96     assert(II.getNormalDest()->getSinglePredecessor() &&
97            "Cannot demote invoke with a critical successor!");
98     InsertPt = II.getNormalDest()->begin();
99   }
100
101   for (; isa<PHINode>(InsertPt); ++InsertPt)
102   /* empty */;   // Don't insert before any PHI nodes.
103   new StoreInst(&I, Slot, InsertPt);
104
105   return Slot;
106 }
107
108
109 /// DemotePHIToStack - This function takes a virtual register computed by a phi
110 /// node and replaces it with a slot in the stack frame, allocated via alloca.
111 /// The phi node is deleted and it returns the pointer to the alloca inserted.
112 AllocaInst* llvm::DemotePHIToStack(LLVMContext &Context, PHINode *P,
113                                    Instruction *AllocaPoint) {
114   if (P->use_empty()) {
115     P->eraseFromParent();    
116     return 0;                
117   }
118
119   // Create a stack slot to hold the value.
120   AllocaInst *Slot;
121   if (AllocaPoint) {
122     Slot = new AllocaInst(Context, P->getType(), 0,
123                           P->getName()+".reg2mem", AllocaPoint);
124   } else {
125     Function *F = P->getParent()->getParent();
126     Slot = new AllocaInst(Context, P->getType(), 0, P->getName()+".reg2mem",
127                           F->getEntryBlock().begin());
128   }
129   
130   // Iterate over each operand, insert store in each predecessor.
131   for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
132     if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
133       assert(II->getParent() != P->getIncomingBlock(i) && 
134              "Invoke edge not supported yet"); II=II;
135     }
136     new StoreInst(P->getIncomingValue(i), Slot, 
137                   P->getIncomingBlock(i)->getTerminator());
138   }
139   
140   // Insert load in place of the phi and replace all uses.
141   Value *V = new LoadInst(Slot, P->getName()+".reload", P);
142   P->replaceAllUsesWith(V);
143   
144   // Delete phi.
145   P->eraseFromParent();
146   
147   return Slot;
148 }