On 9/16/26 3:06 PM, Dominic P wrote:
Extend movement_possibility_1 to shift counts that are not known to be in
range, following up on r17-3550: rather than proving the count in range,
hoist the shift and reduce the count where it lands.
gimple_needing_rewrite_undefined reports a shift or rotate whose count may
be out of range, and rewrite_to_defined_unconditional masks it with
precision - 1, or clamps it as unsigned where the precision is not a power
of two.  Such a shift costs 1 rather than LIM_EXPENSIVE, so it moves only
with an expensive statement that depends on it.

v5 asked that of every caller of the predicate, which was wrong.
if-conversion rewrites the statements it makes unconditional too, so the
mask reached vectorised loops as a vector BIT_AND_EXPR: one instruction per
iteration, and a pack and two unpacks at 64 bits.  Nine scans in
gcc.target/i386/cond_op_shift_{q,ud,uq}-1.c fail on that, and the mask buys
nothing, since the shift feeds a select on the original predicate.  The
predicate now takes a SHIFT_COUNT parameter, false by default, that only
the three loop-im callers pass.

Bootstrapped and regression tested on x86_64-pc-linux-gnu.

Assisted-by: Claude Opus 5 (Anthropic)

gcc/ChangeLog:

        * gimple-fold.h (gimple_needing_rewrite_undefined): Add a
        SHIFT_COUNT parameter defaulting to false.
        * gimple-fold.cc (gimple_needing_rewrite_undefined): Return true
        for a shift or rotate whose count may be out of range, only when
        SHIFT_COUNT.
        (rewrite_to_defined_unconditional): Reduce such a count into range.
        * tree-ssa-loop-im.cc (shift_needs_mask_p): New function.
        (determine_max_movement): Cost a shift whose count would need
        masking as cheap.
        (movement_possibility_1): Require preserved execution for a
        possibly out-of-range count only when the rewrite cannot reduce it
        or the loop is optimized for size.

gcc/testsuite/ChangeLog:

        * gcc.dg/tree-ssa/loop-im-shift-2.c: Expect every chain but a lone
        shift's to move; check the masks.
        * gcc.dg/tree-ssa/loop-im-shift-3.c: New test.
        * gcc.dg/tree-ssa/loop-im-shift-4.c: New test.
        * gcc.dg/tree-ssa/loop-im-shift-5.c: New test.
        * gcc.dg/tree-ssa/loop-im-shift-6.c: New test.

Signed-off-by: Dominic P <[email protected]>
THanks.  I've pushed this to the trunk.
jeff

Reply via email to