Home | History | Annotate | Download | only in Utils
      1 //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This pass is a simple pass wrapper around the PromoteMemToReg function call
     11 // exposed by the Utils library.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #define DEBUG_TYPE "mem2reg"
     16 #include "llvm/Transforms/Scalar.h"
     17 #include "llvm/ADT/Statistic.h"
     18 #include "llvm/Analysis/Dominators.h"
     19 #include "llvm/IR/DataLayout.h"
     20 #include "llvm/IR/Function.h"
     21 #include "llvm/IR/Instructions.h"
     22 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
     23 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
     24 using namespace llvm;
     25 
     26 STATISTIC(NumPromoted, "Number of alloca's promoted");
     27 
     28 namespace {
     29   struct PromotePass : public FunctionPass {
     30     static char ID; // Pass identification, replacement for typeid
     31 
     32     PromotePass() : FunctionPass(ID) {
     33       initializePromotePassPass(*PassRegistry::getPassRegistry());
     34     }
     35 
     36     // runOnFunction - To run this pass, first we calculate the alloca
     37     // instructions that are safe for promotion, then we promote each one.
     38     //
     39     virtual bool runOnFunction(Function &F);
     40 
     41     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
     42       AU.addRequired<DominatorTree>();
     43       AU.setPreservesCFG();
     44       // This is a cluster of orthogonal Transforms
     45       AU.addPreserved<UnifyFunctionExitNodes>();
     46       AU.addPreservedID(LowerSwitchID);
     47       AU.addPreservedID(LowerInvokePassID);
     48     }
     49   };
     50 }  // end of anonymous namespace
     51 
     52 char PromotePass::ID = 0;
     53 INITIALIZE_PASS_BEGIN(PromotePass, "mem2reg", "Promote Memory to Register",
     54                 false, false)
     55 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
     56 INITIALIZE_PASS_END(PromotePass, "mem2reg", "Promote Memory to Register",
     57                 false, false)
     58 
     59 bool PromotePass::runOnFunction(Function &F) {
     60   std::vector<AllocaInst*> Allocas;
     61 
     62   BasicBlock &BB = F.getEntryBlock();  // Get the entry node for the function
     63 
     64   bool Changed  = false;
     65 
     66   DominatorTree &DT = getAnalysis<DominatorTree>();
     67   const DataLayout *DL = getAnalysisIfAvailable<DataLayout>();
     68 
     69   while (1) {
     70     Allocas.clear();
     71 
     72     // Find allocas that are safe to promote, by looking at all instructions in
     73     // the entry node
     74     for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
     75       if (AllocaInst *AI = dyn_cast<AllocaInst>(I))       // Is it an alloca?
     76         if (isAllocaPromotable(AI, DL))
     77           Allocas.push_back(AI);
     78 
     79     if (Allocas.empty()) break;
     80 
     81     PromoteMemToReg(Allocas, DT, DL);
     82     NumPromoted += Allocas.size();
     83     Changed = true;
     84   }
     85 
     86   return Changed;
     87 }
     88 
     89 // createPromoteMemoryToRegister - Provide an entry point to create this pass.
     90 //
     91 FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
     92   return new PromotePass();
     93 }
     94