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