1 //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a concrete implementation of profiling information that
11 // loads the information from a profile dump file.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Pass.h"
16 #include "llvm/Analysis/ProfileInfo.h"
17 #include "llvm/Analysis/ProfileInfoLoader.h"
18 #include "Support/CommandLine.h"
23 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
24 cl::value_desc("filename"),
25 cl::desc("Profile file loaded by -profile-loader"));
27 class LoaderPass : public Pass, public ProfileInfo {
29 std::map<BasicBlock*, unsigned> ExecutionCounts;
31 LoaderPass(const std::string &filename = "")
32 : Filename(filename) {
33 if (filename.empty()) Filename = ProfileInfoFilename;
36 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 virtual const char *getPassName() const {
41 return "Profiling information loader";
44 /// run - Load the profile information from the specified file.
45 virtual bool run(Module &M);
47 virtual unsigned getExecutionCount(BasicBlock *BB) {
48 std::map<BasicBlock*, unsigned>::iterator I = ExecutionCounts.find(BB);
49 return I != ExecutionCounts.end() ? I->second : 0;
53 RegisterOpt<LoaderPass>
54 X("profile-loader", "Load profile information from llvmprof.out");
56 RegisterAnalysisGroup<ProfileInfo, LoaderPass> Y;
57 } // End of anonymous namespace
60 /// createProfileLoaderPass - This function returns a Pass that loads the
61 /// profiling information for the module from the specified filename, making it
62 /// available to the optimizers.
63 Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
64 return new LoaderPass(Filename);
67 bool LoaderPass::run(Module &M) {
68 ProfileInfoLoader PIL("opt", Filename, M);
69 if (PIL.hasAccurateBlockCounts()) {
70 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
71 PIL.getBlockCounts(Counts);
72 ExecutionCounts.insert(Counts.begin(), Counts.end());