From c88b167f1518370833c9216706d09735d1e2d697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Sun, 30 Aug 2020 21:26:03 +0200 Subject: [PATCH 1/2] Additional assumption to elide bounds check --- compiler/rustc_codegen_ssa/src/mir/rvalue.rs | 26 ++++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs index 71f924df119a3..43521048012fa 100644 --- a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs +++ b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs @@ -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); } } } From 1d157ce797dddcee16a577796199b1144b4f7f34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Tue, 25 Aug 2020 11:44:18 +0200 Subject: [PATCH 2/2] Add codegen tests --- .../codegen/enum-bounds-check-derived-idx.rs | 25 +++++++++++++++++++ .../codegen/enum-bounds-check-issue-13926.rs | 19 ++++++++++++++ src/test/codegen/enum-bounds-check.rs | 12 +++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/test/codegen/enum-bounds-check-derived-idx.rs create mode 100644 src/test/codegen/enum-bounds-check-issue-13926.rs diff --git a/src/test/codegen/enum-bounds-check-derived-idx.rs b/src/test/codegen/enum-bounds-check-derived-idx.rs new file mode 100644 index 0000000000000..7e3773b6a3eb1 --- /dev/null +++ b/src/test/codegen/enum-bounds-check-derived-idx.rs @@ -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] +} diff --git a/src/test/codegen/enum-bounds-check-issue-13926.rs b/src/test/codegen/enum-bounds-check-issue-13926.rs new file mode 100644 index 0000000000000..ad029f0fa7399 --- /dev/null +++ b/src/test/codegen/enum-bounds-check-issue-13926.rs @@ -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] +} diff --git a/src/test/codegen/enum-bounds-check.rs b/src/test/codegen/enum-bounds-check.rs index 21a27c9f35d4e..17322d5911b92 100644 --- a/src/test/codegen/enum-bounds-check.rs +++ b/src/test/codegen/enum-bounds-check.rs @@ -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] +}