Index: lib/Transforms/Instrumentation/GCOVProfiling.cpp =================================================================== --- lib/Transforms/Instrumentation/GCOVProfiling.cpp +++ lib/Transforms/Instrumentation/GCOVProfiling.cpp @@ -637,7 +637,12 @@ int Successors = isa(TI) ? 1 : TI->getNumSuccessors(); if (Successors) { if (Successors == 1) { - IRBuilder<> Builder(&*BB.getFirstInsertionPt()); + BasicBlock::iterator insertPtr = BB.getFirstInsertionPt(); + if (insertPtr == BB.end()) { + continue; + } + + IRBuilder<> Builder(&*insertPtr); Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Edge); Value *Count = Builder.CreateLoad(Counter); @@ -670,13 +675,24 @@ GlobalVariable *EdgeState = getEdgeStateValue(); for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) { - IRBuilder<> Builder(&*ComplexEdgePreds[i + 1]->getFirstInsertionPt()); + auto *BB = ComplexEdgePreds[i + 1]; + BasicBlock::iterator insertPtr = BB->getFirstInsertionPt(); + if (insertPtr == BB->end()) { + continue; + } + IRBuilder<> Builder(&*insertPtr); Builder.CreateStore(Builder.getInt32(i), EdgeState); } for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) { + auto *BB = ComplexEdgeSuccs[i + 1]; + BasicBlock::iterator insertPtr = BB->getFirstInsertionPt(); + if (insertPtr == BB->end()) { + continue; + } + // Call runtime to perform increment. - IRBuilder<> Builder(&*ComplexEdgeSuccs[i + 1]->getFirstInsertionPt()); + IRBuilder<> Builder(&*insertPtr); Value *CounterPtrArray = Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0, i * ComplexEdgePreds.size());