LLVM API Documentation

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().begin());
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;
00093     ++InsertPt;
00094     for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
00095       /* empty */;   // Don't insert before PHI nodes or landingpad instrs.
00096   } else {
00097     InvokeInst &II = cast<InvokeInst>(I);
00098     InsertPt = II.getNormalDest()->getFirstInsertionPt();
00099   }
00100 
00101   new StoreInst(&I, Slot, InsertPt);
00102   return Slot;
00103 }
00104 
00105 /// DemotePHIToStack - This function takes a virtual register computed by a PHI
00106 /// node and replaces it with a slot in the stack frame allocated via alloca.
00107 /// The PHI node is deleted. It returns the pointer to the alloca inserted.
00108 AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
00109   if (P->use_empty()) {
00110     P->eraseFromParent();
00111     return nullptr;
00112   }
00113 
00114   // Create a stack slot to hold the value.
00115   AllocaInst *Slot;
00116   if (AllocaPoint) {
00117     Slot = new AllocaInst(P->getType(), nullptr,
00118                           P->getName()+".reg2mem", AllocaPoint);
00119   } else {
00120     Function *F = P->getParent()->getParent();
00121     Slot = new AllocaInst(P->getType(), nullptr, P->getName()+".reg2mem",
00122                           F->getEntryBlock().begin());
00123   }
00124 
00125   // Iterate over each operand inserting a store in each predecessor.
00126   for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
00127     if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
00128       assert(II->getParent() != P->getIncomingBlock(i) &&
00129              "Invoke edge not supported yet"); (void)II;
00130     }
00131     new StoreInst(P->getIncomingValue(i), Slot,
00132                   P->getIncomingBlock(i)->getTerminator());
00133   }
00134 
00135   // Insert a load in place of the PHI and replace all uses.
00136   BasicBlock::iterator InsertPt = P;
00137 
00138   for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
00139     /* empty */;   // Don't insert before PHI nodes or landingpad instrs.
00140 
00141   Value *V = new LoadInst(Slot, P->getName()+".reload", InsertPt);
00142   P->replaceAllUsesWith(V);
00143 
00144   // Delete PHI.
00145   P->eraseFromParent();
00146   return Slot;
00147 }