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