LLVM  mainline
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 is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00011 #include "llvm/ADT/DenseMap.h"
00012 #include "llvm/Analysis/CFG.h"
00013 #include "llvm/IR/Function.h"
00014 #include "llvm/IR/Instructions.h"
00015 #include "llvm/IR/Type.h"
00016 #include "llvm/Transforms/Utils/Local.h"
00017 using namespace llvm;
00018 
00019 /// DemoteRegToStack - This function takes a virtual register computed by an
00020 /// Instruction and replaces it with a slot in the stack frame, allocated via
00021 /// alloca.  This allows the CFG to be changed around without fear of
00022 /// invalidating the SSA information for the value.  It returns the pointer to
00023 /// the alloca inserted to create a stack slot for I.
00024 AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
00025                                    Instruction *AllocaPoint) {
00026   if (I.use_empty()) {
00027     I.eraseFromParent();
00028     return nullptr;
00029   }
00030 
00031   // Create a stack slot to hold the value.
00032   AllocaInst *Slot;
00033   if (AllocaPoint) {
00034     Slot = new AllocaInst(I.getType(), nullptr,
00035                           I.getName()+".reg2mem", AllocaPoint);
00036   } else {
00037     Function *F = I.getParent()->getParent();
00038     Slot = new AllocaInst(I.getType(), nullptr, I.getName() + ".reg2mem",
00039                           &F->getEntryBlock().front());
00040   }
00041 
00042   // We cannot demote invoke instructions to the stack if their normal edge
00043   // is critical. Therefore, split the critical edge and create a basic block
00044   // into which the store can be inserted.
00045   if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
00046     if (!II->getNormalDest()->getSinglePredecessor()) {
00047       unsigned SuccNum = GetSuccessorNumber(II->getParent(), II->getNormalDest());
00048       assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
00049       BasicBlock *BB = SplitCriticalEdge(II, SuccNum);
00050       assert(BB && "Unable to split critical edge.");
00051       (void)BB;
00052     }
00053   }
00054 
00055   // Change all of the users of the instruction to read from the stack slot.
00056   while (!I.use_empty()) {
00057     Instruction *U = cast<Instruction>(I.user_back());
00058     if (PHINode *PN = dyn_cast<PHINode>(U)) {
00059       // If this is a PHI node, we can't insert a load of the value before the
00060       // use.  Instead insert the load in the predecessor block corresponding
00061       // to the incoming value.
00062       //
00063       // Note that if there are multiple edges from a basic block to this PHI
00064       // node that we cannot have multiple loads. The problem is that the
00065       // resulting PHI node will have multiple values (from each load) coming in
00066       // from the same block, which is illegal SSA form. For this reason, we
00067       // keep track of and reuse loads we insert.
00068       DenseMap<BasicBlock*, Value*> Loads;
00069       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
00070         if (PN->getIncomingValue(i) == &I) {
00071           Value *&V = Loads[PN->getIncomingBlock(i)];
00072           if (!V) {
00073             // Insert the load into the predecessor block
00074             V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
00075                              PN->getIncomingBlock(i)->getTerminator());
00076           }
00077           PN->setIncomingValue(i, V);
00078         }
00079 
00080     } else {
00081       // If this is a normal instruction, just insert a load.
00082       Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
00083       U->replaceUsesOfWith(&I, V);
00084     }
00085   }
00086 
00087   // Insert stores of the computed value into the stack slot. We have to be
00088   // careful if I is an invoke instruction, because we can't insert the store
00089   // AFTER the terminator instruction.
00090   BasicBlock::iterator InsertPt;
00091   if (!isa<TerminatorInst>(I)) {
00092     InsertPt = ++I.getIterator();
00093     for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
00094       /* empty */;   // Don't insert before PHI nodes or landingpad instrs.
00095   } else {
00096     InvokeInst &II = cast<InvokeInst>(I);
00097     InsertPt = II.getNormalDest()->getFirstInsertionPt();
00098   }
00099 
00100   new StoreInst(&I, Slot, &*InsertPt);
00101   return Slot;
00102 }
00103 
00104 /// DemotePHIToStack - This function takes a virtual register computed by a PHI
00105 /// node and replaces it with a slot in the stack frame allocated via alloca.
00106 /// The PHI node is deleted. It returns the pointer to the alloca inserted.
00107 AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
00108   if (P->use_empty()) {
00109     P->eraseFromParent();
00110     return nullptr;
00111   }
00112 
00113   // Create a stack slot to hold the value.
00114   AllocaInst *Slot;
00115   if (AllocaPoint) {
00116     Slot = new AllocaInst(P->getType(), nullptr,
00117                           P->getName()+".reg2mem", AllocaPoint);
00118   } else {
00119     Function *F = P->getParent()->getParent();
00120     Slot = new AllocaInst(P->getType(), nullptr, P->getName() + ".reg2mem",
00121                           &F->getEntryBlock().front());
00122   }
00123 
00124   // Iterate over each operand inserting a store in each predecessor.
00125   for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
00126     if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
00127       assert(II->getParent() != P->getIncomingBlock(i) &&
00128              "Invoke edge not supported yet"); (void)II;
00129     }
00130     new StoreInst(P->getIncomingValue(i), Slot,
00131                   P->getIncomingBlock(i)->getTerminator());
00132   }
00133 
00134   // Insert a load in place of the PHI and replace all uses.
00135   BasicBlock::iterator InsertPt = P->getIterator();
00136 
00137   for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
00138     /* empty */;   // Don't insert before PHI nodes or landingpad instrs.
00139 
00140   Value *V = new LoadInst(Slot, P->getName() + ".reload", &*InsertPt);
00141   P->replaceAllUsesWith(V);
00142 
00143   // Delete PHI.
00144   P->eraseFromParent();
00145   return Slot;
00146 }