[llvm-bugs] [Bug 40845] New: Missed optimization with redundant overflow checks

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Feb 24 09:39:02 PST 2019


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: unassignedbugs at nondot.org
          Reporter: alex.gaynor at gmail.com
                CC: llvm-bugs at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190224/11ba4c2e/attachment-0001.html>


More information about the llvm-bugs mailing list