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

            Bug ID: 126732
           Summary: Redundant scalar check after vectorized operation
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: blubban at gmail dot com
  Target Milestone: ---

bool x(int* y)
{
  for (int i=0;i<8;i++)
  {
    if (y[i] == 42)
      return true;
  }
  return false;
}

-mavx2 -O3


Result:

"x(int*)":
        test    dil, 31
        jne     .L2
        mov     eax, 42
        vmovd   xmm0, eax
        xor     eax, eax
        vpbroadcastd    ymm0, xmm0
        vpcmpeqd        ymm0, ymm0, YMMWORD PTR [rdi]
        vptest  ymm0, ymm0
        jne     .L16
        vzeroupper
        ret
.L16:
        cmp     DWORD PTR [rdi], 42
        je      .L14
        cmp     DWORD PTR [rdi+4], 42
        je      .L14
        cmp     DWORD PTR [rdi+8], 42
        je      .L14
        cmp     DWORD PTR [rdi+12], 42
        je      .L14
        cmp     DWORD PTR [rdi+16], 42
        je      .L14
        cmp     DWORD PTR [rdi+20], 42
        je      .L14
        cmp     DWORD PTR [rdi+24], 42
        je      .L14
        cmp     DWORD PTR [rdi+28], 42
        sete    al
        vzeroupper
        ret
.L14:
        vzeroupper
.L4:
        mov     eax, 1
        ret
.L2:
        cmp     DWORD PTR [rdi], 42
        je      .L4
        cmp     DWORD PTR [rdi+4], 42
        je      .L4
        cmp     DWORD PTR [rdi+8], 42
        je      .L4
        cmp     DWORD PTR [rdi+12], 42
        je      .L4
        cmp     DWORD PTR [rdi+16], 42
        je      .L4
        cmp     DWORD PTR [rdi+20], 42
        je      .L4
        cmp     DWORD PTR [rdi+24], 42
        je      .L4
        cmp     DWORD PTR [rdi+28], 42
        sete    al
        ret


Expected: Reaching .L16 means at least one element is 42, so that string of
compares is known to return true and should be optimized out. (The .L2 compare
string belongs there.)

Reply via email to