[llvm] r343445 - [DebugInfo][Dexter] Incorrect DBG_VALUE after MCP dead copy instruction removal.

Carlos Alberto Enciso via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 1 01:14:44 PDT 2018


Author: carlos.alberto.enciso
Date: Mon Oct  1 01:14:44 2018
New Revision: 343445

URL: http://llvm.org/viewvc/llvm-project?rev=343445&view=rev
Log:
[DebugInfo][Dexter] Incorrect DBG_VALUE after MCP dead copy instruction removal.

When MachineCopyPropagation eliminates a dead 'copy', its associated debug information becomes invalid. as the recorded register has been removed.  It causes the debugger to display wrong variable value.

Differential Revision: https://reviews.llvm.org/D52614

Added:
    llvm/trunk/test/CodeGen/MIR/X86/pr38773.mir
Modified:
    llvm/trunk/include/llvm/CodeGen/MachineInstr.h
    llvm/trunk/lib/CodeGen/MachineCSE.cpp
    llvm/trunk/lib/CodeGen/MachineCopyPropagation.cpp
    llvm/trunk/lib/CodeGen/MachineInstr.cpp

Modified: llvm/trunk/include/llvm/CodeGen/MachineInstr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineInstr.h?rev=343445&r1=343444&r2=343445&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachineInstr.h (original)
+++ llvm/trunk/include/llvm/CodeGen/MachineInstr.h Mon Oct  1 01:14:44 2018
@@ -1544,6 +1544,10 @@ public:
   /// Scan instructions following MI and collect any matching DBG_VALUEs.
   void collectDebugValues(SmallVectorImpl<MachineInstr *> &DbgValues);
 
+  /// Find all DBG_VALUEs immediately following this instruction that point
+  /// to a register def in this instruction and point them to \p Reg instead.
+  void changeDebugValuesDefReg(unsigned Reg);
+
 private:
   /// If this instruction is embedded into a MachineFunction, return the
   /// MachineRegisterInfo object for the current function, otherwise

Modified: llvm/trunk/lib/CodeGen/MachineCSE.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineCSE.cpp?rev=343445&r1=343444&r2=343445&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineCSE.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineCSE.cpp Mon Oct  1 01:14:44 2018
@@ -181,12 +181,8 @@ bool MachineCSE::PerformTrivialCopyPropa
     LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI);
     LLVM_DEBUG(dbgs() << "***     to: " << *MI);
 
-    // Collect matching debug values.
-    SmallVector<MachineInstr *, 2> DbgValues;
-    DefMI->collectDebugValues(DbgValues);
-    // Propagate SrcReg to debug value instructions.
-    for (auto *DBI : DbgValues)
-      DBI->getOperand(0).setReg(SrcReg);
+    // Update matching debug values.
+    DefMI->changeDebugValuesDefReg(SrcReg);
 
     // Propagate SrcReg of copies to MI.
     MO.setReg(SrcReg);

Modified: llvm/trunk/lib/CodeGen/MachineCopyPropagation.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineCopyPropagation.cpp?rev=343445&r1=343444&r2=343445&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineCopyPropagation.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineCopyPropagation.cpp Mon Oct  1 01:14:44 2018
@@ -598,6 +598,11 @@ void MachineCopyPropagation::CopyPropaga
       LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
                  MaybeDead->dump());
       assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
+
+      // Update matching debug values.
+      assert(MaybeDead->isCopy());
+      MaybeDead->changeDebugValuesDefReg(MaybeDead->getOperand(1).getReg());
+
       MaybeDead->eraseFromParent();
       Changed = true;
       ++NumDeletes;

Modified: llvm/trunk/lib/CodeGen/MachineInstr.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineInstr.cpp?rev=343445&r1=343444&r2=343445&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineInstr.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineInstr.cpp Mon Oct  1 01:14:44 2018
@@ -2092,3 +2092,13 @@ void MachineInstr::collectDebugValues(
       DbgValues.push_back(&*DI);
   }
 }
+
+void MachineInstr::changeDebugValuesDefReg(unsigned Reg) {
+  // Collect matching debug values.
+  SmallVector<MachineInstr *, 2> DbgValues;
+  collectDebugValues(DbgValues);
+
+  // Propagate Reg to debug value instructions.
+  for (auto *DBI : DbgValues)
+    DBI->getOperand(0).setReg(Reg);
+}

