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

--- Comment #1 from Kang-Che Sung <Explorer09 at gmail dot com> ---

For signed integers that GCC can detect to be always positive, it uses signed
extension instead of zero extension, even in the "-Os" optimization mode.
For x86-64, sometimes zero extension can produce smaller code.

Test code:

```c
#include <stdint.h>

extern uint64_t subroutine(uint64_t x);

uint64_t func1a(int32_t x) {
    if (x < 50)
        return 0;
    return subroutine(x);
}

uint64_t func1b(int32_t x) {
    if (x < 50)
        return 0;
    __asm__ ("" : "+r" (x)); // Break the (x >= 50) assumption
    return subroutine((uint64_t)(uint32_t)x);
}
```

x86_64 gcc 15.1 with "-Os" option produces:

```x86asm
func1a:
    cmpl $49, %edi
    jle .L2
    movslq %edi, %rdi
    jmp subroutine
.L2:
    xorl %eax, %eax
    ret

func1b:
    cmpl $49, %edi
    jle .L5
    movl %edi, %edi
    jmp subroutine
.L5:
    xorl %eax, %eax
    ret
```

Reply via email to