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