https://bugs.llvm.org/show_bug.cgi?id=40845

            Bug ID: 40845
           Summary: Missed optimization with redundant overflow checks
           Product: libraries
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedb...@nondot.org
          Reporter: alex.gay...@gmail.com
                CC: llvm-bugs@lists.llvm.org

Godbolt link: https://godbolt.org/z/h2lkGE

The subtraction can be known not to overflow because it's derived from an
overflow checked addition. This was reduced/converted to C from this rust bug:
https://github.com/rust-lang/rust/issues/58692

One commenter suggested they thought the issue was in how GVN handled overflow
checked operations.

Code:

#include <stdlib.h>

extern void panic1();
extern void panic2();
extern void panic3();

size_t f(size_t a, size_t b) {
    size_t x;
    if (!__builtin_add_overflow(a, b, &x)) {
        panic1();
        __builtin_unreachable();
    }

    size_t r;
    if (!__builtin_sub_overflow(x, a, &r)) {
        panic2();
        __builtin_unreachable();
    }

    if (r < 0) {
        panic3();
        __builtin_unreachable();
    }

    return x;
}


Assembly:

f(unsigned long, unsigned long):                                 # @f(unsigned
long, unsigned long)
        push    rax
        mov     rax, rsi
        add     rax, rdi
        jae     .LBB0_3
        cmp     rax, rdi
        jae     .LBB0_4
        pop     rcx
        ret
.LBB0_3:
        call    panic1()
.LBB0_4:
        call    panic2()

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to