[llvm] [RISC-V] Only emit multiples of 16 as immediate for cm.push (PR #84935)

Nemanja Ivanovic via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 08:54:46 PDT 2024


https://github.com/nemanjai updated https://github.com/llvm/llvm-project/pull/84935

>From f6efe6f1ee42b47cca9c36c6ded0d1712ab7a3f1 Mon Sep 17 00:00:00 2001
From: Nemanja Ivanovic <nemanja at synopsys.com>
Date: Tue, 12 Mar 2024 16:48:03 +0100
Subject: [PATCH 1/2] [RISC-V] Only emit multiples of 16 as immediate for
 cm.push

An immediate that isn't a multiple of 16 is meaningless and
we should always emit the next higher multiple. Otherwise
the value is simply truncated of course which leads to
clobbering CSR's on the stack.
---
 llvm/lib/Target/RISCV/RISCVFrameLowering.cpp     | 2 +-
 llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
index 8bac41372b5a83..4f3d915818d1ae 100644
--- a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
@@ -555,7 +555,7 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
       FirstFrameSetup->getOpcode() == RISCV::CM_PUSH) {
     // Use available stack adjustment in push instruction to allocate additional
     // stack space.
-    uint64_t Spimm = std::min(StackSize, (uint64_t)48);
+    uint64_t Spimm = alignTo(std::min(StackSize, (uint64_t)48), 16);
     FirstFrameSetup->getOperand(1).setImm(Spimm);
     StackSize -= Spimm;
   }
diff --git a/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll b/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll
index e5c2e0180ee0a6..738d1b07e2210c 100644
--- a/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll
+++ b/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll
@@ -3,7 +3,8 @@
 define ptr @func(ptr %s, i32 %_c, ptr %incdec.ptr, i1 %0, i8 %conv14) #0 {
 ; RV32-LABEL: func:
 ; RV32:       # %bb.0: # %entry
-; RV32-NEXT:    cm.push {ra, s0-s1}, -24
+; RV32-NEXT:    cm.push {ra, s0-s1}, -32
+; RV32-NEXT:    addi sp, sp, 8
 ; RV32-NEXT:    .cfi_def_cfa_offset 24
 ; RV32-NEXT:    .cfi_offset ra, -12
 ; RV32-NEXT:    .cfi_offset s0, -8

>From 7a5cb38d3c3b68215fcf69b3d36cdd280c2ca2f3 Mon Sep 17 00:00:00 2001
From: Nemanja Ivanovic <nemanja at synopsys.com>
Date: Tue, 12 Mar 2024 16:54:00 +0100
Subject: [PATCH 2/2] Remove the unnecessary stack adjustment (as it's wrong).

---
 llvm/lib/Target/RISCV/RISCVFrameLowering.cpp     | 2 +-
 llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
index 4f3d915818d1ae..ded712ae4251f9 100644
--- a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
@@ -557,7 +557,7 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
     // stack space.
     uint64_t Spimm = alignTo(std::min(StackSize, (uint64_t)48), 16);
     FirstFrameSetup->getOperand(1).setImm(Spimm);
-    StackSize -= Spimm;
+    StackSize -= std::min(StackSize, Spimm);
   }
 
   if (StackSize != 0) {
diff --git a/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll b/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll
index 738d1b07e2210c..c0eb92bed60970 100644
--- a/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll
+++ b/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll
@@ -4,7 +4,6 @@ define ptr @func(ptr %s, i32 %_c, ptr %incdec.ptr, i1 %0, i8 %conv14) #0 {
 ; RV32-LABEL: func:
 ; RV32:       # %bb.0: # %entry
 ; RV32-NEXT:    cm.push {ra, s0-s1}, -32
-; RV32-NEXT:    addi sp, sp, 8
 ; RV32-NEXT:    .cfi_def_cfa_offset 24
 ; RV32-NEXT:    .cfi_offset ra, -12
 ; RV32-NEXT:    .cfi_offset s0, -8



More information about the llvm-commits mailing list