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

            Bug ID: 114524
           Summary: Use less expensive expression when expressions are
                    equal
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: goon.pri.low at gmail dot com
  Target Milestone: ---

When we know multiple expressions are equal, we should only evaluate the least
expensive one.

Example functions:

int a(int b, int *restrict c) {
    if (b != *c) __builtin_unreachable();
    return *c;
}

int b(int b, int c) {
    if (b != c + 1) __builtin_unreachable();
    return c + 1;
}

int c(int b, struct {int a; int b;} c) {
    if (b != c.b) __builtin_unreachable();
    return c.b;
}

int d(int b, int c) {
    if (b * 44 != c / 10) __builtin_unreachable();
    return c / 10;
}

Generated assembly:

a:
        mov     eax, DWORD PTR [rsi]
        ret
b:
        lea     eax, [rsi+1]
        ret
c:
        shr     rsi, 32
        mov     eax, esi
        ret
d:
        movsx   rax, esi
        sar     esi, 31
        imul    rax, rax, 1717986919
        sar     rax, 34
        sub     eax, esi
        ret

Optimal assembly (clang)

a:                                      # @a
        mov     eax, edi
        ret
b:                                      # @b
        mov     eax, edi
        ret
c:                                      # @c
        mov     eax, edi
        ret
d:                                      # @d
        imul    eax, edi, 44
        ret
  • [Bug rtl-optimization/114524] N... goon.pri.low at gmail dot com via Gcc-bugs

Reply via email to