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 intended to be 00011 // the inverse of PromoteMemoryToRegister. By converting to loads, the only 00012 // values live across 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 #include "llvm/Transforms/Scalar.h" 00020 #include "llvm/ADT/Statistic.h" 00021 #include "llvm/IR/BasicBlock.h" 00022 #include "llvm/IR/CFG.h" 00023 #include "llvm/IR/Function.h" 00024 #include "llvm/IR/Instructions.h" 00025 #include "llvm/IR/LLVMContext.h" 00026 #include "llvm/IR/Module.h" 00027 #include "llvm/Pass.h" 00028 #include "llvm/Transforms/Utils/Local.h" 00029 #include <list> 00030 using namespace llvm; 00031 00032 #define DEBUG_TYPE "reg2mem" 00033 00034 STATISTIC(NumRegsDemoted, "Number of registers demoted"); 00035 STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted"); 00036 00037 namespace { 00038 struct RegToMem : public FunctionPass { 00039 static char ID; // Pass identification, replacement for typeid 00040 RegToMem() : FunctionPass(ID) { 00041 initializeRegToMemPass(*PassRegistry::getPassRegistry()); 00042 } 00043 00044 void getAnalysisUsage(AnalysisUsage &AU) const override { 00045 AU.addRequiredID(BreakCriticalEdgesID); 00046 AU.addPreservedID(BreakCriticalEdgesID); 00047 } 00048 00049 bool valueEscapes(const Instruction *Inst) const { 00050 const BasicBlock *BB = Inst->getParent(); 00051 for (const User *U : Inst->users()) { 00052 const Instruction *UI = cast<Instruction>(U); 00053 if (UI->getParent() != BB || isa<PHINode>(UI)) 00054 return true; 00055 } 00056 return false; 00057 } 00058 00059 bool runOnFunction(Function &F) override; 00060 }; 00061 } 00062 00063 char RegToMem::ID = 0; 00064 INITIALIZE_PASS_BEGIN(RegToMem, "reg2mem", "Demote all values to stack slots", 00065 false, false) 00066 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges) 00067 INITIALIZE_PASS_END(RegToMem, "reg2mem", "Demote all values to stack slots", 00068 false, false) 00069 00070 bool RegToMem::runOnFunction(Function &F) { 00071 if (F.isDeclaration()) 00072 return false; 00073 00074 // Insert all new allocas into entry block. 00075 BasicBlock *BBEntry = &F.getEntryBlock(); 00076 assert(pred_begin(BBEntry) == pred_end(BBEntry) && 00077 "Entry block to function must not have predecessors!"); 00078 00079 // Find first non-alloca instruction and create insertion point. This is 00080 // safe if block is well-formed: it always have terminator, otherwise 00081 // we'll get and assertion. 00082 BasicBlock::iterator I = BBEntry->begin(); 00083 while (isa<AllocaInst>(I)) ++I; 00084 00085 CastInst *AllocaInsertionPoint = 00086 new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())), 00087 Type::getInt32Ty(F.getContext()), 00088 "reg2mem alloca point", I); 00089 00090 // Find the escaped instructions. But don't create stack slots for 00091 // allocas in entry block. 00092 std::list<Instruction*> WorkList; 00093 for (Function::iterator ibb = F.begin(), ibe = F.end(); 00094 ibb != ibe; ++ibb) 00095 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); 00096 iib != iie; ++iib) { 00097 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) && 00098 valueEscapes(iib)) { 00099 WorkList.push_front(&*iib); 00100 } 00101 } 00102 00103 // Demote escaped instructions 00104 NumRegsDemoted += WorkList.size(); 00105 for (std::list<Instruction*>::iterator ilb = WorkList.begin(), 00106 ile = WorkList.end(); ilb != ile; ++ilb) 00107 DemoteRegToStack(**ilb, false, AllocaInsertionPoint); 00108 00109 WorkList.clear(); 00110 00111 // Find all phi's 00112 for (Function::iterator ibb = F.begin(), ibe = F.end(); 00113 ibb != ibe; ++ibb) 00114 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); 00115 iib != iie; ++iib) 00116 if (isa<PHINode>(iib)) 00117 WorkList.push_front(&*iib); 00118 00119 // Demote phi nodes 00120 NumPhisDemoted += WorkList.size(); 00121 for (std::list<Instruction*>::iterator ilb = WorkList.begin(), 00122 ile = WorkList.end(); ilb != ile; ++ilb) 00123 DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint); 00124 00125 return true; 00126 } 00127 00128 00129 // createDemoteRegisterToMemory - Provide an entry point to create this pass. 00130 char &llvm::DemoteRegisterToMemoryID = RegToMem::ID; 00131 FunctionPass *llvm::createDemoteRegisterToMemoryPass() { 00132 return new RegToMem(); 00133 }