LLVM API Documentation
00001 //===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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 if (I.use_empty()) return 0; // nothing to do! 00034 00035 // Create a stack slot to hold the value. 00036 Function *F = I.getParent()->getParent(); 00037 AllocaInst *Slot = new AllocaInst(I.getType(), 0, I.getName(), 00038 F->getEntryBlock().begin()); 00039 00040 // Change all of the users of the instruction to read from the stack slot 00041 // instead. 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 multiple loads. The problem is that the resultant 00051 // PHI node will have multiple values (from each load) coming in from the 00052 // same block, which is illegal SSA form. For this reason, we keep track 00053 // and reuse loads we insert. 00054 std::map<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 is I is an invoke instruction though, because we can't insert the 00076 // store AFTER the terminator instruction. 00077 BasicBlock::iterator InsertPt; 00078 if (!isa<TerminatorInst>(I)) { 00079 InsertPt = &I; 00080 ++InsertPt; 00081 } else { 00082 // We cannot demote invoke instructions to the stack if their normal edge 00083 // is critical. 00084 InvokeInst &II = cast<InvokeInst>(I); 00085 assert(II.getNormalDest()->getSinglePredecessor() && 00086 "Cannot demote invoke with a critical successor!"); 00087 InsertPt = II.getNormalDest()->begin(); 00088 } 00089 00090 for (; isa<PHINode>(InsertPt); ++InsertPt) 00091 /* empty */; // Don't insert before any PHI nodes. 00092 new StoreInst(&I, Slot, InsertPt); 00093 00094 return Slot; 00095 }