[llvm-bugs] [Bug 47172] New: Missed bounds check optimization

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Aug 14 13:15:50 PDT 2020


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

            Bug ID: 47172
           Summary: Missed bounds check optimization
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

>From rust-lang github (https://github.com/rust-lang/rust/issues/75525):

#include <stddef.h>
#include <stdint.h>


uint8_t f1(size_t idx) {
    if (idx < 8) {
        // it's ok that this has a bounds check
        return (idx - 1) < 10;
    } else {
        return 0;
    }
}

Clang:
f1:                                     # @f1
        cmp     rdi, 8
        setb    cl
        add     rdi, -1
        cmp     rdi, 10
        setb    al
        and     al, cl
        ret

GCC:
f1:
        sub     rdi, 1
        cmp     rdi, 6
        setbe   al
        ret


Modified test case:

uint8_t f2(size_t idx) {
    if (idx <= 9) {
       return (idx - 1) > 10;
    } else {
        return 0;
    }
}

Clang:
f2:                                     # @f2
        cmp     rdi, 10
        setb    cl
        add     rdi, -1
        cmp     rdi, 10
        seta    al
        and     al, cl
        ret

GCC:
f2:
        test    rdi, rdi
        sete    al
        ret




https://godbolt.org/z/vahzbo

-- 
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/20200814/86238291/attachment.html>


More information about the llvm-bugs mailing list