LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GCOVProfiling.cpp
Go to the documentation of this file.
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 
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/ADT/UniqueVector.h"
26 #include "llvm/DebugInfo.h"
27 #include "llvm/IR/IRBuilder.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/Pass.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/DebugLoc.h"
36 #include "llvm/Support/Path.h"
39 #include <algorithm>
40 #include <string>
41 #include <utility>
42 using namespace llvm;
43 
45 DefaultGCOVVersion("default-gcov-version", cl::init("402*"), cl::Hidden,
47 
49  GCOVOptions Options;
50  Options.EmitNotes = true;
51  Options.EmitData = true;
52  Options.UseCfgChecksum = false;
53  Options.NoRedZone = false;
54  Options.FunctionNamesInData = true;
55 
56  if (DefaultGCOVVersion.size() != 4) {
57  llvm::report_fatal_error(std::string("Invalid -default-gcov-version: ") +
59  }
60  memcpy(Options.Version, DefaultGCOVVersion.c_str(), 4);
61  return Options;
62 }
63 
64 namespace {
65  class GCOVProfiler : public ModulePass {
66  public:
67  static char ID;
68  GCOVProfiler() : ModulePass(ID), Options(GCOVOptions::getDefault()) {
69  ReversedVersion[0] = Options.Version[3];
70  ReversedVersion[1] = Options.Version[2];
71  ReversedVersion[2] = Options.Version[1];
72  ReversedVersion[3] = Options.Version[0];
73  ReversedVersion[4] = '\0';
75  }
76  GCOVProfiler(const GCOVOptions &Options) : ModulePass(ID), Options(Options){
77  assert((Options.EmitNotes || Options.EmitData) &&
78  "GCOVProfiler asked to do nothing?");
79  ReversedVersion[0] = Options.Version[3];
80  ReversedVersion[1] = Options.Version[2];
81  ReversedVersion[2] = Options.Version[1];
82  ReversedVersion[3] = Options.Version[0];
83  ReversedVersion[4] = '\0';
85  }
86  virtual const char *getPassName() const {
87  return "GCOV Profiler";
88  }
89 
90  private:
91  bool runOnModule(Module &M);
92 
93  // Create the .gcno files for the Module based on DebugInfo.
94  void emitProfileNotes();
95 
96  // Modify the program to track transitions along edges and call into the
97  // profiling runtime to emit .gcda files when run.
98  bool emitProfileArcs();
99 
100  // Get pointers to the functions in the runtime library.
101  Constant *getStartFileFunc();
102  Constant *getIncrementIndirectCounterFunc();
103  Constant *getEmitFunctionFunc();
104  Constant *getEmitArcsFunc();
105  Constant *getSummaryInfoFunc();
106  Constant *getDeleteWriteoutFunctionListFunc();
107  Constant *getDeleteFlushFunctionListFunc();
108  Constant *getEndFileFunc();
109 
110  // Create or retrieve an i32 state value that is used to represent the
111  // pred block number for certain non-trivial edges.
112  GlobalVariable *getEdgeStateValue();
113 
114  // Produce a table of pointers to counters, by predecessor and successor
115  // block number.
116  GlobalVariable *buildEdgeLookupTable(Function *F,
117  GlobalVariable *Counter,
118  const UniqueVector<BasicBlock *>&Preds,
119  const UniqueVector<BasicBlock*>&Succs);
120 
121  // Add the function to write out all our counters to the global destructor
122  // list.
123  Function *insertCounterWriteout(ArrayRef<std::pair<GlobalVariable*,
124  MDNode*> >);
125  Function *insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> >);
126  void insertIndirectCounterIncrement();
127 
128  std::string mangleName(DICompileUnit CU, const char *NewStem);
129 
130  GCOVOptions Options;
131 
132  // Reversed, NUL-terminated copy of Options.Version.
133  char ReversedVersion[5];
134 
135  Module *M;
136  LLVMContext *Ctx;
137  };
138 }
139 
140 char GCOVProfiler::ID = 0;
141 INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling",
142  "Insert instrumentation for GCOV profiling", false, false)
143 
144 ModulePass *llvm::createGCOVProfilerPass(const GCOVOptions &Options) {
145  return new GCOVProfiler(Options);
146 }
147 
148 static std::string getFunctionName(DISubprogram SP) {
149  if (!SP.getLinkageName().empty())
150  return SP.getLinkageName();
151  return SP.getName();
152 }
153 
154 namespace {
155  class GCOVRecord {
156  protected:
157  static const char *const LinesTag;
158  static const char *const FunctionTag;
159  static const char *const BlockTag;
160  static const char *const EdgeTag;
161 
162  GCOVRecord() {}
163 
164  void writeBytes(const char *Bytes, int Size) {
165  os->write(Bytes, Size);
166  }
167 
168  void write(uint32_t i) {
169  writeBytes(reinterpret_cast<char*>(&i), 4);
170  }
171 
172  // Returns the length measured in 4-byte blocks that will be used to
173  // represent this string in a GCOV file
174  static unsigned lengthOfGCOVString(StringRef s) {
175  // A GCOV string is a length, followed by a NUL, then between 0 and 3 NULs
176  // padding out to the next 4-byte word. The length is measured in 4-byte
177  // words including padding, not bytes of actual string.
178  return (s.size() / 4) + 1;
179  }
180 
181  void writeGCOVString(StringRef s) {
182  uint32_t Len = lengthOfGCOVString(s);
183  write(Len);
184  writeBytes(s.data(), s.size());
185 
186  // Write 1 to 4 bytes of NUL padding.
187  assert((unsigned)(4 - (s.size() % 4)) > 0);
188  assert((unsigned)(4 - (s.size() % 4)) <= 4);
189  writeBytes("\0\0\0\0", 4 - (s.size() % 4));
190  }
191 
192  raw_ostream *os;
193  };
194  const char *const GCOVRecord::LinesTag = "\0\0\x45\x01";
195  const char *const GCOVRecord::FunctionTag = "\0\0\0\1";
196  const char *const GCOVRecord::BlockTag = "\0\0\x41\x01";
197  const char *const GCOVRecord::EdgeTag = "\0\0\x43\x01";
198 
199  class GCOVFunction;
200  class GCOVBlock;
201 
202  // Constructed only by requesting it from a GCOVBlock, this object stores a
203  // list of line numbers and a single filename, representing lines that belong
204  // to the block.
205  class GCOVLines : public GCOVRecord {
206  public:
207  void addLine(uint32_t Line) {
208  Lines.push_back(Line);
209  }
210 
211  uint32_t length() const {
212  // Here 2 = 1 for string length + 1 for '0' id#.
213  return lengthOfGCOVString(Filename) + 2 + Lines.size();
214  }
215 
216  void writeOut() {
217  write(0);
218  writeGCOVString(Filename);
219  for (int i = 0, e = Lines.size(); i != e; ++i)
220  write(Lines[i]);
221  }
222 
223  GCOVLines(StringRef F, raw_ostream *os)
224  : Filename(F) {
225  this->os = os;
226  }
227 
228  private:
229  StringRef Filename;
231  };
232 
233 
234  // Sorting function for deterministic behaviour in GCOVBlock::writeOut.
235  struct StringKeySort {
236  bool operator()(StringMapEntry<GCOVLines *> *LHS,
237  StringMapEntry<GCOVLines *> *RHS) const {
238  return LHS->getKey() < RHS->getKey();
239  }
240  };
241 
242  // Represent a basic block in GCOV. Each block has a unique number in the
243  // function, number of lines belonging to each block, and a set of edges to
244  // other blocks.
245  class GCOVBlock : public GCOVRecord {
246  public:
247  GCOVLines &getFile(StringRef Filename) {
248  GCOVLines *&Lines = LinesByFile[Filename];
249  if (!Lines) {
250  Lines = new GCOVLines(Filename, os);
251  }
252  return *Lines;
253  }
254 
255  void addEdge(GCOVBlock &Successor) {
256  OutEdges.push_back(&Successor);
257  }
258 
259  void writeOut() {
260  uint32_t Len = 3;
261  SmallVector<StringMapEntry<GCOVLines *> *, 32> SortedLinesByFile;
262  for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
263  E = LinesByFile.end(); I != E; ++I) {
264  Len += I->second->length();
265  SortedLinesByFile.push_back(&*I);
266  }
267 
268  writeBytes(LinesTag, 4);
269  write(Len);
270  write(Number);
271 
272  StringKeySort Sorter;
273  std::sort(SortedLinesByFile.begin(), SortedLinesByFile.end(), Sorter);
275  I = SortedLinesByFile.begin(), E = SortedLinesByFile.end();
276  I != E; ++I)
277  (*I)->getValue()->writeOut();
278  write(0);
279  write(0);
280  }
281 
282  ~GCOVBlock() {
283  DeleteContainerSeconds(LinesByFile);
284  }
285 
286  private:
287  friend class GCOVFunction;
288 
289  GCOVBlock(uint32_t Number, raw_ostream *os)
290  : Number(Number) {
291  this->os = os;
292  }
293 
294  uint32_t Number;
295  StringMap<GCOVLines *> LinesByFile;
297  };
298 
299  // A function has a unique identifier, a checksum (we leave as zero) and a
300  // set of blocks and a map of edges between blocks. This is the only GCOV
301  // object users can construct, the blocks and lines will be rooted here.
302  class GCOVFunction : public GCOVRecord {
303  public:
304  GCOVFunction(DISubprogram SP, raw_ostream *os, uint32_t Ident,
305  bool UseCfgChecksum) {
306  this->os = os;
307 
308  Function *F = SP.getFunction();
309  DEBUG(dbgs() << "Function: " << F->getName() << "\n");
310  uint32_t i = 0;
311  for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
312  Blocks[BB] = new GCOVBlock(i++, os);
313  }
314  ReturnBlock = new GCOVBlock(i++, os);
315 
316  writeBytes(FunctionTag, 4);
317  uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(getFunctionName(SP)) +
318  1 + lengthOfGCOVString(SP.getFilename()) + 1;
319  if (UseCfgChecksum)
320  ++BlockLen;
321  write(BlockLen);
322  write(Ident);
323  write(0); // lineno checksum
324  if (UseCfgChecksum)
325  write(0); // cfg checksum
326  writeGCOVString(getFunctionName(SP));
327  writeGCOVString(SP.getFilename());
328  write(SP.getLineNumber());
329  }
330 
331  ~GCOVFunction() {
332  DeleteContainerSeconds(Blocks);
333  delete ReturnBlock;
334  }
335 
336  GCOVBlock &getBlock(BasicBlock *BB) {
337  return *Blocks[BB];
338  }
339 
340  GCOVBlock &getReturnBlock() {
341  return *ReturnBlock;
342  }
343 
344  void writeOut() {
345  // Emit count of blocks.
346  writeBytes(BlockTag, 4);
347  write(Blocks.size() + 1);
348  for (int i = 0, e = Blocks.size() + 1; i != e; ++i) {
349  write(0); // No flags on our blocks.
350  }
351  DEBUG(dbgs() << Blocks.size() << " blocks.\n");
352 
353  // Emit edges between blocks.
354  if (Blocks.empty()) return;
355  Function *F = Blocks.begin()->first->getParent();
356  for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
357  GCOVBlock &Block = *Blocks[I];
358  if (Block.OutEdges.empty()) continue;
359 
360  writeBytes(EdgeTag, 4);
361  write(Block.OutEdges.size() * 2 + 1);
362  write(Block.Number);
363  for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) {
364  DEBUG(dbgs() << Block.Number << " -> " << Block.OutEdges[i]->Number
365  << "\n");
366  write(Block.OutEdges[i]->Number);
367  write(0); // no flags
368  }
369  }
370 
371  // Emit lines for each block.
372  for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
373  Blocks[I]->writeOut();
374  }
375  }
376 
377  private:
379  GCOVBlock *ReturnBlock;
380  };
381 }
382 
383 std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem) {
384  if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
385  for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
386  MDNode *N = GCov->getOperand(i);
387  if (N->getNumOperands() != 2) continue;
388  MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
390  if (!GCovFile || !CompileUnit) continue;
391  if (CompileUnit == CU) {
392  SmallString<128> Filename = GCovFile->getString();
393  sys::path::replace_extension(Filename, NewStem);
394  return Filename.str();
395  }
396  }
397  }
398 
399  SmallString<128> Filename = CU.getFilename();
400  sys::path::replace_extension(Filename, NewStem);
401  StringRef FName = sys::path::filename(Filename);
402  SmallString<128> CurPath;
403  if (sys::fs::current_path(CurPath)) return FName;
404  sys::path::append(CurPath, FName.str());
405  return CurPath.str();
406 }
407 
408 bool GCOVProfiler::runOnModule(Module &M) {
409  this->M = &M;
410  Ctx = &M.getContext();
411 
412  if (Options.EmitNotes) emitProfileNotes();
413  if (Options.EmitData) return emitProfileArcs();
414  return false;
415 }
416 
417 void GCOVProfiler::emitProfileNotes() {
418  NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
419  if (!CU_Nodes) return;
420 
421  for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
422  // Each compile unit gets its own .gcno file. This means that whether we run
423  // this pass over the original .o's as they're produced, or run it after
424  // LTO, we'll generate the same .gcno files.
425 
426  DICompileUnit CU(CU_Nodes->getOperand(i));
427  std::string ErrorInfo;
428  raw_fd_ostream out(mangleName(CU, "gcno").c_str(), ErrorInfo,
430  out.write("oncg", 4);
431  out.write(ReversedVersion, 4);
432  out.write("MVLL", 4);
433 
434  DIArray SPs = CU.getSubprograms();
435  for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
436  DISubprogram SP(SPs.getElement(i));
437  assert((!SP || SP.isSubprogram()) &&
438  "A MDNode in subprograms of a CU should be null or a DISubprogram.");
439  if (!SP)
440  continue;
441 
442  Function *F = SP.getFunction();
443  if (!F) continue;
444  GCOVFunction Func(SP, &out, i, Options.UseCfgChecksum);
445 
446  for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
447  GCOVBlock &Block = Func.getBlock(BB);
448  TerminatorInst *TI = BB->getTerminator();
449  if (int successors = TI->getNumSuccessors()) {
450  for (int i = 0; i != successors; ++i) {
451  Block.addEdge(Func.getBlock(TI->getSuccessor(i)));
452  }
453  } else if (isa<ReturnInst>(TI)) {
454  Block.addEdge(Func.getReturnBlock());
455  }
456 
457  uint32_t Line = 0;
458  for (BasicBlock::iterator I = BB->begin(), IE = BB->end();
459  I != IE; ++I) {
460  const DebugLoc &Loc = I->getDebugLoc();
461  if (Loc.isUnknown()) continue;
462  if (Line == Loc.getLine()) continue;
463  Line = Loc.getLine();
464  if (SP != getDISubprogram(Loc.getScope(*Ctx))) continue;
465 
466  GCOVLines &Lines = Block.getFile(SP.getFilename());
467  Lines.addLine(Loc.getLine());
468  }
469  }
470  Func.writeOut();
471  }
472  out.write("\0\0\0\0\0\0\0\0", 8); // EOF
473  out.close();
474  }
475 }
476 
477 bool GCOVProfiler::emitProfileArcs() {
478  NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
479  if (!CU_Nodes) return false;
480 
481  bool Result = false;
482  bool InsertIndCounterIncrCode = false;
483  for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
484  DICompileUnit CU(CU_Nodes->getOperand(i));
485  DIArray SPs = CU.getSubprograms();
487  for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
488  DISubprogram SP(SPs.getElement(i));
489  assert((!SP || SP.isSubprogram()) &&
490  "A MDNode in subprograms of a CU should be null or a DISubprogram.");
491  if (!SP)
492  continue;
493  Function *F = SP.getFunction();
494  if (!F) continue;
495  if (!Result) Result = true;
496  unsigned Edges = 0;
497  for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
498  TerminatorInst *TI = BB->getTerminator();
499  if (isa<ReturnInst>(TI))
500  ++Edges;
501  else
502  Edges += TI->getNumSuccessors();
503  }
504 
505  ArrayType *CounterTy =
506  ArrayType::get(Type::getInt64Ty(*Ctx), Edges);
507  GlobalVariable *Counters =
508  new GlobalVariable(*M, CounterTy, false,
510  Constant::getNullValue(CounterTy),
511  "__llvm_gcov_ctr");
512  CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP));
513 
514  UniqueVector<BasicBlock *> ComplexEdgePreds;
515  UniqueVector<BasicBlock *> ComplexEdgeSuccs;
516 
517  unsigned Edge = 0;
518  for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
519  TerminatorInst *TI = BB->getTerminator();
520  int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
521  if (Successors) {
522  if (Successors == 1) {
523  IRBuilder<> Builder(BB->getFirstInsertionPt());
524  Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
525  Edge);
526  Value *Count = Builder.CreateLoad(Counter);
527  Count = Builder.CreateAdd(Count, Builder.getInt64(1));
528  Builder.CreateStore(Count, Counter);
529  } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
530  IRBuilder<> Builder(BI);
531  Value *Sel = Builder.CreateSelect(BI->getCondition(),
532  Builder.getInt64(Edge),
533  Builder.getInt64(Edge + 1));
535  Idx.push_back(Builder.getInt64(0));
536  Idx.push_back(Sel);
537  Value *Counter = Builder.CreateInBoundsGEP(Counters, Idx);
538  Value *Count = Builder.CreateLoad(Counter);
539  Count = Builder.CreateAdd(Count, Builder.getInt64(1));
540  Builder.CreateStore(Count, Counter);
541  } else {
542  ComplexEdgePreds.insert(BB);
543  for (int i = 0; i != Successors; ++i)
544  ComplexEdgeSuccs.insert(TI->getSuccessor(i));
545  }
546 
547  Edge += Successors;
548  }
549  }
550 
551  if (!ComplexEdgePreds.empty()) {
552  GlobalVariable *EdgeTable =
553  buildEdgeLookupTable(F, Counters,
554  ComplexEdgePreds, ComplexEdgeSuccs);
555  GlobalVariable *EdgeState = getEdgeStateValue();
556 
557  for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) {
558  IRBuilder<> Builder(ComplexEdgePreds[i + 1]->getFirstInsertionPt());
559  Builder.CreateStore(Builder.getInt32(i), EdgeState);
560  }
561 
562  for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) {
563  // Call runtime to perform increment.
564  IRBuilder<> Builder(ComplexEdgeSuccs[i+1]->getFirstInsertionPt());
565  Value *CounterPtrArray =
566  Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0,
567  i * ComplexEdgePreds.size());
568 
569  // Build code to increment the counter.
570  InsertIndCounterIncrCode = true;
571  Builder.CreateCall2(getIncrementIndirectCounterFunc(),
572  EdgeState, CounterPtrArray);
573  }
574  }
575  }
576 
577  Function *WriteoutF = insertCounterWriteout(CountersBySP);
578  Function *FlushF = insertFlush(CountersBySP);
579 
580  // Create a small bit of code that registers the "__llvm_gcov_writeout" to
581  // be executed at exit and the "__llvm_gcov_flush" function to be executed
582  // when "__gcov_flush" is called.
583  FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
585  "__llvm_gcov_init", M);
586  F->setUnnamedAddr(true);
589  if (Options.NoRedZone)
591 
592  BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
593  IRBuilder<> Builder(BB);
594 
595  FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
596  Type *Params[] = {
597  PointerType::get(FTy, 0),
598  PointerType::get(FTy, 0)
599  };
600  FTy = FunctionType::get(Builder.getVoidTy(), Params, false);
601 
602  // Initialize the environment and register the local writeout and flush
603  // functions.
604  Constant *GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
605  Builder.CreateCall2(GCOVInit, WriteoutF, FlushF);
606  Builder.CreateRetVoid();
607 
608  appendToGlobalCtors(*M, F, 0);
609  }
610 
611  if (InsertIndCounterIncrCode)
612  insertIndirectCounterIncrement();
613 
614  return Result;
615 }
616 
617 // All edges with successors that aren't branches are "complex", because it
618 // requires complex logic to pick which counter to update.
619 GlobalVariable *GCOVProfiler::buildEdgeLookupTable(
620  Function *F,
621  GlobalVariable *Counters,
622  const UniqueVector<BasicBlock *> &Preds,
623  const UniqueVector<BasicBlock *> &Succs) {
624  // TODO: support invoke, threads. We rely on the fact that nothing can modify
625  // the whole-Module pred edge# between the time we set it and the time we next
626  // read it. Threads and invoke make this untrue.
627 
628  // emit [(succs * preds) x i64*], logically [succ x [pred x i64*]].
629  size_t TableSize = Succs.size() * Preds.size();
630  Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
631  ArrayType *EdgeTableTy = ArrayType::get(Int64PtrTy, TableSize);
632 
633  OwningArrayPtr<Constant *> EdgeTable(new Constant*[TableSize]);
634  Constant *NullValue = Constant::getNullValue(Int64PtrTy);
635  for (size_t i = 0; i != TableSize; ++i)
636  EdgeTable[i] = NullValue;
637 
638  unsigned Edge = 0;
639  for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
640  TerminatorInst *TI = BB->getTerminator();
641  int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
642  if (Successors > 1 && !isa<BranchInst>(TI) && !isa<ReturnInst>(TI)) {
643  for (int i = 0; i != Successors; ++i) {
644  BasicBlock *Succ = TI->getSuccessor(i);
645  IRBuilder<> Builder(Succ);
646  Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
647  Edge + i);
648  EdgeTable[((Succs.idFor(Succ)-1) * Preds.size()) +
649  (Preds.idFor(BB)-1)] = cast<Constant>(Counter);
650  }
651  }
652  Edge += Successors;
653  }
654 
655  ArrayRef<Constant*> V(&EdgeTable[0], TableSize);
656  GlobalVariable *EdgeTableGV =
657  new GlobalVariable(
658  *M, EdgeTableTy, true, GlobalValue::InternalLinkage,
659  ConstantArray::get(EdgeTableTy, V),
660  "__llvm_gcda_edge_table");
661  EdgeTableGV->setUnnamedAddr(true);
662  return EdgeTableGV;
663 }
664 
665 Constant *GCOVProfiler::getStartFileFunc() {
666  Type *Args[] = {
667  Type::getInt8PtrTy(*Ctx), // const char *orig_filename
668  Type::getInt8PtrTy(*Ctx), // const char version[4]
669  };
670  FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
671  return M->getOrInsertFunction("llvm_gcda_start_file", FTy);
672 }
673 
674 Constant *GCOVProfiler::getIncrementIndirectCounterFunc() {
675  Type *Int32Ty = Type::getInt32Ty(*Ctx);
676  Type *Int64Ty = Type::getInt64Ty(*Ctx);
677  Type *Args[] = {
678  Int32Ty->getPointerTo(), // uint32_t *predecessor
679  Int64Ty->getPointerTo()->getPointerTo() // uint64_t **counters
680  };
681  FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
682  return M->getOrInsertFunction("__llvm_gcov_indirect_counter_increment", FTy);
683 }
684 
685 Constant *GCOVProfiler::getEmitFunctionFunc() {
686  Type *Args[3] = {
687  Type::getInt32Ty(*Ctx), // uint32_t ident
688  Type::getInt8PtrTy(*Ctx), // const char *function_name
689  Type::getInt8Ty(*Ctx), // uint8_t use_extra_checksum
690  };
691  FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
692  return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
693 }
694 
695 Constant *GCOVProfiler::getEmitArcsFunc() {
696  Type *Args[] = {
697  Type::getInt32Ty(*Ctx), // uint32_t num_counters
698  Type::getInt64PtrTy(*Ctx), // uint64_t *counters
699  };
700  FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
701  return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy);
702 }
703 
704 Constant *GCOVProfiler::getSummaryInfoFunc() {
705  FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
706  return M->getOrInsertFunction("llvm_gcda_summary_info", FTy);
707 }
708 
709 Constant *GCOVProfiler::getDeleteWriteoutFunctionListFunc() {
710  FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
711  return M->getOrInsertFunction("llvm_delete_writeout_function_list", FTy);
712 }
713 
714 Constant *GCOVProfiler::getDeleteFlushFunctionListFunc() {
715  FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
716  return M->getOrInsertFunction("llvm_delete_flush_function_list", FTy);
717 }
718 
719 Constant *GCOVProfiler::getEndFileFunc() {
720  FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
721  return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
722 }
723 
724 GlobalVariable *GCOVProfiler::getEdgeStateValue() {
725  GlobalVariable *GV = M->getGlobalVariable("__llvm_gcov_global_state_pred");
726  if (!GV) {
727  GV = new GlobalVariable(*M, Type::getInt32Ty(*Ctx), false,
730  0xffffffff),
731  "__llvm_gcov_global_state_pred");
732  GV->setUnnamedAddr(true);
733  }
734  return GV;
735 }
736 
737 Function *GCOVProfiler::insertCounterWriteout(
738  ArrayRef<std::pair<GlobalVariable *, MDNode *> > CountersBySP) {
739  FunctionType *WriteoutFTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
740  Function *WriteoutF = M->getFunction("__llvm_gcov_writeout");
741  if (!WriteoutF)
742  WriteoutF = Function::Create(WriteoutFTy, GlobalValue::InternalLinkage,
743  "__llvm_gcov_writeout", M);
744  WriteoutF->setUnnamedAddr(true);
745  WriteoutF->addFnAttr(Attribute::NoInline);
746  if (Options.NoRedZone)
747  WriteoutF->addFnAttr(Attribute::NoRedZone);
748 
749  BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
750  IRBuilder<> Builder(BB);
751 
752  Constant *StartFile = getStartFileFunc();
753  Constant *EmitFunction = getEmitFunctionFunc();
754  Constant *EmitArcs = getEmitArcsFunc();
755  Constant *SummaryInfo = getSummaryInfoFunc();
756  Constant *EndFile = getEndFileFunc();
757 
758  NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
759  if (CU_Nodes) {
760  for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
761  DICompileUnit CU(CU_Nodes->getOperand(i));
762  std::string FilenameGcda = mangleName(CU, "gcda");
763  Builder.CreateCall2(StartFile,
764  Builder.CreateGlobalStringPtr(FilenameGcda),
765  Builder.CreateGlobalStringPtr(ReversedVersion));
766  for (unsigned j = 0, e = CountersBySP.size(); j != e; ++j) {
767  DISubprogram SP(CountersBySP[j].second);
768  Builder.CreateCall3(
769  EmitFunction, Builder.getInt32(j),
770  Options.FunctionNamesInData ?
771  Builder.CreateGlobalStringPtr(getFunctionName(SP)) :
772  Constant::getNullValue(Builder.getInt8PtrTy()),
773  Builder.getInt8(Options.UseCfgChecksum));
774 
775  GlobalVariable *GV = CountersBySP[j].first;
776  unsigned Arcs =
777  cast<ArrayType>(GV->getType()->getElementType())->getNumElements();
778  Builder.CreateCall2(EmitArcs,
779  Builder.getInt32(Arcs),
780  Builder.CreateConstGEP2_64(GV, 0, 0));
781  }
782  Builder.CreateCall(SummaryInfo);
783  Builder.CreateCall(EndFile);
784  }
785  }
786 
787  Builder.CreateRetVoid();
788  return WriteoutF;
789 }
790 
791 void GCOVProfiler::insertIndirectCounterIncrement() {
792  Function *Fn =
793  cast<Function>(GCOVProfiler::getIncrementIndirectCounterFunc());
794  Fn->setUnnamedAddr(true);
797  if (Options.NoRedZone)
799 
800  // Create basic blocks for function.
801  BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", Fn);
802  IRBuilder<> Builder(BB);
803 
804  BasicBlock *PredNotNegOne = BasicBlock::Create(*Ctx, "", Fn);
805  BasicBlock *CounterEnd = BasicBlock::Create(*Ctx, "", Fn);
806  BasicBlock *Exit = BasicBlock::Create(*Ctx, "exit", Fn);
807 
808  // uint32_t pred = *predecessor;
809  // if (pred == 0xffffffff) return;
810  Argument *Arg = Fn->arg_begin();
811  Arg->setName("predecessor");
812  Value *Pred = Builder.CreateLoad(Arg, "pred");
813  Value *Cond = Builder.CreateICmpEQ(Pred, Builder.getInt32(0xffffffff));
814  BranchInst::Create(Exit, PredNotNegOne, Cond, BB);
815 
816  Builder.SetInsertPoint(PredNotNegOne);
817 
818  // uint64_t *counter = counters[pred];
819  // if (!counter) return;
820  Value *ZExtPred = Builder.CreateZExt(Pred, Builder.getInt64Ty());
821  Arg = llvm::next(Fn->arg_begin());
822  Arg->setName("counters");
823  Value *GEP = Builder.CreateGEP(Arg, ZExtPred);
824  Value *Counter = Builder.CreateLoad(GEP, "counter");
825  Cond = Builder.CreateICmpEQ(Counter,
827  Builder.getInt64Ty()->getPointerTo()));
828  Builder.CreateCondBr(Cond, Exit, CounterEnd);
829 
830  // ++*counter;
831  Builder.SetInsertPoint(CounterEnd);
832  Value *Add = Builder.CreateAdd(Builder.CreateLoad(Counter),
833  Builder.getInt64(1));
834  Builder.CreateStore(Add, Counter);
835  Builder.CreateBr(Exit);
836 
837  // Fill in the exit block.
838  Builder.SetInsertPoint(Exit);
839  Builder.CreateRetVoid();
840 }
841 
842 Function *GCOVProfiler::
843 insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> > CountersBySP) {
844  FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
845  Function *FlushF = M->getFunction("__llvm_gcov_flush");
846  if (!FlushF)
848  "__llvm_gcov_flush", M);
849  else
851  FlushF->setUnnamedAddr(true);
853  if (Options.NoRedZone)
855 
856  BasicBlock *Entry = BasicBlock::Create(*Ctx, "entry", FlushF);
857 
858  // Write out the current counters.
859  Constant *WriteoutF = M->getFunction("__llvm_gcov_writeout");
860  assert(WriteoutF && "Need to create the writeout function first!");
861 
862  IRBuilder<> Builder(Entry);
863  Builder.CreateCall(WriteoutF);
864 
865  // Zero out the counters.
866  for (ArrayRef<std::pair<GlobalVariable *, MDNode *> >::iterator
867  I = CountersBySP.begin(), E = CountersBySP.end();
868  I != E; ++I) {
869  GlobalVariable *GV = I->first;
871  Builder.CreateStore(Null, GV);
872  }
873 
874  Type *RetTy = FlushF->getReturnType();
875  if (RetTy == Type::getVoidTy(*Ctx))
876  Builder.CreateRetVoid();
877  else if (RetTy->isIntegerTy())
878  // Used if __llvm_gcov_flush was implicitly declared.
879  Builder.CreateRet(ConstantInt::get(RetTy, 0));
880  else
881  report_fatal_error("invalid return type for __llvm_gcov_flush");
882 
883  return FlushF;
884 }
void DeleteContainerSeconds(Container &C)
Definition: STLExtras.h:324
StringRef getString() const
Definition: Metadata.h:46
GCOVBlock(GCOVFunction &P, uint32_t N)
Definition: GCOV.h:227
static PassRegistry * getPassRegistry()
LLVM Argument representation.
Definition: Argument.h:35
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
bool isSubprogram() const
Definition: DebugInfo.cpp:225
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
iterator end()
Definition: Function.h:397
static GCOVOptions getDefault()
ModulePass * createGCOVProfilerPass(const GCOVOptions &Options=GCOVOptions::getDefault())
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
unsigned getNumOperands() const
getNumOperands - Return number of MDNode operands.
Definition: Metadata.h:142
void appendToGlobalCtors(Module &M, Function *F, int Priority)
Definition: ModuleUtils.cpp:59
static PointerType * get(Type *ElementType, unsigned AddressSpace)
Definition: Type.cpp:730
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension)
Replace the file extension of path with extension.
Definition: Path.cpp:433
error_code current_path(SmallVectorImpl< char > &result)
Get the current path.
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:181
Type * getReturnType() const
Definition: Function.cpp:179
MDNode - a tuple of other values.
Definition: Metadata.h:69
F(f)
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:242
static PointerType * getInt64PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:296
const GlobalVariable * getGlobalVariable(StringRef Name, bool AllowInternal=false) const
Definition: Module.h:355
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:111
StringRef getName() const
Definition: Value.cpp:167
static std::string getFunctionName(DISubprogram SP)
Value * getOperand(unsigned i) const LLVM_READONLY
getOperand - Return specified operand.
Definition: Metadata.cpp:307
DIArray - This descriptor holds an array of descriptors.
Definition: DebugInfo.h:167
bool isUnknown() const
isUnknown - Return true if this is an unknown location.
Definition: DebugLoc.h:70
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:372
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:421
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=0)
unsigned getLine() const
Definition: DebugLoc.h:72
void setName(const Twine &Name)
Definition: Value.cpp:175
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
void addFnAttr(Attribute::AttrKind N)
Add function attributes to this function.
Definition: Function.h:176
unsigned idFor(const T &Entry) const
Definition: UniqueVector.h:53
DISubprogram - This is a wrapper for a subprogram (e.g. a function).
Definition: DebugInfo.h:429
const char * data() const
Definition: StringRef.h:107
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
Definition: Type.cpp:361
GCOVBlock - Collects block information.
Definition: GCOV.h:225
unsigned getLineNumber() const
Definition: DebugInfo.h:440
StringRef getName() const
Definition: DebugInfo.h:437
iterator begin()
Definition: Function.h:395
Type * getElementType() const
Definition: DerivedTypes.h:319
Function * getFunction(StringRef Name) const
Definition: Module.cpp:221
DISubprogram getDISubprogram(const MDNode *Scope)
getDISubprogram - Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:885
Function * getFunction() const
Definition: DebugInfo.h:482
unsigned getNumSuccessors() const
Definition: InstrTypes.h:59
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:314
StringRef getFilename() const
Definition: DebugInfo.cpp:773
bool empty() const
Definition: UniqueVector.h:77
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeSet AttributeList)
Definition: Module.cpp:138
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
INITIALIZE_PASS(GCOVProfiler,"insert-gcov-profiling","Insert instrumentation for GCOV profiling", false, false) ModulePass *llvm
BasicBlock * getSuccessor(unsigned idx) const
Definition: InstrTypes.h:65
Type * Int32Ty
DIDescriptor getElement(unsigned Idx) const
Definition: DebugInfo.h:172
LLVM Constant Representation.
Definition: Constant.h:41
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:745
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:227
ItTy next(ItTy it, Dist n)
Definition: STLExtras.h:154
void addEdge(uint32_t N)
Definition: GCOV.h:230
MDNode * getOperand(unsigned i) const
getOperand - Return specified operand.
Definition: Metadata.cpp:545
StringRef getLinkageName() const
Definition: DebugInfo.h:439
arg_iterator arg_begin()
Definition: Function.h:410
unsigned getNumElements() const
Definition: DebugInfo.cpp:328
Value * CreateInBoundsGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:944
PointerType * getPointerTo(unsigned AddrSpace=0)
Definition: Type.cpp:756
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:284
~GCOVFunction()
~GCOVFunction - Delete GCOVFunction and its content.
Definition: GCOV.cpp:107
MDNode * getScope(const LLVMContext &Ctx) const
Definition: DebugLoc.cpp:20
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Definition: Windows.h:154
void initializeGCOVProfilerPass(PassRegistry &)
void setUnnamedAddr(bool Val)
Definition: GlobalValue.h:85
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:492
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:217
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
StringRef getKey() const
Definition: StringMap.h:128
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:270
bool isIntegerTy() const
Definition: Type.h:196
ssize_t write(int fildes, const void *buf, size_t nbyte);
static cl::opt< std::string > DefaultGCOVVersion("default-gcov-version", cl::init("402*"), cl::Hidden, cl::ValueRequired)
Disable redzone.
Definition: Attributes.h:88
PointerType * getType() const
getType - Global values are always pointers.
Definition: GlobalValue.h:107
NamedMDNode * getNamedMetadata(const Twine &Name) const
Definition: Module.cpp:286
GCOVFunction - Collects function information.
Definition: GCOV.h:208
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:241
unsigned insert(const T &Entry)
Definition: UniqueVector.h:36
size_t size() const
Definition: UniqueVector.h:73
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:120
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
Definition: BasicBlock.h:109
static ArrayType * get(Type *ElementType, uint64_t NumElements)
Definition: Type.cpp:679
Rename collisions when linking (static functions).
Definition: GlobalValue.h:41
LLVM Value Representation.
Definition: Value.h:66
~GCOVBlock()
~GCOVBlock - Delete GCOVBlock and its content.
Definition: GCOV.cpp:243
unsigned getNumOperands() const
getNumOperands - Return the number of NamedMDNode operands.
Definition: Metadata.cpp:540
#define DEBUG(X)
Definition: Debug.h:97
DIArray getSubprograms() const
Definition: DebugInfo.cpp:799
const StringRef filename(StringRef path)
Get filename.
Definition: Path.cpp:467
DICompileUnit - A wrapper for a compile unit.
Definition: DebugInfo.h:402
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:239
LLVMContext & getContext() const
Definition: Module.h:249
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=0)
Definition: Function.h:128
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110