Auto merge of #91838 - scottmcm:array-slice-eq-via-arrays-not-slices, r=dtolnay
Do array-slice equality via array equality, rather than always via slices ~~Draft because it needs a rebase after #91766 eventually gets through bors.~~ This enables the optimizations from #85828 to be used for array-to-slice comparisons too, not just array-to-array. For example, <https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=5f9ba69b3d5825a782f897c830d3a6aa> ```rust pub fn demo(x: &[u8], y: [u8; 4]) -> bool { *x == y } ``` Currently writes the array to stack for no reason: ```nasm sub rsp, 4 mov dword ptr [rsp], edx cmp rsi, 4 jne .LBB0_1 mov eax, dword ptr [rdi] cmp eax, dword ptr [rsp] sete al add rsp, 4 ret .LBB0_1: xor eax, eax add rsp, 4 ret ``` Whereas with the change in this PR it just compares it directly: ```nasm cmp rsi, 4 jne .LBB1_1 cmp dword ptr [rdi], edx sete al ret .LBB1_1: xor eax, eax ret ```
This commit is contained in:
commit
7abab1efb2
3 changed files with 89 additions and 15 deletions
|
|
@ -4,18 +4,31 @@
|
|||
|
||||
// #71602 reported a simple array comparison just generating a loop.
|
||||
// This was originally fixed by ensuring it generates a single bcmp,
|
||||
// but we now generate it as a load instead. `is_zero_slice` was
|
||||
// but we now generate it as a load+icmp instead. `is_zero_slice` was
|
||||
// tweaked to still test the case of comparison against a slice,
|
||||
// and `is_zero_array` tests the new array-specific behaviour.
|
||||
// The optimization was then extended to short slice-to-array comparisons,
|
||||
// so the first test here now has a long slice to still get the bcmp.
|
||||
|
||||
// CHECK-LABEL: @is_zero_slice
|
||||
// CHECK-LABEL: @is_zero_slice_long
|
||||
#[no_mangle]
|
||||
pub fn is_zero_slice(data: &[u8; 4]) -> bool {
|
||||
pub fn is_zero_slice_long(data: &[u8; 456]) -> bool {
|
||||
// CHECK: :
|
||||
// CHECK-NEXT: %{{.+}} = getelementptr {{.+}}
|
||||
// CHECK-NEXT: %[[BCMP:.+]] = tail call i32 @{{bcmp|memcmp}}({{.+}})
|
||||
// CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[BCMP]], 0
|
||||
// CHECK-NEXT: ret i1 %[[EQ]]
|
||||
&data[..] == [0; 456]
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @is_zero_slice_short
|
||||
#[no_mangle]
|
||||
pub fn is_zero_slice_short(data: &[u8; 4]) -> bool {
|
||||
// CHECK: :
|
||||
// CHECK-NEXT: %[[PTR:.+]] = bitcast [4 x i8]* {{.+}} to i32*
|
||||
// CHECK-NEXT: %[[LOAD:.+]] = load i32, i32* %[[PTR]], align 1
|
||||
// CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[LOAD]], 0
|
||||
// CHECK-NEXT: ret i1 %[[EQ]]
|
||||
&data[..] == [0; 4]
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue