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

            Bug ID: 111617
           Summary: Unnecessary instructions generated when comparing
                    mixed-sign small integers
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: davidfromonline at gmail dot com
  Target Milestone: ---

Compiling with `-std=c2x -O3`

```c
bool a(signed char x, unsigned char y) {
        return x == y;
}

bool b(short x, unsigned short y) {
        return x == y;
}

bool c(int x, unsigned y) {
        return x == y;
}

bool d(long x, unsigned long y) {
        return x == y;
}

bool e(long long x, unsigned long long y) {
        return x == y;
}
```

causes gcc to generate

```asm
a:
        movsx   edi, dil
        movzx   esi, sil
        cmp     edi, esi
        sete    al
        ret
b:
        movsx   edi, di
        movzx   esi, si
        cmp     edi, esi
        sete    al
        ret
c:
        cmp     edi, esi
        sete    al
        ret
d:
        cmp     rdi, rsi
        sete    al
        ret
e:
        cmp     rdi, rsi
        sete    al
        ret
```

The `movsx` and `movzx` seem unnecessary, and are not emitted by clang.

See it live: https://godbolt.org/z/dfc93f7Pv

Reply via email to