Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 1 | //===- ProfileInfo.cpp - Profile Info Interface ---------------------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +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 | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the abstract ProfileInfo interface, and the default |
| 11 | // "no profile" implementation. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ProfileInfo.h" |
| 17 | #include "llvm/Pass.h" |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CFG.h" |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Dan Gohman | 4bac4b9 | 2009-08-26 15:56:38 +0000 | [diff] [blame^] | 20 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 21 | #include <set> |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | ecefc96 | 2004-02-11 06:10:18 +0000 | [diff] [blame] | 24 | // Register the ProfileInfo interface, providing a nice name to refer to. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 25 | static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information"); |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 26 | char ProfileInfo::ID = 0; |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 27 | |
| 28 | ProfileInfo::~ProfileInfo() {} |
| 29 | |
Andreas Neustifter | 96135b6 | 2009-08-24 21:37:48 +0000 | [diff] [blame] | 30 | const double ProfileInfo::MissingValue = -1; |
| 31 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 32 | double ProfileInfo::getExecutionCount(const BasicBlock *BB) { |
| 33 | std::map<const Function*, BlockCounts>::iterator J = |
| 34 | BlockInformation.find(BB->getParent()); |
| 35 | if (J != BlockInformation.end()) { |
| 36 | BlockCounts::iterator I = J->second.find(BB); |
| 37 | if (I != J->second.end()) |
| 38 | return I->second; |
| 39 | } |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | 858cb8a | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 41 | pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB); |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 42 | |
| 43 | // Are there zero predecessors of this block? |
| 44 | if (PI == PE) { |
| 45 | // If this is the entry block, look for the Null -> Entry edge. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 46 | if (BB == &BB->getParent()->getEntryBlock()) |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 47 | return getEdgeWeight(getEdge(0, BB)); |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 48 | else |
| 49 | return 0; // Otherwise, this is a dead block. |
| 50 | } |
| 51 | |
| 52 | // Otherwise, if there are predecessors, the execution count of this block is |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 53 | // the sum of the edge frequencies from the incoming edges. |
Daniel Dunbar | 858cb8a | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 54 | std::set<const BasicBlock*> ProcessedPreds; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 55 | double Count = 0; |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 56 | for (; PI != PE; ++PI) |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 57 | if (ProcessedPreds.insert(*PI).second) { |
| 58 | double w = getEdgeWeight(getEdge(*PI, BB)); |
| 59 | if (w == MissingValue) { |
Andreas Neustifter | 0a324aa | 2009-08-19 05:44:39 +0000 | [diff] [blame] | 60 | Count = MissingValue; |
| 61 | break; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 62 | } |
| 63 | Count += w; |
| 64 | } |
| 65 | |
Andreas Neustifter | 96135b6 | 2009-08-24 21:37:48 +0000 | [diff] [blame] | 66 | if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count; |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 67 | return Count; |
| 68 | } |
| 69 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 70 | double ProfileInfo::getExecutionCount(const Function *F) { |
| 71 | std::map<const Function*, double>::iterator J = |
| 72 | FunctionInformation.find(F); |
| 73 | if (J != FunctionInformation.end()) |
| 74 | return J->second; |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 75 | |
Andreas Neustifter | 3772fb1 | 2009-08-26 13:33:09 +0000 | [diff] [blame] | 76 | // isDeclaration() is checked here and not at start of function to allow |
| 77 | // functions without a body still to have a execution count. |
| 78 | if (F->isDeclaration()) return MissingValue; |
| 79 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 80 | double Count = getExecutionCount(&F->getEntryBlock()); |
Andreas Neustifter | 96135b6 | 2009-08-24 21:37:48 +0000 | [diff] [blame] | 81 | if (Count != MissingValue) FunctionInformation[F] = Count; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 82 | return Count; |
Daniel Dunbar | 858cb8a | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 83 | } |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 84 | |
Dan Gohman | 4bac4b9 | 2009-08-26 15:56:38 +0000 | [diff] [blame^] | 85 | raw_ostream& llvm::operator<<(raw_ostream &O, ProfileInfo::Edge E) { |
| 86 | O << "("; |
| 87 | O << (E.first ? E.first->getNameStr() : "0"); |
| 88 | O << ","; |
| 89 | O << (E.second ? E.second->getNameStr() : "0"); |
| 90 | return O << ")"; |
| 91 | } |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 92 | |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | // NoProfile ProfileInfo implementation |
| 95 | // |
| 96 | |
| 97 | namespace { |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 98 | struct VISIBILITY_HIDDEN NoProfileInfo |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 99 | : public ImmutablePass, public ProfileInfo { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 100 | static char ID; // Class identification, replacement for typeinfo |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 101 | NoProfileInfo() : ImmutablePass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 102 | }; |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 103 | } // End of anonymous namespace |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 104 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 105 | char NoProfileInfo::ID = 0; |
| 106 | // Register this pass... |
| 107 | static RegisterPass<NoProfileInfo> |
| 108 | X("no-profile", "No Profile Information", false, true); |
| 109 | |
| 110 | // Declare that we implement the ProfileInfo interface |
| 111 | static RegisterAnalysisGroup<ProfileInfo, true> Y(X); |
| 112 | |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 113 | ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); } |