The problem occurs when removing duplicate dbg intrinsic as follows:
call void @llvm.dbg.value(metadata i32 0, metadata !22, metadata !DIExpression()), !dbg !31 call void @llvm.dbg.value(metadata i32 %0, metadata !22, metadata !DIExpression()), !dbg !31
The first dbg.value(.. i32 0) is removed by the backward scan in llvm::RemoveRedundantDbgInstrs.
The root cause is in EarlyCSE.
Before EarlyCSE, we have
call void @llvm.dbg.value(metadata i32 %0, metadata !21, metadata !DIExpression()), !dbg !31 call void @llvm.dbg.value(metadata i32 0, metadata !22, metadata !DIExpression()), !dbg !31 %1 = load i32, i32* @a, align 4, !dbg !32, !tbaa !27 call void @llvm.dbg.value(metadata i32 %1, metadata !22, metadata !DIExpression()), !dbg !31
When removing %1 = load i32, i32* @a, align 4, !dbg !32, !tbaa !27, we should salvage the debug info early, before replacing all uses with %0.
The IR after applying the patch:
call void @llvm.dbg.value(metadata i32 0, metadata !22, metadata !DIExpression()), !dbg !31 call void @llvm.dbg.value(metadata i32 undef, metadata !22, metadata !DIExpression()), !dbg !31
// Salvage ...
Does this fit on 80 columns?