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