https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128834

--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <[email protected]>:

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]>

Reply via email to