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

            Bug ID: 105137
           Summary: Missed optimization 64-bit adds and shifts
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andre.schackier at gmail dot com
  Target Milestone: ---

Given the following source code [godbolt](https://godbolt.org/z/8KMMhefqY)

#include <stdint.h>

typedef __int128_t int128_t;
int64_t foo(int128_t a, int64_t b, int cond) {
    if (cond) {
        a += ((int128_t)b) << 64;
    }
    return a >> 64;
}

int64_t bar(int128_t a, int64_t b, int cond) {
    int64_t r = a >> 64;
    if (cond) {
        r += b;
    }
    return r;
}

Compiling with "-O3" we get:

foo:
        mov     rax, rsi
        mov     rsi, rdi
        mov     rdi, rax
        test    ecx, ecx
        je      .L2
        xor     r8d, r8d
        add     rsi, r8
        adc     rdi, rdx
.L2:
        mov     rax, rdi
        ret
bar:
        add     rdx, rsi
        mov     rax, rsi
        test    ecx, ecx
        cmovne  rax, rdx
        ret

Although both functions do the same, gcc implements worse code for foo.

Credits: This was entirely found by Trevor Spiteri reported at the llvm-project
here: https://github.com/llvm/llvm-project/issues/54718

Reply via email to