blob: ea3a291956e2b67e89f05492aa7726e45315807c [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 Neustifterf771dae2009-09-01 19:03:44 +000020#include "llvm/Support/Compiler.h"
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000021#include "llvm/Support/raw_ostream.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Transforms/Utils/BasicBlockUtils.h"
24#include "llvm/Transforms/Instrumentation.h"
25#include "llvm/ADT/Statistic.h"
26#include "MaximumSpanningTree.h"
27#include <set>
28using namespace llvm;
29
30STATISTIC(NumEdgesInserted, "The # of edges inserted.");
31
32namespace {
33 class VISIBILITY_HIDDEN OptimalEdgeProfiler : public ModulePass {
34 bool runOnModule(Module &M);
35 ProfileInfo *PI;
36 public:
37 static char ID; // Pass identification, replacement for typeid
38 OptimalEdgeProfiler() : ModulePass(&ID) {}
39
40 void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.addRequiredID(ProfileEstimatorPassID);
42 AU.addRequired<ProfileInfo>();
43 }
44
45 virtual const char *getPassName() const {
46 return "Optimal Edge Profiler";
47 }
48 };
49}
50
51char OptimalEdgeProfiler::ID = 0;
52static RegisterPass<OptimalEdgeProfiler>
53X("insert-optimal-edge-profiling",
54 "Insert optimal instrumentation for edge profiling");
55
56ModulePass *llvm::createOptimalEdgeProfilerPass() {
57 return new OptimalEdgeProfiler();
58}
59
60inline static void printEdgeCounter(ProfileInfo::Edge e,
61 BasicBlock* b,
62 unsigned i) {
63 DEBUG(errs() << "--Edge Counter for " << (e) << " in " \
64 << ((b)?(b)->getNameStr():"0") << " (# " << (i) << ")\n");
65}
66
67bool OptimalEdgeProfiler::runOnModule(Module &M) {
68 Function *Main = M.getFunction("main");
69 if (Main == 0) {
70 errs() << "WARNING: cannot insert edge profiling into a module"
71 << " with no main function!\n";
72 return false; // No main, no instrumentation!
73 }
74
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000075 // BlocksToInstrument stores all blocks that are in the function prior to
76 // instrumenting, since the spliting of critical edges adds new blocks (which
77 // have not to be instrumented), we have to remember them for later.
Andreas Neustifterf771dae2009-09-01 19:03:44 +000078 std::set<BasicBlock*> BlocksToInstrument;
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000079
80 // NumEdges counts all the edges that may be instrumented. Later on its
81 // decided which edges to actually instrument, to achieve optimal profiling.
82 // For the entry block a virtual edge (0,entry) is reserved, for each block
83 // with no successors an edge (BB,0) is reserved. These edges are necessary
84 // to calculate a truly optimal maximum spanning tree and thus an optimal
85 // instrumentation.
Andreas Neustifterf771dae2009-09-01 19:03:44 +000086 unsigned NumEdges = 0;
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000087
Andreas Neustifterf771dae2009-09-01 19:03:44 +000088 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
89 if (F->isDeclaration()) continue;
90 // Reserve space for (0,entry) edge.
91 ++NumEdges;
92 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
93 // Keep track of which blocks need to be instrumented. We don't want to
94 // instrument blocks that are added as the result of breaking critical
95 // edges!
96 BlocksToInstrument.insert(BB);
97 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);
121 Constant* zeroc = ConstantInt::get(Int32, 0);
122 Constant* minusonec = ConstantInt::get(Int32, ProfileInfo::MissingValue);
123
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;
128 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
129
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 Neustifter9341cdc2009-09-02 12:38:39 +0000137 PI = &getAnalysisID<ProfileInfo>(ProfileEstimatorPassID, *F);
138 MaximumSpanningTree MST = MaximumSpanningTree(&(*F), PI, true);
139
140 // Check if (0,entry) not in the MST. If not, instrument edge
141 // (IncrementCounterInBlock()) and set the counter initially to zero, if
142 // the edge is in the MST the counter is initialised to -1.
143
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000144 BasicBlock *entry = &(F->getEntryBlock());
145 ProfileInfo::Edge edge = ProfileInfo::getEdge(0,entry);
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000146 if (std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000147 printEdgeCounter(edge,entry,i);
148 IncrementCounterInBlock(entry, i, Counters); NumEdgesInserted++;
149 Initializer[i++] = (zeroc);
150 } else{
151 Initializer[i++] = (minusonec);
152 }
153
154 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000155 // Do not count blocks that where introduced by spliting critical edges.
156 if (!BlocksToInstrument.count(BB)) continue;
157
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000158 // Okay, we have to add a counter of each outgoing edge not in MST. If
159 // the outgoing edge is not critical don't split it, just insert the
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000160 // counter in the source or destination of the edge. Also, if the block
161 // has no successors, the virtual edge (BB,0) is processed.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000162 TerminatorInst *TI = BB->getTerminator();
163 if (TI->getNumSuccessors() == 0) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000164 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,0);
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000165 if (std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000166 printEdgeCounter(edge,BB,i);
167 IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
168 Initializer[i++] = (zeroc);
169 } else{
170 Initializer[i++] = (minusonec);
171 }
172 }
173 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
174 BasicBlock *Succ = TI->getSuccessor(s);
175 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ);
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000176 if (std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000177
178 // If the edge is critical, split it.
179 SplitCriticalEdge(TI,s,this);
180 Succ = TI->getSuccessor(s);
181
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000182 // Okay, we are guaranteed that the edge is no longer critical. If
183 // we only have a single successor, insert the counter in this block,
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000184 // otherwise insert it in the successor block.
185 if (TI->getNumSuccessors() == 1) {
186 // Insert counter at the start of the block
187 printEdgeCounter(edge,BB,i);
188 IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
189 } else {
190 // Insert counter at the start of the block
191 printEdgeCounter(edge,Succ,i);
192 IncrementCounterInBlock(Succ, i, Counters); NumEdgesInserted++;
193 }
194 Initializer[i++] = (zeroc);
195 } else {
196 Initializer[i++] = (minusonec);
197 }
198 }
199 }
200 }
201
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000202 // Check if the number of edges counted at first was the number of edges we
203 // considered for instrumentation.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000204 assert(i==NumEdges && "the number of edges in counting array is wrong");
205
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000206 // Assing the now completely defined initialiser to the array.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000207 Constant *init = ConstantArray::get(ATy, Initializer);
208 Counters->setInitializer(init);
209
210 // Add the initialization call to main.
211 InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters);
212 return true;
213}
214