1 // compile-flags: -O -C no-prepopulate-passes
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.
8 // CHECK-LABEL: define{{.*}}i32 @f32_to_bits(float %x)
9 // CHECK: store i32 %{{.*}}, {{.*}} %0
10 // CHECK-NEXT: %[[RES:.*]] = load i32, {{.*}} %0
11 // CHECK: ret i32 %[[RES]]
13 pub fn f32_to_bits(x: f32) -> u32 {
14 unsafe { std::mem::transmute(x) }
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
23 pub fn bool_to_byte(b: bool) -> u8 {
24 unsafe { std::mem::transmute(b) }
27 // CHECK-LABEL: define{{.*}}noundef zeroext i1 @byte_to_bool(i8 %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
35 pub unsafe fn byte_to_bool(byte: u8) -> bool {
36 std::mem::transmute(byte)
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]]
44 pub fn ptr_to_ptr(p: *mut u16) -> *mut u8 {
45 unsafe { std::mem::transmute(p) }
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`.
52 // Tests below show the non-special-cased behavior (with the possible
53 // future special-cased instructions in the "NOTE(eddyb)" comments).
55 // CHECK: define{{.*}}[[USIZE:i[0-9]+]] @ptr_to_int({{i16\*|ptr}} %p)
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, {{.*}}
62 // CHECK-NEXT: %[[RES:.*]] = load [[USIZE]], {{.*}} %0
63 // CHECK: ret [[USIZE]] %[[RES]]
65 pub fn ptr_to_int(p: *mut u16) -> usize {
66 unsafe { std::mem::transmute(p) }
69 // CHECK: define{{.*}}{{i16\*|ptr}} @int_to_ptr([[USIZE]] %i)
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, {{.*}}
76 // CHECK-NEXT: %[[RES:.*]] = load {{i16\*|ptr}}, {{.*}} %0
77 // CHECK: ret {{i16\*|ptr}} %[[RES]]
79 pub fn int_to_ptr(i: usize) -> *mut u16 {
80 unsafe { std::mem::transmute(i) }