]> git.proxmox.com Git - rustc.git/blob - src/test/codegen/loads.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / src / test / codegen / loads.rs
1 // compile-flags: -C no-prepopulate-passes -Zmir-opt-level=0
2
3 #![crate_type = "lib"]
4
5 use std::mem::MaybeUninit;
6 use std::num::NonZeroU16;
7
8 pub struct Bytes {
9 a: u8,
10 b: u8,
11 c: u8,
12 d: u8,
13 }
14
15 #[derive(Copy, Clone)]
16 pub enum MyBool {
17 True,
18 False,
19 }
20
21 #[repr(align(16))]
22 pub struct Align16(u128);
23
24 // CHECK: @ptr_alignment_helper({}** {{.*}}align [[PTR_ALIGNMENT:[0-9]+]]
25 #[no_mangle]
26 pub fn ptr_alignment_helper(x: &&()) {}
27
28 // CHECK-LABEL: @load_ref
29 #[no_mangle]
30 pub fn load_ref<'a>(x: &&'a i32) -> &'a i32 {
31 // CHECK: load i32*, i32** %x, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_4_META:[0-9]+]], !noundef !{{[0-9]+}}
32 *x
33 }
34
35 // CHECK-LABEL: @load_ref_higher_alignment
36 #[no_mangle]
37 pub fn load_ref_higher_alignment<'a>(x: &&'a Align16) -> &'a Align16 {
38 // CHECK: load {{%Align16|i128}}*, {{%Align16|i128}}** %x, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_16_META:[0-9]+]], !noundef !{{[0-9]+}}
39 *x
40 }
41
42 // CHECK-LABEL: @load_scalar_pair
43 #[no_mangle]
44 pub fn load_scalar_pair<'a>(x: &(&'a i32, &'a Align16)) -> (&'a i32, &'a Align16) {
45 // CHECK: load i32*, i32** %{{.+}}, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_4_META]], !noundef !{{[0-9]+}}
46 // CHECK: load i64*, i64** %{{.+}}, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_16_META]], !noundef !{{[0-9]+}}
47 *x
48 }
49
50 // CHECK-LABEL: @load_raw_pointer
51 #[no_mangle]
52 pub fn load_raw_pointer<'a>(x: &*const i32) -> *const i32 {
53 // loaded raw pointer should not have !nonnull, !align, or !noundef metadata
54 // CHECK: load i32*, i32** %x, align [[PTR_ALIGNMENT]]{{$}}
55 *x
56 }
57
58 // CHECK-LABEL: @load_box
59 #[no_mangle]
60 pub fn load_box<'a>(x: Box<Box<i32>>) -> Box<i32> {
61 // CHECK: load i32*, i32** %x, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_4_META]], !noundef !{{[0-9]+}}
62 *x
63 }
64
65 // CHECK-LABEL: @load_bool
66 #[no_mangle]
67 pub fn load_bool(x: &bool) -> bool {
68 // CHECK: load i8, i8* %x, align 1, !range ![[BOOL_RANGE:[0-9]+]], !noundef !{{[0-9]+}}
69 *x
70 }
71
72 // CHECK-LABEL: @load_maybeuninit_bool
73 #[no_mangle]
74 pub fn load_maybeuninit_bool(x: &MaybeUninit<bool>) -> MaybeUninit<bool> {
75 // CHECK: load i8, i8* %x, align 1{{$}}
76 *x
77 }
78
79 // CHECK-LABEL: @load_enum_bool
80 #[no_mangle]
81 pub fn load_enum_bool(x: &MyBool) -> MyBool {
82 // CHECK: load i8, i8* %x, align 1, !range ![[BOOL_RANGE]], !noundef !{{[0-9]+}}
83 *x
84 }
85
86 // CHECK-LABEL: @load_maybeuninit_enum_bool
87 #[no_mangle]
88 pub fn load_maybeuninit_enum_bool(x: &MaybeUninit<MyBool>) -> MaybeUninit<MyBool> {
89 // CHECK: load i8, i8* %x, align 1{{$}}
90 *x
91 }
92
93 // CHECK-LABEL: @load_int
94 #[no_mangle]
95 pub fn load_int(x: &u16) -> u16 {
96 // CHECK: load i16, i16* %x, align 2{{$}}
97 *x
98 }
99
100 // CHECK-LABEL: @load_nonzero_int
101 #[no_mangle]
102 pub fn load_nonzero_int(x: &NonZeroU16) -> NonZeroU16 {
103 // CHECK: load i16, i16* %x, align 2, !range ![[NONZEROU16_RANGE:[0-9]+]], !noundef !{{[0-9]+}}
104 *x
105 }
106
107 // CHECK-LABEL: @load_option_nonzero_int
108 #[no_mangle]
109 pub fn load_option_nonzero_int(x: &Option<NonZeroU16>) -> Option<NonZeroU16> {
110 // CHECK: load i16, i16* %x, align 2{{$}}
111 *x
112 }
113
114 // CHECK-LABEL: @borrow
115 #[no_mangle]
116 pub fn borrow(x: &i32) -> &i32 {
117 // CHECK: load {{(i32\*, )?}}i32** %x{{.*}}, !nonnull
118 &x; // keep variable in an alloca
119 x
120 }
121
122 // CHECK-LABEL: @_box
123 #[no_mangle]
124 pub fn _box(x: Box<i32>) -> i32 {
125 // CHECK: load {{(i32\*, )?}}i32** %x{{.*}}, !nonnull
126 *x
127 }
128
129 // CHECK-LABEL: small_array_alignment
130 // The array is loaded as i32, but its alignment is lower, go with 1 byte to avoid target
131 // dependent alignment
132 #[no_mangle]
133 pub fn small_array_alignment(x: [i8; 4]) -> [i8; 4] {
134 // CHECK: [[VAR:%[0-9]+]] = load {{(i32, )?}}i32* %{{.*}}, align 1
135 // CHECK: ret i32 [[VAR]]
136 x
137 }
138
139 // CHECK-LABEL: small_struct_alignment
140 // The struct is loaded as i32, but its alignment is lower, go with 1 byte to avoid target
141 // dependent alignment
142 #[no_mangle]
143 pub fn small_struct_alignment(x: Bytes) -> Bytes {
144 // CHECK: [[VAR:%[0-9]+]] = load {{(i32, )?}}i32* %{{.*}}, align 1
145 // CHECK: ret i32 [[VAR]]
146 x
147 }
148
149 // CHECK-DAG: ![[BOOL_RANGE]] = !{i8 0, i8 2}
150 // CHECK-DAG: ![[NONZEROU16_RANGE]] = !{i16 1, i16 0}
151 // CHECK-DAG: ![[ALIGN_4_META]] = !{i64 4}
152 // CHECK-DAG: ![[ALIGN_16_META]] = !{i64 16}