LLVM API Documentation
00001 //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===// 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 demotes all registers to memory references. It is intented to be 00011 // the inverse of PromoteMemoryToRegister. By converting to loads, the only 00012 // values live accross basic blocks are allocas and loads before phi nodes. 00013 // It is intended that this should make CFG hacking much easier. 00014 // To make later hacking easier, the entry block is split into two, such that 00015 // all introduced allocas and nothing else are in the entry block. 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #define DEBUG_TYPE "reg2mem" 00020 #include "llvm/Transforms/Scalar.h" 00021 #include "llvm/Transforms/Utils/Local.h" 00022 #include "llvm/Pass.h" 00023 #include "llvm/Function.h" 00024 #include "llvm/Module.h" 00025 #include "llvm/BasicBlock.h" 00026 #include "llvm/Instructions.h" 00027 #include "llvm/ADT/Statistic.h" 00028 #include "llvm/Support/Compiler.h" 00029 #include "llvm/Support/CFG.h" 00030 #include <list> 00031 using namespace llvm; 00032 00033 STATISTIC(NumRegsDemoted, "Number of registers demoted"); 00034 STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted"); 00035 00036 namespace { 00037 struct VISIBILITY_HIDDEN RegToMem : public FunctionPass { 00038 static char ID; // Pass identification, replacement for typeid 00039 RegToMem() : FunctionPass((intptr_t)&ID) {} 00040 00041 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00042 AU.addRequiredID(BreakCriticalEdgesID); 00043 AU.addPreservedID(BreakCriticalEdgesID); 00044 } 00045 00046 bool valueEscapes(Instruction* i) { 00047 BasicBlock* bb = i->getParent(); 00048 for (Value::use_iterator ii = i->use_begin(), ie = i->use_end(); 00049 ii != ie; ++ii) 00050 if (cast<Instruction>(*ii)->getParent() != bb || 00051 isa<PHINode>(*ii)) 00052 return true; 00053 return false; 00054 } 00055 00056 virtual bool runOnFunction(Function &F) { 00057 if (!F.isDeclaration()) { 00058 // Insert all new allocas into entry block. 00059 BasicBlock* BBEntry = &F.getEntryBlock(); 00060 assert(pred_begin(BBEntry) == pred_end(BBEntry) && 00061 "Entry block to function must not have predecessors!"); 00062 00063 // Find first non-alloca instruction and create insertion point. This is 00064 // safe if block is well-formed: it always have terminator, otherwise 00065 // we'll get and assertion. 00066 BasicBlock::iterator I = BBEntry->begin(); 00067 while (isa<AllocaInst>(I)) ++I; 00068 00069 CastInst *AllocaInsertionPoint = 00070 CastInst::Create(Instruction::BitCast, 00071 Constant::getNullValue(Type::Int32Ty), Type::Int32Ty, 00072 "reg2mem alloca point", I); 00073 00074 // Find the escaped instructions. But don't create stack slots for 00075 // allocas in entry block. 00076 std::list<Instruction*> worklist; 00077 for (Function::iterator ibb = F.begin(), ibe = F.end(); 00078 ibb != ibe; ++ibb) 00079 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); 00080 iib != iie; ++iib) { 00081 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) && 00082 valueEscapes(iib)) { 00083 worklist.push_front(&*iib); 00084 } 00085 } 00086 00087 // Demote escaped instructions 00088 NumRegsDemoted += worklist.size(); 00089 for (std::list<Instruction*>::iterator ilb = worklist.begin(), 00090 ile = worklist.end(); ilb != ile; ++ilb) 00091 DemoteRegToStack(**ilb, false, AllocaInsertionPoint); 00092 00093 worklist.clear(); 00094 00095 // Find all phi's 00096 for (Function::iterator ibb = F.begin(), ibe = F.end(); 00097 ibb != ibe; ++ibb) 00098 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); 00099 iib != iie; ++iib) 00100 if (isa<PHINode>(iib)) 00101 worklist.push_front(&*iib); 00102 00103 // Demote phi nodes 00104 NumPhisDemoted += worklist.size(); 00105 for (std::list<Instruction*>::iterator ilb = worklist.begin(), 00106 ile = worklist.end(); ilb != ile; ++ilb) 00107 DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint); 00108 00109 return true; 00110 } 00111 return false; 00112 } 00113 }; 00114 } 00115 00116 char RegToMem::ID = 0; 00117 static RegisterPass<RegToMem> 00118 X("reg2mem", "Demote all values to stack slots"); 00119 00120 // createDemoteRegisterToMemory - Provide an entry point to create this pass. 00121 // 00122 const PassInfo *const llvm::DemoteRegisterToMemoryID = &X; 00123 FunctionPass *llvm::createDemoteRegisterToMemoryPass() { 00124 return new RegToMem(); 00125 }