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