]> git.proxmox.com Git - rustc.git/blame - src/test/codegen/transmute-scalar.rs
Update unsuspicious file list
[rustc.git] / src / test / codegen / transmute-scalar.rs
CommitLineData
fc512014
XL
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
cdc7bbd5 8// CHECK-LABEL: define{{.*}}i32 @f32_to_bits(float %x)
923072b8
FG
9// CHECK: store i32 %{{.*}}, {{.*}} %0
10// CHECK-NEXT: %[[RES:.*]] = load i32, {{.*}} %0
11// CHECK: ret i32 %[[RES]]
fc512014
XL
12#[no_mangle]
13pub fn f32_to_bits(x: f32) -> u32 {
14 unsafe { std::mem::transmute(x) }
15}
16
5099ac24 17// CHECK-LABEL: define{{.*}}i8 @bool_to_byte(i1 noundef zeroext %b)
fc512014 18// CHECK: %1 = zext i1 %b to i8
923072b8
FG
19// CHECK-NEXT: store i8 %1, {{.*}} %0
20// CHECK-NEXT: %2 = load i8, {{.*}} %0
fc512014
XL
21// CHECK: ret i8 %2
22#[no_mangle]
23pub fn bool_to_byte(b: bool) -> u8 {
24 unsafe { std::mem::transmute(b) }
25}
26
5099ac24 27// CHECK-LABEL: define{{.*}}noundef zeroext i1 @byte_to_bool(i8 %byte)
fc512014
XL
28// CHECK: %1 = trunc i8 %byte to i1
29// CHECK-NEXT: %2 = zext i1 %1 to i8
923072b8
FG
30// CHECK-NEXT: store i8 %2, {{.*}} %0
31// CHECK-NEXT: %3 = load i8, {{.*}} %0
fc512014
XL
32// CHECK-NEXT: %4 = trunc i8 %3 to i1
33// CHECK: ret i1 %4
34#[no_mangle]
35pub unsafe fn byte_to_bool(byte: u8) -> bool {
36 std::mem::transmute(byte)
37}
38
923072b8
FG
39// CHECK-LABEL: define{{.*}}{{i8\*|ptr}} @ptr_to_ptr({{i16\*|ptr}} %p)
40// CHECK: store {{i8\*|ptr}} %{{.*}}, {{.*}} %0
41// CHECK-NEXT: %[[RES:.*]] = load {{i8\*|ptr}}, {{.*}} %0
42// CHECK: ret {{i8\*|ptr}} %[[RES]]
fc512014
XL
43#[no_mangle]
44pub 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
923072b8 55// CHECK: define{{.*}}[[USIZE:i[0-9]+]] @ptr_to_int({{i16\*|ptr}} %p)
fc512014
XL
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
923072b8 60// CHECK: store {{i16\*|ptr}} %p, {{.*}}
fc512014 61
923072b8
FG
62// CHECK-NEXT: %[[RES:.*]] = load [[USIZE]], {{.*}} %0
63// CHECK: ret [[USIZE]] %[[RES]]
fc512014
XL
64#[no_mangle]
65pub fn ptr_to_int(p: *mut u16) -> usize {
66 unsafe { std::mem::transmute(p) }
67}
68
923072b8 69// CHECK: define{{.*}}{{i16\*|ptr}} @int_to_ptr([[USIZE]] %i)
fc512014
XL
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
923072b8 74// CHECK: store [[USIZE]] %i, {{.*}}
fc512014 75
923072b8
FG
76// CHECK-NEXT: %[[RES:.*]] = load {{i16\*|ptr}}, {{.*}} %0
77// CHECK: ret {{i16\*|ptr}} %[[RES]]
fc512014
XL
78#[no_mangle]
79pub fn int_to_ptr(i: usize) -> *mut u16 {
80 unsafe { std::mem::transmute(i) }
81}