Added: llvm/trunk/test/CodeGen/MIR/X86/pr38773.mir
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/MIR/X86/pr38773.mir?rev=343445&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/MIR/X86/pr38773.mir (added)
+++ llvm/trunk/test/CodeGen/MIR/X86/pr38773.mir Mon Oct  1 01:14:44 2018
@@ -0,0 +1,105 @@
+# RUN: llc -o - %s -mtriple=x86_64-- -run-pass=machine-cp | FileCheck %s
+
+# When MachineCopyPropagation eliminates a dead 'copy', its associated debug
+# information becomes invalid. as the recorded register has been removed.
+# It causes the debugger to display wrong variable value.
+#
+# When in the debugger, on the line "return read1;", the value of "read1"
+# is reported as '4', where it should be '1'.
+#
+# MIR generated with:
+#   clang -S -g -O2 -emit-llvm pr38773.cpp -o pr38773.ll -mllvm
+#   llc pr38773.ll -stop-after=tailduplication -simplify-mir 
+#
+# // pr38773.cpp
+# int main() {
+#   volatile int foo = 4;
+#   int read1 = foo;
+#   int read2 = foo;
+#
+#   switch ((read1 == 4) ? 3 : 1) {
+#   case 1:
+#     read1 *= read2;
+#     break;
+#   case 3:
+#     read1 /= read2;
+#     break;
+#   }
+#    
+#   return read1;
+# }
+#
+# Update the register for the '@llvm.dbg.value' associated with 'read1', when
+# the 'copy' is removed, to be the 'source' register.
+
+--- |
+  ; ModuleID = 'pr38773.ll'
+  source_filename = "pr38773.cpp"
+  target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+  target triple = "x86_64-pc-linux-gnu"
+  
+  define dso_local i32 @main() local_unnamed_addr !dbg !7 {
+  entry:
+    %foo = alloca i32, align 4
+    store volatile i32 4, i32* %foo, align 4
+    %foo.0.foo.0. = load volatile i32, i32* %foo, align 4
+    %foo.0.foo.0.6 = load volatile i32, i32* %foo, align 4
+    %cmp = icmp eq i32 %foo.0.foo.0., 4
+    br i1 %cmp, label %sw.bb1, label %sw.bb
+  
+  sw.bb:                                            ; preds = %entry
+    %mul = mul nsw i32 %foo.0.foo.0.6, %foo.0.foo.0.
+    br label %sw.epilog
+  
+  sw.bb1:                                           ; preds = %entry
+    %div = sdiv i32 4, %foo.0.foo.0.6
+    call void @llvm.dbg.value(metadata i32 %div, metadata !12, metadata !DIExpression()), !dbg !13
+    br label %sw.epilog
+  
+  sw.epilog:                                        ; preds = %sw.bb1, %sw.bb
+    %read1.0 = phi i32 [ %div, %sw.bb1 ], [ %mul, %sw.bb ]
+    call void @llvm.dbg.value(metadata i32 %read1.0, metadata !12, metadata !DIExpression()), !dbg !13
+    ret i32 %read1.0
+  }
+  
+  declare void @llvm.dbg.value(metadata, metadata, metadata) #0
+  
+  !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 343183)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
+  !1 = !DIFile(filename: "pr38773.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 343183)"}
+  !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}
+  !12 = !DILocalVariable(name: "read1", scope: !7, file: !1, line: 3, type: !10)
+  !13 = !DILocation(line: 3, column: 7, scope: !7)
+
+...
+---
+name:            main
+
+body:             |
+  
+  bb.2.sw.bb1:
+    liveins: $ecx
+  
+    $eax = MOV32ri 4
+    $edx = MOV32r0 implicit-def dead $eflags
+    IDIV32r killed renamable $ecx, implicit-def $eax, implicit-def dead $edx, implicit-def dead $eflags, implicit $eax, implicit killed $edx
+    renamable $ecx = COPY $eax
+    ; CHECK:        IDIV32r killed renamable $ecx
+    ; CHECK-NEXT:   DBG_VALUE debug-use $eax, debug-use $noreg, !12, !DIExpression(), debug-location !13
+    DBG_VALUE debug-use $ecx, debug-use $noreg, !12, !DIExpression(), debug-location !13
+    $eax = COPY killed renamable $ecx
+    RET 0, $eax
+
+...




More information about the llvm-commits mailing list