https://gcc.gnu.org/g:15066a806785f0bb24aa44a8bde974b7df003361
commit r17-6590-g15066a806785f0bb24aa44a8bde974b7df003361 Author: Jakub Jelinek <[email protected]> Date: Mon Nov 23 10:31:14 2026 +0100 widening_mul, i386: Optimize away NE_EXPR on IMAGPART_EXPR of *_OVERFLOW [PR128834] The following testcase regressed due to a change where it now prefers in the i386 backend to emit a TImode comparison rather than IOR of the two halves and DImode comparison, and the TImode comparison fails to be transformed in ce1. While that is something nice to fix in the backend too, I've realized that the widening_mul .*_OVERFLOW detection emits suboptimal GIMPLE which then nothing fixes up before expansion. IFN_*_OVERFLOW always set the IMAGPART_EXPR of the result to a value in range [0, 1], in whatever complex type we choose, because we are returning COMPLEX_TYPE and not e.g. RECORD_TYPE with some integer type and bool. Now, typically that IMAGPART_EXPR can be cast to bool, or compared against zero. If we have say _7 = .MUL_OVERFLOW (x, y); z = REALPART_EXPR <_7>; _8 = IMAGPART_EXPR <_7>; _9 = _8 != 0; iftmp.0_3 = (int) _9; then vrp passes are able to optimize the last stmt into: iftmp.0_3 = (int) _8; based on the knowledge that _8 is in [0, 1] range and subsequent dce removes the _9 stmt. Now, the problem is that when we pattern match some code as .*_OVERFLOW, we keep the NE_EXPR against 0 in the IL and don't propagate further and we don't have a vrp after it to clean this up. As it is quite easy, this patch does that, which fixes the testcase. Now, doing just that broke various other tests - pr112600*, pr79173* and pr94857.c. 3 different reasons, all fixed in this patch below: 1) match-sat-alu.pd has one case where it matched it exactly with the (convert (ne (imag ...) 0)) in there, so it doesn't match anymore; fixed by adding another match case for the same without the ne, that is equivalent for *_OVERFLOW ifns 2) the UADDC_USUBC pattern matching punts if something in the matched stmts (where needed) doesn't have single use; and when just propagating the IMAGPART_EXPR lhs into the casts from NE_EXPR result we had non-single use, the NE_EXPR and the cast (with NE_EXPR now dead but not removed until subsequent dce). Fixed up also removing the NE_EXPR if it has zero uses 3) for pr94857.c the more optimized GIMPLE just results in a slight change in scheduling and it is a PR fixed by a peephole2 which now no longer matches due to intervening 2 insns; now, I've looked around and for some other peephole2s we already have exactly the same thing, one peephole2 with some CC setting insn with memory loads and a subsequent store and another for the same thing with setcc + ext in between; this patch adds another such peephole2 for the PR94857 case The guarding on precision > 1 or unsigned is there for a theoretical only problem reported by sashiko. For ADD/SUB_OVERFLOW discovery we always have unsigned type, for MUL_OVERFLOW we can have either, but 1-bit signed division is special (0wb/~0wb is valid, anything else is UB) and what we try to pattern match is optimized away normally already during ccp. So one would need to disable a lot of optimization passes to break this. 2026-11-23 Jakub Jelinek <[email protected]> PR target/128834 * tree-ssa-math-opts.cc (match_arith_overflow): When we optimize binary assignment into lhs = ovf != 0 attempt to adjust immediate uses which cast the result of the comparison to some integer type and when optimizing all of them, remove the ovf != 0 comparison afterwards. Fix pasto in function comment. * match-sat-alu.pd (unsigned_integer_sat_add): Handle the case where imag of IFN_ADD_OVERFLOW is just converted instead of conversion of imag != 0. * config/i386/i386.md (peephole2 after @add<mode>3_cc_overflow_1): New define_peephole2. * gcc.target/i386/pr128834.c: New test. Reviewed-by: Andrea Pinski <[email protected]> Reviewed-by: Uros Bizjak <[email protected]> Diff: --- gcc/config/i386/i386.md | 27 +++++++++++++++++++++ gcc/match-sat-alu.pd | 6 +++++ gcc/testsuite/gcc.target/i386/pr128834.c | 15 ++++++++++++ gcc/tree-ssa-math-opts.cc | 40 +++++++++++++++++++++++++++++++- 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index 6be934f4d3dd..35dfbf1c9fc7 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -10758,6 +10758,33 @@ (match_dup 1))) (set (match_dup 1) (plus:SWI (match_dup 1) (match_dup 0)))])]) +(define_peephole2 + [(parallel [(set (reg:CCC FLAGS_REG) + (compare:CCC + (plus:SWI (match_operand:SWI 0 "general_reg_operand") + (match_operand:SWI 1 "memory_operand")) + (match_dup 0))) + (set (match_dup 0) (plus:SWI (match_dup 0) (match_dup 1)))]) + (set (match_operand:QI 2 "general_reg_operand") + (ltu:QI (reg:CCC FLAGS_REG) (const_int 0))) + (set (match_operand:SWI48 3 "general_reg_operand") + (zero_extend:SWI48 (match_dup 2))) + (set (match_dup 1) (match_dup 0))] + "(TARGET_READ_MODIFY_WRITE || optimize_insn_for_size_p ()) + && peep2_reg_dead_p (4, operands[0]) + && !reg_overlap_mentioned_p (operands[0], operands[1]) + && !reg_overlap_mentioned_p (operands[0], operands[2]) + && !reg_overlap_mentioned_p (operands[2], operands[1]) + && !reg_overlap_mentioned_p (operands[0], operands[3]) + && !reg_overlap_mentioned_p (operands[3], operands[1])" + [(parallel [(set (reg:CCC FLAGS_REG) + (compare:CCC + (plus:SWI (match_dup 1) (match_dup 0)) + (match_dup 1))) + (set (match_dup 1) (plus:SWI (match_dup 1) (match_dup 0)))]) + (set (match_dup 2) (ltu:QI (reg:CCC FLAGS_REG) (const_int 0))) + (set (match_dup 3) (zero_extend:SWI48 (match_dup 2)))]) + (define_peephole2 [(set (match_operand:SWI 0 "general_reg_operand") (match_operand:SWI 1 "memory_operand")) diff --git a/gcc/match-sat-alu.pd b/gcc/match-sat-alu.pd index 236d6fb2e197..00e9f5980e98 100644 --- a/gcc/match-sat-alu.pd +++ b/gcc/match-sat-alu.pd @@ -93,6 +93,12 @@ along with GCC; see the file COPYING3. If not see (bit_ior:c (realpart (IFN_ADD_OVERFLOW@2 @0 @1)) (negate (convert (ne (imagpart @2) integer_zerop)))) (if (types_match (type, @0, @1)))) + (match (unsigned_integer_sat_add @0 @1) + /* SUM = ADD_OVERFLOW (X, Y) + SAT_U_ADD = REALPART (SUM) | -IMAGPART (SUM) */ + (bit_ior:c (realpart (IFN_ADD_OVERFLOW@2 @0 @1)) + (negate (convert (imagpart @2)))) + (if (types_match (type, @0, @1)))) (match (unsigned_integer_sat_add @0 @1) /* SUM = ADD_OVERFLOW (X, Y) SAT_U_ADD = IMAGPART (SUM) == 0 ? REALPART (SUM) : -1 */ diff --git a/gcc/testsuite/gcc.target/i386/pr128834.c b/gcc/testsuite/gcc.target/i386/pr128834.c new file mode 100644 index 000000000000..5c4bc5baf275 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr128834.c @@ -0,0 +1,15 @@ +/* PR target/128834 */ +/* { dg-do compile { target int128 } } */ +/* { dg-options "-O2 -mtune=generic -march=x86-64 -fno-stack-protector -masm=att" } */ +/* { dg-final { scan-assembler-times "\tmulq\t" 1 } } */ +/* { dg-final { scan-assembler-times "\taddq\t" 1 } } */ +/* { dg-final { scan-assembler-times "\tadcq\t" 2 } } */ +/* { dg-final { scan-assembler-not "\tjn?\[cb]\t" } } */ + +void +foo (unsigned __int128 *x, unsigned long *y, unsigned long a, unsigned long b) +{ + unsigned __int128 p = (unsigned __int128) a * b; + *x += p; + *y += *x < p; +} diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc index 0fcfca4872cd..3cf7891b597a 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -4627,7 +4627,7 @@ match_saturation_trunc (gimple_stmt_iterator *gsi, gphi *phi) # iftmp.0_3 = PHI <_10(3), 0(2)> and replace it with _7 = .MUL_OVERFLOW (x, y); - z = IMAGPART_EXPR <_7>; + z = REALPART_EXPR <_7>; _8 = IMAGPART_EXPR <_7>; _9 = _8 != 0; iftmp.0_3 = (int) _9; */ @@ -5010,6 +5010,7 @@ match_arith_overflow (gimple_stmt_iterator *gsi, gimple *stmt, continue; gimple *orig_use_stmt = use_stmt; + bool remove_use_stmt = false; int ovf_use = arith_overflow_check_p (stmt, cast_stmt, use_stmt, maxval, NULL); if (ovf_use == 0) @@ -5093,6 +5094,37 @@ match_arith_overflow (gimple_stmt_iterator *gsi, gimple *stmt, gimple_assign_set_rhs_code (use_stmt, ovf_use == 1 ? NE_EXPR : EQ_EXPR); + if (ovf_use == 1 + && (TYPE_UNSIGNED (type) || TYPE_PRECISION (type) > 1)) + { + /* Try to optimize further. + _6 = ovf != 0; + _7 = (unsigned long int) _6; + where we know ovf is in [0, 1] range can be + optimized into + _7 = (unsigned long int) ovf; + Normally, vrp pass can do this, but there is + no vrp after widening_mul. */ + imm_use_iterator use2_iter; + gimple *use2_stmt; + tree cmp_lhs = gimple_assign_lhs (use_stmt); + + FOR_EACH_IMM_USE_STMT (use2_stmt, use2_iter, cmp_lhs) + { + if (!gimple_assign_cast_p (use2_stmt)) + continue; + tree cvt_lhs = gimple_assign_lhs (use2_stmt); + tree cvt_rhs = gimple_assign_rhs1 (use2_stmt); + if (INTEGRAL_TYPE_P (TREE_TYPE (cvt_lhs)) + && cvt_rhs == cmp_lhs) + { + gimple_assign_set_rhs1 (use2_stmt, ovf); + update_stmt (use2_stmt); + } + } + if (has_zero_uses (cmp_lhs)) + remove_use_stmt = true; + } } } else @@ -5124,6 +5156,12 @@ match_arith_overflow (gimple_stmt_iterator *gsi, gimple *stmt, gsi_remove (&gsi2, true); release_ssa_name (gimple_assign_lhs (orig_use_stmt)); } + if (remove_use_stmt) + { + gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt); + gsi_remove (&gsi2, true); + release_ssa_name (gimple_assign_lhs (use_stmt)); + } } if (maxval) {
