parent
d8fc819247
commit
1bcb0ec28c
4 changed files with 253 additions and 16 deletions
184
tests/codegen/intrinsics/transmute-niched.rs
Normal file
184
tests/codegen/intrinsics/transmute-niched.rs
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
// revisions: OPT DBG
|
||||
// [OPT] compile-flags: -C opt-level=3 -C no-prepopulate-passes
|
||||
// [DBG] compile-flags: -C opt-level=0 -C no-prepopulate-passes
|
||||
// only-64bit (so I don't need to worry about usize)
|
||||
// min-llvm-version: 15.0 # this test assumes `ptr`s
|
||||
|
||||
#![crate_type = "lib"]
|
||||
|
||||
use std::mem::transmute;
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
#[repr(u8)]
|
||||
pub enum SmallEnum {
|
||||
A = 10,
|
||||
B = 11,
|
||||
C = 12,
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @check_to_enum(
|
||||
#[no_mangle]
|
||||
pub unsafe fn check_to_enum(x: i8) -> SmallEnum {
|
||||
// OPT: %0 = icmp uge i8 %x, 10
|
||||
// OPT: call void @llvm.assume(i1 %0)
|
||||
// OPT: %1 = icmp ule i8 %x, 12
|
||||
// OPT: call void @llvm.assume(i1 %1)
|
||||
// DBG-NOT: icmp
|
||||
// DBG-NOT: assume
|
||||
// CHECK: ret i8 %x
|
||||
|
||||
transmute(x)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @check_from_enum(
|
||||
#[no_mangle]
|
||||
pub unsafe fn check_from_enum(x: SmallEnum) -> i8 {
|
||||
// OPT: %0 = icmp uge i8 %x, 10
|
||||
// OPT: call void @llvm.assume(i1 %0)
|
||||
// OPT: %1 = icmp ule i8 %x, 12
|
||||
// OPT: call void @llvm.assume(i1 %1)
|
||||
// DBG-NOT: icmp
|
||||
// DBG-NOT: assume
|
||||
// CHECK: ret i8 %x
|
||||
|
||||
transmute(x)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @check_to_ordering(
|
||||
#[no_mangle]
|
||||
pub unsafe fn check_to_ordering(x: u8) -> std::cmp::Ordering {
|
||||
// OPT: %0 = icmp uge i8 %x, -1
|
||||
// OPT: %1 = icmp ule i8 %x, 1
|
||||
// OPT: %2 = or i1 %0, %1
|
||||
// OPT: call void @llvm.assume(i1 %2)
|
||||
// DBG-NOT: icmp
|
||||
// DBG-NOT: assume
|
||||
// CHECK: ret i8 %x
|
||||
|
||||
transmute(x)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @check_from_ordering(
|
||||
#[no_mangle]
|
||||
pub unsafe fn check_from_ordering(x: std::cmp::Ordering) -> u8 {
|
||||
// OPT: %0 = icmp uge i8 %x, -1
|
||||
// OPT: %1 = icmp ule i8 %x, 1
|
||||
// OPT: %2 = or i1 %0, %1
|
||||
// OPT: call void @llvm.assume(i1 %2)
|
||||
// DBG-NOT: icmp
|
||||
// DBG-NOT: assume
|
||||
// CHECK: ret i8 %x
|
||||
|
||||
transmute(x)
|
||||
}
|
||||
|
||||
#[repr(i32)]
|
||||
pub enum Minus100ToPlus100 {
|
||||
A = -100,
|
||||
B = -90,
|
||||
C = -80,
|
||||
D = -70,
|
||||
E = -60,
|
||||
F = -50,
|
||||
G = -40,
|
||||
H = -30,
|
||||
I = -20,
|
||||
J = -10,
|
||||
K = 0,
|
||||
L = 10,
|
||||
M = 20,
|
||||
N = 30,
|
||||
O = 40,
|
||||
P = 50,
|
||||
Q = 60,
|
||||
R = 70,
|
||||
S = 80,
|
||||
T = 90,
|
||||
U = 100,
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @check_enum_from_char(
|
||||
#[no_mangle]
|
||||
pub unsafe fn check_enum_from_char(x: char) -> Minus100ToPlus100 {
|
||||
// OPT: %0 = icmp ule i32 %x, 1114111
|
||||
// OPT: call void @llvm.assume(i1 %0)
|
||||
// OPT: %1 = icmp uge i32 %x, -100
|
||||
// OPT: %2 = icmp ule i32 %x, 100
|
||||
// OPT: %3 = or i1 %1, %2
|
||||
// OPT: call void @llvm.assume(i1 %3)
|
||||
// DBG-NOT: icmp
|
||||
// DBG-NOT: assume
|
||||
// CHECK: ret i32 %x
|
||||
|
||||
transmute(x)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @check_enum_to_char(
|
||||
#[no_mangle]
|
||||
pub unsafe fn check_enum_to_char(x: Minus100ToPlus100) -> char {
|
||||
// OPT: %0 = icmp uge i32 %x, -100
|
||||
// OPT: %1 = icmp ule i32 %x, 100
|
||||
// OPT: %2 = or i1 %0, %1
|
||||
// OPT: call void @llvm.assume(i1 %2)
|
||||
// OPT: %3 = icmp ule i32 %x, 1114111
|
||||
// OPT: call void @llvm.assume(i1 %3)
|
||||
// DBG-NOT: icmp
|
||||
// DBG-NOT: assume
|
||||
// CHECK: ret i32 %x
|
||||
|
||||
transmute(x)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @check_swap_pair(
|
||||
#[no_mangle]
|
||||
pub unsafe fn check_swap_pair(x: (char, NonZeroU32)) -> (NonZeroU32, char) {
|
||||
// OPT: %0 = icmp ule i32 %x.0, 1114111
|
||||
// OPT: call void @llvm.assume(i1 %0)
|
||||
// OPT: %1 = icmp uge i32 %x.0, 1
|
||||
// OPT: call void @llvm.assume(i1 %1)
|
||||
// OPT: %2 = icmp uge i32 %x.1, 1
|
||||
// OPT: call void @llvm.assume(i1 %2)
|
||||
// OPT: %3 = icmp ule i32 %x.1, 1114111
|
||||
// OPT: call void @llvm.assume(i1 %3)
|
||||
// DBG-NOT: icmp
|
||||
// DBG-NOT: assume
|
||||
// CHECK: %[[P1:.+]] = insertvalue { i32, i32 } poison, i32 %x.0, 0
|
||||
// CHECK: %[[P2:.+]] = insertvalue { i32, i32 } %[[P1]], i32 %x.1, 1
|
||||
// CHECK: ret { i32, i32 } %[[P2]]
|
||||
|
||||
transmute(x)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @check_bool_from_ordering(
|
||||
#[no_mangle]
|
||||
pub unsafe fn check_bool_from_ordering(x: std::cmp::Ordering) -> bool {
|
||||
// OPT: %0 = icmp uge i8 %x, -1
|
||||
// OPT: %1 = icmp ule i8 %x, 1
|
||||
// OPT: %2 = or i1 %0, %1
|
||||
// OPT: call void @llvm.assume(i1 %2)
|
||||
// OPT: %3 = icmp ule i8 %x, 1
|
||||
// OPT: call void @llvm.assume(i1 %3)
|
||||
// DBG-NOT: icmp
|
||||
// DBG-NOT: assume
|
||||
// CHECK: %[[R:.+]] = trunc i8 %x to i1
|
||||
// CHECK: ret i1 %[[R]]
|
||||
|
||||
transmute(x)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @check_bool_to_ordering(
|
||||
#[no_mangle]
|
||||
pub unsafe fn check_bool_to_ordering(x: bool) -> std::cmp::Ordering {
|
||||
// CHECK: %0 = zext i1 %x to i8
|
||||
// OPT: %1 = icmp ule i8 %0, 1
|
||||
// OPT: call void @llvm.assume(i1 %1)
|
||||
// OPT: %2 = icmp uge i8 %0, -1
|
||||
// OPT: %3 = icmp ule i8 %0, 1
|
||||
// OPT: %4 = or i1 %2, %3
|
||||
// OPT: call void @llvm.assume(i1 %4)
|
||||
// DBG-NOT: icmp
|
||||
// DBG-NOT: assume
|
||||
// CHECK: ret i8 %0
|
||||
|
||||
transmute(x)
|
||||
}
|
||||
|
|
@ -169,8 +169,8 @@ pub unsafe fn check_aggregate_from_bool(x: bool) -> Aggregate8 {
|
|||
#[no_mangle]
|
||||
pub unsafe fn check_byte_to_bool(x: u8) -> bool {
|
||||
// CHECK-NOT: alloca
|
||||
// CHECK: %0 = trunc i8 %x to i1
|
||||
// CHECK: ret i1 %0
|
||||
// CHECK: %[[R:.+]] = trunc i8 %x to i1
|
||||
// CHECK: ret i1 %[[R]]
|
||||
transmute(x)
|
||||
}
|
||||
|
||||
|
|
@ -178,8 +178,8 @@ pub unsafe fn check_byte_to_bool(x: u8) -> bool {
|
|||
#[no_mangle]
|
||||
pub unsafe fn check_byte_from_bool(x: bool) -> u8 {
|
||||
// CHECK-NOT: alloca
|
||||
// CHECK: %0 = zext i1 %x to i8
|
||||
// CHECK: ret i8 %0
|
||||
// CHECK: %[[R:.+]] = zext i1 %x to i8
|
||||
// CHECK: ret i8 %[[R:.+]]
|
||||
transmute(x)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
// compile-flags: -O -C no-prepopulate-passes
|
||||
// compile-flags: -C opt-level=0 -C no-prepopulate-passes
|
||||
// min-llvm-version: 15.0 # this test assumes `ptr`s and thus no `pointercast`s
|
||||
|
||||
#![crate_type = "lib"]
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
// 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`.
|
||||
|
||||
// CHECK-LABEL: define{{.*}}i32 @f32_to_bits(float noundef %x)
|
||||
// CHECK-LABEL: define{{.*}}i32 @f32_to_bits(float %x)
|
||||
// CHECK: %0 = bitcast float %x to i32
|
||||
// CHECK-NEXT: ret i32 %0
|
||||
#[no_mangle]
|
||||
|
|
@ -18,7 +18,7 @@ pub fn f32_to_bits(x: f32) -> u32 {
|
|||
unsafe { std::mem::transmute(x) }
|
||||
}
|
||||
|
||||
// CHECK-LABEL: define{{.*}}i8 @bool_to_byte(i1 noundef zeroext %b)
|
||||
// CHECK-LABEL: define{{.*}}i8 @bool_to_byte(i1 zeroext %b)
|
||||
// CHECK: %0 = zext i1 %b to i8
|
||||
// CHECK-NEXT: ret i8 %0
|
||||
#[no_mangle]
|
||||
|
|
@ -26,7 +26,7 @@ pub fn bool_to_byte(b: bool) -> u8 {
|
|||
unsafe { std::mem::transmute(b) }
|
||||
}
|
||||
|
||||
// CHECK-LABEL: define{{.*}}noundef zeroext i1 @byte_to_bool(i8 noundef %byte)
|
||||
// CHECK-LABEL: define{{.*}}zeroext i1 @byte_to_bool(i8 %byte)
|
||||
// CHECK: %0 = trunc i8 %byte to i1
|
||||
// CHECK-NEXT: ret i1 %0
|
||||
#[no_mangle]
|
||||
|
|
@ -34,14 +34,14 @@ pub unsafe fn byte_to_bool(byte: u8) -> bool {
|
|||
std::mem::transmute(byte)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: define{{.*}}ptr @ptr_to_ptr(ptr noundef %p)
|
||||
// CHECK-LABEL: define{{.*}}ptr @ptr_to_ptr(ptr %p)
|
||||
// CHECK: ret ptr %p
|
||||
#[no_mangle]
|
||||
pub fn ptr_to_ptr(p: *mut u16) -> *mut u8 {
|
||||
unsafe { std::mem::transmute(p) }
|
||||
}
|
||||
|
||||
// CHECK: define{{.*}}[[USIZE:i[0-9]+]] @ptr_to_int(ptr noundef %p)
|
||||
// CHECK: define{{.*}}[[USIZE:i[0-9]+]] @ptr_to_int(ptr %p)
|
||||
// CHECK: %0 = ptrtoint ptr %p to [[USIZE]]
|
||||
// CHECK-NEXT: ret [[USIZE]] %0
|
||||
#[no_mangle]
|
||||
|
|
@ -49,7 +49,7 @@ pub fn ptr_to_int(p: *mut u16) -> usize {
|
|||
unsafe { std::mem::transmute(p) }
|
||||
}
|
||||
|
||||
// CHECK: define{{.*}}ptr @int_to_ptr([[USIZE]] noundef %i)
|
||||
// CHECK: define{{.*}}ptr @int_to_ptr([[USIZE]] %i)
|
||||
// CHECK: %0 = inttoptr [[USIZE]] %i to ptr
|
||||
// CHECK-NEXT: ret ptr %0
|
||||
#[no_mangle]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue