]> git.lizzy.rs Git - rust.git/blob - tests/codegen/dst-vtable-align-nonzero.rs
Rollup merge of #107769 - compiler-errors:pointer-like, r=eholk
[rust.git] / tests / codegen / dst-vtable-align-nonzero.rs
1 // compile-flags: -O -Z merge-functions=disabled
2
3 #![crate_type = "lib"]
4 #![feature(core_intrinsics)]
5
6 // This test checks that we annotate alignment loads from vtables with nonzero range metadata,
7 // and that this allows LLVM to eliminate redundant `align >= 1` checks.
8
9 pub trait Trait {
10     fn f(&self);
11 }
12
13 pub struct WrapperWithAlign1<T: ?Sized> { x: u8, y: T }
14
15 pub struct WrapperWithAlign2<T: ?Sized> { x: u16, y: T }
16
17 pub struct Struct<W: ?Sized> {
18     _field: i8,
19     dst: W,
20 }
21
22 // CHECK-LABEL: @eliminates_runtime_check_when_align_1
23 #[no_mangle]
24 pub fn eliminates_runtime_check_when_align_1(
25     x: &Struct<WrapperWithAlign1<dyn Trait>>
26 ) -> &WrapperWithAlign1<dyn Trait> {
27     // CHECK: load [[USIZE:i[0-9]+]], {{.+}} !range [[RANGE_META:![0-9]+]]
28     // CHECK-NOT: llvm.umax
29     // CHECK-NOT: icmp
30     // CHECK-NOT: select
31     // CHECK: ret
32     &x.dst
33 }
34
35 // CHECK-LABEL: @does_not_eliminate_runtime_check_when_align_2
36 #[no_mangle]
37 pub fn does_not_eliminate_runtime_check_when_align_2(
38     x: &Struct<WrapperWithAlign2<dyn Trait>>
39 ) -> &WrapperWithAlign2<dyn Trait> {
40     // CHECK: [[X0:%[0-9]+]] = load [[USIZE]], {{.+}} !range [[RANGE_META]]
41     // CHECK: {{icmp|llvm.umax}}
42     // CHECK: ret
43     &x.dst
44 }
45
46 // CHECK-LABEL: @align_load_from_align_of_val
47 #[no_mangle]
48 pub fn align_load_from_align_of_val(x: &dyn Trait) -> usize {
49     // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]]
50     core::mem::align_of_val(x)
51 }
52
53 // CHECK-LABEL: @align_load_from_vtable_align_intrinsic
54 #[no_mangle]
55 pub unsafe fn align_load_from_vtable_align_intrinsic(x: &dyn Trait) -> usize {
56     let (data, vtable): (*const (), *const ()) = core::mem::transmute(x);
57     // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]]
58     core::intrinsics::vtable_align(vtable)
59 }
60
61 // CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0}