blob: dab4b0e53c5eef648a3112c50a26270c11bec60f [file] [log] [blame]
Chris Lattner927fec32004-02-11 06:10:05 +00001//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a concrete implementation of profiling information that
11// loads the information from a profile dump file.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Pass.h"
16#include "llvm/Analysis/ProfileInfo.h"
17#include "llvm/Analysis/ProfileInfoLoader.h"
Chris Lattner945871d2004-02-11 18:21:05 +000018#include "Support/CommandLine.h"
Chris Lattner927fec32004-02-11 06:10:05 +000019using namespace llvm;
20
21namespace {
Chris Lattner945871d2004-02-11 18:21:05 +000022 cl::opt<std::string>
23 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
Chris Lattnerd6a556b2004-02-11 19:14:04 +000024 cl::value_desc("filename"),
25 cl::desc("Profile file loaded by -profile-loader"));
Chris Lattner945871d2004-02-11 18:21:05 +000026
Chris Lattner927fec32004-02-11 06:10:05 +000027 class LoaderPass : public Pass, public ProfileInfo {
28 std::string Filename;
Chris Lattner945871d2004-02-11 18:21:05 +000029 std::map<BasicBlock*, unsigned> ExecutionCounts;
Chris Lattner927fec32004-02-11 06:10:05 +000030 public:
Chris Lattner945871d2004-02-11 18:21:05 +000031 LoaderPass(const std::string &filename = "")
32 : Filename(filename) {
33 if (filename.empty()) Filename = ProfileInfoFilename;
34 }
Chris Lattner927fec32004-02-11 06:10:05 +000035
36 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
37 AU.setPreservesAll();
38 }
39
40 virtual const char *getPassName() const {
41 return "Profiling information loader";
42 }
43
44 /// run - Load the profile information from the specified file.
45 virtual bool run(Module &M);
46
Chris Lattner945871d2004-02-11 18:21:05 +000047 virtual unsigned getExecutionCount(BasicBlock *BB) {
48 std::map<BasicBlock*, unsigned>::iterator I = ExecutionCounts.find(BB);
49 return I != ExecutionCounts.end() ? I->second : 0;
50 }
Chris Lattner927fec32004-02-11 06:10:05 +000051 };
52
53 RegisterOpt<LoaderPass>
54 X("profile-loader", "Load profile information from llvmprof.out");
55
56 RegisterAnalysisGroup<ProfileInfo, LoaderPass> Y;
57} // End of anonymous namespace
58
59
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.
63Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
64 return new LoaderPass(Filename);
65}
66
67bool LoaderPass::run(Module &M) {
Chris Lattner945871d2004-02-11 18:21:05 +000068 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());
73 }
Chris Lattner927fec32004-02-11 06:10:05 +000074 return false;
75}