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" |
| 24 | #include "llvm/Module.h" |
| 25 | #include "llvm/BasicBlock.h" |
| 26 | #include "llvm/Instructions.h" |
| 27 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Compiler.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 { |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 37 | struct VISIBILITY_HIDDEN RegToMem : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 38 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 39 | RegToMem() : FunctionPass((intptr_t)&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 | |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 46 | bool valueEscapes(Instruction* i) { |
| 47 | BasicBlock* bb = i->getParent(); |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 48 | for (Value::use_iterator ii = i->use_begin(), ie = i->use_end(); |
| 49 | ii != ie; ++ii) |
Andrew Lenharth | fa25e48 | 2005-11-10 19:39:09 +0000 | [diff] [blame] | 50 | if (cast<Instruction>(*ii)->getParent() != bb || |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 51 | isa<PHINode>(*ii)) |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 52 | return true; |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | virtual bool runOnFunction(Function &F) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 57 | if (!F.isDeclaration()) { |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 58 | // Insert all new allocas into entry block. |
| 59 | BasicBlock* BBEntry = &F.getEntryBlock(); |
| 60 | assert(pred_begin(BBEntry) == pred_end(BBEntry) && |
| 61 | "Entry block to function must not have predecessors!"); |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 62 | |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 63 | // Find first non-alloca instruction and create insertion point. This is |
| 64 | // safe if block is well-formed: it always have terminator, otherwise |
| 65 | // we'll get and assertion. |
| 66 | BasicBlock::iterator I = BBEntry->begin(); |
| 67 | while (isa<AllocaInst>(I)) ++I; |
| 68 | |
| 69 | CastInst *AllocaInsertionPoint = |
| 70 | CastInst::create(Instruction::BitCast, |
| 71 | Constant::getNullValue(Type::Int32Ty), Type::Int32Ty, |
| 72 | "reg2mem alloca point", I); |
| 73 | |
| 74 | // Find the escaped instructions. But don't create stack slots for |
| 75 | // allocas in entry block. |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 76 | std::list<Instruction*> worklist; |
| 77 | for (Function::iterator ibb = F.begin(), ibe = F.end(); |
| 78 | ibb != ibe; ++ibb) |
| 79 | for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); |
| 80 | iib != iie; ++iib) { |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 81 | if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) && |
| 82 | valueEscapes(iib)) { |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 83 | worklist.push_front(&*iib); |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 84 | } |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 85 | } |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 86 | |
| 87 | // Demote escaped instructions |
| 88 | NumRegsDemoted += worklist.size(); |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 89 | for (std::list<Instruction*>::iterator ilb = worklist.begin(), |
| 90 | ile = worklist.end(); ilb != ile; ++ilb) |
Anton Korobeynikov | a024e8c | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 91 | DemoteRegToStack(**ilb, false, AllocaInsertionPoint); |
| 92 | |
| 93 | worklist.clear(); |
| 94 | |
| 95 | // Find all phi's |
| 96 | for (Function::iterator ibb = F.begin(), ibe = F.end(); |
| 97 | ibb != ibe; ++ibb) |
| 98 | for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end(); |
| 99 | iib != iie; ++iib) |
| 100 | if (isa<PHINode>(iib)) |
| 101 | worklist.push_front(&*iib); |
| 102 | |
| 103 | // Demote phi nodes |
| 104 | NumPhisDemoted += worklist.size(); |
| 105 | for (std::list<Instruction*>::iterator ilb = worklist.begin(), |
| 106 | ile = worklist.end(); ilb != ile; ++ilb) |
| 107 | DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint); |
| 108 | |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | }; |
| 114 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 115 | char RegToMem::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 116 | RegisterPass<RegToMem> X("reg2mem", "Demote all values to stack slots"); |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // createDemoteRegisterToMemory - Provide an entry point to create this pass. |
| 120 | // |
Andrew Lenharth | 7c0c567 | 2005-11-22 22:14:23 +0000 | [diff] [blame] | 121 | const PassInfo *llvm::DemoteRegisterToMemoryID = X.getPassInfo(); |
Andrew Lenharth | 183119c | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 122 | FunctionPass *llvm::createDemoteRegisterToMemoryPass() { |
| 123 | return new RegToMem(); |
| 124 | } |