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 void incrementIndirectCounter(IRBuilder<> &Builder, BasicBlock *Exit,
74 GlobalVariable *EdgeState,
75 Value *CounterPtrArray);
76 Constant *getEmitFunctionFunc();
77 Constant *getEmitArcsFunc();
78 Constant *getEndFileFunc();
80 // Create or retrieve an i32 state value that is used to represent the
81 // pred block number for certain non-trivial edges.
82 GlobalVariable *getEdgeStateValue();
84 // Produce a table of pointers to counters, by predecessor and successor
86 GlobalVariable *buildEdgeLookupTable(Function *F,
87 GlobalVariable *Counter,
88 const UniqueVector<BasicBlock *> &Preds,
89 const UniqueVector<BasicBlock *> &Succs);
91 // Add the function to write out all our counters to the global destructor
93 void insertCounterWriteout(SmallVector<std::pair<GlobalVariable *,
96 std::string mangleName(DICompileUnit CU, std::string NewStem);
101 bool UseExtraChecksum;
108 char GCOVProfiler::ID = 0;
109 INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling",
110 "Insert instrumentation for GCOV profiling", false, false)
112 ModulePass *llvm::createGCOVProfilerPass(bool EmitNotes, bool EmitData,
114 bool UseExtraChecksum) {
115 return new GCOVProfiler(EmitNotes, EmitData, Use402Format, UseExtraChecksum);
121 static const char *LinesTag;
122 static const char *FunctionTag;
123 static const char *BlockTag;
124 static const char *EdgeTag;
128 void writeBytes(const char *Bytes, int Size) {
129 os->write(Bytes, Size);
132 void write(uint32_t i) {
133 writeBytes(reinterpret_cast<char*>(&i), 4);
136 // Returns the length measured in 4-byte blocks that will be used to
137 // represent this string in a GCOV file
138 unsigned lengthOfGCOVString(StringRef s) {
139 // A GCOV string is a length, followed by a NUL, then between 0 and 3 NULs
140 // padding out to the next 4-byte word. The length is measured in 4-byte
141 // words including padding, not bytes of actual string.
142 return (s.size() / 4) + 1;
145 void writeGCOVString(StringRef s) {
146 uint32_t Len = lengthOfGCOVString(s);
148 writeBytes(s.data(), s.size());
150 // Write 1 to 4 bytes of NUL padding.
151 assert((unsigned)(4 - (s.size() % 4)) > 0);
152 assert((unsigned)(4 - (s.size() % 4)) <= 4);
153 writeBytes("\0\0\0\0", 4 - (s.size() % 4));
158 const char *GCOVRecord::LinesTag = "\0\0\x45\x01";
159 const char *GCOVRecord::FunctionTag = "\0\0\0\1";
160 const char *GCOVRecord::BlockTag = "\0\0\x41\x01";
161 const char *GCOVRecord::EdgeTag = "\0\0\x43\x01";
166 // Constructed only by requesting it from a GCOVBlock, this object stores a
167 // list of line numbers and a single filename, representing lines that belong
169 class GCOVLines : public GCOVRecord {
171 void addLine(uint32_t Line) {
172 Lines.push_back(Line);
176 // Here 2 = 1 for string length + 1 for '0' id#.
177 return lengthOfGCOVString(Filename) + 2 + Lines.size();
182 writeGCOVString(Filename);
183 for (int i = 0, e = Lines.size(); i != e; ++i)
187 GCOVLines(StringRef F, raw_ostream *os)
194 SmallVector<uint32_t, 32> Lines;
197 // Represent a basic block in GCOV. Each block has a unique number in the
198 // function, number of lines belonging to each block, and a set of edges to
200 class GCOVBlock : public GCOVRecord {
202 GCOVLines &getFile(StringRef Filename) {
203 GCOVLines *&Lines = LinesByFile[Filename];
205 Lines = new GCOVLines(Filename, os);
210 void addEdge(GCOVBlock &Successor) {
211 OutEdges.push_back(&Successor);
216 for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
217 E = LinesByFile.end(); I != E; ++I) {
218 Len += I->second->length();
221 writeBytes(LinesTag, 4);
224 for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
225 E = LinesByFile.end(); I != E; ++I)
226 I->second->writeOut();
232 DeleteContainerSeconds(LinesByFile);
236 friend class GCOVFunction;
238 GCOVBlock(uint32_t Number, raw_ostream *os)
244 StringMap<GCOVLines *> LinesByFile;
245 SmallVector<GCOVBlock *, 4> OutEdges;
248 // A function has a unique identifier, a checksum (we leave as zero) and a
249 // set of blocks and a map of edges between blocks. This is the only GCOV
250 // object users can construct, the blocks and lines will be rooted here.
251 class GCOVFunction : public GCOVRecord {
253 GCOVFunction(DISubprogram SP, raw_ostream *os,
254 bool Use402Format, bool UseExtraChecksum) {
257 Function *F = SP.getFunction();
258 DEBUG(dbgs() << "Function: " << F->getName() << "\n");
260 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
261 Blocks[BB] = new GCOVBlock(i++, os);
263 ReturnBlock = new GCOVBlock(i++, os);
265 writeBytes(FunctionTag, 4);
266 uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(SP.getName()) +
267 1 + lengthOfGCOVString(SP.getFilename()) + 1;
268 if (UseExtraChecksum)
271 uint32_t Ident = reinterpret_cast<intptr_t>((MDNode*)SP);
273 write(0); // lineno checksum
274 if (UseExtraChecksum)
275 write(0); // cfg checksum
276 writeGCOVString(SP.getName());
277 writeGCOVString(SP.getFilename());
278 write(SP.getLineNumber());
282 DeleteContainerSeconds(Blocks);
286 GCOVBlock &getBlock(BasicBlock *BB) {
290 GCOVBlock &getReturnBlock() {
295 // Emit count of blocks.
296 writeBytes(BlockTag, 4);
297 write(Blocks.size() + 1);
298 for (int i = 0, e = Blocks.size() + 1; i != e; ++i) {
299 write(0); // No flags on our blocks.
301 DEBUG(dbgs() << Blocks.size() << " blocks.\n");
303 // Emit edges between blocks.
304 for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = Blocks.begin(),
305 E = Blocks.end(); I != E; ++I) {
306 GCOVBlock &Block = *I->second;
307 if (Block.OutEdges.empty()) continue;
309 writeBytes(EdgeTag, 4);
310 write(Block.OutEdges.size() * 2 + 1);
312 for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) {
313 DEBUG(dbgs() << Block.Number << " -> " << Block.OutEdges[i]->Number
315 write(Block.OutEdges[i]->Number);
316 write(0); // no flags
320 // Emit lines for each block.
321 for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = Blocks.begin(),
322 E = Blocks.end(); I != E; ++I) {
323 I->second->writeOut();
328 DenseMap<BasicBlock *, GCOVBlock *> Blocks;
329 GCOVBlock *ReturnBlock;
333 std::string GCOVProfiler::mangleName(DICompileUnit CU, std::string NewStem) {
334 if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
335 for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
336 MDNode *N = GCov->getOperand(i);
337 if (N->getNumOperands() != 2) continue;
338 MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
339 MDNode *CompileUnit = dyn_cast<MDNode>(N->getOperand(1));
340 if (!GCovFile || !CompileUnit) continue;
341 if (CompileUnit == CU) {
342 SmallString<128> Filename = GCovFile->getString();
343 sys::path::replace_extension(Filename, NewStem);
344 return Filename.str();
349 SmallString<128> Filename = CU.getFilename();
350 sys::path::replace_extension(Filename, NewStem);
351 return sys::path::filename(Filename.str());
354 bool GCOVProfiler::runOnModule(Module &M) {
356 Ctx = &M.getContext();
358 if (EmitNotes) emitGCNO();
359 if (EmitData) return emitProfileArcs();
363 void GCOVProfiler::emitGCNO() {
364 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
365 if (!CU_Nodes) return;
367 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
368 // Each compile unit gets its own .gcno file. This means that whether we run
369 // this pass over the original .o's as they're produced, or run it after
370 // LTO, we'll generate the same .gcno files.
372 DICompileUnit CU(CU_Nodes->getOperand(i));
373 std::string ErrorInfo;
374 raw_fd_ostream out(mangleName(CU, "gcno").c_str(), ErrorInfo,
375 raw_fd_ostream::F_Binary);
377 out.write("oncg*404MVLL", 12);
379 out.write("oncg*204MVLL", 12);
381 DIArray SPs = CU.getSubprograms();
382 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
383 DISubprogram SP(SPs.getElement(i));
384 if (!SP.Verify()) continue;
386 Function *F = SP.getFunction();
388 GCOVFunction Func(SP, &out, Use402Format, UseExtraChecksum);
390 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
391 GCOVBlock &Block = Func.getBlock(BB);
392 TerminatorInst *TI = BB->getTerminator();
393 if (int successors = TI->getNumSuccessors()) {
394 for (int i = 0; i != successors; ++i) {
395 Block.addEdge(Func.getBlock(TI->getSuccessor(i)));
397 } else if (isa<ReturnInst>(TI)) {
398 Block.addEdge(Func.getReturnBlock());
402 for (BasicBlock::iterator I = BB->begin(), IE = BB->end();
404 const DebugLoc &Loc = I->getDebugLoc();
405 if (Loc.isUnknown()) continue;
406 if (Line == Loc.getLine()) continue;
407 Line = Loc.getLine();
408 if (SP != getDISubprogram(Loc.getScope(*Ctx))) continue;
410 GCOVLines &Lines = Block.getFile(SP.getFilename());
411 Lines.addLine(Loc.getLine());
416 out.write("\0\0\0\0\0\0\0\0", 8); // EOF
421 bool GCOVProfiler::emitProfileArcs() {
422 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
423 if (!CU_Nodes) return false;
426 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
427 DICompileUnit CU(CU_Nodes->getOperand(i));
428 DIArray SPs = CU.getSubprograms();
429 SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
430 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
431 DISubprogram SP(SPs.getElement(i));
432 if (!SP.Verify()) continue;
433 Function *F = SP.getFunction();
435 if (!Result) Result = true;
437 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
438 TerminatorInst *TI = BB->getTerminator();
439 if (isa<ReturnInst>(TI))
442 Edges += TI->getNumSuccessors();
445 ArrayType *CounterTy =
446 ArrayType::get(Type::getInt64Ty(*Ctx), Edges);
447 GlobalVariable *Counters =
448 new GlobalVariable(*M, CounterTy, false,
449 GlobalValue::InternalLinkage,
450 Constant::getNullValue(CounterTy),
451 "__llvm_gcov_ctr", 0, false, 0);
452 CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP));
454 UniqueVector<BasicBlock *> ComplexEdgePreds;
455 UniqueVector<BasicBlock *> ComplexEdgeSuccs;
458 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
459 TerminatorInst *TI = BB->getTerminator();
460 int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
462 IRBuilder<> Builder(TI);
464 if (Successors == 1) {
465 Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
467 Value *Count = Builder.CreateLoad(Counter);
468 Count = Builder.CreateAdd(Count,
469 ConstantInt::get(Type::getInt64Ty(*Ctx),1));
470 Builder.CreateStore(Count, Counter);
471 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
472 Value *Sel = Builder.CreateSelect(
474 ConstantInt::get(Type::getInt64Ty(*Ctx), Edge),
475 ConstantInt::get(Type::getInt64Ty(*Ctx), Edge + 1));
476 SmallVector<Value *, 2> Idx;
477 Idx.push_back(Constant::getNullValue(Type::getInt64Ty(*Ctx)));
479 Value *Counter = Builder.CreateInBoundsGEP(Counters, Idx);
480 Value *Count = Builder.CreateLoad(Counter);
481 Count = Builder.CreateAdd(Count,
482 ConstantInt::get(Type::getInt64Ty(*Ctx),1));
483 Builder.CreateStore(Count, Counter);
485 ComplexEdgePreds.insert(BB);
486 for (int i = 0; i != Successors; ++i)
487 ComplexEdgeSuccs.insert(TI->getSuccessor(i));
493 if (!ComplexEdgePreds.empty()) {
494 GlobalVariable *EdgeTable =
495 buildEdgeLookupTable(F, Counters,
496 ComplexEdgePreds, ComplexEdgeSuccs);
497 GlobalVariable *EdgeState = getEdgeStateValue();
499 Type *Int32Ty = Type::getInt32Ty(*Ctx);
500 for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) {
501 IRBuilder<> Builder(ComplexEdgePreds[i+1]->getTerminator());
502 Builder.CreateStore(ConstantInt::get(Int32Ty, i), EdgeState);
504 for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) {
505 // call runtime to perform increment
506 BasicBlock *BB = ComplexEdgeSuccs[i+1];
507 BasicBlock::iterator InsertPt = BB->getFirstInsertionPt();
508 BasicBlock *Split = BB->splitBasicBlock(InsertPt);
509 InsertPt = BB->getFirstInsertionPt();
510 IRBuilder<> Builder(InsertPt);
511 Value *CounterPtrArray =
512 Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0,
513 i * ComplexEdgePreds.size());
515 // Build code to increment the counter.
516 incrementIndirectCounter(Builder, Split, EdgeState, CounterPtrArray);
520 insertCounterWriteout(CountersBySP);
525 // All edges with successors that aren't branches are "complex", because it
526 // requires complex logic to pick which counter to update.
527 GlobalVariable *GCOVProfiler::buildEdgeLookupTable(
529 GlobalVariable *Counters,
530 const UniqueVector<BasicBlock *> &Preds,
531 const UniqueVector<BasicBlock *> &Succs) {
532 // TODO: support invoke, threads. We rely on the fact that nothing can modify
533 // the whole-Module pred edge# between the time we set it and the time we next
534 // read it. Threads and invoke make this untrue.
536 // emit [(succs * preds) x i64*], logically [succ x [pred x i64*]].
537 Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
538 ArrayType *EdgeTableTy = ArrayType::get(
539 Int64PtrTy, Succs.size() * Preds.size());
541 Constant **EdgeTable = new Constant*[Succs.size() * Preds.size()];
542 Constant *NullValue = Constant::getNullValue(Int64PtrTy);
543 for (int i = 0, ie = Succs.size() * Preds.size(); i != ie; ++i)
544 EdgeTable[i] = NullValue;
547 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
548 TerminatorInst *TI = BB->getTerminator();
549 int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
550 if (Successors > 1 && !isa<BranchInst>(TI) && !isa<ReturnInst>(TI)) {
551 for (int i = 0; i != Successors; ++i) {
552 BasicBlock *Succ = TI->getSuccessor(i);
553 IRBuilder<> builder(Succ);
554 Value *Counter = builder.CreateConstInBoundsGEP2_64(Counters, 0,
556 EdgeTable[((Succs.idFor(Succ)-1) * Preds.size()) +
557 (Preds.idFor(BB)-1)] = cast<Constant>(Counter);
563 ArrayRef<Constant*> V(&EdgeTable[0], Succs.size() * Preds.size());
564 GlobalVariable *EdgeTableGV =
566 *M, EdgeTableTy, true, GlobalValue::InternalLinkage,
567 ConstantArray::get(EdgeTableTy, V),
568 "__llvm_gcda_edge_table");
569 EdgeTableGV->setUnnamedAddr(true);
573 Constant *GCOVProfiler::getStartFileFunc() {
574 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
575 Type::getInt8PtrTy(*Ctx), false);
576 return M->getOrInsertFunction("llvm_gcda_start_file", FTy);
579 /// incrementIndirectCounter - Emit code that increments the indirect
580 /// counter. The code is meant to copy the llvm_gcda_increment_indirect_counter
581 /// function, but because it's inlined into the function, we don't have to worry
582 /// about the runtime library possibly writing into a protected area.
583 void GCOVProfiler::incrementIndirectCounter(IRBuilder<> &Builder,
585 GlobalVariable *EdgeState,
586 Value *CounterPtrArray) {
587 Type *Int64Ty = Type::getInt64Ty(*Ctx);
588 ConstantInt *NegOne = ConstantInt::get(Type::getInt32Ty(*Ctx), 0xffffffff);
590 // Create exiting blocks.
591 BasicBlock *InsBB = Builder.GetInsertBlock();
592 Function *Fn = InsBB->getParent();
593 BasicBlock *PredNotNegOne = BasicBlock::Create(*Ctx, "", Fn, Exit);
594 BasicBlock *CounterEnd = BasicBlock::Create(*Ctx, "", Fn, Exit);
596 // uint32_t pred = *EdgeState;
597 // if (pred == 0xffffffff) return;
598 Value *Pred = Builder.CreateLoad(EdgeState, "predecessor");
599 Value *Cond = Builder.CreateICmpEQ(Pred, NegOne);
600 InsBB->getTerminator()->eraseFromParent();
601 BranchInst::Create(Exit, PredNotNegOne, Cond, InsBB);
603 Builder.SetInsertPoint(PredNotNegOne);
605 // uint64_t *counter = CounterPtrArray[pred];
606 // if (!counter) return;
607 Value *ZExtPred = Builder.CreateZExt(Pred, Int64Ty);
608 Value *GEP = Builder.CreateGEP(CounterPtrArray, ZExtPred);
609 Value *Counter = Builder.CreateLoad(GEP, "counter");
610 Cond = Builder.CreateICmpEQ(Counter,
611 Constant::getNullValue(Type::getInt64PtrTy(*Ctx, 0)));
612 Builder.CreateCondBr(Cond, Exit, CounterEnd);
614 Builder.SetInsertPoint(CounterEnd);
617 Value *Add = Builder.CreateAdd(Builder.CreateLoad(Counter),
618 ConstantInt::get(Int64Ty, 1));
619 Builder.CreateStore(Add, Counter);
620 Builder.CreateBr(Exit);
622 // Clear the predecessor number
623 Builder.SetInsertPoint(Exit->getFirstInsertionPt());
624 Builder.CreateStore(NegOne, EdgeState);
627 Constant *GCOVProfiler::getEmitFunctionFunc() {
629 Type::getInt32Ty(*Ctx), // uint32_t ident
630 Type::getInt8PtrTy(*Ctx), // const char *function_name
632 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
633 return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
636 Constant *GCOVProfiler::getEmitArcsFunc() {
638 Type::getInt32Ty(*Ctx), // uint32_t num_counters
639 Type::getInt64PtrTy(*Ctx), // uint64_t *counters
641 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
643 return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy);
646 Constant *GCOVProfiler::getEndFileFunc() {
647 FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
648 return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
651 GlobalVariable *GCOVProfiler::getEdgeStateValue() {
652 GlobalVariable *GV = M->getGlobalVariable("__llvm_gcov_global_state_pred");
654 GV = new GlobalVariable(*M, Type::getInt32Ty(*Ctx), false,
655 GlobalValue::InternalLinkage,
656 ConstantInt::get(Type::getInt32Ty(*Ctx),
658 "__llvm_gcov_global_state_pred");
659 GV->setUnnamedAddr(true);
664 void GCOVProfiler::insertCounterWriteout(
665 SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> &CountersBySP) {
666 FunctionType *WriteoutFTy =
667 FunctionType::get(Type::getVoidTy(*Ctx), false);
668 Function *WriteoutF = Function::Create(WriteoutFTy,
669 GlobalValue::InternalLinkage,
670 "__llvm_gcov_writeout", M);
671 WriteoutF->setUnnamedAddr(true);
672 BasicBlock *BB = BasicBlock::Create(*Ctx, "", WriteoutF);
673 IRBuilder<> Builder(BB);
675 Constant *StartFile = getStartFileFunc();
676 Constant *EmitFunction = getEmitFunctionFunc();
677 Constant *EmitArcs = getEmitArcsFunc();
678 Constant *EndFile = getEndFileFunc();
680 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
682 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
683 DICompileUnit compile_unit(CU_Nodes->getOperand(i));
684 std::string FilenameGcda = mangleName(compile_unit, "gcda");
685 Builder.CreateCall(StartFile,
686 Builder.CreateGlobalStringPtr(FilenameGcda));
687 for (SmallVector<std::pair<GlobalVariable *, MDNode *>, 8>::iterator
688 I = CountersBySP.begin(), E = CountersBySP.end();
690 DISubprogram SP(I->second);
691 intptr_t ident = reinterpret_cast<intptr_t>(I->second);
692 Builder.CreateCall2(EmitFunction,
693 ConstantInt::get(Type::getInt32Ty(*Ctx), ident),
694 Builder.CreateGlobalStringPtr(SP.getName()));
696 GlobalVariable *GV = I->first;
698 cast<ArrayType>(GV->getType()->getElementType())->getNumElements();
699 Builder.CreateCall2(EmitArcs,
700 ConstantInt::get(Type::getInt32Ty(*Ctx), Arcs),
701 Builder.CreateConstGEP2_64(GV, 0, 0));
703 Builder.CreateCall(EndFile);
706 Builder.CreateRetVoid();
708 InsertProfilingShutdownCall(WriteoutF, M);