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

            Bug ID: 95784
           Summary: Failure to optimize usage of __builtin_add_overflow
                    with return statement properly
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

int f(uint8_t operand, int8_t *result)
{
    if (__builtin_add_overflow(operand, 0, result))
    {
        *result = 0;
        return 10213;
    }
    return 0;
}

With -O3, LLVM outputs this :

f(unsigned char, signed char*): # @f(unsigned char, signed char*)
  movsx eax, dil
  xor ecx, ecx
  cmp edi, eax
  cmovne edi, ecx
  mov byte ptr [rsi], dil
  mov eax, 10213
  cmove eax, ecx
  ret

GCC outputs this :

f(unsigned char, signed char*):
  movzx eax, dil
  movsx di, dil
  cmp ax, di
  setne dl
  mov r8d, edx
  sal r8d, 31
  sar r8d, 31
  and r8d, 10213
  test dl, dl
  mov edx, 0
  cmovne eax, edx
  mov BYTE PTR [rsi], al
  mov eax, r8d
  ret

Reply via email to