Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 1 | //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===// |
| 2 | // |
| 3 | // This pass is a simple pass wrapper around the PromoteMemToReg function call |
| 4 | // exposed by the Utils library. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #include "llvm/Transforms/Scalar.h" |
| 9 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
| 10 | #include "llvm/Analysis/Dominators.h" |
| 11 | #include "llvm/iMemory.h" |
| 12 | #include "llvm/Function.h" |
Chris Lattner | fb743a9 | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 13 | #include "llvm/Target/TargetData.h" |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 14 | #include "Support/Statistic.h" |
| 15 | |
| 16 | namespace { |
| 17 | Statistic<> NumPromoted("mem2reg", "Number of alloca's promoted"); |
| 18 | |
| 19 | struct PromotePass : public FunctionPass { |
| 20 | // runOnFunction - To run this pass, first we calculate the alloca |
| 21 | // instructions that are safe for promotion, then we promote each one. |
| 22 | // |
| 23 | virtual bool runOnFunction(Function &F); |
| 24 | |
| 25 | // getAnalysisUsage - We need dominance frontiers |
| 26 | // |
| 27 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 28 | AU.addRequired<DominanceFrontier>(); |
Chris Lattner | fb743a9 | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 29 | AU.addRequired<TargetData>(); |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 30 | AU.setPreservesCFG(); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | RegisterOpt<PromotePass> X("mem2reg", "Promote Memory to Register"); |
| 35 | } // end of anonymous namespace |
| 36 | |
| 37 | bool PromotePass::runOnFunction(Function &F) { |
| 38 | std::vector<AllocaInst*> Allocas; |
Chris Lattner | fb743a9 | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 39 | const TargetData &TD = getAnalysis<TargetData>(); |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 40 | |
| 41 | BasicBlock &BB = F.getEntryNode(); // Get the entry node for the function |
| 42 | |
| 43 | // Find allocas that are safe to promote, by looking at all instructions in |
| 44 | // the entry node |
| 45 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 46 | if (AllocaInst *AI = dyn_cast<AllocaInst>(&*I)) // Is it an alloca? |
Chris Lattner | fb743a9 | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 47 | if (isAllocaPromotable(AI, TD)) |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 48 | Allocas.push_back(AI); |
| 49 | |
| 50 | if (!Allocas.empty()) { |
Chris Lattner | fb743a9 | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 51 | PromoteMemToReg(Allocas, getAnalysis<DominanceFrontier>(), TD); |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 52 | NumPromoted += Allocas.size(); |
| 53 | return true; |
| 54 | } |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | // createPromoteMemoryToRegister - Provide an entry point to create this pass. |
| 59 | // |
| 60 | Pass *createPromoteMemoryToRegister() { |
| 61 | return new PromotePass(); |
| 62 | } |