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