LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DemoteRegToStack.cpp

Go to the documentation of this file.
00001 //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 // 
00010 // This file provide the function DemoteRegToStack().  This function takes a
00011 // virtual register computed by an Instruction and replaces it with a slot in
00012 // the stack frame, allocated via alloca. It returns the pointer to the
00013 // AllocaInst inserted.  After this function is called on an instruction, we are
00014 // guaranteed that the only user of the instruction is a store that is
00015 // immediately after it.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "llvm/Transforms/Utils/Local.h"
00020 #include "llvm/Function.h"
00021 #include "llvm/Instructions.h"
00022 #include <map>
00023 using namespace llvm;
00024 
00025 /// DemoteRegToStack - This function takes a virtual register computed by an
00026 /// Instruction and replaces it with a slot in the stack frame, allocated via
00027 /// alloca.  This allows the CFG to be changed around without fear of
00028 /// invalidating the SSA information for the value.  It returns the pointer to
00029 /// the alloca inserted to create a stack slot for I.
00030 ///
00031 AllocaInst* llvm::DemoteRegToStack(Instruction &I) {
00032   if (I.use_empty()) return 0;                // nothing to do!
00033 
00034   // Create a stack slot to hold the value.
00035   Function *F = I.getParent()->getParent();
00036   AllocaInst *Slot = new AllocaInst(I.getType(), 0, I.getName(),
00037                                     F->getEntryBlock().begin());
00038 
00039   // Change all of the users of the instruction to read from the stack slot
00040   // instead.
00041   while (!I.use_empty()) {
00042     Instruction *U = cast<Instruction>(I.use_back());
00043     if (PHINode *PN = dyn_cast<PHINode>(U)) {
00044       // If this is a PHI node, we can't insert a load of the value before the
00045       // use.  Instead, insert the load in the predecessor block corresponding
00046       // to the incoming value.
00047       //
00048       // Note that if there are multiple edges from a basic block to this PHI
00049       // node that we cannot multiple loads.  The problem is that the resultant
00050       // PHI node will have multiple values (from each load) coming in from the
00051       // same block, which is illegal SSA form.  For this reason, we keep track
00052       // and reuse loads we insert.
00053       std::map<BasicBlock*, Value*> Loads;
00054       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
00055         if (PN->getIncomingValue(i) == &I) {
00056           Value *&V = Loads[PN->getIncomingBlock(i)];
00057           if (V == 0) {
00058             // Insert the load into the predecessor block
00059             V = new LoadInst(Slot, I.getName()+".reload",
00060                              PN->getIncomingBlock(i)->getTerminator());
00061           }
00062           PN->setIncomingValue(i, V);
00063         }
00064 
00065     } else {
00066       // If this is a normal instruction, just insert a load.
00067       Value *V = new LoadInst(Slot, I.getName()+".reload", U);
00068       U->replaceUsesOfWith(&I, V);
00069     }
00070   }
00071 
00072 
00073   // Insert stores of the computed value into the stack slot.  We have to be
00074   // careful is I is an invoke instruction though, because we can't insert the
00075   // store AFTER the terminator instruction.
00076   if (!isa<TerminatorInst>(I)) {
00077     BasicBlock::iterator InsertPt = &I;
00078     for (++InsertPt; isa<PHINode>(InsertPt); ++InsertPt)
00079       /* empty */;   // Don't insert before any PHI nodes.
00080     new StoreInst(&I, Slot, InsertPt);
00081   } else {
00082     // FIXME: We cannot yet demote invoke instructions to the stack, because
00083     // doing so would require breaking critical edges.  This should be fixed
00084     // eventually.
00085     assert(0 &&
00086            "Cannot demote the value computed by an invoke instruction yet!");
00087   }
00088 
00089   return Slot;
00090 }