diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp index 6b0348f8f691f..b497951a598cc 100644 --- a/llvm/lib/IR/Instruction.cpp +++ b/llvm/lib/IR/Instruction.cpp @@ -887,7 +887,8 @@ Instruction::getPrevNonDebugInstruction(bool SkipPseudoOp) const { const DebugLoc &Instruction::getStableDebugLoc() const { if (isa(this)) - return getNextNonDebugInstruction()->getDebugLoc(); + if (const Instruction *Next = getNextNonDebugInstruction()) + return Next->getDebugLoc(); return getDebugLoc(); } diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp index a22c7be73f49f..84ed73333416c 100644 --- a/llvm/unittests/IR/DebugInfoTest.cpp +++ b/llvm/unittests/IR/DebugInfoTest.cpp @@ -566,6 +566,19 @@ 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); + auto IP = BB->getFirstInsertionPt(); + Builder.SetInsertPoint(BB.get(), IP); +} + TEST(AssignmentTrackingTest, InstrMethods) { // Test the assignment tracking Instruction methods. // This includes: