[llvm-bugs] [Bug 48909] New: Missed optimization: llvm unable to remove bounds check

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jan 27 15:17:00 PST 2021


            Bug ID: 48909
           Summary: Missed optimization: llvm unable to remove bounds
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: alex.gaynor at gmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at 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.


#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) {
    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
        pop     rcx
        call    abort

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/20210127/fcccd95c/attachment.html>

More information about the llvm-bugs mailing list