]> git.lizzy.rs Git - rust.git/blob - src/intrinsics.rs
Emulate some simd intrinsics
[rust.git] / src / intrinsics.rs
1 use crate::prelude::*;
2
3 use rustc::ty::subst::SubstsRef;
4
5 macro_rules! intrinsic_pat {
6     (_) => {
7         _
8     };
9     ($name:ident) => {
10         stringify!($name)
11     }
12 }
13
14 macro_rules! intrinsic_arg {
15     (c $fx:expr, $arg:ident) => {
16         $arg
17     };
18     (v $fx:expr, $arg:ident) => {
19         $arg.load_scalar($fx)
20     };
21 }
22
23 macro_rules! intrinsic_substs {
24     ($substs:expr, $index:expr,) => {};
25     ($substs:expr, $index:expr, $first:ident $(,$rest:ident)*) => {
26         let $first = $substs.type_at($index);
27         intrinsic_substs!($substs, $index+1, $($rest),*);
28     };
29 }
30
31 macro_rules! intrinsic_match {
32     ($fx:expr, $intrinsic:expr, $substs:expr, $args:expr, $(
33         $($name:tt)|+ $(if $cond:expr)?, $(<$($subst:ident),*>)? ($($a:ident $arg:ident),*) $content:block;
34     )*) => {
35         match $intrinsic {
36             $(
37                 $(intrinsic_pat!($name))|* $(if $cond)? => {
38                     #[allow(unused_parens, non_snake_case)]
39                     {
40                         $(
41                             intrinsic_substs!($substs, 0, $($subst),*);
42                         )?
43                         if let [$($arg),*] = *$args {
44                             let ($($arg),*) = (
45                                 $(intrinsic_arg!($a $fx, $arg)),*
46                             );
47                             #[warn(unused_parens, non_snake_case)]
48                             {
49                                 $content
50                             }
51                         } else {
52                             bug!("wrong number of args for intrinsic {:?}", $intrinsic);
53                         }
54                     }
55                 }
56             )*
57             _ => unimpl!("unsupported intrinsic {}", $intrinsic),
58         }
59     };
60 }
61
62 macro_rules! call_intrinsic_match {
63     ($fx:expr, $intrinsic:expr, $substs:expr, $ret:expr, $destination:expr, $args:expr, $(
64         $name:ident($($arg:ident),*) -> $ty:ident => $func:ident,
65     )*) => {
66         match $intrinsic {
67             $(
68                 stringify!($name) => {
69                     assert!($substs.is_noop());
70                     if let [$($arg),*] = *$args {
71                         let res = $fx.easy_call(stringify!($func), &[$($arg),*], $fx.tcx.types.$ty);
72                         $ret.write_cvalue($fx, res);
73
74                         if let Some((_, dest)) = $destination {
75                             let ret_ebb = $fx.get_ebb(dest);
76                             $fx.bcx.ins().jump(ret_ebb, &[]);
77                             return;
78                         } else {
79                             unreachable!();
80                         }
81                     } else {
82                         bug!("wrong number of args for intrinsic {:?}", $intrinsic);
83                     }
84                 }
85             )*
86             _ => {}
87         }
88     }
89 }
90
91 macro_rules! atomic_binop_return_old {
92     ($fx:expr, $op:ident<$T:ident>($ptr:ident, $src:ident) -> $ret:ident) => {
93         let clif_ty = $fx.clif_type($T).unwrap();
94         let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
95         let new = $fx.bcx.ins().$op(old, $src);
96         $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
97         $ret.write_cvalue($fx, CValue::by_val(old, $fx.layout_of($T)));
98     };
99 }
100
101 macro_rules! atomic_minmax {
102     ($fx:expr, $cc:expr, <$T:ident> ($ptr:ident, $src:ident) -> $ret:ident) => {
103         // Read old
104         let clif_ty = $fx.clif_type($T).unwrap();
105         let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
106
107         // Compare
108         let is_eq = $fx.bcx.ins().icmp(IntCC::SignedGreaterThan, old, $src);
109         let new = crate::common::codegen_select(&mut $fx.bcx, is_eq, old, $src);
110
111         // Write new
112         $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
113
114         let ret_val = CValue::by_val(old, $ret.layout());
115         $ret.write_cvalue($fx, ret_val);
116     };
117 }
118
119 fn lane_type_and_count<'tcx>(
120     fx: &FunctionCx<'_, 'tcx, impl Backend>,
121     layout: TyLayout<'tcx>,
122     intrinsic: &str,
123 ) -> (TyLayout<'tcx>, usize) {
124     let lane_count = match layout.fields {
125         layout::FieldPlacement::Array { stride: _, count } => usize::try_from(count).unwrap(),
126         _ => panic!("Non vector type {:?} passed to or returned from simd_* intrinsic {}", layout.ty, intrinsic),
127     };
128     let lane_layout = layout.field(fx, 0);
129     (lane_layout, lane_count)
130 }
131
132 fn simd_for_each_lane<'tcx, B: Backend>(
133     fx: &mut FunctionCx<'_, 'tcx, B>,
134     intrinsic: &str,
135     x: CValue<'tcx>,
136     y: CValue<'tcx>,
137     ret: CPlace<'tcx>,
138     f: impl Fn(&mut FunctionCx<'_, 'tcx, B>, TyLayout<'tcx>, TyLayout<'tcx>, Value, Value) -> CValue<'tcx>,
139 ) {
140     assert_eq!(x.layout(), y.layout());
141     let layout = x.layout();
142
143     let (lane_layout, lane_count) = lane_type_and_count(fx, layout, intrinsic);
144     let (ret_lane_layout, ret_lane_count) = lane_type_and_count(fx, ret.layout(), intrinsic);
145     assert_eq!(lane_count, ret_lane_count);
146
147     for lane in 0..lane_count {
148         let lane = mir::Field::new(lane);
149         let x_lane = x.value_field(fx, lane).load_scalar(fx);
150         let y_lane = y.value_field(fx, lane).load_scalar(fx);
151
152         let res_lane = f(fx, lane_layout, ret_lane_layout, x_lane, y_lane);
153
154         ret.place_field(fx, lane).write_cvalue(fx, res_lane);
155     }
156 }
157
158 macro_rules! simd_binop {
159     ($fx:expr, $intrinsic:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) => {
160         simd_for_each_lane($fx, $intrinsic, $x, $y, $ret, |fx, _lane_layout, ret_lane_layout, x_lane, y_lane| {
161             let res_lane = fx.bcx.ins().$op(x_lane, y_lane);
162             CValue::by_val(res_lane, ret_lane_layout)
163         });
164     };
165     ($fx:expr, $intrinsic:expr, $op_u:ident|$op_s:ident($x:ident, $y:ident) -> $ret:ident) => {
166         simd_for_each_lane($fx, $intrinsic, $x, $y, $ret, |fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
167             let res_lane = match lane_layout.ty.sty {
168                 ty::Uint(_) => fx.bcx.ins().$op_u(x_lane, y_lane),
169                 ty::Int(_) => fx.bcx.ins().$op_s(x_lane, y_lane),
170                 _ => unreachable!("{:?}", lane_layout.ty),
171             };
172             CValue::by_val(res_lane, ret_lane_layout)
173         });
174     };
175 }
176
177 pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
178     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
179     def_id: DefId,
180     substs: SubstsRef<'tcx>,
181     args: Vec<CValue<'tcx>>,
182     destination: Option<(CPlace<'tcx>, BasicBlock)>,
183 ) {
184     let intrinsic = fx.tcx.item_name(def_id).as_str();
185     let intrinsic = &intrinsic[..];
186
187     let ret = match destination {
188         Some((place, _)) => place,
189         None => {
190             // Insert non returning intrinsics here
191             match intrinsic {
192                 "abort" => {
193                     trap_panic(fx, "Called intrinsic::abort.");
194                 }
195                 "unreachable" => {
196                     trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
197                 }
198                 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
199             }
200             return;
201         }
202     };
203
204     let u64_layout = fx.layout_of(fx.tcx.types.u64);
205     let usize_layout = fx.layout_of(fx.tcx.types.usize);
206
207     call_intrinsic_match! {
208         fx, intrinsic, substs, ret, destination, args,
209         expf32(flt) -> f32 => expf,
210         expf64(flt) -> f64 => exp,
211         exp2f32(flt) -> f32 => exp2f,
212         exp2f64(flt) -> f64 => exp2,
213         sqrtf32(flt) -> f32 => sqrtf,
214         sqrtf64(flt) -> f64 => sqrt,
215         powif32(a, x) -> f32 => __powisf2, // compiler-builtins
216         powif64(a, x) -> f64 => __powidf2, // compiler-builtins
217         logf32(flt) -> f32 => logf,
218         logf64(flt) -> f64 => log,
219         fabsf32(flt) -> f32 => fabsf,
220         fabsf64(flt) -> f64 => fabs,
221         fmaf32(x, y, z) -> f32 => fmaf,
222         fmaf64(x, y, z) -> f64 => fma,
223
224         // rounding variants
225         floorf32(flt) -> f32 => floorf,
226         floorf64(flt) -> f64 => floor,
227         ceilf32(flt) -> f32 => ceilf,
228         ceilf64(flt) -> f64 => ceil,
229         truncf32(flt) -> f32 => truncf,
230         truncf64(flt) -> f64 => trunc,
231         roundf32(flt) -> f32 => roundf,
232         roundf64(flt) -> f64 => round,
233
234         // trigonometry
235         sinf32(flt) -> f32 => sinf,
236         sinf64(flt) -> f64 => sin,
237         cosf32(flt) -> f32 => cosf,
238         cosf64(flt) -> f64 => cos,
239         tanf32(flt) -> f32 => tanf,
240         tanf64(flt) -> f64 => tan,
241     }
242
243     intrinsic_match! {
244         fx, intrinsic, substs, args,
245
246         assume, (c _a) {};
247         likely | unlikely, (c a) {
248             ret.write_cvalue(fx, a);
249         };
250         breakpoint, () {
251             fx.bcx.ins().debugtrap();
252         };
253         copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
254             let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
255             let elem_size = fx
256                 .bcx
257                 .ins()
258                 .iconst(fx.pointer_type, elem_size as i64);
259             assert_eq!(args.len(), 3);
260             let byte_amount = fx.bcx.ins().imul(count, elem_size);
261
262             if intrinsic.ends_with("_nonoverlapping") {
263                 fx.bcx.call_memcpy(fx.module.target_config(), dst, src, byte_amount);
264             } else {
265                 fx.bcx.call_memmove(fx.module.target_config(), dst, src, byte_amount);
266             }
267         };
268         discriminant_value, (c val) {
269             let pointee_layout = fx.layout_of(val.layout().ty.builtin_deref(true).unwrap().ty);
270             let place = CPlace::for_addr(val.load_scalar(fx), pointee_layout);
271             let discr = crate::base::trans_get_discriminant(fx, place, ret.layout());
272             ret.write_cvalue(fx, discr);
273         };
274         size_of, <T> () {
275             let size_of = fx.layout_of(T).size.bytes();
276             let size_of = CValue::const_val(fx, usize_layout.ty, size_of.into());
277             ret.write_cvalue(fx, size_of);
278         };
279         size_of_val, <T> (c ptr) {
280             let layout = fx.layout_of(T);
281             let size = if layout.is_unsized() {
282                 let (_ptr, info) = ptr.load_scalar_pair(fx);
283                 let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
284                 size
285             } else {
286                 fx
287                     .bcx
288                     .ins()
289                     .iconst(fx.pointer_type, layout.size.bytes() as i64)
290             };
291             ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
292         };
293         min_align_of, <T> () {
294             let min_align = fx.layout_of(T).align.abi.bytes();
295             let min_align = CValue::const_val(fx, usize_layout.ty, min_align.into());
296             ret.write_cvalue(fx, min_align);
297         };
298         min_align_of_val, <T> (c ptr) {
299             let layout = fx.layout_of(T);
300             let align = if layout.is_unsized() {
301                 let (_ptr, info) = ptr.load_scalar_pair(fx);
302                 let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
303                 align
304             } else {
305                 fx
306                     .bcx
307                     .ins()
308                     .iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
309             };
310             ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
311         };
312         pref_align_of, <T> () {
313             let pref_align = fx.layout_of(T).align.pref.bytes();
314             let pref_align = CValue::const_val(fx, usize_layout.ty, pref_align.into());
315             ret.write_cvalue(fx, pref_align);
316         };
317
318
319         type_id, <T> () {
320             let type_id = fx.tcx.type_id_hash(T);
321             let type_id = CValue::const_val(fx, u64_layout.ty, type_id.into());
322             ret.write_cvalue(fx, type_id);
323         };
324         type_name, <T> () {
325             let type_name = fx.tcx.type_name(T);
326             let type_name = crate::constant::trans_const_value(fx, type_name);
327             ret.write_cvalue(fx, type_name);
328         };
329
330         _ if intrinsic.starts_with("unchecked_") || intrinsic == "exact_div", (c x, c y) {
331             // FIXME trap on overflow
332             let bin_op = match intrinsic {
333                 "unchecked_sub" => BinOp::Sub,
334                 "unchecked_div" | "exact_div" => BinOp::Div,
335                 "unchecked_rem" => BinOp::Rem,
336                 "unchecked_shl" => BinOp::Shl,
337                 "unchecked_shr" => BinOp::Shr,
338                 _ => unimplemented!("intrinsic {}", intrinsic),
339             };
340             let res = match ret.layout().ty.sty {
341                 ty::Uint(_) => crate::base::trans_int_binop(
342                     fx,
343                     bin_op,
344                     x,
345                     y,
346                     ret.layout().ty,
347                     false,
348                 ),
349                 ty::Int(_) => crate::base::trans_int_binop(
350                     fx,
351                     bin_op,
352                     x,
353                     y,
354                     ret.layout().ty,
355                     true,
356                 ),
357                 _ => panic!(),
358             };
359             ret.write_cvalue(fx, res);
360         };
361         _ if intrinsic.ends_with("_with_overflow"), <T> (c x, c y) {
362             assert_eq!(x.layout().ty, y.layout().ty);
363             let bin_op = match intrinsic {
364                 "add_with_overflow" => BinOp::Add,
365                 "sub_with_overflow" => BinOp::Sub,
366                 "mul_with_overflow" => BinOp::Mul,
367                 _ => unimplemented!("intrinsic {}", intrinsic),
368             };
369             let res = match T.sty {
370                 ty::Uint(_) => crate::base::trans_checked_int_binop(
371                     fx,
372                     bin_op,
373                     x,
374                     y,
375                     ret.layout().ty,
376                     false,
377                 ),
378                 ty::Int(_) => crate::base::trans_checked_int_binop(
379                     fx,
380                     bin_op,
381                     x,
382                     y,
383                     ret.layout().ty,
384                     true,
385                 ),
386                 _ => panic!(),
387             };
388             ret.write_cvalue(fx, res);
389         };
390         _ if intrinsic.starts_with("overflowing_"), <T> (c x, c y) {
391             assert_eq!(x.layout().ty, y.layout().ty);
392             let bin_op = match intrinsic {
393                 "overflowing_add" => BinOp::Add,
394                 "overflowing_sub" => BinOp::Sub,
395                 "overflowing_mul" => BinOp::Mul,
396                 _ => unimplemented!("intrinsic {}", intrinsic),
397             };
398             let res = match T.sty {
399                 ty::Uint(_) => crate::base::trans_int_binop(
400                     fx,
401                     bin_op,
402                     x,
403                     y,
404                     ret.layout().ty,
405                     false,
406                 ),
407                 ty::Int(_) => crate::base::trans_int_binop(
408                     fx,
409                     bin_op,
410                     x,
411                     y,
412                     ret.layout().ty,
413                     true,
414                 ),
415                 _ => panic!(),
416             };
417             ret.write_cvalue(fx, res);
418         };
419         _ if intrinsic.starts_with("saturating_"), <T> (c x, c y) {
420             // FIXME implement saturating behavior
421             assert_eq!(x.layout().ty, y.layout().ty);
422             let bin_op = match intrinsic {
423                 "saturating_add" => BinOp::Add,
424                 "saturating_sub" => BinOp::Sub,
425                 "saturating_mul" => BinOp::Mul,
426                 _ => unimplemented!("intrinsic {}", intrinsic),
427             };
428             let res = match T.sty {
429                 ty::Uint(_) => crate::base::trans_int_binop(
430                     fx,
431                     bin_op,
432                     x,
433                     y,
434                     ret.layout().ty,
435                     false,
436                 ),
437                 ty::Int(_) => crate::base::trans_int_binop(
438                     fx,
439                     bin_op,
440                     x,
441                     y,
442                     ret.layout().ty,
443                     true,
444                 ),
445                 _ => panic!(),
446             };
447             ret.write_cvalue(fx, res);
448         };
449         rotate_left, <T>(v x, v y) {
450             let layout = fx.layout_of(T);
451             let res = fx.bcx.ins().rotl(x, y);
452             ret.write_cvalue(fx, CValue::by_val(res, layout));
453         };
454         rotate_right, <T>(v x, v y) {
455             let layout = fx.layout_of(T);
456             let res = fx.bcx.ins().rotr(x, y);
457             ret.write_cvalue(fx, CValue::by_val(res, layout));
458         };
459
460         // The only difference between offset and arith_offset is regarding UB. Because Cranelift
461         // doesn't have UB both are codegen'ed the same way
462         offset | arith_offset, (c base, v offset) {
463             let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
464             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
465             let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
466             let base_val = base.load_scalar(fx);
467             let res = fx.bcx.ins().iadd(base_val, ptr_diff);
468             ret.write_cvalue(fx, CValue::by_val(res, args[0].layout()));
469         };
470
471         transmute, <src_ty, dst_ty> (c from) {
472             assert_eq!(from.layout().ty, src_ty);
473             let addr = from.force_stack(fx);
474             let dst_layout = fx.layout_of(dst_ty);
475             ret.write_cvalue(fx, CValue::by_ref(addr, dst_layout))
476         };
477         init, () {
478             if ret.layout().abi == Abi::Uninhabited {
479                 crate::trap::trap_panic(fx, "[panic] Called intrinsic::init for uninhabited type.");
480                 return;
481             }
482
483             match ret {
484                 CPlace::NoPlace(_layout) => {}
485                 CPlace::Var(var, layout) => {
486                     let clif_ty = fx.clif_type(layout.ty).unwrap();
487                     let val = match clif_ty {
488                         types::I8 | types::I16 | types::I32 | types::I64 => fx.bcx.ins().iconst(clif_ty, 0),
489                         types::F32 => {
490                             let zero = fx.bcx.ins().iconst(types::I32, 0);
491                             fx.bcx.ins().bitcast(types::F32, zero)
492                         }
493                         types::F64 => {
494                             let zero = fx.bcx.ins().iconst(types::I64, 0);
495                             fx.bcx.ins().bitcast(types::F64, zero)
496                         }
497                         _ => panic!("clif_type returned {}", clif_ty),
498                     };
499                     fx.bcx.def_var(mir_var(var), val);
500                 }
501                 _ => {
502                     let addr = ret.to_addr(fx);
503                     let layout = ret.layout();
504                     fx.bcx.emit_small_memset(fx.module.target_config(), addr, 0, layout.size.bytes(), 1);
505                 }
506             }
507         };
508         write_bytes, (c dst, v val, v count) {
509             let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
510             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
511             let count = fx.bcx.ins().imul_imm(count, pointee_size as i64);
512             let dst_ptr = dst.load_scalar(fx);
513             fx.bcx.call_memset(fx.module.target_config(), dst_ptr, val, count);
514         };
515         ctlz | ctlz_nonzero, <T> (v arg) {
516             let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
517                 // FIXME verify this algorithm is correct
518                 let (lsb, msb) = fx.bcx.ins().isplit(arg);
519                 let lsb_lz = fx.bcx.ins().clz(lsb);
520                 let msb_lz = fx.bcx.ins().clz(msb);
521                 let msb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, msb, 0);
522                 let lsb_lz_plus_64 = fx.bcx.ins().iadd_imm(lsb_lz, 64);
523                 fx.bcx.ins().select(msb_is_zero, lsb_lz_plus_64, msb_lz)
524             } else {
525                 fx.bcx.ins().clz(arg)
526             };
527             let res = CValue::by_val(res, fx.layout_of(T));
528             ret.write_cvalue(fx, res);
529         };
530         cttz | cttz_nonzero, <T> (v arg) {
531             let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
532                 // FIXME verify this algorithm is correct
533                 let (lsb, msb) = fx.bcx.ins().isplit(arg);
534                 let lsb_tz = fx.bcx.ins().ctz(lsb);
535                 let msb_tz = fx.bcx.ins().ctz(msb);
536                 let lsb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, lsb, 0);
537                 let msb_tz_plus_64 = fx.bcx.ins().iadd_imm(msb_tz, 64);
538                 fx.bcx.ins().select(lsb_is_zero, msb_tz_plus_64, lsb_tz)
539             } else {
540                 fx.bcx.ins().ctz(arg)
541             };
542             let res = CValue::by_val(res, fx.layout_of(T));
543             ret.write_cvalue(fx, res);
544         };
545         ctpop, <T> (v arg) {
546             let res = CValue::by_val(fx.bcx.ins().popcnt(arg), fx.layout_of(T));
547             ret.write_cvalue(fx, res);
548         };
549         bitreverse, <T> (v arg) {
550             let res = CValue::by_val(fx.bcx.ins().bitrev(arg), fx.layout_of(T));
551             ret.write_cvalue(fx, res);
552         };
553         bswap, <T> (v arg) {
554             // FIXME(CraneStation/cranelift#794) add bswap instruction to cranelift
555             fn swap(bcx: &mut FunctionBuilder, v: Value) -> Value {
556                 match bcx.func.dfg.value_type(v) {
557                     types::I8 => v,
558
559                     // https://code.woboq.org/gcc/include/bits/byteswap.h.html
560                     types::I16 => {
561                         let tmp1 = bcx.ins().ishl_imm(v, 8);
562                         let n1 = bcx.ins().band_imm(tmp1, 0xFF00);
563
564                         let tmp2 = bcx.ins().ushr_imm(v, 8);
565                         let n2 = bcx.ins().band_imm(tmp2, 0x00FF);
566
567                         bcx.ins().bor(n1, n2)
568                     }
569                     types::I32 => {
570                         let tmp1 = bcx.ins().ishl_imm(v, 24);
571                         let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000);
572
573                         let tmp2 = bcx.ins().ishl_imm(v, 8);
574                         let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000);
575
576                         let tmp3 = bcx.ins().ushr_imm(v, 8);
577                         let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00);
578
579                         let tmp4 = bcx.ins().ushr_imm(v, 24);
580                         let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF);
581
582                         let or_tmp1 = bcx.ins().bor(n1, n2);
583                         let or_tmp2 = bcx.ins().bor(n3, n4);
584                         bcx.ins().bor(or_tmp1, or_tmp2)
585                     }
586                     types::I64 => {
587                         let tmp1 = bcx.ins().ishl_imm(v, 56);
588                         let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000_0000_0000u64 as i64);
589
590                         let tmp2 = bcx.ins().ishl_imm(v, 40);
591                         let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000_0000_0000u64 as i64);
592
593                         let tmp3 = bcx.ins().ishl_imm(v, 24);
594                         let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00_0000_0000u64 as i64);
595
596                         let tmp4 = bcx.ins().ishl_imm(v, 8);
597                         let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF_0000_0000u64 as i64);
598
599                         let tmp5 = bcx.ins().ushr_imm(v, 8);
600                         let n5 = bcx.ins().band_imm(tmp5, 0x0000_0000_FF00_0000u64 as i64);
601
602                         let tmp6 = bcx.ins().ushr_imm(v, 24);
603                         let n6 = bcx.ins().band_imm(tmp6, 0x0000_0000_00FF_0000u64 as i64);
604
605                         let tmp7 = bcx.ins().ushr_imm(v, 40);
606                         let n7 = bcx.ins().band_imm(tmp7, 0x0000_0000_0000_FF00u64 as i64);
607
608                         let tmp8 = bcx.ins().ushr_imm(v, 56);
609                         let n8 = bcx.ins().band_imm(tmp8, 0x0000_0000_0000_00FFu64 as i64);
610
611                         let or_tmp1 = bcx.ins().bor(n1, n2);
612                         let or_tmp2 = bcx.ins().bor(n3, n4);
613                         let or_tmp3 = bcx.ins().bor(n5, n6);
614                         let or_tmp4 = bcx.ins().bor(n7, n8);
615
616                         let or_tmp5 = bcx.ins().bor(or_tmp1, or_tmp2);
617                         let or_tmp6 = bcx.ins().bor(or_tmp3, or_tmp4);
618                         bcx.ins().bor(or_tmp5, or_tmp6)
619                     }
620                     types::I128 => {
621                         let (lo, hi) = bcx.ins().isplit(v);
622                         let lo = swap(bcx, lo);
623                         let hi = swap(bcx, hi);
624                         bcx.ins().iconcat(hi, lo)
625                     }
626                     ty => unimplemented!("bswap {}", ty),
627                 }
628             };
629             let res = CValue::by_val(swap(&mut fx.bcx, arg), fx.layout_of(T));
630             ret.write_cvalue(fx, res);
631         };
632         needs_drop, <T> () {
633             let needs_drop = if T.needs_drop(fx.tcx, ParamEnv::reveal_all()) {
634                 1
635             } else {
636                 0
637             };
638             let needs_drop = CValue::const_val(fx, fx.tcx.types.bool, needs_drop);
639             ret.write_cvalue(fx, needs_drop);
640         };
641         panic_if_uninhabited, <T> () {
642             if fx.layout_of(T).abi.is_uninhabited() {
643                 crate::trap::trap_panic(fx, "[panic] Called intrinsic::panic_if_uninhabited for uninhabited type.");
644                 return;
645             }
646         };
647
648         volatile_load, (c ptr) {
649             // Cranelift treats loads as volatile by default
650             let inner_layout =
651                 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
652             let val = CValue::by_ref(ptr.load_scalar(fx), inner_layout);
653             ret.write_cvalue(fx, val);
654         };
655         volatile_store, (v ptr, c val) {
656             // Cranelift treats stores as volatile by default
657             let dest = CPlace::for_addr(ptr, val.layout());
658             dest.write_cvalue(fx, val);
659         };
660
661         _ if intrinsic.starts_with("atomic_fence"), () {};
662         _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
663         _ if intrinsic.starts_with("atomic_load"), (c ptr) {
664             let inner_layout =
665                 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
666             let val = CValue::by_ref(ptr.load_scalar(fx), inner_layout);
667             ret.write_cvalue(fx, val);
668         };
669         _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
670             let dest = CPlace::for_addr(ptr, val.layout());
671             dest.write_cvalue(fx, val);
672         };
673         _ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
674             // Read old
675             let clif_ty = fx.clif_type(T).unwrap();
676             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
677             ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
678
679             // Write new
680             let dest = CPlace::for_addr(ptr, src.layout());
681             dest.write_cvalue(fx, src);
682         };
683         _ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*
684             // Read old
685             let clif_ty = fx.clif_type(T).unwrap();
686             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
687
688             // Compare
689             let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old);
690             let new = crate::common::codegen_select(&mut fx.bcx, is_eq, new, old); // Keep old if not equal to test_old
691
692             // Write new
693             fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
694
695             let ret_val = CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
696             ret.write_cvalue(fx, ret_val);
697         };
698
699         _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
700             atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
701         };
702         _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
703             atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
704         };
705         _ if intrinsic.starts_with("atomic_and"), <T> (v ptr, v src) {
706             atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
707         };
708         _ if intrinsic.starts_with("atomic_nand"), <T> (v ptr, v src) {
709             atomic_binop_return_old! (fx, band_not<T>(ptr, src) -> ret);
710         };
711         _ if intrinsic.starts_with("atomic_or"), <T> (v ptr, v src) {
712             atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
713         };
714         _ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, v src) {
715             atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
716         };
717
718         _ if intrinsic.starts_with("atomic_max"), <T> (v ptr, v src) {
719             atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
720         };
721         _ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, v src) {
722             atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
723         };
724         _ if intrinsic.starts_with("atomic_min"), <T> (v ptr, v src) {
725             atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
726         };
727         _ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, v src) {
728             atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
729         };
730
731         minnumf32, (v a, v b) {
732             let val = fx.bcx.ins().fmin(a, b);
733             let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
734             ret.write_cvalue(fx, val);
735         };
736         minnumf64, (v a, v b) {
737             let val = fx.bcx.ins().fmin(a, b);
738             let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
739             ret.write_cvalue(fx, val);
740         };
741         maxnumf32, (v a, v b) {
742             let val = fx.bcx.ins().fmax(a, b);
743             let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
744             ret.write_cvalue(fx, val);
745         };
746         maxnumf64, (v a, v b) {
747             let val = fx.bcx.ins().fmax(a, b);
748             let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
749             ret.write_cvalue(fx, val);
750         };
751
752         simd_cast, (c x) {
753             ret.write_cvalue(fx, x.unchecked_cast_to(ret.layout()));
754         };
755
756         simd_add, (c x, c y) {
757             simd_binop!(fx, intrinsic, iadd(x, y) -> ret);
758         };
759         simd_sub, (c x, c y) {
760             simd_binop!(fx, intrinsic, isub(x, y) -> ret);
761         };
762         simd_mul, (c x, c y) {
763             simd_binop!(fx, intrinsic, imul(x, y) -> ret);
764         };
765         simd_div, (c x, c y) {
766             simd_binop!(fx, intrinsic, udiv|sdiv(x, y) -> ret);
767         };
768         simd_rem, (c x, c y) {
769             simd_binop!(fx, intrinsic, urem|srem(x, y) -> ret);
770         };
771         simd_shl, (c x, c y) {
772             simd_binop!(fx, intrinsic, ishl(x, y) -> ret);
773         };
774         simd_shr, (c x, c y) {
775             simd_binop!(fx, intrinsic, ushr|sshr(x, y) -> ret);
776         };
777         simd_and, (c x, c y) {
778             simd_binop!(fx, intrinsic, band(x, y) -> ret);
779         };
780         simd_or, (c x, c y) {
781             simd_binop!(fx, intrinsic, bor(x, y) -> ret);
782         };
783         simd_bxor, (c x, c y) {
784             simd_binop!(fx, intrinsic, bxor(x, y) -> ret);
785         };
786
787         simd_fmin, (c x, c y) {
788             simd_binop!(fx, intrinsic, fmin(x, y) -> ret);
789         };
790         simd_fmax, (c x, c y) {
791             simd_binop!(fx, intrinsic, fmax(x, y) -> ret);
792         };
793     }
794
795     if let Some((_, dest)) = destination {
796         let ret_ebb = fx.get_ebb(dest);
797         fx.bcx.ins().jump(ret_ebb, &[]);
798     } else {
799         trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
800     }
801 }