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

            Bug ID: 123133
           Summary: GCC misses opportunity to use ranges to elide
                    extensions
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tnfchris at gcc dot gnu.org
  Target Milestone: ---

The following testcase

#include <stdint.h>

int64_t bar(int32_t in) {
    return (7 - (in % 8));
}

at -O3 produces

bar(int):
        negs    w1, w0
        mov     w2, 7
        and     w0, w0, w2
        and     w1, w1, w2
        csneg   w1, w0, w1, mi
        sub     w0, w2, w1
        sxtw    x0, w0
        ret

but the result of the subtract is guaranteed to be positive so the sign
extension isn't needed.

Clang produces the optimal

bar(int):
        negs    w9, w0
        and     w10, w0, #0x7
        mov     w8, #7
        and     w9, w9, #0x7
        csneg   w9, w10, w9, mi
        sub     w0, w8, w9
        ret

Reply via email to