Skip to content

Commit f702389

Browse files
committed
[RISCV] Add zext.h/zext.w to RISCVTTIImpl::getIntImmCostInst.
If we have these instructions, we don't need to hoist the immediate for an AND that would match them. Reviewed By: luismarques Differential Revision: https://reviews.llvm.org/D107783
1 parent 7557d6c commit f702389

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,15 @@ InstructionCost RISCVTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
5252
// split up large offsets in GEP into better parts than ConstantHoisting
5353
// can.
5454
return TTI::TCC_Free;
55-
case Instruction::Add:
5655
case Instruction::And:
56+
// zext.h
57+
if (Imm == UINT64_C(0xffff) && ST->hasStdExtZbb())
58+
return TTI::TCC_Free;
59+
// zext.w
60+
if (Imm == UINT64_C(0xffffffff) && ST->hasStdExtZbb())
61+
return TTI::TCC_Free;
62+
LLVM_FALLTHROUGH;
63+
case Instruction::Add:
5764
case Instruction::Or:
5865
case Instruction::Xor:
5966
case Instruction::Mul:

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,39 @@ define i128 @test4(i128 %a) nounwind {
3636
%2 = add i128 %1, 12297829382473034410122878
3737
ret i128 %2
3838
}
39+
40+
; Check that we hoist zext.h without Zbb.
41+
define i32 @test5(i32 %a) nounwind {
42+
; CHECK-LABEL: test5
43+
; CHECK: %const = bitcast i32 65535 to i32
44+
%1 = and i32 %a, 65535
45+
%2 = and i32 %1, 65535
46+
ret i32 %2
47+
}
48+
49+
; Check that we don't hoist zext.h with 65535 with Zbb.
50+
define i32 @test6(i32 %a) nounwind "target-features"="+experimental-zbb" {
51+
; CHECK-LABEL: test6
52+
; CHECK: and i32 %a, 65535
53+
%1 = and i32 %a, 65535
54+
%2 = and i32 %1, 65535
55+
ret i32 %2
56+
}
57+
58+
; Check that we hoist zext.w without Zba.
59+
define i64 @test7(i64 %a) nounwind {
60+
; CHECK-LABEL: test7
61+
; CHECK: %const = bitcast i64 4294967295 to i64
62+
%1 = and i64 %a, 4294967295
63+
%2 = and i64 %1, 4294967295
64+
ret i64 %2
65+
}
66+
67+
; Check that we don't hoist zext.w with Zba.
68+
define i64 @test8(i64 %a) nounwind "target-features"="+experimental-zbb" {
69+
; CHECK-LABEL: test8
70+
; CHECK: and i64 %a, 4294967295
71+
%1 = and i64 %a, 4294967295
72+
%2 = and i64 %1, 4294967295
73+
ret i64 %2
74+
}

0 commit comments

Comments
 (0)