ProfileInfoLoaderPass.cpp revision a5370172b64bed5daf8e2869d7bf7cb52f80d6b7
1//===- 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/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 <iostream>
23
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 LoaderPass : public ModulePass, public ProfileInfo {
33    std::string Filename;
34  public:
35    LoaderPass(const std::string &filename = "")
36      : Filename(filename) {
37      if (filename.empty()) Filename = ProfileInfoFilename;
38    }
39
40    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41      AU.setPreservesAll();
42    }
43
44    virtual const char *getPassName() const {
45      return "Profiling information loader";
46    }
47
48    /// run - Load the profile information from the specified file.
49    virtual bool runOnModule(Module &M);
50  };
51
52  RegisterPass<LoaderPass>
53  X("profile-loader", "Load profile information from llvmprof.out");
54
55  RegisterAnalysisGroup<ProfileInfo> Y(X);
56}  // End of anonymous namespace
57
58ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
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::runOnModule(Module &M) {
68  ProfileInfoLoader PIL("profile-loader", Filename, M);
69  EdgeCounts.clear();
70  bool PrintedWarning = false;
71
72  std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
73  PIL.getEdgeCounts(ECs);
74  for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
75    BasicBlock *BB = ECs[i].first.first;
76    unsigned SuccNum = ECs[i].first.second;
77    TerminatorInst *TI = BB->getTerminator();
78    if (SuccNum >= TI->getNumSuccessors()) {
79      if (!PrintedWarning) {
80        std::cerr << "WARNING: profile information is inconsistent with "
81                  << "the current program!\n";
82        PrintedWarning = true;
83      }
84    } else {
85      EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;
86    }
87  }
88
89  return false;
90}
91