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" |
Andreas Neustifter | a1eaf2a | 2009-09-09 17:52:57 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Dan Gohman | 4637d17 | 2009-08-26 15:56:38 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Andreas Neustifter | a1eaf2a | 2009-09-09 17:52:57 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include <set> |
| 24 | using namespace llvm; |
| 25 | |
| 26 | // Register the ProfileInfo interface, providing a nice name to refer to. |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 27 | static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 28 | char ProfileInfo::ID = 0; |
| 29 | |
| 30 | ProfileInfo::~ProfileInfo() {} |
| 31 | |
Andreas Neustifter | 92b39ac | 2009-08-24 21:37:48 +0000 | [diff] [blame] | 32 | const double ProfileInfo::MissingValue = -1; |
| 33 | |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 34 | double ProfileInfo::getExecutionCount(const BasicBlock *BB) { |
| 35 | std::map<const Function*, BlockCounts>::iterator J = |
| 36 | BlockInformation.find(BB->getParent()); |
| 37 | if (J != BlockInformation.end()) { |
| 38 | BlockCounts::iterator I = J->second.find(BB); |
| 39 | if (I != J->second.end()) |
| 40 | return I->second; |
| 41 | } |
Daniel Dunbar | 2350509 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 42 | |
Daniel Dunbar | 042521c | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 43 | pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 44 | |
| 45 | // Are there zero predecessors of this block? |
| 46 | if (PI == PE) { |
| 47 | // If this is the entry block, look for the Null -> Entry edge. |
| 48 | if (BB == &BB->getParent()->getEntryBlock()) |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 49 | return getEdgeWeight(getEdge(0, BB)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 50 | else |
| 51 | return 0; // Otherwise, this is a dead block. |
| 52 | } |
| 53 | |
| 54 | // Otherwise, if there are predecessors, the execution count of this block is |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 55 | // the sum of the edge frequencies from the incoming edges. |
Daniel Dunbar | 042521c | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 56 | std::set<const BasicBlock*> ProcessedPreds; |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 57 | double Count = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 58 | for (; PI != PE; ++PI) |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 59 | if (ProcessedPreds.insert(*PI).second) { |
| 60 | double w = getEdgeWeight(getEdge(*PI, BB)); |
| 61 | if (w == MissingValue) { |
Andreas Neustifter | 375a8bc | 2009-08-19 05:44:39 +0000 | [diff] [blame] | 62 | Count = MissingValue; |
| 63 | break; |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 64 | } |
| 65 | Count += w; |
| 66 | } |
| 67 | |
Andreas Neustifter | 92b39ac | 2009-08-24 21:37:48 +0000 | [diff] [blame] | 68 | if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | return Count; |
| 70 | } |
| 71 | |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 72 | double ProfileInfo::getExecutionCount(const Function *F) { |
| 73 | std::map<const Function*, double>::iterator J = |
| 74 | FunctionInformation.find(F); |
| 75 | if (J != FunctionInformation.end()) |
| 76 | return J->second; |
Daniel Dunbar | 2350509 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 77 | |
Andreas Neustifter | 39fd40a | 2009-08-26 13:33:09 +0000 | [diff] [blame] | 78 | // isDeclaration() is checked here and not at start of function to allow |
| 79 | // functions without a body still to have a execution count. |
| 80 | if (F->isDeclaration()) return MissingValue; |
| 81 | |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 82 | double Count = getExecutionCount(&F->getEntryBlock()); |
Andreas Neustifter | 92b39ac | 2009-08-24 21:37:48 +0000 | [diff] [blame] | 83 | if (Count != MissingValue) FunctionInformation[F] = Count; |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 84 | return Count; |
Daniel Dunbar | 042521c | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 85 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 86 | |
Andreas Neustifter | a1eaf2a | 2009-09-09 17:52:57 +0000 | [diff] [blame] | 87 | /// Replaces all occurences of RmBB in the ProfilingInfo with DestBB. |
| 88 | /// This checks all edges of the function the blocks reside in and replaces the |
| 89 | /// occurences of RmBB with DestBB. |
| 90 | void ProfileInfo::replaceAllUses(const BasicBlock *RmBB, |
| 91 | const BasicBlock *DestBB) { |
| 92 | DEBUG(errs() << "Replacing " << RmBB->getNameStr() |
| 93 | << " with " << DestBB->getNameStr() << "\n"); |
| 94 | const Function *F = DestBB->getParent(); |
| 95 | std::map<const Function*, EdgeWeights>::iterator J = |
| 96 | EdgeInformation.find(F); |
| 97 | if (J == EdgeInformation.end()) return; |
| 98 | |
| 99 | for (EdgeWeights::iterator I = J->second.begin(), E = J->second.end(); |
| 100 | I != E; ++I) { |
| 101 | Edge e = I->first; |
| 102 | Edge newedge; bool foundedge = false; |
| 103 | if (e.first == RmBB) { |
| 104 | newedge = getEdge(DestBB, e.second); |
| 105 | foundedge = true; |
| 106 | } |
| 107 | if (e.second == RmBB) { |
| 108 | newedge = getEdge(e.first, DestBB); |
| 109 | foundedge = true; |
| 110 | } |
| 111 | if (foundedge) { |
| 112 | double w = getEdgeWeight(e); |
| 113 | EdgeInformation[F][newedge] = w; |
| 114 | DEBUG(errs() << "Replacing " << e << " with " << newedge << "\n"); |
| 115 | J->second.erase(e); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// Splits an edge in the ProfileInfo and redirects flow over NewBB. |
| 121 | /// Since its possible that there is more than one edge in the CFG from FristBB |
| 122 | /// to SecondBB its necessary to redirect the flow proporionally. |
| 123 | void ProfileInfo::splitEdge(const BasicBlock *FirstBB, |
| 124 | const BasicBlock *SecondBB, |
| 125 | const BasicBlock *NewBB, |
| 126 | bool MergeIdenticalEdges) { |
| 127 | const Function *F = FirstBB->getParent(); |
| 128 | std::map<const Function*, EdgeWeights>::iterator J = |
| 129 | EdgeInformation.find(F); |
| 130 | if (J == EdgeInformation.end()) return; |
| 131 | |
| 132 | // Generate edges and read current weight. |
| 133 | Edge e = getEdge(FirstBB, SecondBB); |
| 134 | Edge n1 = getEdge(FirstBB, NewBB); |
| 135 | Edge n2 = getEdge(NewBB, SecondBB); |
| 136 | EdgeWeights &ECs = J->second; |
| 137 | double w = ECs[e]; |
| 138 | |
| 139 | int succ_count = 0; |
| 140 | if (!MergeIdenticalEdges) { |
| 141 | // First count the edges from FristBB to SecondBB, if there is more than |
| 142 | // one, only slice out a proporional part for NewBB. |
| 143 | for(succ_const_iterator BBI = succ_begin(FirstBB), BBE = succ_end(FirstBB); |
| 144 | BBI != BBE; ++BBI) { |
| 145 | if (*BBI == SecondBB) succ_count++; |
| 146 | } |
| 147 | // When the NewBB is completely new, increment the count by one so that |
| 148 | // the counts are properly distributed. |
| 149 | if (getExecutionCount(NewBB) == ProfileInfo::MissingValue) succ_count++; |
| 150 | } else { |
| 151 | // When the edges are merged anyway, then redirect all flow. |
| 152 | succ_count = 1; |
| 153 | } |
| 154 | |
| 155 | // We know now how many edges there are from FirstBB to SecondBB, reroute a |
| 156 | // proportional part of the edge weight over NewBB. |
| 157 | double neww = w / succ_count; |
| 158 | ECs[n1] += neww; |
| 159 | ECs[n2] += neww; |
| 160 | BlockInformation[F][NewBB] += neww; |
| 161 | if (succ_count == 1) { |
| 162 | ECs.erase(e); |
| 163 | } else { |
| 164 | ECs[e] -= neww; |
| 165 | } |
| 166 | } |
| 167 | |
Dan Gohman | 4637d17 | 2009-08-26 15:56:38 +0000 | [diff] [blame] | 168 | raw_ostream& llvm::operator<<(raw_ostream &O, ProfileInfo::Edge E) { |
| 169 | O << "("; |
| 170 | O << (E.first ? E.first->getNameStr() : "0"); |
| 171 | O << ","; |
| 172 | O << (E.second ? E.second->getNameStr() : "0"); |
| 173 | return O << ")"; |
| 174 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | |
| 176 | //===----------------------------------------------------------------------===// |
| 177 | // NoProfile ProfileInfo implementation |
| 178 | // |
| 179 | |
| 180 | namespace { |
| 181 | struct VISIBILITY_HIDDEN NoProfileInfo |
| 182 | : public ImmutablePass, public ProfileInfo { |
| 183 | static char ID; // Class identification, replacement for typeinfo |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 184 | NoProfileInfo() : ImmutablePass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 185 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 186 | } // End of anonymous namespace |
| 187 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 188 | char NoProfileInfo::ID = 0; |
| 189 | // Register this pass... |
| 190 | static RegisterPass<NoProfileInfo> |
| 191 | X("no-profile", "No Profile Information", false, true); |
| 192 | |
| 193 | // Declare that we implement the ProfileInfo interface |
| 194 | static RegisterAnalysisGroup<ProfileInfo, true> Y(X); |
| 195 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 196 | ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); } |