]> git.lizzy.rs Git - rust.git/blob - tests/codegen/transmute-scalar.rs
Rollup merge of #106979 - Nilstrieb:type-of-default-assoc-type, r=petrochenkov
[rust.git] / tests / codegen / transmute-scalar.rs
1 // compile-flags: -O -C no-prepopulate-passes
2
3 #![crate_type = "lib"]
4
5 // FIXME(eddyb) all of these tests show memory stores and loads, even after a
6 // scalar `bitcast`, more special-casing is required to remove `alloca` usage.
7
8 // CHECK-LABEL: define{{.*}}i32 @f32_to_bits(float noundef %x)
9 // CHECK: store i32 %{{.*}}, {{.*}} %0
10 // CHECK-NEXT: %[[RES:.*]] = load i32, {{.*}} %0
11 // CHECK: ret i32 %[[RES]]
12 #[no_mangle]
13 pub fn f32_to_bits(x: f32) -> u32 {
14     unsafe { std::mem::transmute(x) }
15 }
16
17 // CHECK-LABEL: define{{.*}}i8 @bool_to_byte(i1 noundef zeroext %b)
18 // CHECK: %1 = zext i1 %b to i8
19 // CHECK-NEXT: store i8 %1, {{.*}} %0
20 // CHECK-NEXT: %2 = load i8, {{.*}} %0
21 // CHECK: ret i8 %2
22 #[no_mangle]
23 pub fn bool_to_byte(b: bool) -> u8 {
24     unsafe { std::mem::transmute(b) }
25 }
26
27 // CHECK-LABEL: define{{.*}}noundef zeroext i1 @byte_to_bool(i8 noundef %byte)
28 // CHECK: %1 = trunc i8 %byte to i1
29 // CHECK-NEXT: %2 = zext i1 %1 to i8
30 // CHECK-NEXT: store i8 %2, {{.*}} %0
31 // CHECK-NEXT: %3 = load i8, {{.*}} %0
32 // CHECK-NEXT: %4 = trunc i8 %3 to i1
33 // CHECK: ret i1 %4
34 #[no_mangle]
35 pub unsafe fn byte_to_bool(byte: u8) -> bool {
36     std::mem::transmute(byte)
37 }
38
39 // CHECK-LABEL: define{{.*}}{{i8\*|ptr}} @ptr_to_ptr({{i16\*|ptr}} noundef %p)
40 // CHECK: store {{i8\*|ptr}} %{{.*}}, {{.*}} %0
41 // CHECK-NEXT: %[[RES:.*]] = load {{i8\*|ptr}}, {{.*}} %0
42 // CHECK: ret {{i8\*|ptr}} %[[RES]]
43 #[no_mangle]
44 pub fn ptr_to_ptr(p: *mut u16) -> *mut u8 {
45     unsafe { std::mem::transmute(p) }
46 }
47
48 // HACK(eddyb) scalar `transmute`s between pointers and non-pointers are
49 // currently not special-cased like other scalar `transmute`s, because
50 // LLVM requires specifically `ptrtoint`/`inttoptr` instead of `bitcast`.
51 //
52 // Tests below show the non-special-cased behavior (with the possible
53 // future special-cased instructions in the "NOTE(eddyb)" comments).
54
55 // CHECK: define{{.*}}[[USIZE:i[0-9]+]] @ptr_to_int({{i16\*|ptr}} noundef %p)
56
57 // NOTE(eddyb) see above, the following two CHECK lines should ideally be this:
58 //        %2 = ptrtoint i16* %p to [[USIZE]]
59 //             store [[USIZE]] %2, [[USIZE]]* %0
60 // CHECK: store {{i16\*|ptr}} %p, {{.*}}
61
62 // CHECK-NEXT: %[[RES:.*]] = load [[USIZE]], {{.*}} %0
63 // CHECK: ret [[USIZE]] %[[RES]]
64 #[no_mangle]
65 pub fn ptr_to_int(p: *mut u16) -> usize {
66     unsafe { std::mem::transmute(p) }
67 }
68
69 // CHECK: define{{.*}}{{i16\*|ptr}} @int_to_ptr([[USIZE]] noundef %i)
70
71 // NOTE(eddyb) see above, the following two CHECK lines should ideally be this:
72 //        %2 = inttoptr [[USIZE]] %i to i16*
73 //             store i16* %2, i16** %0
74 // CHECK: store [[USIZE]] %i, {{.*}}
75
76 // CHECK-NEXT: %[[RES:.*]] = load {{i16\*|ptr}}, {{.*}} %0
77 // CHECK: ret {{i16\*|ptr}} %[[RES]]
78 #[no_mangle]
79 pub fn int_to_ptr(i: usize) -> *mut u16 {
80     unsafe { std::mem::transmute(i) }
81 }