Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame^] | 1 | //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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/Module.h" |
| 25 | #include "llvm/BasicBlock.h" |
| 26 | #include "llvm/Instructions.h" |
| 27 | #include "llvm/ADT/Statistic.h" |
| 28 | #include "llvm/Support/Compiler.h" |
| 29 | #include <list> |
| 30 | using namespace llvm; |
| 31 | |
| 32 | STATISTIC(NumDemoted, "Number of registers demoted"); |
| 33 | |
| 34 | namespace { |
| 35 | struct VISIBILITY_HIDDEN RegToMem : public FunctionPass { |
| 36 | static char ID; // Pass identification, replacement for typeid |
| 37 | RegToMem() : FunctionPass((intptr_t)&ID) {} |
| 38 | |
| 39 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 40 | AU.addRequiredID(BreakCriticalEdgesID); |
| 41 | AU.addPreservedID(BreakCriticalEdgesID); |
| 42 | } |
| 43 | |
| 44 | bool valueEscapes(Instruction* i) { |
| 45 | BasicBlock* bb = i->getParent(); |
| 46 | for(Value::use_iterator ii = i->use_begin(), ie = i->use_end(); |
| 47 | ii != ie; ++ii) |
| 48 | if (cast<Instruction>(*ii)->getParent() != bb || |
| 49 | isa<PHINode>(*ii)) |
| 50 | return true; |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | virtual bool runOnFunction(Function &F) { |
| 55 | if (!F.isDeclaration()) { |
| 56 | //give us a clean block |
| 57 | BasicBlock* bbold = &F.getEntryBlock(); |
| 58 | BasicBlock* bbnew = new BasicBlock("allocablock", &F, |
| 59 | &F.getEntryBlock()); |
| 60 | new BranchInst(bbold, bbnew); |
| 61 | |
| 62 | //find the instructions |
| 63 | std::list<Instruction*> worklist; |
| 64 | for (Function::iterator ibb = F.begin(), ibe = F.end(); |
| 65 | ibb != ibe; ++ibb) |
| 66 | for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); |
| 67 | iib != iie; ++iib) { |
| 68 | if(valueEscapes(iib)) |
| 69 | worklist.push_front(&*iib); |
| 70 | } |
| 71 | //demote escaped instructions |
| 72 | NumDemoted += worklist.size(); |
| 73 | for (std::list<Instruction*>::iterator ilb = worklist.begin(), |
| 74 | ile = worklist.end(); ilb != ile; ++ilb) |
| 75 | DemoteRegToStack(**ilb, false); |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | char RegToMem::ID = 0; |
| 83 | RegisterPass<RegToMem> X("reg2mem", "Demote all values to stack slots"); |
| 84 | } |
| 85 | |
| 86 | // createDemoteRegisterToMemory - Provide an entry point to create this pass. |
| 87 | // |
| 88 | const PassInfo *llvm::DemoteRegisterToMemoryID = X.getPassInfo(); |
| 89 | FunctionPass *llvm::createDemoteRegisterToMemoryPass() { |
| 90 | return new RegToMem(); |
| 91 | } |