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