[llvm] [CodeGen][RegCoalescer] Shrink non-PHI live-through subranges (PR #216297)

Vishwas Adiga via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 03:53:24 PDT 2026


https://github.com/Vishwas-Adiga updated https://github.com/llvm/llvm-project/pull/216297

>From b9e152ecf2ebec67ffe142d136c8e73928959ba6 Mon Sep 17 00:00:00 2001
From: Vishwas Adiga <vishwas.adiga at arm.com>
Date: Fri, 14 Aug 2026 09:20:26 +0100
Subject: [PATCH 1/3] [CodeGen][RegCoalescer] Shrink non-PHI live-through
 subranges

When an identity copy of a subrange is removed, ensure that it is
marked for shrinking even when it is a non-PHI definition.
---
 llvm/lib/CodeGen/RegisterCoalescer.cpp        |  8 ++---
 .../coalescer-shrink-nonphi-live-through.mir  | 35 +++++++++++++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir

diff --git a/llvm/lib/CodeGen/RegisterCoalescer.cpp b/llvm/lib/CodeGen/RegisterCoalescer.cpp
index fae504c7c9cbab..c69e82fe0e42f0 100644
--- a/llvm/lib/CodeGen/RegisterCoalescer.cpp
+++ b/llvm/lib/CodeGen/RegisterCoalescer.cpp
@@ -3365,11 +3365,11 @@ void JoinVals::pruneValues(JoinVals &Other,
   }
 }
 
-// Check if the segment consists of a copied live-through value (i.e. the copy
-// in the block only extended the liveness, of an undef value which we may need
-// to handle).
+// Check whether the subrange contains the same value before and after the
+// copy. Removing the copy can leave such a live-through subrange with a stale
+// endpoint, so it must be considered when shrinking the joined interval.
 static bool isLiveThrough(const LiveQueryResult Q) {
-  return Q.valueIn() && Q.valueIn()->isPHIDef() && Q.valueIn() == Q.valueOut();
+  return Q.valueIn() && Q.valueIn() == Q.valueOut();
 }
 
 /// Consider the following situation when coalescing the copy between
diff --git a/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir b/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir
new file mode 100644
index 00000000000000..de8ada650e8e9d
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir
@@ -0,0 +1,35 @@
+# RUN: llc -mtriple=aarch64 \
+# RUN:   -aarch64-enable-subreg-liveness-tracking \
+# RUN:   -run-pass=register-coalescer -debug-only=regalloc \
+# RUN:   -o /dev/null %s 2>&1 | FileCheck %s
+# REQUIRES: asserts
+
+# Check that removing an identity copy marks non-PHI live-through subranges for
+# shrinking.
+#
+# Both subranges are non-PHI and live through the removed COPY. Without
+# handling non-PHI values, neither subrange is added to ShrinkMask.
+
+# CHECK-LABEL: Function: nonphi_live_through
+# CHECK: Expecting instruction removal
+# CHECK-COUNT-2: Dead uses at sublane
+
+---
+name: nonphi_live_through
+tracksRegLiveness: true
+body: |
+  bb.0:
+    successors: %bb.1
+    liveins: $d0
+
+    undef %0.dsub0:dd = COPY $d0
+    %0.dsub1:dd = IMPLICIT_DEF
+    B %bb.1
+
+  bb.1:
+    %1:dd = COPY %0
+    HINT 0, implicit %1.dsub1
+    HINT 0, implicit %1.dsub0
+    RET_ReallyLR
+
+...
\ No newline at end of file

>From ee09fe167cdb75be2366d1f19f7a0372fd6d9f1f Mon Sep 17 00:00:00 2001
From: Vishwas Adiga <vishwas.adiga at arm.com>
Date: Fri, 14 Aug 2026 11:28:23 +0100
Subject: [PATCH 2/3] [CodeGen][RegCoalescer] Fix newline at end of test

---
 .../CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir b/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir
index de8ada650e8e9d..a9c7cb7cd0db44 100644
--- a/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir
+++ b/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir
@@ -32,4 +32,4 @@ body: |
     HINT 0, implicit %1.dsub0
     RET_ReallyLR
 
-...
\ No newline at end of file
+...

>From c19d20427e6d16d63a88a6412d709ebc586216b4 Mon Sep 17 00:00:00 2001
From: Vishwas Adiga <Vishwas.Adiga at arm.com>
Date: Mon, 21 Sep 2026 11:53:12 +0100
Subject: [PATCH 3/3] Update
 llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir

Co-authored-by: Matt Arsenault <arsenm2 at gmail.com>
---
 .../CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir b/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir
index a9c7cb7cd0db44..706149f4c74248 100644
--- a/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir
+++ b/llvm/test/CodeGen/AArch64/coalescer-shrink-nonphi-live-through.mir
@@ -1,7 +1,7 @@
 # RUN: llc -mtriple=aarch64 \
 # RUN:   -aarch64-enable-subreg-liveness-tracking \
 # RUN:   -run-pass=register-coalescer -debug-only=regalloc \
-# RUN:   -o /dev/null %s 2>&1 | FileCheck %s
+# RUN:   -filetype=null %s 2>&1 | FileCheck %s
 # REQUIRES: asserts
 
 # Check that removing an identity copy marks non-PHI live-through subranges for



More information about the llvm-commits mailing list