1 //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements a concrete implementation of profiling information that
11 // loads the information from a profile dump file.
13 //===----------------------------------------------------------------------===//
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/Streams.h"
27 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
28 cl::value_desc("filename"),
29 cl::desc("Profile file loaded by -profile-loader"));
31 class LoaderPass : public ModulePass, public ProfileInfo {
34 LoaderPass(const std::string &filename = "")
35 : Filename(filename) {
36 if (filename.empty()) Filename = ProfileInfoFilename;
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 virtual const char *getPassName() const {
44 return "Profiling information loader";
47 /// run - Load the profile information from the specified file.
48 virtual bool runOnModule(Module &M);
51 RegisterPass<LoaderPass>
52 X("profile-loader", "Load profile information from llvmprof.out");
54 RegisterAnalysisGroup<ProfileInfo> Y(X);
55 } // End of anonymous namespace
57 ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
59 /// createProfileLoaderPass - This function returns a Pass that loads the
60 /// profiling information for the module from the specified filename, making it
61 /// available to the optimizers.
62 Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
63 return new LoaderPass(Filename);
66 bool LoaderPass::runOnModule(Module &M) {
67 ProfileInfoLoader PIL("profile-loader", Filename, M);
69 bool PrintedWarning = false;
71 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
72 PIL.getEdgeCounts(ECs);
73 for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
74 BasicBlock *BB = ECs[i].first.first;
75 unsigned SuccNum = ECs[i].first.second;
76 TerminatorInst *TI = BB->getTerminator();
77 if (SuccNum >= TI->getNumSuccessors()) {
78 if (!PrintedWarning) {
79 cerr << "WARNING: profile information is inconsistent with "
80 << "the current program!\n";
81 PrintedWarning = true;
84 EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;