This code was both incredibly complex and incredibly broken. Fix it.
[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 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 // 
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 using namespace llvm;
23
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.
29 ///
30 AllocaInst* llvm::DemoteRegToStack(Instruction &I) {
31   if (I.use_empty()) return 0;                // nothing to do!
32
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());
37
38   // Change all of the users of the instruction to read from the stack slot
39   // instead.
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.
46       //
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);
56         }
57
58     } else {
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);
62     }
63   }
64
65
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);
74   } else {
75     // FIXME: We cannot yet demote invoke instructions to the stack, because
76     // doing so would require breaking critical edges.  This should be fixed
77     // eventually.
78     assert(0 &&
79            "Cannot demote the value computed by an invoke instruction yet!");
80   }
81
82   return Slot;
83 }