| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 1 | //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===// |
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
| Chris Lattner | f7a5d98 | 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 | 9bde783 | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/InstrTypes.h" |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
| Jeff Cohen | cede1ce | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/Passes.h" |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ProfileInfo.h" |
| 20 | #include "llvm/Analysis/ProfileInfoLoader.h" |
| Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
| Reid Spencer | f75727a | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
| Bill Wendling | 597d451 | 2006-11-28 22:46:12 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Streams.h" |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| Chris Lattner | f10f6b1 | 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 | 4b095b9 | 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 | f10f6b1 | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 31 | |
| Reid Spencer | f75727a | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 32 | class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo { |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 33 | std::string Filename; |
| 34 | public: |
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 35 | static char ID; // Class identification, replacement for typeinfo |
| Dan Gohman | 34d442f | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 36 | explicit LoaderPass(const std::string &filename = "") |
| Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 37 | : ModulePass((intptr_t)&ID), Filename(filename) { |
| Chris Lattner | f10f6b1 | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 38 | if (filename.empty()) Filename = ProfileInfoFilename; |
| 39 | } |
| Chris Lattner | f7a5d98 | 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 | |
| Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame^] | 49 | /// isAnalysis - Return true if this pass is implementing an analysis pass. |
| 50 | virtual bool isAnalysis() const { return true; } |
| 51 | |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 52 | /// run - Load the profile information from the specified file. |
| Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 53 | virtual bool runOnModule(Module &M); |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 54 | }; |
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 55 | |
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 56 | char LoaderPass::ID = 0; |
| Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 57 | RegisterPass<LoaderPass> |
| Chris Lattner | e7134c5 | 2006-08-21 17:20:01 +0000 | [diff] [blame] | 58 | X("profile-loader", "Load profile information from llvmprof.out"); |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 59 | |
| Chris Lattner | 97c9f20 | 2006-08-28 00:42:29 +0000 | [diff] [blame] | 60 | RegisterAnalysisGroup<ProfileInfo> Y(X); |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 61 | } // End of anonymous namespace |
| 62 | |
| Jeff Cohen | 703f7db | 2005-01-10 03:56:27 +0000 | [diff] [blame] | 63 | ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); } |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 64 | |
| 65 | /// createProfileLoaderPass - This function returns a Pass that loads the |
| 66 | /// profiling information for the module from the specified filename, making it |
| 67 | /// available to the optimizers. |
| 68 | Pass *llvm::createProfileLoaderPass(const std::string &Filename) { |
| 69 | return new LoaderPass(Filename); |
| 70 | } |
| 71 | |
| Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 72 | bool LoaderPass::runOnModule(Module &M) { |
| Chris Lattner | bd481d5 | 2004-03-08 21:30:35 +0000 | [diff] [blame] | 73 | ProfileInfoLoader PIL("profile-loader", Filename, M); |
| Chris Lattner | 9bde783 | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 74 | EdgeCounts.clear(); |
| 75 | bool PrintedWarning = false; |
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 76 | |
| Chris Lattner | 9bde783 | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 77 | std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs; |
| 78 | PIL.getEdgeCounts(ECs); |
| 79 | for (unsigned i = 0, e = ECs.size(); i != e; ++i) { |
| 80 | BasicBlock *BB = ECs[i].first.first; |
| 81 | unsigned SuccNum = ECs[i].first.second; |
| 82 | TerminatorInst *TI = BB->getTerminator(); |
| 83 | if (SuccNum >= TI->getNumSuccessors()) { |
| 84 | if (!PrintedWarning) { |
| Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 85 | cerr << "WARNING: profile information is inconsistent with " |
| 86 | << "the current program!\n"; |
| Chris Lattner | 9bde783 | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 87 | PrintedWarning = true; |
| 88 | } |
| 89 | } else { |
| 90 | EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second; |
| 91 | } |
| Chris Lattner | f10f6b1 | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 92 | } |
| Chris Lattner | 9bde783 | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 93 | |
| Chris Lattner | f7a5d98 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 94 | return false; |
| 95 | } |