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/Analysis/AssumptionCache.h"
00018 #include "llvm/IR/Dominators.h"
00019 #include "llvm/IR/Function.h"
00020 #include "llvm/IR/Instructions.h"
00021 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
00022 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
00023 using namespace llvm;
00024 
00025 #define DEBUG_TYPE "mem2reg"
00026 
00027 STATISTIC(NumPromoted, "Number of alloca's promoted");
00028 
00029 namespace {
00030   struct PromotePass : public FunctionPass {
00031     static char ID; // Pass identification, replacement for typeid
00032     PromotePass() : FunctionPass(ID) {
00033       initializePromotePassPass(*PassRegistry::getPassRegistry());
00034     }
00035 
00036     // runOnFunction - To run this pass, first we calculate the alloca
00037     // instructions that are safe for promotion, then we promote each one.
00038     //
00039     bool runOnFunction(Function &F) override;
00040 
00041     void getAnalysisUsage(AnalysisUsage &AU) const override {
00042       AU.addRequired<AssumptionCacheTracker>();
00043       AU.addRequired<DominatorTreeWrapperPass>();
00044       AU.setPreservesCFG();
00045       // This is a cluster of orthogonal Transforms
00046       AU.addPreserved<UnifyFunctionExitNodes>();
00047       AU.addPreservedID(LowerSwitchID);
00048       AU.addPreservedID(LowerInvokePassID);
00049     }
00050   };
00051 }  // end of anonymous namespace
00052 
00053 char PromotePass::ID = 0;
00054 INITIALIZE_PASS_BEGIN(PromotePass, "mem2reg", "Promote Memory to Register",
00055                 false, false)
00056 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
00057 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
00058 INITIALIZE_PASS_END(PromotePass, "mem2reg", "Promote Memory to Register",
00059                 false, false)
00060 
00061 bool PromotePass::runOnFunction(Function &F) {
00062   std::vector<AllocaInst*> Allocas;
00063 
00064   BasicBlock &BB = F.getEntryBlock();  // Get the entry node for the function
00065 
00066   bool Changed  = false;
00067 
00068   DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
00069   AssumptionCache &AC =
00070       getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
00071 
00072   while (1) {
00073     Allocas.clear();
00074 
00075     // Find allocas that are safe to promote, by looking at all instructions in
00076     // the entry node
00077     for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
00078       if (AllocaInst *AI = dyn_cast<AllocaInst>(I))       // Is it an alloca?
00079         if (isAllocaPromotable(AI))
00080           Allocas.push_back(AI);
00081 
00082     if (Allocas.empty()) break;
00083 
00084     PromoteMemToReg(Allocas, DT, nullptr, &AC);
00085     NumPromoted += Allocas.size();
00086     Changed = true;
00087   }
00088 
00089   return Changed;
00090 }
00091 
00092 // createPromoteMemoryToRegister - Provide an entry point to create this pass.
00093 //
00094 FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
00095   return new PromotePass();
00096 }