Skip to content

Missed optimization in bounds analysis in unsigned arithmetic that cannot underflow #91619

@davidben

Description

@davidben

Clang doesn't seem to be able to optimize the following out:

bool f(size_t a, size_t b) {
    __builtin_assume(a <= b);
    __builtin_assume(b <= PTRDIFF_MAX);
    // This can be deleted, because:
    //   1. 0 <= a <= b implies that b - a <= b
    //   2. b - a <= b <= PTRDIFF_MAX
    return b - a <= PTRDIFF_MAX;
}

https://godbolt.org/z/vzE7qb7Gb

I ran into this while exploring what combination of __builtin_assumes would dispatch some unnecessary assertions in std::string_view. (A separate bug while I'll file after this.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions