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

            Bug ID: 123506
           Summary: Some unnecessary assembly generated in simple function
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tobi at gcc dot gnu.org
  Target Milestone: ---

This is a small offshot from PR123505

https://godbolt.org/z/xx54ejr1G

=================================
#include <stdint.h>
#include <string.h>

struct Result { int dec_exp; char *buffer; };

Result bar(char *buffer, int dec_exp) {
  uint16_t e_sign = dec_exp >= 0 ? ('-' << 8 | 'e') : ('+' << 8 | 'e');
  memcpy(buffer, &e_sign, 2);
  int mask = (dec_exp >= 0) - 1;
  dec_exp = ((dec_exp + mask) ^ mask);  // absolute value
  return  { dec_exp, buffer + 2 };
}
==================================

yields:
bar(char*, int):
        mov     eax, 11621
        test    esi, esi
        jns     .L2
        neg     esi
        mov     eax, 11109
.L2:
        mov     WORD PTR [rdi], ax
        mov     esi, esi   <---- NOP
        add     rdi, 2     <--\
        mov     rdx, rdi   <----- lea [rdx, rdi+2] seems shorter
        mov     eax, esi
        ret

Where the assembly contains a seemingly unnecessary no-op instruction and the
addition appears to be executed in a suboptimal way.

Reply via email to