Index: llvm/lib/Transforms/Scalar/EarlyCSE.cpp =================================================================== --- llvm/lib/Transforms/Scalar/EarlyCSE.cpp +++ llvm/lib/Transforms/Scalar/EarlyCSE.cpp @@ -1136,8 +1136,12 @@ LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n"); continue; } - if (!Inst.use_empty()) + if (!Inst.use_empty()) { + // Salvage debug info early to be compatible with + // removeRedundantDbgInstrsUsingBackwardScan() used in SimplfyCFG. + salvageDebugInfoOrMarkUndef(Inst); Inst.replaceAllUsesWith(Op); + } salvageKnowledge(&Inst, &AC); removeMSSA(Inst); Inst.eraseFromParent(); Index: llvm/test/DebugInfo/X86/earlycse-load-marks-undef.ll =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/earlycse-load-marks-undef.ll @@ -0,0 +1,65 @@ +; RUN: opt -mtriple=x86_64-- %s -S --early-cse -o - | FileCheck %s +; Ensure that the second dbg.value is undef as RemoveRedundantDbgInstrs +; will remove the first dbg.value. +; Bugzilla #46114 + +@a = dso_local global i32 1, align 4, !dbg !0 +@c = dso_local global i8 0, align 1, !dbg !9 +@b = dso_local global i16 0, align 2, !dbg !6 + +define dso_local void @main() #0 !dbg !15 { +entry: + %0 = load i32, i32* @a, align 4 + call void @llvm.dbg.value(metadata i32 0, metadata !20, metadata !DIExpression()), !dbg !24 + %1 = load i32, i32* @a, align 4, !dbg !25, !tbaa !26 +; CHECK: call void @llvm.dbg.value(metadata i32 undef + call void @llvm.dbg.value(metadata i32 %1, metadata !20, metadata !DIExpression()), !dbg !24 + %sub = sub nsw i32 %1, 0 + %conv1 = trunc i32 %sub to i16 + store i16 %conv1, i16* @b, align 2, !dbg !30, !tbaa !31 + %tobool = icmp ne i32 %0, 0 + unreachable +} + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #1 + +attributes #0 = { "use-soft-float"="false" } +attributes #1 = { nounwind readnone speculatable willreturn } + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!13, !14} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 1, type: !12, isLocal: false, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 11.0.0 (https://github.com/llvm/llvm-project.git 20c9bb44ec1a4a795215ff6964d264219f9b05f2)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None) +!3 = !DIFile(filename: "earlycse-load-marks-undef.ll", directory: "/temp/bz46114") +!4 = !{} +!5 = !{!0, !6, !9} +!6 = !DIGlobalVariableExpression(var: !7, expr: !DIExpression()) +!7 = distinct !DIGlobalVariable(name: "b", scope: !2, file: !3, line: 2, type: !8, isLocal: false, isDefinition: true) +!8 = !DIBasicType(name: "short", size: 16, encoding: DW_ATE_signed) +!9 = !DIGlobalVariableExpression(var: !10, expr: !DIExpression()) +!10 = distinct !DIGlobalVariable(name: "c", scope: !2, file: !3, line: 3, type: !11, isLocal: false, isDefinition: true) +!11 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char) +!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!13 = !{i32 2, !"Debug Info Version", i32 3} +!14 = !{i32 1, !"wchar_size", i32 4} +!15 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 4, type: !16, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !18) +!16 = !DISubroutineType(types: !17) +!17 = !{!12} +!18 = !{!19, !20, !21} +!19 = !DILocalVariable(name: "d", scope: !15, file: !3, line: 5, type: !12) +!20 = !DILocalVariable(name: "l_52", scope: !15, file: !3, line: 5, type: !12) +!21 = !DILocalVariable(name: "e", scope: !22, file: !3, line: 8, type: !12) +!22 = distinct !DILexicalBlock(scope: !23, file: !3, line: 7, column: 10) +!23 = distinct !DILexicalBlock(scope: !15, file: !3, line: 7, column: 7) +!24 = !DILocation(line: 0, scope: !15) +!25 = !DILocation(line: 6, column: 15, scope: !15) +!26 = !{!27, !27, i64 0} +!27 = !{!"int", !28, i64 0} +!28 = !{!"omnipotent char", !29, i64 0} +!29 = !{!"Simple C/C++ TBAA"} +!30 = !DILocation(line: 6, column: 5, scope: !15) +!31 = !{!32, !32, i64 0} +!32 = !{!"short", !28, i64 0}