Skip to content

LLVM doesn't know the result of a usize::saturating_add is >= the first argument #112782

Description

@saethlin

https://godbolt.org/z/67q79vnEW

This function should be optimized away completely, but it isn't:

pub fn demo(x: usize, y: usize) {
    let res = x.saturating_add(y);
    assert!(res >= x);
}

Original example was this, which currently has two bounds checks (that the range end is >= than the start, and that the end of the range is in-bounds), but it should only have one:

pub fn subslice(slice: &[u8], x: usize) {
    let _ = &slice[x..x.saturating_add(10)];
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions