[llvm] [RISCV] Let LiveIntervals::shrinkToUses compute dead AVLs (PR #90629)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Sun May 5 23:30:59 PDT 2024


https://github.com/lukel97 updated https://github.com/llvm/llvm-project/pull/90629

>From ad80ea4b49b64f1f75e079157bdd3c73dbd6dc10 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Wed, 1 May 2024 01:19:56 +0800
Subject: [PATCH 1/2] [RISCV] Let LiveIntervals::shrinkToUses compute dead
 immediate. NFC

We can simplify removing dead AVL immediates > 31 by using the dead argument to shrinkToUses, since it will already compute dead values.
---
 llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp    | 17 ++++++-----------
 .../test/CodeGen/RISCV/rvv/coalesce-vsetvli.mir |  6 ++----
 2 files changed, 8 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
index 216dc78085204e..bdd273c6686f6f 100644
--- a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
@@ -1648,17 +1648,9 @@ bool RISCVCoalesceVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) {
           if (NextMI->getOperand(1).isReg())
             NextMI->getOperand(1).setReg(RISCV::NoRegister);
 
-          if (OldVLReg && OldVLReg.isVirtual()) {
-            // NextMI no longer uses OldVLReg so shrink its LiveInterval.
-            LIS->shrinkToUses(&LIS->getInterval(OldVLReg));
-
-            MachineInstr *VLOpDef = MRI->getUniqueVRegDef(OldVLReg);
-            if (VLOpDef && TII->isAddImmediate(*VLOpDef, OldVLReg) &&
-                MRI->use_nodbg_empty(OldVLReg)) {
-              VLOpDef->eraseFromParent();
-              LIS->removeInterval(OldVLReg);
-            }
-          }
+          // NextMI no longer uses OldVLReg so shrink its LiveInterval.
+          if (OldVLReg && OldVLReg.isVirtual())
+            LIS->shrinkToUses(&LIS->getInterval(OldVLReg), &ToDelete);
           MI.setDesc(NextMI->getDesc());
         }
         MI.getOperand(2).setImm(NextMI->getOperand(2).getImm());
@@ -1674,6 +1666,9 @@ bool RISCVCoalesceVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) {
   for (auto *MI : ToDelete) {
     LIS->RemoveMachineInstrFromMaps(*MI);
     MI->eraseFromParent();
+    for (MachineOperand &MO : MI->uses())
+      if (MO.isReg() && MO.getReg().isVirtual())
+        LIS->shrinkToUses(&LIS->getInterval(MO.getReg()));
   }
 
   return !ToDelete.empty();
diff --git a/llvm/test/CodeGen/RISCV/rvv/coalesce-vsetvli.mir b/llvm/test/CodeGen/RISCV/rvv/coalesce-vsetvli.mir
index f888534ebc035d..b3bc281b6ea3d0 100644
--- a/llvm/test/CodeGen/RISCV/rvv/coalesce-vsetvli.mir
+++ b/llvm/test/CodeGen/RISCV/rvv/coalesce-vsetvli.mir
@@ -27,8 +27,7 @@ body:             |
     ; CHECK-LABEL: name: dead_avl_nonvolatile_load
     ; CHECK: liveins: $x1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: %ptr:gpr = COPY $x1
-    ; CHECK-NEXT: dead %avl:gprnox0 = LW %ptr, 0 :: (dereferenceable load (s32))
+    ; CHECK-NEXT: dead %ptr:gpr = COPY $x1
     ; CHECK-NEXT: $x0 = PseudoVSETIVLI 3, 216 /* e64, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
     ; CHECK-NEXT: dead %x:gpr = PseudoVMV_X_S $noreg, 6 /* e64 */
     ; CHECK-NEXT: $v0 = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, 3, 6 /* e64 */, 0 /* tu, mu */
@@ -50,8 +49,7 @@ body:             |
     ; CHECK-LABEL: name: dead_avl_volatile_load
     ; CHECK: liveins: $x1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: %ptr:gpr = COPY $x1
-    ; CHECK-NEXT: dead %avl:gprnox0 = LW %ptr, 0 :: (volatile dereferenceable load (s32))
+    ; CHECK-NEXT: dead %ptr:gpr = COPY $x1
     ; CHECK-NEXT: $x0 = PseudoVSETIVLI 3, 216 /* e64, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
     ; CHECK-NEXT: dead %x:gpr = PseudoVMV_X_S $noreg, 6 /* e64 */
     ; CHECK-NEXT: $v0 = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, 3, 6 /* e64 */, 0 /* tu, mu */

>From 983ceefd7287fb92b67be8bb8fc7e0b9784c7f71 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Mon, 6 May 2024 14:28:01 +0800
Subject: [PATCH 2/2] Check dead MIs are safe to delete

---
 llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp     | 10 +++++++++-
 llvm/test/CodeGen/RISCV/rvv/coalesce-vsetvli.mir |  3 ++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
index bdd273c6686f6f..0667f2e9d34771 100644
--- a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
@@ -1649,8 +1649,16 @@ bool RISCVCoalesceVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) {
             NextMI->getOperand(1).setReg(RISCV::NoRegister);
 
           // NextMI no longer uses OldVLReg so shrink its LiveInterval.
+          SmallVector<MachineInstr *> DeadMIs;
           if (OldVLReg && OldVLReg.isVirtual())
-            LIS->shrinkToUses(&LIS->getInterval(OldVLReg), &ToDelete);
+            LIS->shrinkToUses(&LIS->getInterval(OldVLReg), &DeadMIs);
+          for (MachineInstr *DeadMI : DeadMIs) {
+            bool SawStore = false;
+            if (DeadMI->isSafeToMove(nullptr, SawStore) &&
+                !DeadMI->isBundled() && !DeadMI->isInlineAsm())
+              ToDelete.push_back(DeadMI);
+          }
+
           MI.setDesc(NextMI->getDesc());
         }
         MI.getOperand(2).setImm(NextMI->getOperand(2).getImm());
diff --git a/llvm/test/CodeGen/RISCV/rvv/coalesce-vsetvli.mir b/llvm/test/CodeGen/RISCV/rvv/coalesce-vsetvli.mir
index b3bc281b6ea3d0..6d931425958489 100644
--- a/llvm/test/CodeGen/RISCV/rvv/coalesce-vsetvli.mir
+++ b/llvm/test/CodeGen/RISCV/rvv/coalesce-vsetvli.mir
@@ -49,7 +49,8 @@ body:             |
     ; CHECK-LABEL: name: dead_avl_volatile_load
     ; CHECK: liveins: $x1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: dead %ptr:gpr = COPY $x1
+    ; CHECK-NEXT: %ptr:gpr = COPY $x1
+    ; CHECK-NEXT: dead %avl:gprnox0 = LW %ptr, 0 :: (volatile dereferenceable load (s32))
     ; CHECK-NEXT: $x0 = PseudoVSETIVLI 3, 216 /* e64, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
     ; CHECK-NEXT: dead %x:gpr = PseudoVMV_X_S $noreg, 6 /* e64 */
     ; CHECK-NEXT: $v0 = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, 3, 6 /* e64 */, 0 /* tu, mu */



More information about the llvm-commits mailing list