Fix some grammar-os and formatting.
[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 AllocaInst* llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
32                                    Instruction *AllocaPoint) {
33   if (I.use_empty()) {
34     I.eraseFromParent();
35     return 0;
36   }
37
38   // Create a stack slot to hold the value.
39   AllocaInst *Slot;
40   if (AllocaPoint) {
41     Slot = new AllocaInst(I.getType(), 0,
42                           I.getName()+".reg2mem", AllocaPoint);
43   } else {
44     Function *F = I.getParent()->getParent();
45     Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem",
46                           F->getEntryBlock().begin());
47   }
48
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.
56       //
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)];
66           if (V == 0) {
67             // Insert the load into the predecessor block
68             V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
69                              PN->getIncomingBlock(i)->getTerminator());
70           }
71           PN->setIncomingValue(i, V);
72         }
73
74     } else {
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);
78     }
79   }
80
81
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)) {
87     InsertPt = &I;
88     ++InsertPt;
89   } else {
90     // We cannot demote invoke instructions to the stack if their normal edge
91     // is critical.
92     InvokeInst &II = cast<InvokeInst>(I);
93     assert(II.getNormalDest()->getSinglePredecessor() &&
94            "Cannot demote invoke with a critical successor!");
95     InsertPt = II.getNormalDest()->begin();
96   }
97
98   for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
99     /* empty */;   // Don't insert before PHI nodes or landingpad instrs.
100
101   new StoreInst(&I, Slot, InsertPt);
102   return Slot;
103 }
104
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();
111     return 0;
112   }
113
114   // Create a stack slot to hold the value.
115   AllocaInst *Slot;
116   if (AllocaPoint) {
117     Slot = new AllocaInst(P->getType(), 0,
118                           P->getName()+".reg2mem", AllocaPoint);
119   } else {
120     Function *F = P->getParent()->getParent();
121     Slot = new AllocaInst(P->getType(), 0, P->getName()+".reg2mem",
122                           F->getEntryBlock().begin());
123   }
124
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;
130     }
131     new StoreInst(P->getIncomingValue(i), Slot,
132                   P->getIncomingBlock(i)->getTerminator());
133   }
134
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);
138
139   // Delete PHI.
140   P->eraseFromParent();
141   return Slot;
142 }