Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +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 | #include "llvm/Transforms/Scalar.h" |
| 20 | #include "llvm/Transforms/Utils/Local.h" |
| 21 | #include "llvm/Pass.h" |
| 22 | #include "llvm/Function.h" |
| 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/BasicBlock.h" |
| 25 | #include "llvm/Instructions.h" |
| 26 | #include "llvm/ADT/Statistic.h" |
| 27 | |
| 28 | #include <list> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | Statistic<> NumDemoted("reg2mem", "Number of registers demoted"); |
| 34 | |
| 35 | struct RegToMem : public FunctionPass { |
| 36 | |
Andrew Lenharth | 71b09bb | 2005-11-22 21:45:19 +0000 | [diff] [blame] | 37 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 38 | AU.addRequiredID(BreakCriticalEdgesID); |
Andrew Lenharth | 5fc3794 | 2005-11-25 16:04:54 +0000 | [diff] [blame^] | 39 | AU.addPreservedID(BreakCriticalEdgesID); |
Andrew Lenharth | 71b09bb | 2005-11-22 21:45:19 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 42 | bool valueEscapes(Instruction* i) { |
| 43 | BasicBlock* bb = i->getParent(); |
| 44 | for(Value::use_iterator ii = i->use_begin(), ie = i->use_end(); |
| 45 | ii != ie; ++ii) |
Andrew Lenharth | d9c13b1 | 2005-11-10 19:39:09 +0000 | [diff] [blame] | 46 | if (cast<Instruction>(*ii)->getParent() != bb || |
| 47 | isa<PHINode>(*ii)) |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 48 | return true; |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | virtual bool runOnFunction(Function &F) { |
| 53 | if (!F.isExternal()) { |
| 54 | //give us a clean block |
Andrew Lenharth | 8e66c0c | 2005-11-10 17:35:34 +0000 | [diff] [blame] | 55 | BasicBlock* bbold = &F.getEntryBlock(); |
| 56 | BasicBlock* bbnew = new BasicBlock("allocablock", &F, &F.getEntryBlock()); |
| 57 | new BranchInst(bbold, bbnew); |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 58 | |
| 59 | //find the instructions |
| 60 | std::list<Instruction*> worklist; |
| 61 | for (Function::iterator ibb = F.begin(), ibe = F.end(); |
| 62 | ibb != ibe; ++ibb) |
| 63 | for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); |
| 64 | iib != iie; ++iib) { |
| 65 | if(valueEscapes(iib)) |
| 66 | worklist.push_front(&*iib); |
| 67 | } |
| 68 | //demote escaped instructions |
| 69 | NumDemoted += worklist.size(); |
| 70 | for (std::list<Instruction*>::iterator ilb = worklist.begin(), |
| 71 | ile = worklist.end(); ilb != ile; ++ilb) |
| 72 | DemoteRegToStack(**ilb, false); |
| 73 | return true; |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | RegisterOpt<RegToMem> X("reg2mem", "Demote all values to stack slots"); |
| 80 | } |
| 81 | |
| 82 | // createDemoteRegisterToMemory - Provide an entry point to create this pass. |
| 83 | // |
Andrew Lenharth | 061029d | 2005-11-22 22:14:23 +0000 | [diff] [blame] | 84 | const PassInfo *llvm::DemoteRegisterToMemoryID = X.getPassInfo(); |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 85 | FunctionPass *llvm::createDemoteRegisterToMemoryPass() { |
| 86 | return new RegToMem(); |
| 87 | } |