Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 1 | //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass is a simple pass wrapper around the PromoteMemToReg function call |
| 11 | // exposed by the Utils library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Transforms/Scalar.h" |
| 16 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
| 17 | #include "llvm/Analysis/Dominators.h" |
| 18 | #include "llvm/iMemory.h" |
| 19 | #include "llvm/Function.h" |
Chris Lattner | e27406e | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 21 | #include "Support/Statistic.h" |
| 22 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | namespace llvm { |
| 24 | |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | Statistic<> NumPromoted("mem2reg", "Number of alloca's promoted"); |
| 27 | |
| 28 | struct PromotePass : public FunctionPass { |
| 29 | // runOnFunction - To run this pass, first we calculate the alloca |
| 30 | // instructions that are safe for promotion, then we promote each one. |
| 31 | // |
| 32 | virtual bool runOnFunction(Function &F); |
| 33 | |
| 34 | // getAnalysisUsage - We need dominance frontiers |
| 35 | // |
| 36 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | a906bac | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 37 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 38 | AU.addRequired<DominanceFrontier>(); |
Chris Lattner | e27406e | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 39 | AU.addRequired<TargetData>(); |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 40 | AU.setPreservesCFG(); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | RegisterOpt<PromotePass> X("mem2reg", "Promote Memory to Register"); |
| 45 | } // end of anonymous namespace |
| 46 | |
| 47 | bool PromotePass::runOnFunction(Function &F) { |
| 48 | std::vector<AllocaInst*> Allocas; |
Chris Lattner | e27406e | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 49 | const TargetData &TD = getAnalysis<TargetData>(); |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 5dac64f | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 51 | BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 52 | |
Chris Lattner | b396afd | 2003-06-25 14:58:56 +0000 | [diff] [blame] | 53 | bool Changed = false; |
Chris Lattner | a906bac | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 54 | |
| 55 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
| 56 | DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); |
Chris Lattner | b396afd | 2003-06-25 14:58:56 +0000 | [diff] [blame] | 57 | |
| 58 | while (1) { |
| 59 | Allocas.clear(); |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 60 | |
Chris Lattner | b396afd | 2003-06-25 14:58:56 +0000 | [diff] [blame] | 61 | // Find allocas that are safe to promote, by looking at all instructions in |
| 62 | // the entry node |
| 63 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 64 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? |
| 65 | if (isAllocaPromotable(AI, TD)) |
| 66 | Allocas.push_back(AI); |
| 67 | |
| 68 | if (Allocas.empty()) break; |
| 69 | |
Chris Lattner | a906bac | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 70 | PromoteMemToReg(Allocas, DT, DF, TD); |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 71 | NumPromoted += Allocas.size(); |
Chris Lattner | b396afd | 2003-06-25 14:58:56 +0000 | [diff] [blame] | 72 | Changed = true; |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 73 | } |
Chris Lattner | b396afd | 2003-06-25 14:58:56 +0000 | [diff] [blame] | 74 | |
| 75 | return Changed; |
Chris Lattner | 26f1590 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // createPromoteMemoryToRegister - Provide an entry point to create this pass. |
| 79 | // |
| 80 | Pass *createPromoteMemoryToRegister() { |
| 81 | return new PromotePass(); |
| 82 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 83 | |
| 84 | } // End llvm namespace |