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

            Bug ID: 125811
           Summary: Possible Missed Optimization at O3
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: apowenq at gmail dot com
  Target Milestone: ---

cat prog.c
#include <stdint.h>
uint8_t src(uint8_t v0_u8) {
  if (!((0 <= v0_u8) && (v0_u8 <= 2))) __builtin_unreachable();
  uint8_t i0_u8 = 1 << v0_u8;
  uint8_t i1_u8 = v0_u8 * i0_u8;
  return i1_u8;
}

GCC truck at O3 cannot optimize the C program, but Clang truck at O3 can.
Reproducer: https://godbolt.org/z/WqhqoEKb3

GCC at O3:
src:
        mov     ecx, edi
        mov     edx, 1
        sal     edx, cl
        mov     eax, edx
        imul    eax, edi
        ret

Clang at O3:
src:
        mov     eax, edi
        mov     ecx, eax
        shl     al, cl
        ret

If change `uint8_t` to `int`, GCC O3 can optimize the program like Clang.

Reply via email to