Skip to content

Commit 9dfdea6

Browse files
committed
[RISCV] Add XTheadMac patterns for -riscv-experimental-rv64-legal-i32.
1 parent a3d8b78 commit 9dfdea6

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,7 @@ def assertsexti32 : PatFrag<(ops node:$src), (assertsext node:$src), [{
11861186
return cast<VTSDNode>(N->getOperand(1))->getVT().bitsLE(MVT::i32);
11871187
}]>;
11881188
def sexti16 : ComplexPattern<XLenVT, 1, "selectSExtBits<16>">;
1189+
def sexti16i32 : ComplexPattern<i32, 1, "selectSExtBits<16>">;
11891190
def sexti32 : ComplexPattern<i64, 1, "selectSExtBits<32>">;
11901191
def assertzexti32 : PatFrag<(ops node:$src), (assertzext node:$src), [{
11911192
return cast<VTSDNode>(N->getOperand(1))->getVT().bitsLE(MVT::i32);

llvm/lib/Target/RISCV/RISCVInstrInfoXTHead.td

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,3 +935,20 @@ def : Pat<(select (riscv_seteq (XLenVT GPR:$cond)), (i32 0), (i32 GPR:$b)),
935935
def : Pat<(select (riscv_setne (XLenVT GPR:$cond)), (i32 0), (i32 GPR:$b)),
936936
(TH_MVNEZ GPR:$b, (XLenVT X0), GPR:$cond)>;
937937
} // Predicates = [HasVendorXTHeadCondMov]
938+
939+
let Predicates = [HasVendorXTHeadMac, IsRV64] in {
940+
// mulaw, mulsw are available only in RV64.
941+
def : Pat<(i32 (add GPR:$rd, (mul GPR:$rs1, GPR:$rs2))),
942+
(TH_MULAW GPR:$rd, GPR:$rs1, GPR:$rs2)>;
943+
def : Pat<(i32 (sub GPR:$rd, (mul GPR:$rs1, GPR:$rs2))),
944+
(TH_MULSW GPR:$rd, GPR:$rs1, GPR:$rs2)>;
945+
// mulah, mulsh produce a sign-extended result.
946+
def : Pat<(i32 (add GPR:$rd,
947+
(mul (sexti16i32 (i32 GPR:$rs1)),
948+
(sexti16i32 (i32 GPR:$rs2))))),
949+
(TH_MULAH GPR:$rd, GPR:$rs1, GPR:$rs2)>;
950+
def : Pat<(i32 (sub GPR:$rd,
951+
(mul (sexti16i32 (i32 GPR:$rs1)),
952+
(sexti16i32 (i32 GPR:$rs2))))),
953+
(TH_MULSH GPR:$rd, GPR:$rs1, GPR:$rs2)>;
954+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=riscv64 -mattr=+xtheadmac -mattr=+m -verify-machineinstrs < %s \
3+
; RUN: -riscv-experimental-rv64-legal-i32 | FileCheck %s -check-prefix=RV64XTHEADMAC
4+
5+
define i32 @mula_i32(i32 %a, i32 %b, i32 %c) {
6+
; RV64XTHEADMAC-LABEL: mula_i32:
7+
; RV64XTHEADMAC: # %bb.0:
8+
; RV64XTHEADMAC-NEXT: th.mulaw a0, a1, a2
9+
; RV64XTHEADMAC-NEXT: ret
10+
%d = mul i32 %b, %c
11+
%e = add i32 %a, %d
12+
ret i32 %e
13+
}
14+
15+
define i32 @muls_i32(i32 %a, i32 %b, i32 %c) {
16+
; RV64XTHEADMAC-LABEL: muls_i32:
17+
; RV64XTHEADMAC: # %bb.0:
18+
; RV64XTHEADMAC-NEXT: th.mulsw a0, a1, a2
19+
; RV64XTHEADMAC-NEXT: ret
20+
%d = mul i32 %b, %c
21+
%e = sub i32 %a, %d
22+
ret i32 %e
23+
}
24+
25+
define i64 @mula_i64(i64 %a, i64 %b, i64 %c) {
26+
; RV64XTHEADMAC-LABEL: mula_i64:
27+
; RV64XTHEADMAC: # %bb.0:
28+
; RV64XTHEADMAC-NEXT: th.mula a0, a1, a2
29+
; RV64XTHEADMAC-NEXT: ret
30+
%d = mul i64 %b, %c
31+
%f = add i64 %a, %d
32+
ret i64 %f
33+
}
34+
35+
define i64 @mulaw_i64(i32 %a, i32 %b, i32 %c) {
36+
; RV64XTHEADMAC-LABEL: mulaw_i64:
37+
; RV64XTHEADMAC: # %bb.0:
38+
; RV64XTHEADMAC-NEXT: th.mulaw a0, a1, a2
39+
; RV64XTHEADMAC-NEXT: ret
40+
%d = mul i32 %b, %c
41+
%e = add i32 %a, %d
42+
%f = sext i32 %e to i64
43+
ret i64 %f
44+
}
45+
46+
define i64 @mulah_i64(i32 %a, i16 %b, i16 %c) {
47+
; RV64XTHEADMAC-LABEL: mulah_i64:
48+
; RV64XTHEADMAC: # %bb.0:
49+
; RV64XTHEADMAC-NEXT: th.mulah a0, a1, a2
50+
; RV64XTHEADMAC-NEXT: ret
51+
%d = sext i16 %b to i32
52+
%e = sext i16 %c to i32
53+
%f = mul i32 %d, %e
54+
%g = add i32 %a, %f
55+
%h = sext i32 %g to i64
56+
ret i64 %h
57+
}
58+
59+
define i64 @muls_i64(i64 %a, i64 %b, i64 %c) {
60+
; RV64XTHEADMAC-LABEL: muls_i64:
61+
; RV64XTHEADMAC: # %bb.0:
62+
; RV64XTHEADMAC-NEXT: th.muls a0, a1, a2
63+
; RV64XTHEADMAC-NEXT: ret
64+
%d = mul i64 %b, %c
65+
%f = sub i64 %a, %d
66+
ret i64 %f
67+
}
68+
69+
define i64 @mulsw_i64(i32 %a, i32 %b, i32 %c) {
70+
; RV64XTHEADMAC-LABEL: mulsw_i64:
71+
; RV64XTHEADMAC: # %bb.0:
72+
; RV64XTHEADMAC-NEXT: th.mulsw a0, a1, a2
73+
; RV64XTHEADMAC-NEXT: ret
74+
%d = mul i32 %b, %c
75+
%e = sub i32 %a, %d
76+
%f = sext i32 %e to i64
77+
ret i64 %f
78+
}
79+
80+
define i64 @mulsh_i64(i32 %a, i16 %b, i16 %c) {
81+
; RV64XTHEADMAC-LABEL: mulsh_i64:
82+
; RV64XTHEADMAC: # %bb.0:
83+
; RV64XTHEADMAC-NEXT: th.mulsh a0, a1, a2
84+
; RV64XTHEADMAC-NEXT: ret
85+
%d = sext i16 %b to i32
86+
%e = sext i16 %c to i32
87+
%f = mul i32 %d, %e
88+
%g = sub i32 %a, %f
89+
%h = sext i32 %g to i64
90+
ret i64 %h
91+
}
92+
93+
define i32 @commutative1(i32 %A, i32 %B, i32 %C) {
94+
; RV64XTHEADMAC-LABEL: commutative1:
95+
; RV64XTHEADMAC: # %bb.0:
96+
; RV64XTHEADMAC-NEXT: th.mulaw a2, a1, a0
97+
; RV64XTHEADMAC-NEXT: mv a0, a2
98+
; RV64XTHEADMAC-NEXT: ret
99+
%mul = mul nsw i32 %B, %A
100+
%add = add i32 %mul, %C
101+
ret i32 %add
102+
}
103+
104+
define i32 @commutative2(i32 %A, i32 %B, i32 %C) {
105+
; RV64XTHEADMAC-LABEL: commutative2:
106+
; RV64XTHEADMAC: # %bb.0:
107+
; RV64XTHEADMAC-NEXT: th.mulaw a0, a1, a2
108+
; RV64XTHEADMAC-NEXT: ret
109+
%mul = mul nsw i32 %B, %C
110+
%add = add i32 %mul, %A
111+
ret i32 %add
112+
}
113+
114+
define i32 @commutative3(i32 %A, i32 %B, i32 %C) {
115+
; RV64XTHEADMAC-LABEL: commutative3:
116+
; RV64XTHEADMAC: # %bb.0:
117+
; RV64XTHEADMAC-NEXT: th.mulaw a1, a2, a0
118+
; RV64XTHEADMAC-NEXT: mv a0, a1
119+
; RV64XTHEADMAC-NEXT: ret
120+
%mul = mul nsw i32 %C, %A
121+
%add = add i32 %mul, %B
122+
ret i32 %add
123+
}

0 commit comments

Comments
 (0)