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