1 //! Codegen of intrinsics. This includes `extern "rust-intrinsic"`, `extern "platform-intrinsic"`
2 //! and LLVM intrinsics that have symbol names starting with `llvm.`.
8 pub(crate) use cpuid::codegen_cpuid_call;
9 pub(crate) use llvm::codegen_llvm_intrinsic_call;
11 use crate::prelude::*;
23 ($x:ident . $($xs:tt).*) => {
24 concat!(stringify!($x), ".", intrinsic_pat!($($xs).*))
29 (o $fx:expr, $arg:ident) => {
32 (c $fx:expr, $arg:ident) => {
33 trans_operand($fx, $arg)
35 (v $fx:expr, $arg:ident) => {
36 trans_operand($fx, $arg).load_scalar($fx)
40 macro intrinsic_substs {
41 ($substs:expr, $index:expr,) => {},
42 ($substs:expr, $index:expr, $first:ident $(,$rest:ident)*) => {
43 let $first = $substs.type_at($index);
44 intrinsic_substs!($substs, $index+1, $($rest),*);
48 macro intrinsic_match {
49 ($fx:expr, $intrinsic:expr, $substs:expr, $args:expr,
52 $($($name:tt).*)|+ $(if $cond:expr)?, $(<$($subst:ident),*>)? ($($a:ident $arg:ident),*) $content:block;
54 let _ = $substs; // Silence warning when substs is unused.
57 $(intrinsic_pat!($($name).*))|* $(if $cond)? => {
58 #[allow(unused_parens, non_snake_case)]
61 intrinsic_substs!($substs, 0, $($subst),*);
63 if let [$($arg),*] = $args {
65 $(intrinsic_arg!($a $fx, $arg),)*
67 #[warn(unused_parens, non_snake_case)]
72 bug!("wrong number of args for intrinsic {:?}", $intrinsic);
82 macro call_intrinsic_match {
83 ($fx:expr, $intrinsic:expr, $substs:expr, $ret:expr, $destination:expr, $args:expr, $(
84 $name:ident($($arg:ident),*) -> $ty:ident => $func:ident,
88 stringify!($name) => {
89 assert!($substs.is_noop());
90 if let [$(ref $arg),*] = *$args {
92 $(trans_operand($fx, $arg),)*
94 let res = $fx.easy_call(stringify!($func), &[$($arg),*], $fx.tcx.types.$ty);
95 $ret.write_cvalue($fx, res);
97 if let Some((_, dest)) = $destination {
98 let ret_block = $fx.get_block(dest);
99 $fx.bcx.ins().jump(ret_block, &[]);
105 bug!("wrong number of args for intrinsic {:?}", $intrinsic);
114 macro atomic_binop_return_old($fx:expr, $op:ident<$T:ident>($ptr:ident, $src:ident) -> $ret:ident) {
115 crate::atomic_shim::lock_global_lock($fx);
117 let clif_ty = $fx.clif_type($T).unwrap();
118 let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
119 let new = $fx.bcx.ins().$op(old, $src);
120 $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
121 $ret.write_cvalue($fx, CValue::by_val(old, $fx.layout_of($T)));
123 crate::atomic_shim::unlock_global_lock($fx);
126 macro atomic_minmax($fx:expr, $cc:expr, <$T:ident> ($ptr:ident, $src:ident) -> $ret:ident) {
127 crate::atomic_shim::lock_global_lock($fx);
130 let clif_ty = $fx.clif_type($T).unwrap();
131 let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
134 let is_eq = $fx.bcx.ins().icmp(IntCC::SignedGreaterThan, old, $src);
135 let new = $fx.bcx.ins().select(is_eq, old, $src);
138 $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
140 let ret_val = CValue::by_val(old, $ret.layout());
141 $ret.write_cvalue($fx, ret_val);
143 crate::atomic_shim::unlock_global_lock($fx);
146 macro validate_atomic_type($fx:ident, $intrinsic:ident, $span:ident, $ty:expr) {
148 ty::Uint(_) | ty::Int(_) => {}
150 $fx.tcx.sess.span_err(
153 "`{}` intrinsic: expected basic integer type, found `{:?}`",
157 // Prevent verifier error
158 crate::trap::trap_unreachable($fx, "compilation should not have succeeded");
164 macro validate_simd_type($fx:ident, $intrinsic:ident, $span:ident, $ty:expr) {
166 $fx.tcx.sess.span_err($span, &format!("invalid monomorphization of `{}` intrinsic: expected SIMD input type, found non-SIMD `{}`", $intrinsic, $ty));
167 // Prevent verifier error
168 crate::trap::trap_unreachable($fx, "compilation should not have succeeded");
173 fn lane_type_and_count<'tcx>(
175 layout: TyAndLayout<'tcx>,
176 ) -> (TyAndLayout<'tcx>, u16) {
177 assert!(layout.ty.is_simd());
178 let lane_count = match layout.fields {
179 rustc_target::abi::FieldsShape::Array { stride: _, count } => u16::try_from(count).unwrap(),
180 _ => unreachable!("lane_type_and_count({:?})", layout),
182 let lane_layout = layout
184 &ty::layout::LayoutCx {
186 param_env: ParamEnv::reveal_all(),
191 (lane_layout, lane_count)
194 pub(crate) fn clif_vector_type<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx>) -> Option<Type> {
195 let (element, count) = match &layout.abi {
196 Abi::Vector { element, count } => (element.clone(), *count),
200 match scalar_to_clif_type(tcx, element).by(u16::try_from(count).unwrap()) {
201 // Cranelift currently only implements icmp for 128bit vectors.
202 Some(vector_ty) if vector_ty.bits() == 128 => Some(vector_ty),
207 fn simd_for_each_lane<'tcx, M: Module>(
208 fx: &mut FunctionCx<'_, 'tcx, M>,
212 &mut FunctionCx<'_, 'tcx, M>,
218 let layout = val.layout();
220 let (lane_layout, lane_count) = lane_type_and_count(fx.tcx, layout);
221 let (ret_lane_layout, ret_lane_count) = lane_type_and_count(fx.tcx, ret.layout());
222 assert_eq!(lane_count, ret_lane_count);
224 for lane_idx in 0..lane_count {
225 let lane_idx = mir::Field::new(lane_idx.try_into().unwrap());
226 let lane = val.value_field(fx, lane_idx).load_scalar(fx);
228 let res_lane = f(fx, lane_layout, ret_lane_layout, lane);
230 ret.place_field(fx, lane_idx).write_cvalue(fx, res_lane);
234 fn simd_pair_for_each_lane<'tcx, M: Module>(
235 fx: &mut FunctionCx<'_, 'tcx, M>,
240 &mut FunctionCx<'_, 'tcx, M>,
247 assert_eq!(x.layout(), y.layout());
248 let layout = x.layout();
250 let (lane_layout, lane_count) = lane_type_and_count(fx.tcx, layout);
251 let (ret_lane_layout, ret_lane_count) = lane_type_and_count(fx.tcx, ret.layout());
252 assert_eq!(lane_count, ret_lane_count);
254 for lane in 0..lane_count {
255 let lane = mir::Field::new(lane.try_into().unwrap());
256 let x_lane = x.value_field(fx, lane).load_scalar(fx);
257 let y_lane = y.value_field(fx, lane).load_scalar(fx);
259 let res_lane = f(fx, lane_layout, ret_lane_layout, x_lane, y_lane);
261 ret.place_field(fx, lane).write_cvalue(fx, res_lane);
265 fn bool_to_zero_or_max_uint<'tcx>(
266 fx: &mut FunctionCx<'_, 'tcx, impl Module>,
267 layout: TyAndLayout<'tcx>,
270 let ty = fx.clif_type(layout.ty).unwrap();
272 let int_ty = match ty {
273 types::F32 => types::I32,
274 types::F64 => types::I64,
278 let val = fx.bcx.ins().bint(int_ty, val);
279 let mut res = fx.bcx.ins().ineg(val);
282 res = fx.bcx.ins().bitcast(ty, res);
285 CValue::by_val(res, layout)
289 ($fx:expr, $cc:ident($x:ident, $y:ident) -> $ret:ident) => {
290 let vector_ty = clif_vector_type($fx.tcx, $x.layout());
292 if let Some(vector_ty) = vector_ty {
293 let x = $x.load_scalar($fx);
294 let y = $y.load_scalar($fx);
295 let val = $fx.bcx.ins().icmp(IntCC::$cc, x, y);
297 // HACK This depends on the fact that icmp for vectors represents bools as 0 and !0, not 0 and 1.
298 let val = $fx.bcx.ins().raw_bitcast(vector_ty, val);
300 $ret.write_cvalue($fx, CValue::by_val(val, $ret.layout()));
302 simd_pair_for_each_lane(
307 |fx, lane_layout, res_lane_layout, x_lane, y_lane| {
308 let res_lane = match lane_layout.ty.kind() {
309 ty::Uint(_) | ty::Int(_) => fx.bcx.ins().icmp(IntCC::$cc, x_lane, y_lane),
310 _ => unreachable!("{:?}", lane_layout.ty),
312 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
317 ($fx:expr, $cc_u:ident|$cc_s:ident($x:ident, $y:ident) -> $ret:ident) => {
318 // FIXME use vector icmp when possible
319 simd_pair_for_each_lane(
324 |fx, lane_layout, res_lane_layout, x_lane, y_lane| {
325 let res_lane = match lane_layout.ty.kind() {
326 ty::Uint(_) => fx.bcx.ins().icmp(IntCC::$cc_u, x_lane, y_lane),
327 ty::Int(_) => fx.bcx.ins().icmp(IntCC::$cc_s, x_lane, y_lane),
328 _ => unreachable!("{:?}", lane_layout.ty),
330 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
336 macro simd_int_binop {
337 ($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) => {
338 simd_int_binop!($fx, $op|$op($x, $y) -> $ret);
340 ($fx:expr, $op_u:ident|$op_s:ident($x:ident, $y:ident) -> $ret:ident) => {
341 simd_pair_for_each_lane(
346 |fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
347 let res_lane = match lane_layout.ty.kind() {
348 ty::Uint(_) => fx.bcx.ins().$op_u(x_lane, y_lane),
349 ty::Int(_) => fx.bcx.ins().$op_s(x_lane, y_lane),
350 _ => unreachable!("{:?}", lane_layout.ty),
352 CValue::by_val(res_lane, ret_lane_layout)
358 macro simd_int_flt_binop {
359 ($fx:expr, $op:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
360 simd_int_flt_binop!($fx, $op|$op|$op_f($x, $y) -> $ret);
362 ($fx:expr, $op_u:ident|$op_s:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
363 simd_pair_for_each_lane(
368 |fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
369 let res_lane = match lane_layout.ty.kind() {
370 ty::Uint(_) => fx.bcx.ins().$op_u(x_lane, y_lane),
371 ty::Int(_) => fx.bcx.ins().$op_s(x_lane, y_lane),
372 ty::Float(_) => fx.bcx.ins().$op_f(x_lane, y_lane),
373 _ => unreachable!("{:?}", lane_layout.ty),
375 CValue::by_val(res_lane, ret_lane_layout)
381 macro simd_flt_binop($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) {
382 simd_pair_for_each_lane(
387 |fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
388 let res_lane = match lane_layout.ty.kind() {
389 ty::Float(_) => fx.bcx.ins().$op(x_lane, y_lane),
390 _ => unreachable!("{:?}", lane_layout.ty),
392 CValue::by_val(res_lane, ret_lane_layout)
397 pub(crate) fn codegen_intrinsic_call<'tcx>(
398 fx: &mut FunctionCx<'_, 'tcx, impl Module>,
399 instance: Instance<'tcx>,
400 args: &[mir::Operand<'tcx>],
401 destination: Option<(CPlace<'tcx>, BasicBlock)>,
404 let def_id = instance.def_id();
405 let substs = instance.substs;
407 let intrinsic = fx.tcx.item_name(def_id).as_str();
408 let intrinsic = &intrinsic[..];
410 let ret = match destination {
411 Some((place, _)) => place,
413 // Insert non returning intrinsics here
416 trap_abort(fx, "Called intrinsic::abort.");
419 trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
422 crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", span);
424 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
430 if intrinsic.starts_with("simd_") {
431 self::simd::codegen_simd_intrinsic_call(fx, instance, args, ret, span);
432 let ret_block = fx.get_block(destination.expect("SIMD intrinsics don't diverge").1);
433 fx.bcx.ins().jump(ret_block, &[]);
437 let usize_layout = fx.layout_of(fx.tcx.types.usize);
439 call_intrinsic_match! {
440 fx, intrinsic, substs, ret, destination, args,
441 expf32(flt) -> f32 => expf,
442 expf64(flt) -> f64 => exp,
443 exp2f32(flt) -> f32 => exp2f,
444 exp2f64(flt) -> f64 => exp2,
445 sqrtf32(flt) -> f32 => sqrtf,
446 sqrtf64(flt) -> f64 => sqrt,
447 powif32(a, x) -> f32 => __powisf2, // compiler-builtins
448 powif64(a, x) -> f64 => __powidf2, // compiler-builtins
449 powf32(a, x) -> f32 => powf,
450 powf64(a, x) -> f64 => pow,
451 logf32(flt) -> f32 => logf,
452 logf64(flt) -> f64 => log,
453 log2f32(flt) -> f32 => log2f,
454 log2f64(flt) -> f64 => log2,
455 log10f32(flt) -> f32 => log10f,
456 log10f64(flt) -> f64 => log10,
457 fabsf32(flt) -> f32 => fabsf,
458 fabsf64(flt) -> f64 => fabs,
459 fmaf32(x, y, z) -> f32 => fmaf,
460 fmaf64(x, y, z) -> f64 => fma,
461 copysignf32(x, y) -> f32 => copysignf,
462 copysignf64(x, y) -> f64 => copysign,
465 // FIXME use clif insts
466 floorf32(flt) -> f32 => floorf,
467 floorf64(flt) -> f64 => floor,
468 ceilf32(flt) -> f32 => ceilf,
469 ceilf64(flt) -> f64 => ceil,
470 truncf32(flt) -> f32 => truncf,
471 truncf64(flt) -> f64 => trunc,
472 roundf32(flt) -> f32 => roundf,
473 roundf64(flt) -> f64 => round,
476 sinf32(flt) -> f32 => sinf,
477 sinf64(flt) -> f64 => sin,
478 cosf32(flt) -> f32 => cosf,
479 cosf64(flt) -> f64 => cos,
480 tanf32(flt) -> f32 => tanf,
481 tanf64(flt) -> f64 => tan,
485 fx, intrinsic, substs, args,
487 fx.tcx.sess.span_fatal(span, &format!("unsupported intrinsic {}", intrinsic));
491 likely | unlikely, (c a) {
492 ret.write_cvalue(fx, a);
495 fx.bcx.ins().debugtrap();
497 copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
498 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
502 .iconst(fx.pointer_type, elem_size as i64);
503 assert_eq!(args.len(), 3);
504 let byte_amount = fx.bcx.ins().imul(count, elem_size);
506 if intrinsic.contains("nonoverlapping") {
507 // FIXME emit_small_memcpy
508 fx.bcx.call_memcpy(fx.cx.module.target_config(), dst, src, byte_amount);
510 // FIXME emit_small_memmove
511 fx.bcx.call_memmove(fx.cx.module.target_config(), dst, src, byte_amount);
514 // NOTE: the volatile variants have src and dst swapped
515 volatile_copy_memory | volatile_copy_nonoverlapping_memory, <elem_ty> (v dst, v src, v count) {
516 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
520 .iconst(fx.pointer_type, elem_size as i64);
521 assert_eq!(args.len(), 3);
522 let byte_amount = fx.bcx.ins().imul(count, elem_size);
524 // FIXME make the copy actually volatile when using emit_small_mem{cpy,move}
525 if intrinsic.contains("nonoverlapping") {
526 // FIXME emit_small_memcpy
527 fx.bcx.call_memcpy(fx.cx.module.target_config(), dst, src, byte_amount);
529 // FIXME emit_small_memmove
530 fx.bcx.call_memmove(fx.cx.module.target_config(), dst, src, byte_amount);
533 discriminant_value, (c ptr) {
534 let pointee_layout = fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
535 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), pointee_layout);
536 let discr = crate::discriminant::codegen_get_discriminant(fx, val, ret.layout());
537 ret.write_cvalue(fx, discr);
539 size_of_val, <T> (c ptr) {
540 let layout = fx.layout_of(T);
541 let size = if layout.is_unsized() {
542 let (_ptr, info) = ptr.load_scalar_pair(fx);
543 let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout, info);
549 .iconst(fx.pointer_type, layout.size.bytes() as i64)
551 ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
553 min_align_of_val, <T> (c ptr) {
554 let layout = fx.layout_of(T);
555 let align = if layout.is_unsized() {
556 let (_ptr, info) = ptr.load_scalar_pair(fx);
557 let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout, info);
563 .iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
565 ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
568 _ if intrinsic.starts_with("unchecked_") || intrinsic == "exact_div", (c x, c y) {
569 // FIXME trap on overflow
570 let bin_op = match intrinsic {
571 "unchecked_add" => BinOp::Add,
572 "unchecked_sub" => BinOp::Sub,
573 "unchecked_div" | "exact_div" => BinOp::Div,
574 "unchecked_rem" => BinOp::Rem,
575 "unchecked_shl" => BinOp::Shl,
576 "unchecked_shr" => BinOp::Shr,
577 _ => unreachable!("intrinsic {}", intrinsic),
579 let res = crate::num::trans_int_binop(fx, bin_op, x, y);
580 ret.write_cvalue(fx, res);
582 _ if intrinsic.ends_with("_with_overflow"), (c x, c y) {
583 assert_eq!(x.layout().ty, y.layout().ty);
584 let bin_op = match intrinsic {
585 "add_with_overflow" => BinOp::Add,
586 "sub_with_overflow" => BinOp::Sub,
587 "mul_with_overflow" => BinOp::Mul,
588 _ => unreachable!("intrinsic {}", intrinsic),
591 let res = crate::num::trans_checked_int_binop(
597 ret.write_cvalue(fx, res);
599 _ if intrinsic.starts_with("wrapping_"), (c x, c y) {
600 assert_eq!(x.layout().ty, y.layout().ty);
601 let bin_op = match intrinsic {
602 "wrapping_add" => BinOp::Add,
603 "wrapping_sub" => BinOp::Sub,
604 "wrapping_mul" => BinOp::Mul,
605 _ => unreachable!("intrinsic {}", intrinsic),
607 let res = crate::num::trans_int_binop(
613 ret.write_cvalue(fx, res);
615 _ if intrinsic.starts_with("saturating_"), <T> (c lhs, c rhs) {
616 assert_eq!(lhs.layout().ty, rhs.layout().ty);
617 let bin_op = match intrinsic {
618 "saturating_add" => BinOp::Add,
619 "saturating_sub" => BinOp::Sub,
620 _ => unreachable!("intrinsic {}", intrinsic),
623 let signed = type_sign(T);
625 let checked_res = crate::num::trans_checked_int_binop(
632 let (val, has_overflow) = checked_res.load_scalar_pair(fx);
633 let clif_ty = fx.clif_type(T).unwrap();
635 // `select.i8` is not implemented by Cranelift.
636 let has_overflow = fx.bcx.ins().uextend(types::I32, has_overflow);
638 let (min, max) = type_min_max_value(&mut fx.bcx, clif_ty, signed);
640 let val = match (intrinsic, signed) {
641 ("saturating_add", false) => fx.bcx.ins().select(has_overflow, max, val),
642 ("saturating_sub", false) => fx.bcx.ins().select(has_overflow, min, val),
643 ("saturating_add", true) => {
644 let rhs = rhs.load_scalar(fx);
645 let rhs_ge_zero = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
646 let sat_val = fx.bcx.ins().select(rhs_ge_zero, max, min);
647 fx.bcx.ins().select(has_overflow, sat_val, val)
649 ("saturating_sub", true) => {
650 let rhs = rhs.load_scalar(fx);
651 let rhs_ge_zero = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
652 let sat_val = fx.bcx.ins().select(rhs_ge_zero, min, max);
653 fx.bcx.ins().select(has_overflow, sat_val, val)
658 let res = CValue::by_val(val, fx.layout_of(T));
660 ret.write_cvalue(fx, res);
662 rotate_left, <T>(v x, v y) {
663 let layout = fx.layout_of(T);
664 let res = fx.bcx.ins().rotl(x, y);
665 ret.write_cvalue(fx, CValue::by_val(res, layout));
667 rotate_right, <T>(v x, v y) {
668 let layout = fx.layout_of(T);
669 let res = fx.bcx.ins().rotr(x, y);
670 ret.write_cvalue(fx, CValue::by_val(res, layout));
673 // The only difference between offset and arith_offset is regarding UB. Because Cranelift
674 // doesn't have UB both are codegen'ed the same way
675 offset | arith_offset, (c base, v offset) {
676 let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
677 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
678 let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
679 let base_val = base.load_scalar(fx);
680 let res = fx.bcx.ins().iadd(base_val, ptr_diff);
681 ret.write_cvalue(fx, CValue::by_val(res, base.layout()));
684 transmute, (c from) {
685 ret.write_cvalue_transmute(fx, from);
687 write_bytes | volatile_set_memory, (c dst, v val, v count) {
688 let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
689 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
690 let count = fx.bcx.ins().imul_imm(count, pointee_size as i64);
691 let dst_ptr = dst.load_scalar(fx);
692 // FIXME make the memset actually volatile when switching to emit_small_memset
693 // FIXME use emit_small_memset
694 fx.bcx.call_memset(fx.cx.module.target_config(), dst_ptr, val, count);
696 ctlz | ctlz_nonzero, <T> (v arg) {
697 // FIXME trap on `ctlz_nonzero` with zero arg.
698 let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
699 // FIXME verify this algorithm is correct
700 let (lsb, msb) = fx.bcx.ins().isplit(arg);
701 let lsb_lz = fx.bcx.ins().clz(lsb);
702 let msb_lz = fx.bcx.ins().clz(msb);
703 let msb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, msb, 0);
704 let lsb_lz_plus_64 = fx.bcx.ins().iadd_imm(lsb_lz, 64);
705 let res = fx.bcx.ins().select(msb_is_zero, lsb_lz_plus_64, msb_lz);
706 fx.bcx.ins().uextend(types::I128, res)
708 fx.bcx.ins().clz(arg)
710 let res = CValue::by_val(res, fx.layout_of(T));
711 ret.write_cvalue(fx, res);
713 cttz | cttz_nonzero, <T> (v arg) {
714 // FIXME trap on `cttz_nonzero` with zero arg.
715 let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
716 // FIXME verify this algorithm is correct
717 let (lsb, msb) = fx.bcx.ins().isplit(arg);
718 let lsb_tz = fx.bcx.ins().ctz(lsb);
719 let msb_tz = fx.bcx.ins().ctz(msb);
720 let lsb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, lsb, 0);
721 let msb_tz_plus_64 = fx.bcx.ins().iadd_imm(msb_tz, 64);
722 let res = fx.bcx.ins().select(lsb_is_zero, msb_tz_plus_64, lsb_tz);
723 fx.bcx.ins().uextend(types::I128, res)
725 fx.bcx.ins().ctz(arg)
727 let res = CValue::by_val(res, fx.layout_of(T));
728 ret.write_cvalue(fx, res);
731 let res = fx.bcx.ins().popcnt(arg);
732 let res = CValue::by_val(res, fx.layout_of(T));
733 ret.write_cvalue(fx, res);
735 bitreverse, <T> (v arg) {
736 let res = fx.bcx.ins().bitrev(arg);
737 let res = CValue::by_val(res, fx.layout_of(T));
738 ret.write_cvalue(fx, res);
741 // FIXME(CraneStation/cranelift#794) add bswap instruction to cranelift
742 fn swap(bcx: &mut FunctionBuilder<'_>, v: Value) -> Value {
743 match bcx.func.dfg.value_type(v) {
746 // https://code.woboq.org/gcc/include/bits/byteswap.h.html
748 let tmp1 = bcx.ins().ishl_imm(v, 8);
749 let n1 = bcx.ins().band_imm(tmp1, 0xFF00);
751 let tmp2 = bcx.ins().ushr_imm(v, 8);
752 let n2 = bcx.ins().band_imm(tmp2, 0x00FF);
754 bcx.ins().bor(n1, n2)
757 let tmp1 = bcx.ins().ishl_imm(v, 24);
758 let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000);
760 let tmp2 = bcx.ins().ishl_imm(v, 8);
761 let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000);
763 let tmp3 = bcx.ins().ushr_imm(v, 8);
764 let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00);
766 let tmp4 = bcx.ins().ushr_imm(v, 24);
767 let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF);
769 let or_tmp1 = bcx.ins().bor(n1, n2);
770 let or_tmp2 = bcx.ins().bor(n3, n4);
771 bcx.ins().bor(or_tmp1, or_tmp2)
774 let tmp1 = bcx.ins().ishl_imm(v, 56);
775 let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000_0000_0000u64 as i64);
777 let tmp2 = bcx.ins().ishl_imm(v, 40);
778 let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000_0000_0000u64 as i64);
780 let tmp3 = bcx.ins().ishl_imm(v, 24);
781 let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00_0000_0000u64 as i64);
783 let tmp4 = bcx.ins().ishl_imm(v, 8);
784 let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF_0000_0000u64 as i64);
786 let tmp5 = bcx.ins().ushr_imm(v, 8);
787 let n5 = bcx.ins().band_imm(tmp5, 0x0000_0000_FF00_0000u64 as i64);
789 let tmp6 = bcx.ins().ushr_imm(v, 24);
790 let n6 = bcx.ins().band_imm(tmp6, 0x0000_0000_00FF_0000u64 as i64);
792 let tmp7 = bcx.ins().ushr_imm(v, 40);
793 let n7 = bcx.ins().band_imm(tmp7, 0x0000_0000_0000_FF00u64 as i64);
795 let tmp8 = bcx.ins().ushr_imm(v, 56);
796 let n8 = bcx.ins().band_imm(tmp8, 0x0000_0000_0000_00FFu64 as i64);
798 let or_tmp1 = bcx.ins().bor(n1, n2);
799 let or_tmp2 = bcx.ins().bor(n3, n4);
800 let or_tmp3 = bcx.ins().bor(n5, n6);
801 let or_tmp4 = bcx.ins().bor(n7, n8);
803 let or_tmp5 = bcx.ins().bor(or_tmp1, or_tmp2);
804 let or_tmp6 = bcx.ins().bor(or_tmp3, or_tmp4);
805 bcx.ins().bor(or_tmp5, or_tmp6)
808 let (lo, hi) = bcx.ins().isplit(v);
809 let lo = swap(bcx, lo);
810 let hi = swap(bcx, hi);
811 bcx.ins().iconcat(hi, lo)
813 ty => unreachable!("bswap {}", ty),
816 let res = CValue::by_val(swap(&mut fx.bcx, arg), fx.layout_of(T));
817 ret.write_cvalue(fx, res);
819 assert_inhabited | assert_zero_valid | assert_uninit_valid, <T> () {
820 let layout = fx.layout_of(T);
821 if layout.abi.is_uninhabited() {
822 crate::base::codegen_panic(
824 &format!("attempted to instantiate uninhabited type `{}`", T),
830 if intrinsic == "assert_zero_valid" && !layout.might_permit_raw_init(fx, /*zero:*/ true).unwrap() {
831 crate::base::codegen_panic(
833 &format!("attempted to zero-initialize type `{}`, which is invalid", T),
839 if intrinsic == "assert_uninit_valid" && !layout.might_permit_raw_init(fx, /*zero:*/ false).unwrap() {
840 crate::base::codegen_panic(
842 &format!("attempted to leave type `{}` uninitialized, which is invalid", T),
849 volatile_load | unaligned_volatile_load, (c ptr) {
850 // Cranelift treats loads as volatile by default
851 // FIXME ignore during stack2reg optimization
852 // FIXME correctly handle unaligned_volatile_load
854 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
855 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
856 ret.write_cvalue(fx, val);
858 volatile_store | unaligned_volatile_store, (v ptr, c val) {
859 // Cranelift treats stores as volatile by default
860 // FIXME ignore during stack2reg optimization
861 // FIXME correctly handle unaligned_volatile_store
862 let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
863 dest.write_cvalue(fx, val);
866 size_of | pref_align_of | min_align_of | needs_drop | type_id | type_name | variant_count, () {
868 fx.tcx.const_eval_instance(ParamEnv::reveal_all(), instance, None).unwrap();
869 let val = crate::constant::trans_const_value(
874 ret.write_cvalue(fx, val);
877 ptr_offset_from, <T> (v ptr, v base) {
878 let isize_layout = fx.layout_of(fx.tcx.types.isize);
880 let pointee_size: u64 = fx.layout_of(T).size.bytes();
881 let diff = fx.bcx.ins().isub(ptr, base);
882 // FIXME this can be an exact division.
883 let val = CValue::by_val(fx.bcx.ins().sdiv_imm(diff, pointee_size as i64), isize_layout);
884 ret.write_cvalue(fx, val);
887 ptr_guaranteed_eq, (c a, c b) {
888 let val = crate::num::trans_ptr_binop(fx, BinOp::Eq, a, b);
889 ret.write_cvalue(fx, val);
892 ptr_guaranteed_ne, (c a, c b) {
893 let val = crate::num::trans_ptr_binop(fx, BinOp::Ne, a, b);
894 ret.write_cvalue(fx, val);
897 caller_location, () {
898 let caller_location = fx.get_caller_location(span);
899 ret.write_cvalue(fx, caller_location);
902 _ if intrinsic.starts_with("atomic_fence"), () {
903 crate::atomic_shim::lock_global_lock(fx);
904 crate::atomic_shim::unlock_global_lock(fx);
906 _ if intrinsic.starts_with("atomic_singlethreadfence"), () {
907 crate::atomic_shim::lock_global_lock(fx);
908 crate::atomic_shim::unlock_global_lock(fx);
910 _ if intrinsic.starts_with("atomic_load"), (c ptr) {
911 crate::atomic_shim::lock_global_lock(fx);
914 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
915 validate_atomic_type!(fx, intrinsic, span, inner_layout.ty);
916 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
917 ret.write_cvalue(fx, val);
919 crate::atomic_shim::unlock_global_lock(fx);
921 _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
922 validate_atomic_type!(fx, intrinsic, span, val.layout().ty);
924 crate::atomic_shim::lock_global_lock(fx);
926 let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
927 dest.write_cvalue(fx, val);
929 crate::atomic_shim::unlock_global_lock(fx);
931 _ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
932 validate_atomic_type!(fx, intrinsic, span, T);
934 crate::atomic_shim::lock_global_lock(fx);
937 let clif_ty = fx.clif_type(T).unwrap();
938 let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
939 ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
942 let dest = CPlace::for_ptr(Pointer::new(ptr), src.layout());
943 dest.write_cvalue(fx, src);
945 crate::atomic_shim::unlock_global_lock(fx);
947 _ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, c test_old, c new) { // both atomic_cxchg_* and atomic_cxchgweak_*
948 validate_atomic_type!(fx, intrinsic, span, T);
950 let test_old = test_old.load_scalar(fx);
951 let new = new.load_scalar(fx);
953 crate::atomic_shim::lock_global_lock(fx);
956 let clif_ty = fx.clif_type(T).unwrap();
957 let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
960 let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old);
961 let new = fx.bcx.ins().select(is_eq, new, old); // Keep old if not equal to test_old
964 fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
966 let ret_val = CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
967 ret.write_cvalue(fx, ret_val);
969 crate::atomic_shim::unlock_global_lock(fx);
972 _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, c amount) {
973 validate_atomic_type!(fx, intrinsic, span, ret.layout().ty);
974 let amount = amount.load_scalar(fx);
975 atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
977 _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, c amount) {
978 validate_atomic_type!(fx, intrinsic, span, ret.layout().ty);
979 let amount = amount.load_scalar(fx);
980 atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
982 _ if intrinsic.starts_with("atomic_and"), <T> (v ptr, c src) {
983 validate_atomic_type!(fx, intrinsic, span, ret.layout().ty);
984 let src = src.load_scalar(fx);
985 atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
987 _ if intrinsic.starts_with("atomic_nand"), <T> (v ptr, c src) {
988 validate_atomic_type!(fx, intrinsic, span, T);
990 let src = src.load_scalar(fx);
992 crate::atomic_shim::lock_global_lock(fx);
994 let clif_ty = fx.clif_type(T).unwrap();
995 let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
996 let and = fx.bcx.ins().band(old, src);
997 let new = fx.bcx.ins().bnot(and);
998 fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
999 ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
1001 crate::atomic_shim::unlock_global_lock(fx);
1003 _ if intrinsic.starts_with("atomic_or"), <T> (v ptr, c src) {
1004 validate_atomic_type!(fx, intrinsic, span, ret.layout().ty);
1005 let src = src.load_scalar(fx);
1006 atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
1008 _ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, c src) {
1009 validate_atomic_type!(fx, intrinsic, span, ret.layout().ty);
1010 let src = src.load_scalar(fx);
1011 atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
1014 _ if intrinsic.starts_with("atomic_max"), <T> (v ptr, c src) {
1015 validate_atomic_type!(fx, intrinsic, span, ret.layout().ty);
1016 let src = src.load_scalar(fx);
1017 atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
1019 _ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, c src) {
1020 validate_atomic_type!(fx, intrinsic, span, ret.layout().ty);
1021 let src = src.load_scalar(fx);
1022 atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
1024 _ if intrinsic.starts_with("atomic_min"), <T> (v ptr, c src) {
1025 validate_atomic_type!(fx, intrinsic, span, ret.layout().ty);
1026 let src = src.load_scalar(fx);
1027 atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
1029 _ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, c src) {
1030 validate_atomic_type!(fx, intrinsic, span, ret.layout().ty);
1031 let src = src.load_scalar(fx);
1032 atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
1035 minnumf32, (v a, v b) {
1036 let val = fx.bcx.ins().fmin(a, b);
1037 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
1038 ret.write_cvalue(fx, val);
1040 minnumf64, (v a, v b) {
1041 let val = fx.bcx.ins().fmin(a, b);
1042 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
1043 ret.write_cvalue(fx, val);
1045 maxnumf32, (v a, v b) {
1046 let val = fx.bcx.ins().fmax(a, b);
1047 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
1048 ret.write_cvalue(fx, val);
1050 maxnumf64, (v a, v b) {
1051 let val = fx.bcx.ins().fmax(a, b);
1052 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
1053 ret.write_cvalue(fx, val);
1056 try, (v f, v data, v _catch_fn) {
1057 // FIXME once unwinding is supported, change this to actually catch panics
1058 let f_sig = fx.bcx.func.import_signature(Signature {
1059 call_conv: CallConv::triple_default(fx.triple()),
1060 params: vec![AbiParam::new(fx.bcx.func.dfg.value_type(data))],
1064 fx.bcx.ins().call_indirect(f_sig, f, &[data]);
1066 let ret_val = CValue::const_val(fx, ret.layout(), 0);
1067 ret.write_cvalue(fx, ret_val);
1070 fadd_fast | fsub_fast | fmul_fast | fdiv_fast | frem_fast, (c x, c y) {
1071 let res = crate::num::trans_float_binop(fx, match intrinsic {
1072 "fadd_fast" => BinOp::Add,
1073 "fsub_fast" => BinOp::Sub,
1074 "fmul_fast" => BinOp::Mul,
1075 "fdiv_fast" => BinOp::Div,
1076 "frem_fast" => BinOp::Rem,
1077 _ => unreachable!(),
1079 ret.write_cvalue(fx, res);
1081 float_to_int_unchecked, (v f) {
1082 let res = crate::cast::clif_int_or_float_cast(
1086 fx.clif_type(ret.layout().ty).unwrap(),
1087 type_sign(ret.layout().ty),
1089 ret.write_cvalue(fx, CValue::by_val(res, ret.layout()));
1093 if let Some((_, dest)) = destination {
1094 let ret_block = fx.get_block(dest);
1095 fx.bcx.ins().jump(ret_block, &[]);
1097 trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");