ProfileInfoLoaderPass.cpp revision 4ee451de366474b9c228b4e5fa573795a715216d
1//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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 "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 36 explicit LoaderPass(const std::string &filename = "") 37 : 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 49 /// run - Load the profile information from the specified file. 50 virtual bool runOnModule(Module &M); 51 }; 52 53 char LoaderPass::ID = 0; 54 RegisterPass<LoaderPass> 55 X("profile-loader", "Load profile information from llvmprof.out"); 56 57 RegisterAnalysisGroup<ProfileInfo> Y(X); 58} // End of anonymous namespace 59 60ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); } 61 62/// createProfileLoaderPass - This function returns a Pass that loads the 63/// profiling information for the module from the specified filename, making it 64/// available to the optimizers. 65Pass *llvm::createProfileLoaderPass(const std::string &Filename) { 66 return new LoaderPass(Filename); 67} 68 69bool LoaderPass::runOnModule(Module &M) { 70 ProfileInfoLoader PIL("profile-loader", Filename, M); 71 EdgeCounts.clear(); 72 bool PrintedWarning = false; 73 74 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs; 75 PIL.getEdgeCounts(ECs); 76 for (unsigned i = 0, e = ECs.size(); i != e; ++i) { 77 BasicBlock *BB = ECs[i].first.first; 78 unsigned SuccNum = ECs[i].first.second; 79 TerminatorInst *TI = BB->getTerminator(); 80 if (SuccNum >= TI->getNumSuccessors()) { 81 if (!PrintedWarning) { 82 cerr << "WARNING: profile information is inconsistent with " 83 << "the current program!\n"; 84 PrintedWarning = true; 85 } 86 } else { 87 EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second; 88 } 89 } 90 91 return false; 92} 93