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

            Bug ID: 48909
           Summary: Missed optimization: llvm unable to remove bounds
                    check
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedb...@nondot.org
          Reporter: alex.gay...@gmail.com
                CC: htmldevelo...@gmail.com, llvm-bugs@lists.llvm.org

https://godbolt.org/z/vEfPYj (also below)

LLVM should be able to remove the branch that leads to the call to abort() --
data.length is known to be the same as block_len, and block_len is known not to
be 0. Therefore llvm should know that block_len - is always < data.length.

Source:

#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>

typedef struct {
    uint8_t *data;
    size_t length;
} slice;

static inline uint8_t subscript(slice data, size_t index) {
    if (index >= data.length) {
        abort();
    }
    return data.data[index];
}

bool check_padding(slice data, size_t block_len) {
    if (data.length != block_len || block_len == 0) {
        return false;
    }

    uint8_t pad_size = subscript(data, block_len - 1);
    return pad_size > 7;
}


generated assembly:

check_padding:                          # @check_padding
        push    rax
        xor     eax, eax
        cmp     rsi, rdx
        jne     .LBB0_4
        test    rdx, rdx
        je      .LBB0_4
        test    rsi, rsi
        je      .LBB0_5
        cmp     byte ptr [rsi + rdi - 1], 7
        seta    al
.LBB0_4:
        pop     rcx
        ret
.LBB0_5:
        call    abort

-- 
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