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