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 | // |
Chris Lattner | 4ee451d | 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 | 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" |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ProfileInfo.h" |
| 21 | #include "llvm/Analysis/ProfileInfoLoader.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Streams.h" |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 27 | static 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")); |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 31 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 32 | namespace { |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 33 | class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo { |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 34 | std::string Filename; |
| 35 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 36 | static char ID; // Class identification, replacement for typeinfo |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 37 | explicit LoaderPass(const std::string &filename = "") |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 38 | : ModulePass(&ID), Filename(filename) { |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 39 | if (filename.empty()) Filename = ProfileInfoFilename; |
| 40 | } |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 41 | |
| 42 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 43 | AU.setPreservesAll(); |
| 44 | } |
| 45 | |
| 46 | virtual const char *getPassName() const { |
| 47 | return "Profiling information loader"; |
| 48 | } |
| 49 | |
| 50 | /// run - Load the profile information from the specified file. |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 51 | virtual bool runOnModule(Module &M); |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 52 | }; |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 53 | } // End of anonymous namespace |
| 54 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 55 | char LoaderPass::ID = 0; |
| 56 | static RegisterPass<LoaderPass> |
| 57 | X("profile-loader", "Load profile information from llvmprof.out", false, true); |
| 58 | |
| 59 | static RegisterAnalysisGroup<ProfileInfo> Y(X); |
| 60 | |
Jeff Cohen | 6e400f7 | 2005-01-10 03:56:27 +0000 | [diff] [blame] | 61 | ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); } |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 62 | |
| 63 | /// createProfileLoaderPass - This function returns a Pass that loads the |
| 64 | /// profiling information for the module from the specified filename, making it |
| 65 | /// available to the optimizers. |
| 66 | Pass *llvm::createProfileLoaderPass(const std::string &Filename) { |
| 67 | return new LoaderPass(Filename); |
| 68 | } |
| 69 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 70 | bool LoaderPass::runOnModule(Module &M) { |
Chris Lattner | 62e84f3 | 2004-03-08 21:30:35 +0000 | [diff] [blame] | 71 | ProfileInfoLoader PIL("profile-loader", Filename, M); |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 72 | EdgeCounts.clear(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 73 | |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 74 | std::vector<unsigned> ECs = PIL.getRawEdgeCounts(); |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 75 | std::vector<unsigned> BCs = PIL.getRawBlockCounts(); |
| 76 | std::vector<unsigned> FCs = PIL.getRawFunctionCounts(); |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 77 | // Instrument all of the edges... |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 78 | unsigned ei = 0; |
| 79 | unsigned bi = 0; |
| 80 | unsigned fi = 0; |
| 81 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 82 | if (F->isDeclaration()) continue; |
| 83 | if (fi<FCs.size()) FunctionCounts[F] = FCs[fi++]; |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 84 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 85 | if (bi<BCs.size()) BlockCounts[BB] = BCs[bi++]; |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 86 | // Okay, we have to add a counter of each outgoing edge. If the |
| 87 | // outgoing edge is not critical don't split it, just insert the counter |
| 88 | // in the source or destination of the edge. |
| 89 | TerminatorInst *TI = BB->getTerminator(); |
| 90 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 91 | if (ei < ECs.size()) |
| 92 | EdgeCounts[std::make_pair(BB, TI->getSuccessor(s))]+= ECs[ei++]; |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 93 | } |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 94 | } |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 95 | } |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 97 | if (ei != ECs.size()) { |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 98 | cerr << "WARNING: profile information is inconsistent with " |
| 99 | << "the current program!\n"; |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 100 | } |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 102 | return false; |
| 103 | } |