1 //===- GCOVProfiling.cpp - Insert edge counters for gcov profiling --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass implements GCOV-style profiling. When this pass is run it emits
10 // "gcno" files next to the existing source, and instruments the code that runs
11 // to records the edges between blocks that run and emit a complementary "gcda"
12 // file on exit.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/Sequence.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/Analysis/EHPersonalities.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/IR/CFG.h"
26 #include "llvm/IR/DebugInfo.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/IR/IRBuilder.h"
29 #include "llvm/IR/InstIterator.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/InitializePasses.h"
34 #include "llvm/Pass.h"
35 #include "llvm/Support/CRC.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/FileSystem.h"
39 #include "llvm/Support/Path.h"
40 #include "llvm/Support/Regex.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Transforms/Instrumentation.h"
43 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
44 #include "llvm/Transforms/Utils/ModuleUtils.h"
45 #include <algorithm>
46 #include <memory>
47 #include <string>
48 #include <utility>
49 
50 using namespace llvm;
51 namespace endian = llvm::support::endian;
52 
53 #define DEBUG_TYPE "insert-gcov-profiling"
54 
55 enum : uint32_t {
56   GCOV_TAG_FUNCTION = 0x01000000,
57   GCOV_TAG_BLOCKS = 0x01410000,
58   GCOV_TAG_ARCS = 0x01430000,
59   GCOV_TAG_LINES = 0x01450000,
60 };
61 
62 static cl::opt<std::string> DefaultGCOVVersion("default-gcov-version",
63                                                cl::init("408*"), cl::Hidden,
64                                                cl::ValueRequired);
65 
66 static cl::opt<bool> AtomicCounter("gcov-atomic-counter", cl::Hidden,
67                                    cl::desc("Make counter updates atomic"));
68 
69 // Returns the number of words which will be used to represent this string.
70 static unsigned wordsOfString(StringRef s) {
71   // Length + NUL-terminated string + 0~3 padding NULs.
72   return (s.size() / 4) + 2;
73 }
74 
75 GCOVOptions GCOVOptions::getDefault() {
76   GCOVOptions Options;
77   Options.EmitNotes = true;
78   Options.EmitData = true;
79   Options.NoRedZone = false;
80   Options.Atomic = AtomicCounter;
81 
82   if (DefaultGCOVVersion.size() != 4) {
83     llvm::report_fatal_error(std::string("Invalid -default-gcov-version: ") +
84                              DefaultGCOVVersion);
85   }
86   memcpy(Options.Version, DefaultGCOVVersion.c_str(), 4);
87   return Options;
88 }
89 
90 namespace {
91 class GCOVFunction;
92 
93 class GCOVProfiler {
94 public:
95   GCOVProfiler() : GCOVProfiler(GCOVOptions::getDefault()) {}
96   GCOVProfiler(const GCOVOptions &Opts) : Options(Opts) {}
97   bool
98   runOnModule(Module &M,
99               std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
100 
101   void write(uint32_t i) {
102     char Bytes[4];
103     endian::write32(Bytes, i, Endian);
104     os->write(Bytes, 4);
105   }
106   void writeString(StringRef s) {
107     write(wordsOfString(s) - 1);
108     os->write(s.data(), s.size());
109     os->write_zeros(4 - s.size() % 4);
110   }
111   void writeBytes(const char *Bytes, int Size) { os->write(Bytes, Size); }
112 
113 private:
114   // Create the .gcno files for the Module based on DebugInfo.
115   void emitProfileNotes();
116 
117   // Modify the program to track transitions along edges and call into the
118   // profiling runtime to emit .gcda files when run.
119   bool emitProfileArcs();
120 
121   bool isFunctionInstrumented(const Function &F);
122   std::vector<Regex> createRegexesFromString(StringRef RegexesStr);
123   static bool doesFilenameMatchARegex(StringRef Filename,
124                                       std::vector<Regex> &Regexes);
125 
126   // Get pointers to the functions in the runtime library.
127   FunctionCallee getStartFileFunc(const TargetLibraryInfo *TLI);
128   FunctionCallee getEmitFunctionFunc(const TargetLibraryInfo *TLI);
129   FunctionCallee getEmitArcsFunc(const TargetLibraryInfo *TLI);
130   FunctionCallee getSummaryInfoFunc();
131   FunctionCallee getEndFileFunc();
132 
133   // Add the function to write out all our counters to the global destructor
134   // list.
135   Function *
136   insertCounterWriteout(ArrayRef<std::pair<GlobalVariable *, MDNode *>>);
137   Function *insertReset(ArrayRef<std::pair<GlobalVariable *, MDNode *>>);
138 
139   bool AddFlushBeforeForkAndExec();
140 
141   enum class GCovFileType { GCNO, GCDA };
142   std::string mangleName(const DICompileUnit *CU, GCovFileType FileType);
143 
144   GCOVOptions Options;
145   support::endianness Endian;
146   raw_ostream *os;
147 
148   // Checksum, produced by hash of EdgeDestinations
149   SmallVector<uint32_t, 4> FileChecksums;
150 
151   Module *M = nullptr;
152   std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
153   LLVMContext *Ctx = nullptr;
154   SmallVector<std::unique_ptr<GCOVFunction>, 16> Funcs;
155   std::vector<Regex> FilterRe;
156   std::vector<Regex> ExcludeRe;
157   StringMap<bool> InstrumentedFiles;
158 };
159 
160 class GCOVProfilerLegacyPass : public ModulePass {
161 public:
162   static char ID;
163   GCOVProfilerLegacyPass()
164       : GCOVProfilerLegacyPass(GCOVOptions::getDefault()) {}
165   GCOVProfilerLegacyPass(const GCOVOptions &Opts)
166       : ModulePass(ID), Profiler(Opts) {
167     initializeGCOVProfilerLegacyPassPass(*PassRegistry::getPassRegistry());
168   }
169   StringRef getPassName() const override { return "GCOV Profiler"; }
170 
171   bool runOnModule(Module &M) override {
172     return Profiler.runOnModule(M, [this](Function &F) -> TargetLibraryInfo & {
173       return getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
174     });
175   }
176 
177   void getAnalysisUsage(AnalysisUsage &AU) const override {
178     AU.addRequired<TargetLibraryInfoWrapperPass>();
179   }
180 
181 private:
182   GCOVProfiler Profiler;
183 };
184 }
185 
186 char GCOVProfilerLegacyPass::ID = 0;
187 INITIALIZE_PASS_BEGIN(
188     GCOVProfilerLegacyPass, "insert-gcov-profiling",
189     "Insert instrumentation for GCOV profiling", false, false)
190 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
191 INITIALIZE_PASS_END(
192     GCOVProfilerLegacyPass, "insert-gcov-profiling",
193     "Insert instrumentation for GCOV profiling", false, false)
194 
195 ModulePass *llvm::createGCOVProfilerPass(const GCOVOptions &Options) {
196   return new GCOVProfilerLegacyPass(Options);
197 }
198 
199 static StringRef getFunctionName(const DISubprogram *SP) {
200   if (!SP->getLinkageName().empty())
201     return SP->getLinkageName();
202   return SP->getName();
203 }
204 
205 /// Extract a filename for a DISubprogram.
206 ///
207 /// Prefer relative paths in the coverage notes. Clang also may split
208 /// up absolute paths into a directory and filename component. When
209 /// the relative path doesn't exist, reconstruct the absolute path.
210 static SmallString<128> getFilename(const DISubprogram *SP) {
211   SmallString<128> Path;
212   StringRef RelPath = SP->getFilename();
213   if (sys::fs::exists(RelPath))
214     Path = RelPath;
215   else
216     sys::path::append(Path, SP->getDirectory(), SP->getFilename());
217   return Path;
218 }
219 
220 namespace {
221   class GCOVRecord {
222   protected:
223     GCOVProfiler *P;
224 
225     GCOVRecord(GCOVProfiler *P) : P(P) {}
226 
227     void write(uint32_t i) { P->write(i); }
228     void writeString(StringRef s) { P->writeString(s); }
229     void writeBytes(const char *Bytes, int Size) { P->writeBytes(Bytes, Size); }
230   };
231 
232   class GCOVFunction;
233   class GCOVBlock;
234 
235   // Constructed only by requesting it from a GCOVBlock, this object stores a
236   // list of line numbers and a single filename, representing lines that belong
237   // to the block.
238   class GCOVLines : public GCOVRecord {
239    public:
240     void addLine(uint32_t Line) {
241       assert(Line != 0 && "Line zero is not a valid real line number.");
242       Lines.push_back(Line);
243     }
244 
245     uint32_t length() const {
246       return 1 + wordsOfString(Filename) + Lines.size();
247     }
248 
249     void writeOut() {
250       write(0);
251       writeString(Filename);
252       for (int i = 0, e = Lines.size(); i != e; ++i)
253         write(Lines[i]);
254     }
255 
256     GCOVLines(GCOVProfiler *P, StringRef F)
257         : GCOVRecord(P), Filename(std::string(F)) {}
258 
259   private:
260     std::string Filename;
261     SmallVector<uint32_t, 32> Lines;
262   };
263 
264 
265   // Represent a basic block in GCOV. Each block has a unique number in the
266   // function, number of lines belonging to each block, and a set of edges to
267   // other blocks.
268   class GCOVBlock : public GCOVRecord {
269    public:
270     GCOVLines &getFile(StringRef Filename) {
271       return LinesByFile.try_emplace(Filename, P, Filename).first->second;
272     }
273 
274     void addEdge(GCOVBlock &Successor) {
275       OutEdges.push_back(&Successor);
276     }
277 
278     void writeOut() {
279       uint32_t Len = 3;
280       SmallVector<StringMapEntry<GCOVLines> *, 32> SortedLinesByFile;
281       for (auto &I : LinesByFile) {
282         Len += I.second.length();
283         SortedLinesByFile.push_back(&I);
284       }
285 
286       write(GCOV_TAG_LINES);
287       write(Len);
288       write(Number);
289 
290       llvm::sort(SortedLinesByFile, [](StringMapEntry<GCOVLines> *LHS,
291                                        StringMapEntry<GCOVLines> *RHS) {
292         return LHS->getKey() < RHS->getKey();
293       });
294       for (auto &I : SortedLinesByFile)
295         I->getValue().writeOut();
296       write(0);
297       write(0);
298     }
299 
300     GCOVBlock(const GCOVBlock &RHS) : GCOVRecord(RHS), Number(RHS.Number) {
301       // Only allow copy before edges and lines have been added. After that,
302       // there are inter-block pointers (eg: edges) that won't take kindly to
303       // blocks being copied or moved around.
304       assert(LinesByFile.empty());
305       assert(OutEdges.empty());
306     }
307 
308     uint32_t Number;
309     SmallVector<GCOVBlock *, 4> OutEdges;
310 
311    private:
312     friend class GCOVFunction;
313 
314     GCOVBlock(GCOVProfiler *P, uint32_t Number)
315         : GCOVRecord(P), Number(Number) {}
316 
317     StringMap<GCOVLines> LinesByFile;
318   };
319 
320   // A function has a unique identifier, a checksum (we leave as zero) and a
321   // set of blocks and a map of edges between blocks. This is the only GCOV
322   // object users can construct, the blocks and lines will be rooted here.
323   class GCOVFunction : public GCOVRecord {
324   public:
325     GCOVFunction(GCOVProfiler *P, Function *F, const DISubprogram *SP,
326                  unsigned EndLine, uint32_t Ident, int Version)
327         : GCOVRecord(P), SP(SP), EndLine(EndLine), Ident(Ident),
328           Version(Version), EntryBlock(P, 0), ReturnBlock(P, 1) {
329       LLVM_DEBUG(dbgs() << "Function: " << getFunctionName(SP) << "\n");
330       bool ExitBlockBeforeBody = Version >= 48;
331       uint32_t i = ExitBlockBeforeBody ? 2 : 1;
332       for (BasicBlock &BB : *F)
333         Blocks.insert(std::make_pair(&BB, GCOVBlock(P, i++)));
334       if (!ExitBlockBeforeBody)
335         ReturnBlock.Number = i;
336 
337       std::string FunctionNameAndLine;
338       raw_string_ostream FNLOS(FunctionNameAndLine);
339       FNLOS << getFunctionName(SP) << SP->getLine();
340       FNLOS.flush();
341       FuncChecksum = hash_value(FunctionNameAndLine);
342     }
343 
344     GCOVBlock &getBlock(BasicBlock *BB) {
345       return Blocks.find(BB)->second;
346     }
347 
348     GCOVBlock &getEntryBlock() { return EntryBlock; }
349     GCOVBlock &getReturnBlock() {
350       return ReturnBlock;
351     }
352 
353     uint32_t getFuncChecksum() const {
354       return FuncChecksum;
355     }
356 
357     void writeOut(uint32_t CfgChecksum) {
358       write(GCOV_TAG_FUNCTION);
359       SmallString<128> Filename = getFilename(SP);
360       uint32_t BlockLen =
361           2 + (Version >= 47) + wordsOfString(getFunctionName(SP));
362       if (Version < 80)
363         BlockLen += wordsOfString(Filename) + 1;
364       else
365         BlockLen += 1 + wordsOfString(Filename) + 3 + (Version >= 90);
366 
367       write(BlockLen);
368       write(Ident);
369       write(FuncChecksum);
370       if (Version >= 47)
371         write(CfgChecksum);
372       writeString(getFunctionName(SP));
373       if (Version < 80) {
374         writeString(Filename);
375         write(SP->getLine());
376       } else {
377         write(SP->isArtificial()); // artificial
378         writeString(Filename);
379         write(SP->getLine()); // start_line
380         write(0);             // start_column
381         // EndLine is the last line with !dbg. It is not the } line as in GCC,
382         // but good enough.
383         write(EndLine);
384         if (Version >= 90)
385           write(0); // end_column
386       }
387 
388       // Emit count of blocks.
389       write(GCOV_TAG_BLOCKS);
390       if (Version < 80) {
391         write(Blocks.size() + 2);
392         for (int i = Blocks.size() + 2; i; --i)
393           write(0);
394       } else {
395         write(1);
396         write(Blocks.size() + 2);
397       }
398       LLVM_DEBUG(dbgs() << (Blocks.size() + 1) << " blocks\n");
399 
400       // Emit edges between blocks.
401       Function *F = Blocks.begin()->first->getParent();
402       write(GCOV_TAG_ARCS);
403       write(3);
404       write(0);
405       write(getBlock(&F->getEntryBlock()).Number);
406       write(0); // no flags
407       for (BasicBlock &I : *F) {
408         GCOVBlock &Block = getBlock(&I);
409         if (Block.OutEdges.empty()) continue;
410 
411         write(GCOV_TAG_ARCS);
412         write(Block.OutEdges.size() * 2 + 1);
413         write(Block.Number);
414         for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) {
415           LLVM_DEBUG(dbgs() << Block.Number << " -> "
416                             << Block.OutEdges[i]->Number << "\n");
417           write(Block.OutEdges[i]->Number);
418           write(0);  // no flags
419         }
420       }
421 
422       // Emit lines for each block.
423       for (BasicBlock &I : *F)
424         getBlock(&I).writeOut();
425     }
426 
427   private:
428     const DISubprogram *SP;
429     unsigned EndLine;
430     uint32_t Ident;
431     uint32_t FuncChecksum;
432     int Version;
433     DenseMap<BasicBlock *, GCOVBlock> Blocks;
434     GCOVBlock EntryBlock;
435     GCOVBlock ReturnBlock;
436   };
437 }
438 
439 // RegexesStr is a string containing differents regex separated by a semi-colon.
440 // For example "foo\..*$;bar\..*$".
441 std::vector<Regex> GCOVProfiler::createRegexesFromString(StringRef RegexesStr) {
442   std::vector<Regex> Regexes;
443   while (!RegexesStr.empty()) {
444     std::pair<StringRef, StringRef> HeadTail = RegexesStr.split(';');
445     if (!HeadTail.first.empty()) {
446       Regex Re(HeadTail.first);
447       std::string Err;
448       if (!Re.isValid(Err)) {
449         Ctx->emitError(Twine("Regex ") + HeadTail.first +
450                        " is not valid: " + Err);
451       }
452       Regexes.emplace_back(std::move(Re));
453     }
454     RegexesStr = HeadTail.second;
455   }
456   return Regexes;
457 }
458 
459 bool GCOVProfiler::doesFilenameMatchARegex(StringRef Filename,
460                                            std::vector<Regex> &Regexes) {
461   for (Regex &Re : Regexes)
462     if (Re.match(Filename))
463       return true;
464   return false;
465 }
466 
467 bool GCOVProfiler::isFunctionInstrumented(const Function &F) {
468   if (FilterRe.empty() && ExcludeRe.empty()) {
469     return true;
470   }
471   SmallString<128> Filename = getFilename(F.getSubprogram());
472   auto It = InstrumentedFiles.find(Filename);
473   if (It != InstrumentedFiles.end()) {
474     return It->second;
475   }
476 
477   SmallString<256> RealPath;
478   StringRef RealFilename;
479 
480   // Path can be
481   // /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/*.h so for
482   // such a case we must get the real_path.
483   if (sys::fs::real_path(Filename, RealPath)) {
484     // real_path can fail with path like "foo.c".
485     RealFilename = Filename;
486   } else {
487     RealFilename = RealPath;
488   }
489 
490   bool ShouldInstrument;
491   if (FilterRe.empty()) {
492     ShouldInstrument = !doesFilenameMatchARegex(RealFilename, ExcludeRe);
493   } else if (ExcludeRe.empty()) {
494     ShouldInstrument = doesFilenameMatchARegex(RealFilename, FilterRe);
495   } else {
496     ShouldInstrument = doesFilenameMatchARegex(RealFilename, FilterRe) &&
497                        !doesFilenameMatchARegex(RealFilename, ExcludeRe);
498   }
499   InstrumentedFiles[Filename] = ShouldInstrument;
500   return ShouldInstrument;
501 }
502 
503 std::string GCOVProfiler::mangleName(const DICompileUnit *CU,
504                                      GCovFileType OutputType) {
505   bool Notes = OutputType == GCovFileType::GCNO;
506 
507   if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
508     for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
509       MDNode *N = GCov->getOperand(i);
510       bool ThreeElement = N->getNumOperands() == 3;
511       if (!ThreeElement && N->getNumOperands() != 2)
512         continue;
513       if (dyn_cast<MDNode>(N->getOperand(ThreeElement ? 2 : 1)) != CU)
514         continue;
515 
516       if (ThreeElement) {
517         // These nodes have no mangling to apply, it's stored mangled in the
518         // bitcode.
519         MDString *NotesFile = dyn_cast<MDString>(N->getOperand(0));
520         MDString *DataFile = dyn_cast<MDString>(N->getOperand(1));
521         if (!NotesFile || !DataFile)
522           continue;
523         return std::string(Notes ? NotesFile->getString()
524                                  : DataFile->getString());
525       }
526 
527       MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
528       if (!GCovFile)
529         continue;
530 
531       SmallString<128> Filename = GCovFile->getString();
532       sys::path::replace_extension(Filename, Notes ? "gcno" : "gcda");
533       return std::string(Filename.str());
534     }
535   }
536 
537   SmallString<128> Filename = CU->getFilename();
538   sys::path::replace_extension(Filename, Notes ? "gcno" : "gcda");
539   StringRef FName = sys::path::filename(Filename);
540   SmallString<128> CurPath;
541   if (sys::fs::current_path(CurPath))
542     return std::string(FName);
543   sys::path::append(CurPath, FName);
544   return std::string(CurPath.str());
545 }
546 
547 bool GCOVProfiler::runOnModule(
548     Module &M, std::function<const TargetLibraryInfo &(Function &F)> GetTLI) {
549   this->M = &M;
550   this->GetTLI = std::move(GetTLI);
551   Ctx = &M.getContext();
552 
553   bool Modified = AddFlushBeforeForkAndExec();
554 
555   FilterRe = createRegexesFromString(Options.Filter);
556   ExcludeRe = createRegexesFromString(Options.Exclude);
557 
558   if (Options.EmitNotes) emitProfileNotes();
559   if (Options.EmitData)
560     Modified |= emitProfileArcs();
561   return Modified;
562 }
563 
564 PreservedAnalyses GCOVProfilerPass::run(Module &M,
565                                         ModuleAnalysisManager &AM) {
566 
567   GCOVProfiler Profiler(GCOVOpts);
568   FunctionAnalysisManager &FAM =
569       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
570 
571   if (!Profiler.runOnModule(M, [&](Function &F) -> TargetLibraryInfo & {
572         return FAM.getResult<TargetLibraryAnalysis>(F);
573       }))
574     return PreservedAnalyses::all();
575 
576   return PreservedAnalyses::none();
577 }
578 
579 static bool functionHasLines(const Function &F, unsigned &EndLine) {
580   // Check whether this function actually has any source lines. Not only
581   // do these waste space, they also can crash gcov.
582   EndLine = 0;
583   for (auto &BB : F) {
584     for (auto &I : BB) {
585       // Debug intrinsic locations correspond to the location of the
586       // declaration, not necessarily any statements or expressions.
587       if (isa<DbgInfoIntrinsic>(&I)) continue;
588 
589       const DebugLoc &Loc = I.getDebugLoc();
590       if (!Loc)
591         continue;
592 
593       // Artificial lines such as calls to the global constructors.
594       if (Loc.getLine() == 0) continue;
595       EndLine = std::max(EndLine, Loc.getLine());
596 
597       return true;
598     }
599   }
600   return false;
601 }
602 
603 static bool isUsingScopeBasedEH(Function &F) {
604   if (!F.hasPersonalityFn()) return false;
605 
606   EHPersonality Personality = classifyEHPersonality(F.getPersonalityFn());
607   return isScopedEHPersonality(Personality);
608 }
609 
610 bool GCOVProfiler::AddFlushBeforeForkAndExec() {
611   SmallVector<CallInst *, 2> Forks;
612   SmallVector<CallInst *, 2> Execs;
613   for (auto &F : M->functions()) {
614     auto *TLI = &GetTLI(F);
615     for (auto &I : instructions(F)) {
616       if (CallInst *CI = dyn_cast<CallInst>(&I)) {
617         if (Function *Callee = CI->getCalledFunction()) {
618           LibFunc LF;
619           if (TLI->getLibFunc(*Callee, LF)) {
620             if (LF == LibFunc_fork) {
621 #if !defined(_WIN32)
622               Forks.push_back(CI);
623 #endif
624             } else if (LF == LibFunc_execl || LF == LibFunc_execle ||
625                        LF == LibFunc_execlp || LF == LibFunc_execv ||
626                        LF == LibFunc_execvp || LF == LibFunc_execve ||
627                        LF == LibFunc_execvpe || LF == LibFunc_execvP) {
628               Execs.push_back(CI);
629             }
630           }
631         }
632       }
633     }
634   }
635 
636   for (auto F : Forks) {
637     IRBuilder<> Builder(F);
638     BasicBlock *Parent = F->getParent();
639     auto NextInst = ++F->getIterator();
640 
641     // We've a fork so just reset the counters in the child process
642     FunctionType *FTy = FunctionType::get(Builder.getInt32Ty(), {}, false);
643     FunctionCallee GCOVFork = M->getOrInsertFunction("__gcov_fork", FTy);
644     F->setCalledFunction(GCOVFork);
645 
646     // We split just after the fork to have a counter for the lines after
647     // Anyway there's a bug:
648     // void foo() { fork(); }
649     // void bar() { foo(); blah(); }
650     // then "blah();" will be called 2 times but showed as 1
651     // because "blah()" belongs to the same block as "foo();"
652     Parent->splitBasicBlock(NextInst);
653 
654     // back() is a br instruction with a debug location
655     // equals to the one from NextAfterFork
656     // So to avoid to have two debug locs on two blocks just change it
657     DebugLoc Loc = F->getDebugLoc();
658     Parent->back().setDebugLoc(Loc);
659   }
660 
661   for (auto E : Execs) {
662     IRBuilder<> Builder(E);
663     BasicBlock *Parent = E->getParent();
664     auto NextInst = ++E->getIterator();
665 
666     // Since the process is replaced by a new one we need to write out gcdas
667     // No need to reset the counters since they'll be lost after the exec**
668     FunctionType *FTy = FunctionType::get(Builder.getVoidTy(), {}, false);
669     FunctionCallee WriteoutF =
670         M->getOrInsertFunction("llvm_writeout_files", FTy);
671     Builder.CreateCall(WriteoutF);
672 
673     DebugLoc Loc = E->getDebugLoc();
674     Builder.SetInsertPoint(&*NextInst);
675     // If the exec** fails we must reset the counters since they've been
676     // dumped
677     FunctionCallee ResetF = M->getOrInsertFunction("llvm_reset_counters", FTy);
678     Builder.CreateCall(ResetF)->setDebugLoc(Loc);
679     Parent->splitBasicBlock(NextInst);
680     Parent->back().setDebugLoc(Loc);
681   }
682 
683   return !Forks.empty() || !Execs.empty();
684 }
685 
686 void GCOVProfiler::emitProfileNotes() {
687   NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
688   if (!CU_Nodes) return;
689 
690   int Version;
691   {
692     uint8_t c3 = Options.Version[0];
693     uint8_t c2 = Options.Version[1];
694     uint8_t c1 = Options.Version[2];
695     Version = c3 >= 'A' ? (c3 - 'A') * 100 + (c2 - '0') * 10 + c1 - '0'
696                         : (c3 - '0') * 10 + c1 - '0';
697   }
698 
699   for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
700     // Each compile unit gets its own .gcno file. This means that whether we run
701     // this pass over the original .o's as they're produced, or run it after
702     // LTO, we'll generate the same .gcno files.
703 
704     auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i));
705 
706     // Skip module skeleton (and module) CUs.
707     if (CU->getDWOId())
708       continue;
709 
710     std::error_code EC;
711     raw_fd_ostream out(mangleName(CU, GCovFileType::GCNO), EC,
712                        sys::fs::OF_None);
713     if (EC) {
714       Ctx->emitError(Twine("failed to open coverage notes file for writing: ") +
715                      EC.message());
716       continue;
717     }
718 
719     std::vector<uint8_t> EdgeDestinations;
720 
721     Endian = M->getDataLayout().isLittleEndian() ? support::endianness::little
722                                                  : support::endianness::big;
723     unsigned FunctionIdent = 0;
724     for (auto &F : M->functions()) {
725       DISubprogram *SP = F.getSubprogram();
726       unsigned EndLine;
727       if (!SP) continue;
728       if (!functionHasLines(F, EndLine) || !isFunctionInstrumented(F))
729         continue;
730       // TODO: Functions using scope-based EH are currently not supported.
731       if (isUsingScopeBasedEH(F)) continue;
732 
733       // gcov expects every function to start with an entry block that has a
734       // single successor, so split the entry block to make sure of that.
735       BasicBlock &EntryBlock = F.getEntryBlock();
736 
737       Funcs.push_back(std::make_unique<GCOVFunction>(this, &F, SP, EndLine,
738                                                      FunctionIdent++, Version));
739       GCOVFunction &Func = *Funcs.back();
740 
741       // Add the function line number to the lines of the entry block
742       // to have a counter for the function definition.
743       uint32_t Line = SP->getLine();
744       auto Filename = getFilename(SP);
745 
746       // Artificial functions such as global initializers
747       if (!SP->isArtificial())
748         Func.getBlock(&EntryBlock).getFile(Filename).addLine(Line);
749 
750       Func.getEntryBlock().addEdge(Func.getBlock(&EntryBlock));
751       for (auto &BB : F) {
752         GCOVBlock &Block = Func.getBlock(&BB);
753         Instruction *TI = BB.getTerminator();
754         if (int successors = TI->getNumSuccessors()) {
755           for (int i = 0; i != successors; ++i) {
756             Block.addEdge(Func.getBlock(TI->getSuccessor(i)));
757           }
758         } else if (isa<ReturnInst>(TI)) {
759           Block.addEdge(Func.getReturnBlock());
760         }
761         for (GCOVBlock *Succ : Block.OutEdges) {
762           uint32_t Idx = Succ->Number;
763           do EdgeDestinations.push_back(Idx & 255);
764           while ((Idx >>= 8) > 0);
765         }
766 
767         for (auto &I : BB) {
768           // Debug intrinsic locations correspond to the location of the
769           // declaration, not necessarily any statements or expressions.
770           if (isa<DbgInfoIntrinsic>(&I)) continue;
771 
772           const DebugLoc &Loc = I.getDebugLoc();
773           if (!Loc)
774             continue;
775 
776           // Artificial lines such as calls to the global constructors.
777           if (Loc.getLine() == 0 || Loc.isImplicitCode())
778             continue;
779 
780           if (Line == Loc.getLine()) continue;
781           Line = Loc.getLine();
782           if (SP != getDISubprogram(Loc.getScope()))
783             continue;
784 
785           GCOVLines &Lines = Block.getFile(Filename);
786           Lines.addLine(Loc.getLine());
787         }
788         Line = 0;
789       }
790     }
791 
792     char Tmp[4];
793     JamCRC JC;
794     JC.update(EdgeDestinations);
795     os = &out;
796     uint32_t Stamp = JC.getCRC();
797     FileChecksums.push_back(Stamp);
798     if (Endian == support::endianness::big) {
799       out.write("gcno", 4);
800       out.write(Options.Version, 4);
801     } else {
802       out.write("oncg", 4);
803       std::reverse_copy(Options.Version, Options.Version + 4, Tmp);
804       out.write(Tmp, 4);
805     }
806     write(Stamp);
807     if (Version >= 90)
808       writeString(""); // unuseful current_working_directory
809     if (Version >= 80)
810       write(0); // unuseful has_unexecuted_blocks
811 
812     for (auto &Func : Funcs)
813       Func->writeOut(Stamp);
814 
815     write(0);
816     write(0);
817     out.close();
818   }
819 }
820 
821 bool GCOVProfiler::emitProfileArcs() {
822   NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
823   if (!CU_Nodes) return false;
824 
825   bool Result = false;
826   for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
827     SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
828     for (auto &F : M->functions()) {
829       DISubprogram *SP = F.getSubprogram();
830       unsigned EndLine;
831       if (!SP) continue;
832       if (!functionHasLines(F, EndLine) || !isFunctionInstrumented(F))
833         continue;
834       // TODO: Functions using scope-based EH are currently not supported.
835       if (isUsingScopeBasedEH(F)) continue;
836 
837       DenseMap<std::pair<BasicBlock *, BasicBlock *>, unsigned> EdgeToCounter;
838       unsigned Edges = 0;
839       EdgeToCounter[{nullptr, &F.getEntryBlock()}] = Edges++;
840       for (auto &BB : F) {
841         Instruction *TI = BB.getTerminator();
842         if (isa<ReturnInst>(TI)) {
843           EdgeToCounter[{&BB, nullptr}] = Edges++;
844         } else {
845           for (BasicBlock *Succ : successors(TI)) {
846             EdgeToCounter[{&BB, Succ}] = Edges++;
847           }
848         }
849       }
850 
851       ArrayType *CounterTy =
852         ArrayType::get(Type::getInt64Ty(*Ctx), Edges);
853       GlobalVariable *Counters =
854         new GlobalVariable(*M, CounterTy, false,
855                            GlobalValue::InternalLinkage,
856                            Constant::getNullValue(CounterTy),
857                            "__llvm_gcov_ctr");
858       CountersBySP.push_back(std::make_pair(Counters, SP));
859 
860       // If a BB has several predecessors, use a PHINode to select
861       // the correct counter.
862       for (auto &BB : F) {
863         // The phi node must be at the begin of the BB.
864         IRBuilder<> BuilderForPhi(&*BB.begin());
865         IRBuilder<> Builder(&*BB.getFirstInsertionPt());
866         Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
867         Value *V;
868         if (&BB == &F.getEntryBlock()) {
869           auto It = EdgeToCounter.find({nullptr, &BB});
870           V = Builder.CreateConstInBoundsGEP2_64(Counters->getValueType(),
871                                                  Counters, 0, It->second);
872         } else {
873           const unsigned EdgeCount =
874               std::distance(pred_begin(&BB), pred_end(&BB));
875           if (EdgeCount == 0)
876             continue;
877           PHINode *Phi = BuilderForPhi.CreatePHI(Int64PtrTy, EdgeCount);
878           for (BasicBlock *Pred : predecessors(&BB)) {
879             auto It = EdgeToCounter.find({Pred, &BB});
880             assert(It != EdgeToCounter.end());
881             const unsigned Edge = It->second;
882             Value *EdgeCounter = BuilderForPhi.CreateConstInBoundsGEP2_64(
883                 Counters->getValueType(), Counters, 0, Edge);
884             Phi->addIncoming(EdgeCounter, Pred);
885             V = Phi;
886           }
887         }
888 
889         if (Options.Atomic) {
890           Builder.CreateAtomicRMW(AtomicRMWInst::Add, V, Builder.getInt64(1),
891                                   AtomicOrdering::Monotonic);
892         } else {
893           Value *Count =
894               Builder.CreateLoad(Builder.getInt64Ty(), V, "gcov_ctr");
895           Count = Builder.CreateAdd(Count, Builder.getInt64(1));
896           Builder.CreateStore(Count, V);
897         }
898 
899         Instruction *TI = BB.getTerminator();
900         if (isa<ReturnInst>(TI)) {
901           auto It = EdgeToCounter.find({&BB, nullptr});
902           assert(It != EdgeToCounter.end());
903           const unsigned Edge = It->second;
904           Value *Counter = Builder.CreateConstInBoundsGEP2_64(
905               Counters->getValueType(), Counters, 0, Edge);
906           if (Options.Atomic) {
907             Builder.CreateAtomicRMW(AtomicRMWInst::Add, Counter,
908                                     Builder.getInt64(1),
909                                     AtomicOrdering::Monotonic);
910           } else {
911             Value *Count = Builder.CreateLoad(Builder.getInt64Ty(), Counter);
912             Count = Builder.CreateAdd(Count, Builder.getInt64(1));
913             Builder.CreateStore(Count, Counter);
914           }
915         }
916       }
917     }
918 
919     Function *WriteoutF = insertCounterWriteout(CountersBySP);
920     Function *ResetF = insertReset(CountersBySP);
921 
922     // Create a small bit of code that registers the "__llvm_gcov_writeout" to
923     // be executed at exit and the "__llvm_gcov_flush" function to be executed
924     // when "__gcov_flush" is called.
925     FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
926     Function *F = Function::Create(FTy, GlobalValue::InternalLinkage,
927                                    "__llvm_gcov_init", M);
928     F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
929     F->setLinkage(GlobalValue::InternalLinkage);
930     F->addFnAttr(Attribute::NoInline);
931     if (Options.NoRedZone)
932       F->addFnAttr(Attribute::NoRedZone);
933 
934     BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
935     IRBuilder<> Builder(BB);
936 
937     FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
938     auto *PFTy = PointerType::get(FTy, 0);
939     FTy = FunctionType::get(Builder.getVoidTy(), {PFTy, PFTy}, false);
940 
941     // Initialize the environment and register the local writeout, flush and
942     // reset functions.
943     FunctionCallee GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
944     Builder.CreateCall(GCOVInit, {WriteoutF, ResetF});
945     Builder.CreateRetVoid();
946 
947     appendToGlobalCtors(*M, F, 0);
948     Result = true;
949   }
950 
951   return Result;
952 }
953 
954 FunctionCallee GCOVProfiler::getStartFileFunc(const TargetLibraryInfo *TLI) {
955   Type *Args[] = {
956       Type::getInt8PtrTy(*Ctx), // const char *orig_filename
957       Type::getInt32Ty(*Ctx),   // uint32_t version
958       Type::getInt32Ty(*Ctx),   // uint32_t checksum
959   };
960   FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
961   AttributeList AL;
962   if (auto AK = TLI->getExtAttrForI32Param(false))
963     AL = AL.addParamAttribute(*Ctx, 2, AK);
964   FunctionCallee Res = M->getOrInsertFunction("llvm_gcda_start_file", FTy, AL);
965   return Res;
966 }
967 
968 FunctionCallee GCOVProfiler::getEmitFunctionFunc(const TargetLibraryInfo *TLI) {
969   Type *Args[] = {
970     Type::getInt32Ty(*Ctx),    // uint32_t ident
971     Type::getInt32Ty(*Ctx),    // uint32_t func_checksum
972     Type::getInt32Ty(*Ctx),    // uint32_t cfg_checksum
973   };
974   FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
975   AttributeList AL;
976   if (auto AK = TLI->getExtAttrForI32Param(false)) {
977     AL = AL.addParamAttribute(*Ctx, 0, AK);
978     AL = AL.addParamAttribute(*Ctx, 1, AK);
979     AL = AL.addParamAttribute(*Ctx, 2, AK);
980   }
981   return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
982 }
983 
984 FunctionCallee GCOVProfiler::getEmitArcsFunc(const TargetLibraryInfo *TLI) {
985   Type *Args[] = {
986     Type::getInt32Ty(*Ctx),     // uint32_t num_counters
987     Type::getInt64PtrTy(*Ctx),  // uint64_t *counters
988   };
989   FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
990   AttributeList AL;
991   if (auto AK = TLI->getExtAttrForI32Param(false))
992     AL = AL.addParamAttribute(*Ctx, 0, AK);
993   return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy, AL);
994 }
995 
996 FunctionCallee GCOVProfiler::getSummaryInfoFunc() {
997   FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
998   return M->getOrInsertFunction("llvm_gcda_summary_info", FTy);
999 }
1000 
1001 FunctionCallee GCOVProfiler::getEndFileFunc() {
1002   FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
1003   return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
1004 }
1005 
1006 Function *GCOVProfiler::insertCounterWriteout(
1007     ArrayRef<std::pair<GlobalVariable *, MDNode *> > CountersBySP) {
1008   FunctionType *WriteoutFTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
1009   Function *WriteoutF = M->getFunction("__llvm_gcov_writeout");
1010   if (!WriteoutF)
1011     WriteoutF = Function::Create(WriteoutFTy, GlobalValue::InternalLinkage,
1012                                  "__llvm_gcov_writeout", M);
1013   WriteoutF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1014   WriteoutF->addFnAttr(Attribute::NoInline);
1015   if (Options.NoRedZone)
1016     WriteoutF->addFnAttr(Attribute::NoRedZone);
1017 
1018   BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
1019   IRBuilder<> Builder(BB);
1020 
1021   auto *TLI = &GetTLI(*WriteoutF);
1022 
1023   FunctionCallee StartFile = getStartFileFunc(TLI);
1024   FunctionCallee EmitFunction = getEmitFunctionFunc(TLI);
1025   FunctionCallee EmitArcs = getEmitArcsFunc(TLI);
1026   FunctionCallee SummaryInfo = getSummaryInfoFunc();
1027   FunctionCallee EndFile = getEndFileFunc();
1028 
1029   NamedMDNode *CUNodes = M->getNamedMetadata("llvm.dbg.cu");
1030   if (!CUNodes) {
1031     Builder.CreateRetVoid();
1032     return WriteoutF;
1033   }
1034 
1035   // Collect the relevant data into a large constant data structure that we can
1036   // walk to write out everything.
1037   StructType *StartFileCallArgsTy = StructType::create(
1038       {Builder.getInt8PtrTy(), Builder.getInt32Ty(), Builder.getInt32Ty()});
1039   StructType *EmitFunctionCallArgsTy = StructType::create(
1040       {Builder.getInt32Ty(), Builder.getInt32Ty(), Builder.getInt32Ty()});
1041   StructType *EmitArcsCallArgsTy = StructType::create(
1042       {Builder.getInt32Ty(), Builder.getInt64Ty()->getPointerTo()});
1043   StructType *FileInfoTy =
1044       StructType::create({StartFileCallArgsTy, Builder.getInt32Ty(),
1045                           EmitFunctionCallArgsTy->getPointerTo(),
1046                           EmitArcsCallArgsTy->getPointerTo()});
1047 
1048   Constant *Zero32 = Builder.getInt32(0);
1049   // Build an explicit array of two zeros for use in ConstantExpr GEP building.
1050   Constant *TwoZero32s[] = {Zero32, Zero32};
1051 
1052   SmallVector<Constant *, 8> FileInfos;
1053   for (int i : llvm::seq<int>(0, CUNodes->getNumOperands())) {
1054     auto *CU = cast<DICompileUnit>(CUNodes->getOperand(i));
1055 
1056     // Skip module skeleton (and module) CUs.
1057     if (CU->getDWOId())
1058       continue;
1059 
1060     std::string FilenameGcda = mangleName(CU, GCovFileType::GCDA);
1061     uint32_t CfgChecksum = FileChecksums.empty() ? 0 : FileChecksums[i];
1062     auto *StartFileCallArgs = ConstantStruct::get(
1063         StartFileCallArgsTy,
1064         {Builder.CreateGlobalStringPtr(FilenameGcda),
1065          Builder.getInt32(endian::read32be(Options.Version)),
1066          Builder.getInt32(CfgChecksum)});
1067 
1068     SmallVector<Constant *, 8> EmitFunctionCallArgsArray;
1069     SmallVector<Constant *, 8> EmitArcsCallArgsArray;
1070     for (int j : llvm::seq<int>(0, CountersBySP.size())) {
1071       uint32_t FuncChecksum = Funcs.empty() ? 0 : Funcs[j]->getFuncChecksum();
1072       EmitFunctionCallArgsArray.push_back(ConstantStruct::get(
1073           EmitFunctionCallArgsTy,
1074           {Builder.getInt32(j),
1075            Builder.getInt32(FuncChecksum),
1076            Builder.getInt32(CfgChecksum)}));
1077 
1078       GlobalVariable *GV = CountersBySP[j].first;
1079       unsigned Arcs = cast<ArrayType>(GV->getValueType())->getNumElements();
1080       EmitArcsCallArgsArray.push_back(ConstantStruct::get(
1081           EmitArcsCallArgsTy,
1082           {Builder.getInt32(Arcs), ConstantExpr::getInBoundsGetElementPtr(
1083                                        GV->getValueType(), GV, TwoZero32s)}));
1084     }
1085     // Create global arrays for the two emit calls.
1086     int CountersSize = CountersBySP.size();
1087     assert(CountersSize == (int)EmitFunctionCallArgsArray.size() &&
1088            "Mismatched array size!");
1089     assert(CountersSize == (int)EmitArcsCallArgsArray.size() &&
1090            "Mismatched array size!");
1091     auto *EmitFunctionCallArgsArrayTy =
1092         ArrayType::get(EmitFunctionCallArgsTy, CountersSize);
1093     auto *EmitFunctionCallArgsArrayGV = new GlobalVariable(
1094         *M, EmitFunctionCallArgsArrayTy, /*isConstant*/ true,
1095         GlobalValue::InternalLinkage,
1096         ConstantArray::get(EmitFunctionCallArgsArrayTy,
1097                            EmitFunctionCallArgsArray),
1098         Twine("__llvm_internal_gcov_emit_function_args.") + Twine(i));
1099     auto *EmitArcsCallArgsArrayTy =
1100         ArrayType::get(EmitArcsCallArgsTy, CountersSize);
1101     EmitFunctionCallArgsArrayGV->setUnnamedAddr(
1102         GlobalValue::UnnamedAddr::Global);
1103     auto *EmitArcsCallArgsArrayGV = new GlobalVariable(
1104         *M, EmitArcsCallArgsArrayTy, /*isConstant*/ true,
1105         GlobalValue::InternalLinkage,
1106         ConstantArray::get(EmitArcsCallArgsArrayTy, EmitArcsCallArgsArray),
1107         Twine("__llvm_internal_gcov_emit_arcs_args.") + Twine(i));
1108     EmitArcsCallArgsArrayGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1109 
1110     FileInfos.push_back(ConstantStruct::get(
1111         FileInfoTy,
1112         {StartFileCallArgs, Builder.getInt32(CountersSize),
1113          ConstantExpr::getInBoundsGetElementPtr(EmitFunctionCallArgsArrayTy,
1114                                                 EmitFunctionCallArgsArrayGV,
1115                                                 TwoZero32s),
1116          ConstantExpr::getInBoundsGetElementPtr(
1117              EmitArcsCallArgsArrayTy, EmitArcsCallArgsArrayGV, TwoZero32s)}));
1118   }
1119 
1120   // If we didn't find anything to actually emit, bail on out.
1121   if (FileInfos.empty()) {
1122     Builder.CreateRetVoid();
1123     return WriteoutF;
1124   }
1125 
1126   // To simplify code, we cap the number of file infos we write out to fit
1127   // easily in a 32-bit signed integer. This gives consistent behavior between
1128   // 32-bit and 64-bit systems without requiring (potentially very slow) 64-bit
1129   // operations on 32-bit systems. It also seems unreasonable to try to handle
1130   // more than 2 billion files.
1131   if ((int64_t)FileInfos.size() > (int64_t)INT_MAX)
1132     FileInfos.resize(INT_MAX);
1133 
1134   // Create a global for the entire data structure so we can walk it more
1135   // easily.
1136   auto *FileInfoArrayTy = ArrayType::get(FileInfoTy, FileInfos.size());
1137   auto *FileInfoArrayGV = new GlobalVariable(
1138       *M, FileInfoArrayTy, /*isConstant*/ true, GlobalValue::InternalLinkage,
1139       ConstantArray::get(FileInfoArrayTy, FileInfos),
1140       "__llvm_internal_gcov_emit_file_info");
1141   FileInfoArrayGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1142 
1143   // Create the CFG for walking this data structure.
1144   auto *FileLoopHeader =
1145       BasicBlock::Create(*Ctx, "file.loop.header", WriteoutF);
1146   auto *CounterLoopHeader =
1147       BasicBlock::Create(*Ctx, "counter.loop.header", WriteoutF);
1148   auto *FileLoopLatch = BasicBlock::Create(*Ctx, "file.loop.latch", WriteoutF);
1149   auto *ExitBB = BasicBlock::Create(*Ctx, "exit", WriteoutF);
1150 
1151   // We always have at least one file, so just branch to the header.
1152   Builder.CreateBr(FileLoopHeader);
1153 
1154   // The index into the files structure is our loop induction variable.
1155   Builder.SetInsertPoint(FileLoopHeader);
1156   PHINode *IV =
1157       Builder.CreatePHI(Builder.getInt32Ty(), /*NumReservedValues*/ 2);
1158   IV->addIncoming(Builder.getInt32(0), BB);
1159   auto *FileInfoPtr = Builder.CreateInBoundsGEP(
1160       FileInfoArrayTy, FileInfoArrayGV, {Builder.getInt32(0), IV});
1161   auto *StartFileCallArgsPtr =
1162       Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 0);
1163   auto *StartFileCall = Builder.CreateCall(
1164       StartFile,
1165       {Builder.CreateLoad(StartFileCallArgsTy->getElementType(0),
1166                           Builder.CreateStructGEP(StartFileCallArgsTy,
1167                                                   StartFileCallArgsPtr, 0)),
1168        Builder.CreateLoad(StartFileCallArgsTy->getElementType(1),
1169                           Builder.CreateStructGEP(StartFileCallArgsTy,
1170                                                   StartFileCallArgsPtr, 1)),
1171        Builder.CreateLoad(StartFileCallArgsTy->getElementType(2),
1172                           Builder.CreateStructGEP(StartFileCallArgsTy,
1173                                                   StartFileCallArgsPtr, 2))});
1174   if (auto AK = TLI->getExtAttrForI32Param(false))
1175     StartFileCall->addParamAttr(2, AK);
1176   auto *NumCounters =
1177       Builder.CreateLoad(FileInfoTy->getElementType(1),
1178                          Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 1));
1179   auto *EmitFunctionCallArgsArray =
1180       Builder.CreateLoad(FileInfoTy->getElementType(2),
1181                          Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 2));
1182   auto *EmitArcsCallArgsArray =
1183       Builder.CreateLoad(FileInfoTy->getElementType(3),
1184                          Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 3));
1185   auto *EnterCounterLoopCond =
1186       Builder.CreateICmpSLT(Builder.getInt32(0), NumCounters);
1187   Builder.CreateCondBr(EnterCounterLoopCond, CounterLoopHeader, FileLoopLatch);
1188 
1189   Builder.SetInsertPoint(CounterLoopHeader);
1190   auto *JV = Builder.CreatePHI(Builder.getInt32Ty(), /*NumReservedValues*/ 2);
1191   JV->addIncoming(Builder.getInt32(0), FileLoopHeader);
1192   auto *EmitFunctionCallArgsPtr = Builder.CreateInBoundsGEP(
1193       EmitFunctionCallArgsTy, EmitFunctionCallArgsArray, JV);
1194   auto *EmitFunctionCall = Builder.CreateCall(
1195       EmitFunction,
1196       {Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(0),
1197                           Builder.CreateStructGEP(EmitFunctionCallArgsTy,
1198                                                   EmitFunctionCallArgsPtr, 0)),
1199        Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(1),
1200                           Builder.CreateStructGEP(EmitFunctionCallArgsTy,
1201                                                   EmitFunctionCallArgsPtr, 1)),
1202        Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(2),
1203                           Builder.CreateStructGEP(EmitFunctionCallArgsTy,
1204                                                   EmitFunctionCallArgsPtr,
1205                                                   2))});
1206   if (auto AK = TLI->getExtAttrForI32Param(false)) {
1207     EmitFunctionCall->addParamAttr(0, AK);
1208     EmitFunctionCall->addParamAttr(1, AK);
1209     EmitFunctionCall->addParamAttr(2, AK);
1210   }
1211   auto *EmitArcsCallArgsPtr =
1212       Builder.CreateInBoundsGEP(EmitArcsCallArgsTy, EmitArcsCallArgsArray, JV);
1213   auto *EmitArcsCall = Builder.CreateCall(
1214       EmitArcs,
1215       {Builder.CreateLoad(
1216            EmitArcsCallArgsTy->getElementType(0),
1217            Builder.CreateStructGEP(EmitArcsCallArgsTy, EmitArcsCallArgsPtr, 0)),
1218        Builder.CreateLoad(EmitArcsCallArgsTy->getElementType(1),
1219                           Builder.CreateStructGEP(EmitArcsCallArgsTy,
1220                                                   EmitArcsCallArgsPtr, 1))});
1221   if (auto AK = TLI->getExtAttrForI32Param(false))
1222     EmitArcsCall->addParamAttr(0, AK);
1223   auto *NextJV = Builder.CreateAdd(JV, Builder.getInt32(1));
1224   auto *CounterLoopCond = Builder.CreateICmpSLT(NextJV, NumCounters);
1225   Builder.CreateCondBr(CounterLoopCond, CounterLoopHeader, FileLoopLatch);
1226   JV->addIncoming(NextJV, CounterLoopHeader);
1227 
1228   Builder.SetInsertPoint(FileLoopLatch);
1229   Builder.CreateCall(SummaryInfo, {});
1230   Builder.CreateCall(EndFile, {});
1231   auto *NextIV = Builder.CreateAdd(IV, Builder.getInt32(1));
1232   auto *FileLoopCond =
1233       Builder.CreateICmpSLT(NextIV, Builder.getInt32(FileInfos.size()));
1234   Builder.CreateCondBr(FileLoopCond, FileLoopHeader, ExitBB);
1235   IV->addIncoming(NextIV, FileLoopLatch);
1236 
1237   Builder.SetInsertPoint(ExitBB);
1238   Builder.CreateRetVoid();
1239 
1240   return WriteoutF;
1241 }
1242 
1243 Function *GCOVProfiler::insertReset(
1244     ArrayRef<std::pair<GlobalVariable *, MDNode *>> CountersBySP) {
1245   FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
1246   Function *ResetF = M->getFunction("__llvm_gcov_reset");
1247   if (!ResetF)
1248     ResetF = Function::Create(FTy, GlobalValue::InternalLinkage,
1249                               "__llvm_gcov_reset", M);
1250   ResetF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1251   ResetF->addFnAttr(Attribute::NoInline);
1252   if (Options.NoRedZone)
1253     ResetF->addFnAttr(Attribute::NoRedZone);
1254 
1255   BasicBlock *Entry = BasicBlock::Create(*Ctx, "entry", ResetF);
1256   IRBuilder<> Builder(Entry);
1257 
1258   // Zero out the counters.
1259   for (const auto &I : CountersBySP) {
1260     GlobalVariable *GV = I.first;
1261     Constant *Null = Constant::getNullValue(GV->getValueType());
1262     Builder.CreateStore(Null, GV);
1263   }
1264 
1265   Type *RetTy = ResetF->getReturnType();
1266   if (RetTy->isVoidTy())
1267     Builder.CreateRetVoid();
1268   else if (RetTy->isIntegerTy())
1269     // Used if __llvm_gcov_reset was implicitly declared.
1270     Builder.CreateRet(ConstantInt::get(RetTy, 0));
1271   else
1272     report_fatal_error("invalid return type for __llvm_gcov_reset");
1273 
1274   return ResetF;
1275 }
1276