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

            Bug ID: 79201
           Summary: issed optimization: gcc fails to cut out unnecessary
                    loop.
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: drraph at gmail dot com
  Target Milestone: ---

Consider this code:

int f(int n) {
  int i,j=0;
  for (i = 0; i < 32; i++) {
    j = __builtin_ffs(i);
  }
  return j;
}

With gcc -O3 you get

f:
        xor     eax, eax
        xor     edx, edx
        mov     ecx, -1
        jmp     .L3
.L5:
        bsf     eax, edx
        cmove   eax, ecx
        add     eax, 1
.L3:
        add     edx, 1
        cmp     edx, 32
        jne     .L5
        rep ret


However with clang you get

f:                                      # @f
        mov     eax, 1
        ret


A similar difference occurs if you replace the limit 32 with the variable n.

gcc is unable to detect that the loop can be omitted.

Reply via email to