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