@@ -484,12 +484,13 @@ let OtherPredicates = [isGFX10Plus, Has16BitInsts], True16Predicate = NotHasTrue
484484 defm: Ternary_i16_Pats_gfx9<mul, add, V_MAD_U16_gfx9_e64>;
485485} // End OtherPredicates = [isGFX10Plus, Has16BitInsts], True16Predicate = NotHasTrue16BitInsts
486486
487- class ThreeOpFragSDAG<SDPatternOperator op1, SDPatternOperator op2> : PatFrag<
487+ class ThreeOpFragSDAG<SDPatternOperator op1, SDPatternOperator op2, bit op1IsRight = 0 > : PatFrag<
488488 (ops node:$x, node:$y, node:$z),
489489 // When the inner operation is used multiple times, selecting 3-op
490490 // instructions may still be beneficial -- if the other users can be
491491 // combined similarly. Let's be conservative for now.
492- (op2 (HasOneUseBinOp<op1> node:$x, node:$y), node:$z),
492+ !if(op1IsRight, (op2 node:$z, (HasOneUseBinOp<op1> node:$x, node:$y)),
493+ (op2 (HasOneUseBinOp<op1> node:$x, node:$y), node:$z)),
493494 [{
494495 // Only use VALU ops when the result is divergent.
495496 if (!N->isDivergent())
@@ -516,7 +517,10 @@ class ThreeOpFragSDAG<SDPatternOperator op1, SDPatternOperator op2> : PatFrag<
516517 let PredicateCodeUsesOperands = 1;
517518}
518519
519- class ThreeOpFrag<SDPatternOperator op1, SDPatternOperator op2> : ThreeOpFragSDAG<op1, op2> {
520+ // Matches (op2 (op1 x, y), z) if op1IsRight = 0 and
521+ // matches (op2 z, (op1, x, y)) if op1IsRight = 1.
522+ class ThreeOpFrag<SDPatternOperator op1, SDPatternOperator op2,
523+ bit op1IsRight = 0> : ThreeOpFragSDAG<op1, op2, op1IsRight> {
520524 // The divergence predicate is irrelevant in GlobalISel, as we have
521525 // proper register bank checks. We just need to verify the constant
522526 // bus restriction when all the sources are considered.
@@ -806,12 +810,19 @@ def : GCNPat<
806810 (DivergentBinFrag<mul> i32:$src0, IsPow2Plus1:$src1),
807811 (V_LSHL_ADD_U32_e64 i32:$src0, (i32 (Log2_32 imm:$src1)), i32:$src0)>;
808812
809- let SubtargetPredicate = isGFX940Plus in
813+ let SubtargetPredicate = isGFX940Plus in {
810814def : GCNPat<
811815 (ThreeOpFrag<shl_0_to_4, add> i64:$src0, i32:$src1, i64:$src2),
812816 (V_LSHL_ADD_U64_e64 VSrc_b64:$src0, VSrc_b32:$src1, VSrc_b64:$src2)
813817>;
814818
819+ def : GCNPat <
820+ // (ptradd z, (shl x, y)) -> ((x << y) + z)
821+ (ThreeOpFrag<shl_0_to_4, ptradd, /*op1IsRight=*/1> i64:$src0, i32:$src1, i64:$src2),
822+ (V_LSHL_ADD_U64_e64 VSrc_b64:$src0, VSrc_b32:$src1, VSrc_b64:$src2)
823+ >;
824+ } // End SubtargetPredicate = isGFX940Plus
825+
815826def : VOPBinOpClampPat<saddsat, V_ADD_I32_e64, i32>;
816827def : VOPBinOpClampPat<ssubsat, V_SUB_I32_e64, i32>;
817828
@@ -880,19 +891,24 @@ multiclass IMAD32_Pats <VOP3_Pseudo inst> {
880891
881892// Handle cases where amdgpu-codegenprepare-mul24 made a mul24 instead of a normal mul.
882893// We need to separate this because otherwise OtherPredicates would be overriden.
883- class IMAD32_Mul24_Pat<VOP3_Pseudo inst>: GCNPat <
884- (i64 (add (i64 (AMDGPUmul_u24 i32:$src0, i32:$src1)), i64:$src2)),
885- (inst $src0, $src1, $src2, 0 /* clamp */)
886- >;
894+ class IMAD32_Mul24_Pats_Impl<VOP3_Pseudo inst, SDPatternOperator AddOp, bit mulIsRight = 0> : GCNPat <
895+ !if(mulIsRight, (i64 (AddOp i64:$src2, (i64 (AMDGPUmul_u24 i32:$src0, i32:$src1)))),
896+ (i64 (AddOp (i64 (AMDGPUmul_u24 i32:$src0, i32:$src1)), i64:$src2))),
897+ (inst $src0, $src1, $src2, 0 /* clamp */)>;
898+
899+ multiclass IMAD32_Mul24_Pats<VOP3_Pseudo inst> {
900+ def : IMAD32_Mul24_Pats_Impl<inst, add>;
901+ def : IMAD32_Mul24_Pats_Impl<inst, ptradd, /*mulIsRight=*/1>;
902+ }
887903
888904// exclude pre-GFX9 where it was slow
889905let OtherPredicates = [HasNotMADIntraFwdBug], SubtargetPredicate = isGFX9Plus in {
890906 defm : IMAD32_Pats<V_MAD_U64_U32_e64>;
891- def : IMAD32_Mul24_Pat <V_MAD_U64_U32_e64>;
907+ defm : IMAD32_Mul24_Pats <V_MAD_U64_U32_e64>;
892908}
893909let OtherPredicates = [HasMADIntraFwdBug], SubtargetPredicate = isGFX11Only in {
894910 defm : IMAD32_Pats<V_MAD_U64_U32_gfx11_e64>;
895- def : IMAD32_Mul24_Pat <V_MAD_U64_U32_gfx11_e64>;
911+ defm : IMAD32_Mul24_Pats <V_MAD_U64_U32_gfx11_e64>;
896912}
897913
898914def VOP3_PERMLANE_Profile : VOP3_Profile<VOPProfile <[i32, i32, i32, i32]>, VOP3_OPSEL> {
0 commit comments