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