summaryrefslogtreecommitdiffstats
path: root/tests/mir-opt/lower_intrinsics.rs
blob: cba2bc18d867bcedc6e59bf36e2dd1bbce54f9f8 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// unit-test: LowerIntrinsics
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

#![feature(core_intrinsics, intrinsics, rustc_attrs)]
#![crate_type = "lib"]

// EMIT_MIR lower_intrinsics.wrapping.LowerIntrinsics.diff
pub fn wrapping(a: i32, b: i32) {
    // CHECK-LABEL: fn wrapping(
    // CHECK: {{_.*}} = Add(
    // CHECK: {{_.*}} = Sub(
    // CHECK: {{_.*}} = Mul(
    let _x = core::intrinsics::wrapping_add(a, b);
    let _y = core::intrinsics::wrapping_sub(a, b);
    let _z = core::intrinsics::wrapping_mul(a, b);
}

// EMIT_MIR lower_intrinsics.unchecked.LowerIntrinsics.diff
pub unsafe fn unchecked(a: i32, b: i32) {
    // CHECK-LABEL: fn unchecked(
    // CHECK: {{_.*}} = AddUnchecked(
    // CHECK: {{_.*}} = SubUnchecked(
    // CHECK: {{_.*}} = MulUnchecked(
    // CHECK: {{_.*}} = Div(
    // CHECK: {{_.*}} = Rem(
    // CHECK: {{_.*}} = ShlUnchecked(
    // CHECK: {{_.*}} = ShrUnchecked(
    let _a = core::intrinsics::unchecked_add(a, b);
    let _b = core::intrinsics::unchecked_sub(a, b);
    let _c = core::intrinsics::unchecked_mul(a, b);
    let _x = core::intrinsics::unchecked_div(a, b);
    let _y = core::intrinsics::unchecked_rem(a, b);
    let _i = core::intrinsics::unchecked_shl(a, b);
    let _j = core::intrinsics::unchecked_shr(a, b);
}

// EMIT_MIR lower_intrinsics.size_of.LowerIntrinsics.diff
pub fn size_of<T>() -> usize {
    // CHECK-LABEL: fn size_of(
    // CHECK: {{_.*}} = SizeOf(T);
    core::intrinsics::size_of::<T>()
}

// EMIT_MIR lower_intrinsics.align_of.LowerIntrinsics.diff
pub fn align_of<T>() -> usize {
    // CHECK-LABEL: fn align_of(
    // CHECK: {{_.*}} = AlignOf(T);
    core::intrinsics::min_align_of::<T>()
}

// EMIT_MIR lower_intrinsics.forget.LowerIntrinsics.diff
pub fn forget<T>(t: T) {
    // CHECK-LABEL: fn forget(
    // CHECK-NOT: Drop(
    // CHECK: return;
    // CHECK-NOT: Drop(
    core::intrinsics::forget(t)
}

// EMIT_MIR lower_intrinsics.unreachable.LowerIntrinsics.diff
pub fn unreachable() -> ! {
    // CHECK-LABEL: fn unreachable(
    // CHECK: unreachable;
    unsafe { core::intrinsics::unreachable() };
}

// EMIT_MIR lower_intrinsics.non_const.LowerIntrinsics.diff
pub fn non_const<T>() -> usize {
    // CHECK-LABEL: fn non_const(
    // CHECK: SizeOf(T);

    // Check that lowering works with non-const operand as a func.
    let size_of_t = core::intrinsics::size_of::<T>;
    size_of_t()
}

// EMIT_MIR lower_intrinsics.transmute_inhabited.LowerIntrinsics.diff
pub fn transmute_inhabited(c: std::cmp::Ordering) -> i8 {
    // CHECK-LABEL: fn transmute_inhabited(
    // CHECK: {{_.*}} = {{.*}} as i8 (Transmute);

    unsafe { std::mem::transmute(c) }
}

// EMIT_MIR lower_intrinsics.transmute_uninhabited.LowerIntrinsics.diff
pub unsafe fn transmute_uninhabited(u: ()) -> Never {
    // CHECK-LABEL: fn transmute_uninhabited(
    // CHECK: {{_.*}} = {{.*}} as Never (Transmute);
    // CHECK: unreachable;

    unsafe { std::mem::transmute::<(), Never>(u) }
}

// EMIT_MIR lower_intrinsics.transmute_ref_dst.LowerIntrinsics.diff
pub unsafe fn transmute_ref_dst<T: ?Sized>(u: &T) -> *const T {
    // CHECK-LABEL: fn transmute_ref_dst(
    // CHECK: {{_.*}} = {{.*}} as *const T (Transmute);

    unsafe { std::mem::transmute(u) }
}

// EMIT_MIR lower_intrinsics.transmute_to_ref_uninhabited.LowerIntrinsics.diff
pub unsafe fn transmute_to_ref_uninhabited() -> ! {
    // CHECK-LABEL: fn transmute_to_ref_uninhabited(
    // CHECK: {{_.*}} = {{.*}} as &Never (Transmute);
    // CHECK: unreachable;

    let x: &Never = std::mem::transmute(1usize);
    match *x {}
}

