[llvm] [RISCV] Correctly account for the copy cost of GPR pairs in RISCVMakeCompressible. (PR #141251)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri May 23 09:42:25 PDT 2025


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/141251

GPR pairs require 2 ADDIs to copy, so we need to be updating more instructions to get a benefit.

>From adbbc571764c3316decff642215b228e6d41ef14 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Fri, 23 May 2025 09:24:24 -0700
Subject: [PATCH 1/2] Pre-commit test case

---
 .../CodeGen/RISCV/make-compressible-zilsd.mir | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir b/llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir
index c5ac599d8d53f..8ffd103ac87a9 100644
--- a/llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir
+++ b/llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir
@@ -62,6 +62,13 @@
     ret void
   }
 
+  define void @store_common_value_double_no_opt2(ptr %a, i32 %b, double %c, double %d, double %e) #0 {
+  entry:
+    store volatile double %e, ptr %a, align 8
+    store volatile double %e, ptr %a, align 8
+    ret void
+  }
+
   define void @store_common_ptr_double_no_opt(double %a, i32 %b, i32 %c, i32 %d, i32 %e, ptr %p) #0 {
   entry:
     store volatile double %a, ptr %p, align 8
@@ -216,6 +223,26 @@ body:             |
     SD_RV32 killed renamable $x16_x17, killed renamable $x10, 0 :: (store (s64) into %ir.a)
     PseudoRET
 
+...
+---
+name:            store_common_value_double_no_opt2
+tracksRegLiveness: true
+body:             |
+  bb.0.entry:
+    liveins: $x10, $x16, $x17
+
+    ; RV32-LABEL: name: store_common_value_double_no_opt2
+    ; RV32: liveins: $x10, $x16, $x17
+    ; RV32-NEXT: {{  $}}
+    ; RV32-NEXT: $x12 = ADDI $x16, 0
+    ; RV32-NEXT: $x13 = ADDI $x17, 0
+    ; RV32-NEXT: SD_RV32 $x12_x13, renamable $x10, 0 :: (volatile store (s64) into %ir.a)
+    ; RV32-NEXT: SD_RV32 killed $x12_x13, killed renamable $x10, 0 :: (volatile store (s64) into %ir.a)
+    ; RV32-NEXT: PseudoRET
+    SD_RV32 renamable $x16_x17, renamable $x10, 0 :: (volatile store (s64) into %ir.a)
+    SD_RV32 killed renamable $x16_x17, killed renamable $x10, 0 :: (volatile store (s64) into %ir.a)
+    PseudoRET
+
 ...
 ---
 name:            store_common_ptr_double_no_opt

>From 4f7c9a892acaff720977056a0fd60cfa691f4012 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Fri, 23 May 2025 09:26:00 -0700
Subject: [PATCH 2/2] [RISCV] Correctly account for the copy cost of GPR pairs
 in RISCVMakeCompressible.

---
 llvm/lib/Target/RISCV/RISCVMakeCompressible.cpp     | 8 +++++---
 llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir | 6 ++----
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVMakeCompressible.cpp b/llvm/lib/Target/RISCV/RISCVMakeCompressible.cpp
index 1e2bdb10aa810..54e959b59010f 100644
--- a/llvm/lib/Target/RISCV/RISCVMakeCompressible.cpp
+++ b/llvm/lib/Target/RISCV/RISCVMakeCompressible.cpp
@@ -332,9 +332,11 @@ static Register analyzeCompressibleUses(MachineInstr &FirstMI,
   // are required for a code size reduction. If no base adjustment is required,
   // then copying the register costs one new c.mv (or c.li Rd, 0 for "copying"
   // the zero register) and therefore two uses are required for a code size
-  // reduction.
-  if (MIs.size() < 2 || (RegImm.Imm != 0 && MIs.size() < 3))
-    return RISCV::NoRegister;
+  // reduction. For GPR pairs, we need 2 ADDIs to copy so we need three users.
+  unsigned CopyCost = RISCV::GPRPairRegClass.contains(RegImm.Reg) ? 2 : 1;
+  assert((RegImm.Imm == 0 || CopyCost == 1) && "GPRPair should have zero imm");
+  if (MIs.size() <= CopyCost || (RegImm.Imm != 0 && MIs.size() <= 2))
+    return Register();
 
   // Find a compressible register which will be available from the first
   // instruction we care about to the last.
diff --git a/llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir b/llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir
index 8ffd103ac87a9..930501dcb378e 100644
--- a/llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir
+++ b/llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir
@@ -234,10 +234,8 @@ body:             |
     ; RV32-LABEL: name: store_common_value_double_no_opt2
     ; RV32: liveins: $x10, $x16, $x17
     ; RV32-NEXT: {{  $}}
-    ; RV32-NEXT: $x12 = ADDI $x16, 0
-    ; RV32-NEXT: $x13 = ADDI $x17, 0
-    ; RV32-NEXT: SD_RV32 $x12_x13, renamable $x10, 0 :: (volatile store (s64) into %ir.a)
-    ; RV32-NEXT: SD_RV32 killed $x12_x13, killed renamable $x10, 0 :: (volatile store (s64) into %ir.a)
+    ; RV32-NEXT: SD_RV32 renamable $x16_x17, renamable $x10, 0 :: (volatile store (s64) into %ir.a)
+    ; RV32-NEXT: SD_RV32 killed renamable $x16_x17, killed renamable $x10, 0 :: (volatile store (s64) into %ir.a)
     ; RV32-NEXT: PseudoRET
     SD_RV32 renamable $x16_x17, renamable $x10, 0 :: (volatile store (s64) into %ir.a)
     SD_RV32 killed renamable $x16_x17, killed renamable $x10, 0 :: (volatile store (s64) into %ir.a)



More information about the llvm-commits mailing list