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