LLVM API Documentation

Mem2Reg.cpp
Go to the documentation of this file.
00001 //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass is a simple pass wrapper around the PromoteMemToReg function call
00011 // exposed by the Utils library.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Transforms/Scalar.h"
00016 #include "llvm/ADT/Statistic.h"
00017 #include "llvm/IR/Dominators.h"
00018 #include "llvm/IR/Function.h"
00019 #include "llvm/IR/Instructions.h"
00020 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
00021 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
00022 using namespace llvm;
00023 
00024 #define DEBUG_TYPE "mem2reg"
00025 
00026 STATISTIC(NumPromoted, "Number of alloca's promoted");
00027 
00028 namespace {
00029   struct PromotePass : public FunctionPass {
00030     static char ID; // Pass identification, replacement for typeid
00031     PromotePass() : FunctionPass(ID) {
00032       initializePromotePassPass(*PassRegistry::getPassRegistry());
00033     }
00034 
00035     // runOnFunction - To run this pass, first we calculate the alloca
00036     // instructions that are safe for promotion, then we promote each one.
00037     //
00038     bool runOnFunction(Function &F) override;
00039 
00040     void getAnalysisUsage(AnalysisUsage &AU) const override {
00041       AU.addRequired<DominatorTreeWrapperPass>();
00042       AU.setPreservesCFG();
00043       // This is a cluster of orthogonal Transforms
00044       AU.addPreserved<UnifyFunctionExitNodes>();
00045       AU.addPreservedID(LowerSwitchID);
00046       AU.addPreservedID(LowerInvokePassID);
00047     }
00048   };
00049 }  // end of anonymous namespace
00050 
00051 char PromotePass::ID = 0;
00052 INITIALIZE_PASS_BEGIN(PromotePass, "mem2reg", "Promote Memory to Register",
00053                 false, false)
00054 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
00055 INITIALIZE_PASS_END(PromotePass, "mem2reg", "Promote Memory to Register",
00056                 false, false)
00057 
00058 bool PromotePass::runOnFunction(Function &F) {
00059   std::vector<AllocaInst*> Allocas;
00060 
00061   BasicBlock &BB = F.getEntryBlock();  // Get the entry node for the function
00062 
00063   bool Changed  = false;
00064 
00065   DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
00066 
00067   while (1) {
00068     Allocas.clear();
00069 
00070     // Find allocas that are safe to promote, by looking at all instructions in
00071     // the entry node
00072     for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
00073       if (AllocaInst *AI = dyn_cast<AllocaInst>(I))       // Is it an alloca?
00074         if (isAllocaPromotable(AI))
00075           Allocas.push_back(AI);
00076 
00077     if (Allocas.empty()) break;
00078 
00079     PromoteMemToReg(Allocas, DT);
00080     NumPromoted += Allocas.size();
00081     Changed = true;
00082   }
00083 
00084   return Changed;
00085 }
00086 
00087 // createPromoteMemoryToRegister - Provide an entry point to create this pass.
00088 //
00089 FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
00090   return new PromotePass();
00091 }