1 //===- GCOVProfiling.cpp - Insert edge counters for gcov profiling --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Instrumentation.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Hashing.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/UniqueVector.h"
25 #include "llvm/IR/DebugInfo.h"
26 #include "llvm/IR/DebugLoc.h"
27 #include "llvm/IR/IRBuilder.h"
28 #include "llvm/IR/InstIterator.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/IntrinsicInst.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/Pass.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/Path.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Transforms/Utils/ModuleUtils.h"
45 #define DEBUG_TYPE "insert-gcov-profiling"
47 static cl::opt<std::string>
48 DefaultGCOVVersion("default-gcov-version", cl::init("402*"), cl::Hidden,
51 GCOVOptions GCOVOptions::getDefault() {
53 Options.EmitNotes = true;
54 Options.EmitData = true;
55 Options.UseCfgChecksum = false;
56 Options.NoRedZone = false;
57 Options.FunctionNamesInData = true;
59 if (DefaultGCOVVersion.size() != 4) {
60 llvm::report_fatal_error(std::string("Invalid -default-gcov-version: ") +
63 memcpy(Options.Version, DefaultGCOVVersion.c_str(), 4);
70 class GCOVProfiler : public ModulePass {
73 GCOVProfiler() : ModulePass(ID), Options(GCOVOptions::getDefault()) {
76 GCOVProfiler(const GCOVOptions &Options) : ModulePass(ID), Options(Options){
77 assert((Options.EmitNotes || Options.EmitData) &&
78 "GCOVProfiler asked to do nothing?");
81 const char *getPassName() const override {
82 return "GCOV Profiler";
87 ReversedVersion[0] = Options.Version[3];
88 ReversedVersion[1] = Options.Version[2];
89 ReversedVersion[2] = Options.Version[1];
90 ReversedVersion[3] = Options.Version[0];
91 ReversedVersion[4] = '\0';
92 initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
94 bool runOnModule(Module &M) override;
96 // Create the .gcno files for the Module based on DebugInfo.
97 void emitProfileNotes();
99 // Modify the program to track transitions along edges and call into the
100 // profiling runtime to emit .gcda files when run.
101 bool emitProfileArcs();
103 // Get pointers to the functions in the runtime library.
104 Constant *getStartFileFunc();
105 Constant *getIncrementIndirectCounterFunc();
106 Constant *getEmitFunctionFunc();
107 Constant *getEmitArcsFunc();
108 Constant *getSummaryInfoFunc();
109 Constant *getDeleteWriteoutFunctionListFunc();
110 Constant *getDeleteFlushFunctionListFunc();
111 Constant *getEndFileFunc();
113 // Create or retrieve an i32 state value that is used to represent the
114 // pred block number for certain non-trivial edges.
115 GlobalVariable *getEdgeStateValue();
117 // Produce a table of pointers to counters, by predecessor and successor
119 GlobalVariable *buildEdgeLookupTable(Function *F,
120 GlobalVariable *Counter,
121 const UniqueVector<BasicBlock *>&Preds,
122 const UniqueVector<BasicBlock*>&Succs);
124 // Add the function to write out all our counters to the global destructor
126 Function *insertCounterWriteout(ArrayRef<std::pair<GlobalVariable*,
128 Function *insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> >);
129 void insertIndirectCounterIncrement();
131 std::string mangleName(DICompileUnit CU, const char *NewStem);
135 // Reversed, NUL-terminated copy of Options.Version.
136 char ReversedVersion[5];
137 // Checksum, produced by hash of EdgeDestinations
138 SmallVector<uint32_t, 4> FileChecksums;
142 SmallVector<std::unique_ptr<GCOVFunction>, 16> Funcs;
146 char GCOVProfiler::ID = 0;
147 INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling",
148 "Insert instrumentation for GCOV profiling", false, false)
150 ModulePass *llvm::createGCOVProfilerPass(const GCOVOptions &Options) {
151 return new GCOVProfiler(Options);
154 static StringRef getFunctionName(DISubprogram SP) {
155 if (!SP.getLinkageName().empty())
156 return SP.getLinkageName();
163 static const char *const LinesTag;
164 static const char *const FunctionTag;
165 static const char *const BlockTag;
166 static const char *const EdgeTag;
170 void writeBytes(const char *Bytes, int Size) {
171 os->write(Bytes, Size);
174 void write(uint32_t i) {
175 writeBytes(reinterpret_cast<char*>(&i), 4);
178 // Returns the length measured in 4-byte blocks that will be used to
179 // represent this string in a GCOV file
180 static unsigned lengthOfGCOVString(StringRef s) {
181 // A GCOV string is a length, followed by a NUL, then between 0 and 3 NULs
182 // padding out to the next 4-byte word. The length is measured in 4-byte
183 // words including padding, not bytes of actual string.
184 return (s.size() / 4) + 1;
187 void writeGCOVString(StringRef s) {
188 uint32_t Len = lengthOfGCOVString(s);
190 writeBytes(s.data(), s.size());
192 // Write 1 to 4 bytes of NUL padding.
193 assert((unsigned)(4 - (s.size() % 4)) > 0);
194 assert((unsigned)(4 - (s.size() % 4)) <= 4);
195 writeBytes("\0\0\0\0", 4 - (s.size() % 4));
200 const char *const GCOVRecord::LinesTag = "\0\0\x45\x01";
201 const char *const GCOVRecord::FunctionTag = "\0\0\0\1";
202 const char *const GCOVRecord::BlockTag = "\0\0\x41\x01";
203 const char *const GCOVRecord::EdgeTag = "\0\0\x43\x01";
208 // Constructed only by requesting it from a GCOVBlock, this object stores a
209 // list of line numbers and a single filename, representing lines that belong
211 class GCOVLines : public GCOVRecord {
213 void addLine(uint32_t Line) {
214 Lines.push_back(Line);
217 uint32_t length() const {
218 // Here 2 = 1 for string length + 1 for '0' id#.
219 return lengthOfGCOVString(Filename) + 2 + Lines.size();
224 writeGCOVString(Filename);
225 for (int i = 0, e = Lines.size(); i != e; ++i)
229 GCOVLines(StringRef F, raw_ostream *os)
236 SmallVector<uint32_t, 32> Lines;
240 // Represent a basic block in GCOV. Each block has a unique number in the
241 // function, number of lines belonging to each block, and a set of edges to
243 class GCOVBlock : public GCOVRecord {
245 GCOVLines &getFile(StringRef Filename) {
246 GCOVLines *&Lines = LinesByFile[Filename];
248 Lines = new GCOVLines(Filename, os);
253 void addEdge(GCOVBlock &Successor) {
254 OutEdges.push_back(&Successor);
259 SmallVector<StringMapEntry<GCOVLines *> *, 32> SortedLinesByFile;
260 for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
261 E = LinesByFile.end(); I != E; ++I) {
262 Len += I->second->length();
263 SortedLinesByFile.push_back(&*I);
266 writeBytes(LinesTag, 4);
270 std::sort(SortedLinesByFile.begin(), SortedLinesByFile.end(),
271 [](StringMapEntry<GCOVLines *> *LHS,
272 StringMapEntry<GCOVLines *> *RHS) {
273 return LHS->getKey() < RHS->getKey();
275 for (SmallVectorImpl<StringMapEntry<GCOVLines *> *>::iterator
276 I = SortedLinesByFile.begin(), E = SortedLinesByFile.end();
278 (*I)->getValue()->writeOut();
284 DeleteContainerSeconds(LinesByFile);
288 friend class GCOVFunction;
290 GCOVBlock(uint32_t Number, raw_ostream *os)
296 StringMap<GCOVLines *> LinesByFile;
297 SmallVector<GCOVBlock *, 4> OutEdges;
300 // A function has a unique identifier, a checksum (we leave as zero) and a
301 // set of blocks and a map of edges between blocks. This is the only GCOV
302 // object users can construct, the blocks and lines will be rooted here.
303 class GCOVFunction : public GCOVRecord {
305 GCOVFunction(DISubprogram SP, raw_ostream *os, uint32_t Ident,
306 bool UseCfgChecksum) :
307 SP(SP), Ident(Ident), UseCfgChecksum(UseCfgChecksum), CfgChecksum(0) {
310 Function *F = SP.getFunction();
311 DEBUG(dbgs() << "Function: " << getFunctionName(SP) << "\n");
313 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
314 Blocks[BB] = new GCOVBlock(i++, os);
316 ReturnBlock = new GCOVBlock(i++, os);
318 std::string FunctionNameAndLine;
319 raw_string_ostream FNLOS(FunctionNameAndLine);
320 FNLOS << getFunctionName(SP) << SP.getLineNumber();
322 FuncChecksum = hash_value(FunctionNameAndLine);
326 DeleteContainerSeconds(Blocks);
330 GCOVBlock &getBlock(BasicBlock *BB) {
334 GCOVBlock &getReturnBlock() {
338 std::string getEdgeDestinations() {
339 std::string EdgeDestinations;
340 raw_string_ostream EDOS(EdgeDestinations);
341 Function *F = Blocks.begin()->first->getParent();
342 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
343 GCOVBlock &Block = *Blocks[I];
344 for (int i = 0, e = Block.OutEdges.size(); i != e; ++i)
345 EDOS << Block.OutEdges[i]->Number;
347 return EdgeDestinations;
350 uint32_t getFuncChecksum() {
354 void setCfgChecksum(uint32_t Checksum) {
355 CfgChecksum = Checksum;
359 writeBytes(FunctionTag, 4);
360 uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(getFunctionName(SP)) +
361 1 + lengthOfGCOVString(SP.getFilename()) + 1;
369 writeGCOVString(getFunctionName(SP));
370 writeGCOVString(SP.getFilename());
371 write(SP.getLineNumber());
373 // Emit count of blocks.
374 writeBytes(BlockTag, 4);
375 write(Blocks.size() + 1);
376 for (int i = 0, e = Blocks.size() + 1; i != e; ++i) {
377 write(0); // No flags on our blocks.
379 DEBUG(dbgs() << Blocks.size() << " blocks.\n");
381 // Emit edges between blocks.
382 if (Blocks.empty()) return;
383 Function *F = Blocks.begin()->first->getParent();
384 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
385 GCOVBlock &Block = *Blocks[I];
386 if (Block.OutEdges.empty()) continue;
388 writeBytes(EdgeTag, 4);
389 write(Block.OutEdges.size() * 2 + 1);
391 for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) {
392 DEBUG(dbgs() << Block.Number << " -> " << Block.OutEdges[i]->Number
394 write(Block.OutEdges[i]->Number);
395 write(0); // no flags
399 // Emit lines for each block.
400 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
401 Blocks[I]->writeOut();
408 uint32_t FuncChecksum;
410 uint32_t CfgChecksum;
411 DenseMap<BasicBlock *, GCOVBlock *> Blocks;
412 GCOVBlock *ReturnBlock;
416 std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem) {
417 if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
418 for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
419 MDNode *N = GCov->getOperand(i);
420 if (N->getNumOperands() != 2) continue;
421 MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
422 MDNode *CompileUnit = dyn_cast<MDNode>(N->getOperand(1));
423 if (!GCovFile || !CompileUnit) continue;
424 if (CompileUnit == CU) {
425 SmallString<128> Filename = GCovFile->getString();
426 sys::path::replace_extension(Filename, NewStem);
427 return Filename.str();
432 SmallString<128> Filename = CU.getFilename();
433 sys::path::replace_extension(Filename, NewStem);
434 StringRef FName = sys::path::filename(Filename);
435 SmallString<128> CurPath;
436 if (sys::fs::current_path(CurPath)) return FName;
437 sys::path::append(CurPath, FName.str());
438 return CurPath.str();
441 bool GCOVProfiler::runOnModule(Module &M) {
443 Ctx = &M.getContext();
445 if (Options.EmitNotes) emitProfileNotes();
446 if (Options.EmitData) return emitProfileArcs();
450 static bool functionHasLines(Function *F) {
451 // Check whether this function actually has any source lines. Not only
452 // do these waste space, they also can crash gcov.
453 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
454 for (BasicBlock::iterator I = BB->begin(), IE = BB->end();
456 const DebugLoc &Loc = I->getDebugLoc();
457 if (Loc.isUnknown()) continue;
458 if (Loc.getLine() != 0)
465 void GCOVProfiler::emitProfileNotes() {
466 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
467 if (!CU_Nodes) return;
469 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
470 // Each compile unit gets its own .gcno file. This means that whether we run
471 // this pass over the original .o's as they're produced, or run it after
472 // LTO, we'll generate the same .gcno files.
474 DICompileUnit CU(CU_Nodes->getOperand(i));
475 std::string ErrorInfo;
476 raw_fd_ostream out(mangleName(CU, "gcno").c_str(), ErrorInfo,
478 std::string EdgeDestinations;
480 DIArray SPs = CU.getSubprograms();
481 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
482 DISubprogram SP(SPs.getElement(i));
483 assert((!SP || SP.isSubprogram()) &&
484 "A MDNode in subprograms of a CU should be null or a DISubprogram.");
488 Function *F = SP.getFunction();
490 if (!functionHasLines(F)) continue;
492 // gcov expects every function to start with an entry block that has a
493 // single successor, so split the entry block to make sure of that.
494 BasicBlock &EntryBlock = F->getEntryBlock();
495 BasicBlock::iterator It = EntryBlock.begin();
496 while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It))
498 EntryBlock.splitBasicBlock(It);
501 make_unique<GCOVFunction>(SP, &out, i, Options.UseCfgChecksum));
502 GCOVFunction &Func = *Funcs.back();
504 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
505 GCOVBlock &Block = Func.getBlock(BB);
506 TerminatorInst *TI = BB->getTerminator();
507 if (int successors = TI->getNumSuccessors()) {
508 for (int i = 0; i != successors; ++i) {
509 Block.addEdge(Func.getBlock(TI->getSuccessor(i)));
511 } else if (isa<ReturnInst>(TI)) {
512 Block.addEdge(Func.getReturnBlock());
516 for (BasicBlock::iterator I = BB->begin(), IE = BB->end();
518 const DebugLoc &Loc = I->getDebugLoc();
519 if (Loc.isUnknown()) continue;
520 if (Line == Loc.getLine()) continue;
521 Line = Loc.getLine();
522 if (SP != getDISubprogram(Loc.getScope(*Ctx))) continue;
524 GCOVLines &Lines = Block.getFile(SP.getFilename());
525 Lines.addLine(Loc.getLine());
528 EdgeDestinations += Func.getEdgeDestinations();
531 FileChecksums.push_back(hash_value(EdgeDestinations));
532 out.write("oncg", 4);
533 out.write(ReversedVersion, 4);
534 out.write(reinterpret_cast<char*>(&FileChecksums.back()), 4);
536 for (auto &Func : Funcs) {
537 Func->setCfgChecksum(FileChecksums.back());
541 out.write("\0\0\0\0\0\0\0\0", 8); // EOF
546 bool GCOVProfiler::emitProfileArcs() {
547 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
548 if (!CU_Nodes) return false;
551 bool InsertIndCounterIncrCode = false;
552 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
553 DICompileUnit CU(CU_Nodes->getOperand(i));
554 DIArray SPs = CU.getSubprograms();
555 SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
556 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
557 DISubprogram SP(SPs.getElement(i));
558 assert((!SP || SP.isSubprogram()) &&
559 "A MDNode in subprograms of a CU should be null or a DISubprogram.");
562 Function *F = SP.getFunction();
564 if (!functionHasLines(F)) continue;
565 if (!Result) Result = true;
567 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
568 TerminatorInst *TI = BB->getTerminator();
569 if (isa<ReturnInst>(TI))
572 Edges += TI->getNumSuccessors();
575 ArrayType *CounterTy =
576 ArrayType::get(Type::getInt64Ty(*Ctx), Edges);
577 GlobalVariable *Counters =
578 new GlobalVariable(*M, CounterTy, false,
579 GlobalValue::InternalLinkage,
580 Constant::getNullValue(CounterTy),
582 CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP));
584 UniqueVector<BasicBlock *> ComplexEdgePreds;
585 UniqueVector<BasicBlock *> ComplexEdgeSuccs;
588 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
589 TerminatorInst *TI = BB->getTerminator();
590 int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
592 if (Successors == 1) {
593 IRBuilder<> Builder(BB->getFirstInsertionPt());
594 Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
596 Value *Count = Builder.CreateLoad(Counter);
597 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
598 Builder.CreateStore(Count, Counter);
599 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
600 IRBuilder<> Builder(BI);
601 Value *Sel = Builder.CreateSelect(BI->getCondition(),
602 Builder.getInt64(Edge),
603 Builder.getInt64(Edge + 1));
604 SmallVector<Value *, 2> Idx;
605 Idx.push_back(Builder.getInt64(0));
607 Value *Counter = Builder.CreateInBoundsGEP(Counters, Idx);
608 Value *Count = Builder.CreateLoad(Counter);
609 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
610 Builder.CreateStore(Count, Counter);
612 ComplexEdgePreds.insert(BB);
613 for (int i = 0; i != Successors; ++i)
614 ComplexEdgeSuccs.insert(TI->getSuccessor(i));
621 if (!ComplexEdgePreds.empty()) {
622 GlobalVariable *EdgeTable =
623 buildEdgeLookupTable(F, Counters,
624 ComplexEdgePreds, ComplexEdgeSuccs);
625 GlobalVariable *EdgeState = getEdgeStateValue();
627 for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) {
628 IRBuilder<> Builder(ComplexEdgePreds[i + 1]->getFirstInsertionPt());
629 Builder.CreateStore(Builder.getInt32(i), EdgeState);
632 for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) {
633 // Call runtime to perform increment.
634 IRBuilder<> Builder(ComplexEdgeSuccs[i+1]->getFirstInsertionPt());
635 Value *CounterPtrArray =
636 Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0,
637 i * ComplexEdgePreds.size());
639 // Build code to increment the counter.
640 InsertIndCounterIncrCode = true;
641 Builder.CreateCall2(getIncrementIndirectCounterFunc(),
642 EdgeState, CounterPtrArray);
647 Function *WriteoutF = insertCounterWriteout(CountersBySP);
648 Function *FlushF = insertFlush(CountersBySP);
650 // Create a small bit of code that registers the "__llvm_gcov_writeout" to
651 // be executed at exit and the "__llvm_gcov_flush" function to be executed
652 // when "__gcov_flush" is called.
653 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
654 Function *F = Function::Create(FTy, GlobalValue::InternalLinkage,
655 "__llvm_gcov_init", M);
656 F->setUnnamedAddr(true);
657 F->setLinkage(GlobalValue::InternalLinkage);
658 F->addFnAttr(Attribute::NoInline);
659 if (Options.NoRedZone)
660 F->addFnAttr(Attribute::NoRedZone);
662 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
663 IRBuilder<> Builder(BB);
665 FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
667 PointerType::get(FTy, 0),
668 PointerType::get(FTy, 0)
670 FTy = FunctionType::get(Builder.getVoidTy(), Params, false);
672 // Initialize the environment and register the local writeout and flush
674 Constant *GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
675 Builder.CreateCall2(GCOVInit, WriteoutF, FlushF);
676 Builder.CreateRetVoid();
678 appendToGlobalCtors(*M, F, 0);
681 if (InsertIndCounterIncrCode)
682 insertIndirectCounterIncrement();
687 // All edges with successors that aren't branches are "complex", because it
688 // requires complex logic to pick which counter to update.
689 GlobalVariable *GCOVProfiler::buildEdgeLookupTable(
691 GlobalVariable *Counters,
692 const UniqueVector<BasicBlock *> &Preds,
693 const UniqueVector<BasicBlock *> &Succs) {
694 // TODO: support invoke, threads. We rely on the fact that nothing can modify
695 // the whole-Module pred edge# between the time we set it and the time we next
696 // read it. Threads and invoke make this untrue.
698 // emit [(succs * preds) x i64*], logically [succ x [pred x i64*]].
699 size_t TableSize = Succs.size() * Preds.size();
700 Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
701 ArrayType *EdgeTableTy = ArrayType::get(Int64PtrTy, TableSize);
703 std::unique_ptr<Constant * []> EdgeTable(new Constant *[TableSize]);
704 Constant *NullValue = Constant::getNullValue(Int64PtrTy);
705 for (size_t i = 0; i != TableSize; ++i)
706 EdgeTable[i] = NullValue;
709 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
710 TerminatorInst *TI = BB->getTerminator();
711 int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
712 if (Successors > 1 && !isa<BranchInst>(TI) && !isa<ReturnInst>(TI)) {
713 for (int i = 0; i != Successors; ++i) {
714 BasicBlock *Succ = TI->getSuccessor(i);
715 IRBuilder<> Builder(Succ);
716 Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
718 EdgeTable[((Succs.idFor(Succ)-1) * Preds.size()) +
719 (Preds.idFor(BB)-1)] = cast<Constant>(Counter);
725 ArrayRef<Constant*> V(&EdgeTable[0], TableSize);
726 GlobalVariable *EdgeTableGV =
728 *M, EdgeTableTy, true, GlobalValue::InternalLinkage,
729 ConstantArray::get(EdgeTableTy, V),
730 "__llvm_gcda_edge_table");
731 EdgeTableGV->setUnnamedAddr(true);
735 Constant *GCOVProfiler::getStartFileFunc() {
737 Type::getInt8PtrTy(*Ctx), // const char *orig_filename
738 Type::getInt8PtrTy(*Ctx), // const char version[4]
739 Type::getInt32Ty(*Ctx), // uint32_t checksum
741 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
742 return M->getOrInsertFunction("llvm_gcda_start_file", FTy);
745 Constant *GCOVProfiler::getIncrementIndirectCounterFunc() {
746 Type *Int32Ty = Type::getInt32Ty(*Ctx);
747 Type *Int64Ty = Type::getInt64Ty(*Ctx);
749 Int32Ty->getPointerTo(), // uint32_t *predecessor
750 Int64Ty->getPointerTo()->getPointerTo() // uint64_t **counters
752 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
753 return M->getOrInsertFunction("__llvm_gcov_indirect_counter_increment", FTy);
756 Constant *GCOVProfiler::getEmitFunctionFunc() {
758 Type::getInt32Ty(*Ctx), // uint32_t ident
759 Type::getInt8PtrTy(*Ctx), // const char *function_name
760 Type::getInt32Ty(*Ctx), // uint32_t func_checksum
761 Type::getInt8Ty(*Ctx), // uint8_t use_extra_checksum
762 Type::getInt32Ty(*Ctx), // uint32_t cfg_checksum
764 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
765 return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
768 Constant *GCOVProfiler::getEmitArcsFunc() {
770 Type::getInt32Ty(*Ctx), // uint32_t num_counters
771 Type::getInt64PtrTy(*Ctx), // uint64_t *counters
773 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
774 return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy);
777 Constant *GCOVProfiler::getSummaryInfoFunc() {
778 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
779 return M->getOrInsertFunction("llvm_gcda_summary_info", FTy);
782 Constant *GCOVProfiler::getDeleteWriteoutFunctionListFunc() {
783 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
784 return M->getOrInsertFunction("llvm_delete_writeout_function_list", FTy);
787 Constant *GCOVProfiler::getDeleteFlushFunctionListFunc() {
788 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
789 return M->getOrInsertFunction("llvm_delete_flush_function_list", FTy);
792 Constant *GCOVProfiler::getEndFileFunc() {
793 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
794 return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
797 GlobalVariable *GCOVProfiler::getEdgeStateValue() {
798 GlobalVariable *GV = M->getGlobalVariable("__llvm_gcov_global_state_pred");
800 GV = new GlobalVariable(*M, Type::getInt32Ty(*Ctx), false,
801 GlobalValue::InternalLinkage,
802 ConstantInt::get(Type::getInt32Ty(*Ctx),
804 "__llvm_gcov_global_state_pred");
805 GV->setUnnamedAddr(true);
810 Function *GCOVProfiler::insertCounterWriteout(
811 ArrayRef<std::pair<GlobalVariable *, MDNode *> > CountersBySP) {
812 FunctionType *WriteoutFTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
813 Function *WriteoutF = M->getFunction("__llvm_gcov_writeout");
815 WriteoutF = Function::Create(WriteoutFTy, GlobalValue::InternalLinkage,
816 "__llvm_gcov_writeout", M);
817 WriteoutF->setUnnamedAddr(true);
818 WriteoutF->addFnAttr(Attribute::NoInline);
819 if (Options.NoRedZone)
820 WriteoutF->addFnAttr(Attribute::NoRedZone);
822 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
823 IRBuilder<> Builder(BB);
825 Constant *StartFile = getStartFileFunc();
826 Constant *EmitFunction = getEmitFunctionFunc();
827 Constant *EmitArcs = getEmitArcsFunc();
828 Constant *SummaryInfo = getSummaryInfoFunc();
829 Constant *EndFile = getEndFileFunc();
831 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
833 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
834 DICompileUnit CU(CU_Nodes->getOperand(i));
835 std::string FilenameGcda = mangleName(CU, "gcda");
836 uint32_t CfgChecksum = FileChecksums.empty() ? 0 : FileChecksums[i];
837 Builder.CreateCall3(StartFile,
838 Builder.CreateGlobalStringPtr(FilenameGcda),
839 Builder.CreateGlobalStringPtr(ReversedVersion),
840 Builder.getInt32(CfgChecksum));
841 for (unsigned j = 0, e = CountersBySP.size(); j != e; ++j) {
842 DISubprogram SP(CountersBySP[j].second);
843 uint32_t FuncChecksum = Funcs.empty() ? 0 : Funcs[j]->getFuncChecksum();
845 EmitFunction, Builder.getInt32(j),
846 Options.FunctionNamesInData ?
847 Builder.CreateGlobalStringPtr(getFunctionName(SP)) :
848 Constant::getNullValue(Builder.getInt8PtrTy()),
849 Builder.getInt32(FuncChecksum),
850 Builder.getInt8(Options.UseCfgChecksum),
851 Builder.getInt32(CfgChecksum));
853 GlobalVariable *GV = CountersBySP[j].first;
855 cast<ArrayType>(GV->getType()->getElementType())->getNumElements();
856 Builder.CreateCall2(EmitArcs,
857 Builder.getInt32(Arcs),
858 Builder.CreateConstGEP2_64(GV, 0, 0));
860 Builder.CreateCall(SummaryInfo);
861 Builder.CreateCall(EndFile);
865 Builder.CreateRetVoid();
869 void GCOVProfiler::insertIndirectCounterIncrement() {
871 cast<Function>(GCOVProfiler::getIncrementIndirectCounterFunc());
872 Fn->setUnnamedAddr(true);
873 Fn->setLinkage(GlobalValue::InternalLinkage);
874 Fn->addFnAttr(Attribute::NoInline);
875 if (Options.NoRedZone)
876 Fn->addFnAttr(Attribute::NoRedZone);
878 // Create basic blocks for function.
879 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", Fn);
880 IRBuilder<> Builder(BB);
882 BasicBlock *PredNotNegOne = BasicBlock::Create(*Ctx, "", Fn);
883 BasicBlock *CounterEnd = BasicBlock::Create(*Ctx, "", Fn);
884 BasicBlock *Exit = BasicBlock::Create(*Ctx, "exit", Fn);
886 // uint32_t pred = *predecessor;
887 // if (pred == 0xffffffff) return;
888 Argument *Arg = Fn->arg_begin();
889 Arg->setName("predecessor");
890 Value *Pred = Builder.CreateLoad(Arg, "pred");
891 Value *Cond = Builder.CreateICmpEQ(Pred, Builder.getInt32(0xffffffff));
892 BranchInst::Create(Exit, PredNotNegOne, Cond, BB);
894 Builder.SetInsertPoint(PredNotNegOne);
896 // uint64_t *counter = counters[pred];
897 // if (!counter) return;
898 Value *ZExtPred = Builder.CreateZExt(Pred, Builder.getInt64Ty());
899 Arg = std::next(Fn->arg_begin());
900 Arg->setName("counters");
901 Value *GEP = Builder.CreateGEP(Arg, ZExtPred);
902 Value *Counter = Builder.CreateLoad(GEP, "counter");
903 Cond = Builder.CreateICmpEQ(Counter,
904 Constant::getNullValue(
905 Builder.getInt64Ty()->getPointerTo()));
906 Builder.CreateCondBr(Cond, Exit, CounterEnd);
909 Builder.SetInsertPoint(CounterEnd);
910 Value *Add = Builder.CreateAdd(Builder.CreateLoad(Counter),
911 Builder.getInt64(1));
912 Builder.CreateStore(Add, Counter);
913 Builder.CreateBr(Exit);
915 // Fill in the exit block.
916 Builder.SetInsertPoint(Exit);
917 Builder.CreateRetVoid();
920 Function *GCOVProfiler::
921 insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> > CountersBySP) {
922 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
923 Function *FlushF = M->getFunction("__llvm_gcov_flush");
925 FlushF = Function::Create(FTy, GlobalValue::InternalLinkage,
926 "__llvm_gcov_flush", M);
928 FlushF->setLinkage(GlobalValue::InternalLinkage);
929 FlushF->setUnnamedAddr(true);
930 FlushF->addFnAttr(Attribute::NoInline);
931 if (Options.NoRedZone)
932 FlushF->addFnAttr(Attribute::NoRedZone);
934 BasicBlock *Entry = BasicBlock::Create(*Ctx, "entry", FlushF);
936 // Write out the current counters.
937 Constant *WriteoutF = M->getFunction("__llvm_gcov_writeout");
938 assert(WriteoutF && "Need to create the writeout function first!");
940 IRBuilder<> Builder(Entry);
941 Builder.CreateCall(WriteoutF);
943 // Zero out the counters.
944 for (ArrayRef<std::pair<GlobalVariable *, MDNode *> >::iterator
945 I = CountersBySP.begin(), E = CountersBySP.end();
947 GlobalVariable *GV = I->first;
948 Constant *Null = Constant::getNullValue(GV->getType()->getElementType());
949 Builder.CreateStore(Null, GV);
952 Type *RetTy = FlushF->getReturnType();
953 if (RetTy == Type::getVoidTy(*Ctx))
954 Builder.CreateRetVoid();
955 else if (RetTy->isIntegerTy())
956 // Used if __llvm_gcov_flush was implicitly declared.
957 Builder.CreateRet(ConstantInt::get(RetTy, 0));
959 report_fatal_error("invalid return type for __llvm_gcov_flush");