@@ -659,30 +659,103 @@ def : Pat<(i32 (and GPR:$rs, 0xFFFF)), (PACK GPR:$rs, (XLenVT X0))>;
659659let Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV64] in
660660def : Pat<(i64 (and GPR:$rs, 0xFFFF)), (PACKW GPR:$rs, (XLenVT X0))>;
661661
662- let Predicates = [HasStdExtZba] in {
662+ multiclass ShxAddPat<int i, Instruction shxadd> {
663+ def : Pat<(XLenVT (add_like_non_imm12 (shl GPR:$rs1, (XLenVT i)), GPR:$rs2)),
664+ (shxadd GPR:$rs1, GPR:$rs2)>;
665+ def : Pat<(XLenVT (riscv_shl_add GPR:$rs1, (XLenVT i), GPR:$rs2)),
666+ (shxadd GPR:$rs1, GPR:$rs2)>;
663667
664- foreach i = {1,2,3} in {
665- defvar shxadd = !cast<Instruction>("SH"#i#"ADD");
666- def : Pat<(XLenVT (add_like_non_imm12 (shl GPR:$rs1, (XLenVT i)), GPR:$rs2)),
667- (shxadd GPR:$rs1, GPR:$rs2)>;
668- def : Pat<(XLenVT (riscv_shl_add GPR:$rs1, (XLenVT i), GPR:$rs2)),
669- (shxadd GPR:$rs1, GPR:$rs2)>;
670-
671- defvar pat = !cast<ComplexPattern>("sh"#i#"add_op");
672- // More complex cases use a ComplexPattern.
673- def : Pat<(XLenVT (add_like_non_imm12 pat:$rs1, GPR:$rs2)),
674- (shxadd pat:$rs1, GPR:$rs2)>;
668+ defvar pat = !cast<ComplexPattern>("sh"#i#"add_op");
669+ // More complex cases use a ComplexPattern.
670+ def : Pat<(XLenVT (add_like_non_imm12 pat:$rs1, GPR:$rs2)),
671+ (shxadd pat:$rs1, GPR:$rs2)>;
675672}
676673
677- def : Pat<(add_like (XLenVT GPR:$r), CSImm12MulBy4:$i),
678- (SH2ADD (XLenVT (ADDI (XLenVT X0), CSImm12MulBy4:$i)),
674+ class CSImm12MulBy4Pat<Instruction sh2add>
675+ : Pat<(add_like (XLenVT GPR:$r), CSImm12MulBy4:$i),
676+ (sh2add (XLenVT (ADDI (XLenVT X0), CSImm12MulBy4:$i)),
679677 GPR:$r)>;
680- def : Pat<(add_like (XLenVT GPR:$r), CSImm12MulBy8:$i),
681- (SH3ADD (XLenVT (ADDI (XLenVT X0), CSImm12MulBy8:$i)),
678+
679+ class CSImm12MulBy8Pat<Instruction sh3add>
680+ : Pat<(add_like (XLenVT GPR:$r), CSImm12MulBy8:$i),
681+ (sh3add (XLenVT (ADDI (XLenVT X0), CSImm12MulBy8:$i)),
682682 GPR:$r)>;
683683
684+ let Predicates = [HasStdExtZba] in {
685+
686+ foreach i = {1,2,3} in {
687+ defvar shxadd = !cast<Instruction>("SH"#i#"ADD");
688+ defm : ShxAddPat<i, shxadd>;
689+ }
690+
691+ def : CSImm12MulBy4Pat<SH2ADD>;
692+ def : CSImm12MulBy8Pat<SH3ADD>;
684693} // Predicates = [HasStdExtZba]
685694
695+ multiclass ADD_UWPat<Instruction add_uw> {
696+ def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFF), GPR:$rs2)),
697+ (add_uw GPR:$rs1, GPR:$rs2)>;
698+ def : Pat<(i64 (and GPR:$rs, 0xFFFFFFFF)), (add_uw GPR:$rs, (XLenVT X0))>;
699+ }
700+
701+ multiclass ShxAdd_UWPat<int i, Instruction shxadd_uw> {
702+ def : Pat<(i64 (add_like_non_imm12 (shl (and GPR:$rs1, 0xFFFFFFFF), (i64 i)),
703+ (XLenVT GPR:$rs2))),
704+ (shxadd_uw GPR:$rs1, GPR:$rs2)>;
705+ def : Pat<(i64 (riscv_shl_add (and GPR:$rs1, 0xFFFFFFFF), (i64 i), GPR:$rs2)),
706+ (shxadd_uw GPR:$rs1, GPR:$rs2)>;
707+
708+ defvar pat = !cast<ComplexPattern>("sh"#i#"add_uw_op");
709+ // More complex cases use a ComplexPattern.
710+ def : Pat<(i64 (add_like_non_imm12 pat:$rs1, (XLenVT GPR:$rs2))),
711+ (shxadd_uw pat:$rs1, GPR:$rs2)>;
712+ }
713+
714+ multiclass Sh1Add_UWPat<Instruction sh1add_uw> {
715+ def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 1)), 0x1FFFFFFFF),
716+ (XLenVT GPR:$rs2))),
717+ (sh1add_uw GPR:$rs1, GPR:$rs2)>;
718+ // Use SRLI to clear the LSBs and SHXADD_UW to mask and shift.
719+ def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x1FFFFFFFE),
720+ (XLenVT GPR:$rs2))),
721+ (sh1add_uw (XLenVT (SRLI GPR:$rs1, 1)), GPR:$rs2)>;
722+ }
723+
724+ multiclass Sh2Add_UWPat<Instruction sh2add_uw> {
725+ def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 2)), 0x3FFFFFFFF),
726+ (XLenVT GPR:$rs2))),
727+ (sh2add_uw GPR:$rs1, GPR:$rs2)>;
728+ // Use SRLI to clear the LSBs and SHXADD_UW to mask and shift.
729+ def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x3FFFFFFFC),
730+ (XLenVT GPR:$rs2))),
731+ (sh2add_uw (XLenVT (SRLI GPR:$rs1, 2)), GPR:$rs2)>;
732+ }
733+
734+ multiclass Sh3Add_UWPat<Instruction sh3add_uw> {
735+ def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFF8),
736+ (XLenVT GPR:$rs2))),
737+ (sh3add_uw (XLenVT (SRLIW GPR:$rs1, 3)), GPR:$rs2)>;
738+ // Use SRLI to clear the LSBs and SHXADD_UW to mask and shift.
739+ def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x7FFFFFFF8),
740+ (XLenVT GPR:$rs2))),
741+ (sh3add_uw (XLenVT (SRLI GPR:$rs1, 3)), GPR:$rs2)>;
742+ }
743+
744+ class Sh1AddPat<Instruction sh1add>
745+ : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFE),
746+ (XLenVT GPR:$rs2))),
747+ (sh1add (XLenVT (SRLIW GPR:$rs1, 1)), GPR:$rs2)>;
748+
749+ class Sh2AddPat<Instruction sh2add>
750+ : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFC),
751+ (XLenVT GPR:$rs2))),
752+ (sh2add (XLenVT (SRLIW GPR:$rs1, 2)), GPR:$rs2)>;
753+
754+ class Sh3AddPat<Instruction sh3add>
755+ : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFF8),
756+ (XLenVT GPR:$rs2))),
757+ (sh3add (XLenVT (SRLIW GPR:$rs1, 3)), GPR:$rs2)>;
758+
686759let Predicates = [HasStdExtZba, IsRV64] in {
687760def : Pat<(i64 (shl (and GPR:$rs1, 0xFFFFFFFF), uimm5:$shamt)),
688761 (SLLI_UW GPR:$rs1, uimm5:$shamt)>;
@@ -691,47 +764,21 @@ def : Pat<(i64 (shl (and GPR:$rs1, 0xFFFFFFFF), uimm5:$shamt)),
691764def : Pat<(i64 (and GPR:$rs1, Shifted32OnesMask:$mask)),
692765 (SLLI_UW (XLenVT (SRLI GPR:$rs1, Shifted32OnesMask:$mask)),
693766 Shifted32OnesMask:$mask)>;
694- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFF), GPR:$rs2)),
695- (ADD_UW GPR:$rs1, GPR:$rs2)>;
696- def : Pat<(i64 (and GPR:$rs, 0xFFFFFFFF)), (ADD_UW GPR:$rs, (XLenVT X0))>;
697-
698- foreach i = {1,2,3} in {
699- defvar shxadd_uw = !cast<Instruction>("SH"#i#"ADD_UW");
700- def : Pat<(i64 (add_like_non_imm12 (shl (and GPR:$rs1, 0xFFFFFFFF), (i64 i)), (XLenVT GPR:$rs2))),
701- (shxadd_uw GPR:$rs1, GPR:$rs2)>;
702- def : Pat<(i64 (riscv_shl_add (and GPR:$rs1, 0xFFFFFFFF), (i64 i), GPR:$rs2)),
703- (shxadd_uw GPR:$rs1, GPR:$rs2)>;
704- }
705767
706- def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 1)), 0x1FFFFFFFF), (XLenVT GPR:$rs2))),
707- (SH1ADD_UW GPR:$rs1, GPR:$rs2)>;
708- def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 2)), 0x3FFFFFFFF), (XLenVT GPR:$rs2))),
709- (SH2ADD_UW GPR:$rs1, GPR:$rs2)>;
710- def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 3)), 0x7FFFFFFFF), (XLenVT GPR:$rs2))),
711- (SH3ADD_UW GPR:$rs1, GPR:$rs2)>;
768+ defm : ADD_UWPat<ADD_UW>;
712769
713- // More complex cases use a ComplexPattern.
714770foreach i = {1,2,3} in {
715- defvar pat = !cast<ComplexPattern>("sh"#i#"add_uw_op");
716- def : Pat<(i64 (add_like_non_imm12 pat:$rs1, (XLenVT GPR:$rs2))),
717- (!cast<Instruction>("SH"#i#"ADD_UW") pat:$rs1, GPR:$rs2)>;
771+ defvar shxadd_uw = !cast<Instruction>("SH"#i#"ADD_UW");
772+ defm : ShxAdd_UWPat<i, shxadd_uw>;
718773}
719774
720- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFE), (XLenVT GPR:$rs2))),
721- (SH1ADD (XLenVT (SRLIW GPR:$rs1, 1)), GPR:$rs2)>;
722- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFFC), (XLenVT GPR:$rs2))),
723- (SH2ADD (XLenVT (SRLIW GPR:$rs1, 2)), GPR:$rs2)>;
724- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0xFFFFFFF8), (XLenVT GPR:$rs2))),
725- (SH3ADD (XLenVT (SRLIW GPR:$rs1, 3)), GPR:$rs2)>;
726-
727- // Use SRLI to clear the LSBs and SHXADD_UW to mask and shift.
728- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x1FFFFFFFE), (XLenVT GPR:$rs2))),
729- (SH1ADD_UW (XLenVT (SRLI GPR:$rs1, 1)), GPR:$rs2)>;
730- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x3FFFFFFFC), (XLenVT GPR:$rs2))),
731- (SH2ADD_UW (XLenVT (SRLI GPR:$rs1, 2)), GPR:$rs2)>;
732- def : Pat<(i64 (add_like_non_imm12 (and GPR:$rs1, 0x7FFFFFFF8), (XLenVT GPR:$rs2))),
733- (SH3ADD_UW (XLenVT (SRLI GPR:$rs1, 3)), GPR:$rs2)>;
775+ defm : Sh1Add_UWPat<SH1ADD_UW>;
776+ defm : Sh2Add_UWPat<SH2ADD_UW>;
777+ defm : Sh3Add_UWPat<SH3ADD_UW>;
734778
779+ def : Sh1AddPat<SH1ADD>;
780+ def : Sh2AddPat<SH2ADD>;
781+ def : Sh3AddPat<SH3ADD>;
735782} // Predicates = [HasStdExtZba, IsRV64]
736783
737784let Predicates = [HasStdExtZbcOrZbkc] in {
0 commit comments