@@ -672,10 +672,7 @@ def : Pat<(i32 (and GPR:$rs, 0xFFFF)), (PACK GPR:$rs, (XLenVT X0))>;
672672let Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV64] in
673673def : Pat<(i64 (and GPR:$rs, 0xFFFF)), (PACKW GPR:$rs, (XLenVT X0))>;
674674
675- let Predicates = [HasStdExtZba] in {
676-
677- foreach i = {1,2,3} in {
678- defvar shxadd = !cast<Instruction>("SH"#i#"ADD");
675+ multiclass ShxAddPat<int i, Instruction shxadd> {
679676 def : Pat<(XLenVT (add_like_non_imm12 (shl GPR:$rs1, (XLenVT i)), GPR:$rs2)),
680677 (shxadd GPR:$rs1, GPR:$rs2)>;
681678 def : Pat<(XLenVT (riscv_shl_add GPR:$rs1, (XLenVT i), GPR:$rs2)),
@@ -687,15 +684,90 @@ foreach i = {1,2,3} in {
687684 (shxadd pat:$rs1, GPR:$rs2)>;
688685}
689686
690- def : Pat<(add_like (XLenVT GPR:$r), CSImm12MulBy4:$i),
691- (SH2ADD (XLenVT (ADDI (XLenVT X0), CSImm12MulBy4:$i)),
687+ class CSImm12MulBy4Pat<Instruction sh2add>
688+ : Pat<(add_like (XLenVT GPR:$r), CSImm12MulBy4:$i),
689+ (sh2add (XLenVT (ADDI (XLenVT X0), CSImm12MulBy4:$i)),
692690 GPR:$r)>;
693- def : Pat<(add_like (XLenVT GPR:$r), CSImm12MulBy8:$i),
694- (SH3ADD (XLenVT (ADDI (XLenVT X0), CSImm12MulBy8:$i)),
691+
692+ class CSImm12MulBy8Pat<Instruction sh3add>
693+ : Pat<(add_like (XLenVT GPR:$r), CSImm12MulBy8:$i),
694+ (sh3add (XLenVT (ADDI (XLenVT X0), CSImm12MulBy8:$i)),
695695 GPR:$r)>;
696696
697+ let Predicates = [HasStdExtZba] in {
698+ foreach i = {1,2,3} in {
699+ defvar shxadd = !cast<Instruction>("SH"#i#"ADD");
700+ defm : ShxAddPat<i, shxadd>;
701+ }
702+
703+ def : CSImm12MulBy4Pat<SH2ADD>;
704+ def : CSImm12MulBy8Pat<SH3ADD>;
697705} // Predicates = [HasStdExtZba]
698706
707+ multiclass ADD_UWPat<Instruction add_uw> {
708+ def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFF), GPR:$rs2)),
709+ (add_uw GPR:$rs1, GPR:$rs2)>;
710+ def : Pat<(i64 (and GPR:$rs, 0xFFFFFFFF)), (add_uw GPR:$rs, (XLenVT X0))>;
711+ }
712+
713+ multiclass ShxAdd_UWPat<int i, Instruction shxadd_uw> {
714+ def : Pat<(i64 (add_like_non_imm12 (shl (and GPR:$rs1, 0xFFFFFFFF), (i64 i)),
715+ (XLenVT GPR:$rs2))),
716+ (shxadd_uw GPR:$rs1, GPR:$rs2)>;
717+ def : Pat<(i64 (riscv_shl_add (and GPR:$rs1, 0xFFFFFFFF), (i64 i), GPR:$rs2)),
718+ (shxadd_uw GPR:$rs1, GPR:$rs2)>;
719+
720+ defvar pat = !cast<ComplexPattern>("sh"#i#"add_uw_op");
721+ // More complex cases use a ComplexPattern.
722+ def : Pat<(i64 (add_like_non_imm12 pat:$rs1, (XLenVT GPR:$rs2))),
723+ (shxadd_uw pat:$rs1, GPR:$rs2)>;
724+ }
725+
726+ multiclass Sh1Add_UWPat<Instruction sh1add_uw> {
727+ def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 1)), 0x1FFFFFFFF),
728+ (XLenVT GPR:$rs2))),
729+ (sh1add_uw GPR:$rs1, GPR:$rs2)>;
730+ // Use SRLI to clear the LSBs and SHXADD_UW to mask and shift.
731+ def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x1FFFFFFFE),
732+ (XLenVT GPR:$rs2))),
733+ (sh1add_uw (XLenVT (SRLI GPR:$rs1, 1)), GPR:$rs2)>;
734+ }
735+
736+ multiclass Sh2Add_UWPat<Instruction sh2add_uw> {
737+ def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 2)), 0x3FFFFFFFF),
738+ (XLenVT GPR:$rs2))),
739+ (sh2add_uw GPR:$rs1, GPR:$rs2)>;
740+ // Use SRLI to clear the LSBs and SHXADD_UW to mask and shift.
741+ def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x3FFFFFFFC),
742+ (XLenVT GPR:$rs2))),
743+ (sh2add_uw (XLenVT (SRLI GPR:$rs1, 2)), GPR:$rs2)>;
744+ }
745+
746+ multiclass Sh3Add_UWPat<Instruction sh3add_uw> {
747+ def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFF8),
748+ (XLenVT GPR:$rs2))),
749+ (sh3add_uw (XLenVT (SRLIW GPR:$rs1, 3)), GPR:$rs2)>;
750+ // Use SRLI to clear the LSBs and SHXADD_UW to mask and shift.
751+ def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x7FFFFFFF8),
752+ (XLenVT GPR:$rs2))),
753+ (sh3add_uw (XLenVT (SRLI GPR:$rs1, 3)), GPR:$rs2)>;
754+ }
755+
756+ class Sh1AddPat<Instruction sh1add>
757+ : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFE),
758+ (XLenVT GPR:$rs2))),
759+ (sh1add (XLenVT (SRLIW GPR:$rs1, 1)), GPR:$rs2)>;
760+
761+ class Sh2AddPat<Instruction sh2add>
762+ : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFC),
763+ (XLenVT GPR:$rs2))),
764+ (sh2add (XLenVT (SRLIW GPR:$rs1, 2)), GPR:$rs2)>;
765+
766+ class Sh3AddPat<Instruction sh3add>
767+ : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFF8),
768+ (XLenVT GPR:$rs2))),
769+ (sh3add (XLenVT (SRLIW GPR:$rs1, 3)), GPR:$rs2)>;
770+
699771let Predicates = [HasStdExtZba, IsRV64] in {
700772def : Pat<(i64 (shl (and GPR:$rs1, 0xFFFFFFFF), uimm5:$shamt)),
701773 (SLLI_UW GPR:$rs1, uimm5:$shamt)>;
@@ -704,47 +776,21 @@ def : Pat<(i64 (shl (and GPR:$rs1, 0xFFFFFFFF), uimm5:$shamt)),
704776def : Pat<(i64 (and GPR:$rs1, Shifted32OnesMask:$mask)),
705777 (SLLI_UW (XLenVT (SRLI GPR:$rs1, Shifted32OnesMask:$mask)),
706778 Shifted32OnesMask:$mask)>;
707- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFF), GPR:$rs2)),
708- (ADD_UW GPR:$rs1, GPR:$rs2)>;
709- def : Pat<(i64 (and GPR:$rs, 0xFFFFFFFF)), (ADD_UW GPR:$rs, (XLenVT X0))>;
710779
711- foreach i = {1,2,3} in {
712- defvar shxadd_uw = !cast<Instruction>("SH"#i#"ADD_UW");
713- def : Pat<(i64 (add_like_non_imm12 (shl (and GPR:$rs1, 0xFFFFFFFF), (i64 i)), (XLenVT GPR:$rs2))),
714- (shxadd_uw GPR:$rs1, GPR:$rs2)>;
715- def : Pat<(i64 (riscv_shl_add (and GPR:$rs1, 0xFFFFFFFF), (i64 i), GPR:$rs2)),
716- (shxadd_uw GPR:$rs1, GPR:$rs2)>;
717- }
718-
719- def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 1)), 0x1FFFFFFFF), (XLenVT GPR:$rs2))),
720- (SH1ADD_UW GPR:$rs1, GPR:$rs2)>;
721- def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 2)), 0x3FFFFFFFF), (XLenVT GPR:$rs2))),
722- (SH2ADD_UW GPR:$rs1, GPR:$rs2)>;
723- def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 3)), 0x7FFFFFFFF), (XLenVT GPR:$rs2))),
724- (SH3ADD_UW GPR:$rs1, GPR:$rs2)>;
780+ defm : ADD_UWPat<ADD_UW>;
725781
726- // More complex cases use a ComplexPattern.
727782foreach i = {1,2,3} in {
728- defvar pat = !cast<ComplexPattern>("sh"#i#"add_uw_op");
729- def : Pat<(i64 (add_like_non_imm12 pat:$rs1, (XLenVT GPR:$rs2))),
730- (!cast<Instruction>("SH"#i#"ADD_UW") pat:$rs1, GPR:$rs2)>;
783+ defvar shxadd_uw = !cast<Instruction>("SH"#i#"ADD_UW");
784+ defm : ShxAdd_UWPat<i, shxadd_uw>;
731785}
732786
733- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFE), (XLenVT GPR:$rs2))),
734- (SH1ADD (XLenVT (SRLIW GPR:$rs1, 1)), GPR:$rs2)>;
735- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFC), (XLenVT GPR:$rs2))),
736- (SH2ADD (XLenVT (SRLIW GPR:$rs1, 2)), GPR:$rs2)>;
737- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFF8), (XLenVT GPR:$rs2))),
738- (SH3ADD (XLenVT (SRLIW GPR:$rs1, 3)), GPR:$rs2)>;
739-
740- // Use SRLI to clear the LSBs and SHXADD_UW to mask and shift.
741- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x1FFFFFFFE), (XLenVT GPR:$rs2))),
742- (SH1ADD_UW (XLenVT (SRLI GPR:$rs1, 1)), GPR:$rs2)>;
743- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x3FFFFFFFC), (XLenVT GPR:$rs2))),
744- (SH2ADD_UW (XLenVT (SRLI GPR:$rs1, 2)), GPR:$rs2)>;
745- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x7FFFFFFF8), (XLenVT GPR:$rs2))),
746- (SH3ADD_UW (XLenVT (SRLI GPR:$rs1, 3)), GPR:$rs2)>;
787+ defm : Sh1Add_UWPat<SH1ADD_UW>;
788+ defm : Sh2Add_UWPat<SH2ADD_UW>;
789+ defm : Sh3Add_UWPat<SH3ADD_UW>;
747790
791+ def : Sh1AddPat<SH1ADD>;
792+ def : Sh2AddPat<SH2ADD>;
793+ def : Sh3AddPat<SH3ADD>;
748794} // Predicates = [HasStdExtZba, IsRV64]
749795
750796let Predicates = [HasStdExtZbcOrZbkc] in {
0 commit comments