diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 8b5965ba45a6d..df0c85b87ba60 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -2994,6 +2994,8 @@ LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) { LLVMDbgRecordRef LLVMGetFirstDbgRecord(LLVMValueRef Inst) { Instruction *Instr = unwrap(Inst); + if (!Instr->DebugMarker) + return nullptr; auto I = Instr->DebugMarker->StoredDbgRecords.begin(); if (I == Instr->DebugMarker->StoredDbgRecords.end()) return nullptr; @@ -3002,6 +3004,8 @@ LLVMDbgRecordRef LLVMGetFirstDbgRecord(LLVMValueRef Inst) { LLVMDbgRecordRef LLVMGetLastDbgRecord(LLVMValueRef Inst) { Instruction *Instr = unwrap(Inst); + if (!Instr->DebugMarker) + return nullptr; auto I = Instr->DebugMarker->StoredDbgRecords.rbegin(); if (I == Instr->DebugMarker->StoredDbgRecords.rend()) return nullptr; diff --git a/llvm/tools/llvm-c-test/debuginfo.c b/llvm/tools/llvm-c-test/debuginfo.c index 0f09c74a476bb..e376d82009be0 100644 --- a/llvm/tools/llvm-c-test/debuginfo.c +++ b/llvm/tools/llvm-c-test/debuginfo.c @@ -325,6 +325,13 @@ int llvm_test_dibuilder(void) { LLVMValueRef Phi2 = LLVMBuildPhi(Builder, I64, "p2"); LLVMAddIncoming(Phi2, &Zero, &FooEntryBlock, 1); + // Test that LLVMGetFirstDbgRecord and LLVMGetLastDbgRecord return NULL for + // instructions without debug info. + LLVMDbgRecordRef Phi1FirstDbgRecord = LLVMGetFirstDbgRecord(Phi1); + assert(Phi1FirstDbgRecord == NULL); + LLVMDbgRecordRef Phi1LastDbgRecord = LLVMGetLastDbgRecord(Phi1); + assert(Phi1LastDbgRecord == NULL); + // Insert a non-phi before the `ret` but not before the debug records to // test that works as expected. LLVMPositionBuilder(Builder, FooVarBlock, Ret);