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 // 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 "llvm/Type.h" 00023 #include <map> 00024 using namespace llvm; 00025 00026 /// DemoteRegToStack - This function takes a virtual register computed by an 00027 /// Instruction and replaces it with a slot in the stack frame, allocated via 00028 /// alloca. This allows the CFG to be changed around without fear of 00029 /// invalidating the SSA information for the value. It returns the pointer to 00030 /// the alloca inserted to create a stack slot for I. 00031 /// 00032 AllocaInst* llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads, 00033 Instruction *AllocaPoint) { 00034 if (I.use_empty()) { 00035 I.eraseFromParent(); 00036 return 0; 00037 } 00038 00039 // Create a stack slot to hold the value. 00040 AllocaInst *Slot; 00041 if (AllocaPoint) { 00042 Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem", AllocaPoint); 00043 } else { 00044 Function *F = I.getParent()->getParent(); 00045 Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem", 00046 F->getEntryBlock().begin()); 00047 } 00048 00049 // Change all of the users of the instruction to read from the stack slot 00050 // instead. 00051 while (!I.use_empty()) { 00052 Instruction *U = cast<Instruction>(I.use_back()); 00053 if (PHINode *PN = dyn_cast<PHINode>(U)) { 00054 // If this is a PHI node, we can't insert a load of the value before the 00055 // use. Instead, insert the load in the predecessor block corresponding 00056 // to the incoming value. 00057 // 00058 // Note that if there are multiple edges from a basic block to this PHI 00059 // node that we cannot multiple loads. The problem is that the resultant 00060 // PHI node will have multiple values (from each load) coming in from the 00061 // same block, which is illegal SSA form. For this reason, we keep track 00062 // and reuse loads we insert. 00063 std::map<BasicBlock*, Value*> Loads; 00064 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 00065 if (PN->getIncomingValue(i) == &I) { 00066 Value *&V = Loads[PN->getIncomingBlock(i)]; 00067 if (V == 0) { 00068 // Insert the load into the predecessor block 00069 V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, 00070 PN->getIncomingBlock(i)->getTerminator()); 00071 } 00072 PN->setIncomingValue(i, V); 00073 } 00074 00075 } else { 00076 // If this is a normal instruction, just insert a load. 00077 Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U); 00078 U->replaceUsesOfWith(&I, V); 00079 } 00080 } 00081 00082 00083 // Insert stores of the computed value into the stack slot. We have to be 00084 // careful is I is an invoke instruction though, because we can't insert the 00085 // store AFTER the terminator instruction. 00086 BasicBlock::iterator InsertPt; 00087 if (!isa<TerminatorInst>(I)) { 00088 InsertPt = &I; 00089 ++InsertPt; 00090 } else { 00091 // We cannot demote invoke instructions to the stack if their normal edge 00092 // is critical. 00093 InvokeInst &II = cast<InvokeInst>(I); 00094 assert(II.getNormalDest()->getSinglePredecessor() && 00095 "Cannot demote invoke with a critical successor!"); 00096 InsertPt = II.getNormalDest()->begin(); 00097 } 00098 00099 for (; isa<PHINode>(InsertPt); ++InsertPt) 00100 /* empty */; // Don't insert before any PHI nodes. 00101 new StoreInst(&I, Slot, InsertPt); 00102 00103 return Slot; 00104 } 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 and 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, 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, insert 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"); II=II; 00131 } 00132 new StoreInst(P->getIncomingValue(i), Slot, 00133 P->getIncomingBlock(i)->getTerminator()); 00134 } 00135 00136 // Insert load in place of the phi and replace all uses. 00137 Value *V = new LoadInst(Slot, P->getName()+".reload", P); 00138 P->replaceAllUsesWith(V); 00139 00140 // Delete phi. 00141 P->eraseFromParent(); 00142 00143 return Slot; 00144 }
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.