Index: include/llvm/Transforms/Utils/Local.h =================================================================== --- include/llvm/Transforms/Utils/Local.h +++ include/llvm/Transforms/Utils/Local.h @@ -443,6 +443,9 @@ void copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, MDNode *N, LoadInst &NewLI); +/// Remove the debug intrinsic instructions for the given machine instruction. +void dropDebugUsers(Instruction &I); + //===----------------------------------------------------------------------===// // Intrinsic pattern matching // Index: lib/Transforms/Utils/Local.cpp =================================================================== --- lib/Transforms/Utils/Local.cpp +++ lib/Transforms/Utils/Local.cpp @@ -2522,6 +2522,13 @@ } } +void llvm::dropDebugUsers(Instruction &I) { + SmallVector DbgUsers; + findDbgUsers(DbgUsers, &I); + for (auto *DII : DbgUsers) + DII->eraseFromParent(); +} + namespace { /// A potential constituent of a bitreverse or bswap expression. See Index: lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- lib/Transforms/Utils/SimplifyCFG.cpp +++ lib/Transforms/Utils/SimplifyCFG.cpp @@ -2370,15 +2370,19 @@ // Move all 'aggressive' instructions, which are defined in the // conditional parts of the if's up to the dominating block. if (IfBlock1) { - for (auto &I : *IfBlock1) + for (auto &I : *IfBlock1) { I.dropUnknownNonDebugMetadata(); + dropDebugUsers(I); + } DomBlock->getInstList().splice(InsertPt->getIterator(), IfBlock1->getInstList(), IfBlock1->begin(), IfBlock1->getTerminator()->getIterator()); } if (IfBlock2) { - for (auto &I : *IfBlock2) + for (auto &I : *IfBlock2) { I.dropUnknownNonDebugMetadata(); + dropDebugUsers(I); + } DomBlock->getInstList().splice(InsertPt->getIterator(), IfBlock2->getInstList(), IfBlock2->begin(), IfBlock2->getTerminator()->getIterator()); Index: test/CodeGen/X86/pr38763.ll =================================================================== --- test/CodeGen/X86/pr38763.ll +++ test/CodeGen/X86/pr38763.ll @@ -0,0 +1,137 @@ +; RUN: opt %s -S -simplifycfg | FileCheck %s + +; When SimplifyCFG changes the PHI node into a select instruction, the debug +; information becomes ambiguous. It causes the debugger to display wrong +; variable value. +; +; When in the debugger, on the line "if (read == 4)", the value of "result" +; is reported as '2', where it should be zero. + +; IR generated with: +; clang -S -g -O2 -emit-llvm pr38763.cpp -o pr38763.ll -mllvm -opt-bisect-limit=10 + +; // pr38763.cpp +; int main() { +; volatile int foo = 4; +; int read = foo; +; int read1 = foo; +; +; int result = 0; +; if (read == 4) { +; result = read1 + 2; +; } else { +; result = read1 - 2; +; } +; +; return result; +; } + +; Remove the '@llvm.dbg.value' associated with 'result' for the true/false +; branches, as they becomes ambiguous. + +; CHECK-LABEL: entry +; CHECK: %cmp = icmp eq i32 %foo.0., 4, !dbg !28 +; CHECK: %add = add nsw i32 %foo.0.4, 2, !dbg !30 +; CHECK: %sub = add nsw i32 %foo.0.4, -2, !dbg !32 +; CHECK: %result.0 = select i1 %cmp, i32 %add, i32 %sub, !dbg !34 +; CHECK: call void @llvm.dbg.value(metadata i32 %result.0, metadata !16, metadata !DIExpression()), !dbg !27 +; CHECK: call void @llvm.lifetime.end.p0i8(i64 4, i8* %foo.0..sroa_cast), !dbg !35 +; CHECK: ret i32 %result.0, !dbg !36 + +; ModuleID = 'pr38763.cpp' +source_filename = "pr38763.cpp" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-linux-gnu" + +; Function Attrs: norecurse nounwind uwtable +define dso_local i32 @main() local_unnamed_addr #0 !dbg !7 { +entry: + %foo = alloca i32, align 4 + %foo.0..sroa_cast = bitcast i32* %foo to i8*, !dbg !17 + call void @llvm.lifetime.start.p0i8(i64 4, i8* %foo.0..sroa_cast), !dbg !17 + call void @llvm.dbg.declare(metadata i32* %foo, metadata !12, metadata !DIExpression()), !dbg !18 + store volatile i32 4, i32* %foo, align 4, !dbg !18, !tbaa !19 + %foo.0. = load volatile i32, i32* %foo, align 4, !dbg !23, !tbaa !19 + call void @llvm.dbg.value(metadata i32 %foo.0., metadata !14, metadata !DIExpression()), !dbg !24 + %foo.0.4 = load volatile i32, i32* %foo, align 4, !dbg !25, !tbaa !19 + call void @llvm.dbg.value(metadata i32 %foo.0.4, metadata !15, metadata !DIExpression()), !dbg !26 + call void @llvm.dbg.value(metadata i32 0, metadata !16, metadata !DIExpression()), !dbg !27 + %cmp = icmp eq i32 %foo.0., 4, !dbg !28 + br i1 %cmp, label %if.then, label %if.else, !dbg !30 + +if.then: ; preds = %entry + %add = add nsw i32 %foo.0.4, 2, !dbg !31 + call void @llvm.dbg.value(metadata i32 %add, metadata !16, metadata !DIExpression()), !dbg !27 + br label %if.end, !dbg !33 + +if.else: ; preds = %entry + %sub = add nsw i32 %foo.0.4, -2, !dbg !34 + call void @llvm.dbg.value(metadata i32 %sub, metadata !16, metadata !DIExpression()), !dbg !27 + br label %if.end + +if.end: ; preds = %if.else, %if.then + %result.0 = phi i32 [ %add, %if.then ], [ %sub, %if.else ], !dbg !36 + call void @llvm.dbg.value(metadata i32 %result.0, metadata !16, metadata !DIExpression()), !dbg !27 + call void @llvm.lifetime.end.p0i8(i64 4, i8* %foo.0..sroa_cast), !dbg !37 + ret i32 %result.0, !dbg !38 +} + +; Function Attrs: argmemonly nounwind +declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) #1 + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.declare(metadata, metadata, metadata) #2 + +; Function Attrs: argmemonly nounwind +declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #1 + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.value(metadata, metadata, metadata) #2 + +attributes #0 = { norecurse nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { argmemonly nounwind } +attributes #2 = { nounwind readnone speculatable } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 8.0.0 (trunk 342209)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None) +!1 = !DIFile(filename: "pr38763.cpp", directory: ".") +!2 = !{} +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 8.0.0 (trunk 342209)"} +!7 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !11) +!8 = !DISubroutineType(types: !9) +!9 = !{!10} +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !{!12, !14, !15, !16} +!12 = !DILocalVariable(name: "foo", scope: !7, file: !1, line: 2, type: !13) +!13 = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: !10) +!14 = !DILocalVariable(name: "read", scope: !7, file: !1, line: 3, type: !10) +!15 = !DILocalVariable(name: "read1", scope: !7, file: !1, line: 4, type: !10) +!16 = !DILocalVariable(name: "result", scope: !7, file: !1, line: 6, type: !10) +!17 = !DILocation(line: 2, column: 3, scope: !7) +!18 = !DILocation(line: 2, column: 16, scope: !7) +!19 = !{!20, !20, i64 0} +!20 = !{!"int", !21, i64 0} +!21 = !{!"omnipotent char", !22, i64 0} +!22 = !{!"Simple C++ TBAA"} +!23 = !DILocation(line: 3, column: 14, scope: !7) +!24 = !DILocation(line: 3, column: 7, scope: !7) +!25 = !DILocation(line: 4, column: 15, scope: !7) +!26 = !DILocation(line: 4, column: 7, scope: !7) +!27 = !DILocation(line: 6, column: 7, scope: !7) +!28 = !DILocation(line: 7, column: 12, scope: !29) +!29 = distinct !DILexicalBlock(scope: !7, file: !1, line: 7, column: 7) +!30 = !DILocation(line: 7, column: 7, scope: !7) +!31 = !DILocation(line: 8, column: 20, scope: !32) +!32 = distinct !DILexicalBlock(scope: !29, file: !1, line: 7, column: 18) +!33 = !DILocation(line: 9, column: 3, scope: !32) +!34 = !DILocation(line: 10, column: 20, scope: !35) +!35 = distinct !DILexicalBlock(scope: !29, file: !1, line: 9, column: 10) +!36 = !DILocation(line: 0, scope: !29) +!37 = !DILocation(line: 14, column: 1, scope: !7) +!38 = !DILocation(line: 13, column: 3, scope: !7)