Skip to content

Commit 654d1f8

Browse files
authored
[RISCV][GISel] Move i32 patterns that aren't used by SelectionDAG to RISCVGISel.td. NFC (#102685)
Reduces RISCVGenDAGISel.inc by ~6000 bytes.
1 parent 8b6e9de commit 654d1f8

File tree

6 files changed

+308
-306
lines changed

6 files changed

+308
-306
lines changed

llvm/lib/Target/RISCV/RISCVGISel.td

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def GIAddrRegImm :
5050
GIComplexOperandMatcher<s32, "selectAddrRegImm">,
5151
GIComplexPatternEquiv<AddrRegImm>;
5252

53+
// Convert from i32 immediate to i64 target immediate to make SelectionDAG type
54+
// checking happy so we can use ADDIW which expects an XLen immediate.
55+
def as_i64imm : SDNodeXForm<imm, [{
56+
return CurDAG->getTargetConstant(N->getSExtValue(), SDLoc(N), MVT::i64);
57+
}]>;
58+
5359
def gi_as_i64imm : GICustomOperandRenderer<"renderImm">,
5460
GISDNodeXFormEquiv<as_i64imm>;
5561

@@ -157,3 +163,305 @@ let Predicates = [IsRV64] in {
157163
def : LdPat<load, LD, PtrVT>;
158164
def : StPat<store, SD, GPR, PtrVT>;
159165
}
166+
167+
//===----------------------------------------------------------------------===//
168+
// RV64 i32 patterns not used by SelectionDAG
169+
//===----------------------------------------------------------------------===//
170+
171+
def simm12i32 : ImmLeaf<i32, [{return isInt<12>(Imm);}]>;
172+
173+
def zext_is_sext : PatFrag<(ops node:$src), (zext node:$src), [{
174+
KnownBits Known = CurDAG->computeKnownBits(N->getOperand(0), 0);
175+
return Known.isNonNegative();
176+
}]>;
177+
178+
let Predicates = [IsRV64] in {
179+
def : LdPat<sextloadi8, LB, i32>;
180+
def : LdPat<extloadi8, LBU, i32>; // Prefer unsigned due to no c.lb in Zcb.
181+
def : LdPat<sextloadi16, LH, i32>;
182+
def : LdPat<extloadi16, LH, i32>;
183+
def : LdPat<zextloadi8, LBU, i32>;
184+
def : LdPat<zextloadi16, LHU, i32>;
185+
186+
def : StPat<truncstorei8, SB, GPR, i32>;
187+
def : StPat<truncstorei16, SH, GPR, i32>;
188+
189+
def : Pat<(anyext GPR:$src), (COPY GPR:$src)>;
190+
def : Pat<(sext GPR:$src), (ADDIW GPR:$src, 0)>;
191+
def : Pat<(trunc GPR:$src), (COPY GPR:$src)>;
192+
193+
def : PatGprGpr<add, ADDW, i32, i32>;
194+
def : PatGprGpr<sub, SUBW, i32, i32>;
195+
def : PatGprGpr<and, AND, i32, i32>;
196+
def : PatGprGpr<or, OR, i32, i32>;
197+
def : PatGprGpr<xor, XOR, i32, i32>;
198+
def : PatGprGpr<shiftopw<shl>, SLLW, i32, i64>;
199+
def : PatGprGpr<shiftopw<srl>, SRLW, i32, i64>;
200+
def : PatGprGpr<shiftopw<sra>, SRAW, i32, i64>;
201+
202+
def : Pat<(i32 (add GPR:$rs1, simm12i32:$imm)),
203+
(ADDIW GPR:$rs1, (i64 (as_i64imm $imm)))>;
204+
def : Pat<(i32 (and GPR:$rs1, simm12i32:$imm)),
205+
(ANDI GPR:$rs1, (i64 (as_i64imm $imm)))>;
206+
def : Pat<(i32 (or GPR:$rs1, simm12i32:$imm)),
207+
(ORI GPR:$rs1, (i64 (as_i64imm $imm)))>;
208+
def : Pat<(i32 (xor GPR:$rs1, simm12i32:$imm)),
209+
(XORI GPR:$rs1, (i64 (as_i64imm $imm)))>;
210+
211+
def : PatGprImm<shl, SLLIW, uimm5, i32>;
212+
def : PatGprImm<srl, SRLIW, uimm5, i32>;
213+
def : PatGprImm<sra, SRAIW, uimm5, i32>;
214+
215+
def : Pat<(i32 (and GPR:$rs, TrailingOnesMask:$mask)),
216+
(SRLI (i32 (SLLI $rs, (i64 (XLenSubTrailingOnes $mask)))),
217+
(i64 (XLenSubTrailingOnes $mask)))>;
218+
219+
// Use sext if the sign bit of the input is 0.
220+
def : Pat<(zext_is_sext GPR:$src), (ADDIW GPR:$src, 0)>;
221+
}
222+
223+
let Predicates = [IsRV64, NotHasStdExtZba] in {
224+
def : Pat<(zext GPR:$src), (SRLI (i64 (SLLI GPR:$src, 32)), 32)>;
225+
226+
// If we're shifting a 32-bit zero extended value left by 0-31 bits, use 2
227+
// shifts instead of 3. This can occur when unsigned is used to index an array.
228+
def : Pat<(shl (zext GPR:$rs), uimm5:$shamt),
229+
(SRLI (i64 (SLLI GPR:$rs, 32)), (ImmSubFrom32 uimm5:$shamt))>;
230+
}
231+
232+
//===----------------------------------------------------------------------===//
233+
// M RV64 i32 legalization patterns.
234+
//===----------------------------------------------------------------------===//
235+
236+
let Predicates = [HasStdExtZmmul, IsRV64] in {
237+
def : PatGprGpr<mul, MULW, i32, i32>;
238+
}
239+
240+
let Predicates = [HasStdExtM, IsRV64] in {
241+
def : PatGprGpr<sdiv, DIVW, i32, i32>;
242+
def : PatGprGpr<udiv, DIVUW, i32, i32>;
243+
def : PatGprGpr<srem, REMW, i32, i32>;
244+
def : PatGprGpr<urem, REMUW, i32, i32>;
245+
}
246+
247+
//===----------------------------------------------------------------------===//
248+
// Atomic RV64 i32 patterns not used by SelectionDAG
249+
//===----------------------------------------------------------------------===//
250+
251+
class PatGprGprA<SDPatternOperator OpNode, RVInst Inst, ValueType vt>
252+
: Pat<(vt (OpNode (XLenVT GPR:$rs1), (vt GPR:$rs2))), (Inst GPR:$rs1, GPR:$rs2)>;
253+
254+
multiclass AMOPat2<string AtomicOp, string BaseInst, ValueType vt = XLenVT,
255+
list<Predicate> ExtraPreds = []> {
256+
let Predicates = !listconcat([HasStdExtA, NotHasStdExtZtso], ExtraPreds) in {
257+
def : PatGprGprA<!cast<PatFrag>(AtomicOp#"_monotonic"),
258+
!cast<RVInst>(BaseInst), vt>;
259+
def : PatGprGprA<!cast<PatFrag>(AtomicOp#"_acquire"),
260+
!cast<RVInst>(BaseInst#"_AQ"), vt>;
261+
def : PatGprGprA<!cast<PatFrag>(AtomicOp#"_release"),
262+
!cast<RVInst>(BaseInst#"_RL"), vt>;
263+
def : PatGprGprA<!cast<PatFrag>(AtomicOp#"_acq_rel"),
264+
!cast<RVInst>(BaseInst#"_AQ_RL"), vt>;
265+
def : PatGprGprA<!cast<PatFrag>(AtomicOp#"_seq_cst"),
266+
!cast<RVInst>(BaseInst#"_AQ_RL"), vt>;
267+
}
268+
let Predicates = !listconcat([HasStdExtA, HasStdExtZtso], ExtraPreds) in {
269+
def : PatGprGprA<!cast<PatFrag>(AtomicOp#"_monotonic"),
270+
!cast<RVInst>(BaseInst), vt>;
271+
def : PatGprGprA<!cast<PatFrag>(AtomicOp#"_acquire"),
272+
!cast<RVInst>(BaseInst), vt>;
273+
def : PatGprGprA<!cast<PatFrag>(AtomicOp#"_release"),
274+
!cast<RVInst>(BaseInst), vt>;
275+
def : PatGprGprA<!cast<PatFrag>(AtomicOp#"_acq_rel"),
276+
!cast<RVInst>(BaseInst), vt>;
277+
def : PatGprGprA<!cast<PatFrag>(AtomicOp#"_seq_cst"),
278+
!cast<RVInst>(BaseInst), vt>;
279+
}
280+
}
281+
282+
defm : AMOPat2<"atomic_swap_i32", "AMOSWAP_W", i32>;
283+
defm : AMOPat2<"atomic_load_add_i32", "AMOADD_W", i32>;
284+
defm : AMOPat2<"atomic_load_and_i32", "AMOAND_W", i32>;
285+
defm : AMOPat2<"atomic_load_or_i32", "AMOOR_W", i32>;
286+
defm : AMOPat2<"atomic_load_xor_i32", "AMOXOR_W", i32>;
287+
defm : AMOPat2<"atomic_load_max_i32", "AMOMAX_W", i32>;
288+
defm : AMOPat2<"atomic_load_min_i32", "AMOMIN_W", i32>;
289+
defm : AMOPat2<"atomic_load_umax_i32", "AMOMAXU_W", i32>;
290+
defm : AMOPat2<"atomic_load_umin_i32", "AMOMINU_W", i32>;
291+
292+
let Predicates = [HasStdExtA, IsRV64] in
293+
defm : PseudoCmpXchgPat<"atomic_cmp_swap_i32", PseudoCmpXchg32, i32>;
294+
295+
let Predicates = [HasAtomicLdSt] in {
296+
def : LdPat<atomic_load_8, LB, i32>;
297+
def : LdPat<atomic_load_16, LH, i32>;
298+
def : LdPat<atomic_load_32, LW, i32>;
299+
300+
def : StPat<atomic_store_8, SB, GPR, i32>;
301+
def : StPat<atomic_store_16, SH, GPR, i32>;
302+
def : StPat<atomic_store_32, SW, GPR, i32>;
303+
}
304+
305+
306+
//===----------------------------------------------------------------------===//
307+
// Zb* RV64 i32 patterns not used by SelectionDAG.
308+
//===----------------------------------------------------------------------===//
309+
310+
def zexti16i32 : ComplexPattern<i32, 1, "selectZExtBits<16>">;
311+
def zexti8i32 : ComplexPattern<i32, 1, "selectZExtBits<8>">;
312+
313+
def BCLRMaski32 : ImmLeaf<i32, [{
314+
return !isInt<12>(Imm) && isPowerOf2_32(~Imm);
315+
}]>;
316+
def SingleBitSetMaski32 : ImmLeaf<i32, [{
317+
return !isInt<12>(Imm) && isPowerOf2_32(Imm);
318+
}]>;
319+
320+
let Predicates = [HasStdExtZbb, IsRV64] in {
321+
def : PatGpr<ctlz, CLZW, i32>;
322+
def : PatGpr<cttz, CTZW, i32>;
323+
def : PatGpr<ctpop, CPOPW, i32>;
324+
325+
def : Pat<(i32 (sext_inreg GPR:$rs1, i8)), (SEXT_B GPR:$rs1)>;
326+
def : Pat<(i32 (sext_inreg GPR:$rs1, i16)), (SEXT_H GPR:$rs1)>;
327+
328+
def : Pat<(i32 (and GPR:$rs, 0xFFFF)), (ZEXT_H_RV64 GPR:$rs)>;
329+
} // Predicates = [HasStdExtZbb, IsRV64]
330+
331+
let Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV64] in {
332+
def : Pat<(i32 (and GPR:$rs, 0xFFFF)), (PACKW GPR:$rs, (XLenVT X0))>;
333+
}
334+
335+
let Predicates = [HasStdExtZbbOrZbkb, IsRV64] in {
336+
def : Pat<(i32 (and GPR:$rs1, (not GPR:$rs2))), (ANDN GPR:$rs1, GPR:$rs2)>;
337+
def : Pat<(i32 (or GPR:$rs1, (not GPR:$rs2))), (ORN GPR:$rs1, GPR:$rs2)>;
338+
def : Pat<(i32 (xor GPR:$rs1, (not GPR:$rs2))), (XNOR GPR:$rs1, GPR:$rs2)>;
339+
340+
def : PatGprGpr<shiftopw<rotl>, ROLW, i32, i64>;
341+
def : PatGprGpr<shiftopw<rotr>, RORW, i32, i64>;
342+
def : PatGprImm<rotr, RORIW, uimm5, i32>;
343+
344+
def : Pat<(i32 (rotl GPR:$rs1, uimm5:$rs2)),
345+
(RORIW GPR:$rs1, (ImmSubFrom32 uimm5:$rs2))>;
346+
} // Predicates = [HasStdExtZbbOrZbkb, IsRV64]
347+
348+
let Predicates = [HasStdExtZbkb, IsRV64] in {
349+
def : Pat<(or (and (shl GPR:$rs2, (i64 8)), 0xFFFF),
350+
(zexti8i32 (i32 GPR:$rs1))),
351+
(PACKH GPR:$rs1, GPR:$rs2)>;
352+
def : Pat<(or (shl (zexti8i32 (i32 GPR:$rs2)), (i64 8)),
353+
(zexti8i32 (i32 GPR:$rs1))),
354+
(PACKH GPR:$rs1, GPR:$rs2)>;
355+
def : Pat<(and (anyext (or (shl GPR:$rs2, (XLenVT 8)),
356+
(zexti8i32 (i32 GPR:$rs1)))), 0xFFFF),
357+
(PACKH GPR:$rs1, GPR:$rs2)>;
358+
359+
def : Pat<(i32 (or (shl GPR:$rs2, (i64 16)), (zexti16i32 (i32 GPR:$rs1)))),
360+
(PACKW GPR:$rs1, GPR:$rs2)>;
361+
} // Predicates = [HasStdExtZbkb, IsRV64]
362+
363+
let Predicates = [HasStdExtZba, IsRV64] in {
364+
def : Pat<(shl (i64 (zext i32:$rs1)), uimm5:$shamt),
365+
(SLLI_UW GPR:$rs1, uimm5:$shamt)>;
366+
367+
def : Pat<(i64 (add_like_non_imm12 (zext GPR:$rs1), GPR:$rs2)),
368+
(ADD_UW GPR:$rs1, GPR:$rs2)>;
369+
def : Pat<(zext GPR:$src), (ADD_UW GPR:$src, (XLenVT X0))>;
370+
371+
foreach i = {1,2,3} in {
372+
defvar shxadd = !cast<Instruction>("SH"#i#"ADD");
373+
def : Pat<(i32 (add_like_non_imm12 (shl GPR:$rs1, (i64 i)), GPR:$rs2)),
374+
(shxadd GPR:$rs1, GPR:$rs2)>;
375+
def : Pat<(i32 (riscv_shl_add GPR:$rs1, (i32 i), GPR:$rs2)),
376+
(shxadd GPR:$rs1, GPR:$rs2)>;
377+
}
378+
}
379+
380+
let Predicates = [HasStdExtZbs, IsRV64] in {
381+
def : Pat<(i32 (and (not (shiftop<shl> 1, (i64 GPR:$rs2))), GPR:$rs1)),
382+
(BCLR GPR:$rs1, GPR:$rs2)>;
383+
def : Pat<(i32 (and (rotl -2, (i64 GPR:$rs2)), GPR:$rs1)),
384+
(BCLR GPR:$rs1, GPR:$rs2)>;
385+
def : Pat<(i32 (or (shiftop<shl> 1, (i64 GPR:$rs2)), GPR:$rs1)),
386+
(BSET GPR:$rs1, GPR:$rs2)>;
387+
def : Pat<(i32 (xor (shiftop<shl> 1, (i64 GPR:$rs2)), GPR:$rs1)),
388+
(BINV GPR:$rs1, GPR:$rs2)>;
389+
def : Pat<(i32 (and (shiftop<srl> GPR:$rs1, (i64 GPR:$rs2)), 1)),
390+
(BEXT GPR:$rs1, GPR:$rs2)>;
391+
def : Pat<(i64 (and (anyext (i32 (shiftop<srl> GPR:$rs1, (i64 GPR:$rs2)))), 1)),
392+
(BEXT GPR:$rs1, GPR:$rs2)>;
393+
394+
def : Pat<(i32 (shiftop<shl> 1, (i64 GPR:$rs2))),
395+
(BSET (XLenVT X0), GPR:$rs2)>;
396+
def : Pat<(i32 (not (shiftop<shl> -1, (i64 GPR:$rs2)))),
397+
(ADDI (i32 (BSET (XLenVT X0), GPR:$rs2)), -1)>;
398+
399+
def : Pat<(i32 (and (srl GPR:$rs1, uimm5:$shamt), (i32 1))),
400+
(BEXTI GPR:$rs1, uimm5:$shamt)>;
401+
402+
def : Pat<(i32 (and GPR:$rs1, BCLRMaski32:$mask)),
403+
(BCLRI GPR:$rs1, (i64 (BCLRXForm $mask)))>;
404+
def : Pat<(i32 (or GPR:$rs1, SingleBitSetMaski32:$mask)),
405+
(BSETI GPR:$rs1, (i64 (SingleBitSetMaskToIndex $mask)))>;
406+
def : Pat<(i32 (xor GPR:$rs1, SingleBitSetMaski32:$mask)),
407+
(BINVI GPR:$rs1, (i64 (SingleBitSetMaskToIndex $mask)))>;
408+
} // Predicates = [HasStdExtZbs, IsRV64]
409+
410+
//===----------------------------------------------------------------------===//
411+
// XTHead RV64 i32 patterns not used by SelectionDAG.
412+
//===----------------------------------------------------------------------===//
413+
414+
def sexti16i32 : ComplexPattern<i32, 1, "selectSExtBits<16>">;
415+
416+
let Predicates = [HasVendorXTHeadMemIdx, IsRV64] in {
417+
defm : StoreUpdatePat<post_truncsti8, TH_SBIA, i32>;
418+
defm : StoreUpdatePat<pre_truncsti8, TH_SBIB, i32>;
419+
defm : StoreUpdatePat<post_truncsti16, TH_SHIA, i32>;
420+
defm : StoreUpdatePat<pre_truncsti16, TH_SHIB, i32>;
421+
422+
defm : StIdxPat<truncstorei8, TH_SRB, GPR, i32>;
423+
defm : StIdxPat<truncstorei16, TH_SRH, GPR, i32>;
424+
425+
defm : StZextIdxPat<truncstorei8, TH_SURB, GPR, i32>;
426+
defm : StZextIdxPat<truncstorei16, TH_SURH, GPR, i32>;
427+
defm : StZextIdxPat<store, TH_SURW, GPR, i32>;
428+
}
429+
430+
let Predicates = [HasVendorXTHeadCondMov, IsRV64] in {
431+
def : Pat<(select (XLenVT GPR:$cond), (i32 GPR:$a), (i32 GPR:$b)),
432+
(TH_MVEQZ GPR:$a, GPR:$b, GPR:$cond)>;
433+
def : Pat<(select (XLenVT GPR:$cond), (i32 GPR:$a), (i32 0)),
434+
(TH_MVEQZ GPR:$a, (XLenVT X0), GPR:$cond)>;
435+
def : Pat<(select (XLenVT GPR:$cond), (i32 0), (i32 GPR:$b)),
436+
(TH_MVNEZ GPR:$b, (XLenVT X0), GPR:$cond)>;
437+
438+
def : Pat<(select (riscv_seteq (XLenVT GPR:$cond)), (i32 GPR:$a), (i32 GPR:$b)),
439+
(TH_MVNEZ GPR:$a, GPR:$b, GPR:$cond)>;
440+
def : Pat<(select (riscv_setne (XLenVT GPR:$cond)), (i32 GPR:$a), (i32 GPR:$b)),
441+
(TH_MVEQZ GPR:$a, GPR:$b, GPR:$cond)>;
442+
def : Pat<(select (riscv_seteq (XLenVT GPR:$cond)), (i32 GPR:$a), (i32 0)),
443+
(TH_MVNEZ GPR:$a, (XLenVT X0), GPR:$cond)>;
444+
def : Pat<(select (riscv_setne (XLenVT GPR:$cond)), (i32 GPR:$a), (i32 0)),
445+
(TH_MVEQZ GPR:$a, (XLenVT X0), GPR:$cond)>;
446+
def : Pat<(select (riscv_seteq (XLenVT GPR:$cond)), (i32 0), (i32 GPR:$b)),
447+
(TH_MVEQZ GPR:$b, (XLenVT X0), GPR:$cond)>;
448+
def : Pat<(select (riscv_setne (XLenVT GPR:$cond)), (i32 0), (i32 GPR:$b)),
449+
(TH_MVNEZ GPR:$b, (XLenVT X0), GPR:$cond)>;
450+
} // Predicates = [HasVendorXTHeadCondMov]
451+
452+
let Predicates = [HasVendorXTHeadMac, IsRV64] in {
453+
// mulaw, mulsw are available only in RV64.
454+
def : Pat<(i32 (add GPR:$rd, (mul GPR:$rs1, GPR:$rs2))),
455+
(TH_MULAW GPR:$rd, GPR:$rs1, GPR:$rs2)>;
456+
def : Pat<(i32 (sub GPR:$rd, (mul GPR:$rs1, GPR:$rs2))),
457+
(TH_MULSW GPR:$rd, GPR:$rs1, GPR:$rs2)>;
458+
// mulah, mulsh produce a sign-extended result.
459+
def : Pat<(i32 (add GPR:$rd,
460+
(mul (sexti16i32 (i32 GPR:$rs1)),
461+
(sexti16i32 (i32 GPR:$rs2))))),
462+
(TH_MULAH GPR:$rd, GPR:$rs1, GPR:$rs2)>;
463+
def : Pat<(i32 (sub GPR:$rd,
464+
(mul (sexti16i32 (i32 GPR:$rs1)),
465+
(sexti16i32 (i32 GPR:$rs2))))),
466+
(TH_MULSH GPR:$rd, GPR:$rs1, GPR:$rs2)>;
467+
}

0 commit comments

Comments
 (0)