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 #define DEBUG_TYPE "insert-gcov-profiling"
19 #include "llvm/Transforms/Instrumentation.h"
20 #include "ProfilingUtils.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/ADT/UniqueVector.h"
27 #include "llvm/DebugInfo.h"
28 #include "llvm/IR/IRBuilder.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/DebugLoc.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/InstIterator.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Transforms/Utils/ModuleUtils.h"
45 static cl::opt<std::string>
46 DefaultGCOVVersion("default-gcov-version", cl::init("402*"), cl::Hidden,
49 GCOVOptions GCOVOptions::getDefault() {
51 Options.EmitNotes = true;
52 Options.EmitData = true;
53 Options.UseCfgChecksum = false;
54 Options.NoRedZone = false;
55 Options.FunctionNamesInData = true;
57 if (DefaultGCOVVersion.size() != 4) {
58 llvm::report_fatal_error(std::string("Invalid -default-gcov-version: ") +
61 memcpy(Options.Version, DefaultGCOVVersion.c_str(), 4);
66 class GCOVProfiler : public ModulePass {
69 GCOVProfiler() : ModulePass(ID), Options(GCOVOptions::getDefault()) {
70 ReversedVersion[0] = Options.Version[3];
71 ReversedVersion[1] = Options.Version[2];
72 ReversedVersion[2] = Options.Version[1];
73 ReversedVersion[3] = Options.Version[0];
74 ReversedVersion[4] = '\0';
75 initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
77 GCOVProfiler(const GCOVOptions &Options) : ModulePass(ID), Options(Options){
78 assert((Options.EmitNotes || Options.EmitData) &&
79 "GCOVProfiler asked to do nothing?");
80 ReversedVersion[0] = Options.Version[3];
81 ReversedVersion[1] = Options.Version[2];
82 ReversedVersion[2] = Options.Version[1];
83 ReversedVersion[3] = Options.Version[0];
84 ReversedVersion[4] = '\0';
85 initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
87 virtual const char *getPassName() const {
88 return "GCOV Profiler";
92 bool runOnModule(Module &M);
94 // Create the .gcno files for the Module based on DebugInfo.
95 void emitProfileNotes();
97 // Modify the program to track transitions along edges and call into the
98 // profiling runtime to emit .gcda files when run.
99 bool emitProfileArcs();
101 // Get pointers to the functions in the runtime library.
102 Constant *getStartFileFunc();
103 Constant *getIncrementIndirectCounterFunc();
104 Constant *getEmitFunctionFunc();
105 Constant *getEmitArcsFunc();
106 Constant *getDeleteWriteoutFunctionListFunc();
107 Constant *getDeleteFlushFunctionListFunc();
108 Constant *getEndFileFunc();
110 // Create or retrieve an i32 state value that is used to represent the
111 // pred block number for certain non-trivial edges.
112 GlobalVariable *getEdgeStateValue();
114 // Produce a table of pointers to counters, by predecessor and successor
116 GlobalVariable *buildEdgeLookupTable(Function *F,
117 GlobalVariable *Counter,
118 const UniqueVector<BasicBlock *>&Preds,
119 const UniqueVector<BasicBlock*>&Succs);
121 // Add the function to write out all our counters to the global destructor
123 Function *insertCounterWriteout(ArrayRef<std::pair<GlobalVariable*,
125 Function *insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> >);
126 void insertIndirectCounterIncrement();
128 std::string mangleName(DICompileUnit CU, const char *NewStem);
132 // Reversed, NUL-terminated copy of Options.Version.
133 char ReversedVersion[5];
140 char GCOVProfiler::ID = 0;
141 INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling",
142 "Insert instrumentation for GCOV profiling", false, false)
144 ModulePass *llvm::createGCOVProfilerPass(const GCOVOptions &Options) {
145 return new GCOVProfiler(Options);
148 static std::string getFunctionName(DISubprogram SP) {
149 if (!SP.getLinkageName().empty())
150 return SP.getLinkageName();
157 static const char *LinesTag;
158 static const char *FunctionTag;
159 static const char *BlockTag;
160 static const char *EdgeTag;
164 void writeBytes(const char *Bytes, int Size) {
165 os->write(Bytes, Size);
168 void write(uint32_t i) {
169 writeBytes(reinterpret_cast<char*>(&i), 4);
172 // Returns the length measured in 4-byte blocks that will be used to
173 // represent this string in a GCOV file
174 unsigned lengthOfGCOVString(StringRef s) {
175 // A GCOV string is a length, followed by a NUL, then between 0 and 3 NULs
176 // padding out to the next 4-byte word. The length is measured in 4-byte
177 // words including padding, not bytes of actual string.
178 return (s.size() / 4) + 1;
181 void writeGCOVString(StringRef s) {
182 uint32_t Len = lengthOfGCOVString(s);
184 writeBytes(s.data(), s.size());
186 // Write 1 to 4 bytes of NUL padding.
187 assert((unsigned)(4 - (s.size() % 4)) > 0);
188 assert((unsigned)(4 - (s.size() % 4)) <= 4);
189 writeBytes("\0\0\0\0", 4 - (s.size() % 4));
194 const char *GCOVRecord::LinesTag = "\0\0\x45\x01";
195 const char *GCOVRecord::FunctionTag = "\0\0\0\1";
196 const char *GCOVRecord::BlockTag = "\0\0\x41\x01";
197 const char *GCOVRecord::EdgeTag = "\0\0\x43\x01";
202 // Constructed only by requesting it from a GCOVBlock, this object stores a
203 // list of line numbers and a single filename, representing lines that belong
205 class GCOVLines : public GCOVRecord {
207 void addLine(uint32_t Line) {
208 Lines.push_back(Line);
212 // Here 2 = 1 for string length + 1 for '0' id#.
213 return lengthOfGCOVString(Filename) + 2 + Lines.size();
218 writeGCOVString(Filename);
219 for (int i = 0, e = Lines.size(); i != e; ++i)
223 GCOVLines(StringRef F, raw_ostream *os)
230 SmallVector<uint32_t, 32> Lines;
234 // Sorting function for deterministic behaviour in GCOVBlock::writeOut.
235 struct StringKeySort {
236 bool operator()(StringMapEntry<GCOVLines *> *LHS,
237 StringMapEntry<GCOVLines *> *RHS) const {
238 return LHS->getKey() < RHS->getKey();
242 // Represent a basic block in GCOV. Each block has a unique number in the
243 // function, number of lines belonging to each block, and a set of edges to
245 class GCOVBlock : public GCOVRecord {
247 GCOVLines &getFile(StringRef Filename) {
248 GCOVLines *&Lines = LinesByFile[Filename];
250 Lines = new GCOVLines(Filename, os);
255 void addEdge(GCOVBlock &Successor) {
256 OutEdges.push_back(&Successor);
261 SmallVector<StringMapEntry<GCOVLines *> *, 32> SortedLinesByFile;
262 for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
263 E = LinesByFile.end(); I != E; ++I) {
264 Len += I->second->length();
265 SortedLinesByFile.push_back(&*I);
268 writeBytes(LinesTag, 4);
272 StringKeySort Sorter;
273 std::sort(SortedLinesByFile.begin(), SortedLinesByFile.end(), Sorter);
274 for (SmallVectorImpl<StringMapEntry<GCOVLines *> *>::iterator
275 I = SortedLinesByFile.begin(), E = SortedLinesByFile.end();
277 (*I)->getValue()->writeOut();
283 DeleteContainerSeconds(LinesByFile);
287 friend class GCOVFunction;
289 GCOVBlock(uint32_t Number, raw_ostream *os)
295 StringMap<GCOVLines *> LinesByFile;
296 SmallVector<GCOVBlock *, 4> OutEdges;
299 // A function has a unique identifier, a checksum (we leave as zero) and a
300 // set of blocks and a map of edges between blocks. This is the only GCOV
301 // object users can construct, the blocks and lines will be rooted here.
302 class GCOVFunction : public GCOVRecord {
304 GCOVFunction(DISubprogram SP, raw_ostream *os, uint32_t Ident,
305 bool UseCfgChecksum) {
308 Function *F = SP.getFunction();
309 DEBUG(dbgs() << "Function: " << F->getName() << "\n");
311 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
312 Blocks[BB] = new GCOVBlock(i++, os);
314 ReturnBlock = new GCOVBlock(i++, os);
316 writeBytes(FunctionTag, 4);
317 uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(getFunctionName(SP)) +
318 1 + lengthOfGCOVString(SP.getFilename()) + 1;
323 write(0); // lineno checksum
325 write(0); // cfg checksum
326 writeGCOVString(getFunctionName(SP));
327 writeGCOVString(SP.getFilename());
328 write(SP.getLineNumber());
332 DeleteContainerSeconds(Blocks);
336 GCOVBlock &getBlock(BasicBlock *BB) {
340 GCOVBlock &getReturnBlock() {
345 // Emit count of blocks.
346 writeBytes(BlockTag, 4);
347 write(Blocks.size() + 1);
348 for (int i = 0, e = Blocks.size() + 1; i != e; ++i) {
349 write(0); // No flags on our blocks.
351 DEBUG(dbgs() << Blocks.size() << " blocks.\n");
353 // Emit edges between blocks.
354 if (Blocks.empty()) return;
355 Function *F = Blocks.begin()->first->getParent();
356 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
357 GCOVBlock &Block = *Blocks[I];
358 if (Block.OutEdges.empty()) continue;
360 writeBytes(EdgeTag, 4);
361 write(Block.OutEdges.size() * 2 + 1);
363 for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) {
364 DEBUG(dbgs() << Block.Number << " -> " << Block.OutEdges[i]->Number
366 write(Block.OutEdges[i]->Number);
367 write(0); // no flags
371 // Emit lines for each block.
372 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
373 Blocks[I]->writeOut();
378 DenseMap<BasicBlock *, GCOVBlock *> Blocks;
379 GCOVBlock *ReturnBlock;
383 std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem) {
384 if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
385 for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
386 MDNode *N = GCov->getOperand(i);
387 if (N->getNumOperands() != 2) continue;
388 MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
389 MDNode *CompileUnit = dyn_cast<MDNode>(N->getOperand(1));
390 if (!GCovFile || !CompileUnit) continue;
391 if (CompileUnit == CU) {
392 SmallString<128> Filename = GCovFile->getString();
393 sys::path::replace_extension(Filename, NewStem);
394 return Filename.str();
399 SmallString<128> Filename = CU.getFilename();
400 sys::path::replace_extension(Filename, NewStem);
401 StringRef FName = sys::path::filename(Filename);
402 SmallString<128> CurPath;
403 if (sys::fs::current_path(CurPath)) return FName;
404 sys::path::append(CurPath, FName.str());
405 return CurPath.str();
408 bool GCOVProfiler::runOnModule(Module &M) {
410 Ctx = &M.getContext();
412 if (Options.EmitNotes) emitProfileNotes();
413 if (Options.EmitData) return emitProfileArcs();
417 void GCOVProfiler::emitProfileNotes() {
418 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
419 if (!CU_Nodes) return;
421 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
422 // Each compile unit gets its own .gcno file. This means that whether we run
423 // this pass over the original .o's as they're produced, or run it after
424 // LTO, we'll generate the same .gcno files.
426 DICompileUnit CU(CU_Nodes->getOperand(i));
427 std::string ErrorInfo;
428 raw_fd_ostream out(mangleName(CU, "gcno").c_str(), ErrorInfo,
429 raw_fd_ostream::F_Binary);
430 out.write("oncg", 4);
431 out.write(ReversedVersion, 4);
432 out.write("MVLL", 4);
434 DIArray SPs = CU.getSubprograms();
435 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
436 DISubprogram SP(SPs.getElement(i));
437 assert((!SP || SP.isSubprogram()) &&
438 "A MDNode in subprograms of a CU should be null or a DISubprogram.");
442 Function *F = SP.getFunction();
444 GCOVFunction Func(SP, &out, i, Options.UseCfgChecksum);
446 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
447 GCOVBlock &Block = Func.getBlock(BB);
448 TerminatorInst *TI = BB->getTerminator();
449 if (int successors = TI->getNumSuccessors()) {
450 for (int i = 0; i != successors; ++i) {
451 Block.addEdge(Func.getBlock(TI->getSuccessor(i)));
453 } else if (isa<ReturnInst>(TI)) {
454 Block.addEdge(Func.getReturnBlock());
458 for (BasicBlock::iterator I = BB->begin(), IE = BB->end();
460 const DebugLoc &Loc = I->getDebugLoc();
461 if (Loc.isUnknown()) continue;
462 if (Line == Loc.getLine()) continue;
463 Line = Loc.getLine();
464 if (SP != getDISubprogram(Loc.getScope(*Ctx))) continue;
466 GCOVLines &Lines = Block.getFile(SP.getFilename());
467 Lines.addLine(Loc.getLine());
472 out.write("\0\0\0\0\0\0\0\0", 8); // EOF
477 bool GCOVProfiler::emitProfileArcs() {
478 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
479 if (!CU_Nodes) return false;
482 bool InsertIndCounterIncrCode = false;
483 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
484 DICompileUnit CU(CU_Nodes->getOperand(i));
485 DIArray SPs = CU.getSubprograms();
486 SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
487 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
488 DISubprogram SP(SPs.getElement(i));
489 assert((!SP || SP.isSubprogram()) &&
490 "A MDNode in subprograms of a CU should be null or a DISubprogram.");
493 Function *F = SP.getFunction();
495 if (!Result) Result = true;
497 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
498 TerminatorInst *TI = BB->getTerminator();
499 if (isa<ReturnInst>(TI))
502 Edges += TI->getNumSuccessors();
505 ArrayType *CounterTy =
506 ArrayType::get(Type::getInt64Ty(*Ctx), Edges);
507 GlobalVariable *Counters =
508 new GlobalVariable(*M, CounterTy, false,
509 GlobalValue::InternalLinkage,
510 Constant::getNullValue(CounterTy),
512 CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP));
514 UniqueVector<BasicBlock *> ComplexEdgePreds;
515 UniqueVector<BasicBlock *> ComplexEdgeSuccs;
518 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
519 TerminatorInst *TI = BB->getTerminator();
520 int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
522 IRBuilder<> Builder(TI);
524 if (Successors == 1) {
525 Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
527 Value *Count = Builder.CreateLoad(Counter);
528 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
529 Builder.CreateStore(Count, Counter);
530 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
531 Value *Sel = Builder.CreateSelect(BI->getCondition(),
532 Builder.getInt64(Edge),
533 Builder.getInt64(Edge + 1));
534 SmallVector<Value *, 2> Idx;
535 Idx.push_back(Builder.getInt64(0));
537 Value *Counter = Builder.CreateInBoundsGEP(Counters, Idx);
538 Value *Count = Builder.CreateLoad(Counter);
539 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
540 Builder.CreateStore(Count, Counter);
542 ComplexEdgePreds.insert(BB);
543 for (int i = 0; i != Successors; ++i)
544 ComplexEdgeSuccs.insert(TI->getSuccessor(i));
550 if (!ComplexEdgePreds.empty()) {
551 GlobalVariable *EdgeTable =
552 buildEdgeLookupTable(F, Counters,
553 ComplexEdgePreds, ComplexEdgeSuccs);
554 GlobalVariable *EdgeState = getEdgeStateValue();
556 for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) {
557 IRBuilder<> Builder(ComplexEdgePreds[i+1]->getTerminator());
558 Builder.CreateStore(Builder.getInt32(i), EdgeState);
560 for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) {
561 // call runtime to perform increment
562 BasicBlock::iterator InsertPt =
563 ComplexEdgeSuccs[i+1]->getFirstInsertionPt();
564 IRBuilder<> Builder(InsertPt);
565 Value *CounterPtrArray =
566 Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0,
567 i * ComplexEdgePreds.size());
569 // Build code to increment the counter.
570 InsertIndCounterIncrCode = true;
571 Builder.CreateCall2(getIncrementIndirectCounterFunc(),
572 EdgeState, CounterPtrArray);
577 Function *WriteoutF = insertCounterWriteout(CountersBySP);
578 Function *FlushF = insertFlush(CountersBySP);
580 // Create a small bit of code that registers the "__llvm_gcov_writeout" to
581 // be executed at exit and the "__llvm_gcov_flush" function to be executed
582 // when "__gcov_flush" is called.
583 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
584 Function *F = Function::Create(FTy, GlobalValue::InternalLinkage,
585 "__llvm_gcov_init", M);
586 F->setUnnamedAddr(true);
587 F->setLinkage(GlobalValue::InternalLinkage);
588 F->addFnAttr(Attribute::NoInline);
589 if (Options.NoRedZone)
590 F->addFnAttr(Attribute::NoRedZone);
592 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
593 IRBuilder<> Builder(BB);
595 FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
597 PointerType::get(FTy, 0),
598 PointerType::get(FTy, 0)
600 FTy = FunctionType::get(Builder.getVoidTy(), Params, false);
602 // Inialize the environment and register the local writeout and flush
604 Constant *GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
605 Builder.CreateCall2(GCOVInit, WriteoutF, FlushF);
606 Builder.CreateRetVoid();
608 appendToGlobalCtors(*M, F, 0);
611 if (InsertIndCounterIncrCode)
612 insertIndirectCounterIncrement();
617 // All edges with successors that aren't branches are "complex", because it
618 // requires complex logic to pick which counter to update.
619 GlobalVariable *GCOVProfiler::buildEdgeLookupTable(
621 GlobalVariable *Counters,
622 const UniqueVector<BasicBlock *> &Preds,
623 const UniqueVector<BasicBlock *> &Succs) {
624 // TODO: support invoke, threads. We rely on the fact that nothing can modify
625 // the whole-Module pred edge# between the time we set it and the time we next
626 // read it. Threads and invoke make this untrue.
628 // emit [(succs * preds) x i64*], logically [succ x [pred x i64*]].
629 size_t TableSize = Succs.size() * Preds.size();
630 Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
631 ArrayType *EdgeTableTy = ArrayType::get(Int64PtrTy, TableSize);
633 OwningArrayPtr<Constant *> EdgeTable(new Constant*[TableSize]);
634 Constant *NullValue = Constant::getNullValue(Int64PtrTy);
635 for (size_t i = 0; i != TableSize; ++i)
636 EdgeTable[i] = NullValue;
639 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
640 TerminatorInst *TI = BB->getTerminator();
641 int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
642 if (Successors > 1 && !isa<BranchInst>(TI) && !isa<ReturnInst>(TI)) {
643 for (int i = 0; i != Successors; ++i) {
644 BasicBlock *Succ = TI->getSuccessor(i);
645 IRBuilder<> Builder(Succ);
646 Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
648 EdgeTable[((Succs.idFor(Succ)-1) * Preds.size()) +
649 (Preds.idFor(BB)-1)] = cast<Constant>(Counter);
655 ArrayRef<Constant*> V(&EdgeTable[0], TableSize);
656 GlobalVariable *EdgeTableGV =
658 *M, EdgeTableTy, true, GlobalValue::InternalLinkage,
659 ConstantArray::get(EdgeTableTy, V),
660 "__llvm_gcda_edge_table");
661 EdgeTableGV->setUnnamedAddr(true);
665 Constant *GCOVProfiler::getStartFileFunc() {
667 Type::getInt8PtrTy(*Ctx), // const char *orig_filename
668 Type::getInt8PtrTy(*Ctx), // const char version[4]
670 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
671 return M->getOrInsertFunction("llvm_gcda_start_file", FTy);
674 Constant *GCOVProfiler::getIncrementIndirectCounterFunc() {
675 Type *Int32Ty = Type::getInt32Ty(*Ctx);
676 Type *Int64Ty = Type::getInt64Ty(*Ctx);
678 Int32Ty->getPointerTo(), // uint32_t *predecessor
679 Int64Ty->getPointerTo()->getPointerTo() // uint64_t **counters
681 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
682 return M->getOrInsertFunction("__llvm_gcov_indirect_counter_increment", FTy);
685 Constant *GCOVProfiler::getEmitFunctionFunc() {
687 Type::getInt32Ty(*Ctx), // uint32_t ident
688 Type::getInt8PtrTy(*Ctx), // const char *function_name
689 Type::getInt8Ty(*Ctx), // uint8_t use_extra_checksum
691 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
692 return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
695 Constant *GCOVProfiler::getEmitArcsFunc() {
697 Type::getInt32Ty(*Ctx), // uint32_t num_counters
698 Type::getInt64PtrTy(*Ctx), // uint64_t *counters
700 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
701 return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy);
704 Constant *GCOVProfiler::getDeleteWriteoutFunctionListFunc() {
705 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
706 return M->getOrInsertFunction("llvm_delete_writeout_function_list", FTy);
709 Constant *GCOVProfiler::getDeleteFlushFunctionListFunc() {
710 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
711 return M->getOrInsertFunction("llvm_delete_flush_function_list", FTy);
714 Constant *GCOVProfiler::getEndFileFunc() {
715 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
716 return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
719 GlobalVariable *GCOVProfiler::getEdgeStateValue() {
720 GlobalVariable *GV = M->getGlobalVariable("__llvm_gcov_global_state_pred");
722 GV = new GlobalVariable(*M, Type::getInt32Ty(*Ctx), false,
723 GlobalValue::InternalLinkage,
724 ConstantInt::get(Type::getInt32Ty(*Ctx),
726 "__llvm_gcov_global_state_pred");
727 GV->setUnnamedAddr(true);
732 Function *GCOVProfiler::insertCounterWriteout(
733 ArrayRef<std::pair<GlobalVariable *, MDNode *> > CountersBySP) {
734 FunctionType *WriteoutFTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
735 Function *WriteoutF = M->getFunction("__llvm_gcov_writeout");
737 WriteoutF = Function::Create(WriteoutFTy, GlobalValue::InternalLinkage,
738 "__llvm_gcov_writeout", M);
739 WriteoutF->setUnnamedAddr(true);
740 WriteoutF->addFnAttr(Attribute::NoInline);
741 if (Options.NoRedZone)
742 WriteoutF->addFnAttr(Attribute::NoRedZone);
744 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
745 IRBuilder<> Builder(BB);
747 Constant *StartFile = getStartFileFunc();
748 Constant *EmitFunction = getEmitFunctionFunc();
749 Constant *EmitArcs = getEmitArcsFunc();
750 Constant *EndFile = getEndFileFunc();
752 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
754 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
755 DICompileUnit CU(CU_Nodes->getOperand(i));
756 std::string FilenameGcda = mangleName(CU, "gcda");
757 Builder.CreateCall2(StartFile,
758 Builder.CreateGlobalStringPtr(FilenameGcda),
759 Builder.CreateGlobalStringPtr(ReversedVersion));
760 for (unsigned j = 0, e = CountersBySP.size(); j != e; ++j) {
761 DISubprogram SP(CountersBySP[j].second);
763 EmitFunction, Builder.getInt32(j),
764 Options.FunctionNamesInData ?
765 Builder.CreateGlobalStringPtr(getFunctionName(SP)) :
766 Constant::getNullValue(Builder.getInt8PtrTy()),
767 Builder.getInt8(Options.UseCfgChecksum));
769 GlobalVariable *GV = CountersBySP[j].first;
771 cast<ArrayType>(GV->getType()->getElementType())->getNumElements();
772 Builder.CreateCall2(EmitArcs,
773 Builder.getInt32(Arcs),
774 Builder.CreateConstGEP2_64(GV, 0, 0));
776 Builder.CreateCall(EndFile);
780 Builder.CreateRetVoid();
784 void GCOVProfiler::insertIndirectCounterIncrement() {
786 cast<Function>(GCOVProfiler::getIncrementIndirectCounterFunc());
787 Fn->setUnnamedAddr(true);
788 Fn->setLinkage(GlobalValue::InternalLinkage);
789 Fn->addFnAttr(Attribute::NoInline);
790 if (Options.NoRedZone)
791 Fn->addFnAttr(Attribute::NoRedZone);
793 // Create basic blocks for function.
794 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", Fn);
795 IRBuilder<> Builder(BB);
797 BasicBlock *PredNotNegOne = BasicBlock::Create(*Ctx, "", Fn);
798 BasicBlock *CounterEnd = BasicBlock::Create(*Ctx, "", Fn);
799 BasicBlock *Exit = BasicBlock::Create(*Ctx, "exit", Fn);
801 // uint32_t pred = *predecessor;
802 // if (pred == 0xffffffff) return;
803 Argument *Arg = Fn->arg_begin();
804 Arg->setName("predecessor");
805 Value *Pred = Builder.CreateLoad(Arg, "pred");
806 Value *Cond = Builder.CreateICmpEQ(Pred, Builder.getInt32(0xffffffff));
807 BranchInst::Create(Exit, PredNotNegOne, Cond, BB);
809 Builder.SetInsertPoint(PredNotNegOne);
811 // uint64_t *counter = counters[pred];
812 // if (!counter) return;
813 Value *ZExtPred = Builder.CreateZExt(Pred, Builder.getInt64Ty());
814 Arg = llvm::next(Fn->arg_begin());
815 Arg->setName("counters");
816 Value *GEP = Builder.CreateGEP(Arg, ZExtPred);
817 Value *Counter = Builder.CreateLoad(GEP, "counter");
818 Cond = Builder.CreateICmpEQ(Counter,
819 Constant::getNullValue(
820 Builder.getInt64Ty()->getPointerTo()));
821 Builder.CreateCondBr(Cond, Exit, CounterEnd);
824 Builder.SetInsertPoint(CounterEnd);
825 Value *Add = Builder.CreateAdd(Builder.CreateLoad(Counter),
826 Builder.getInt64(1));
827 Builder.CreateStore(Add, Counter);
828 Builder.CreateBr(Exit);
830 // Fill in the exit block.
831 Builder.SetInsertPoint(Exit);
832 Builder.CreateRetVoid();
835 Function *GCOVProfiler::
836 insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> > CountersBySP) {
837 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
838 Function *FlushF = M->getFunction("__llvm_gcov_flush");
840 FlushF = Function::Create(FTy, GlobalValue::InternalLinkage,
841 "__llvm_gcov_flush", M);
843 FlushF->setLinkage(GlobalValue::InternalLinkage);
844 FlushF->setUnnamedAddr(true);
845 FlushF->addFnAttr(Attribute::NoInline);
846 if (Options.NoRedZone)
847 FlushF->addFnAttr(Attribute::NoRedZone);
849 BasicBlock *Entry = BasicBlock::Create(*Ctx, "entry", FlushF);
851 // Write out the current counters.
852 Constant *WriteoutF = M->getFunction("__llvm_gcov_writeout");
853 assert(WriteoutF && "Need to create the writeout function first!");
855 IRBuilder<> Builder(Entry);
856 Builder.CreateCall(WriteoutF);
858 // Zero out the counters.
859 for (ArrayRef<std::pair<GlobalVariable *, MDNode *> >::iterator
860 I = CountersBySP.begin(), E = CountersBySP.end();
862 GlobalVariable *GV = I->first;
863 Constant *Null = Constant::getNullValue(GV->getType()->getElementType());
864 Builder.CreateStore(Null, GV);
867 Type *RetTy = FlushF->getReturnType();
868 if (RetTy == Type::getVoidTy(*Ctx))
869 Builder.CreateRetVoid();
870 else if (RetTy->isIntegerTy())
871 // Used if __llvm_gcov_flush was implicitly declared.
872 Builder.CreateRet(ConstantInt::get(RetTy, 0));
874 report_fatal_error("invalid return type for __llvm_gcov_flush");