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

            Bug ID: 92638
           Summary: gcc unable to remove empty loop after loop body is
                    removed
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hiraditya at msn dot com
  Target Milestone: ---

$cat test.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

char* get(const char* value, const char separator) {
  int separator_index = strchr(value, separator) - value;
  char* result = (char*)malloc(separator_index);
  memcpy(result, value, separator_index);
  result[separator_index] = '\0';

  return result;
}

int main() {
  const char separator = ',';
  clock_t t = clock();

  for (size_t i = 0; i < 100000000; ++i) {
    free(get("127.0.0.1, 127.0.0.2:1111", separator));
  }

  float elapsed_seconds = (((double)(clock() - t)) / CLOCKS_PER_SEC);
  printf("%f seconds.\n", elapsed_seconds);

  return 0;
}


$ gcc -O3 -S -o-

get(char const*, char):
        push    rbp
        movsx   esi, sil
        mov     rbp, rdi
        push    rbx
        sub     rsp, 8
        call    strchr
        sub     rax, rbp
        movsx   rbx, eax
        mov     rdi, rbx
        call    malloc
        mov     rdx, rbx
        mov     rsi, rbp
        mov     rdi, rax
        call    memcpy
        mov     BYTE PTR [rax+rbx], 0
        add     rsp, 8
        pop     rbx
        pop     rbp
        ret
.LC1:
        .string "%f seconds.\n"
main:
        push    rbx
        call    clock
        mov     rbx, rax
        mov     eax, 100000000
.L5:
        sub     rax, 1 // <------------------ Loop body still there.
        jne     .L5
        call    clock
        pxor    xmm0, xmm0
        mov     edi, OFFSET FLAT:.LC1
        sub     rax, rbx
        cvtsi2sd        xmm0, rax
        mov     eax, 1
        divsd   xmm0, QWORD PTR .LC0[rip]
        cvtsd2ss        xmm0, xmm0
        cvtss2sd        xmm0, xmm0
        call    printf
        xor     eax, eax
        pop     rbx
        ret
.LC0:
        .long   0
        .long   1093567616

Reply via email to