Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate some other bound checks when index comes from an enum #75529

Merged
merged 2 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions compiler/rustc_codegen_ssa/src/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,29 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
if er.end != er.start
&& scalar.valid_range.end() > scalar.valid_range.start()
{
// We want `table[e as usize]` to not
// We want `table[e as usize ± k]` to not
// have bound checks, and this is the most
// convenient place to put the `assume`.
let ll_t_in_const =
// convenient place to put the `assume`s.
if *scalar.valid_range.start() > 0 {
let enum_value_lower_bound = bx
.cx()
.const_uint_big(ll_t_in, *scalar.valid_range.start());
let cmp_start = bx.icmp(
IntPredicate::IntUGE,
llval,
enum_value_lower_bound,
);
bx.assume(cmp_start);
}

let enum_value_upper_bound =
bx.cx().const_uint_big(ll_t_in, *scalar.valid_range.end());
let cmp = bx.icmp(IntPredicate::IntULE, llval, ll_t_in_const);
bx.assume(cmp);
let cmp_end = bx.icmp(
IntPredicate::IntULE,
llval,
enum_value_upper_bound,
);
bx.assume(cmp_end);
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/codegen/enum-bounds-check-derived-idx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// This test checks an optimization that is not guaranteed to work. This test case should not block
// a future LLVM update.
// compile-flags: -O
// min-llvm-version: 11.0

#![crate_type = "lib"]

pub enum Bar {
A = 1,
B = 3,
}

// CHECK-LABEL: @lookup_inc
#[no_mangle]
pub fn lookup_inc(buf: &[u8; 5], f: Bar) -> u8 {
// CHECK-NOT: panic_bounds_check
buf[f as usize + 1]
}

// CHECK-LABEL: @lookup_dec
#[no_mangle]
pub fn lookup_dec(buf: &[u8; 5], f: Bar) -> u8 {
// CHECK-NOT: panic_bounds_check
buf[f as usize - 1]
}
19 changes: 19 additions & 0 deletions src/test/codegen/enum-bounds-check-issue-13926.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This test checks an optimization that is not guaranteed to work. This test case should not block
// a future LLVM update.
// compile-flags: -O
// min-llvm-version: 11.0

#![crate_type = "lib"]

#[repr(u8)]
pub enum Exception {
Low = 5,
High = 10,
}

// CHECK-LABEL: @access
#[no_mangle]
pub fn access(array: &[usize; 12], exc: Exception) -> usize {
// CHECK-NOT: panic_bounds_check
array[(exc as u8 - 4) as usize]
}
12 changes: 12 additions & 0 deletions src/test/codegen/enum-bounds-check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ pub fn lookup(buf: &[u8; 2], f: Foo) -> u8 {
// CHECK-NOT: panic_bounds_check
buf[f as usize]
}

pub enum Bar {
A = 2,
B = 3
}

// CHECK-LABEL: @lookup_unmodified
#[no_mangle]
pub fn lookup_unmodified(buf: &[u8; 5], f: Bar) -> u8 {
// CHECK-NOT: panic_bounds_check
buf[f as usize]
}