Skip to content

Commit b7f6c6e

Browse files
toppercarichardson
authored andcommitted
[RISCV] Add regalloc hints for Zcb instructions. (#78949)
This hints the register allocator to use the same register for source and destination to enable more compression.
2 parents fefa968 + d360963 commit b7f6c6e

File tree

2 files changed

+120
-6
lines changed

2 files changed

+120
-6
lines changed

llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ bool RISCVRegisterInfo::getRegAllocationHints(
817817
SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF,
818818
const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
819819
const MachineRegisterInfo *MRI = &MF.getRegInfo();
820+
auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
820821

821822
bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
822823
VirtReg, Order, Hints, MF, VRM, Matrix);
@@ -844,7 +845,7 @@ bool RISCVRegisterInfo::getRegAllocationHints(
844845

845846
// This is all of the compressible binary instructions. If an instruction
846847
// needs GPRC register class operands \p NeedGPRC will be set to true.
847-
auto isCompressible = [](const MachineInstr &MI, bool &NeedGPRC) {
848+
auto isCompressible = [&Subtarget](const MachineInstr &MI, bool &NeedGPRC) {
848849
NeedGPRC = false;
849850
switch (MI.getOpcode()) {
850851
default:
@@ -857,9 +858,16 @@ bool RISCVRegisterInfo::getRegAllocationHints(
857858
case RISCV::SUBW:
858859
NeedGPRC = true;
859860
return true;
860-
case RISCV::ANDI:
861+
case RISCV::ANDI: {
861862
NeedGPRC = true;
862-
return MI.getOperand(2).isImm() && isInt<6>(MI.getOperand(2).getImm());
863+
if (!MI.getOperand(2).isImm())
864+
return false;
865+
int64_t Imm = MI.getOperand(2).getImm();
866+
if (isInt<6>(Imm))
867+
return true;
868+
// c.zext.b
869+
return Subtarget.hasStdExtZcb() && Imm == 255;
870+
}
863871
case RISCV::SRAI:
864872
case RISCV::SRLI:
865873
NeedGPRC = true;
@@ -870,6 +878,24 @@ bool RISCVRegisterInfo::getRegAllocationHints(
870878
case RISCV::ADDI:
871879
case RISCV::ADDIW:
872880
return MI.getOperand(2).isImm() && isInt<6>(MI.getOperand(2).getImm());
881+
case RISCV::MUL:
882+
case RISCV::SEXT_B:
883+
case RISCV::SEXT_H:
884+
case RISCV::ZEXT_H_RV32:
885+
case RISCV::ZEXT_H_RV64:
886+
// c.mul, c.sext.b, c.sext.h, c.zext.h
887+
NeedGPRC = true;
888+
return Subtarget.hasStdExtZcb();
889+
case RISCV::ADD_UW:
890+
// c.zext.w
891+
NeedGPRC = true;
892+
return Subtarget.hasStdExtZcb() && MI.getOperand(2).isReg() &&
893+
MI.getOperand(2).getReg() == RISCV::X0;
894+
case RISCV::XORI:
895+
// c.not
896+
NeedGPRC = true;
897+
return Subtarget.hasStdExtZcb() && MI.getOperand(2).isImm() &&
898+
MI.getOperand(2).getImm() == -1;
873899
}
874900
};
875901

@@ -891,13 +917,15 @@ bool RISCVRegisterInfo::getRegAllocationHints(
891917
bool NeedGPRC;
892918
if (isCompressible(MI, NeedGPRC)) {
893919
if (OpIdx == 0 && MI.getOperand(1).isReg()) {
894-
if (!NeedGPRC || isCompressibleOpnd(MI.getOperand(2)))
920+
if (!NeedGPRC || MI.getNumExplicitOperands() < 3 ||
921+
MI.getOpcode() == RISCV::ADD_UW ||
922+
isCompressibleOpnd(MI.getOperand(2)))
895923
tryAddHint(MO, MI.getOperand(1), NeedGPRC);
896924
if (MI.isCommutable() && MI.getOperand(2).isReg() &&
897925
(!NeedGPRC || isCompressibleOpnd(MI.getOperand(1))))
898926
tryAddHint(MO, MI.getOperand(2), NeedGPRC);
899-
} else if (OpIdx == 1 &&
900-
(!NeedGPRC || isCompressibleOpnd(MI.getOperand(2)))) {
927+
} else if (OpIdx == 1 && (!NeedGPRC || MI.getNumExplicitOperands() < 3 ||
928+
isCompressibleOpnd(MI.getOperand(2)))) {
901929
tryAddHint(MO, MI.getOperand(0), NeedGPRC);
902930
} else if (MI.isCommutable() && OpIdx == 2 &&
903931
(!NeedGPRC || isCompressibleOpnd(MI.getOperand(1)))) {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc < %s -mtriple=riscv64 -mattr=+m,+zba,+zbb,+zcb | FileCheck %s
3+
4+
define i64 @c_not(i64 %x, i64 %y, i64 %z) {
5+
; CHECK-LABEL: c_not:
6+
; CHECK: # %bb.0:
7+
; CHECK-NEXT: not a1, a1
8+
; CHECK-NEXT: li a0, 1234
9+
; CHECK-NEXT: mul a0, a0, a1
10+
; CHECK-NEXT: ret
11+
%a = xor i64 %y, -1
12+
%b = mul i64 %a, 1234
13+
ret i64 %b
14+
}
15+
16+
define i64 @c_mul(i64 %x, i64 %y, i64 %z, i64 %w) {
17+
; CHECK-LABEL: c_mul:
18+
; CHECK: # %bb.0:
19+
; CHECK-NEXT: mul a1, a1, a2
20+
; CHECK-NEXT: lui a0, 1
21+
; CHECK-NEXT: or a0, a0, a1
22+
; CHECK-NEXT: ret
23+
%a = mul i64 %y, %z
24+
%b = or i64 %a, 4096
25+
ret i64 %b
26+
}
27+
28+
define i64 @c_sext_b(i64 %x, i8 %y, i64 %z) {
29+
; CHECK-LABEL: c_sext_b:
30+
; CHECK: # %bb.0:
31+
; CHECK-NEXT: sext.b a1, a1
32+
; CHECK-NEXT: lui a0, 1
33+
; CHECK-NEXT: or a0, a0, a1
34+
; CHECK-NEXT: ret
35+
%a = sext i8 %y to i64
36+
%b = or i64 %a, 4096
37+
ret i64 %b
38+
}
39+
40+
define i64 @c_sext_h(i64 %x, i16 %y, i64 %z) {
41+
; CHECK-LABEL: c_sext_h:
42+
; CHECK: # %bb.0:
43+
; CHECK-NEXT: sext.h a1, a1
44+
; CHECK-NEXT: lui a0, 1
45+
; CHECK-NEXT: or a0, a0, a1
46+
; CHECK-NEXT: ret
47+
%a = sext i16 %y to i64
48+
%b = or i64 %a, 4096
49+
ret i64 %b
50+
}
51+
52+
define i64 @c_zext_b(i64 %x, i8 %y, i64 %z) {
53+
; CHECK-LABEL: c_zext_b:
54+
; CHECK: # %bb.0:
55+
; CHECK-NEXT: andi a1, a1, 255
56+
; CHECK-NEXT: lui a0, 1
57+
; CHECK-NEXT: or a0, a0, a1
58+
; CHECK-NEXT: ret
59+
%a = zext i8 %y to i64
60+
%b = or i64 %a, 4096
61+
ret i64 %b
62+
}
63+
64+
define i64 @c_zext_h(i64 %x, i16 %y) {
65+
; CHECK-LABEL: c_zext_h:
66+
; CHECK: # %bb.0:
67+
; CHECK-NEXT: zext.h a1, a1
68+
; CHECK-NEXT: lui a0, 4096
69+
; CHECK-NEXT: or a0, a0, a1
70+
; CHECK-NEXT: ret
71+
%a = zext i16 %y to i64
72+
%b = or i64 %a, 16777216
73+
ret i64 %b
74+
}
75+
76+
define i64 @c_zext_w(i64 %x, i32 %y) {
77+
; CHECK-LABEL: c_zext_w:
78+
; CHECK: # %bb.0:
79+
; CHECK-NEXT: zext.w a1, a1
80+
; CHECK-NEXT: li a0, 1234
81+
; CHECK-NEXT: mul a0, a0, a1
82+
; CHECK-NEXT: ret
83+
%a = zext i32 %y to i64
84+
%b = mul i64 %a, 1234
85+
ret i64 %b
86+
}

0 commit comments

Comments
 (0)