[llvm] f7bee65 - [TailDuplicator] Don't constrain register classes due to debug instructions

Mikael Holmen via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 25 23:21:56 PDT 2023


Author: Mikael Holmen
Date: 2023-04-26T08:17:42+02:00
New Revision: f7bee65728becbfbf82ba46fd0726f13f43b0e33

URL: https://github.com/llvm/llvm-project/commit/f7bee65728becbfbf82ba46fd0726f13f43b0e33
DIFF: https://github.com/llvm/llvm-project/commit/f7bee65728becbfbf82ba46fd0726f13f43b0e33.diff

LOG: [TailDuplicator] Don't constrain register classes due to debug instructions

If cloning a DBG_VALUE instruction, register uses in that instruction could
lead to constraining of a virtual register that would not happen if the
DBG_VALUE was not present at all. This lead to different code with/without
debug info.

Now we only do that register class constraining if we dealing with a non
debug instruction.

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

Added: 
    llvm/test/CodeGen/AArch64/tail-duplicate-debug-variance.mir

Modified: 
    llvm/lib/CodeGen/TailDuplicator.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/TailDuplicator.cpp b/llvm/lib/CodeGen/TailDuplicator.cpp
index 93c2252978391..2ba2ea717b2c8 100644
--- a/llvm/lib/CodeGen/TailDuplicator.cpp
+++ b/llvm/lib/CodeGen/TailDuplicator.cpp
@@ -427,7 +427,13 @@ void TailDuplicator::duplicateInstruction(
           } else {
             // For mapped registers that do not have sub-registers, simply
             // restrict their class to match the original one.
-            ConstrRC = MRI->constrainRegClass(VI->second.Reg, OrigRC);
+
+            // We don't want debug instructions affecting the resulting code so
+            // if we're cloning a debug instruction then just use MappedRC
+            // rather than constraining the register class further.
+            ConstrRC = NewMI.isDebugInstr()
+                           ? MappedRC
+                           : MRI->constrainRegClass(VI->second.Reg, OrigRC);
           }
 
           if (ConstrRC) {

diff  --git a/llvm/test/CodeGen/AArch64/tail-duplicate-debug-variance.mir b/llvm/test/CodeGen/AArch64/tail-duplicate-debug-variance.mir
new file mode 100644
index 0000000000000..8787d85aa3e44
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/tail-duplicate-debug-variance.mir
@@ -0,0 +1,95 @@
+# RUN: llc %s -o - -run-pass=early-tailduplication | FileCheck %s --check-prefixes=CHECK,DEBUG
+# RUN: llc %s -o - -run-pass=mir-strip-debug,early-tailduplication -mir-strip-debugify-only=0 | FileCheck %s --check-prefix=CHECK
+
+# We should get the same result with and without debug information.
+# Specifically, the register class of [[COPY]] should not be changed to
+# gpr32common with debug info while staying as gpr32sp without.
+
+--- |
+  target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
+  target triple = "aarch64-none-linux-gnu"
+
+  define void @test() !dbg !8 {
+  entry:
+    ret void
+  }
+
+  !llvm.dbg.cu = !{!0}
+  !llvm.module.flags = !{!2, !3, !4, !5}
+  !llvm.ident = !{!6}
+
+  !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "My compiler", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
+  !1 = !DIFile(filename: "foo.c", directory: "/bar")
+  !2 = !{i32 7, !"Dwarf Version", i32 4}
+  !3 = !{i32 2, !"Debug Info Version", i32 3}
+  !4 = !{i32 1, !"wchar_size", i32 1}
+  !5 = !{i32 7, !"frame-pointer", i32 2}
+  !6 = !{!"My compiler"}
+  !7 = !DILocalVariable(name: "i", scope: !8, file: !1, line: 6, type: !12)
+  !8 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 5, type: !9, scopeLine: 5, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
+  !9 = !DISubroutineType(types: !10)
+  !10 = !{null}
+  !11 = !{!7}
+  !12 = !DIBasicType(name: "int", size: 16, encoding: DW_ATE_signed)
+  !13 = !DILocation(line: 0, scope: !8)
+...
+---
+name:            test
+tracksRegLiveness: true
+liveins:         []
+body:             |
+  ; CHECK-LABEL: name: test
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.2(0x40000000), %bb.1(0x40000000)
+  ; CHECK-NEXT:   liveins: $w0, $wzr
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   [[COPY:%[0-9]+]]:gpr32sp = COPY $w0
+  ; CHECK-NEXT:   CBNZW $wzr, %bb.2
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   successors: %bb.4(0x80000000)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   $x0 = COPY undef $x0
+  ; DEBUG-NEXT:   DBG_VALUE [[COPY]], $noreg, !11, !DIExpression(), debug-location !DILocation(line: 0, scope: !7)
+  ; CHECK-NEXT:   [[COPY1:%[0-9]+]]:gpr32 = COPY [[COPY]]
+  ; CHECK-NEXT:   B %bb.4
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.2:
+  ; CHECK-NEXT:   successors: %bb.4(0x80000000)
+  ; CHECK-NEXT: {{  $}}
+  ; DEBUG-NEXT:   DBG_VALUE [[COPY]], $noreg, !11, !DIExpression(), debug-location !DILocation(line: 0, scope: !7)
+  ; CHECK-NEXT:   [[COPY2:%[0-9]+]]:gpr32 = COPY [[COPY]]
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.4:
+  ; CHECK-NEXT:   RET undef $lr, implicit undef $w0
+  bb.0:
+    successors: %bb.2, %bb.1
+    liveins: $w0, $wzr
+
+    %1:gpr32sp = COPY $w0
+    CBNZW $wzr, %bb.2
+
+  bb.1:
+    successors: %bb.3
+
+    $x0 = COPY undef $x0
+    B %bb.3
+
+  bb.2:
+    successors: %bb.3
+
+  bb.3:
+    successors: %bb.5
+
+    %2:gpr32 = PHI %1, %bb.1, %1, %bb.2
+    DBG_VALUE %2, $noreg, !7, !DIExpression(), debug-location !13
+    B %bb.5
+
+  bb.4:
+    RET undef $lr, implicit undef $w0
+
+  bb.5:
+    successors: %bb.4
+
+    B %bb.4
+...


        


More information about the llvm-commits mailing list