Index: lib/Transforms/Instrumentation/GCOVProfiling.cpp =================================================================== --- lib/Transforms/Instrumentation/GCOVProfiling.cpp +++ lib/Transforms/Instrumentation/GCOVProfiling.cpp @@ -515,16 +515,6 @@ return isScopedEHPersonality(Personality); } -static bool shouldKeepInEntry(BasicBlock::iterator It) { - if (isa(*It)) return true; - if (isa(*It)) return true; - if (auto *II = dyn_cast(It)) { - if (II->getIntrinsicID() == llvm::Intrinsic::localescape) return true; - } - - return false; -} - void GCOVProfiler::emitProfileNotes() { NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu"); if (!CU_Nodes) return; @@ -558,14 +548,6 @@ // TODO: Functions using scope-based EH are currently not supported. if (isUsingScopeBasedEH(F)) continue; - // gcov expects every function to start with an entry block that has a - // single successor, so split the entry block to make sure of that. - BasicBlock &EntryBlock = F.getEntryBlock(); - BasicBlock::iterator It = EntryBlock.begin(); - while (shouldKeepInEntry(It)) - ++It; - EntryBlock.splitBasicBlock(It); - Funcs.push_back(make_unique(SP, &F, &out, FunctionIdent++, Options.UseCfgChecksum, Options.ExitBlockBeforeBody)); @@ -573,13 +555,13 @@ for (auto &BB : F) { GCOVBlock &Block = Func.getBlock(&BB); - TerminatorInst *TI = BB.getTerminator(); - if (int successors = TI->getNumSuccessors()) { - for (int i = 0; i != successors; ++i) { - Block.addEdge(Func.getBlock(TI->getSuccessor(i))); - } - } else if (isa(TI)) { + if (succ_begin(&BB) == succ_end(&BB)) { + // No successors. Block.addEdge(Func.getReturnBlock()); + } else { + for (BasicBlock *Succ : successors(&BB)) { + Block.addEdge(Func.getBlock(Succ)); + } } uint32_t Line = 0; @@ -640,11 +622,11 @@ DenseMap, unsigned> EdgeToCounter; unsigned Edges = 0; for (auto &BB : F) { - TerminatorInst *TI = BB.getTerminator(); - if (isa(TI)) { + if (succ_begin(&BB) == succ_end(&BB)) { + // No successors. EdgeToCounter[{&BB, nullptr}] = Edges++; } else { - for (BasicBlock *Succ : successors(TI)) { + for (BasicBlock *Succ : successors(&BB)) { EdgeToCounter[{&BB, Succ}] = Edges++; } } @@ -683,18 +665,18 @@ Value *Count = Builder.CreateLoad(Phi); Count = Builder.CreateAdd(Count, Builder.getInt64(1)); Builder.CreateStore(Count, Phi); + } - TerminatorInst *TI = BB.getTerminator(); - if (isa(TI)) { - auto It = EdgeToCounter.find({&BB, nullptr}); - assert(It != EdgeToCounter.end()); - const unsigned Edge = It->second; - Value *Counter = - Builder.CreateConstInBoundsGEP2_64(Counters, 0, Edge); - Value *Count = Builder.CreateLoad(Counter); - Count = Builder.CreateAdd(Count, Builder.getInt64(1)); - Builder.CreateStore(Count, Counter); - } + if (succ_begin(&BB) == succ_end(&BB)) { + IRBuilder<> Builder(&*BB.getFirstInsertionPt()); + auto It = EdgeToCounter.find({&BB, nullptr}); + assert(It != EdgeToCounter.end()); + const unsigned Edge = It->second; + Value *Counter = + Builder.CreateConstInBoundsGEP2_64(Counters, 0, Edge); + Value *Count = Builder.CreateLoad(Counter); + Count = Builder.CreateAdd(Count, Builder.getInt64(1)); + Builder.CreateStore(Count, Counter); } } } Index: test/Transforms/GCOVProfiling/Inputs/empty-function-1.ll =================================================================== --- /dev/null +++ test/Transforms/GCOVProfiling/Inputs/empty-function-1.ll @@ -0,0 +1,3 @@ +define void @empty1() { + ret void +} Index: test/Transforms/GCOVProfiling/Inputs/empty-function-2.ll =================================================================== --- /dev/null +++ test/Transforms/GCOVProfiling/Inputs/empty-function-2.ll @@ -0,0 +1,3 @@ +define void @empty2() { + ret void +} Index: test/Transforms/GCOVProfiling/multiple-cus.ll =================================================================== --- /dev/null +++ test/Transforms/GCOVProfiling/multiple-cus.ll @@ -0,0 +1,5 @@ +; RUN: opt -debugify %S/Inputs/empty-function-1.ll -o %t.1.bc +; RUN: opt -debugify %S/Inputs/empty-function-2.ll -o %t.2.bc +; RUN: llvm-link %t.1.bc %t.2.bc -o %t.lto.bc +; RUN: opt -S -insert-gcov-profiling %t.lto.bc -o - +; TODO: Check that the IR is correct. Index: test/Transforms/GCOVProfiling/return-block.ll =================================================================== --- test/Transforms/GCOVProfiling/return-block.ll +++ test/Transforms/GCOVProfiling/return-block.ll @@ -78,7 +78,7 @@ ; RETURN-LAST: Destination Edges ; RETURN-SECOND-NOT: Destination Edges ; CHECK: Block : 2 Counter : 0 -; CHECK: Block : 4 Counter : 0 +; CHECK: Block : 3 Counter : 0 ; RETURN-LAST-NOT: Destination Edges ; RETURN-SECOND: Destination Edges ; CHECK-NOT: Block :