Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions llvm/lib/Target/RISCV/RISCVMakeCompressible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -216,6 +223,24 @@ 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: 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)
PseudoRET

...
---
name: store_common_ptr_double_no_opt
Expand Down