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" |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 20 | #include <set> |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Chris Lattner | ecefc96 | 2004-02-11 06:10:18 +0000 | [diff] [blame] | 23 | // Register the ProfileInfo interface, providing a nice name to refer to. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 24 | static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information"); |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 25 | char ProfileInfo::ID = 0; |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 26 | |
| 27 | ProfileInfo::~ProfileInfo() {} |
| 28 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 29 | double ProfileInfo::getExecutionCount(const BasicBlock *BB) { |
| 30 | std::map<const Function*, BlockCounts>::iterator J = |
| 31 | BlockInformation.find(BB->getParent()); |
| 32 | if (J != BlockInformation.end()) { |
| 33 | BlockCounts::iterator I = J->second.find(BB); |
| 34 | if (I != J->second.end()) |
| 35 | return I->second; |
| 36 | } |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 37 | |
Daniel Dunbar | 858cb8a | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 38 | pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB); |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 39 | |
| 40 | // Are there zero predecessors of this block? |
| 41 | if (PI == PE) { |
| 42 | // If this is the entry block, look for the Null -> Entry edge. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 43 | if (BB == &BB->getParent()->getEntryBlock()) |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 44 | return getEdgeWeight(getEdge(0, BB)); |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 45 | else |
| 46 | return 0; // Otherwise, this is a dead block. |
| 47 | } |
| 48 | |
| 49 | // Otherwise, if there are predecessors, the execution count of this block is |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 50 | // the sum of the edge frequencies from the incoming edges. |
Daniel Dunbar | 858cb8a | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 51 | std::set<const BasicBlock*> ProcessedPreds; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 52 | double Count = 0; |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 53 | for (; PI != PE; ++PI) |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 54 | if (ProcessedPreds.insert(*PI).second) { |
| 55 | double w = getEdgeWeight(getEdge(*PI, BB)); |
| 56 | if (w == MissingValue) { |
| 57 | Count = MissingValue; break; |
| 58 | } |
| 59 | Count += w; |
| 60 | } |
| 61 | |
| 62 | BlockInformation[BB->getParent()][BB] = Count; |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 63 | return Count; |
| 64 | } |
| 65 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 66 | double ProfileInfo::getExecutionCount(const Function *F) { |
| 67 | std::map<const Function*, double>::iterator J = |
| 68 | FunctionInformation.find(F); |
| 69 | if (J != FunctionInformation.end()) |
| 70 | return J->second; |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 71 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 72 | double Count = getExecutionCount(&F->getEntryBlock()); |
| 73 | FunctionInformation[F] = Count; |
| 74 | return Count; |
Daniel Dunbar | 858cb8a | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 75 | } |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 77 | |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | // NoProfile ProfileInfo implementation |
| 80 | // |
| 81 | |
| 82 | namespace { |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 83 | struct VISIBILITY_HIDDEN NoProfileInfo |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 84 | : public ImmutablePass, public ProfileInfo { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 85 | static char ID; // Class identification, replacement for typeinfo |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 86 | NoProfileInfo() : ImmutablePass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 87 | }; |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 88 | } // End of anonymous namespace |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 89 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 90 | char NoProfileInfo::ID = 0; |
| 91 | // Register this pass... |
| 92 | static RegisterPass<NoProfileInfo> |
| 93 | X("no-profile", "No Profile Information", false, true); |
| 94 | |
| 95 | // Declare that we implement the ProfileInfo interface |
| 96 | static RegisterAnalysisGroup<ProfileInfo, true> Y(X); |
| 97 | |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 98 | ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); } |