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