blob: a4d41c756e35468ab9556526982496b684b7a245 [file] [log] [blame]
Andreas Neustifterf771dae2009-09-01 19:03:44 +00001//===- OptimalEdgeProfiling.cpp - Insert counters for opt. edge profiling -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass instruments the specified program with counters for edge profiling.
11// Edge profiling can give a reasonable approximation of the hot paths through a
12// program, and is used for a wide variety of program transformations.
13//
14//===----------------------------------------------------------------------===//
15#define DEBUG_TYPE "insert-optimal-edge-profiling"
16#include "ProfilingUtils.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000017#include "llvm/Module.h"
18#include "llvm/Pass.h"
19#include "llvm/Analysis/Passes.h"
Andreas Neustiftered1ac4a2009-09-04 12:34:44 +000020#include "llvm/Analysis/ProfileInfo.h"
Andreas Neustifter92332722009-09-16 11:35:50 +000021#include "llvm/Analysis/ProfileInfoLoader.h"
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000022#include "llvm/Support/raw_ostream.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Transforms/Utils/BasicBlockUtils.h"
25#include "llvm/Transforms/Instrumentation.h"
Andreas Neustifter39859432009-09-03 08:52:52 +000026#include "llvm/ADT/DenseSet.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000027#include "llvm/ADT/Statistic.h"
28#include "MaximumSpanningTree.h"
29#include <set>
30using namespace llvm;
31
32STATISTIC(NumEdgesInserted, "The # of edges inserted.");
33
34namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000035 class OptimalEdgeProfiler : public ModulePass {
Andreas Neustifterf771dae2009-09-01 19:03:44 +000036 bool runOnModule(Module &M);
Andreas Neustifterf771dae2009-09-01 19:03:44 +000037 public:
38 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000039 OptimalEdgeProfiler() : ModulePass(ID) {}
Andreas Neustifterf771dae2009-09-01 19:03:44 +000040
41 void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequiredID(ProfileEstimatorPassID);
43 AU.addRequired<ProfileInfo>();
44 }
45
46 virtual const char *getPassName() const {
47 return "Optimal Edge Profiler";
48 }
49 };
50}
51
52char OptimalEdgeProfiler::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000053INITIALIZE_PASS_BEGIN(OptimalEdgeProfiler, "insert-optimal-edge-profiling",
54 "Insert optimal instrumentation for edge profiling",
55 false, false)
56INITIALIZE_PASS_DEPENDENCY(ProfileEstimatorPass)
57INITIALIZE_AG_DEPENDENCY(ProfileInfo)
58INITIALIZE_PASS_END(OptimalEdgeProfiler, "insert-optimal-edge-profiling",
Owen Andersond13db2c2010-07-21 22:09:45 +000059 "Insert optimal instrumentation for edge profiling",
Owen Andersonce665bd2010-10-07 22:25:06 +000060 false, false)
Andreas Neustifterf771dae2009-09-01 19:03:44 +000061
62ModulePass *llvm::createOptimalEdgeProfilerPass() {
63 return new OptimalEdgeProfiler();
64}
65
66inline static void printEdgeCounter(ProfileInfo::Edge e,
67 BasicBlock* b,
68 unsigned i) {
David Greene0b9afb42010-01-05 01:27:01 +000069 DEBUG(dbgs() << "--Edge Counter for " << (e) << " in " \
Andreas Neustifterf771dae2009-09-01 19:03:44 +000070 << ((b)?(b)->getNameStr():"0") << " (# " << (i) << ")\n");
71}
72
73bool OptimalEdgeProfiler::runOnModule(Module &M) {
74 Function *Main = M.getFunction("main");
75 if (Main == 0) {
76 errs() << "WARNING: cannot insert edge profiling into a module"
77 << " with no main function!\n";
78 return false; // No main, no instrumentation!
79 }
80
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000081 // NumEdges counts all the edges that may be instrumented. Later on its
82 // decided which edges to actually instrument, to achieve optimal profiling.
83 // For the entry block a virtual edge (0,entry) is reserved, for each block
84 // with no successors an edge (BB,0) is reserved. These edges are necessary
85 // to calculate a truly optimal maximum spanning tree and thus an optimal
86 // instrumentation.
Andreas Neustifterf771dae2009-09-01 19:03:44 +000087 unsigned NumEdges = 0;
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000088
Andreas Neustifterf771dae2009-09-01 19:03:44 +000089 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
90 if (F->isDeclaration()) continue;
91 // Reserve space for (0,entry) edge.
92 ++NumEdges;
93 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
94 // Keep track of which blocks need to be instrumented. We don't want to
95 // instrument blocks that are added as the result of breaking critical
96 // edges!
Andreas Neustifterf771dae2009-09-01 19:03:44 +000097 if (BB->getTerminator()->getNumSuccessors() == 0) {
98 // Reserve space for (BB,0) edge.
99 ++NumEdges;
100 } else {
101 NumEdges += BB->getTerminator()->getNumSuccessors();
102 }
103 }
104 }
105
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000106 // In the profiling output a counter for each edge is reserved, but only few
107 // are used. This is done to be able to read back in the profile without
108 // calulating the maximum spanning tree again, instead each edge counter that
109 // is not used is initialised with -1 to signal that this edge counter has to
110 // be calculated from other edge counters on reading the profile info back
111 // in.
112
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000113 const Type *Int32 = Type::getInt32Ty(M.getContext());
114 const ArrayType *ATy = ArrayType::get(Int32, NumEdges);
115 GlobalVariable *Counters =
116 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
117 Constant::getNullValue(ATy), "OptEdgeProfCounters");
118 NumEdgesInserted = 0;
119
120 std::vector<Constant*> Initializer(NumEdges);
Andreas Neustifter92332722009-09-16 11:35:50 +0000121 Constant* Zero = ConstantInt::get(Int32, 0);
122 Constant* Uncounted = ConstantInt::get(Int32, ProfileInfoLoader::Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000123
124 // Instrument all of the edges not in MST...
125 unsigned i = 0;
126 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
127 if (F->isDeclaration()) continue;
David Greene0b9afb42010-01-05 01:27:01 +0000128 DEBUG(dbgs()<<"Working on "<<F->getNameStr()<<"\n");
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000129
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000130 // Calculate a Maximum Spanning Tree with the edge weights determined by
131 // ProfileEstimator. ProfileEstimator also assign weights to the virtual
132 // edges (0,entry) and (BB,0) (for blocks with no successors) and this
133 // edges also participate in the maximum spanning tree calculation.
134 // The third parameter of MaximumSpanningTree() has the effect that not the
135 // actual MST is returned but the edges _not_ in the MST.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000136
Andreas Neustifter39859432009-09-03 08:52:52 +0000137 ProfileInfo::EdgeWeights ECs =
Chris Lattner05273442010-01-20 23:30:28 +0000138 getAnalysis<ProfileInfo>(*F).getEdgeWeights(F);
Andreas Neustifter39859432009-09-03 08:52:52 +0000139 std::vector<ProfileInfo::EdgeWeight> EdgeVector(ECs.begin(), ECs.end());
Andreas Neustiftered1ac4a2009-09-04 12:34:44 +0000140 MaximumSpanningTree<BasicBlock> MST (EdgeVector);
141 std::stable_sort(MST.begin(),MST.end());
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000142
143 // Check if (0,entry) not in the MST. If not, instrument edge
144 // (IncrementCounterInBlock()) and set the counter initially to zero, if
145 // the edge is in the MST the counter is initialised to -1.
146
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000147 BasicBlock *entry = &(F->getEntryBlock());
148 ProfileInfo::Edge edge = ProfileInfo::getEdge(0,entry);
Andreas Neustifter39859432009-09-03 08:52:52 +0000149 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000150 printEdgeCounter(edge,entry,i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000151 IncrementCounterInBlock(entry, i, Counters); ++NumEdgesInserted;
Andreas Neustifter92332722009-09-16 11:35:50 +0000152 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000153 } else{
Andreas Neustifter92332722009-09-16 11:35:50 +0000154 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000155 }
156
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000157 // InsertedBlocks contains all blocks that were inserted for splitting an
158 // edge, this blocks do not have to be instrumented.
Andreas Neustifter39859432009-09-03 08:52:52 +0000159 DenseSet<BasicBlock*> InsertedBlocks;
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000160 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000161 // Check if block was not inserted and thus does not have to be
162 // instrumented.
163 if (InsertedBlocks.count(BB)) continue;
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000164
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000165 // Okay, we have to add a counter of each outgoing edge not in MST. If
166 // the outgoing edge is not critical don't split it, just insert the
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000167 // counter in the source or destination of the edge. Also, if the block
168 // has no successors, the virtual edge (BB,0) is processed.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000169 TerminatorInst *TI = BB->getTerminator();
170 if (TI->getNumSuccessors() == 0) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000171 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,0);
Andreas Neustifter39859432009-09-03 08:52:52 +0000172 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000173 printEdgeCounter(edge,BB,i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000174 IncrementCounterInBlock(BB, i, Counters); ++NumEdgesInserted;
Andreas Neustifter92332722009-09-16 11:35:50 +0000175 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000176 } else{
Andreas Neustifter92332722009-09-16 11:35:50 +0000177 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000178 }
179 }
180 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
181 BasicBlock *Succ = TI->getSuccessor(s);
182 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ);
Andreas Neustifter39859432009-09-03 08:52:52 +0000183 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000184
185 // If the edge is critical, split it.
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000186 bool wasInserted = SplitCriticalEdge(TI, s, this);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000187 Succ = TI->getSuccessor(s);
Andreas Neustifter39859432009-09-03 08:52:52 +0000188 if (wasInserted)
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000189 InsertedBlocks.insert(Succ);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000190
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000191 // Okay, we are guaranteed that the edge is no longer critical. If
192 // we only have a single successor, insert the counter in this block,
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000193 // otherwise insert it in the successor block.
194 if (TI->getNumSuccessors() == 1) {
195 // Insert counter at the start of the block
196 printEdgeCounter(edge,BB,i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000197 IncrementCounterInBlock(BB, i, Counters); ++NumEdgesInserted;
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000198 } else {
199 // Insert counter at the start of the block
200 printEdgeCounter(edge,Succ,i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000201 IncrementCounterInBlock(Succ, i, Counters); ++NumEdgesInserted;
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000202 }
Andreas Neustifter92332722009-09-16 11:35:50 +0000203 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000204 } else {
Andreas Neustifter92332722009-09-16 11:35:50 +0000205 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000206 }
207 }
208 }
209 }
210
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000211 // Check if the number of edges counted at first was the number of edges we
212 // considered for instrumentation.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000213 assert(i==NumEdges && "the number of edges in counting array is wrong");
214
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000215 // Assing the now completely defined initialiser to the array.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000216 Constant *init = ConstantArray::get(ATy, Initializer);
217 Counters->setInitializer(init);
218
219 // Add the initialization call to main.
220 InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters);
221 return true;
222}
223