blob: b32dcc406ad92209239f29f3eccf227ade57e98a [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
Chris Lattner96ab5ca2004-03-08 22:04:08 +000015#include "llvm/BasicBlock.h"
16#include "llvm/InstrTypes.h"
Chris Lattner927fec32004-02-11 06:10:05 +000017#include "llvm/Pass.h"
18#include "llvm/Analysis/ProfileInfo.h"
19#include "llvm/Analysis/ProfileInfoLoader.h"
Chris Lattner945871d2004-02-11 18:21:05 +000020#include "Support/CommandLine.h"
Chris Lattner927fec32004-02-11 06:10:05 +000021using namespace llvm;
22
23namespace {
Chris Lattner945871d2004-02-11 18:21:05 +000024 cl::opt<std::string>
25 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
Chris Lattnerd6a556b2004-02-11 19:14:04 +000026 cl::value_desc("filename"),
27 cl::desc("Profile file loaded by -profile-loader"));
Chris Lattner945871d2004-02-11 18:21:05 +000028
Chris Lattner927fec32004-02-11 06:10:05 +000029 class LoaderPass : public Pass, public ProfileInfo {
30 std::string Filename;
31 public:
Chris Lattner945871d2004-02-11 18:21:05 +000032 LoaderPass(const std::string &filename = "")
33 : Filename(filename) {
34 if (filename.empty()) Filename = ProfileInfoFilename;
35 }
Chris Lattner927fec32004-02-11 06:10:05 +000036
37 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.setPreservesAll();
39 }
40
41 virtual const char *getPassName() const {
42 return "Profiling information loader";
43 }
44
45 /// run - Load the profile information from the specified file.
46 virtual bool run(Module &M);
Chris Lattner927fec32004-02-11 06:10:05 +000047 };
48
49 RegisterOpt<LoaderPass>
50 X("profile-loader", "Load profile information from llvmprof.out");
51
52 RegisterAnalysisGroup<ProfileInfo, LoaderPass> Y;
53} // End of anonymous namespace
54
55
56/// createProfileLoaderPass - This function returns a Pass that loads the
57/// profiling information for the module from the specified filename, making it
58/// available to the optimizers.
59Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
60 return new LoaderPass(Filename);
61}
62
63bool LoaderPass::run(Module &M) {
Chris Lattner62e84f32004-03-08 21:30:35 +000064 ProfileInfoLoader PIL("profile-loader", Filename, M);
Chris Lattner96ab5ca2004-03-08 22:04:08 +000065 EdgeCounts.clear();
66 bool PrintedWarning = false;
67
68 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
69 PIL.getEdgeCounts(ECs);
70 for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
71 BasicBlock *BB = ECs[i].first.first;
72 unsigned SuccNum = ECs[i].first.second;
73 TerminatorInst *TI = BB->getTerminator();
74 if (SuccNum >= TI->getNumSuccessors()) {
75 if (!PrintedWarning) {
76 std::cerr << "WARNING: profile information is inconsistent with "
77 << "the current program!\n";
78 PrintedWarning = true;
79 }
80 } else {
81 EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;
82 }
Chris Lattner945871d2004-02-11 18:21:05 +000083 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +000084
Chris Lattner927fec32004-02-11 06:10:05 +000085 return false;
86}