about summary refs log tree commit diff
path: root/tests/codegen-llvm/slice-last-elements-optimization.rs
blob: d982cda709d47a6d6af3d4bdb30eef02c43a7e0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//@ compile-flags: -Copt-level=3
//@ min-llvm-version: 20
#![crate_type = "lib"]

// This test verifies that LLVM 20 properly optimizes the bounds check
// when accessing the last few elements of a slice with proper conditions.
// Previously, this would generate an unreachable branch to
// slice_index_fail even when the bounds check was provably safe.

// CHECK-LABEL: @last_four_initial(
#[no_mangle]
pub fn last_four_initial(s: &[u8]) -> &[u8] {
    // Previously this would generate a branch to slice_index_fail
    // that is unreachable. The LLVM 20 fix should eliminate this branch.
    // CHECK-NOT: slice_index_fail
    // CHECK-NOT: unreachable
    let start = if s.len() <= 4 { 0 } else { s.len() - 4 };
    &s[start..]
}

// CHECK-LABEL: @last_four_optimized(
#[no_mangle]
pub fn last_four_optimized(s: &[u8]) -> &[u8] {
    // This version was already correctly optimized before the fix in LLVM 20.
    // CHECK-NOT: slice_index_fail
    // CHECK-NOT: unreachable
    if s.len() <= 4 { &s[0..] } else { &s[s.len() - 4..] }
}

// Just to verify we're correctly checking for the right thing
// CHECK-LABEL: @test_bounds_check_happens(
#[no_mangle]
pub fn test_bounds_check_happens(s: &[u8], i: usize) -> &[u8] {
    // CHECK: slice_index_fail
    &s[i..]
}