Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 1 | //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 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 | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "reg2mem" |
Andrew Lenharth | 183119c | 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" |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 24 | #include "llvm/LLVMContext.h" |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" |
| 26 | #include "llvm/BasicBlock.h" |
| 27 | #include "llvm/Instructions.h" |
| 28 | #include "llvm/ADT/Statistic.h" |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CFG.h" |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 30 | #include <list> |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 33 | STATISTIC(NumRegsDemoted, "Number of registers demoted"); |
| 34 | STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted"); |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 35 | |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 36 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 37 | struct RegToMem : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 38 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 39 | RegToMem() : FunctionPass(&ID) {} |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 40 | |
Andrew Lenharth | 7045f6c | 2005-11-22 21:45:19 +0000 | [diff] [blame] | 41 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 42 | AU.addRequiredID(BreakCriticalEdgesID); |
Andrew Lenharth | b082652 | 2005-11-25 16:04:54 +0000 | [diff] [blame] | 43 | AU.addPreservedID(BreakCriticalEdgesID); |
Andrew Lenharth | 7045f6c | 2005-11-22 21:45:19 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Chris Lattner | 20a2faa | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 46 | bool valueEscapes(const Instruction *Inst) const { |
| 47 | const BasicBlock *BB = Inst->getParent(); |
Gabor Greif | 60ad781 | 2010-03-25 23:06:16 +0000 | [diff] [blame^] | 48 | for (Value::const_use_iterator UI = Inst->use_begin(),E = Inst->use_end(); |
Chris Lattner | 20a2faa | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 49 | UI != E; ++UI) |
| 50 | if (cast<Instruction>(*UI)->getParent() != BB || |
| 51 | isa<PHINode>(*UI)) |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 52 | return true; |
| 53 | return false; |
| 54 | } |
| 55 | |
Chris Lattner | 20a2faa | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 56 | virtual bool runOnFunction(Function &F); |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 57 | }; |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 58 | } |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 59 | |
| 60 | char RegToMem::ID = 0; |
| 61 | static RegisterPass<RegToMem> |
| 62 | X("reg2mem", "Demote all values to stack slots"); |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 20a2faa | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 64 | |
| 65 | bool RegToMem::runOnFunction(Function &F) { |
| 66 | if (F.isDeclaration()) |
| 67 | return false; |
| 68 | |
| 69 | // Insert all new allocas into entry block. |
| 70 | BasicBlock *BBEntry = &F.getEntryBlock(); |
| 71 | assert(pred_begin(BBEntry) == pred_end(BBEntry) && |
| 72 | "Entry block to function must not have predecessors!"); |
| 73 | |
| 74 | // Find first non-alloca instruction and create insertion point. This is |
| 75 | // safe if block is well-formed: it always have terminator, otherwise |
| 76 | // we'll get and assertion. |
| 77 | BasicBlock::iterator I = BBEntry->begin(); |
| 78 | while (isa<AllocaInst>(I)) ++I; |
| 79 | |
| 80 | CastInst *AllocaInsertionPoint = |
| 81 | new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())), |
| 82 | Type::getInt32Ty(F.getContext()), |
| 83 | "reg2mem alloca point", I); |
| 84 | |
| 85 | // Find the escaped instructions. But don't create stack slots for |
| 86 | // allocas in entry block. |
| 87 | std::list<Instruction*> WorkList; |
| 88 | for (Function::iterator ibb = F.begin(), ibe = F.end(); |
| 89 | ibb != ibe; ++ibb) |
| 90 | for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); |
| 91 | iib != iie; ++iib) { |
| 92 | if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) && |
| 93 | valueEscapes(iib)) { |
| 94 | WorkList.push_front(&*iib); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Demote escaped instructions |
| 99 | NumRegsDemoted += WorkList.size(); |
| 100 | for (std::list<Instruction*>::iterator ilb = WorkList.begin(), |
| 101 | ile = WorkList.end(); ilb != ile; ++ilb) |
| 102 | DemoteRegToStack(**ilb, false, AllocaInsertionPoint); |
| 103 | |
| 104 | WorkList.clear(); |
| 105 | |
| 106 | // Find all phi's |
| 107 | for (Function::iterator ibb = F.begin(), ibe = F.end(); |
| 108 | ibb != ibe; ++ibb) |
| 109 | for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); |
| 110 | iib != iie; ++iib) |
| 111 | if (isa<PHINode>(iib)) |
| 112 | WorkList.push_front(&*iib); |
| 113 | |
| 114 | // Demote phi nodes |
| 115 | NumPhisDemoted += WorkList.size(); |
| 116 | for (std::list<Instruction*>::iterator ilb = WorkList.begin(), |
| 117 | ile = WorkList.end(); ilb != ile; ++ilb) |
| 118 | DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint); |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 124 | // createDemoteRegisterToMemory - Provide an entry point to create this pass. |
| 125 | // |
Dan Gohman | 6ddba2b | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 126 | const PassInfo *const llvm::DemoteRegisterToMemoryID = &X; |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 127 | FunctionPass *llvm::createDemoteRegisterToMemoryPass() { |
| 128 | return new RegToMem(); |
| 129 | } |