Skip to content

Commit 990d735

Browse files
committed
[RISCV] Add macro fusions for Xiangshan
Doc: https://xiangshan-doc.readthedocs.io/zh-cn/latest/frontend/decode/ This PR is to show the usage of TableGen-based macro fusions. Some instrcution pairs can be folded into one MacroFusion definition but I leave them standalone to show the different ways to define a macro fusion. This PR is stacked on llvm#72219, llvm#72222, llvm#72223, llvm#72224, llvm#72227
1 parent e2bdff1 commit 990d735

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

llvm/lib/Target/RISCV/RISCVMacroFusion.td

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,94 @@
99
// ===---------------------------------------------------------------------===//
1010
// The following definitions describe the macro fusion predicators.
1111

12-
def LUIADDI: SimpleFusion<CheckOpcode<[LUI]>, CheckOpcode<[ADDI, ADDIW]>>;
12+
class RISCVMacroFusion<list<Instruction> first,list<Instruction> second,
13+
list<MCInstPredicate> extraFirstPreds = [],
14+
list<MCInstPredicate> extraSecondPreds = []>
15+
: SimpleFusion<CheckAll<!listconcat([CheckOpcode<first>], extraFirstPreds)>,
16+
CheckAll<!listconcat([CheckOpcode<second>], extraSecondPreds)>>;
17+
18+
def LUIADDI: RISCVMacroFusion<[LUI], [ADDI, ADDIW]>;
19+
20+
// clear upper 32 bits / get lower 32 bits: slli r1, r0, 32 + srli r1, r1, 32
21+
def ClearUpper32Bits : RISCVMacroFusion<[SLLI], [SRLI],
22+
[CheckImmOperand<2, 32>],
23+
[CheckImmOperand<2, 32>]>;
24+
25+
// clear upper 48 bits / get lower 16 bits: slli r1, r0, 48 + srli r1, r1, 48
26+
def ClearUpper48Bits : RISCVMacroFusion<[SLLI], [SRLI],
27+
[CheckImmOperand<2, 48>],
28+
[CheckImmOperand<2, 48>]>;
29+
30+
// clear upper 48 bits / get lower 16 bits: slliw r1, r0, 16 + srliw r1, r1, 16
31+
def GetLower16Bits : RISCVMacroFusion<[SLLIW], [SRLIW],
32+
[CheckImmOperand<2, 16>],
33+
[CheckImmOperand<2, 16>]>;
34+
35+
// sign-extend a 16-bit number: slliw r1, r0, 16 + sraiw r1, r1, 16
36+
def SExtH : RISCVMacroFusion<[SLLIW], [SRAIW],
37+
[CheckImmOperand<2, 16>],
38+
[CheckImmOperand<2, 16>]>;
39+
40+
// These should be covered by Zba extension?
41+
// shift left by one and add: slli r1, r0, 1 + add r1, r1, r2
42+
// shift left by two and add: slli r1, r0, 2 + add r1, r1, r2
43+
// shift left by three and add: slli r1, r0, 3 + add r1, r1, r2
44+
def ShiftNAdd : RISCVMacroFusion<[SLLI], [ADD],
45+
[CheckAny<[CheckImmOperand<2, 1>,
46+
CheckImmOperand<2, 2>,
47+
CheckImmOperand<2, 3>]>]>;
48+
49+
// shift zero-extended word left by one: slli r1, r0, 32 + srli r1, r0, 31
50+
// shift zero-extended word left by two: slli r1, r0, 32 + srli r1, r0, 30
51+
// shift zero-extended word left by three: slli r1, r0, 32 + srli r1, r0, 29
52+
def ShiftZExtByN : RISCVMacroFusion<[SLLI], [SRLI],
53+
[CheckImmOperand<2, 32>],
54+
[CheckAny<[CheckImmOperand<2, 29>,
55+
CheckImmOperand<2, 30>,
56+
CheckImmOperand<2, 31>]>]>;
57+
58+
// get the second byte: srli r1, r0, 8 + andi r1, r1, 255
59+
def GetSecondByte : RISCVMacroFusion<[SRLI], [ANDI],
60+
[CheckImmOperand<2, 8>],
61+
[CheckImmOperand<2, 255>]>;
62+
63+
// shift left by four and add: slli r1, r0, 4 + add r1, r1, r2
64+
def ShiftLeft4Add : RISCVMacroFusion<[SLLI], [ADD], [CheckImmOperand<2, 4>]>;
65+
66+
// shift right by 29 and add: srli r1, r0, 29 + add r1, r1, r2
67+
// shift right by 30 and add: srli r1, r0, 30 + add r1, r1, r2
68+
// shift right by 31 and add: srli r1, r0, 31 + add r1, r1, r2
69+
// shift right by 32 and add: srli r1, r0, 32 + add r1, r1, r2
70+
def ShiftRightNAdd : RISCVMacroFusion<[SRLI], [ADD],
71+
[CheckAny<[CheckImmOperand<2, 29>,
72+
CheckImmOperand<2, 30>,
73+
CheckImmOperand<2, 31>,
74+
CheckImmOperand<2, 32>]>]>;
75+
76+
// add one if odd, otherwise unchanged: andi r1, r0, 1 + add r1, r1, r2
77+
// add one if odd (in word format), otherwise unchanged: andi r1, r0, 1 + addw r1, r1, r2
78+
def AddOneIfOdd : RISCVMacroFusion<[ANDI], [ADD, ADDW], [CheckImmOperand<2, 1>]>;
79+
80+
// addw and extract its lower 8 bits (fused into addwbyte)
81+
// addw and extract its lower 1 bit (fused into addwbit)
82+
def AddAndExtractNBits : RISCVMacroFusion<[ADDW], [ANDI], [],
83+
[CheckAny<[CheckImmOperand<2, 1>,
84+
CheckImmOperand<2, 255>]>]>;
85+
86+
// addw and zext.h (fused into addwzexth)
87+
// addw and sext.h (fused into addwsexth)
88+
def AddwAndExt : RISCVMacroFusion<[ADDW], [ZEXT_H_RV32, ZEXT_H_RV64, SEXT_H]>;
89+
90+
// logic operation and extract its LSB
91+
def LogicOpAndExtractLSB : RISCVMacroFusion<[AND, OR, XOR, ANDI, ORI, XORI, ORC_B], [ANDI], [],
92+
[CheckImmOperand<2, 1>]>;
93+
94+
// logic operation and extract its lower 16 bits
95+
def LogicOpAndExtractLow16Bits : RISCVMacroFusion<[AND, OR, XOR, ANDI, ORI, XORI, ORC_B],
96+
[ZEXT_H_RV32, ZEXT_H_RV64]>;
97+
98+
// OR(Cat(src1(63, 8), 0.U(8.W)), src2): andi r1, r0, -256 + or r1, r1, r2
99+
def OrCat : RISCVMacroFusion<[ANDI], [OR], [CheckImmOperand<2, -256>]>;
100+
101+
// mul 7-bit data with 32-bit data: andi r1, r0, 127 + mulw r1, r1, r2
102+
def Mul7BitsWith32Bits : RISCVMacroFusion<[ANDI], [MULW], [CheckImmOperand<2, 127>]>;

0 commit comments

Comments
 (0)