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