blob: e18625f8e76514ee814b2299ecdec06363f44dd0 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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/BasicBlock.h"
16#include "llvm/InstrTypes.h"
17#include "llvm/Pass.h"
18#include "llvm/Analysis/Passes.h"
19#include "llvm/Analysis/ProfileInfo.h"
20#include "llvm/Analysis/ProfileInfoLoader.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/Streams.h"
24using namespace llvm;
25
26namespace {
27 cl::opt<std::string>
28 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
29 cl::value_desc("filename"),
30 cl::desc("Profile file loaded by -profile-loader"));
31
32 class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
33 std::string Filename;
34 public:
35 static char ID; // Class identification, replacement for typeinfo
Dan Gohman34c280e2007-08-01 15:32:29 +000036 explicit LoaderPass(const std::string &filename = "")
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 : ModulePass((intptr_t)&ID), Filename(filename) {
38 if (filename.empty()) Filename = ProfileInfoFilename;
39 }
40
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.setPreservesAll();
43 }
44
45 virtual const char *getPassName() const {
46 return "Profiling information loader";
47 }
48
Devang Patel2b4fa682008-03-18 00:39:19 +000049 /// isAnalysis - Return true if this pass is implementing an analysis pass.
50 virtual bool isAnalysis() const { return true; }
51
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 /// run - Load the profile information from the specified file.
53 virtual bool runOnModule(Module &M);
54 };
55
56 char LoaderPass::ID = 0;
57 RegisterPass<LoaderPass>
58 X("profile-loader", "Load profile information from llvmprof.out");
59
60 RegisterAnalysisGroup<ProfileInfo> Y(X);
61} // End of anonymous namespace
62
63ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
64
65/// createProfileLoaderPass - This function returns a Pass that loads the
66/// profiling information for the module from the specified filename, making it
67/// available to the optimizers.
68Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
69 return new LoaderPass(Filename);
70}
71
72bool LoaderPass::runOnModule(Module &M) {
73 ProfileInfoLoader PIL("profile-loader", Filename, M);
74 EdgeCounts.clear();
75 bool PrintedWarning = false;
76
77 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
78 PIL.getEdgeCounts(ECs);
79 for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
80 BasicBlock *BB = ECs[i].first.first;
81 unsigned SuccNum = ECs[i].first.second;
82 TerminatorInst *TI = BB->getTerminator();
83 if (SuccNum >= TI->getNumSuccessors()) {
84 if (!PrintedWarning) {
85 cerr << "WARNING: profile information is inconsistent with "
86 << "the current program!\n";
87 PrintedWarning = true;
88 }
89 } else {
90 EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;
91 }
92 }
93
94 return false;
95}