blob: c1ff8cf563193384f25a485452578f91f88da2b1 [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;
29 public:
Chris Lattner945871d2004-02-11 18:21:05 +000030 LoaderPass(const std::string &filename = "")
31 : Filename(filename) {
32 if (filename.empty()) Filename = ProfileInfoFilename;
33 }
Chris Lattner927fec32004-02-11 06:10:05 +000034
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.setPreservesAll();
37 }
38
39 virtual const char *getPassName() const {
40 return "Profiling information loader";
41 }
42
43 /// run - Load the profile information from the specified file.
44 virtual bool run(Module &M);
Chris Lattner927fec32004-02-11 06:10:05 +000045 };
46
47 RegisterOpt<LoaderPass>
48 X("profile-loader", "Load profile information from llvmprof.out");
49
50 RegisterAnalysisGroup<ProfileInfo, LoaderPass> Y;
51} // End of anonymous namespace
52
53
54/// createProfileLoaderPass - This function returns a Pass that loads the
55/// profiling information for the module from the specified filename, making it
56/// available to the optimizers.
57Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
58 return new LoaderPass(Filename);
59}
60
61bool LoaderPass::run(Module &M) {
Chris Lattner62e84f32004-03-08 21:30:35 +000062 ProfileInfoLoader PIL("profile-loader", Filename, M);
63 ExecutionCounts.clear();
Chris Lattner945871d2004-02-11 18:21:05 +000064 if (PIL.hasAccurateBlockCounts()) {
65 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
66 PIL.getBlockCounts(Counts);
67 ExecutionCounts.insert(Counts.begin(), Counts.end());
68 }
Chris Lattner927fec32004-02-11 06:10:05 +000069 return false;
70}