]> git.lizzy.rs Git - rust.git/blob - src/test/codegen/slice-ref-equality.rs
Rollup merge of #91977 - GuillaumeGomez:unify-search-code, r=jsha
[rust.git] / src / test / codegen / slice-ref-equality.rs
1 // compile-flags: -C opt-level=3
2
3 #![crate_type = "lib"]
4
5 // #71602 reported a simple array comparison just generating a loop.
6 // This was originally fixed by ensuring it generates a single bcmp,
7 // but we now generate it as a load+icmp instead. `is_zero_slice` was
8 // tweaked to still test the case of comparison against a slice,
9 // and `is_zero_array` tests the new array-specific behaviour.
10 // The optimization was then extended to short slice-to-array comparisons,
11 // so the first test here now has a long slice to still get the bcmp.
12
13 // CHECK-LABEL: @is_zero_slice_long
14 #[no_mangle]
15 pub fn is_zero_slice_long(data: &[u8; 456]) -> bool {
16     // CHECK: :
17     // CHECK-NEXT: %{{.+}} = getelementptr {{.+}}
18     // CHECK-NEXT: %[[BCMP:.+]] = tail call i32 @{{bcmp|memcmp}}({{.+}})
19     // CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[BCMP]], 0
20     // CHECK-NEXT: ret i1 %[[EQ]]
21     &data[..] == [0; 456]
22 }
23
24 // CHECK-LABEL: @is_zero_slice_short
25 #[no_mangle]
26 pub fn is_zero_slice_short(data: &[u8; 4]) -> bool {
27     // CHECK: :
28     // CHECK-NEXT: %[[PTR:.+]] = bitcast [4 x i8]* {{.+}} to i32*
29     // CHECK-NEXT: %[[LOAD:.+]] = load i32, i32* %[[PTR]], align 1
30     // CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[LOAD]], 0
31     // CHECK-NEXT: ret i1 %[[EQ]]
32     &data[..] == [0; 4]
33 }
34
35 // CHECK-LABEL: @is_zero_array
36 #[no_mangle]
37 pub fn is_zero_array(data: &[u8; 4]) -> bool {
38     // CHECK: start:
39     // CHECK-NEXT: %[[PTR:.+]] = bitcast [4 x i8]* {{.+}} to i32*
40     // CHECK-NEXT: %[[LOAD:.+]] = load i32, i32* %[[PTR]], align 1
41     // CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[LOAD]], 0
42     // CHECK-NEXT: ret i1 %[[EQ]]
43     *data == [0; 4]
44 }