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 "ProfilingUtils.h"
20 #include "llvm/Transforms/Instrumentation.h"
21 #include "llvm/Analysis/DebugInfo.h"
22 #include "llvm/Module.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/DebugLoc.h"
28 #include "llvm/Support/InstIterator.h"
29 #include "llvm/Support/IRBuilder.h"
30 #include "llvm/Support/PathV2.h"
31 #include "llvm/ADT/DenseMap.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/STLExtras.h"
34 #include "llvm/ADT/StringExtras.h"
35 #include "llvm/ADT/StringMap.h"
36 #include "llvm/ADT/UniqueVector.h"
42 class GCOVProfiler : public ModulePass {
46 : ModulePass(ID), EmitNotes(true), EmitData(true), Use402Format(false),
47 UseExtraChecksum(false) {
48 initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
50 GCOVProfiler(bool EmitNotes, bool EmitData, bool use402Format = false,
51 bool useExtraChecksum = false)
52 : ModulePass(ID), EmitNotes(EmitNotes), EmitData(EmitData),
53 Use402Format(use402Format), UseExtraChecksum(useExtraChecksum) {
54 assert((EmitNotes || EmitData) && "GCOVProfiler asked to do nothing?");
55 initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
57 virtual const char *getPassName() const {
58 return "GCOV Profiler";
62 bool runOnModule(Module &M);
64 // Create the GCNO files for the Module based on DebugInfo.
67 // Modify the program to track transitions along edges and call into the
68 // profiling runtime to emit .gcda files when run.
69 bool emitProfileArcs();
71 // Get pointers to the functions in the runtime library.
72 Constant *getStartFileFunc();
73 Constant *getIncrementIndirectCounterFunc();
74 Constant *getEmitFunctionFunc();
75 Constant *getEmitArcsFunc();
76 Constant *getEndFileFunc();
78 // Create or retrieve an i32 state value that is used to represent the
79 // pred block number for certain non-trivial edges.
80 GlobalVariable *getEdgeStateValue();
82 // Produce a table of pointers to counters, by predecessor and successor
84 GlobalVariable *buildEdgeLookupTable(Function *F,
85 GlobalVariable *Counter,
86 const UniqueVector<BasicBlock *> &Preds,
87 const UniqueVector<BasicBlock *> &Succs);
89 // Add the function to write out all our counters to the global destructor
91 void insertCounterWriteout(SmallVector<std::pair<GlobalVariable *,
94 std::string mangleName(DICompileUnit CU, std::string NewStem);
99 bool UseExtraChecksum;
106 char GCOVProfiler::ID = 0;
107 INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling",
108 "Insert instrumentation for GCOV profiling", false, false)
110 ModulePass *llvm::createGCOVProfilerPass(bool EmitNotes, bool EmitData,
112 return new GCOVProfiler(EmitNotes, EmitData, Use402Format);
118 static const char *LinesTag;
119 static const char *FunctionTag;
120 static const char *BlockTag;
121 static const char *EdgeTag;
125 void writeBytes(const char *Bytes, int Size) {
126 os->write(Bytes, Size);
129 void write(uint32_t i) {
130 writeBytes(reinterpret_cast<char*>(&i), 4);
133 // Returns the length measured in 4-byte blocks that will be used to
134 // represent this string in a GCOV file
135 unsigned lengthOfGCOVString(StringRef s) {
136 // A GCOV string is a length, followed by a NUL, then between 0 and 3 NULs
137 // padding out to the next 4-byte word. The length is measured in 4-byte
138 // words including padding, not bytes of actual string.
139 return (s.size() / 4) + 1;
142 void writeGCOVString(StringRef s) {
143 uint32_t Len = lengthOfGCOVString(s);
145 writeBytes(s.data(), s.size());
147 // Write 1 to 4 bytes of NUL padding.
148 assert((unsigned)(4 - (s.size() % 4)) > 0);
149 assert((unsigned)(4 - (s.size() % 4)) <= 4);
150 writeBytes("\0\0\0\0", 4 - (s.size() % 4));
155 const char *GCOVRecord::LinesTag = "\0\0\x45\x01";
156 const char *GCOVRecord::FunctionTag = "\0\0\0\1";
157 const char *GCOVRecord::BlockTag = "\0\0\x41\x01";
158 const char *GCOVRecord::EdgeTag = "\0\0\x43\x01";
163 // Constructed only by requesting it from a GCOVBlock, this object stores a
164 // list of line numbers and a single filename, representing lines that belong
166 class GCOVLines : public GCOVRecord {
168 void addLine(uint32_t Line) {
169 Lines.push_back(Line);
173 // Here 2 = 1 for string length + 1 for '0' id#.
174 return lengthOfGCOVString(Filename) + 2 + Lines.size();
179 writeGCOVString(Filename);
180 for (int i = 0, e = Lines.size(); i != e; ++i)
184 GCOVLines(StringRef F, raw_ostream *os)
191 SmallVector<uint32_t, 32> Lines;
194 // Represent a basic block in GCOV. Each block has a unique number in the
195 // function, number of lines belonging to each block, and a set of edges to
197 class GCOVBlock : public GCOVRecord {
199 GCOVLines &getFile(StringRef Filename) {
200 GCOVLines *&Lines = LinesByFile[Filename];
202 Lines = new GCOVLines(Filename, os);
207 void addEdge(GCOVBlock &Successor) {
208 OutEdges.push_back(&Successor);
213 for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
214 E = LinesByFile.end(); I != E; ++I) {
215 Len += I->second->length();
218 writeBytes(LinesTag, 4);
221 for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
222 E = LinesByFile.end(); I != E; ++I)
223 I->second->writeOut();
229 DeleteContainerSeconds(LinesByFile);
233 friend class GCOVFunction;
235 GCOVBlock(uint32_t Number, raw_ostream *os)
241 StringMap<GCOVLines *> LinesByFile;
242 SmallVector<GCOVBlock *, 4> OutEdges;
245 // A function has a unique identifier, a checksum (we leave as zero) and a
246 // set of blocks and a map of edges between blocks. This is the only GCOV
247 // object users can construct, the blocks and lines will be rooted here.
248 class GCOVFunction : public GCOVRecord {
250 GCOVFunction(DISubprogram SP, raw_ostream *os,
251 bool Use402Format, bool UseExtraChecksum) {
254 Function *F = SP.getFunction();
255 DEBUG(dbgs() << "Function: " << F->getName() << "\n");
257 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
258 Blocks[BB] = new GCOVBlock(i++, os);
260 ReturnBlock = new GCOVBlock(i++, os);
262 writeBytes(FunctionTag, 4);
263 uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(SP.getName()) +
264 1 + lengthOfGCOVString(SP.getFilename()) + 1;
265 if (UseExtraChecksum)
268 uint32_t Ident = reinterpret_cast<intptr_t>((MDNode*)SP);
270 write(0); // lineno checksum
271 if (UseExtraChecksum)
272 write(0); // cfg checksum
273 writeGCOVString(SP.getName());
274 writeGCOVString(SP.getFilename());
275 write(SP.getLineNumber());
279 DeleteContainerSeconds(Blocks);
283 GCOVBlock &getBlock(BasicBlock *BB) {
287 GCOVBlock &getReturnBlock() {
292 // Emit count of blocks.
293 writeBytes(BlockTag, 4);
294 write(Blocks.size() + 1);
295 for (int i = 0, e = Blocks.size() + 1; i != e; ++i) {
296 write(0); // No flags on our blocks.
298 DEBUG(dbgs() << Blocks.size() << " blocks.\n");
300 // Emit edges between blocks.
301 for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = Blocks.begin(),
302 E = Blocks.end(); I != E; ++I) {
303 GCOVBlock &Block = *I->second;
304 if (Block.OutEdges.empty()) continue;
306 writeBytes(EdgeTag, 4);
307 write(Block.OutEdges.size() * 2 + 1);
309 for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) {
310 DEBUG(dbgs() << Block.Number << " -> " << Block.OutEdges[i]->Number
312 write(Block.OutEdges[i]->Number);
313 write(0); // no flags
317 // Emit lines for each block.
318 for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = Blocks.begin(),
319 E = Blocks.end(); I != E; ++I) {
320 I->second->writeOut();
325 DenseMap<BasicBlock *, GCOVBlock *> Blocks;
326 GCOVBlock *ReturnBlock;
330 std::string GCOVProfiler::mangleName(DICompileUnit CU, std::string NewStem) {
331 if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
332 for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
333 MDNode *N = GCov->getOperand(i);
334 if (N->getNumOperands() != 2) continue;
335 MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
336 MDNode *CompileUnit = dyn_cast<MDNode>(N->getOperand(1));
337 if (!GCovFile || !CompileUnit) continue;
338 if (CompileUnit == CU) {
339 SmallString<128> Filename = GCovFile->getString();
340 sys::path::replace_extension(Filename, NewStem);
341 return Filename.str();
346 SmallString<128> Filename = CU.getFilename();
347 sys::path::replace_extension(Filename, NewStem);
348 return sys::path::filename(Filename.str());
351 bool GCOVProfiler::runOnModule(Module &M) {
353 Ctx = &M.getContext();
355 if (EmitNotes) emitGCNO();
356 if (EmitData) return emitProfileArcs();
360 void GCOVProfiler::emitGCNO() {
361 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
362 if (!CU_Nodes) return;
364 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
365 // Each compile unit gets its own .gcno file. This means that whether we run
366 // this pass over the original .o's as they're produced, or run it after
367 // LTO, we'll generate the same .gcno files.
369 DICompileUnit CU(CU_Nodes->getOperand(i));
370 std::string ErrorInfo;
371 raw_fd_ostream out(mangleName(CU, "gcno").c_str(), ErrorInfo,
372 raw_fd_ostream::F_Binary);
374 out.write("oncg*404MVLL", 12);
376 out.write("oncg*204MVLL", 12);
378 DIArray SPs = CU.getSubprograms();
379 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
380 DISubprogram SP(SPs.getElement(i));
381 if (!SP.Verify()) continue;
383 Function *F = SP.getFunction();
385 GCOVFunction Func(SP, &out, Use402Format, UseExtraChecksum);
387 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
388 GCOVBlock &Block = Func.getBlock(BB);
389 TerminatorInst *TI = BB->getTerminator();
390 if (int successors = TI->getNumSuccessors()) {
391 for (int i = 0; i != successors; ++i) {
392 Block.addEdge(Func.getBlock(TI->getSuccessor(i)));
394 } else if (isa<ReturnInst>(TI)) {
395 Block.addEdge(Func.getReturnBlock());
399 for (BasicBlock::iterator I = BB->begin(), IE = BB->end();
401 const DebugLoc &Loc = I->getDebugLoc();
402 if (Loc.isUnknown()) continue;
403 if (Line == Loc.getLine()) continue;
404 Line = Loc.getLine();
405 if (SP != getDISubprogram(Loc.getScope(*Ctx))) continue;
407 GCOVLines &Lines = Block.getFile(SP.getFilename());
408 Lines.addLine(Loc.getLine());
413 out.write("\0\0\0\0\0\0\0\0", 8); // EOF
418 bool GCOVProfiler::emitProfileArcs() {
419 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
420 if (!CU_Nodes) return false;
423 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
424 DICompileUnit CU(CU_Nodes->getOperand(i));
425 DIArray SPs = CU.getSubprograms();
426 SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
427 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
428 DISubprogram SP(SPs.getElement(i));
429 if (!SP.Verify()) continue;
430 Function *F = SP.getFunction();
432 if (!Result) Result = true;
434 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
435 TerminatorInst *TI = BB->getTerminator();
436 if (isa<ReturnInst>(TI))
439 Edges += TI->getNumSuccessors();
442 ArrayType *CounterTy =
443 ArrayType::get(Type::getInt64Ty(*Ctx), Edges);
444 GlobalVariable *Counters =
445 new GlobalVariable(*M, CounterTy, false,
446 GlobalValue::InternalLinkage,
447 Constant::getNullValue(CounterTy),
448 "__llvm_gcov_ctr", 0, false, 0);
449 CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP));
451 UniqueVector<BasicBlock *> ComplexEdgePreds;
452 UniqueVector<BasicBlock *> ComplexEdgeSuccs;
455 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
456 TerminatorInst *TI = BB->getTerminator();
457 int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
459 IRBuilder<> Builder(TI);
461 if (Successors == 1) {
462 Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
464 Value *Count = Builder.CreateLoad(Counter);
465 Count = Builder.CreateAdd(Count,
466 ConstantInt::get(Type::getInt64Ty(*Ctx),1));
467 Builder.CreateStore(Count, Counter);
468 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
469 Value *Sel = Builder.CreateSelect(
471 ConstantInt::get(Type::getInt64Ty(*Ctx), Edge),
472 ConstantInt::get(Type::getInt64Ty(*Ctx), Edge + 1));
473 SmallVector<Value *, 2> Idx;
474 Idx.push_back(Constant::getNullValue(Type::getInt64Ty(*Ctx)));
476 Value *Counter = Builder.CreateInBoundsGEP(Counters, Idx);
477 Value *Count = Builder.CreateLoad(Counter);
478 Count = Builder.CreateAdd(Count,
479 ConstantInt::get(Type::getInt64Ty(*Ctx),1));
480 Builder.CreateStore(Count, Counter);
482 ComplexEdgePreds.insert(BB);
483 for (int i = 0; i != Successors; ++i)
484 ComplexEdgeSuccs.insert(TI->getSuccessor(i));
490 if (!ComplexEdgePreds.empty()) {
491 GlobalVariable *EdgeTable =
492 buildEdgeLookupTable(F, Counters,
493 ComplexEdgePreds, ComplexEdgeSuccs);
494 GlobalVariable *EdgeState = getEdgeStateValue();
496 Type *Int32Ty = Type::getInt32Ty(*Ctx);
497 for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) {
498 IRBuilder<> Builder(ComplexEdgePreds[i+1]->getTerminator());
499 Builder.CreateStore(ConstantInt::get(Int32Ty, i), EdgeState);
501 for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) {
502 // call runtime to perform increment
503 BasicBlock::iterator InsertPt =
504 ComplexEdgeSuccs[i+1]->getFirstInsertionPt();
505 IRBuilder<> Builder(InsertPt);
506 Value *CounterPtrArray =
507 Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0,
508 i * ComplexEdgePreds.size());
509 Builder.CreateCall2(getIncrementIndirectCounterFunc(),
510 EdgeState, CounterPtrArray);
511 // clear the predecessor number
512 Builder.CreateStore(ConstantInt::get(Int32Ty, 0xffffffff), EdgeState);
516 insertCounterWriteout(CountersBySP);
521 // All edges with successors that aren't branches are "complex", because it
522 // requires complex logic to pick which counter to update.
523 GlobalVariable *GCOVProfiler::buildEdgeLookupTable(
525 GlobalVariable *Counters,
526 const UniqueVector<BasicBlock *> &Preds,
527 const UniqueVector<BasicBlock *> &Succs) {
528 // TODO: support invoke, threads. We rely on the fact that nothing can modify
529 // the whole-Module pred edge# between the time we set it and the time we next
530 // read it. Threads and invoke make this untrue.
532 // emit [(succs * preds) x i64*], logically [succ x [pred x i64*]].
533 Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
534 ArrayType *EdgeTableTy = ArrayType::get(
535 Int64PtrTy, Succs.size() * Preds.size());
537 Constant **EdgeTable = new Constant*[Succs.size() * Preds.size()];
538 Constant *NullValue = Constant::getNullValue(Int64PtrTy);
539 for (int i = 0, ie = Succs.size() * Preds.size(); i != ie; ++i)
540 EdgeTable[i] = NullValue;
543 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
544 TerminatorInst *TI = BB->getTerminator();
545 int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
546 if (Successors > 1 && !isa<BranchInst>(TI) && !isa<ReturnInst>(TI)) {
547 for (int i = 0; i != Successors; ++i) {
548 BasicBlock *Succ = TI->getSuccessor(i);
549 IRBuilder<> builder(Succ);
550 Value *Counter = builder.CreateConstInBoundsGEP2_64(Counters, 0,
552 EdgeTable[((Succs.idFor(Succ)-1) * Preds.size()) +
553 (Preds.idFor(BB)-1)] = cast<Constant>(Counter);
559 ArrayRef<Constant*> V(&EdgeTable[0], Succs.size() * Preds.size());
560 GlobalVariable *EdgeTableGV =
562 *M, EdgeTableTy, true, GlobalValue::InternalLinkage,
563 ConstantArray::get(EdgeTableTy, V),
564 "__llvm_gcda_edge_table");
565 EdgeTableGV->setUnnamedAddr(true);
569 Constant *GCOVProfiler::getStartFileFunc() {
570 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
571 Type::getInt8PtrTy(*Ctx), false);
572 return M->getOrInsertFunction("llvm_gcda_start_file", FTy);
575 Constant *GCOVProfiler::getIncrementIndirectCounterFunc() {
577 Type::getInt32PtrTy(*Ctx), // uint32_t *predecessor
578 Type::getInt64PtrTy(*Ctx)->getPointerTo(), // uint64_t **state_table_row
580 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
582 return M->getOrInsertFunction("llvm_gcda_increment_indirect_counter", FTy);
585 Constant *GCOVProfiler::getEmitFunctionFunc() {
587 Type::getInt32Ty(*Ctx), // uint32_t ident
588 Type::getInt8PtrTy(*Ctx), // const char *function_name
590 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
592 return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
595 Constant *GCOVProfiler::getEmitArcsFunc() {
597 Type::getInt32Ty(*Ctx), // uint32_t num_counters
598 Type::getInt64PtrTy(*Ctx), // uint64_t *counters
600 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
602 return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy);
605 Constant *GCOVProfiler::getEndFileFunc() {
606 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
607 return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
610 GlobalVariable *GCOVProfiler::getEdgeStateValue() {
611 GlobalVariable *GV = M->getGlobalVariable("__llvm_gcov_global_state_pred");
613 GV = new GlobalVariable(*M, Type::getInt32Ty(*Ctx), false,
614 GlobalValue::InternalLinkage,
615 ConstantInt::get(Type::getInt32Ty(*Ctx),
617 "__llvm_gcov_global_state_pred");
618 GV->setUnnamedAddr(true);
623 void GCOVProfiler::insertCounterWriteout(
624 SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> &CountersBySP) {
625 FunctionType *WriteoutFTy =
626 FunctionType::get(Type::getVoidTy(*Ctx), false);
627 Function *WriteoutF = Function::Create(WriteoutFTy,
628 GlobalValue::InternalLinkage,
629 "__llvm_gcov_writeout", M);
630 WriteoutF->setUnnamedAddr(true);
631 BasicBlock *BB = BasicBlock::Create(*Ctx, "", WriteoutF);
632 IRBuilder<> Builder(BB);
634 Constant *StartFile = getStartFileFunc();
635 Constant *EmitFunction = getEmitFunctionFunc();
636 Constant *EmitArcs = getEmitArcsFunc();
637 Constant *EndFile = getEndFileFunc();
639 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
641 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
642 DICompileUnit compile_unit(CU_Nodes->getOperand(i));
643 std::string FilenameGcda = mangleName(compile_unit, "gcda");
644 Builder.CreateCall(StartFile,
645 Builder.CreateGlobalStringPtr(FilenameGcda));
646 for (SmallVector<std::pair<GlobalVariable *, MDNode *>, 8>::iterator
647 I = CountersBySP.begin(), E = CountersBySP.end();
649 DISubprogram SP(I->second);
650 intptr_t ident = reinterpret_cast<intptr_t>(I->second);
651 Builder.CreateCall2(EmitFunction,
652 ConstantInt::get(Type::getInt32Ty(*Ctx), ident),
653 Builder.CreateGlobalStringPtr(SP.getName()));
655 GlobalVariable *GV = I->first;
657 cast<ArrayType>(GV->getType()->getElementType())->getNumElements();
658 Builder.CreateCall2(EmitArcs,
659 ConstantInt::get(Type::getInt32Ty(*Ctx), Arcs),
660 Builder.CreateConstGEP2_64(GV, 0, 0));
662 Builder.CreateCall(EndFile);
665 Builder.CreateRetVoid();
667 InsertProfilingShutdownCall(WriteoutF, M);