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

            Bug ID: 128955
           Summary: [rs6000] Redundant zero extension (rlwinm/clrlwi) of a
                    value loaded by lhz not removed by REE
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pmenzel+gcc at molgen dot mpg.de
  Target Milestone: ---

Created attachment 66258
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66258&action=edit
Reproducer

[This report was drafted with an LLM (Claude Opus 5.5 in Claude Code) and
reviewed by me; the analysis and the measurements were also done with it.]

$ cat zext.c
void
f1 (unsigned short *p)
{
  *p += (2048 - *p) >> 5;
}

GCC trunk (r17-6554-g54805df7d2f), -O2, powerpc64le (power8, same with
-mcpu=power9):

f1:
        lhz 10,0(3)
        rlwinm 9,10,0,0xffff    <- redundant, lhz already zero-extends
        subfic 9,9,2048
        srawi 9,9,5 
        add 9,9,10  
        sth 9,0(3)
        blr
                                                                               
                                                                               
                Clang 23.1.3:

f1:
        lhz 4, 0(3)
        subfic 5, 4, 2048
        srwi 5, 5, 5
        add 4, 4, 5
        sth 4, 0(3)
        blr

Before RA, the load is a HImode move, and its result is used both zero-extended
and as a subreg, so combine keeps the separate extension (IRA dump):

(insn 6 2 7 2 (set (reg:HI 125 [ _1 ])
        (mem:HI (reg/v/f:DI 131 [ p ]) [1 *p_8(D)+0 S2 A16])) {*movhi_internal}
(insn 7 6 8 2 (set (reg:SI 132 [ _2 ])
        (zero_extend:SI (reg:HI 125 [ _1 ]))) {zero_extendhisi2}
...
            (subreg:SI (reg:HI 125 [ _1 ]) 0))) {*addsi3}

postreload then rewrites the load into an explicit extension (LOAD_EXTEND_OP
(HImode) == ZERO_EXTEND), which makes insn 7 redundant:

(insn 6 3 7 2 (set (reg:DI 10 10)
        (zero_extend:DI (mem:HI (reg/v/f:DI 3 3 [orig:131 p ] [131]) [1
*p_8(D)+0 S2 A16]))) {zero_extendhidi2}
(insn 7 6 8 2 (set (reg:SI 9 9 [orig:132 _2 ] [132])
        (zero_extend:SI (reg:HI 10 10 [orig:125 _1 ] [125])))
{zero_extendhisi2}

REE sees the candidate but does not remove it:

  Elimination opportunities = 1 realized = 0

REE seems to only try to merge the extension into its definition, which fails
here because the definition already is a (wider) zero extension and no longer
sets HImode; removing an extension that all reaching definitions have already
done is not attempted.

This looks like a simpler instance of the family in PR107949 (there the
redundant extension follows an xor of two zero-extended byte loads; here it
directly follows the load). 

Impact: in the range coder of the xz-embedded LZMA2 decoder
(linux/lib/xz/xz_dec_lzma2.c [1], rc_bit),

    bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob;

compiles to

    lhz     r3,176(r7)
    clrlwi  r6,r3,16
    mullw   r9,r9,r6

so the extension adds two cycles on POWER8 to the critical path of every
decoded bit; lzma_main has 9 such extensions after lhz.  xz-embedded master
(52f2742), userspace benchmark decompressing i915.ko.xz, -O2
-fno-strict-aliasing -static, minimum of 5 iterations of 3 interleaved rounds,
pinned to one CPU of an idle POWER8 (3.49 GHz), with the alias change from
PR128945 applied:

    GCC 17.0.0 + alias change                  266.2 ms
    Clang 23.1.3                               264.2 ms

An experimental REE change (LLM-written, so not submittable under the GCC AI
policy) that removes these extensions brought this to 262.9 ms (-1.2 %),
slightly faster than Clang.

(The srawi vs. srwi difference is harmless: only bits that the 16-bit store
drops differ, and both take the same time; it made no measurable difference.)

[1]:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/xz/xz_dec_lzma2.c

Reply via email to