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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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" |
| 19 | #include <set> |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Chris Lattner | ecefc96 | 2004-02-11 06:10:18 +0000 | [diff] [blame] | 22 | // Register the ProfileInfo interface, providing a nice name to refer to. |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | RegisterAnalysisGroup<ProfileInfo> Z("Profile Information"); |
| 25 | } |
| 26 | |
| 27 | ProfileInfo::~ProfileInfo() {} |
| 28 | |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 29 | unsigned ProfileInfo::getExecutionCount(BasicBlock *BB) const { |
| 30 | pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 31 | |
| 32 | // Are there zero predecessors of this block? |
| 33 | if (PI == PE) { |
| 34 | // If this is the entry block, look for the Null -> Entry edge. |
| 35 | if (BB == &BB->getParent()->front()) |
| 36 | return getEdgeWeight(0, BB); |
| 37 | else |
| 38 | return 0; // Otherwise, this is a dead block. |
| 39 | } |
| 40 | |
| 41 | // Otherwise, if there are predecessors, the execution count of this block is |
| 42 | // the sum of the edge frequencies from the incoming edges. Note that if |
| 43 | // there are multiple edges from a predecessor to this block that we don't |
| 44 | // want to count its weight multiple times. For this reason, we keep track of |
| 45 | // the predecessors we've seen and only count them if we haven't run into them |
| 46 | // yet. |
| 47 | // |
| 48 | // We don't want to create an std::set unless we are dealing with a block that |
| 49 | // has a LARGE number of in-edges. Handle the common case of having only a |
| 50 | // few in-edges with special code. |
| 51 | // |
| 52 | BasicBlock *FirstPred = *PI; |
| 53 | unsigned Count = getEdgeWeight(FirstPred, BB); |
| 54 | ++PI; |
| 55 | if (PI == PE) return Count; // Quick exit for single predecessor blocks |
| 56 | |
| 57 | BasicBlock *SecondPred = *PI; |
| 58 | if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB); |
| 59 | ++PI; |
| 60 | if (PI == PE) return Count; // Quick exit for two predecessor blocks |
| 61 | |
| 62 | BasicBlock *ThirdPred = *PI; |
| 63 | if (ThirdPred != FirstPred && ThirdPred != SecondPred) |
| 64 | Count += getEdgeWeight(ThirdPred, BB); |
| 65 | ++PI; |
| 66 | if (PI == PE) return Count; // Quick exit for three predecessor blocks |
| 67 | |
| 68 | std::set<BasicBlock*> ProcessedPreds; |
| 69 | ProcessedPreds.insert(FirstPred); |
| 70 | ProcessedPreds.insert(SecondPred); |
| 71 | ProcessedPreds.insert(ThirdPred); |
| 72 | for (; PI != PE; ++PI) |
| 73 | if (ProcessedPreds.insert(*PI).second) |
| 74 | Count += getEdgeWeight(*PI, BB); |
| 75 | return Count; |
| 76 | } |
| 77 | |
| 78 | |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 79 | |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | // NoProfile ProfileInfo implementation |
| 82 | // |
| 83 | |
| 84 | namespace { |
Chris Lattner | 62e84f3 | 2004-03-08 21:30:35 +0000 | [diff] [blame] | 85 | struct NoProfileInfo : public ImmutablePass, public ProfileInfo {}; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 87 | // Register this pass... |
| 88 | RegisterOpt<NoProfileInfo> |
| 89 | X("no-profile", "No Profile Information"); |
| 90 | |
Chris Lattner | ecefc96 | 2004-02-11 06:10:18 +0000 | [diff] [blame] | 91 | // Declare that we implement the ProfileInfo interface |
Chris Lattner | b060194 | 2004-02-11 04:47:54 +0000 | [diff] [blame] | 92 | RegisterAnalysisGroup<ProfileInfo, NoProfileInfo, true> Y; |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 93 | } // End of anonymous namespace |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 94 | |
| 95 | ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); } |