// EMIT_MIR lower_intrinsics.transmute_to_mut_uninhabited.LowerIntrinsics.diff
pub unsafe fn transmute_to_mut_uninhabited() -> ! {
    // CHECK-LABEL: fn transmute_to_mut_uninhabited(
    // CHECK: {{_.*}} = {{.*}} as &mut Never (Transmute);
    // CHECK: unreachable;

    let x: &mut Never = std::mem::transmute(1usize);
    match *x {}
}

// EMIT_MIR lower_intrinsics.transmute_to_box_uninhabited.LowerIntrinsics.diff
pub unsafe fn transmute_to_box_uninhabited() -> ! {
    // CHECK-LABEL: fn transmute_to_box_uninhabited(
    // CHECK: {{_.*}} = {{.*}} as std::boxed::Box<Never> (Transmute);
    // CHECK: unreachable;

    let x: Box<Never> = std::mem::transmute(1usize);
    match *x {}
}

pub enum E {
    A,
    B,
    C,
}

// EMIT_MIR lower_intrinsics.discriminant.LowerIntrinsics.diff
pub fn discriminant<T>(t: T) {
    // CHECK-LABEL: fn discriminant(
    // CHECK: {{_.*}} = discriminant(
    // CHECK: {{_.*}} = discriminant(
    // CHECK: {{_.*}} = discriminant(
    // CHECK: {{_.*}} = discriminant(

    core::intrinsics::discriminant_value(&t);
    core::intrinsics::discriminant_value(&0);
    core::intrinsics::discriminant_value(&());
    core::intrinsics::discriminant_value(&E::B);
}

extern "rust-intrinsic" {
    // Cannot use `std::intrinsics::copy_nonoverlapping` as that is a wrapper function
    #[rustc_nounwind]
    fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
}

// EMIT_MIR lower_intrinsics.f_copy_nonoverlapping.LowerIntrinsics.diff
pub fn f_copy_nonoverlapping() {
    // CHECK-LABEL: fn f_copy_nonoverlapping(
    // CHECK: copy_nonoverlapping({{.*}});

    let src = ();
    let mut dst = ();
    unsafe {
        copy_nonoverlapping(&src as *const _ as *const i32, &mut dst as *mut _ as *mut i32, 0);
    }
}

// EMIT_MIR lower_intrinsics.assume.LowerIntrinsics.diff
pub fn assume() {
    // CHECK-LABEL: fn assume(
    // CHECK: assume({{.*}});

    unsafe {
        std::intrinsics::assume(true);
    }
}

// EMIT_MIR lower_intrinsics.with_overflow.LowerIntrinsics.diff
pub fn with_overflow(a: i32, b: i32) {
    // CHECK-LABEL: fn with_overflow(
    // CHECK: CheckedAdd(
    // CHECK: CheckedSub(
    // CHECK: CheckedMul(

    let _x = core::intrinsics::add_with_overflow(a, b);
    let _y = core::intrinsics::sub_with_overflow(a, b);
    let _z = core::intrinsics::mul_with_overflow(a, b);
}

// EMIT_MIR lower_intrinsics.read_via_copy_primitive.LowerIntrinsics.diff
pub fn read_via_copy_primitive(r: &i32) -> i32 {
    // CHECK-LABEL: fn read_via_copy_primitive(
    // CHECK: [[tmp:_.*]] = &raw const (*_1);
    // CHECK: _0 = (*[[tmp]]);
    // CHECK: return;

    unsafe { core::intrinsics::read_via_copy(r) }
}

// EMIT_MIR lower_intrinsics.read_via_copy_uninhabited.LowerIntrinsics.diff
pub fn read_via_copy_uninhabited(r: &Never) -> Never {
    // CHECK-LABEL: fn read_via_copy_uninhabited(
    // CHECK: [[tmp:_.*]] = &raw const (*_1);
    // CHECK: _0 = (*[[tmp]]);
    // CHECK: unreachable;

    unsafe { core::intrinsics::read_via_copy(r) }
}

// EMIT_MIR lower_intrinsics.write_via_move_string.LowerIntrinsics.diff
pub fn write_via_move_string(r: &mut String, v: String) {
    // CHECK-LABEL: fn write_via_move_string(
    // CHECK: [[ptr:_.*]] = &raw mut (*_1);
    // CHECK: [[tmp:_.*]] = move _2;
    // CHECK: (*[[ptr]]) = move [[tmp]];
    // CHECK: return;

    unsafe { core::intrinsics::write_via_move(r, v) }
}

pub enum Never {}

// EMIT_MIR lower_intrinsics.ptr_offset.LowerIntrinsics.diff
pub unsafe fn ptr_offset(p: *const i32, d: isize) -> *const i32 {
    // CHECK-LABEL: fn ptr_offset(
    // CHECK: _0 = Offset(

    core::intrinsics::offset(p, d)
}