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 is distributed under the University of Illinois Open Source
00006 // 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 "llvm/Support/Compiler.h"
00023 #include "llvm/Support/Streams.h"
00024 using namespace llvm;
00025 
00026 static cl::opt<std::string>
00027 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
00028                     cl::value_desc("filename"),
00029                     cl::desc("Profile file loaded by -profile-loader"));
00030 
00031 namespace {
00032   class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
00033     std::string Filename;
00034   public:
00035     static char ID; // Class identification, replacement for typeinfo
00036     explicit LoaderPass(const std::string &filename = "")
00037       : ModulePass(&ID), Filename(filename) {
00038       if (filename.empty()) Filename = ProfileInfoFilename;
00039     }
00040 
00041     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00042       AU.setPreservesAll();
00043     }
00044 
00045     virtual const char *getPassName() const {
00046       return "Profiling information loader";
00047     }
00048 
00049     /// run - Load the profile information from the specified file.
00050     virtual bool runOnModule(Module &M);
00051   };
00052 }  // End of anonymous namespace
00053 
00054 char LoaderPass::ID = 0;
00055 static RegisterPass<LoaderPass>
00056 X("profile-loader", "Load profile information from llvmprof.out", false, true);
00057 
00058 static RegisterAnalysisGroup<ProfileInfo> Y(X);
00059 
00060 ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
00061 
00062 /// createProfileLoaderPass - This function returns a Pass that loads the
00063 /// profiling information for the module from the specified filename, making it
00064 /// available to the optimizers.
00065 Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
00066   return new LoaderPass(Filename);
00067 }
00068 
00069 bool LoaderPass::runOnModule(Module &M) {
00070   ProfileInfoLoader PIL("profile-loader", Filename, M);
00071   EdgeCounts.clear();
00072   bool PrintedWarning = false;
00073 
00074   std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
00075   PIL.getEdgeCounts(ECs);
00076   for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
00077     BasicBlock *BB = ECs[i].first.first;
00078     unsigned SuccNum = ECs[i].first.second;
00079     TerminatorInst *TI = BB->getTerminator();
00080     if (SuccNum >= TI->getNumSuccessors()) {
00081       if (!PrintedWarning) {
00082         cerr << "WARNING: profile information is inconsistent with "
00083              << "the current program!\n";
00084         PrintedWarning = true;
00085       }
00086     } else {
00087       EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;
00088     }
00089   }
00090 
00091   return false;
00092 }