File: unchecked_shifts.rs

package info (click to toggle)
rustc-web 1.85.0%2Bdfsg3-1~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,759,988 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,056; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (113 lines) | stat: -rw-r--r-- 3,717 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//@ compile-flags: -O

#![crate_type = "lib"]
#![feature(unchecked_shifts)]
#![feature(core_intrinsics)]

// CHECK-LABEL: @unchecked_shl_unsigned_same
#[no_mangle]
pub unsafe fn unchecked_shl_unsigned_same(a: u32, b: u32) -> u32 {
    // CHECK-NOT: assume
    // CHECK-NOT: and i32
    // CHECK: shl i32 %a, %b
    // CHECK-NOT: and i32
    a.unchecked_shl(b)
}

// CHECK-LABEL: @unchecked_shl_unsigned_smaller
#[no_mangle]
pub unsafe fn unchecked_shl_unsigned_smaller(a: u16, b: u32) -> u16 {
    // This uses -DAG to avoid failing on irrelevant reorderings,
    // like emitting the truncation earlier.

    // CHECK-DAG: %[[INRANGE:.+]] = icmp ult i32 %b, 16
    // CHECK-DAG: tail call void @llvm.assume(i1 %[[INRANGE]])
    // CHECK-DAG: %[[TRUNC:.+]] = trunc{{( nuw)?( nsw)?}} i32 %b to i16
    // CHECK-DAG: shl i16 %a, %[[TRUNC]]
    a.unchecked_shl(b)
}

// CHECK-LABEL: @unchecked_shl_unsigned_bigger
#[no_mangle]
pub unsafe fn unchecked_shl_unsigned_bigger(a: u64, b: u32) -> u64 {
    // CHECK-NOT: assume
    // CHECK: %[[EXT:.+]] = zext{{( nneg)?}} i32 %b to i64
    // CHECK: shl i64 %a, %[[EXT]]
    a.unchecked_shl(b)
}

// CHECK-LABEL: @unchecked_shr_signed_same
#[no_mangle]
pub unsafe fn unchecked_shr_signed_same(a: i32, b: u32) -> i32 {
    // CHECK-NOT: assume
    // CHECK-NOT: and i32
    // CHECK: ashr i32 %a, %b
    // CHECK-NOT: and i32
    a.unchecked_shr(b)
}

// CHECK-LABEL: @unchecked_shr_signed_smaller
#[no_mangle]
pub unsafe fn unchecked_shr_signed_smaller(a: i16, b: u32) -> i16 {
    // This uses -DAG to avoid failing on irrelevant reorderings,
    // like emitting the truncation earlier.

    // CHECK-DAG: %[[INRANGE:.+]] = icmp ult i32 %b, 16
    // CHECK-DAG: tail call void @llvm.assume(i1 %[[INRANGE]])
    // CHECK-DAG: %[[TRUNC:.+]] = trunc{{( nuw)?( nsw)?}}  i32 %b to i16
    // CHECK-DAG: ashr i16 %a, %[[TRUNC]]
    a.unchecked_shr(b)
}

// CHECK-LABEL: @unchecked_shr_signed_bigger
#[no_mangle]
pub unsafe fn unchecked_shr_signed_bigger(a: i64, b: u32) -> i64 {
    // CHECK-NOT: assume
    // CHECK: %[[EXT:.+]] = zext{{( nneg)?}} i32 %b to i64
    // CHECK: ashr i64 %a, %[[EXT]]
    a.unchecked_shr(b)
}

// CHECK-LABEL: @unchecked_shr_u128_i8
#[no_mangle]
pub unsafe fn unchecked_shr_u128_i8(a: u128, b: i8) -> u128 {
    // CHECK-NOT: assume
    // CHECK: %[[EXT:.+]] = zext{{( nneg)?}} i8 %b to i128
    // CHECK: lshr i128 %a, %[[EXT]]
    std::intrinsics::unchecked_shr(a, b)
}

// CHECK-LABEL: @unchecked_shl_i128_u8
#[no_mangle]
pub unsafe fn unchecked_shl_i128_u8(a: i128, b: u8) -> i128 {
    // CHECK-NOT: assume
    // CHECK: %[[EXT:.+]] = zext{{( nneg)?}} i8 %b to i128
    // CHECK: shl i128 %a, %[[EXT]]
    std::intrinsics::unchecked_shl(a, b)
}

// CHECK-LABEL: @unchecked_shl_u8_i128
#[no_mangle]
pub unsafe fn unchecked_shl_u8_i128(a: u8, b: i128) -> u8 {
    // This uses -DAG to avoid failing on irrelevant reorderings,
    // like emitting the truncation earlier.

    // CHECK-DAG: %[[INRANGE:.+]] = icmp ult i128 %b, 8
    // CHECK-DAG: tail call void @llvm.assume(i1 %[[INRANGE]])
    // CHECK-DAG: %[[TRUNC:.+]] = trunc{{( nuw)?( nsw)?}} i128 %b to i8
    // CHECK-DAG: shl i8 %a, %[[TRUNC]]
    std::intrinsics::unchecked_shl(a, b)
}

// CHECK-LABEL: @unchecked_shr_i8_u128
#[no_mangle]
pub unsafe fn unchecked_shr_i8_u128(a: i8, b: u128) -> i8 {
    // This uses -DAG to avoid failing on irrelevant reorderings,
    // like emitting the truncation earlier.

    // CHECK-DAG: %[[INRANGE:.+]] = icmp ult i128 %b, 8
    // CHECK-DAG: tail call void @llvm.assume(i1 %[[INRANGE]])
    // CHECK-DAG: %[[TRUNC:.+]] = trunc{{( nuw)?( nsw)?}} i128 %b to i8
    // CHECK-DAG: ashr i8 %a, %[[TRUNC]]
    std::intrinsics::unchecked_shr(a, b)
}