ec05872179eff2eb2ca6abca16318b2bec5c66b9
[oota-llvm.git] / lib / Transforms / Instrumentation / GCOVProfiling.cpp
1 //===- GCOVProfiling.cpp - Insert edge counters for gcov 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 implements GCOV-style profiling. When this pass is run it emits
11 // "gcno" files next to the existing source, and instruments the code that runs
12 // to records the edges between blocks that run and emit a complementary "gcda"
13 // file on exit.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #define DEBUG_TYPE "insert-gcov-profiling"
18
19 #include "ProfilingUtils.h"
20 #include "llvm/Transforms/Instrumentation.h"
21 #include "llvm/Analysis/DebugInfo.h"
22 #include "llvm/Module.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/DebugLoc.h"
28 #include "llvm/Support/InstIterator.h"
29 #include "llvm/Support/IRBuilder.h"
30 #include "llvm/Support/PathV2.h"
31 #include "llvm/ADT/DenseMap.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/STLExtras.h"
34 #include "llvm/ADT/StringExtras.h"
35 #include "llvm/ADT/StringMap.h"
36 #include "llvm/ADT/UniqueVector.h"
37 #include <string>
38 #include <utility>
39 using namespace llvm;
40
41 namespace {
42   class GCOVProfiler : public ModulePass {
43     bool runOnModule(Module &M);
44   public:
45     static char ID;
46     GCOVProfiler() : ModulePass(ID) {
47       initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
48     }
49     virtual const char *getPassName() const {
50       return "GCOV Profiler";
51     }
52
53   private:
54     // Create the GCNO files for the Module based on DebugInfo.
55     void EmitGCNO(DebugInfoFinder &DIF);
56
57     // Get pointers to the functions in the runtime library.
58     Constant *getStartFileFunc();
59     Constant *getEmitFunctionFunc();
60     Constant *getEmitArcsFunc();
61     Constant *getEndFileFunc();
62
63     // Add the function to write out all our counters to the global destructor
64     // list.
65     void InsertCounterWriteout(DebugInfoFinder &,
66                                SmallVector<std::pair<GlobalVariable *,
67                                                      uint32_t>, 8> &);
68
69     Module *Mod;
70     LLVMContext *Ctx;
71   };
72 }
73
74 char GCOVProfiler::ID = 0;
75 INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling",
76                 "Insert instrumentation for GCOV profiling", false, false)
77
78 ModulePass *llvm::createGCOVProfilerPass() { return new GCOVProfiler(); }
79
80 static DISubprogram FindSubprogram(DIScope scope) {
81   while (!scope.isSubprogram()) {
82     assert(scope.isLexicalBlock() &&
83            "Debug location not lexical block or subprogram");
84     scope = DILexicalBlock(scope).getContext();
85   }
86   return DISubprogram(scope);
87 }
88
89 namespace {
90   class GCOVRecord {
91    protected:
92     static const char *lines_tag;
93     static const char *function_tag;
94     static const char *block_tag;
95     static const char *edge_tag;
96
97     GCOVRecord() {}
98
99     void WriteBytes(const char *b, int size) {
100       os->write(b, size);
101     }
102
103     void Write(uint32_t i) {
104       WriteBytes(reinterpret_cast<char*>(&i), 4);
105     }
106
107     // Returns the length measured in 4-byte blocks that will be used to
108     // represent this string in a GCOV file
109     unsigned LengthOfGCOVString(StringRef s) {
110       // A GCOV string is a length, followed by a NUL, then between 0 and 3 NULs
111       // padding out to the next 4-byte word. The length is measured in 4-byte words
112       // including padding, not bytes of actual string.
113       return (s.size() + 5) / 4;
114     }
115
116     void WriteGCOVString(StringRef s) {
117       uint32_t len = LengthOfGCOVString(s);
118       Write(len);
119       WriteBytes(s.data(), s.size());
120
121       // Write 1 to 4 bytes of NUL padding.
122       assert((unsigned)(5 - ((s.size() + 1) % 4)) > 0);
123       assert((unsigned)(5 - ((s.size() + 1) % 4)) <= 4);
124       WriteBytes("\0\0\0\0", 5 - ((s.size() + 1) % 4));
125     }
126
127     raw_ostream *os;
128   };
129   const char *GCOVRecord::lines_tag = "\0\0\x45\x01";
130   const char *GCOVRecord::function_tag = "\0\0\0\1";
131   const char *GCOVRecord::block_tag = "\0\0\x41\x01";
132   const char *GCOVRecord::edge_tag = "\0\0\x43\x01";
133
134   class GCOVFunction;
135   class GCOVBlock;
136
137   // Constructed only by requesting it from a GCOVBlock, this object stores a
138   // list of line numbers and a single filename, representing lines that belong
139   // to the block.
140   class GCOVLines : public GCOVRecord {
141    public:
142     void AddLine(uint32_t line) {
143       lines.push_back(line);
144     }
145
146     uint32_t Length() {
147       return LengthOfGCOVString(filename) + 2 + lines.size();
148     }
149
150    private:
151     friend class GCOVBlock;
152
153     GCOVLines(std::string filename, raw_ostream *os)
154         : filename(filename) {
155       this->os = os;
156     }
157
158     std::string filename;
159     SmallVector<uint32_t, 32> lines;
160   };
161
162   // Represent a basic block in GCOV. Each block has a unique number in the
163   // function, number of lines belonging to each block, and a set of edges to
164   // other blocks.
165   class GCOVBlock : public GCOVRecord {
166    public:
167     GCOVLines &GetFile(std::string filename) {
168       GCOVLines *&lines = lines_by_file[filename];
169       if (!lines) {
170         lines = new GCOVLines(filename, os);
171       }
172       return *lines;
173     }
174
175     void AddEdge(GCOVBlock &successor) {
176       out_edges.push_back(&successor);
177     }
178
179     void WriteOut() {
180       uint32_t len = 3;
181       for (StringMap<GCOVLines *>::iterator I = lines_by_file.begin(),
182                E = lines_by_file.end(); I != E; ++I) {
183         len += I->second->Length();
184       }
185
186       WriteBytes(lines_tag, 4);
187       Write(len);
188       Write(number);
189       for (StringMap<GCOVLines *>::iterator I = lines_by_file.begin(),
190                E = lines_by_file.end(); I != E; ++I) {
191         Write(0);
192         WriteGCOVString(I->second->filename);
193         for (int i = 0, e = I->second->lines.size(); i != e; ++i) {
194           Write(I->second->lines[i]);
195         }
196       }
197       Write(0);
198       Write(0);
199     }
200
201     ~GCOVBlock() {
202       DeleteContainerSeconds(lines_by_file);
203     }
204
205    private:
206     friend class GCOVFunction;
207
208     GCOVBlock(uint32_t number, raw_ostream *os)
209         : number(number) {
210       this->os = os;
211     }
212
213     uint32_t number;
214     BasicBlock *block;
215     StringMap<GCOVLines *> lines_by_file;
216     SmallVector<GCOVBlock *, 4> out_edges;
217   };
218
219   // A function has a unique identifier, a checksum (we leave as zero) and a
220   // set of blocks and a map of edges between blocks. This is the only GCOV
221   // object users can construct, the blocks and lines will be rooted here.
222   class GCOVFunction : public GCOVRecord {
223    public:
224     GCOVFunction(DISubprogram SP, raw_ostream *os) {
225       this->os = os;
226
227       Function *F = SP.getFunction();
228       uint32_t i = 0;
229       for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
230         blocks[BB] = new GCOVBlock(i++, os);
231       }
232
233       WriteBytes(function_tag, 4);
234       uint32_t block_len = 1 + 1 + 1 + LengthOfGCOVString(SP.getName()) +
235           1 + LengthOfGCOVString(SP.getFilename()) + 1;
236       Write(block_len);
237       uint32_t ident = reinterpret_cast<intptr_t>((MDNode*)SP);
238       Write(ident);
239       Write(0); // checksum
240       WriteGCOVString(SP.getName());
241       WriteGCOVString(SP.getFilename());
242       Write(SP.getLineNumber());
243     }
244
245     ~GCOVFunction() {
246       DeleteContainerSeconds(blocks);
247     }
248
249     GCOVBlock &GetBlock(BasicBlock *BB) {
250       return *blocks[BB];
251     }
252
253     void WriteOut() {
254       // Emit count of blocks.
255       WriteBytes(block_tag, 4);
256       Write(blocks.size());
257       for (int i = 0, e = blocks.size(); i != e; ++i) {
258         Write(0);  // No flags on our blocks.
259       }
260
261       // Emit edges between blocks.
262       for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = blocks.begin(),
263                E = blocks.end(); I != E; ++I) {
264         GCOVBlock &block = *I->second;
265         if (block.out_edges.empty()) continue;
266
267         WriteBytes(edge_tag, 4);
268         Write(block.out_edges.size() * 2 + 1);
269         Write(block.number);
270         for (int i = 0, e = block.out_edges.size(); i != e; ++i) {
271           Write(block.out_edges[i]->number);
272           Write(0);  // no flags
273         }
274       }
275
276       // Emit lines for each block.
277       for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = blocks.begin(),
278                E = blocks.end(); I != E; ++I) {
279         I->second->WriteOut();
280       }
281     }
282
283    private:
284     DenseMap<BasicBlock *, GCOVBlock *> blocks;
285   };
286 }
287
288 void GCOVProfiler::EmitGCNO(DebugInfoFinder &DIF) {
289   DenseMap<const MDNode *, raw_fd_ostream *> gcno_files;
290   for (DebugInfoFinder::iterator I = DIF.compile_unit_begin(),
291            E = DIF.compile_unit_end(); I != E; ++I) {
292     // Each compile unit gets its own .gcno file. This means that whether we run
293     // this pass over the original .o's as they're produced, or run it after
294     // LTO, we'll generate the same .gcno files.
295
296     DICompileUnit CU(*I);
297     raw_fd_ostream *&Out = gcno_files[CU];
298     std::string ErrorInfo;
299     Out = new raw_fd_ostream(
300         (sys::path::stem(CU.getFilename()) + ".gcno").str().c_str(),
301         ErrorInfo, raw_fd_ostream::F_Binary);
302     Out->write("oncg*404MVLL", 12);
303   }
304
305   for (DebugInfoFinder::iterator SPI = DIF.subprogram_begin(),
306            SPE = DIF.subprogram_end(); SPI != SPE; ++SPI) {
307     DISubprogram SP(*SPI);
308     raw_fd_ostream *&os = gcno_files[SP.getCompileUnit()];
309
310     GCOVFunction function(SP, os);
311     Function *F = SP.getFunction();
312     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
313       GCOVBlock &block = function.GetBlock(BB);
314       TerminatorInst *TI = BB->getTerminator();
315       if (int successors = TI->getNumSuccessors()) {
316         for (int i = 0; i != successors; ++i) {
317           block.AddEdge(function.GetBlock(TI->getSuccessor(i)));
318         }
319       }
320
321       uint32_t line = 0;
322       for (BasicBlock::iterator I = BB->begin(), IE = BB->end(); I != IE; ++I) {
323         const DebugLoc &loc = I->getDebugLoc();
324         if (loc.isUnknown()) continue;
325         if (line == loc.getLine()) continue;
326         line = loc.getLine();
327         if (SP != FindSubprogram(DIScope(loc.getScope(*Ctx)))) continue;
328
329         GCOVLines &lines = block.GetFile(SP.getFilename());
330         lines.AddLine(loc.getLine());
331       }
332     }
333     function.WriteOut();
334   }
335
336   for (DenseMap<const MDNode *, raw_fd_ostream *>::iterator
337            I = gcno_files.begin(), E = gcno_files.end(); I != E; ++I) {
338     raw_fd_ostream *&Out = I->second;
339     Out->write("\0\0\0\0\0\0\0\0", 4); // EOF
340     Out->close();
341     delete Out;
342   }
343 }
344
345 bool GCOVProfiler::runOnModule(Module &M) {
346   Mod = &M;
347   Ctx = &M.getContext();
348
349   DebugInfoFinder DIF;
350   DIF.processModule(*Mod);
351
352   EmitGCNO(DIF);
353
354   SmallVector<std::pair<GlobalVariable *, uint32_t>, 8> counters_by_ident;
355   for (DebugInfoFinder::iterator SPI = DIF.subprogram_begin(),
356            SPE = DIF.subprogram_end(); SPI != SPE; ++SPI) {
357     DISubprogram SP(*SPI);
358     Function *F = SP.getFunction();
359
360     // TODO: GCOV format requires a distinct unified exit block.
361     unsigned edges = 0;
362     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
363       TerminatorInst *TI = BB->getTerminator();
364       edges += TI->getNumSuccessors();
365     }
366
367     const ArrayType *counter_type =
368         ArrayType::get(Type::getInt64Ty(*Ctx), edges);
369     GlobalVariable *counter =
370         new GlobalVariable(*Mod, counter_type, false,
371                            GlobalValue::InternalLinkage,
372                            Constant::getNullValue(counter_type),
373                            "__llvm_gcov_ctr", 0, false, 0);
374     counters_by_ident.push_back(
375         std::make_pair(counter, reinterpret_cast<intptr_t>((MDNode*)SP)));
376
377     UniqueVector<BasicBlock *> complex_edge_preds;
378     UniqueVector<BasicBlock *> complex_edge_succs;
379
380     unsigned edge_num = 0;
381     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
382       TerminatorInst *TI = BB->getTerminator();
383       if (int successors = TI->getNumSuccessors()) {
384         IRBuilder<> builder(TI);
385
386         if (successors == 1) {
387           Value *ctr = builder.CreateConstInBoundsGEP2_64(counter, 0, edge_num);
388           Value *count = builder.CreateLoad(ctr);
389           count = builder.CreateAdd(count,
390                                     ConstantInt::get(Type::getInt64Ty(*Ctx),1));
391           builder.CreateStore(count, ctr);
392         } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
393           Value *sel = builder.CreateSelect(
394               BI->getCondition(),
395               ConstantInt::get(Type::getInt64Ty(*Ctx), edge_num),
396               ConstantInt::get(Type::getInt64Ty(*Ctx), edge_num + 1));
397           SmallVector<Value *, 2> idx;
398           idx.push_back(Constant::getNullValue(Type::getInt64Ty(*Ctx)));
399           idx.push_back(sel);
400           Value *ctr = builder.CreateInBoundsGEP(counter,
401                                                  idx.begin(), idx.end());
402           Value *count = builder.CreateLoad(ctr);
403           count = builder.CreateAdd(count,
404                                     ConstantInt::get(Type::getInt64Ty(*Ctx),1));
405           builder.CreateStore(count, ctr);
406         } else {
407           complex_edge_preds.insert(BB);
408           for (int i = 0; i != successors; ++i) {
409             complex_edge_succs.insert(TI->getSuccessor(i));
410           }
411         }
412         edge_num += successors;
413       }
414     }
415
416     // TODO: support switch, invoke, indirectbr
417     if (!complex_edge_preds.empty()) {
418       // emit a [preds x [succs x i64*]].
419       for (int i = 0, e = complex_edge_preds.size(); i != e; ++i) {
420         // call runtime to state save
421       }
422       for (int i = 0, e = complex_edge_succs.size(); i != e; ++i) {
423         // call runtime to perform increment
424       }
425     }
426   }
427
428   InsertCounterWriteout(DIF, counters_by_ident);
429
430   return true;
431 }
432
433 Constant *GCOVProfiler::getStartFileFunc() {
434   const Type *Args[1] = { Type::getInt8PtrTy(*Ctx) };
435   const FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
436                                               Args, false);
437   return Mod->getOrInsertFunction("llvm_gcda_start_file", FTy);
438 }
439
440 Constant *GCOVProfiler::getEmitFunctionFunc() {
441   const Type *Args[1] = { Type::getInt32Ty(*Ctx) };
442   const FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
443                                               Args, false);
444   return Mod->getOrInsertFunction("llvm_gcda_emit_function", FTy);
445 }
446
447 Constant *GCOVProfiler::getEmitArcsFunc() {
448   const Type *Args[] = {
449     Type::getInt32Ty(*Ctx),     // uint32_t num_counters
450     Type::getInt64PtrTy(*Ctx),  // uint64_t *counters
451   };
452   const FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
453                                               Args, false);
454   return Mod->getOrInsertFunction("llvm_gcda_emit_arcs", FTy);
455 }
456
457 Constant *GCOVProfiler::getEndFileFunc() {
458   const FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
459   return Mod->getOrInsertFunction("llvm_gcda_end_file", FTy);
460 }
461
462 static std::string ReplaceStem(std::string orig_filename, std::string new_stem){
463   return (sys::path::stem(orig_filename) + "." + new_stem).str();
464 }
465
466 void GCOVProfiler::InsertCounterWriteout(
467     DebugInfoFinder &DIF,
468     SmallVector<std::pair<GlobalVariable *, uint32_t>, 8> &counters_by_ident) {
469
470   const FunctionType *WriteoutFTy =
471       FunctionType::get(Type::getVoidTy(*Ctx), false);
472   Function *WriteoutF = Function::Create(WriteoutFTy,
473                                          GlobalValue::InternalLinkage,
474                                          "__llvm_gcda_writeout", Mod);
475   WriteoutF->setUnnamedAddr(true);
476   BasicBlock *BB = BasicBlock::Create(*Ctx, "", WriteoutF);
477   IRBuilder<> builder(BB);
478
479   Constant *StartFile = getStartFileFunc();
480   Constant *EmitFunction = getEmitFunctionFunc();
481   Constant *EmitArcs = getEmitArcsFunc();
482   Constant *EndFile = getEndFileFunc();
483
484   for (DebugInfoFinder::iterator CUI = DIF.compile_unit_begin(),
485            CUE = DIF.compile_unit_end(); CUI != CUE; ++CUI) {
486     DICompileUnit compile_unit(*CUI);
487     std::string filename_gcda = ReplaceStem(compile_unit.getFilename(), "gcda");
488     builder.CreateCall(StartFile,
489                        builder.CreateGlobalStringPtr(filename_gcda));
490     for (SmallVector<std::pair<GlobalVariable *, uint32_t>, 8>::iterator
491              I = counters_by_ident.begin(), E = counters_by_ident.end();
492          I != E; ++I) {
493       builder.CreateCall(EmitFunction, ConstantInt::get(Type::getInt32Ty(*Ctx),
494                                                         I->second));
495       GlobalVariable *GV = I->first;
496       unsigned num_arcs =
497           cast<ArrayType>(GV->getType()->getElementType())->getNumElements();
498       builder.CreateCall2(
499           EmitArcs,
500           ConstantInt::get(Type::getInt32Ty(*Ctx), num_arcs),
501           builder.CreateConstGEP2_64(GV, 0, 0));
502     }
503     builder.CreateCall(EndFile);
504   }
505   builder.CreateRetVoid();
506
507   InsertProfilingShutdownCall(WriteoutF, Mod);
508 }