2024-02-22 06:10:29 -06:00
|
|
|
//@ compile-flags: -C opt-level=0 -C no-prepopulate-passes
|
2020-12-07 09:33:43 -06:00
|
|
|
|
|
|
|
#![crate_type = "lib"]
|
|
|
|
|
2023-02-24 20:32:52 -06:00
|
|
|
// With opaque ptrs in LLVM, `transmute` can load/store any `alloca` as any type,
|
|
|
|
// without needing to pointercast, and SRoA will turn that into a `bitcast`.
|
2023-04-01 03:46:36 -05:00
|
|
|
// Thus memory-to-memory transmutes don't need to generate them ourselves.
|
2023-02-24 20:32:52 -06:00
|
|
|
|
2023-04-01 03:46:36 -05:00
|
|
|
// However, `bitcast`s and `ptrtoint`s and `inttoptr`s are still worth doing when
|
|
|
|
// that allows us to avoid the `alloca`s entirely; see `rvalue_creates_operand`.
|
2020-12-09 13:54:24 -06:00
|
|
|
|
2023-04-06 02:36:36 -05:00
|
|
|
// CHECK-LABEL: define{{.*}}i32 @f32_to_bits(float %x)
|
2023-05-15 14:15:28 -05:00
|
|
|
// CHECK: %_0 = bitcast float %x to i32
|
|
|
|
// CHECK-NEXT: ret i32 %_0
|
2020-12-07 09:33:43 -06:00
|
|
|
#[no_mangle]
|
|
|
|
pub fn f32_to_bits(x: f32) -> u32 {
|
|
|
|
unsafe { std::mem::transmute(x) }
|
|
|
|
}
|
|
|
|
|
2023-04-06 02:36:36 -05:00
|
|
|
// CHECK-LABEL: define{{.*}}i8 @bool_to_byte(i1 zeroext %b)
|
2023-05-15 14:15:28 -05:00
|
|
|
// CHECK: %_0 = zext i1 %b to i8
|
|
|
|
// CHECK-NEXT: ret i8 %_0
|
2020-12-07 09:33:43 -06:00
|
|
|
#[no_mangle]
|
|
|
|
pub fn bool_to_byte(b: bool) -> u8 {
|
|
|
|
unsafe { std::mem::transmute(b) }
|
|
|
|
}
|
|
|
|
|
2023-04-06 02:36:36 -05:00
|
|
|
// CHECK-LABEL: define{{.*}}zeroext i1 @byte_to_bool(i8 %byte)
|
2023-05-15 14:15:28 -05:00
|
|
|
// CHECK: %_0 = trunc i8 %byte to i1
|
|
|
|
// CHECK-NEXT: ret i1 %_0
|
2020-12-07 09:33:43 -06:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe fn byte_to_bool(byte: u8) -> bool {
|
|
|
|
std::mem::transmute(byte)
|
|
|
|
}
|
2020-12-09 13:54:24 -06:00
|
|
|
|
2023-04-06 02:36:36 -05:00
|
|
|
// CHECK-LABEL: define{{.*}}ptr @ptr_to_ptr(ptr %p)
|
2023-04-01 03:46:36 -05:00
|
|
|
// CHECK: ret ptr %p
|
2020-12-09 13:54:24 -06:00
|
|
|
#[no_mangle]
|
|
|
|
pub fn ptr_to_ptr(p: *mut u16) -> *mut u8 {
|
|
|
|
unsafe { std::mem::transmute(p) }
|
|
|
|
}
|
|
|
|
|
2023-04-06 02:36:36 -05:00
|
|
|
// CHECK: define{{.*}}[[USIZE:i[0-9]+]] @ptr_to_int(ptr %p)
|
2023-05-15 14:15:28 -05:00
|
|
|
// CHECK: %_0 = ptrtoint ptr %p to [[USIZE]]
|
|
|
|
// CHECK-NEXT: ret [[USIZE]] %_0
|
2020-12-09 13:54:24 -06:00
|
|
|
#[no_mangle]
|
|
|
|
pub fn ptr_to_int(p: *mut u16) -> usize {
|
|
|
|
unsafe { std::mem::transmute(p) }
|
|
|
|
}
|
|
|
|
|
2023-04-06 02:36:36 -05:00
|
|
|
// CHECK: define{{.*}}ptr @int_to_ptr([[USIZE]] %i)
|
2024-02-18 18:42:27 -06:00
|
|
|
// CHECK: %_0 = getelementptr i8, ptr null, [[USIZE]] %i
|
2023-05-15 14:15:28 -05:00
|
|
|
// CHECK-NEXT: ret ptr %_0
|
2020-12-09 13:54:24 -06:00
|
|
|
#[no_mangle]
|
|
|
|
pub fn int_to_ptr(i: usize) -> *mut u16 {
|
|
|
|
unsafe { std::mem::transmute(i) }
|
|
|
|
}
|