]> git.lizzy.rs Git - rust.git/blob - src/intrinsics/mod.rs
Move simd intrinsics to intrinsics/simd.rs
[rust.git] / src / intrinsics / mod.rs
1 pub mod llvm;
2 mod simd;
3
4 use crate::prelude::*;
5
6 macro intrinsic_pat {
7     (_) => {
8         _
9     },
10     ($name:ident) => {
11         stringify!($name)
12     },
13     ($name:literal) => {
14         stringify!($name)
15     },
16     ($x:ident . $($xs:tt).*) => {
17         concat!(stringify!($x), ".", intrinsic_pat!($($xs).*))
18     }
19 }
20
21 macro intrinsic_arg {
22     (o $fx:expr, $arg:ident) => {
23         $arg
24     },
25     (c $fx:expr, $arg:ident) => {
26         trans_operand($fx, $arg)
27     },
28     (v $fx:expr, $arg:ident) => {
29         trans_operand($fx, $arg).load_scalar($fx)
30     }
31 }
32
33 macro intrinsic_substs {
34     ($substs:expr, $index:expr,) => {},
35     ($substs:expr, $index:expr, $first:ident $(,$rest:ident)*) => {
36         let $first = $substs.type_at($index);
37         intrinsic_substs!($substs, $index+1, $($rest),*);
38     }
39 }
40
41 macro intrinsic_match {
42     ($fx:expr, $intrinsic:expr, $substs:expr, $args:expr,
43     _ => $unknown:block;
44     $(
45         $($($name:tt).*)|+ $(if $cond:expr)?, $(<$($subst:ident),*>)? ($($a:ident $arg:ident),*) $content:block;
46     )*) => {
47         match $intrinsic {
48             $(
49                 $(intrinsic_pat!($($name).*))|* $(if $cond)? => {
50                     #[allow(unused_parens, non_snake_case)]
51                     {
52                         $(
53                             intrinsic_substs!($substs, 0, $($subst),*);
54                         )?
55                         if let [$($arg),*] = $args {
56                             let ($($arg,)*) = (
57                                 $(intrinsic_arg!($a $fx, $arg),)*
58                             );
59                             #[warn(unused_parens, non_snake_case)]
60                             {
61                                 $content
62                             }
63                         } else {
64                             bug!("wrong number of args for intrinsic {:?}", $intrinsic);
65                         }
66                     }
67                 }
68             )*
69             _ => $unknown,
70         }
71     }
72 }
73
74 macro call_intrinsic_match {
75     ($fx:expr, $intrinsic:expr, $substs:expr, $ret:expr, $destination:expr, $args:expr, $(
76         $name:ident($($arg:ident),*) -> $ty:ident => $func:ident,
77     )*) => {
78         match $intrinsic {
79             $(
80                 stringify!($name) => {
81                     assert!($substs.is_noop());
82                     if let [$(ref $arg),*] = *$args {
83                         let ($($arg,)*) = (
84                             $(trans_operand($fx, $arg),)*
85                         );
86                         let res = $fx.easy_call(stringify!($func), &[$($arg),*], $fx.tcx.types.$ty);
87                         $ret.write_cvalue($fx, res);
88
89                         if let Some((_, dest)) = $destination {
90                             let ret_ebb = $fx.get_ebb(dest);
91                             $fx.bcx.ins().jump(ret_ebb, &[]);
92                             return;
93                         } else {
94                             unreachable!();
95                         }
96                     } else {
97                         bug!("wrong number of args for intrinsic {:?}", $intrinsic);
98                     }
99                 }
100             )*
101             _ => {}
102         }
103     }
104 }
105
106 macro atomic_binop_return_old($fx:expr, $op:ident<$T:ident>($ptr:ident, $src:ident) -> $ret:ident)  {
107     let clif_ty = $fx.clif_type($T).unwrap();
108     let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
109     let new = $fx.bcx.ins().$op(old, $src);
110     $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
111     $ret.write_cvalue($fx, CValue::by_val(old, $fx.layout_of($T)));
112 }
113
114 macro atomic_minmax($fx:expr, $cc:expr, <$T:ident> ($ptr:ident, $src:ident) -> $ret:ident) {
115     // Read old
116     let clif_ty = $fx.clif_type($T).unwrap();
117     let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
118
119     // Compare
120     let is_eq = codegen_icmp($fx, IntCC::SignedGreaterThan, old, $src);
121     let new = $fx.bcx.ins().select(is_eq, old, $src);
122
123     // Write new
124     $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
125
126     let ret_val = CValue::by_val(old, $ret.layout());
127     $ret.write_cvalue($fx, ret_val);
128 }
129
130 fn lane_type_and_count<'tcx>(
131     fx: &FunctionCx<'_, 'tcx, impl Backend>,
132     layout: TyLayout<'tcx>,
133     intrinsic: &str,
134 ) -> (TyLayout<'tcx>, u32) {
135     assert!(layout.ty.is_simd());
136     let lane_count = match layout.fields {
137         layout::FieldPlacement::Array { stride: _, count } => u32::try_from(count).unwrap(),
138         _ => panic!(
139             "Non vector type {:?} passed to or returned from simd_* intrinsic {}",
140             layout.ty, intrinsic
141         ),
142     };
143     let lane_layout = layout.field(fx, 0);
144     (lane_layout, lane_count)
145 }
146
147 fn simd_for_each_lane<'tcx, B: Backend>(
148     fx: &mut FunctionCx<'_, 'tcx, B>,
149     intrinsic: &str,
150     x: CValue<'tcx>,
151     y: CValue<'tcx>,
152     ret: CPlace<'tcx>,
153     f: impl Fn(
154         &mut FunctionCx<'_, 'tcx, B>,
155         TyLayout<'tcx>,
156         TyLayout<'tcx>,
157         Value,
158         Value,
159     ) -> CValue<'tcx>,
160 ) {
161     assert_eq!(x.layout(), y.layout());
162     let layout = x.layout();
163
164     let (lane_layout, lane_count) = lane_type_and_count(fx, layout, intrinsic);
165     let (ret_lane_layout, ret_lane_count) = lane_type_and_count(fx, ret.layout(), intrinsic);
166     assert_eq!(lane_count, ret_lane_count);
167
168     for lane in 0..lane_count {
169         let lane = mir::Field::new(lane.try_into().unwrap());
170         let x_lane = x.value_field(fx, lane).load_scalar(fx);
171         let y_lane = y.value_field(fx, lane).load_scalar(fx);
172
173         let res_lane = f(fx, lane_layout, ret_lane_layout, x_lane, y_lane);
174
175         ret.place_field(fx, lane).write_cvalue(fx, res_lane);
176     }
177 }
178
179 fn bool_to_zero_or_max_uint<'tcx>(
180     fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
181     layout: TyLayout<'tcx>,
182     val: Value,
183 ) -> CValue<'tcx> {
184     let ty = fx.clif_type(layout.ty).unwrap();
185
186     let int_ty = match ty {
187         types::F32 => types::I32,
188         types::F64 => types::I64,
189         ty => ty,
190     };
191
192     let zero = fx.bcx.ins().iconst(int_ty, 0);
193     let max = fx
194         .bcx
195         .ins()
196         .iconst(int_ty, (u64::max_value() >> (64 - int_ty.bits())) as i64);
197     let mut res = fx.bcx.ins().select(val, max, zero);
198
199     if ty.is_float() {
200         res = fx.bcx.ins().bitcast(ty, res);
201     }
202
203     CValue::by_val(res, layout)
204 }
205
206 macro simd_cmp {
207     ($fx:expr, $intrinsic:expr, $cc:ident($x:ident, $y:ident) -> $ret:ident) => {
208         simd_for_each_lane(
209             $fx,
210             $intrinsic,
211             $x,
212             $y,
213             $ret,
214             |fx, lane_layout, res_lane_layout, x_lane, y_lane| {
215                 let res_lane = match lane_layout.ty.kind {
216                     ty::Uint(_) | ty::Int(_) => codegen_icmp(fx, IntCC::$cc, x_lane, y_lane),
217                     _ => unreachable!("{:?}", lane_layout.ty),
218                 };
219                 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
220             },
221         );
222     },
223     ($fx:expr, $intrinsic:expr, $cc_u:ident|$cc_s:ident($x:ident, $y:ident) -> $ret:ident) => {
224         simd_for_each_lane(
225             $fx,
226             $intrinsic,
227             $x,
228             $y,
229             $ret,
230             |fx, lane_layout, res_lane_layout, x_lane, y_lane| {
231                 let res_lane = match lane_layout.ty.kind {
232                     ty::Uint(_) => codegen_icmp(fx, IntCC::$cc_u, x_lane, y_lane),
233                     ty::Int(_) => codegen_icmp(fx, IntCC::$cc_s, x_lane, y_lane),
234                     _ => unreachable!("{:?}", lane_layout.ty),
235                 };
236                 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
237             },
238         );
239     },
240 }
241
242 macro simd_int_binop {
243     ($fx:expr, $intrinsic:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) => {
244         simd_for_each_lane(
245             $fx,
246             $intrinsic,
247             $x,
248             $y,
249             $ret,
250             |fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
251                 let res_lane = match lane_layout.ty.kind {
252                     ty::Uint(_) | ty::Int(_) => fx.bcx.ins().$op(x_lane, y_lane),
253                     _ => unreachable!("{:?}", lane_layout.ty),
254                 };
255                 CValue::by_val(res_lane, ret_lane_layout)
256             },
257         );
258     },
259     ($fx:expr, $intrinsic:expr, $op_u:ident|$op_s:ident($x:ident, $y:ident) -> $ret:ident) => {
260         simd_for_each_lane(
261             $fx,
262             $intrinsic,
263             $x,
264             $y,
265             $ret,
266             |fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
267                 let res_lane = match lane_layout.ty.kind {
268                     ty::Uint(_) => fx.bcx.ins().$op_u(x_lane, y_lane),
269                     ty::Int(_) => fx.bcx.ins().$op_s(x_lane, y_lane),
270                     _ => unreachable!("{:?}", lane_layout.ty),
271                 };
272                 CValue::by_val(res_lane, ret_lane_layout)
273             },
274         );
275     },
276 }
277
278 macro simd_int_flt_binop {
279     ($fx:expr, $intrinsic:expr, $op:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
280         simd_for_each_lane(
281             $fx,
282             $intrinsic,
283             $x,
284             $y,
285             $ret,
286             |fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
287                 let res_lane = match lane_layout.ty.kind {
288                     ty::Uint(_) | ty::Int(_) => fx.bcx.ins().$op(x_lane, y_lane),
289                     ty::Float(_) => fx.bcx.ins().$op_f(x_lane, y_lane),
290                     _ => unreachable!("{:?}", lane_layout.ty),
291                 };
292                 CValue::by_val(res_lane, ret_lane_layout)
293             },
294         );
295     },
296     ($fx:expr, $intrinsic:expr, $op_u:ident|$op_s:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
297         simd_for_each_lane(
298             $fx,
299             $intrinsic,
300             $x,
301             $y,
302             $ret,
303             |fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
304                 let res_lane = match lane_layout.ty.kind {
305                     ty::Uint(_) => fx.bcx.ins().$op_u(x_lane, y_lane),
306                     ty::Int(_) => fx.bcx.ins().$op_s(x_lane, y_lane),
307                     ty::Float(_) => fx.bcx.ins().$op_f(x_lane, y_lane),
308                     _ => unreachable!("{:?}", lane_layout.ty),
309                 };
310                 CValue::by_val(res_lane, ret_lane_layout)
311             },
312         );
313     },
314 }
315
316 macro simd_flt_binop($fx:expr, $intrinsic:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) {
317     simd_for_each_lane(
318         $fx,
319         $intrinsic,
320         $x,
321         $y,
322         $ret,
323         |fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
324             let res_lane = match lane_layout.ty.kind {
325                 ty::Float(_) => fx.bcx.ins().$op(x_lane, y_lane),
326                 _ => unreachable!("{:?}", lane_layout.ty),
327             };
328             CValue::by_val(res_lane, ret_lane_layout)
329         },
330     );
331 }
332
333 pub fn codegen_intrinsic_call<'tcx>(
334     fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
335     instance: Instance<'tcx>,
336     args: &[mir::Operand<'tcx>],
337     destination: Option<(CPlace<'tcx>, BasicBlock)>,
338     span: Span,
339 ) {
340     let def_id = instance.def_id();
341     let substs = instance.substs;
342
343     let intrinsic = fx.tcx.item_name(def_id).as_str();
344     let intrinsic = &intrinsic[..];
345
346     let ret = match destination {
347         Some((place, _)) => place,
348         None => {
349             // Insert non returning intrinsics here
350             match intrinsic {
351                 "abort" => {
352                     trap_panic(fx, "Called intrinsic::abort.");
353                 }
354                 "unreachable" => {
355                     trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
356                 }
357                 "transmute" => {
358                     trap_unreachable(
359                         fx,
360                         "[corruption] Called intrinsic::transmute with uninhabited argument.",
361                     );
362                 }
363                 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
364             }
365             return;
366         }
367     };
368
369     if intrinsic.starts_with("simd_") {
370         self::simd::codegen_simd_intrinsic_call(fx, instance, args, ret, span);
371         let ret_ebb = fx.get_ebb(destination.expect("SIMD intrinsics don't diverge").1);
372         fx.bcx.ins().jump(ret_ebb, &[]);
373         return;
374     }
375
376     let usize_layout = fx.layout_of(fx.tcx.types.usize);
377
378     call_intrinsic_match! {
379         fx, intrinsic, substs, ret, destination, args,
380         expf32(flt) -> f32 => expf,
381         expf64(flt) -> f64 => exp,
382         exp2f32(flt) -> f32 => exp2f,
383         exp2f64(flt) -> f64 => exp2,
384         sqrtf32(flt) -> f32 => sqrtf,
385         sqrtf64(flt) -> f64 => sqrt,
386         powif32(a, x) -> f32 => __powisf2, // compiler-builtins
387         powif64(a, x) -> f64 => __powidf2, // compiler-builtins
388         powf32(a, x) -> f32 => powf,
389         powf64(a, x) -> f64 => pow,
390         logf32(flt) -> f32 => logf,
391         logf64(flt) -> f64 => log,
392         log2f32(flt) -> f32 => log2f,
393         log2f64(flt) -> f64 => log2,
394         log10f32(flt) -> f32 => log10f,
395         log10f64(flt) -> f64 => log10,
396         fabsf32(flt) -> f32 => fabsf,
397         fabsf64(flt) -> f64 => fabs,
398         fmaf32(x, y, z) -> f32 => fmaf,
399         fmaf64(x, y, z) -> f64 => fma,
400         copysignf32(x, y) -> f32 => copysignf,
401         copysignf64(x, y) -> f64 => copysign,
402
403         // rounding variants
404         // FIXME use clif insts
405         floorf32(flt) -> f32 => floorf,
406         floorf64(flt) -> f64 => floor,
407         ceilf32(flt) -> f32 => ceilf,
408         ceilf64(flt) -> f64 => ceil,
409         truncf32(flt) -> f32 => truncf,
410         truncf64(flt) -> f64 => trunc,
411         roundf32(flt) -> f32 => roundf,
412         roundf64(flt) -> f64 => round,
413
414         // trigonometry
415         sinf32(flt) -> f32 => sinf,
416         sinf64(flt) -> f64 => sin,
417         cosf32(flt) -> f32 => cosf,
418         cosf64(flt) -> f64 => cos,
419         tanf32(flt) -> f32 => tanf,
420         tanf64(flt) -> f64 => tan,
421     }
422
423     intrinsic_match! {
424         fx, intrinsic, substs, args,
425         _ => {
426             unimpl!("unsupported intrinsic {}", intrinsic)
427         };
428
429         assume, (c _a) {};
430         likely | unlikely, (c a) {
431             ret.write_cvalue(fx, a);
432         };
433         breakpoint, () {
434             fx.bcx.ins().debugtrap();
435         };
436         copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
437             let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
438             let elem_size = fx
439                 .bcx
440                 .ins()
441                 .iconst(fx.pointer_type, elem_size as i64);
442             assert_eq!(args.len(), 3);
443             let byte_amount = fx.bcx.ins().imul(count, elem_size);
444
445             if intrinsic.ends_with("_nonoverlapping") {
446                 fx.bcx.call_memcpy(fx.module.target_config(), dst, src, byte_amount);
447             } else {
448                 fx.bcx.call_memmove(fx.module.target_config(), dst, src, byte_amount);
449             }
450         };
451         discriminant_value, (c ptr) {
452             let pointee_layout = fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
453             let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), pointee_layout);
454             let discr = crate::discriminant::codegen_get_discriminant(fx, val, ret.layout());
455             ret.write_cvalue(fx, discr);
456         };
457         size_of_val, <T> (c ptr) {
458             let layout = fx.layout_of(T);
459             let size = if layout.is_unsized() {
460                 let (_ptr, info) = ptr.load_scalar_pair(fx);
461                 let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
462                 size
463             } else {
464                 fx
465                     .bcx
466                     .ins()
467                     .iconst(fx.pointer_type, layout.size.bytes() as i64)
468             };
469             ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
470         };
471         min_align_of_val, <T> (c ptr) {
472             let layout = fx.layout_of(T);
473             let align = if layout.is_unsized() {
474                 let (_ptr, info) = ptr.load_scalar_pair(fx);
475                 let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
476                 align
477             } else {
478                 fx
479                     .bcx
480                     .ins()
481                     .iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
482             };
483             ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
484         };
485
486         _ if intrinsic.starts_with("unchecked_") || intrinsic == "exact_div", (c x, c y) {
487             // FIXME trap on overflow
488             let bin_op = match intrinsic {
489                 "unchecked_sub" => BinOp::Sub,
490                 "unchecked_div" | "exact_div" => BinOp::Div,
491                 "unchecked_rem" => BinOp::Rem,
492                 "unchecked_shl" => BinOp::Shl,
493                 "unchecked_shr" => BinOp::Shr,
494                 _ => unimplemented!("intrinsic {}", intrinsic),
495             };
496             let res = crate::num::trans_int_binop(fx, bin_op, x, y);
497             ret.write_cvalue(fx, res);
498         };
499         _ if intrinsic.ends_with("_with_overflow"), (c x, c y) {
500             assert_eq!(x.layout().ty, y.layout().ty);
501             let bin_op = match intrinsic {
502                 "add_with_overflow" => BinOp::Add,
503                 "sub_with_overflow" => BinOp::Sub,
504                 "mul_with_overflow" => BinOp::Mul,
505                 _ => unimplemented!("intrinsic {}", intrinsic),
506             };
507
508             let res = crate::num::trans_checked_int_binop(
509                 fx,
510                 bin_op,
511                 x,
512                 y,
513             );
514             ret.write_cvalue(fx, res);
515         };
516         _ if intrinsic.starts_with("wrapping_"), (c x, c y) {
517             assert_eq!(x.layout().ty, y.layout().ty);
518             let bin_op = match intrinsic {
519                 "wrapping_add" => BinOp::Add,
520                 "wrapping_sub" => BinOp::Sub,
521                 "wrapping_mul" => BinOp::Mul,
522                 _ => unimplemented!("intrinsic {}", intrinsic),
523             };
524             let res = crate::num::trans_int_binop(
525                 fx,
526                 bin_op,
527                 x,
528                 y,
529             );
530             ret.write_cvalue(fx, res);
531         };
532         _ if intrinsic.starts_with("saturating_"), <T> (c lhs, c rhs) {
533             assert_eq!(lhs.layout().ty, rhs.layout().ty);
534             let bin_op = match intrinsic {
535                 "saturating_add" => BinOp::Add,
536                 "saturating_sub" => BinOp::Sub,
537                 _ => unimplemented!("intrinsic {}", intrinsic),
538             };
539
540             let signed = type_sign(T);
541
542             let checked_res = crate::num::trans_checked_int_binop(
543                 fx,
544                 bin_op,
545                 lhs,
546                 rhs,
547             );
548
549             let (val, has_overflow) = checked_res.load_scalar_pair(fx);
550             let clif_ty = fx.clif_type(T).unwrap();
551
552             // `select.i8` is not implemented by Cranelift.
553             let has_overflow = fx.bcx.ins().uextend(types::I32, has_overflow);
554
555             let (min, max) = type_min_max_value(clif_ty, signed);
556             let min = fx.bcx.ins().iconst(clif_ty, min);
557             let max = fx.bcx.ins().iconst(clif_ty, max);
558
559             let val = match (intrinsic, signed) {
560                 ("saturating_add", false) => fx.bcx.ins().select(has_overflow, max, val),
561                 ("saturating_sub", false) => fx.bcx.ins().select(has_overflow, min, val),
562                 ("saturating_add", true) => {
563                     let rhs = rhs.load_scalar(fx);
564                     let rhs_ge_zero = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
565                     let sat_val = fx.bcx.ins().select(rhs_ge_zero, max, min);
566                     fx.bcx.ins().select(has_overflow, sat_val, val)
567                 }
568                 ("saturating_sub", true) => {
569                     let rhs = rhs.load_scalar(fx);
570                     let rhs_ge_zero = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
571                     let sat_val = fx.bcx.ins().select(rhs_ge_zero, min, max);
572                     fx.bcx.ins().select(has_overflow, sat_val, val)
573                 }
574                 _ => unreachable!(),
575             };
576
577             let res = CValue::by_val(val, fx.layout_of(T));
578
579             ret.write_cvalue(fx, res);
580         };
581         rotate_left, <T>(v x, v y) {
582             let layout = fx.layout_of(T);
583             let res = fx.bcx.ins().rotl(x, y);
584             ret.write_cvalue(fx, CValue::by_val(res, layout));
585         };
586         rotate_right, <T>(v x, v y) {
587             let layout = fx.layout_of(T);
588             let res = fx.bcx.ins().rotr(x, y);
589             ret.write_cvalue(fx, CValue::by_val(res, layout));
590         };
591
592         // The only difference between offset and arith_offset is regarding UB. Because Cranelift
593         // doesn't have UB both are codegen'ed the same way
594         offset | arith_offset, (c base, v offset) {
595             let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
596             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
597             let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
598             let base_val = base.load_scalar(fx);
599             let res = fx.bcx.ins().iadd(base_val, ptr_diff);
600             ret.write_cvalue(fx, CValue::by_val(res, base.layout()));
601         };
602
603         transmute, <src_ty, dst_ty> (c from) {
604             assert_eq!(from.layout().ty, src_ty);
605             let addr = from.force_stack(fx);
606             let dst_layout = fx.layout_of(dst_ty);
607             ret.write_cvalue(fx, CValue::by_ref(addr, dst_layout))
608         };
609         init, () {
610             let layout = ret.layout();
611             if layout.abi == Abi::Uninhabited {
612                 crate::trap::trap_panic(fx, "[panic] Called intrinsic::init for uninhabited type.");
613                 return;
614             }
615
616             match *ret.inner() {
617                 CPlaceInner::NoPlace => {}
618                 CPlaceInner::Var(var) => {
619                     let clif_ty = fx.clif_type(layout.ty).unwrap();
620                     let val = match clif_ty {
621                         types::I8 | types::I16 | types::I32 | types::I64 => fx.bcx.ins().iconst(clif_ty, 0),
622                         types::I128 => {
623                             let zero = fx.bcx.ins().iconst(types::I64, 0);
624                             fx.bcx.ins().iconcat(zero, zero)
625                         }
626                         types::F32 => {
627                             let zero = fx.bcx.ins().iconst(types::I32, 0);
628                             fx.bcx.ins().bitcast(types::F32, zero)
629                         }
630                         types::F64 => {
631                             let zero = fx.bcx.ins().iconst(types::I64, 0);
632                             fx.bcx.ins().bitcast(types::F64, zero)
633                         }
634                         _ => panic!("clif_type returned {}", clif_ty),
635                     };
636                     fx.bcx.set_val_label(val, cranelift::codegen::ir::ValueLabel::from_u32(var.as_u32()));
637                     fx.bcx.def_var(mir_var(var), val);
638                 }
639                 _ => {
640                     let addr = ret.to_ptr(fx).get_addr(fx);
641                     let layout = ret.layout();
642                     fx.bcx.emit_small_memset(fx.module.target_config(), addr, 0, layout.size.bytes(), 1);
643                 }
644             }
645         };
646         uninit, () {
647             let layout = ret.layout();
648             if layout.abi == Abi::Uninhabited {
649                 crate::trap::trap_panic(fx, "[panic] Called intrinsic::uninit for uninhabited type.");
650                 return;
651             }
652             match *ret.inner() {
653                 CPlaceInner::NoPlace => {},
654                 CPlaceInner::Var(var) => {
655                     let clif_ty = fx.clif_type(layout.ty).unwrap();
656                     let val = match clif_ty {
657                         types::I8 | types::I16 | types::I32 | types::I64 => fx.bcx.ins().iconst(clif_ty, 42),
658                         types::I128 => {
659                             let zero = fx.bcx.ins().iconst(types::I64, 0);
660                             let fourty_two = fx.bcx.ins().iconst(types::I64, 42);
661                             fx.bcx.ins().iconcat(fourty_two, zero)
662                         }
663                         types::F32 => {
664                             let zero = fx.bcx.ins().iconst(types::I32, 0xdeadbeef);
665                             fx.bcx.ins().bitcast(types::F32, zero)
666                         }
667                         types::F64 => {
668                             let zero = fx.bcx.ins().iconst(types::I64, 0xcafebabedeadbeefu64 as i64);
669                             fx.bcx.ins().bitcast(types::F64, zero)
670                         }
671                         _ => panic!("clif_type returned {}", clif_ty),
672                     };
673                     fx.bcx.set_val_label(val, cranelift::codegen::ir::ValueLabel::from_u32(var.as_u32()));
674                     fx.bcx.def_var(mir_var(var), val);
675                 }
676                 CPlaceInner::Addr(_, _) => {
677                     // Don't write to `ret`, as the destination memory is already uninitialized.
678                 }
679             }
680         };
681         write_bytes, (c dst, v val, v count) {
682             let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
683             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
684             let count = fx.bcx.ins().imul_imm(count, pointee_size as i64);
685             let dst_ptr = dst.load_scalar(fx);
686             fx.bcx.call_memset(fx.module.target_config(), dst_ptr, val, count);
687         };
688         ctlz | ctlz_nonzero, <T> (v arg) {
689             // FIXME trap on `ctlz_nonzero` with zero arg.
690             let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
691                 // FIXME verify this algorithm is correct
692                 let (lsb, msb) = fx.bcx.ins().isplit(arg);
693                 let lsb_lz = fx.bcx.ins().clz(lsb);
694                 let msb_lz = fx.bcx.ins().clz(msb);
695                 let msb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, msb, 0);
696                 let lsb_lz_plus_64 = fx.bcx.ins().iadd_imm(lsb_lz, 64);
697                 let res = fx.bcx.ins().select(msb_is_zero, lsb_lz_plus_64, msb_lz);
698                 fx.bcx.ins().uextend(types::I128, res)
699             } else {
700                 fx.bcx.ins().clz(arg)
701             };
702             let res = CValue::by_val(res, fx.layout_of(T));
703             ret.write_cvalue(fx, res);
704         };
705         cttz | cttz_nonzero, <T> (v arg) {
706             // FIXME trap on `cttz_nonzero` with zero arg.
707             let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
708                 // FIXME verify this algorithm is correct
709                 let (lsb, msb) = fx.bcx.ins().isplit(arg);
710                 let lsb_tz = fx.bcx.ins().ctz(lsb);
711                 let msb_tz = fx.bcx.ins().ctz(msb);
712                 let lsb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, lsb, 0);
713                 let msb_tz_plus_64 = fx.bcx.ins().iadd_imm(msb_tz, 64);
714                 let res = fx.bcx.ins().select(lsb_is_zero, msb_tz_plus_64, lsb_tz);
715                 fx.bcx.ins().uextend(types::I128, res)
716             } else {
717                 fx.bcx.ins().ctz(arg)
718             };
719             let res = CValue::by_val(res, fx.layout_of(T));
720             ret.write_cvalue(fx, res);
721         };
722         ctpop, <T> (v arg) {
723             let res = fx.bcx.ins().popcnt(arg);
724             let res = CValue::by_val(res, fx.layout_of(T));
725             ret.write_cvalue(fx, res);
726         };
727         bitreverse, <T> (v arg) {
728             let res = fx.bcx.ins().bitrev(arg);
729             let res = CValue::by_val(res, fx.layout_of(T));
730             ret.write_cvalue(fx, res);
731         };
732         bswap, <T> (v arg) {
733             // FIXME(CraneStation/cranelift#794) add bswap instruction to cranelift
734             fn swap(bcx: &mut FunctionBuilder, v: Value) -> Value {
735                 match bcx.func.dfg.value_type(v) {
736                     types::I8 => v,
737
738                     // https://code.woboq.org/gcc/include/bits/byteswap.h.html
739                     types::I16 => {
740                         let tmp1 = bcx.ins().ishl_imm(v, 8);
741                         let n1 = bcx.ins().band_imm(tmp1, 0xFF00);
742
743                         let tmp2 = bcx.ins().ushr_imm(v, 8);
744                         let n2 = bcx.ins().band_imm(tmp2, 0x00FF);
745
746                         bcx.ins().bor(n1, n2)
747                     }
748                     types::I32 => {
749                         let tmp1 = bcx.ins().ishl_imm(v, 24);
750                         let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000);
751
752                         let tmp2 = bcx.ins().ishl_imm(v, 8);
753                         let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000);
754
755                         let tmp3 = bcx.ins().ushr_imm(v, 8);
756                         let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00);
757
758                         let tmp4 = bcx.ins().ushr_imm(v, 24);
759                         let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF);
760
761                         let or_tmp1 = bcx.ins().bor(n1, n2);
762                         let or_tmp2 = bcx.ins().bor(n3, n4);
763                         bcx.ins().bor(or_tmp1, or_tmp2)
764                     }
765                     types::I64 => {
766                         let tmp1 = bcx.ins().ishl_imm(v, 56);
767                         let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000_0000_0000u64 as i64);
768
769                         let tmp2 = bcx.ins().ishl_imm(v, 40);
770                         let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000_0000_0000u64 as i64);
771
772                         let tmp3 = bcx.ins().ishl_imm(v, 24);
773                         let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00_0000_0000u64 as i64);
774
775                         let tmp4 = bcx.ins().ishl_imm(v, 8);
776                         let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF_0000_0000u64 as i64);
777
778                         let tmp5 = bcx.ins().ushr_imm(v, 8);
779                         let n5 = bcx.ins().band_imm(tmp5, 0x0000_0000_FF00_0000u64 as i64);
780
781                         let tmp6 = bcx.ins().ushr_imm(v, 24);
782                         let n6 = bcx.ins().band_imm(tmp6, 0x0000_0000_00FF_0000u64 as i64);
783
784                         let tmp7 = bcx.ins().ushr_imm(v, 40);
785                         let n7 = bcx.ins().band_imm(tmp7, 0x0000_0000_0000_FF00u64 as i64);
786
787                         let tmp8 = bcx.ins().ushr_imm(v, 56);
788                         let n8 = bcx.ins().band_imm(tmp8, 0x0000_0000_0000_00FFu64 as i64);
789
790                         let or_tmp1 = bcx.ins().bor(n1, n2);
791                         let or_tmp2 = bcx.ins().bor(n3, n4);
792                         let or_tmp3 = bcx.ins().bor(n5, n6);
793                         let or_tmp4 = bcx.ins().bor(n7, n8);
794
795                         let or_tmp5 = bcx.ins().bor(or_tmp1, or_tmp2);
796                         let or_tmp6 = bcx.ins().bor(or_tmp3, or_tmp4);
797                         bcx.ins().bor(or_tmp5, or_tmp6)
798                     }
799                     types::I128 => {
800                         let (lo, hi) = bcx.ins().isplit(v);
801                         let lo = swap(bcx, lo);
802                         let hi = swap(bcx, hi);
803                         bcx.ins().iconcat(hi, lo)
804                     }
805                     ty => unimplemented!("bswap {}", ty),
806                 }
807             };
808             let res = CValue::by_val(swap(&mut fx.bcx, arg), fx.layout_of(T));
809             ret.write_cvalue(fx, res);
810         };
811         panic_if_uninhabited, <T> () {
812             if fx.layout_of(T).abi.is_uninhabited() {
813                 crate::trap::trap_panic(fx, "[panic] Called intrinsic::panic_if_uninhabited for uninhabited type.");
814                 return;
815             }
816         };
817
818         volatile_load, (c ptr) {
819             // Cranelift treats loads as volatile by default
820             let inner_layout =
821                 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
822             let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
823             ret.write_cvalue(fx, val);
824         };
825         volatile_store, (v ptr, c val) {
826             // Cranelift treats stores as volatile by default
827             let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
828             dest.write_cvalue(fx, val);
829         };
830
831         size_of | pref_align_of | min_align_of | needs_drop | type_id | type_name, () {
832             let const_val =
833                 fx.tcx.const_eval_instance(ParamEnv::reveal_all(), instance, None).unwrap();
834             let val = crate::constant::trans_const_value(fx, const_val);
835             ret.write_cvalue(fx, val);
836         };
837
838         ptr_offset_from, <T> (v ptr, v base) {
839             let isize_layout = fx.layout_of(fx.tcx.types.isize);
840
841             let pointee_size: u64 = fx.layout_of(T).size.bytes();
842             let diff = fx.bcx.ins().isub(ptr, base);
843             // FIXME this can be an exact division.
844             let val = CValue::by_val(fx.bcx.ins().udiv_imm(diff, pointee_size as i64), isize_layout);
845             ret.write_cvalue(fx, val);
846         };
847
848         caller_location, () {
849             let caller_location = fx.get_caller_location(span);
850             ret.write_cvalue(fx, caller_location);
851         };
852
853         _ if intrinsic.starts_with("atomic_fence"), () {};
854         _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
855         _ if intrinsic.starts_with("atomic_load"), (c ptr) {
856             let inner_layout =
857                 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
858             let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
859             ret.write_cvalue(fx, val);
860         };
861         _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
862             let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
863             dest.write_cvalue(fx, val);
864         };
865         _ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
866             // Read old
867             let clif_ty = fx.clif_type(T).unwrap();
868             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
869             ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
870
871             // Write new
872             let dest = CPlace::for_ptr(Pointer::new(ptr), src.layout());
873             dest.write_cvalue(fx, src);
874         };
875         _ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*
876             // Read old
877             let clif_ty = fx.clif_type(T).unwrap();
878             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
879
880             // Compare
881             let is_eq = codegen_icmp(fx, IntCC::Equal, old, test_old);
882             let new = fx.bcx.ins().select(is_eq, new, old); // Keep old if not equal to test_old
883
884             // Write new
885             fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
886
887             let ret_val = CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
888             ret.write_cvalue(fx, ret_val);
889         };
890
891         _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
892             atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
893         };
894         _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
895             atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
896         };
897         _ if intrinsic.starts_with("atomic_and"), <T> (v ptr, v src) {
898             atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
899         };
900         _ if intrinsic.starts_with("atomic_nand"), <T> (v ptr, v src) {
901             let clif_ty = fx.clif_type(T).unwrap();
902             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
903             let and = fx.bcx.ins().band(old, src);
904             let new = fx.bcx.ins().bnot(and);
905             fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
906             ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
907         };
908         _ if intrinsic.starts_with("atomic_or"), <T> (v ptr, v src) {
909             atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
910         };
911         _ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, v src) {
912             atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
913         };
914
915         _ if intrinsic.starts_with("atomic_max"), <T> (v ptr, v src) {
916             atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
917         };
918         _ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, v src) {
919             atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
920         };
921         _ if intrinsic.starts_with("atomic_min"), <T> (v ptr, v src) {
922             atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
923         };
924         _ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, v src) {
925             atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
926         };
927
928         minnumf32, (v a, v b) {
929             let val = fx.bcx.ins().fmin(a, b);
930             let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
931             ret.write_cvalue(fx, val);
932         };
933         minnumf64, (v a, v b) {
934             let val = fx.bcx.ins().fmin(a, b);
935             let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
936             ret.write_cvalue(fx, val);
937         };
938         maxnumf32, (v a, v b) {
939             let val = fx.bcx.ins().fmax(a, b);
940             let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
941             ret.write_cvalue(fx, val);
942         };
943         maxnumf64, (v a, v b) {
944             let val = fx.bcx.ins().fmax(a, b);
945             let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
946             ret.write_cvalue(fx, val);
947         };
948
949         try, (v f, v data, v _local_ptr) {
950             // FIXME once unwinding is supported, change this to actually catch panics
951             let f_sig = fx.bcx.func.import_signature(Signature {
952                 call_conv: CallConv::triple_default(fx.triple()),
953                 params: vec![AbiParam::new(fx.bcx.func.dfg.value_type(data))],
954                 returns: vec![],
955             });
956
957             fx.bcx.ins().call_indirect(f_sig, f, &[data]);
958
959             let ret_val = CValue::const_val(fx, ret.layout().ty, 0);
960             ret.write_cvalue(fx, ret_val);
961         };
962     }
963
964     if let Some((_, dest)) = destination {
965         let ret_ebb = fx.get_ebb(dest);
966         fx.bcx.ins().jump(ret_ebb, &[]);
967     } else {
968         trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
969     }
970 }