diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp index 6b0348f8f691f..a4982f14222b9 100644 --- a/llvm/lib/IR/Instruction.cpp +++ b/llvm/lib/IR/Instruction.cpp @@ -886,8 +886,15 @@ Instruction::getPrevNonDebugInstruction(bool SkipPseudoOp) const { } const DebugLoc &Instruction::getStableDebugLoc() const { - if (isa(this)) - return getNextNonDebugInstruction()->getDebugLoc(); + if (isa(this)) { + if (const Instruction* Next = getNextNonDebugInstruction()) { + return Next->getDebugLoc(); + } + + // FIXME: remove, just to demonstrate the test causes Next above to be null. + llvm::errs() << "error: getStableDebugLoc:getNextNonDebugInstruction is NULL\n"; + exit(1); + } return getDebugLoc(); } diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp index a22c7be73f49f..3a91bd39d1bde 100644 --- a/llvm/unittests/IR/DebugInfoTest.cpp +++ b/llvm/unittests/IR/DebugInfoTest.cpp @@ -566,6 +566,24 @@ TEST(AssignmentTrackingTest, Utils) { EXPECT_FALSE(at::getAssignmentMarkers(&Fun2Alloca).empty()); } +TEST(IRBuilder, GetSetInsertionPointWithEmptyBasicBlock) { + LLVMContext C; + std::unique_ptr BB(BasicBlock::Create(C, "start")); + Module *M = new Module("module", C); + IRBuilder<> Builder(BB.get()); + Function *DbgDeclare = Intrinsic::getDeclaration(M, Intrinsic::dbg_declare); + Value *DIV = MetadataAsValue::get(C, (Metadata *)nullptr); + SmallVector Args = {DIV, DIV, DIV}; + Builder.CreateCall(DbgDeclare, Args); + + // FIXME: remove the dump(), just for debugging. + M->dump(); + BB->dump(); + + auto IP = BB->getFirstInsertionPt(); + Builder.SetInsertPoint(BB.get(), IP); +} + TEST(AssignmentTrackingTest, InstrMethods) { // Test the assignment tracking Instruction methods. // This includes: