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

            Bug ID: 91775
           Summary: Can eliminate compare from loop with known number of
                    iterations
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ubizjak at gmail dot com
  Target Milestone: ---

The compiler can eliminate compare for targets, where operation sets flags (e.g
x86).  Following example:

--cut here--
#define N 1024

int a[N], b[N], c[N];

void
foo (void)
{
  int i;

  for (i = 0; i < N; i++)
    a[i] = b[i] + c[i];
}
--cut here--

compiles to (-O2):

        xorl    %eax, %eax
.L2:
        movl    c(%rax), %edx
        addl    b(%rax), %edx
        addq    $4, %rax
        movl    %edx, a-4(%rax)
(*)     cmpq    $4096, %rax
        jne     .L2
        ret


clang generates (-O2 -fno-vectorize -fno-unroll-loops):

        movq    $-4096, %rax            # imm = 0xF000
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        movl    c+4096(%rax), %ecx
        addl    b+4096(%rax), %ecx
        movl    %ecx, a+4096(%rax)
        addq    $4, %rax
        jne     .LBB0_1
        retq

Please note that clang rewrites loop to eliminate compare (*).

Reply via email to