[llvm] [RISCV] Correct RISCVTTIImpl::getIntImmCostInst for Zba (PR #128174)

Gergely Futo via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 25 00:54:40 PST 2025


https://github.com/futog updated https://github.com/llvm/llvm-project/pull/128174

>From d995599899aff80e8a0e6fd58cef39deb8bfb1eb Mon Sep 17 00:00:00 2001
From: Gergely Futo <gergely.futo at hightec-rt.com>
Date: Fri, 21 Feb 2025 14:25:54 +0100
Subject: [PATCH] [RISCV] Correct RISCVTTIImpl::getIntImmCostInst for Zba

zext.w is only available on RV64.

We also never hoist UINT64_C(0xffffffff) on RV32, since the AND is
deleted by SelectionDAG after type legalization splits it.
---
 .../Target/RISCV/RISCVTargetTransformInfo.cpp   |  3 ++-
 .../ConstantHoisting/RISCV/immediates.ll        | 17 +++++++++++------
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 79e3b9ee09744..4707cb5f728fe 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -207,7 +207,8 @@ InstructionCost RISCVTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
     if (Imm == UINT64_C(0xffff) && ST->hasStdExtZbb())
       return TTI::TCC_Free;
     // zext.w
-    if (Imm == UINT64_C(0xffffffff) && ST->hasStdExtZba())
+    if (Imm == UINT64_C(0xffffffff) &&
+        ((ST->hasStdExtZba() && ST->isRV64()) || ST->isRV32()))
       return TTI::TCC_Free;
     // bclri
     if (ST->hasStdExtZbs() && (~Imm).isPowerOf2())
diff --git a/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll b/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll
index 329281e7dc301..a244bb101fd0a 100644
--- a/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll
+++ b/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll
@@ -1,5 +1,5 @@
-; RUN: opt -mtriple=riscv32-unknown-elf -S -passes=consthoist < %s | FileCheck %s
-; RUN: opt -mtriple=riscv64-unknown-elf -S -passes=consthoist < %s | FileCheck %s
+; RUN: opt -mtriple=riscv32-unknown-elf -S -passes=consthoist < %s | FileCheck %s -check-prefixes=CHECK,RV32I
+; RUN: opt -mtriple=riscv64-unknown-elf -S -passes=consthoist < %s | FileCheck %s -check-prefixes=CHECK,RV64I
 
 ; Check that we don't hoist immediates with small values.
 define i64 @test1(i64 %a) nounwind {
@@ -55,16 +55,21 @@ define i32 @test6(i32 %a) nounwind "target-features"="+zbb" {
   ret i32 %2
 }
 
-; Check that we hoist zext.w without Zba.
+; Check that we hoist zext.w without Zba on RV64.
+; Check that we don't hoist on RV32.
 define i64 @test7(i64 %a) nounwind {
-; CHECK-LABEL: test7
-; CHECK: %const = bitcast i64 4294967295 to i64
+; RV32I-LABEL: test7
+; RV32I: and i64 %a, 4294967295
+
+; RV64I-LABEL: test7
+; RV64I: %const = bitcast i64 4294967295 to i64
   %1 = and i64 %a, 4294967295
   %2 = and i64 %1, 4294967295
   ret i64 %2
 }
 
-; Check that we don't hoist zext.w with Zba.
+; Check that we don't hoist zext.w with Zba on RC64.
+; Check that we don't hoist on RV32.
 define i64 @test8(i64 %a) nounwind "target-features"="+zba" {
 ; CHECK-LABEL: test8
 ; CHECK: and i64 %a, 4294967295



More information about the llvm-commits mailing list