> Best guess is the optimized form causes us to miss the saturation discovery ..
Yes, seems to be the case. In match-sat-alu.pd there are matchers like this one, for example: (match (signed_integer_sat_add @0 @1) /* T SUM = (T)((UT)X + (UT)IMM) SAT_S_ADD = (X ^ SUM) < 0 && (X ^ IMM) >= 0 ? (-(T)(X < 0) ^ MAX) : SUM */ (cond^ (bit_and:c (lt (bit_xor @0 (nop_convert@2 (plus (nop_convert @0) INTEGER_CST@1))) integer_zerop) (ge (bit_xor:c @0 INTEGER_CST@3) integer_zerop)) (signed_integer_sat_val @0) @2) ... Some of these matchers seem don't to trigger any more after our new fold. I guess we need either some new sat-matchers to consider our new fold, or we have to exclude some cases from the new folds so that old sat-matchers trigger. Or maybe something else ? The former variant seems more correct to me. I'll have more time to look into this towards middle of the week. > As an aside, it would be useful to move/copy some of these SAT_ADD > detection testcases to be in the vect.exp testsuite with the > appropriate target supports and scanning for SAT_ADD. Makes sense. I can check that too. Thanks, Daniel ________________________________________ From: Andrea Pinski <[email protected]> Sent: 22 November 2026 21:03 To: Daniel Petrovic <[email protected]> Cc: Jeff Law <[email protected]>; [email protected] <[email protected]>; Richard Biener <[email protected]>; [email protected] <[email protected]> Subject: Re: [PATCH] match.pd: Fold (X | A) cmp C and (X ^ A) cmp C into X cmp C [PR115392] On Sun, Nov 22, 2026 at 11:30 AM Daniel Petrovic <[email protected]> wrote: > > Hi Jeff, > > Thanks for committing the patch and for tracking down the regression. > > I'll try to reproduce that regression and figure this out. I’m not sure how > long the investigation will take, so I’m fine with reverting the patch in the > meantime. Ok, I can reproduce the regression on x86_64 though there is no testcase for it. See https://godbolt.org/z/4dqzjeEcj for the full testcase. As you can see GCC 16.2.0 can detect the SAT_ADD while the trunk does not. As an aside, it would be useful to move/copy some of these SAT_ADD detection testcases to be in the vect.exp testsuite with the appropriate target supports and scanning for SAT_ADD. Thanks, Andrea > > Thanks, > Daniel > > > ________________________________ > From: Jeff Law <[email protected]> > Sent: 22 November 2026 18:45 > To: Daniel Petrovic <[email protected]>; [email protected] > <[email protected]> > Cc: Richard Biener <[email protected]>; [email protected] > <[email protected]>; Andrea Pinski <[email protected]> > Subject: Re: [PATCH] match.pd: Fold (X | A) cmp C and (X ^ A) cmp C into X > cmp C [PR115392] > > > > On 11/21/26 11:55 PM, Daniel Petrovic wrote: > > @Richard Biener Thanks again for the review > @Jeff Law Thanks for committing > > NP. Note it's causing overnight regressions on the RISC-V port. The code > is clearly worse after this patch; it looks like the rewrite interferes with > saturated arithmetic discovery in a very generic way. > > Tests that now fail, but worked before (166 tests): > > unix/-march=rv64gc_zba_zbb_zbs_zicond: gcc: > gcc.target/riscv/rvv/autovec/sat/vec_sat_s_add_imm-1-i16.c -ftree-vectorize > -O2 -mrvv-vector-bits=scalable -mrvv-max-lmul=dynamic -ffast-math > scan-assembler-times vsadd\\.vi 1 > unix/-march=rv64gc_zba_zbb_zbs_zicond: gcc: > gcc.target/riscv/rvv/autovec/sat/vec_sat_s_add_imm-1-i16.c -ftree-vectorize > -O2 -mrvv-vector-bits=scalable -mrvv-max-lmul=dynamic -ffast-math > scan-tree-dump-times optimized ".SAT_ADD " 2 > > [ ... ] > > If we look at the .original dump we have this statement before your change: > > *(out + (sizetype) ((long unsigned int) i * 2)) = ((short int) x ^ 9) >= 0 >? ((short int) sum ^ (short int) x) < 0 > ? x < 0 ? -32768 : 32767 : sum : sum; > > Which turns into: > > vect_x_15.10_88 = .MASK_LEN_LOAD (vectp_op_1.8_83, 16B, { -1, ... }, _87(D), >_97, 0); > vect_patt_25.11_89 = .SAT_ADD (vect_x_15.10_88, { 9, ... }); > [ ... ] > .MASK_LEN_STORE (vectp_out.12_90, 16B, { -1, ... }, _97, 0, >vect_patt_25.11_89); > > Which looks pretty sensible. > > > After your change we get: > > *(out + (sizetype) ((long unsigned int) i * 2)) = x >= 0 ? ((short int) >sum ^ (short int) x) < 0 ? x < 0 ? -32768 : > 32767 : sum : sum; > > > We can see your simplification in the .original dump. It's semantically > correct and profitable according to our gimple cost model but in the first > vectorization pass: > > vect_x_13.10_51 = .MASK_LEN_LOAD (vectp_op_1.8_46, 16B, { -1, ... }, _50(D), >_66, 0); > mask__19.15_56 = vect_x_13.10_51 >= { 0, ... }; > vect_x.11_52 = VIEW_CONVERT_EXPR<vector([32,32]) unsigned >short>(vect_x_13.10_51); > vect__5.12_53 = vect_x.11_52 + { 9, ... }; > vect_sum_14.13_54 = VIEW_CONVERT_EXPR<vector([32,32]) short >int>(vect__5.12_53); > mask__18.14_55 = vect_sum_14.13_54 < { 0, ... }; > mask__20.16_57 = mask__18.14_55 & mask__19.15_56; > vect_iftmp.17_58 = VEC_COND_EXPR <mask__20.16_57, { 32767, ... }, >vect_sum_14.13_54>; > .MASK_LEN_STORE (vectp_out.18_59, 16B, { -1, ... }, _66, 0, >vect_iftmp.17_58); > > Definitely not good. Best guess is the optimized form causes us to miss the > saturation discovery and we get the mess above. So the question turns into > whether or not we can discover the saturating nature after your changes. > It's an area I'm not particularly well versed in. > > > > Jeff >
