1 // compile-flags: -O -Z merge-functions=disabled
4 #![feature(core_intrinsics)]
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.
13 pub struct WrapperWithAlign1<T: ?Sized> { x: u8, y: T }
15 pub struct WrapperWithAlign2<T: ?Sized> { x: u16, y: T }
17 pub struct Struct<W: ?Sized> {
22 // CHECK-LABEL: @eliminates_runtime_check_when_align_1
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
35 // CHECK-LABEL: @does_not_eliminate_runtime_check_when_align_2
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}}
46 // CHECK-LABEL: @align_load_from_align_of_val
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)
53 // CHECK-LABEL: @align_load_from_vtable_align_intrinsic
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)
61 // CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0}