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/InstIterator.h"
36 #include "llvm/Support/PathV2.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Transforms/Utils/ModuleUtils.h"
43 static cl::opt<std::string>
44 DefaultGCOVVersion("default-gcov-version", cl::init("402*"), cl::Hidden,
47 GCOVOptions GCOVOptions::getDefault() {
49 Options.EmitNotes = true;
50 Options.EmitData = true;
51 Options.UseCfgChecksum = false;
52 Options.NoRedZone = false;
53 Options.FunctionNamesInData = true;
55 if (DefaultGCOVVersion.size() != 4) {
56 llvm::report_fatal_error(std::string("Invalid -default-gcov-version: ") +
59 memcpy(Options.Version, DefaultGCOVVersion.c_str(), 4);
64 class GCOVProfiler : public ModulePass {
67 GCOVProfiler() : ModulePass(ID), Options(GCOVOptions::getDefault()) {
68 ReversedVersion[0] = Options.Version[3];
69 ReversedVersion[1] = Options.Version[2];
70 ReversedVersion[2] = Options.Version[1];
71 ReversedVersion[3] = Options.Version[0];
72 ReversedVersion[4] = '\0';
73 initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
75 GCOVProfiler(const GCOVOptions &Options) : ModulePass(ID), Options(Options){
76 assert((Options.EmitNotes || Options.EmitData) &&
77 "GCOVProfiler asked to do nothing?");
78 ReversedVersion[0] = Options.Version[3];
79 ReversedVersion[1] = Options.Version[2];
80 ReversedVersion[2] = Options.Version[1];
81 ReversedVersion[3] = Options.Version[0];
82 ReversedVersion[4] = '\0';
83 initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
85 virtual const char *getPassName() const {
86 return "GCOV Profiler";
90 bool runOnModule(Module &M);
92 // Create the .gcno files for the Module based on DebugInfo.
93 void emitProfileNotes();
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();
99 // Get pointers to the functions in the runtime library.
100 Constant *getStartFileFunc();
101 Constant *getIncrementIndirectCounterFunc();
102 Constant *getEmitFunctionFunc();
103 Constant *getEmitArcsFunc();
104 Constant *getDeleteFlushFunctionListFunc();
105 Constant *getEndFileFunc();
107 // Create or retrieve an i32 state value that is used to represent the
108 // pred block number for certain non-trivial edges.
109 GlobalVariable *getEdgeStateValue();
111 // Produce a table of pointers to counters, by predecessor and successor
113 GlobalVariable *buildEdgeLookupTable(Function *F,
114 GlobalVariable *Counter,
115 const UniqueVector<BasicBlock *>&Preds,
116 const UniqueVector<BasicBlock*>&Succs);
118 // Add the function to write out all our counters to the global destructor
120 Function *insertCounterWriteout(ArrayRef<std::pair<GlobalVariable*,
122 Function *insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> >);
123 void insertIndirectCounterIncrement();
125 std::string mangleName(DICompileUnit CU, const char *NewStem);
129 // Reversed, NUL-terminated copy of Options.Version.
130 char ReversedVersion[5];
137 char GCOVProfiler::ID = 0;
138 INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling",
139 "Insert instrumentation for GCOV profiling", false, false)
141 ModulePass *llvm::createGCOVProfilerPass(const GCOVOptions &Options) {
142 return new GCOVProfiler(Options);
148 static const char *LinesTag;
149 static const char *FunctionTag;
150 static const char *BlockTag;
151 static const char *EdgeTag;
155 void writeBytes(const char *Bytes, int Size) {
156 os->write(Bytes, Size);
159 void write(uint32_t i) {
160 writeBytes(reinterpret_cast<char*>(&i), 4);
163 // Returns the length measured in 4-byte blocks that will be used to
164 // represent this string in a GCOV file
165 unsigned lengthOfGCOVString(StringRef s) {
166 // A GCOV string is a length, followed by a NUL, then between 0 and 3 NULs
167 // padding out to the next 4-byte word. The length is measured in 4-byte
168 // words including padding, not bytes of actual string.
169 return (s.size() / 4) + 1;
172 void writeGCOVString(StringRef s) {
173 uint32_t Len = lengthOfGCOVString(s);
175 writeBytes(s.data(), s.size());
177 // Write 1 to 4 bytes of NUL padding.
178 assert((unsigned)(4 - (s.size() % 4)) > 0);
179 assert((unsigned)(4 - (s.size() % 4)) <= 4);
180 writeBytes("\0\0\0\0", 4 - (s.size() % 4));
185 const char *GCOVRecord::LinesTag = "\0\0\x45\x01";
186 const char *GCOVRecord::FunctionTag = "\0\0\0\1";
187 const char *GCOVRecord::BlockTag = "\0\0\x41\x01";
188 const char *GCOVRecord::EdgeTag = "\0\0\x43\x01";
193 // Constructed only by requesting it from a GCOVBlock, this object stores a
194 // list of line numbers and a single filename, representing lines that belong
196 class GCOVLines : public GCOVRecord {
198 void addLine(uint32_t Line) {
199 Lines.push_back(Line);
203 // Here 2 = 1 for string length + 1 for '0' id#.
204 return lengthOfGCOVString(Filename) + 2 + Lines.size();
209 writeGCOVString(Filename);
210 for (int i = 0, e = Lines.size(); i != e; ++i)
214 GCOVLines(StringRef F, raw_ostream *os)
221 SmallVector<uint32_t, 32> Lines;
224 // Represent a basic block in GCOV. Each block has a unique number in the
225 // function, number of lines belonging to each block, and a set of edges to
227 class GCOVBlock : public GCOVRecord {
229 GCOVLines &getFile(StringRef Filename) {
230 GCOVLines *&Lines = LinesByFile[Filename];
232 Lines = new GCOVLines(Filename, os);
237 void addEdge(GCOVBlock &Successor) {
238 OutEdges.push_back(&Successor);
243 for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
244 E = LinesByFile.end(); I != E; ++I) {
245 Len += I->second->length();
248 writeBytes(LinesTag, 4);
251 for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
252 E = LinesByFile.end(); I != E; ++I)
253 I->second->writeOut();
259 DeleteContainerSeconds(LinesByFile);
263 friend class GCOVFunction;
265 GCOVBlock(uint32_t Number, raw_ostream *os)
271 StringMap<GCOVLines *> LinesByFile;
272 SmallVector<GCOVBlock *, 4> OutEdges;
275 // A function has a unique identifier, a checksum (we leave as zero) and a
276 // set of blocks and a map of edges between blocks. This is the only GCOV
277 // object users can construct, the blocks and lines will be rooted here.
278 class GCOVFunction : public GCOVRecord {
280 GCOVFunction(DISubprogram SP, raw_ostream *os, uint32_t Ident,
281 bool UseCfgChecksum) {
284 Function *F = SP.getFunction();
285 DEBUG(dbgs() << "Function: " << F->getName() << "\n");
287 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
288 Blocks[BB] = new GCOVBlock(i++, os);
290 ReturnBlock = new GCOVBlock(i++, os);
292 writeBytes(FunctionTag, 4);
293 uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(SP.getName()) +
294 1 + lengthOfGCOVString(SP.getFilename()) + 1;
299 write(0); // lineno checksum
301 write(0); // cfg checksum
302 writeGCOVString(SP.getName());
303 writeGCOVString(SP.getFilename());
304 write(SP.getLineNumber());
308 DeleteContainerSeconds(Blocks);
312 GCOVBlock &getBlock(BasicBlock *BB) {
316 GCOVBlock &getReturnBlock() {
321 // Emit count of blocks.
322 writeBytes(BlockTag, 4);
323 write(Blocks.size() + 1);
324 for (int i = 0, e = Blocks.size() + 1; i != e; ++i) {
325 write(0); // No flags on our blocks.
327 DEBUG(dbgs() << Blocks.size() << " blocks.\n");
329 // Emit edges between blocks.
330 for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = Blocks.begin(),
331 E = Blocks.end(); I != E; ++I) {
332 GCOVBlock &Block = *I->second;
333 if (Block.OutEdges.empty()) continue;
335 writeBytes(EdgeTag, 4);
336 write(Block.OutEdges.size() * 2 + 1);
338 for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) {
339 DEBUG(dbgs() << Block.Number << " -> " << Block.OutEdges[i]->Number
341 write(Block.OutEdges[i]->Number);
342 write(0); // no flags
346 // Emit lines for each block.
347 for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = Blocks.begin(),
348 E = Blocks.end(); I != E; ++I) {
349 I->second->writeOut();
354 DenseMap<BasicBlock *, GCOVBlock *> Blocks;
355 GCOVBlock *ReturnBlock;
359 std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem) {
360 if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
361 for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
362 MDNode *N = GCov->getOperand(i);
363 if (N->getNumOperands() != 2) continue;
364 MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
365 MDNode *CompileUnit = dyn_cast<MDNode>(N->getOperand(1));
366 if (!GCovFile || !CompileUnit) continue;
367 if (CompileUnit == CU) {
368 SmallString<128> Filename = GCovFile->getString();
369 sys::path::replace_extension(Filename, NewStem);
370 return Filename.str();
375 SmallString<128> Filename = CU.getFilename();
376 sys::path::replace_extension(Filename, NewStem);
377 return sys::path::filename(Filename.str());
380 bool GCOVProfiler::runOnModule(Module &M) {
382 Ctx = &M.getContext();
384 if (Options.EmitNotes) emitProfileNotes();
385 if (Options.EmitData) return emitProfileArcs();
389 void GCOVProfiler::emitProfileNotes() {
390 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
391 if (!CU_Nodes) return;
393 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
394 // Each compile unit gets its own .gcno file. This means that whether we run
395 // this pass over the original .o's as they're produced, or run it after
396 // LTO, we'll generate the same .gcno files.
398 DICompileUnit CU(CU_Nodes->getOperand(i));
399 std::string ErrorInfo;
400 raw_fd_ostream out(mangleName(CU, "gcno").c_str(), ErrorInfo,
401 raw_fd_ostream::F_Binary);
402 out.write("oncg", 4);
403 out.write(ReversedVersion, 4);
404 out.write("MVLL", 4);
406 DIArray SPs = CU.getSubprograms();
407 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
408 DISubprogram SP(SPs.getElement(i));
409 if (!SP.Verify()) continue;
411 Function *F = SP.getFunction();
413 GCOVFunction Func(SP, &out, i, Options.UseCfgChecksum);
415 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
416 GCOVBlock &Block = Func.getBlock(BB);
417 TerminatorInst *TI = BB->getTerminator();
418 if (int successors = TI->getNumSuccessors()) {
419 for (int i = 0; i != successors; ++i) {
420 Block.addEdge(Func.getBlock(TI->getSuccessor(i)));
422 } else if (isa<ReturnInst>(TI)) {
423 Block.addEdge(Func.getReturnBlock());
427 for (BasicBlock::iterator I = BB->begin(), IE = BB->end();
429 const DebugLoc &Loc = I->getDebugLoc();
430 if (Loc.isUnknown()) continue;
431 if (Line == Loc.getLine()) continue;
432 Line = Loc.getLine();
433 if (SP != getDISubprogram(Loc.getScope(*Ctx))) continue;
435 GCOVLines &Lines = Block.getFile(SP.getFilename());
436 Lines.addLine(Loc.getLine());
441 out.write("\0\0\0\0\0\0\0\0", 8); // EOF
446 bool GCOVProfiler::emitProfileArcs() {
447 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
448 if (!CU_Nodes) return false;
451 bool InsertIndCounterIncrCode = false;
452 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
453 DICompileUnit CU(CU_Nodes->getOperand(i));
454 DIArray SPs = CU.getSubprograms();
455 SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
456 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
457 DISubprogram SP(SPs.getElement(i));
458 if (!SP.Verify()) continue;
459 Function *F = SP.getFunction();
461 if (!Result) Result = true;
463 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
464 TerminatorInst *TI = BB->getTerminator();
465 if (isa<ReturnInst>(TI))
468 Edges += TI->getNumSuccessors();
471 ArrayType *CounterTy =
472 ArrayType::get(Type::getInt64Ty(*Ctx), Edges);
473 GlobalVariable *Counters =
474 new GlobalVariable(*M, CounterTy, false,
475 GlobalValue::InternalLinkage,
476 Constant::getNullValue(CounterTy),
478 CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP));
480 UniqueVector<BasicBlock *> ComplexEdgePreds;
481 UniqueVector<BasicBlock *> ComplexEdgeSuccs;
484 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
485 TerminatorInst *TI = BB->getTerminator();
486 int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
488 IRBuilder<> Builder(TI);
490 if (Successors == 1) {
491 Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
493 Value *Count = Builder.CreateLoad(Counter);
494 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
495 Builder.CreateStore(Count, Counter);
496 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
497 Value *Sel = Builder.CreateSelect(BI->getCondition(),
498 Builder.getInt64(Edge),
499 Builder.getInt64(Edge + 1));
500 SmallVector<Value *, 2> Idx;
501 Idx.push_back(Builder.getInt64(0));
503 Value *Counter = Builder.CreateInBoundsGEP(Counters, Idx);
504 Value *Count = Builder.CreateLoad(Counter);
505 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
506 Builder.CreateStore(Count, Counter);
508 ComplexEdgePreds.insert(BB);
509 for (int i = 0; i != Successors; ++i)
510 ComplexEdgeSuccs.insert(TI->getSuccessor(i));
516 if (!ComplexEdgePreds.empty()) {
517 GlobalVariable *EdgeTable =
518 buildEdgeLookupTable(F, Counters,
519 ComplexEdgePreds, ComplexEdgeSuccs);
520 GlobalVariable *EdgeState = getEdgeStateValue();
522 for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) {
523 IRBuilder<> Builder(ComplexEdgePreds[i+1]->getTerminator());
524 Builder.CreateStore(Builder.getInt32(i), EdgeState);
526 for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) {
527 // call runtime to perform increment
528 BasicBlock::iterator InsertPt =
529 ComplexEdgeSuccs[i+1]->getFirstInsertionPt();
530 IRBuilder<> Builder(InsertPt);
531 Value *CounterPtrArray =
532 Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0,
533 i * ComplexEdgePreds.size());
535 // Build code to increment the counter.
536 InsertIndCounterIncrCode = true;
537 Builder.CreateCall2(getIncrementIndirectCounterFunc(),
538 EdgeState, CounterPtrArray);
543 Function *WriteoutF = insertCounterWriteout(CountersBySP);
544 Function *FlushF = insertFlush(CountersBySP);
546 // Create a small bit of code that registers the "__llvm_gcov_writeout" to
547 // be executed at exit and the "__llvm_gcov_flush" function to be executed
548 // when "__gcov_flush" is called.
549 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
550 Function *F = Function::Create(FTy, GlobalValue::InternalLinkage,
551 "__llvm_gcov_init", M);
552 F->setUnnamedAddr(true);
553 F->setLinkage(GlobalValue::InternalLinkage);
554 F->addFnAttr(Attribute::NoInline);
555 if (Options.NoRedZone)
556 F->addFnAttr(Attribute::NoRedZone);
558 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
559 IRBuilder<> Builder(BB);
561 FTy = FunctionType::get(Builder.getInt32Ty(),
562 PointerType::get(FTy, 0), false);
563 Constant *AtExitFn = M->getOrInsertFunction("atexit", FTy);
564 Builder.CreateCall(AtExitFn, WriteoutF);
566 // Register the local flush function.
567 FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
568 FTy = FunctionType::get(Builder.getVoidTy(),
569 PointerType::get(FTy, 0), false);
571 M->getOrInsertFunction("llvm_register_flush_function", FTy);
572 Builder.CreateCall(RegFlush, FlushF);
574 // Make sure that all the flush function list is deleted.
575 Builder.CreateCall(AtExitFn, getDeleteFlushFunctionListFunc());
576 Builder.CreateRetVoid();
578 appendToGlobalCtors(*M, F, 0);
581 if (InsertIndCounterIncrCode)
582 insertIndirectCounterIncrement();
587 // All edges with successors that aren't branches are "complex", because it
588 // requires complex logic to pick which counter to update.
589 GlobalVariable *GCOVProfiler::buildEdgeLookupTable(
591 GlobalVariable *Counters,
592 const UniqueVector<BasicBlock *> &Preds,
593 const UniqueVector<BasicBlock *> &Succs) {
594 // TODO: support invoke, threads. We rely on the fact that nothing can modify
595 // the whole-Module pred edge# between the time we set it and the time we next
596 // read it. Threads and invoke make this untrue.
598 // emit [(succs * preds) x i64*], logically [succ x [pred x i64*]].
599 size_t TableSize = Succs.size() * Preds.size();
600 Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
601 ArrayType *EdgeTableTy = ArrayType::get(Int64PtrTy, TableSize);
603 OwningArrayPtr<Constant *> EdgeTable(new Constant*[TableSize]);
604 Constant *NullValue = Constant::getNullValue(Int64PtrTy);
605 for (size_t i = 0; i != TableSize; ++i)
606 EdgeTable[i] = NullValue;
609 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
610 TerminatorInst *TI = BB->getTerminator();
611 int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
612 if (Successors > 1 && !isa<BranchInst>(TI) && !isa<ReturnInst>(TI)) {
613 for (int i = 0; i != Successors; ++i) {
614 BasicBlock *Succ = TI->getSuccessor(i);
615 IRBuilder<> Builder(Succ);
616 Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
618 EdgeTable[((Succs.idFor(Succ)-1) * Preds.size()) +
619 (Preds.idFor(BB)-1)] = cast<Constant>(Counter);
625 ArrayRef<Constant*> V(&EdgeTable[0], TableSize);
626 GlobalVariable *EdgeTableGV =
628 *M, EdgeTableTy, true, GlobalValue::InternalLinkage,
629 ConstantArray::get(EdgeTableTy, V),
630 "__llvm_gcda_edge_table");
631 EdgeTableGV->setUnnamedAddr(true);
635 Constant *GCOVProfiler::getStartFileFunc() {
637 Type::getInt8PtrTy(*Ctx), // const char *orig_filename
638 Type::getInt8PtrTy(*Ctx), // const char version[4]
640 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
641 return M->getOrInsertFunction("llvm_gcda_start_file", FTy);
644 Constant *GCOVProfiler::getIncrementIndirectCounterFunc() {
645 Type *Int32Ty = Type::getInt32Ty(*Ctx);
646 Type *Int64Ty = Type::getInt64Ty(*Ctx);
648 Int32Ty->getPointerTo(), // uint32_t *predecessor
649 Int64Ty->getPointerTo()->getPointerTo() // uint64_t **counters
651 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
652 return M->getOrInsertFunction("__llvm_gcov_indirect_counter_increment", FTy);
655 Constant *GCOVProfiler::getEmitFunctionFunc() {
657 Type::getInt32Ty(*Ctx), // uint32_t ident
658 Type::getInt8PtrTy(*Ctx), // const char *function_name
659 Type::getInt8Ty(*Ctx), // uint8_t use_extra_checksum
661 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
662 return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
665 Constant *GCOVProfiler::getEmitArcsFunc() {
667 Type::getInt32Ty(*Ctx), // uint32_t num_counters
668 Type::getInt64PtrTy(*Ctx), // uint64_t *counters
670 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
671 return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy);
674 Constant *GCOVProfiler::getDeleteFlushFunctionListFunc() {
675 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
676 return M->getOrInsertFunction("llvm_delete_flush_function_list", FTy);
679 Constant *GCOVProfiler::getEndFileFunc() {
680 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
681 return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
684 GlobalVariable *GCOVProfiler::getEdgeStateValue() {
685 GlobalVariable *GV = M->getGlobalVariable("__llvm_gcov_global_state_pred");
687 GV = new GlobalVariable(*M, Type::getInt32Ty(*Ctx), false,
688 GlobalValue::InternalLinkage,
689 ConstantInt::get(Type::getInt32Ty(*Ctx),
691 "__llvm_gcov_global_state_pred");
692 GV->setUnnamedAddr(true);
697 Function *GCOVProfiler::insertCounterWriteout(
698 ArrayRef<std::pair<GlobalVariable *, MDNode *> > CountersBySP) {
699 FunctionType *WriteoutFTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
700 Function *WriteoutF = M->getFunction("__llvm_gcov_writeout");
702 WriteoutF = Function::Create(WriteoutFTy, GlobalValue::InternalLinkage,
703 "__llvm_gcov_writeout", M);
704 WriteoutF->setUnnamedAddr(true);
705 WriteoutF->addFnAttr(Attribute::NoInline);
706 if (Options.NoRedZone)
707 WriteoutF->addFnAttr(Attribute::NoRedZone);
709 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
710 IRBuilder<> Builder(BB);
712 Constant *StartFile = getStartFileFunc();
713 Constant *EmitFunction = getEmitFunctionFunc();
714 Constant *EmitArcs = getEmitArcsFunc();
715 Constant *EndFile = getEndFileFunc();
717 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
719 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
720 DICompileUnit CU(CU_Nodes->getOperand(i));
721 std::string FilenameGcda = mangleName(CU, "gcda");
722 Builder.CreateCall2(StartFile,
723 Builder.CreateGlobalStringPtr(FilenameGcda),
724 Builder.CreateGlobalStringPtr(ReversedVersion));
725 for (unsigned j = 0, e = CountersBySP.size(); j != e; ++j) {
726 DISubprogram SP(CountersBySP[j].second);
727 Builder.CreateCall3(EmitFunction,
729 Options.FunctionNamesInData ?
730 Builder.CreateGlobalStringPtr(SP.getName()) :
731 Constant::getNullValue(Builder.getInt8PtrTy()),
732 Builder.getInt8(Options.UseCfgChecksum));
734 GlobalVariable *GV = CountersBySP[j].first;
736 cast<ArrayType>(GV->getType()->getElementType())->getNumElements();
737 Builder.CreateCall2(EmitArcs,
738 Builder.getInt32(Arcs),
739 Builder.CreateConstGEP2_64(GV, 0, 0));
741 Builder.CreateCall(EndFile);
745 Builder.CreateRetVoid();
749 void GCOVProfiler::insertIndirectCounterIncrement() {
751 cast<Function>(GCOVProfiler::getIncrementIndirectCounterFunc());
752 Fn->setUnnamedAddr(true);
753 Fn->setLinkage(GlobalValue::InternalLinkage);
754 Fn->addFnAttr(Attribute::NoInline);
755 if (Options.NoRedZone)
756 Fn->addFnAttr(Attribute::NoRedZone);
758 // Create basic blocks for function.
759 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", Fn);
760 IRBuilder<> Builder(BB);
762 BasicBlock *PredNotNegOne = BasicBlock::Create(*Ctx, "", Fn);
763 BasicBlock *CounterEnd = BasicBlock::Create(*Ctx, "", Fn);
764 BasicBlock *Exit = BasicBlock::Create(*Ctx, "exit", Fn);
766 // uint32_t pred = *predecessor;
767 // if (pred == 0xffffffff) return;
768 Argument *Arg = Fn->arg_begin();
769 Arg->setName("predecessor");
770 Value *Pred = Builder.CreateLoad(Arg, "pred");
771 Value *Cond = Builder.CreateICmpEQ(Pred, Builder.getInt32(0xffffffff));
772 BranchInst::Create(Exit, PredNotNegOne, Cond, BB);
774 Builder.SetInsertPoint(PredNotNegOne);
776 // uint64_t *counter = counters[pred];
777 // if (!counter) return;
778 Value *ZExtPred = Builder.CreateZExt(Pred, Builder.getInt64Ty());
779 Arg = llvm::next(Fn->arg_begin());
780 Arg->setName("counters");
781 Value *GEP = Builder.CreateGEP(Arg, ZExtPred);
782 Value *Counter = Builder.CreateLoad(GEP, "counter");
783 Cond = Builder.CreateICmpEQ(Counter,
784 Constant::getNullValue(
785 Builder.getInt64Ty()->getPointerTo()));
786 Builder.CreateCondBr(Cond, Exit, CounterEnd);
789 Builder.SetInsertPoint(CounterEnd);
790 Value *Add = Builder.CreateAdd(Builder.CreateLoad(Counter),
791 Builder.getInt64(1));
792 Builder.CreateStore(Add, Counter);
793 Builder.CreateBr(Exit);
795 // Fill in the exit block.
796 Builder.SetInsertPoint(Exit);
797 Builder.CreateRetVoid();
800 Function *GCOVProfiler::
801 insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> > CountersBySP) {
802 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
803 Function *FlushF = M->getFunction("__llvm_gcov_flush");
805 FlushF = Function::Create(FTy, GlobalValue::InternalLinkage,
806 "__llvm_gcov_flush", M);
808 FlushF->setLinkage(GlobalValue::InternalLinkage);
809 FlushF->setUnnamedAddr(true);
810 FlushF->addFnAttr(Attribute::NoInline);
811 if (Options.NoRedZone)
812 FlushF->addFnAttr(Attribute::NoRedZone);
814 BasicBlock *Entry = BasicBlock::Create(*Ctx, "entry", FlushF);
816 // Write out the current counters.
817 Constant *WriteoutF = M->getFunction("__llvm_gcov_writeout");
818 assert(WriteoutF && "Need to create the writeout function first!");
820 IRBuilder<> Builder(Entry);
821 Builder.CreateCall(WriteoutF);
823 // Zero out the counters.
824 for (ArrayRef<std::pair<GlobalVariable *, MDNode *> >::iterator
825 I = CountersBySP.begin(), E = CountersBySP.end();
827 GlobalVariable *GV = I->first;
828 Constant *Null = Constant::getNullValue(GV->getType()->getElementType());
829 Builder.CreateStore(Null, GV);
832 Type *RetTy = FlushF->getReturnType();
833 if (RetTy == Type::getVoidTy(*Ctx))
834 Builder.CreateRetVoid();
835 else if (RetTy->isIntegerTy())
836 // Used if __llvm_gcov_flush was implicitly declared.
837 Builder.CreateRet(ConstantInt::get(RetTy, 0));
839 report_fatal_error("invalid return type for __llvm_gcov_flush");