Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 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/Pass.h" |
| 16 | #include "llvm/Analysis/ProfileInfo.h" |
| 17 | #include "llvm/Analysis/ProfileInfoLoader.h" |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame^] | 18 | #include "Support/CommandLine.h" |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame^] | 22 | cl::opt<std::string> |
| 23 | ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"), |
| 24 | cl::desc("")); |
| 25 | |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 26 | class LoaderPass : public Pass, public ProfileInfo { |
| 27 | std::string Filename; |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame^] | 28 | std::map<BasicBlock*, unsigned> ExecutionCounts; |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 29 | public: |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame^] | 30 | LoaderPass(const std::string &filename = "") |
| 31 | : Filename(filename) { |
| 32 | if (filename.empty()) Filename = ProfileInfoFilename; |
| 33 | } |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 34 | |
| 35 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 36 | AU.setPreservesAll(); |
| 37 | } |
| 38 | |
| 39 | virtual const char *getPassName() const { |
| 40 | return "Profiling information loader"; |
| 41 | } |
| 42 | |
| 43 | /// run - Load the profile information from the specified file. |
| 44 | virtual bool run(Module &M); |
| 45 | |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame^] | 46 | virtual unsigned getExecutionCount(BasicBlock *BB) { |
| 47 | std::map<BasicBlock*, unsigned>::iterator I = ExecutionCounts.find(BB); |
| 48 | return I != ExecutionCounts.end() ? I->second : 0; |
| 49 | } |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | RegisterOpt<LoaderPass> |
| 53 | X("profile-loader", "Load profile information from llvmprof.out"); |
| 54 | |
| 55 | RegisterAnalysisGroup<ProfileInfo, LoaderPass> Y; |
| 56 | } // End of anonymous namespace |
| 57 | |
| 58 | |
| 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); |
| 64 | } |
| 65 | |
| 66 | bool LoaderPass::run(Module &M) { |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame^] | 67 | ProfileInfoLoader PIL("opt", Filename, M); |
| 68 | if (PIL.hasAccurateBlockCounts()) { |
| 69 | std::vector<std::pair<BasicBlock*, unsigned> > Counts; |
| 70 | PIL.getBlockCounts(Counts); |
| 71 | ExecutionCounts.insert(Counts.begin(), Counts.end()); |
| 72 | } |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 73 | return false; |
| 74 | } |