16 ($x:ident . $($xs:tt).*) => {
17 concat!(stringify!($x), ".", intrinsic_pat!($($xs).*))
22 (o $fx:expr, $arg:ident) => {
25 (c $fx:expr, $arg:ident) => {
26 trans_operand($fx, $arg)
28 (v $fx:expr, $arg:ident) => {
29 trans_operand($fx, $arg).load_scalar($fx)
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),*);
41 macro intrinsic_match {
42 ($fx:expr, $intrinsic:expr, $substs:expr, $args:expr,
45 $($($name:tt).*)|+ $(if $cond:expr)?, $(<$($subst:ident),*>)? ($($a:ident $arg:ident),*) $content:block;
49 $(intrinsic_pat!($($name).*))|* $(if $cond)? => {
50 #[allow(unused_parens, non_snake_case)]
53 intrinsic_substs!($substs, 0, $($subst),*);
55 if let [$($arg),*] = $args {
57 $(intrinsic_arg!($a $fx, $arg),)*
59 #[warn(unused_parens, non_snake_case)]
64 bug!("wrong number of args for intrinsic {:?}", $intrinsic);
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,
80 stringify!($name) => {
81 assert!($substs.is_noop());
82 if let [$(ref $arg),*] = *$args {
84 $(trans_operand($fx, $arg),)*
86 let res = $fx.easy_call(stringify!($func), &[$($arg),*], $fx.tcx.types.$ty);
87 $ret.write_cvalue($fx, res);
89 if let Some((_, dest)) = $destination {
90 let ret_ebb = $fx.get_ebb(dest);
91 $fx.bcx.ins().jump(ret_ebb, &[]);
97 bug!("wrong number of args for intrinsic {:?}", $intrinsic);
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)));
114 macro atomic_minmax($fx:expr, $cc:expr, <$T:ident> ($ptr:ident, $src:ident) -> $ret:ident) {
116 let clif_ty = $fx.clif_type($T).unwrap();
117 let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
120 let is_eq = codegen_icmp($fx, IntCC::SignedGreaterThan, old, $src);
121 let new = $fx.bcx.ins().select(is_eq, old, $src);
124 $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
126 let ret_val = CValue::by_val(old, $ret.layout());
127 $ret.write_cvalue($fx, ret_val);
130 fn lane_type_and_count<'tcx>(
131 fx: &FunctionCx<'_, 'tcx, impl Backend>,
132 layout: TyLayout<'tcx>,
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(),
139 "Non vector type {:?} passed to or returned from simd_* intrinsic {}",
143 let lane_layout = layout.field(fx, 0);
144 (lane_layout, lane_count)
147 fn simd_for_each_lane<'tcx, B: Backend>(
148 fx: &mut FunctionCx<'_, 'tcx, B>,
154 &mut FunctionCx<'_, 'tcx, B>,
161 assert_eq!(x.layout(), y.layout());
162 let layout = x.layout();
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);
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);
173 let res_lane = f(fx, lane_layout, ret_lane_layout, x_lane, y_lane);
175 ret.place_field(fx, lane).write_cvalue(fx, res_lane);
179 fn bool_to_zero_or_max_uint<'tcx>(
180 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
181 layout: TyLayout<'tcx>,
184 let ty = fx.clif_type(layout.ty).unwrap();
186 let int_ty = match ty {
187 types::F32 => types::I32,
188 types::F64 => types::I64,
192 let zero = fx.bcx.ins().iconst(int_ty, 0);
196 .iconst(int_ty, (u64::max_value() >> (64 - int_ty.bits())) as i64);
197 let mut res = fx.bcx.ins().select(val, max, zero);
200 res = fx.bcx.ins().bitcast(ty, res);
203 CValue::by_val(res, layout)
207 ($fx:expr, $intrinsic:expr, $cc:ident($x:ident, $y:ident) -> $ret:ident) => {
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),
219 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
223 ($fx:expr, $intrinsic:expr, $cc_u:ident|$cc_s:ident($x:ident, $y:ident) -> $ret:ident) => {
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),
236 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
242 macro simd_int_binop {
243 ($fx:expr, $intrinsic:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) => {
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),
255 CValue::by_val(res_lane, ret_lane_layout)
259 ($fx:expr, $intrinsic:expr, $op_u:ident|$op_s:ident($x:ident, $y:ident) -> $ret:ident) => {
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),
272 CValue::by_val(res_lane, ret_lane_layout)
278 macro simd_int_flt_binop {
279 ($fx:expr, $intrinsic:expr, $op:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
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),
292 CValue::by_val(res_lane, ret_lane_layout)
296 ($fx:expr, $intrinsic:expr, $op_u:ident|$op_s:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
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),
310 CValue::by_val(res_lane, ret_lane_layout)
316 macro simd_flt_binop($fx:expr, $intrinsic:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) {
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),
328 CValue::by_val(res_lane, ret_lane_layout)
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)>,
340 let def_id = instance.def_id();
341 let substs = instance.substs;
343 let intrinsic = fx.tcx.item_name(def_id).as_str();
344 let intrinsic = &intrinsic[..];
346 let ret = match destination {
347 Some((place, _)) => place,
349 // Insert non returning intrinsics here
352 trap_panic(fx, "Called intrinsic::abort.");
355 trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
360 "[corruption] Called intrinsic::transmute with uninhabited argument.",
363 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
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, &[]);
376 let usize_layout = fx.layout_of(fx.tcx.types.usize);
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,
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,
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,
424 fx, intrinsic, substs, args,
426 unimpl!("unsupported intrinsic {}", intrinsic)
430 likely | unlikely, (c a) {
431 ret.write_cvalue(fx, a);
434 fx.bcx.ins().debugtrap();
436 copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
437 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
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);
445 if intrinsic.ends_with("_nonoverlapping") {
446 fx.bcx.call_memcpy(fx.module.target_config(), dst, src, byte_amount);
448 fx.bcx.call_memmove(fx.module.target_config(), dst, src, byte_amount);
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);
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);
467 .iconst(fx.pointer_type, layout.size.bytes() as i64)
469 ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
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);
481 .iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
483 ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
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),
496 let res = crate::num::trans_int_binop(fx, bin_op, x, y);
497 ret.write_cvalue(fx, res);
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),
508 let res = crate::num::trans_checked_int_binop(
514 ret.write_cvalue(fx, res);
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),
524 let res = crate::num::trans_int_binop(
530 ret.write_cvalue(fx, res);
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),
540 let signed = type_sign(T);
542 let checked_res = crate::num::trans_checked_int_binop(
549 let (val, has_overflow) = checked_res.load_scalar_pair(fx);
550 let clif_ty = fx.clif_type(T).unwrap();
552 // `select.i8` is not implemented by Cranelift.
553 let has_overflow = fx.bcx.ins().uextend(types::I32, has_overflow);
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);
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)
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)
577 let res = CValue::by_val(val, fx.layout_of(T));
579 ret.write_cvalue(fx, res);
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));
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));
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()));
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))
610 let layout = ret.layout();
611 if layout.abi == Abi::Uninhabited {
612 crate::trap::trap_panic(fx, "[panic] Called intrinsic::init for uninhabited type.");
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),
623 let zero = fx.bcx.ins().iconst(types::I64, 0);
624 fx.bcx.ins().iconcat(zero, zero)
627 let zero = fx.bcx.ins().iconst(types::I32, 0);
628 fx.bcx.ins().bitcast(types::F32, zero)
631 let zero = fx.bcx.ins().iconst(types::I64, 0);
632 fx.bcx.ins().bitcast(types::F64, zero)
634 _ => panic!("clif_type returned {}", clif_ty),
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);
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);
647 let layout = ret.layout();
648 if layout.abi == Abi::Uninhabited {
649 crate::trap::trap_panic(fx, "[panic] Called intrinsic::uninit for uninhabited type.");
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),
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)
664 let zero = fx.bcx.ins().iconst(types::I32, 0xdeadbeef);
665 fx.bcx.ins().bitcast(types::F32, zero)
668 let zero = fx.bcx.ins().iconst(types::I64, 0xcafebabedeadbeefu64 as i64);
669 fx.bcx.ins().bitcast(types::F64, zero)
671 _ => panic!("clif_type returned {}", clif_ty),
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);
676 CPlaceInner::Addr(_, _) => {
677 // Don't write to `ret`, as the destination memory is already uninitialized.
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);
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)
700 fx.bcx.ins().clz(arg)
702 let res = CValue::by_val(res, fx.layout_of(T));
703 ret.write_cvalue(fx, res);
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)
717 fx.bcx.ins().ctz(arg)
719 let res = CValue::by_val(res, fx.layout_of(T));
720 ret.write_cvalue(fx, res);
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);
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);
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) {
738 // https://code.woboq.org/gcc/include/bits/byteswap.h.html
740 let tmp1 = bcx.ins().ishl_imm(v, 8);
741 let n1 = bcx.ins().band_imm(tmp1, 0xFF00);
743 let tmp2 = bcx.ins().ushr_imm(v, 8);
744 let n2 = bcx.ins().band_imm(tmp2, 0x00FF);
746 bcx.ins().bor(n1, n2)
749 let tmp1 = bcx.ins().ishl_imm(v, 24);
750 let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000);
752 let tmp2 = bcx.ins().ishl_imm(v, 8);
753 let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000);
755 let tmp3 = bcx.ins().ushr_imm(v, 8);
756 let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00);
758 let tmp4 = bcx.ins().ushr_imm(v, 24);
759 let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF);
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)
766 let tmp1 = bcx.ins().ishl_imm(v, 56);
767 let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000_0000_0000u64 as i64);
769 let tmp2 = bcx.ins().ishl_imm(v, 40);
770 let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000_0000_0000u64 as i64);
772 let tmp3 = bcx.ins().ishl_imm(v, 24);
773 let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00_0000_0000u64 as i64);
775 let tmp4 = bcx.ins().ishl_imm(v, 8);
776 let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF_0000_0000u64 as i64);
778 let tmp5 = bcx.ins().ushr_imm(v, 8);
779 let n5 = bcx.ins().band_imm(tmp5, 0x0000_0000_FF00_0000u64 as i64);
781 let tmp6 = bcx.ins().ushr_imm(v, 24);
782 let n6 = bcx.ins().band_imm(tmp6, 0x0000_0000_00FF_0000u64 as i64);
784 let tmp7 = bcx.ins().ushr_imm(v, 40);
785 let n7 = bcx.ins().band_imm(tmp7, 0x0000_0000_0000_FF00u64 as i64);
787 let tmp8 = bcx.ins().ushr_imm(v, 56);
788 let n8 = bcx.ins().band_imm(tmp8, 0x0000_0000_0000_00FFu64 as i64);
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);
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)
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)
805 ty => unimplemented!("bswap {}", ty),
808 let res = CValue::by_val(swap(&mut fx.bcx, arg), fx.layout_of(T));
809 ret.write_cvalue(fx, res);
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.");
818 volatile_load, (c ptr) {
819 // Cranelift treats loads as volatile by default
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);
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);
831 size_of | pref_align_of | min_align_of | needs_drop | type_id | type_name, () {
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);
838 ptr_offset_from, <T> (v ptr, v base) {
839 let isize_layout = fx.layout_of(fx.tcx.types.isize);
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);
848 caller_location, () {
849 let caller_location = fx.get_caller_location(span);
850 ret.write_cvalue(fx, caller_location);
853 _ if intrinsic.starts_with("atomic_fence"), () {};
854 _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
855 _ if intrinsic.starts_with("atomic_load"), (c ptr) {
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);
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);
865 _ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
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)));
872 let dest = CPlace::for_ptr(Pointer::new(ptr), src.layout());
873 dest.write_cvalue(fx, src);
875 _ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*
877 let clif_ty = fx.clif_type(T).unwrap();
878 let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
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
885 fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
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);
891 _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
892 atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
894 _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
895 atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
897 _ if intrinsic.starts_with("atomic_and"), <T> (v ptr, v src) {
898 atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
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)));
908 _ if intrinsic.starts_with("atomic_or"), <T> (v ptr, v src) {
909 atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
911 _ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, v src) {
912 atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
915 _ if intrinsic.starts_with("atomic_max"), <T> (v ptr, v src) {
916 atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
918 _ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, v src) {
919 atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
921 _ if intrinsic.starts_with("atomic_min"), <T> (v ptr, v src) {
922 atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
924 _ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, v src) {
925 atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
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);
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);
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);
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);
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))],
957 fx.bcx.ins().call_indirect(f_sig, f, &[data]);
959 let ret_val = CValue::const_val(fx, ret.layout().ty, 0);
960 ret.write_cvalue(fx, ret_val);
964 if let Some((_, dest)) = destination {
965 let ret_ebb = fx.get_ebb(dest);
966 fx.bcx.ins().jump(ret_ebb, &[]);
968 trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");