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

            Bug ID: 123479
           Summary: missed optimization for redundant conditional
                    computation
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 652023330028 at smail dot nju.edu.cn
  Target Milestone: ---

Hello,

For the reduced test case below, GCC -O3 appears to generate a redundant
conditional computation.

Reduced code (from ABC project
https://github.com/berkeley-abc/abc/blob/c18b9a24de46d3b0209afa416511b7a1127344f9/src/sat/cadical/radix.hpp#L45):

https://godbolt.org/z/zEKoWx6Ta

void c(unsigned long* e, unsigned long* i) {
  unsigned long n = i - e;
  if (n <= 1)
    return;
  for (unsigned long g = 0; g < n; g++)
    e[n] = g;
}



Optimized code of GCC -O3:
```
  <bb 2> [local count: 178956971]:
  _1 = i_6(D) - e_7(D);
  _9 = (long unsigned int) _1;
  if (_9 <= 8)
    goto <bb 4>; [34.00%]
  else
    goto <bb 3>; [66.00%]

  <bb 3> [local count: 118111600]:
  _2 = _1 /[ex] 8;
  _17 = (long unsigned int) _2;
  _18 = _1 != 0;                    #redundant 
  _11 = (long unsigned int) _18;
  g_13 = _17 - _11;
  *i_6(D) = g_13;

  <bb 4> [local count: 178956971]:
  return;
```

GCC -O3:
"c(unsigned long*, unsigned long*)":
        mov     rax, rsi
        sub     rax, rdi
        cmp     rax, 8
        jbe     .L1
        mov     rdx, rax
        sar     rdx, 3
        cmp     rax, 1
        mov     rax, rdx
        adc     rax, -1
        mov     QWORD PTR [rsi], rax
.L1:
        ret

Expected:
_Z1cPmS_:
.LFB0:
        .cfi_startproc
        movq    %rsi, %rax
        subq    %rdi, %rax
        cmpq    $8, %rax
        jbe     .L1
        sarq    $3, %rax
        subq    $1, %rax
        movq    %rax, (%rsi)
.L1:
        ret

Reply via email to