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

            Bug ID: 113440
           Summary: Missed optimization for redundancy computation
                    elimination because of missed judgment for unsigned
                    overflow
           Product: gcc
           Version: 14.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, we noticed that maybe there is a missed optimization for redundancy
computation elimination because of missed judgment for unsigned overflow.

https://godbolt.org/z/r54ezx9ea

unsigned n;
void func(unsigned a){
    if((a+a<a&&a>0) || a==0) return;   
    n=(a+a)/a;
}

GCC -O3:
func(unsigned int):
        lea     eax, [rdi+rdi]
        lea     edx, [rdi-1]
        cmp     edx, eax
        jb      .L4
        ret
.L4:
        xor     edx, edx
        div     edi
        mov     DWORD PTR n[rip], eax
        ret

Expected code (Clang):
func(unsigned int):                               # @func(unsigned int)
        test    edi, edi
        jle     .LBB0_2
        mov     dword ptr [rip + n], 2
.LBB0_2:                                # %return
        ret


Compare this to:
void func2(unsigned a){
    if(a>2147483647 || a==0) return;   
    n=(a+a)/a;
}
evrp (tree) of func2:
Folding statement: _3 = _2 / a_5(D);
Applying pattern match.pd:954, gimple-match-3.cc:2072
gimple_simplified to _3 = 2;
Global Exported: _3 = [irange] unsigned int [0, 4294967294]
Folded into: _3 = 2;

Therefore, it may be because gcc misses a judgment on unsigned overflow or
value ranges.

Thank you very much for your time and effort! We look forward to hearing from
you.

Reply via email to