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

            Bug ID: 93565
           Summary: Combine duplicates count trailing zero instructions
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wilco at gcc dot gnu.org
  Target Milestone: ---

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

The attached example causes Combine to duplicate count trailing zero
instructions on targets which have CTZ_DEFINED_VALUE = 2:

f:
        cbz     x0, .L2
        rbit    x2, x0
        rbit    x0, x0
        clz     x2, x2
        clz     x0, x0
        ldr     w2, [x1, x2, lsl 2]
        orr     w0, w2, w0
        str     w0, [x1]
.L2:
        mov     x0, 0
        ret

The cause is Combine deciding to merge CTZ into a sign-extend:

(insn 10 9 12 3 (set (reg:DI 100)
        (ctz:DI (reg/v:DI 98 [ x ]))) "ctz2.c":17:15 689 {ctzdi2}
     (expr_list:REG_DEAD (reg/v:DI 98 [ x ])
        (nil)))
(insn 12 10 14 3 (set (reg:DI 101 [ _9 ])
        (sign_extend:DI (subreg:SI (reg:DI 100) 0))) "ctz2.c":25:15 104
{*extendsidi2_aarch64}
     (nil))

allowing combination of insns 10 and 12
original costs 4 + 4 = 8
replacement costs 4 + 4 = 8
modifying insn i2    10: r100:DI=ctz(r98:DI)
deferring rescan insn with uid = 10.
modifying insn i3    12: r101:DI=ctz(r98:DI)
      REG_DEAD r98:DI

(insn 10 9 12 3 (set (reg:DI 100)
        (ctz:DI (reg/v:DI 98 [ x ]))) "ctz2.c":17:15 689 {ctzdi2}
     (nil))
(insn 12 10 14 3 (set (reg:DI 101 [ _9 ])
        (ctz:DI (reg/v:DI 98 [ x ]))) "ctz2.c":25:15 689 {ctzdi2}
     (expr_list:REG_DEAD (reg/v:DI 98 [ x ])
        (nil)))

Later passes then seem unable to CSE the two identical CTZ instructions...

This doesn't seem right - if Combine can optimize the sign-extend away then
there is no point in duplicating the CTZ.

Reply via email to