Skip to content

Commit c11e3da

Browse files
authored
[RISCV] Correct RISCVTTIImpl::getIntImmCostInst for Zba (#128174)
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.
1 parent b38fdfc commit c11e3da

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ InstructionCost RISCVTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
207207
if (Imm == UINT64_C(0xffff) && ST->hasStdExtZbb())
208208
return TTI::TCC_Free;
209209
// zext.w
210-
if (Imm == UINT64_C(0xffffffff) && ST->hasStdExtZba())
210+
if (Imm == UINT64_C(0xffffffff) &&
211+
((ST->hasStdExtZba() && ST->isRV64()) || ST->isRV32()))
211212
return TTI::TCC_Free;
212213
// bclri
213214
if (ST->hasStdExtZbs() && (~Imm).isPowerOf2())

llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
; RUN: opt -mtriple=riscv32-unknown-elf -S -passes=consthoist < %s | FileCheck %s
2-
; RUN: opt -mtriple=riscv64-unknown-elf -S -passes=consthoist < %s | FileCheck %s
1+
; RUN: opt -mtriple=riscv32-unknown-elf -S -passes=consthoist < %s | FileCheck %s -check-prefixes=CHECK,RV32I
2+
; RUN: opt -mtriple=riscv64-unknown-elf -S -passes=consthoist < %s | FileCheck %s -check-prefixes=CHECK,RV64I
33

44
; Check that we don't hoist immediates with small values.
55
define i64 @test1(i64 %a) nounwind {
@@ -55,16 +55,21 @@ define i32 @test6(i32 %a) nounwind "target-features"="+zbb" {
5555
ret i32 %2
5656
}
5757

58-
; Check that we hoist zext.w without Zba.
58+
; Check that we hoist zext.w without Zba on RV64.
59+
; Check that we don't hoist on RV32.
5960
define i64 @test7(i64 %a) nounwind {
60-
; CHECK-LABEL: test7
61-
; CHECK: %const = bitcast i64 4294967295 to i64
61+
; RV32I-LABEL: test7
62+
; RV32I: and i64 %a, 4294967295
63+
64+
; RV64I-LABEL: test7
65+
; RV64I: %const = bitcast i64 4294967295 to i64
6266
%1 = and i64 %a, 4294967295
6367
%2 = and i64 %1, 4294967295
6468
ret i64 %2
6569
}
6670

67-
; Check that we don't hoist zext.w with Zba.
71+
; Check that we don't hoist zext.w with Zba on RV64.
72+
; Check that we don't hoist on RV32.
6873
define i64 @test8(i64 %a) nounwind "target-features"="+zba" {
6974
; CHECK-LABEL: test8
7075
; CHECK: and i64 %a, 4294967295

0 commit comments

Comments
 (0)