blob: 466c34c2a692685d61d2be2df71098834e1bd04c [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 Neustifterf771dae2009-09-01 19:03:44 +000022#include "llvm/Support/Compiler.h"
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000023#include "llvm/Support/raw_ostream.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Transforms/Utils/BasicBlockUtils.h"
26#include "llvm/Transforms/Instrumentation.h"
Andreas Neustifter39859432009-09-03 08:52:52 +000027#include "llvm/ADT/DenseSet.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000028#include "llvm/ADT/Statistic.h"
29#include "MaximumSpanningTree.h"
30#include <set>
31using namespace llvm;
32
33STATISTIC(NumEdgesInserted, "The # of edges inserted.");
34
35namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000036 class OptimalEdgeProfiler : public ModulePass {
Andreas Neustifterf771dae2009-09-01 19:03:44 +000037 bool runOnModule(Module &M);
Andreas Neustifterf771dae2009-09-01 19:03:44 +000038 public:
39 static char ID; // Pass identification, replacement for typeid
40 OptimalEdgeProfiler() : ModulePass(&ID) {}
41
42 void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequiredID(ProfileEstimatorPassID);
44 AU.addRequired<ProfileInfo>();
45 }
46
47 virtual const char *getPassName() const {
48 return "Optimal Edge Profiler";
49 }
50 };
51}
52
53char OptimalEdgeProfiler::ID = 0;
54static RegisterPass<OptimalEdgeProfiler>
55X("insert-optimal-edge-profiling",
56 "Insert optimal instrumentation for edge profiling");
57
58ModulePass *llvm::createOptimalEdgeProfilerPass() {
59 return new OptimalEdgeProfiler();
60}
61
62inline static void printEdgeCounter(ProfileInfo::Edge e,
63 BasicBlock* b,
64 unsigned i) {
65 DEBUG(errs() << "--Edge Counter for " << (e) << " in " \
66 << ((b)?(b)->getNameStr():"0") << " (# " << (i) << ")\n");
67}
68
69bool OptimalEdgeProfiler::runOnModule(Module &M) {
70 Function *Main = M.getFunction("main");
71 if (Main == 0) {
72 errs() << "WARNING: cannot insert edge profiling into a module"
73 << " with no main function!\n";
74 return false; // No main, no instrumentation!
75 }
76
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000077 // NumEdges counts all the edges that may be instrumented. Later on its
78 // decided which edges to actually instrument, to achieve optimal profiling.
79 // For the entry block a virtual edge (0,entry) is reserved, for each block
80 // with no successors an edge (BB,0) is reserved. These edges are necessary
81 // to calculate a truly optimal maximum spanning tree and thus an optimal
82 // instrumentation.
Andreas Neustifterf771dae2009-09-01 19:03:44 +000083 unsigned NumEdges = 0;
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000084
Andreas Neustifterf771dae2009-09-01 19:03:44 +000085 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
86 if (F->isDeclaration()) continue;
87 // Reserve space for (0,entry) edge.
88 ++NumEdges;
89 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
90 // Keep track of which blocks need to be instrumented. We don't want to
91 // instrument blocks that are added as the result of breaking critical
92 // edges!
Andreas Neustifterf771dae2009-09-01 19:03:44 +000093 if (BB->getTerminator()->getNumSuccessors() == 0) {
94 // Reserve space for (BB,0) edge.
95 ++NumEdges;
96 } else {
97 NumEdges += BB->getTerminator()->getNumSuccessors();
98 }
99 }
100 }
101
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000102 // In the profiling output a counter for each edge is reserved, but only few
103 // are used. This is done to be able to read back in the profile without
104 // calulating the maximum spanning tree again, instead each edge counter that
105 // is not used is initialised with -1 to signal that this edge counter has to
106 // be calculated from other edge counters on reading the profile info back
107 // in.
108
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000109 const Type *Int32 = Type::getInt32Ty(M.getContext());
110 const ArrayType *ATy = ArrayType::get(Int32, NumEdges);
111 GlobalVariable *Counters =
112 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
113 Constant::getNullValue(ATy), "OptEdgeProfCounters");
114 NumEdgesInserted = 0;
115
116 std::vector<Constant*> Initializer(NumEdges);
Andreas Neustifter92332722009-09-16 11:35:50 +0000117 Constant* Zero = ConstantInt::get(Int32, 0);
118 Constant* Uncounted = ConstantInt::get(Int32, ProfileInfoLoader::Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000119
120 // Instrument all of the edges not in MST...
121 unsigned i = 0;
122 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
123 if (F->isDeclaration()) continue;
124 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
125
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000126 // Calculate a Maximum Spanning Tree with the edge weights determined by
127 // ProfileEstimator. ProfileEstimator also assign weights to the virtual
128 // edges (0,entry) and (BB,0) (for blocks with no successors) and this
129 // edges also participate in the maximum spanning tree calculation.
130 // The third parameter of MaximumSpanningTree() has the effect that not the
131 // actual MST is returned but the edges _not_ in the MST.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000132
Andreas Neustifter39859432009-09-03 08:52:52 +0000133 ProfileInfo::EdgeWeights ECs =
134 getAnalysisID<ProfileInfo>(ProfileEstimatorPassID, *F).getEdgeWeights(F);
135 std::vector<ProfileInfo::EdgeWeight> EdgeVector(ECs.begin(), ECs.end());
Andreas Neustiftered1ac4a2009-09-04 12:34:44 +0000136 MaximumSpanningTree<BasicBlock> MST (EdgeVector);
137 std::stable_sort(MST.begin(),MST.end());
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000138
139 // Check if (0,entry) not in the MST. If not, instrument edge
140 // (IncrementCounterInBlock()) and set the counter initially to zero, if
141 // the edge is in the MST the counter is initialised to -1.
142
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000143 BasicBlock *entry = &(F->getEntryBlock());
144 ProfileInfo::Edge edge = ProfileInfo::getEdge(0,entry);
Andreas Neustifter39859432009-09-03 08:52:52 +0000145 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000146 printEdgeCounter(edge,entry,i);
147 IncrementCounterInBlock(entry, i, Counters); NumEdgesInserted++;
Andreas Neustifter92332722009-09-16 11:35:50 +0000148 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000149 } else{
Andreas Neustifter92332722009-09-16 11:35:50 +0000150 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000151 }
152
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000153 // InsertedBlocks contains all blocks that were inserted for splitting an
154 // edge, this blocks do not have to be instrumented.
Andreas Neustifter39859432009-09-03 08:52:52 +0000155 DenseSet<BasicBlock*> InsertedBlocks;
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000156 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000157 // Check if block was not inserted and thus does not have to be
158 // instrumented.
159 if (InsertedBlocks.count(BB)) continue;
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000160
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000161 // Okay, we have to add a counter of each outgoing edge not in MST. If
162 // the outgoing edge is not critical don't split it, just insert the
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000163 // counter in the source or destination of the edge. Also, if the block
164 // has no successors, the virtual edge (BB,0) is processed.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000165 TerminatorInst *TI = BB->getTerminator();
166 if (TI->getNumSuccessors() == 0) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000167 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,0);
Andreas Neustifter39859432009-09-03 08:52:52 +0000168 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000169 printEdgeCounter(edge,BB,i);
170 IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
Andreas Neustifter92332722009-09-16 11:35:50 +0000171 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000172 } else{
Andreas Neustifter92332722009-09-16 11:35:50 +0000173 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000174 }
175 }
176 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
177 BasicBlock *Succ = TI->getSuccessor(s);
178 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ);
Andreas Neustifter39859432009-09-03 08:52:52 +0000179 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000180
181 // If the edge is critical, split it.
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000182 bool wasInserted = SplitCriticalEdge(TI, s, this);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000183 Succ = TI->getSuccessor(s);
Andreas Neustifter39859432009-09-03 08:52:52 +0000184 if (wasInserted)
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000185 InsertedBlocks.insert(Succ);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000186
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000187 // Okay, we are guaranteed that the edge is no longer critical. If
188 // we only have a single successor, insert the counter in this block,
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000189 // otherwise insert it in the successor block.
190 if (TI->getNumSuccessors() == 1) {
191 // Insert counter at the start of the block
192 printEdgeCounter(edge,BB,i);
193 IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
194 } else {
195 // Insert counter at the start of the block
196 printEdgeCounter(edge,Succ,i);
197 IncrementCounterInBlock(Succ, i, Counters); NumEdgesInserted++;
198 }
Andreas Neustifter92332722009-09-16 11:35:50 +0000199 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000200 } else {
Andreas Neustifter92332722009-09-16 11:35:50 +0000201 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000202 }
203 }
204 }
205 }
206
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000207 // Check if the number of edges counted at first was the number of edges we
208 // considered for instrumentation.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000209 assert(i==NumEdges && "the number of edges in counting array is wrong");
210
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000211 // Assing the now completely defined initialiser to the array.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000212 Constant *init = ConstantArray::get(ATy, Initializer);
213 Counters->setInitializer(init);
214
215 // Add the initialization call to main.
216 InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters);
217 return true;
218}
219