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

--- Comment #3 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:e1636028f8acd23c03b52b5b8a2b567a1f6e89f8

commit r17-6752-ge1636028f8acd23c03b52b5b8a2b567a1f6e89f8
Author: Jakub Jelinek <[email protected]>
Date:   Fri Nov 27 09:59:01 2026 +0100

    bitintlower: Avoid merging non-mergeable stmts with plus_minus or lshift
[PR128699]

    The handle_plus_minus and handle_lshift (note, the latter handles
    only mergeable shifts, so LSHIFT_EXPR with constant shift count <
limb_prec)
    need state (carry for the former, bits from the previous lower limb if any
    in the latter) and as such they really can't be merged with some outermost
    operation which isn't expanded in m_upwards_2limb mode, i.e. sequentially
    from least significant to most significant limb, with 2 limbs per
iteration.

    While gimple_lower_bitint has complicated code to decide whether some
    SSA_NAME can be merged or not, those decisions always look at most at the
    SSA_NAME_DEF_STMT of some SSA_NAME, its single use_stmt (if any) and
    sometimes SSA_NAME_DEF_STMT of the rhs1 of the current stmt (all of which
    obviously are supposed to be in the same bb in order to optimize), here
    we need to analyze the whole chain of supposedly merged stmts and see if
    it is appropriate and if not, mark some SSA_NAME in the chain as needing
    an underlying VAR_DECL.  In the current loop, we process SSA_NAMEs in
    order of increasing SSA_NAME_VERSION, so making such decisions can be
    premature unless we repeat all the checking for all the SSA_NAMEs we
    process.

    So, this patch instead turns the body of the loop into a lambda, replaces
    all continue; stmts that skipped to next iteration with return false; or
    return true; (where the latter means it is the last SSA_NAME before some
    non-mergeable operation) and does the loop after the lambda definition,
    for the cases where the lambda returns true uses a recursive function to
    find out if a chain potentially includes some plus_minus or lshift and if
    so, remembers it in a vector, then walks this vector and calls that
    recursive function again (things could have changed, some SSA_NAMEs along
    the path could be added to large_huge.m_names) and if still true, forces
    it into large_huge.m_names.

    I've run
    make -j32 -k check-gcc GCC_TEST_RUN_EXPENSIVE=1
RUNTESTFLAGS="GCC_TEST_RUN_EXPENSIVE=1 dg.exp='*bitint* pr112673.c
builtin-stdc-bit-*.c pr112566-2.c pr112511.c pr116588.c pr116003.c
    +pr113693.c pr113602.c flex-array-counted-by-7.c' dg-torture.exp='*bitint*
pr116480-2.c pr114312.c pr114121.c' dfp.exp=*bitint*
vect.exp='vect-early-break_99-pr113287.c'
    +tree-ssa.exp=pr113735.c"
    with additional instrumentation and this patch changed behavior solely
    on the two new tests, so I think it won't pessimize code in real-world
    too much.

    Note, in theory handle_lshift could be still handled differently in the
    !m_upwards_2limb case by simply asking for the lower idx too, but for
    non-constant idx it would be quite ugly.  plus_minus (i.e. PLUS_EXPR,
    MINUS_EXPR, NEGATE_EXPR) can't be changed, the carry can depend on
    all the lower additions at each point, not just about the lower limb.

    2026-11-27  Jakub Jelinek  <[email protected]>

            PR middle-end/128699
            PR middle-end/128982
            * gimple-lower-bitint.cc (requires_upwards_2limb): New function.
            (gimple_lower_bitint): Make sure chains of mergeable_op and
            gimple_assign_cast_p without large_huge.m_names entry which
            include PLUS/MINUS/NEGATE/LSHIFT aren't merged into a
            non-m_upwards_2limb outermost stmt.

            * gcc.dg/bitint-153.c: New test.
            * gcc.dg/bitint-154.c: New test.

    Reviewed-by: Richard Biener <[email protected]>

Reply via email to