llvm.org GIT mirror llvm / f771dae
OptimalEdgeProfiling: Creation of profiles. This adds the instrumentation and runtime part of OptimalEdgeProfiling. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80712 91177308-0d34-0410-b5e6-96231b3b80d8 Andreas Neustifter 10 years ago
5 changed file(s) with 235 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
2121 BlockInfo = 3, /* Block profiling information */
2222 EdgeInfo = 4, /* Edge profiling information */
2323 PathInfo = 5, /* Path profiling information */
24 BBTraceInfo = 6 /* Basic block trace information */
24 BBTraceInfo = 6, /* Basic block trace information */
25 OptEdgeInfo = 7 /* Edge profiling information, optimal version */
2526 };
2627
2728 #endif /* LLVM_ANALYSIS_PROFILEINFOTYPES_H */
11 BlockProfiling.cpp
22 EdgeProfiling.cpp
33 MaximumSpanningTree.cpp
4 OptimalEdgeProfiling.cpp
45 ProfilingUtils.cpp
56 RSProfiling.cpp
67 )
0 //===- OptimalEdgeProfiling.cpp - Insert counters for opt. edge profiling -===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass instruments the specified program with counters for edge profiling.
10 // Edge profiling can give a reasonable approximation of the hot paths through a
11 // program, and is used for a wide variety of program transformations.
12 //
13 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "insert-optimal-edge-profiling"
15 #include "ProfilingUtils.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Function.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Analysis/Passes.h"
22 #include "llvm/Analysis/ProfileInfo.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
26 #include "llvm/Transforms/Instrumentation.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "MaximumSpanningTree.h"
29 #include
30 using namespace llvm;
31
32 STATISTIC(NumEdgesInserted, "The # of edges inserted.");
33
34 namespace {
35 class VISIBILITY_HIDDEN OptimalEdgeProfiler : public ModulePass {
36 bool runOnModule(Module &M);
37 ProfileInfo *PI;
38 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();
45 }
46
47 virtual const char *getPassName() const {
48 return "Optimal Edge Profiler";
49 }
50 };
51 }
52
53 char OptimalEdgeProfiler::ID = 0;
54 static RegisterPass
55 X("insert-optimal-edge-profiling",
56 "Insert optimal instrumentation for edge profiling");
57
58 ModulePass *llvm::createOptimalEdgeProfilerPass() {
59 return new OptimalEdgeProfiler();
60 }
61
62 inline 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
69 bool 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
77 std::set BlocksToInstrument;
78 unsigned NumEdges = 0;
79 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
80 if (F->isDeclaration()) continue;
81 // Reserve space for (0,entry) edge.
82 ++NumEdges;
83 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
84 // Keep track of which blocks need to be instrumented. We don't want to
85 // instrument blocks that are added as the result of breaking critical
86 // edges!
87 BlocksToInstrument.insert(BB);
88 if (BB->getTerminator()->getNumSuccessors() == 0) {
89 // Reserve space for (BB,0) edge.
90 ++NumEdges;
91 } else {
92 NumEdges += BB->getTerminator()->getNumSuccessors();
93 }
94 }
95 }
96
97 const Type *Int32 = Type::getInt32Ty(M.getContext());
98 const ArrayType *ATy = ArrayType::get(Int32, NumEdges);
99 GlobalVariable *Counters =
100 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
101 Constant::getNullValue(ATy), "OptEdgeProfCounters");
102 NumEdgesInserted = 0;
103
104 std::vector Initializer(NumEdges);
105 Constant* zeroc = ConstantInt::get(Int32, 0);
106 Constant* minusonec = ConstantInt::get(Int32, ProfileInfo::MissingValue);
107
108 // Instrument all of the edges not in MST...
109 unsigned i = 0;
110 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
111 if (F->isDeclaration()) continue;
112 DEBUG(errs()<<"Working on "<getNameStr()<<"\n");
113
114 PI = &getAnalysisID(ProfileEstimatorPassID,*F);
115 MaximumSpanningTree MST = MaximumSpanningTree(&(*F),PI,true);
116
117 // Create counter for (0,entry) edge.
118 BasicBlock *entry = &(F->getEntryBlock());
119 ProfileInfo::Edge edge = ProfileInfo::getEdge(0,entry);
120 if (std::binary_search(MST.begin(),MST.end(),edge)) {
121 printEdgeCounter(edge,entry,i);
122 IncrementCounterInBlock(entry, i, Counters); NumEdgesInserted++;
123 Initializer[i++] = (zeroc);
124 } else{
125 Initializer[i++] = (minusonec);
126 }
127
128 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
129 if (!BlocksToInstrument.count(BB)) continue; // Don't count new blocks
130 // Okay, we have to add a counter of each outgoing edge not in MST. If
131 // the outgoing edge is not critical don't split it, just insert the
132 // counter in the source or destination of the edge.
133 TerminatorInst *TI = BB->getTerminator();
134 if (TI->getNumSuccessors() == 0) {
135 // Create counter for (BB,0), edge.
136 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,0);
137 if (std::binary_search(MST.begin(),MST.end(),edge)) {
138 printEdgeCounter(edge,BB,i);
139 IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
140 Initializer[i++] = (zeroc);
141 } else{
142 Initializer[i++] = (minusonec);
143 }
144 }
145 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
146 BasicBlock *Succ = TI->getSuccessor(s);
147 ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ);
148 if (std::binary_search(MST.begin(),MST.end(),edge)) {
149
150 // If the edge is critical, split it.
151 SplitCriticalEdge(TI,s,this);
152 Succ = TI->getSuccessor(s);
153
154 // Okay, we are guaranteed that the edge is no longer critical. If we
155 // only have a single successor, insert the counter in this block,
156 // otherwise insert it in the successor block.
157 if (TI->getNumSuccessors() == 1) {
158 // Insert counter at the start of the block
159 printEdgeCounter(edge,BB,i);
160 IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
161 } else {
162 // Insert counter at the start of the block
163 printEdgeCounter(edge,Succ,i);
164 IncrementCounterInBlock(Succ, i, Counters); NumEdgesInserted++;
165 }
166 Initializer[i++] = (zeroc);
167 } else {
168 Initializer[i++] = (minusonec);
169 }
170 }
171 }
172 }
173
174 // check if indeed all counters have been used
175 assert(i==NumEdges && "the number of edges in counting array is wrong");
176
177 // assign initialiser to array
178 Constant *init = ConstantArray::get(ATy, Initializer);
179 Counters->setInitializer(init);
180
181 // Add the initialization call to main.
182 InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters);
183 return true;
184 }
185
0 /*===-- OptimalEdgeProfiling.c - Support library for opt. edge profiling --===*\
1 |*
2 |* The LLVM Compiler Infrastructure
3 |*
4 |* This file is distributed under the University of Illinois Open Source
5 |* License. See LICENSE.TXT for details.
6 |*
7 |*===----------------------------------------------------------------------===*|
8 |*
9 |* This file implements the call back routines for the edge profiling
10 |* instrumentation pass. This should be used with the
11 |* -insert-opt-edge-profiling LLVM pass.
12 |*
13 \*===----------------------------------------------------------------------===*/
14
15 #include "Profiling.h"
16 #include
17
18 static unsigned *ArrayStart;
19 static unsigned NumElements;
20
21 /* OptEdgeProfAtExitHandler - When the program exits, just write out the
22 * profiling data.
23 */
24 static void OptEdgeProfAtExitHandler() {
25 /* Note that, although the array has a counter for each edge, not all
26 * counters are updated, the ones that are not used are initialised with -1.
27 * When loading this information the counters with value -1 have to be
28 * recalculated, it is guranteed that this is possible.
29 */
30 write_profiling_data(OptEdgeInfo, ArrayStart, NumElements);
31 }
32
33
34 /* llvm_start_opt_edge_profiling - This is the main entry point of the edge
35 * profiling library. It is responsible for setting up the atexit handler.
36 */
37 int llvm_start_opt_edge_profiling(int argc, const char **argv,
38 unsigned *arrayStart, unsigned numElements) {
39 int Ret = save_arguments(argc, argv);
40 ArrayStart = arrayStart;
41 NumElements = numElements;
42 atexit(OptEdgeProfAtExitHandler);
43 return Ret;
44 }
11 llvm_start_func_profiling
22 llvm_start_block_profiling
33 llvm_start_edge_profiling
4 llvm_start_opt_edge_profiling
45 llvm_start_basic_block_tracing
56 llvm_trace_basic_block