Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 1 | //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 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. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/InstrTypes.h" |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ProfileInfo.h" |
| 20 | #include "llvm/Analysis/ProfileInfoLoader.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Streams.h" |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 27 | cl::opt<std::string> |
| 28 | ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"), |
Chris Lattner | d6a556b | 2004-02-11 19:14:04 +0000 | [diff] [blame] | 29 | cl::value_desc("filename"), |
| 30 | cl::desc("Profile file loaded by -profile-loader")); |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 31 | |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 32 | class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo { |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 33 | std::string Filename; |
| 34 | public: |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 35 | static const int ID; // Class identification, replacement for typeinfo |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 36 | LoaderPass(const std::string &filename = "") |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 37 | : ModulePass((intptr_t)&ID), Filename(filename) { |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 38 | if (filename.empty()) Filename = ProfileInfoFilename; |
| 39 | } |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 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. |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 50 | virtual bool runOnModule(Module &M); |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 51 | }; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 52 | |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 53 | const int LoaderPass::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 54 | RegisterPass<LoaderPass> |
Chris Lattner | 3dd965c | 2006-08-21 17:20:01 +0000 | [diff] [blame] | 55 | X("profile-loader", "Load profile information from llvmprof.out"); |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 56 | |
Chris Lattner | a537017 | 2006-08-28 00:42:29 +0000 | [diff] [blame] | 57 | RegisterAnalysisGroup<ProfileInfo> Y(X); |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 58 | } // End of anonymous namespace |
| 59 | |
Jeff Cohen | 6e400f7 | 2005-01-10 03:56:27 +0000 | [diff] [blame] | 60 | ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); } |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 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. |
| 65 | Pass *llvm::createProfileLoaderPass(const std::string &Filename) { |
| 66 | return new LoaderPass(Filename); |
| 67 | } |
| 68 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 69 | bool LoaderPass::runOnModule(Module &M) { |
Chris Lattner | 62e84f3 | 2004-03-08 21:30:35 +0000 | [diff] [blame] | 70 | ProfileInfoLoader PIL("profile-loader", Filename, M); |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 71 | EdgeCounts.clear(); |
| 72 | bool PrintedWarning = false; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 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) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 82 | cerr << "WARNING: profile information is inconsistent with " |
| 83 | << "the current program!\n"; |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 84 | PrintedWarning = true; |
| 85 | } |
| 86 | } else { |
| 87 | EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second; |
| 88 | } |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 89 | } |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 91 | return false; |
| 92 | } |