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