/src/test/codegen/slice-ref-equality.rs

https://gitlab.com/rust-lang/rust · Rust · 38 lines · 13 code · 5 blank · 20 comment · 3 complexity · b19b33fc50fbc8d768b048d432238809 MD5 · raw file

  1. // compile-flags: -C opt-level=3
  2. #![crate_type = "lib"]
  3. // #71602 reported a simple array comparison just generating a loop.
  4. // This was originally fixed by ensuring it generates a single bcmp,
  5. // but we now generate it as a load+icmp instead. `is_zero_slice` was
  6. // tweaked to still test the case of comparison against a slice,
  7. // and `is_zero_array` tests the new array-specific behaviour.
  8. // The optimization was then extended to short slice-to-array comparisons,
  9. // so the first test here now has a long slice to still get the bcmp.
  10. // CHECK-LABEL: @is_zero_slice_long
  11. #[no_mangle]
  12. pub fn is_zero_slice_long(data: &[u8; 456]) -> bool {
  13. // CHECK: %[[BCMP:.+]] = tail call i32 @{{bcmp|memcmp}}({{.+}})
  14. // CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[BCMP]], 0
  15. // CHECK-NEXT: ret i1 %[[EQ]]
  16. &data[..] == [0; 456]
  17. }
  18. // CHECK-LABEL: @is_zero_slice_short
  19. #[no_mangle]
  20. pub fn is_zero_slice_short(data: &[u8; 4]) -> bool {
  21. // CHECK: %[[LOAD:.+]] = load i32, {{i32\*|ptr}} %{{.+}}, align 1
  22. // CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[LOAD]], 0
  23. // CHECK-NEXT: ret i1 %[[EQ]]
  24. &data[..] == [0; 4]
  25. }
  26. // CHECK-LABEL: @is_zero_array
  27. #[no_mangle]
  28. pub fn is_zero_array(data: &[u8; 4]) -> bool {
  29. // CHECK: %[[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. }