Reg2Mem.cpp revision 67894a3d02caa0b4628a942bef923907bbd97c47
1//===- Reg2Mem.cpp - Convert registers to allocas -------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file demotes all registers to memory references. It is intented to be 11// the inverse of PromoteMemoryToRegister. By converting to loads, the only 12// values live accross basic blocks are allocas and loads before phi nodes. 13// It is intended that this should make CFG hacking much easier. 14// To make later hacking easier, the entry block is split into two, such that 15// all introduced allocas and nothing else are in the entry block. 16// 17//===----------------------------------------------------------------------===// 18 19#define DEBUG_TYPE "reg2mem" 20#include "llvm/Transforms/Scalar.h" 21#include "llvm/Transforms/Utils/Local.h" 22#include "llvm/Pass.h" 23#include "llvm/Function.h" 24#include "llvm/LLVMContext.h" 25#include "llvm/Module.h" 26#include "llvm/BasicBlock.h" 27#include "llvm/Instructions.h" 28#include "llvm/ADT/Statistic.h" 29#include "llvm/Support/CFG.h" 30#include <list> 31using namespace llvm; 32 33STATISTIC(NumRegsDemoted, "Number of registers demoted"); 34STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted"); 35 36namespace { 37 struct RegToMem : public FunctionPass { 38 static char ID; // Pass identification, replacement for typeid 39 RegToMem() : FunctionPass(&ID) {} 40 41 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 42 AU.addRequiredID(BreakCriticalEdgesID); 43 AU.addPreservedID(BreakCriticalEdgesID); 44 } 45 46 bool valueEscapes(const Instruction *Inst) const { 47 const BasicBlock *BB = Inst->getParent(); 48 for (Value::const_use_iterator UI = Inst->use_begin(),E = Inst->use_end(); 49 UI != E; ++UI) { 50 const Instruction *I = cast<Instruction>(*UI); 51 if (I->getParent() != BB || isa<PHINode>(I)) 52 return true; 53 } 54 return false; 55 } 56 57 virtual bool runOnFunction(Function &F); 58 }; 59} 60 61char RegToMem::ID = 0; 62static RegisterPass<RegToMem> 63X("reg2mem", "Demote all values to stack slots"); 64 65 66bool RegToMem::runOnFunction(Function &F) { 67 if (F.isDeclaration()) 68 return false; 69 70 // Insert all new allocas into entry block. 71 BasicBlock *BBEntry = &F.getEntryBlock(); 72 assert(pred_begin(BBEntry) == pred_end(BBEntry) && 73 "Entry block to function must not have predecessors!"); 74 75 // Find first non-alloca instruction and create insertion point. This is 76 // safe if block is well-formed: it always have terminator, otherwise 77 // we'll get and assertion. 78 BasicBlock::iterator I = BBEntry->begin(); 79 while (isa<AllocaInst>(I)) ++I; 80 81 CastInst *AllocaInsertionPoint = 82 new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())), 83 Type::getInt32Ty(F.getContext()), 84 "reg2mem alloca point", I); 85 86 // Find the escaped instructions. But don't create stack slots for 87 // allocas in entry block. 88 std::list<Instruction*> WorkList; 89 for (Function::iterator ibb = F.begin(), ibe = F.end(); 90 ibb != ibe; ++ibb) 91 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); 92 iib != iie; ++iib) { 93 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) && 94 valueEscapes(iib)) { 95 WorkList.push_front(&*iib); 96 } 97 } 98 99 // Demote escaped instructions 100 NumRegsDemoted += WorkList.size(); 101 for (std::list<Instruction*>::iterator ilb = WorkList.begin(), 102 ile = WorkList.end(); ilb != ile; ++ilb) 103 DemoteRegToStack(**ilb, false, AllocaInsertionPoint); 104 105 WorkList.clear(); 106 107 // Find all phi's 108 for (Function::iterator ibb = F.begin(), ibe = F.end(); 109 ibb != ibe; ++ibb) 110 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); 111 iib != iie; ++iib) 112 if (isa<PHINode>(iib)) 113 WorkList.push_front(&*iib); 114 115 // Demote phi nodes 116 NumPhisDemoted += WorkList.size(); 117 for (std::list<Instruction*>::iterator ilb = WorkList.begin(), 118 ile = WorkList.end(); ilb != ile; ++ilb) 119 DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint); 120 121 return true; 122} 123 124 125// createDemoteRegisterToMemory - Provide an entry point to create this pass. 126// 127const PassInfo *const llvm::DemoteRegisterToMemoryID = &X; 128FunctionPass *llvm::createDemoteRegisterToMemoryPass() { 129 return new RegToMem(); 130} 131