blob: c5a1fe9188685a65c48ec3889aa64adb399b3ef0 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/Transforms/Instrumentation.h"
17#include "MaximumSpanningTree.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000018#include "ProfilingUtils.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/DenseSet.h"
20#include "llvm/ADT/Statistic.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000021#include "llvm/Analysis/Passes.h"
Andreas Neustiftered1ac4a2009-09-04 12:34:44 +000022#include "llvm/Analysis/ProfileInfo.h"
Andreas Neustifter92332722009-09-16 11:35:50 +000023#include "llvm/Analysis/ProfileInfoLoader.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Pass.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000028#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Support/raw_ostream.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000030#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Andreas Neustifterf771dae2009-09-01 19:03:44 +000031using 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
Owen Anderson081c34b2010-10-19 17:21:58 +000040 OptimalEdgeProfiler() : ModulePass(ID) {
41 initializeOptimalEdgeProfilerPass(*PassRegistry::getPassRegistry());
42 }
Andreas Neustifterf771dae2009-09-01 19:03:44 +000043
44 void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.addRequiredID(ProfileEstimatorPassID);
46 AU.addRequired<ProfileInfo>();
47 }
48
49 virtual const char *getPassName() const {
50 return "Optimal Edge Profiler";
51 }
52 };
53}
54
55char OptimalEdgeProfiler::ID = 0;
Andrew Trick04317cc2011-01-29 01:09:53 +000056INITIALIZE_PASS_BEGIN(OptimalEdgeProfiler, "insert-optimal-edge-profiling",
Owen Anderson2ab36d32010-10-12 19:48:12 +000057 "Insert optimal instrumentation for edge profiling",
58 false, false)
59INITIALIZE_PASS_DEPENDENCY(ProfileEstimatorPass)
60INITIALIZE_AG_DEPENDENCY(ProfileInfo)
Andrew Trick04317cc2011-01-29 01:09:53 +000061INITIALIZE_PASS_END(OptimalEdgeProfiler, "insert-optimal-edge-profiling",
Owen Andersond13db2c2010-07-21 22:09:45 +000062 "Insert optimal instrumentation for edge profiling",
Owen Andersonce665bd2010-10-07 22:25:06 +000063 false, false)
Andreas Neustifterf771dae2009-09-01 19:03:44 +000064
65ModulePass *llvm::createOptimalEdgeProfilerPass() {
66 return new OptimalEdgeProfiler();
67}
68
69inline static void printEdgeCounter(ProfileInfo::Edge e,
70 BasicBlock* b,
71 unsigned i) {
David Greene0b9afb42010-01-05 01:27:01 +000072 DEBUG(dbgs() << "--Edge Counter for " << (e) << " in " \
Benjamin Kramera7b0cb72011-11-15 16:27:03 +000073 << ((b)?(b)->getName():"0") << " (# " << (i) << ")\n");
Andreas Neustifterf771dae2009-09-01 19:03:44 +000074}
75
76bool OptimalEdgeProfiler::runOnModule(Module &M) {
77 Function *Main = M.getFunction("main");
78 if (Main == 0) {
Bob Wilsona0be09f2012-12-24 18:15:21 +000079 M.getContext().emitWarning("cannot insert edge profiling into a module"
80 " with no main function");
Andreas Neustifterf771dae2009-09-01 19:03:44 +000081 return false; // No main, no instrumentation!
82 }
83
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000084 // NumEdges counts all the edges that may be instrumented. Later on its
85 // decided which edges to actually instrument, to achieve optimal profiling.
86 // For the entry block a virtual edge (0,entry) is reserved, for each block
87 // with no successors an edge (BB,0) is reserved. These edges are necessary
88 // to calculate a truly optimal maximum spanning tree and thus an optimal
89 // instrumentation.
Andreas Neustifterf771dae2009-09-01 19:03:44 +000090 unsigned NumEdges = 0;
Andreas Neustifter9341cdc2009-09-02 12:38:39 +000091
Andreas Neustifterf771dae2009-09-01 19:03:44 +000092 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
93 if (F->isDeclaration()) continue;
94 // Reserve space for (0,entry) edge.
95 ++NumEdges;
96 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
97 // Keep track of which blocks need to be instrumented. We don't want to
98 // instrument blocks that are added as the result of breaking critical
99 // edges!
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000100 if (BB->getTerminator()->getNumSuccessors() == 0) {
101 // Reserve space for (BB,0) edge.
102 ++NumEdges;
103 } else {
104 NumEdges += BB->getTerminator()->getNumSuccessors();
105 }
106 }
107 }
108
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000109 // In the profiling output a counter for each edge is reserved, but only few
110 // are used. This is done to be able to read back in the profile without
111 // calulating the maximum spanning tree again, instead each edge counter that
112 // is not used is initialised with -1 to signal that this edge counter has to
113 // be calculated from other edge counters on reading the profile info back
114 // in.
115
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000116 Type *Int32 = Type::getInt32Ty(M.getContext());
117 ArrayType *ATy = ArrayType::get(Int32, NumEdges);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000118 GlobalVariable *Counters =
119 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
120 Constant::getNullValue(ATy), "OptEdgeProfCounters");
121 NumEdgesInserted = 0;
122
123 std::vector<Constant*> Initializer(NumEdges);
Nick Lewycky06d2b422011-04-05 20:39:27 +0000124 Constant *Zero = ConstantInt::get(Int32, 0);
125 Constant *Uncounted = ConstantInt::get(Int32, ProfileInfoLoader::Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000126
127 // Instrument all of the edges not in MST...
128 unsigned i = 0;
129 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
130 if (F->isDeclaration()) continue;
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000131 DEBUG(dbgs() << "Working on " << F->getName() << "\n");
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000132
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000133 // Calculate a Maximum Spanning Tree with the edge weights determined by
134 // ProfileEstimator. ProfileEstimator also assign weights to the virtual
135 // edges (0,entry) and (BB,0) (for blocks with no successors) and this
Andrew Trick04317cc2011-01-29 01:09:53 +0000136 // edges also participate in the maximum spanning tree calculation.
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000137 // The third parameter of MaximumSpanningTree() has the effect that not the
138 // actual MST is returned but the edges _not_ in the MST.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000139
Andrew Trick04317cc2011-01-29 01:09:53 +0000140 ProfileInfo::EdgeWeights ECs =
Chris Lattner05273442010-01-20 23:30:28 +0000141 getAnalysis<ProfileInfo>(*F).getEdgeWeights(F);
Andreas Neustifter39859432009-09-03 08:52:52 +0000142 std::vector<ProfileInfo::EdgeWeight> EdgeVector(ECs.begin(), ECs.end());
Nick Lewycky06d2b422011-04-05 20:39:27 +0000143 MaximumSpanningTree<BasicBlock> MST(EdgeVector);
144 std::stable_sort(MST.begin(), MST.end());
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000145
146 // Check if (0,entry) not in the MST. If not, instrument edge
147 // (IncrementCounterInBlock()) and set the counter initially to zero, if
148 // the edge is in the MST the counter is initialised to -1.
149
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000150 BasicBlock *entry = &(F->getEntryBlock());
Nick Lewycky06d2b422011-04-05 20:39:27 +0000151 ProfileInfo::Edge edge = ProfileInfo::getEdge(0, entry);
Andreas Neustifter39859432009-09-03 08:52:52 +0000152 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Nick Lewycky06d2b422011-04-05 20:39:27 +0000153 printEdgeCounter(edge, entry, i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000154 IncrementCounterInBlock(entry, i, Counters); ++NumEdgesInserted;
Andreas Neustifter92332722009-09-16 11:35:50 +0000155 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000156 } else{
Andreas Neustifter92332722009-09-16 11:35:50 +0000157 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000158 }
159
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000160 // InsertedBlocks contains all blocks that were inserted for splitting an
161 // edge, this blocks do not have to be instrumented.
Andreas Neustifter39859432009-09-03 08:52:52 +0000162 DenseSet<BasicBlock*> InsertedBlocks;
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000163 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000164 // Check if block was not inserted and thus does not have to be
165 // instrumented.
166 if (InsertedBlocks.count(BB)) continue;
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000167
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000168 // Okay, we have to add a counter of each outgoing edge not in MST. If
169 // the outgoing edge is not critical don't split it, just insert the
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000170 // counter in the source or destination of the edge. Also, if the block
171 // has no successors, the virtual edge (BB,0) is processed.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000172 TerminatorInst *TI = BB->getTerminator();
173 if (TI->getNumSuccessors() == 0) {
Nick Lewycky06d2b422011-04-05 20:39:27 +0000174 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB, 0);
Andreas Neustifter39859432009-09-03 08:52:52 +0000175 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Nick Lewycky06d2b422011-04-05 20:39:27 +0000176 printEdgeCounter(edge, BB, i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000177 IncrementCounterInBlock(BB, i, Counters); ++NumEdgesInserted;
Andreas Neustifter92332722009-09-16 11:35:50 +0000178 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000179 } else{
Andreas Neustifter92332722009-09-16 11:35:50 +0000180 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000181 }
182 }
183 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
184 BasicBlock *Succ = TI->getSuccessor(s);
185 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ);
Andreas Neustifter39859432009-09-03 08:52:52 +0000186 if (!std::binary_search(MST.begin(), MST.end(), edge)) {
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000187
188 // If the edge is critical, split it.
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000189 bool wasInserted = SplitCriticalEdge(TI, s, this);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000190 Succ = TI->getSuccessor(s);
Andreas Neustifter39859432009-09-03 08:52:52 +0000191 if (wasInserted)
Andreas Neustifter8f123b82009-09-02 13:59:05 +0000192 InsertedBlocks.insert(Succ);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000193
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000194 // Okay, we are guaranteed that the edge is no longer critical. If
195 // we only have a single successor, insert the counter in this block,
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000196 // otherwise insert it in the successor block.
197 if (TI->getNumSuccessors() == 1) {
198 // Insert counter at the start of the block
Nick Lewycky06d2b422011-04-05 20:39:27 +0000199 printEdgeCounter(edge, BB, i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000200 IncrementCounterInBlock(BB, i, Counters); ++NumEdgesInserted;
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000201 } else {
202 // Insert counter at the start of the block
Nick Lewycky06d2b422011-04-05 20:39:27 +0000203 printEdgeCounter(edge, Succ, i);
Dan Gohmanfe601042010-06-22 15:08:57 +0000204 IncrementCounterInBlock(Succ, i, Counters); ++NumEdgesInserted;
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000205 }
Andreas Neustifter92332722009-09-16 11:35:50 +0000206 Initializer[i++] = (Zero);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000207 } else {
Andreas Neustifter92332722009-09-16 11:35:50 +0000208 Initializer[i++] = (Uncounted);
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000209 }
210 }
211 }
212 }
213
Andreas Neustifter9341cdc2009-09-02 12:38:39 +0000214 // Check if the number of edges counted at first was the number of edges we
215 // considered for instrumentation.
Nick Lewycky06d2b422011-04-05 20:39:27 +0000216 assert(i == NumEdges && "the number of edges in counting array is wrong");
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000217
Nick Lewycky06d2b422011-04-05 20:39:27 +0000218 // Assign the now completely defined initialiser to the array.
Andreas Neustifterf771dae2009-09-01 19:03:44 +0000219 Constant *init = ConstantArray::get(ATy, Initializer);
220 Counters->setInitializer(init);
221
222 // Add the initialization call to main.
223 InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters);
224 return true;
225}
226