LLVM API Documentation

ProfileInfoLoaderPass.cpp

Go to the documentation of this file.
00001 //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements a concrete implementation of profiling information that
00011 // loads the information from a profile dump file.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/BasicBlock.h"
00016 #include "llvm/InstrTypes.h"
00017 #include "llvm/Pass.h"
00018 #include "llvm/Analysis/Passes.h"
00019 #include "llvm/Analysis/ProfileInfo.h"
00020 #include "llvm/Analysis/ProfileInfoLoader.h"
00021 #include "llvm/Support/CommandLine.h"
00022 #include <iostream>
00023 
00024 using namespace llvm;
00025 
00026 namespace {
00027   cl::opt<std::string>
00028   ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
00029                       cl::value_desc("filename"),
00030                       cl::desc("Profile file loaded by -profile-loader"));
00031 
00032   class LoaderPass : public ModulePass, public ProfileInfo {
00033     std::string Filename;
00034   public:
00035     LoaderPass(const std::string &filename = "")
00036       : Filename(filename) {
00037       if (filename.empty()) Filename = ProfileInfoFilename;
00038     }
00039 
00040     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00041       AU.setPreservesAll();
00042     }
00043 
00044     virtual const char *getPassName() const {
00045       return "Profiling information loader";
00046     }
00047 
00048     /// run - Load the profile information from the specified file.
00049     virtual bool runOnModule(Module &M);
00050   };
00051 
00052   RegisterPass<LoaderPass>
00053   X("profile-loader", "Load profile information from llvmprof.out",
00054     PassInfo::Analysis|PassInfo::Optimization);
00055 
00056   RegisterAnalysisGroup<ProfileInfo, LoaderPass> Y;
00057 }  // End of anonymous namespace
00058 
00059 ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
00060 
00061 /// createProfileLoaderPass - This function returns a Pass that loads the
00062 /// profiling information for the module from the specified filename, making it
00063 /// available to the optimizers.
00064 Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
00065   return new LoaderPass(Filename);
00066 }
00067 
00068 bool LoaderPass::runOnModule(Module &M) {
00069   ProfileInfoLoader PIL("profile-loader", Filename, M);
00070   EdgeCounts.clear();
00071   bool PrintedWarning = false;
00072 
00073   std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
00074   PIL.getEdgeCounts(ECs);
00075   for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
00076     BasicBlock *BB = ECs[i].first.first;
00077     unsigned SuccNum = ECs[i].first.second;
00078     TerminatorInst *TI = BB->getTerminator();
00079     if (SuccNum >= TI->getNumSuccessors()) {
00080       if (!PrintedWarning) {
00081         std::cerr << "WARNING: profile information is inconsistent with "
00082                   << "the current program!\n";
00083         PrintedWarning = true;
00084       }
00085     } else {
00086       EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;
00087     }
00088   }
00089 
00090   return false;
00091 }