Index: llvm/include/llvm/IR/Metadata.h =================================================================== --- llvm/include/llvm/IR/Metadata.h +++ llvm/include/llvm/IR/Metadata.h @@ -302,7 +302,8 @@ /// /// Replace all uses of this with \c MD, which is allowed to be null. void replaceAllUsesWith(Metadata *MD); - + /// Replace all uses of the constant with Undef in debug info metadata + static void SalvageDebugInfo(const Constant &C); /// Returns the list of all DIArgList users of this. SmallVector getAllArgListUsers(); Index: llvm/lib/IR/Constants.cpp =================================================================== --- llvm/lib/IR/Constants.cpp +++ llvm/lib/IR/Constants.cpp @@ -740,9 +740,12 @@ else ++I; } - - if (RemoveDeadUsers) + if (RemoveDeadUsers) { + // If C is only used by metadata, it should not be preserved but should + // have its uses replaced. + ReplaceableMetadataImpl::SalvageDebugInfo(*C); const_cast(C)->destroyConstant(); + } return true; } Index: llvm/lib/IR/Metadata.cpp =================================================================== --- llvm/lib/IR/Metadata.cpp +++ llvm/lib/IR/Metadata.cpp @@ -245,6 +245,36 @@ "Reference without owner must be direct"); } +void ReplaceableMetadataImpl::SalvageDebugInfo(const Constant &C) { + if (!C.isUsedByMetadata()) { + return; + } + + LLVMContext &Context = C.getType()->getContext(); + auto &Store = Context.pImpl->ValuesAsMetadata; + auto I = Store.find(&C); + ValueAsMetadata *MD = I->second; + using UseTy = + std::pair>; + // Copy out uses and update value of Constant used by debug info metadata with undef below + SmallVector Uses(MD->UseMap.begin(), MD->UseMap.end()); + + for (const auto &Pair : Uses) { + MetadataTracking::OwnerTy Owner = Pair.second.first; + if (!Owner) + continue; + if (!Owner.is()) + continue; + auto *OwnerMD = dyn_cast(Owner.get()); + if (!OwnerMD) + continue; + if (isa(OwnerMD)) { + OwnerMD->handleChangedOperand( + Pair.first, ValueAsMetadata::get(UndefValue::get(C.getType()))); + } + } +} + void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) { if (UseMap.empty()) return; Index: llvm/test/DebugInfo/X86/undef-type-md.ll =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/undef-type-md.ll @@ -0,0 +1,38 @@ +; RUN: opt -S -ipsccp %S/undef-type-md.ll | FileCheck %s +; CHECK: llvm.nondebug.metadata = !{[[NONDEBUG_METADATA:![0-9]+]]} +; CHECK: [[NONDEBUG_METADATA]] = distinct !{null} +; CHECK: !DITemplateValueParameter({{.*}} value: %class.1 addrspace(1)* undef) + +; ModuleID = '' +source_filename = "test.cpp" + +%"struct.1" = type <{ float, i32, i8, [3 x i8] }> +%"class.1" = type { %"struct.1" } + +@extern_const = external addrspace(1) constant { { float, i32, i8 } } + +; Function Attrs: convergent mustprogress norecurse +define linkonce_odr spir_func void @foo() #0 align 2 !dbg !5 { +entry: + %0 = alloca %"class.1", align 8 + ret void +} + +attributes #0 = { nofree nosync nounwind readnone speculatable willreturn } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4} +!llvm.nondebug.metadata= !{!11} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, emissionKind: FullDebug) +!1 = !DIFile(filename: "test.cpp", directory: "/path/to") +!2 = !{} +!3 = !{i32 7, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 27, type: !6, scopeLine: 27, spFlags: DISPFlagDefinition, unit: !0, templateParams: !8) +!6 = !DISubroutineType(types: !7) +!7 = !{null} +!8 = !{!9} +!9 = !DITemplateValueParameter(name: "S", type: !10, value: %"class.1" addrspace(1)* bitcast ({ { float, i32, i8 } } addrspace(1)* @extern_const to %"class.1" addrspace(1)*)) +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !{%"class.1" addrspace(1)* bitcast ({ { float, i32, i8 } } addrspace(1)* @extern_const to %"class.1" addrspace(1)*)}