Skip to content

Commit 4c97c51

Browse files
authored
[RISCV] Add ISel patterns for Xqcilia instructions (#135724)
This patch adds instruction selection patterns for generating the long immediate arithmetic instructions. We prefer generating instructions that have a 26 bit immediate to a 32 bit immediate given that both are of the same size but the former might be easier to register allocate for. Base RISC-V arithmetic instructions will be preferred, when applicable.
1 parent 6c6ab2a commit 4c97c51

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfoXqci.td

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ def simm20_li : RISCVOp<XLenVT> {
118118

119119
def simm26 : RISCVSImmLeafOp<26>;
120120

121+
def simm26_nosimm12 : ImmLeaf<XLenVT, [{
122+
return isInt<26>(Imm) && !isInt<12>(Imm);}]>;
123+
124+
def simm32_nosimm26 : ImmLeaf<XLenVT, [{
125+
return isInt<32>(Imm) && !isInt<26>(Imm);}]>;
126+
121127
class BareSImmNAsmOperand<int width>
122128
: ImmAsmOperand<"BareS", width, ""> {
123129
let PredicateMethod = "isBareSimmN<" # width # ">";
@@ -1223,5 +1229,29 @@ def PseudoQC_E_SW : PseudoStore<"qc.e.sw">;
12231229
// Code Gen Patterns
12241230
//===----------------------------------------------------------------------===//
12251231

1232+
/// Generic pattern classes
1233+
1234+
class PatGprNoX0Simm26NoSimm12<SDPatternOperator OpNode, RVInst48 Inst>
1235+
: Pat<(i32 (OpNode (i32 GPRNoX0:$rs1), simm26_nosimm12:$imm)),
1236+
(Inst GPRNoX0:$rs1, simm26_nosimm12:$imm)>;
1237+
1238+
class PatGprNoX0Simm32NoSimm26<SDPatternOperator OpNode, RVInst48 Inst>
1239+
: Pat<(i32 (OpNode (i32 GPRNoX0:$rs1), simm32_nosimm26:$imm)),
1240+
(Inst GPRNoX0:$rs1, simm32_nosimm26:$imm)>;
1241+
1242+
/// Simple arithmetic operations
1243+
1244+
let Predicates = [HasVendorXqcilia, IsRV32] in {
1245+
def : PatGprNoX0Simm32NoSimm26<add, QC_E_ADDAI>;
1246+
def : PatGprNoX0Simm32NoSimm26<and, QC_E_ANDAI>;
1247+
def : PatGprNoX0Simm32NoSimm26<or, QC_E_ORAI>;
1248+
def : PatGprNoX0Simm32NoSimm26<xor, QC_E_XORAI>;
1249+
1250+
def : PatGprNoX0Simm26NoSimm12<add, QC_E_ADDI>;
1251+
def : PatGprNoX0Simm26NoSimm12<and, QC_E_ANDI>;
1252+
def : PatGprNoX0Simm26NoSimm12<or, QC_E_ORI>;
1253+
def : PatGprNoX0Simm26NoSimm12<xor, QC_E_XORI>;
1254+
} // Predicates = [HasVendorXqcilia, IsRV32]
1255+
12261256
let Predicates = [HasVendorXqciint, IsRV32] in
12271257
def : Pat<(riscv_mileaveret_glue), (QC_C_MILEAVERET)>;

llvm/test/CodeGen/RISCV/xqcilia.ll

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; Test that we are able to generate the Xqcilia instructions
3+
; RUN: llc < %s -mtriple=riscv32 | FileCheck %s -check-prefix=RV32I
4+
; RUN: llc < %s -mtriple=riscv32 -mattr=+experimental-xqcilia | FileCheck %s -check-prefix=RV32XQCILIA
5+
6+
define i32 @add(i32 %a, i32 %b) {
7+
; RV32I-LABEL: add:
8+
; RV32I: # %bb.0:
9+
; RV32I-NEXT: lui a2, 65536
10+
; RV32I-NEXT: add a0, a0, a2
11+
; RV32I-NEXT: lui a2, 573
12+
; RV32I-NEXT: addi a2, a2, -1330
13+
; RV32I-NEXT: add a1, a1, a2
14+
; RV32I-NEXT: and a0, a1, a0
15+
; RV32I-NEXT: addi a0, a0, 13
16+
; RV32I-NEXT: ret
17+
;
18+
; RV32XQCILIA-LABEL: add:
19+
; RV32XQCILIA: # %bb.0:
20+
; RV32XQCILIA-NEXT: qc.e.addi a1, a1, 2345678
21+
; RV32XQCILIA-NEXT: qc.e.addai a0, 268435456
22+
; RV32XQCILIA-NEXT: and a0, a0, a1
23+
; RV32XQCILIA-NEXT: addi a0, a0, 13
24+
; RV32XQCILIA-NEXT: ret
25+
%addai = add i32 %a, 268435456
26+
%add = add i32 %b, 2345678
27+
%and = and i32 %add, %addai
28+
%res = add i32 %and, 13
29+
ret i32 %res
30+
}
31+
32+
define i32 @and(i32 %a, i32 %b) {
33+
; RV32I-LABEL: and:
34+
; RV32I: # %bb.0:
35+
; RV32I-NEXT: lui a2, 65536
36+
; RV32I-NEXT: and a0, a0, a2
37+
; RV32I-NEXT: lui a2, 573
38+
; RV32I-NEXT: addi a2, a2, -1330
39+
; RV32I-NEXT: and a1, a1, a2
40+
; RV32I-NEXT: srl a0, a1, a0
41+
; RV32I-NEXT: andi a0, a0, 10
42+
; RV32I-NEXT: ret
43+
;
44+
; RV32XQCILIA-LABEL: and:
45+
; RV32XQCILIA: # %bb.0:
46+
; RV32XQCILIA-NEXT: qc.e.andi a1, a1, 2345678
47+
; RV32XQCILIA-NEXT: qc.e.andai a0, 268435456
48+
; RV32XQCILIA-NEXT: srl a0, a1, a0
49+
; RV32XQCILIA-NEXT: andi a0, a0, 10
50+
; RV32XQCILIA-NEXT: ret
51+
%andai = and i32 %a, 268435456
52+
%and = and i32 %b, 2345678
53+
%srl = lshr i32 %and, %andai
54+
%res = and i32 %srl, 10
55+
ret i32 %res
56+
}
57+
58+
define i32 @or(i32 %a, i32 %b) {
59+
; RV32I-LABEL: or:
60+
; RV32I: # %bb.0:
61+
; RV32I-NEXT: lui a2, 65536
62+
; RV32I-NEXT: or a0, a0, a2
63+
; RV32I-NEXT: lui a2, 573
64+
; RV32I-NEXT: addi a2, a2, -1330
65+
; RV32I-NEXT: or a1, a1, a2
66+
; RV32I-NEXT: add a0, a1, a0
67+
; RV32I-NEXT: ori a0, a0, 13
68+
; RV32I-NEXT: ret
69+
;
70+
; RV32XQCILIA-LABEL: or:
71+
; RV32XQCILIA: # %bb.0:
72+
; RV32XQCILIA-NEXT: qc.e.ori a1, a1, 2345678
73+
; RV32XQCILIA-NEXT: qc.e.orai a0, 268435456
74+
; RV32XQCILIA-NEXT: add a0, a0, a1
75+
; RV32XQCILIA-NEXT: ori a0, a0, 13
76+
; RV32XQCILIA-NEXT: ret
77+
%orai = or i32 %a, 268435456
78+
%or = or i32 %b, 2345678
79+
%add = add i32 %or, %orai
80+
%res = or i32 %add, 13
81+
ret i32 %res
82+
}
83+
84+
define i32 @xor(i32 %a, i32 %b) {
85+
; RV32I-LABEL: xor:
86+
; RV32I: # %bb.0:
87+
; RV32I-NEXT: lui a2, 65536
88+
; RV32I-NEXT: xor a0, a0, a2
89+
; RV32I-NEXT: lui a2, 573
90+
; RV32I-NEXT: addi a2, a2, -1330
91+
; RV32I-NEXT: xor a1, a1, a2
92+
; RV32I-NEXT: add a0, a1, a0
93+
; RV32I-NEXT: xori a0, a0, 13
94+
; RV32I-NEXT: ret
95+
;
96+
; RV32XQCILIA-LABEL: xor:
97+
; RV32XQCILIA: # %bb.0:
98+
; RV32XQCILIA-NEXT: qc.e.xori a1, a1, 2345678
99+
; RV32XQCILIA-NEXT: qc.e.xorai a0, 268435456
100+
; RV32XQCILIA-NEXT: add a0, a0, a1
101+
; RV32XQCILIA-NEXT: xori a0, a0, 13
102+
; RV32XQCILIA-NEXT: ret
103+
%xorai = xor i32 %a, 268435456
104+
%xor = xor i32 %b, 2345678
105+
%add = add i32 %xor, %xorai
106+
%res = xor i32 %add, 13
107+
ret i32 %res
108+
}

0 commit comments

Comments
 (0)