blob: ae2f2e2e85bcdac55b69fdbd603bf8c22eb130d0 [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 Anderson081c34b2010-10-19 17:21:58 +000039 OptimalEdgeProfiler() : ModulePass(ID) {
40 initializeOptimalEdgeProfilerPass(*PassRegistry::getPassRegistry());
41 }
Andreas Neustifterf771dae2009-09-01 19:03:44 +000042
43 void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.addRequiredID(ProfileEstimatorPassID);
45 AU.addRequired<ProfileInfo>();
46 }
47
48 virtual const char *getPassName() const {
49 return "Optimal Edge Profiler";
50 }
51 };
52}
53
54char OptimalEdgeProfiler::ID = 0;
Andrew Trick04317cc2011-01-29 01:09:53 +000055INITIALIZE_PASS_BEGIN(OptimalEdgeProfiler, "insert-optimal-edge-profiling",
Owen Anderson2ab36d32010-10-12 19:48:12 +000056 "Insert optimal instrumentation for edge profiling",
57 false, false)
58INITIALIZE_PASS_DEPENDENCY(ProfileEstimatorPass)
59INITIALIZE_AG_DEPENDENCY(ProfileInfo)
Andrew Trick04317cc2011-01-29 01:09:53 +000060INITIALIZE_PASS_END(OptimalEdgeProfiler, "insert-optimal-edge-profiling",
Owen Andersond13db2c2010-07-21 22:09:45 +000061 "Insert optimal instrumentation for edge profiling",
Owen Andersonce665bd2010-10-07 22:25:06 +000062 false, false)
Andreas Neustifterf771dae2009-09-01 19:03:44 +000063
64ModulePass *llvm::createOptimalEdgeProfilerPass() {
65 return new OptimalEdgeProfiler();
66}
67
68inline static void printEdgeCounter(ProfileInfo::Edge e,
69 BasicBlock* b,
70 unsigned i) {
David Greene0b9afb42010-01-05 01:27:01 +000071 DEBUG(dbgs() << "--Edge Counter for " << (e) << " in " \
Andreas Neustifterf771dae2009-09-01 19:03:44 +000072 << ((b)?(b)->getNameStr():"0") << " (# " << (i) << ")\n");
73}
74
75bool OptimalEdgeProfiler::runOnModule(Module &M) {
76 Function *Main = M.getFunction("main");
77 if (Main == 0) {
78 errs() << "WARNING: cannot insert edge profiling into a module"
79 << " with no main function!\n";
80 return false; // No main, no instrumentation!
81 }
82
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000083 // NumEdges counts all the edges that may be instrumented. Later on its
84 // decided which edges to actually instrument, to achieve optimal profiling.
85 // For the entry block a virtual edge (0,entry) is reserved, for each block
86 // with no successors an edge (BB,0) is reserved. These edges are necessary
87 // to calculate a truly optimal maximum spanning tree and thus an optimal
88 // instrumentation.
Andreas Neustifterf771dae2009-09-01 19:03:44 +000089 unsigned NumEdges = 0;
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000090
Andreas Neustifterf771dae2009-09-01 19:03:44 +000091 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
92 if (F->isDeclaration()) continue;
93 // Reserve space for (0,entry) edge.
94 ++NumEdges;
95 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
96 // Keep track of which blocks need to be instrumented. We don't want to
97 // instrument blocks that are added as the result of breaking critical
98 // edges!
Andreas Neustifterf771dae2009-09-01 19:03:44 +000099 if (BB->getTerminator()->getNumSuccessors() == 0) {
100 // Reserve space for (BB,0) edge.
101 ++NumEdges;
102 } else {
103 NumEdges += BB->getTerminator()->getNumSuccessors();
104 }
105 }
106 }
107
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000108 // In the profiling output a counter for each edge is reserved, but only few
109 // are used. This is done to be able to read back in the profile without
110 // calulating the maximum spanning tree again, instead each edge counter that
111 // is not used is initialised with -1 to signal that this edge counter has to
112 // be calculated from other edge counters on reading the profile info back
113 // in.
114
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000115 const Type *Int32 = Type::getInt32Ty(M.getContext());
116 const ArrayType *ATy = ArrayType::get(Int32, NumEdges);
117 GlobalVariable *Counters =
118 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
119 Constant::getNullValue(ATy), "OptEdgeProfCounters");
120 NumEdgesInserted = 0;
121
122 std::vector<Constant*> Initializer(NumEdges);
Nick Lewycky06d2b422011-04-05 20:39:27 +0000123 Constant *Zero = ConstantInt::get(Int32, 0);
124 Constant *Uncounted = ConstantInt::get(Int32, ProfileInfoLoader::Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000125
126 // Instrument all of the edges not in MST...
127 unsigned i = 0;
128 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
129 if (F->isDeclaration()) continue;
Nick Lewycky06d2b422011-04-05 20:39:27 +0000130 DEBUG(dbgs() << "Working on " << F->getNameStr() << "\n");
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000131
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000132 // Calculate a Maximum Spanning Tree with the edge weights determined by
133 // ProfileEstimator. ProfileEstimator also assign weights to the virtual
134 // edges (0,entry) and (BB,0) (for blocks with no successors) and this
Andrew Trick04317cc2011-01-29 01:09:53 +0000135 // edges also participate in the maximum spanning tree calculation.
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000136 // The third parameter of MaximumSpanningTree() has the effect that not the
137 // actual MST is returned but the edges _not_ in the MST.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000138
Andrew Trick04317cc2011-01-29 01:09:53 +0000139 ProfileInfo::EdgeWeights ECs =
Chris Lattner05273442010-01-20 23:30:28 +0000140 getAnalysis<ProfileInfo>(*F).getEdgeWeights(F);
Andreas Neustifter39859432009-09-03 08:52:52 +0000141 std::vector<ProfileInfo::EdgeWeight> EdgeVector(ECs.begin(), ECs.end());
Nick Lewycky06d2b422011-04-05 20:39:27 +0000142 MaximumSpanningTree<BasicBlock> MST(EdgeVector);
143 std::stable_sort(MST.begin(), MST.end());
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000144
145 // Check if (0,entry) not in the MST. If not, instrument edge
146 // (IncrementCounterInBlock()) and set the counter initially to zero, if
147 // the edge is in the MST the counter is initialised to -1.
148
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000149 BasicBlock *entry = &(F->getEntryBlock());
Nick Lewycky06d2b422011-04-05 20:39:27 +0000150 ProfileInfo::Edge edge = ProfileInfo::getEdge(0, entry);
Andreas Neustifter39859432009-09-03 08:52:52 +0000151 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Nick Lewycky06d2b422011-04-05 20:39:27 +0000152 printEdgeCounter(edge, entry, i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000153 IncrementCounterInBlock(entry, i, Counters); ++NumEdgesInserted;
Andreas Neustifter92332722009-09-16 11:35:50 +0000154 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000155 } else{
Andreas Neustifter92332722009-09-16 11:35:50 +0000156 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000157 }
158
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000159 // InsertedBlocks contains all blocks that were inserted for splitting an
160 // edge, this blocks do not have to be instrumented.
Andreas Neustifter39859432009-09-03 08:52:52 +0000161 DenseSet<BasicBlock*> InsertedBlocks;
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000162 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000163 // Check if block was not inserted and thus does not have to be
164 // instrumented.
165 if (InsertedBlocks.count(BB)) continue;
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000166
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000167 // Okay, we have to add a counter of each outgoing edge not in MST. If
168 // the outgoing edge is not critical don't split it, just insert the
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000169 // counter in the source or destination of the edge. Also, if the block
170 // has no successors, the virtual edge (BB,0) is processed.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000171 TerminatorInst *TI = BB->getTerminator();
172 if (TI->getNumSuccessors() == 0) {
Nick Lewycky06d2b422011-04-05 20:39:27 +0000173 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB, 0);
Andreas Neustifter39859432009-09-03 08:52:52 +0000174 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Nick Lewycky06d2b422011-04-05 20:39:27 +0000175 printEdgeCounter(edge, BB, i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000176 IncrementCounterInBlock(BB, i, Counters); ++NumEdgesInserted;
Andreas Neustifter92332722009-09-16 11:35:50 +0000177 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000178 } else{
Andreas Neustifter92332722009-09-16 11:35:50 +0000179 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000180 }
181 }
182 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
183 BasicBlock *Succ = TI->getSuccessor(s);
184 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ);
Andreas Neustifter39859432009-09-03 08:52:52 +0000185 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000186
187 // If the edge is critical, split it.
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000188 bool wasInserted = SplitCriticalEdge(TI, s, this);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000189 Succ = TI->getSuccessor(s);
Andreas Neustifter39859432009-09-03 08:52:52 +0000190 if (wasInserted)
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000191 InsertedBlocks.insert(Succ);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000192
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000193 // Okay, we are guaranteed that the edge is no longer critical. If
194 // we only have a single successor, insert the counter in this block,
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000195 // otherwise insert it in the successor block.
196 if (TI->getNumSuccessors() == 1) {
197 // Insert counter at the start of the block
Nick Lewycky06d2b422011-04-05 20:39:27 +0000198 printEdgeCounter(edge, BB, i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000199 IncrementCounterInBlock(BB, i, Counters); ++NumEdgesInserted;
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000200 } else {
201 // Insert counter at the start of the block
Nick Lewycky06d2b422011-04-05 20:39:27 +0000202 printEdgeCounter(edge, Succ, i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000203 IncrementCounterInBlock(Succ, i, Counters); ++NumEdgesInserted;
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000204 }
Andreas Neustifter92332722009-09-16 11:35:50 +0000205 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000206 } else {
Andreas Neustifter92332722009-09-16 11:35:50 +0000207 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000208 }
209 }
210 }
211 }
212
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000213 // Check if the number of edges counted at first was the number of edges we
214 // considered for instrumentation.
Nick Lewycky06d2b422011-04-05 20:39:27 +0000215 assert(i == NumEdges && "the number of edges in counting array is wrong");
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000216
Nick Lewycky06d2b422011-04-05 20:39:27 +0000217 // Assign the now completely defined initialiser to the array.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000218 Constant *init = ConstantArray::get(ATy, Initializer);
219 Counters->setInitializer(init);
220
221 // Add the initialization call to main.
222 InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters);
223 return true;
224}
225