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 pub fn lane_type_and_count<'tcx>(
132 layout: TyLayout<'tcx>,
133 ) -> (TyLayout<'tcx>, u32) {
134 assert!(layout.ty.is_simd());
135 let lane_count = match layout.fields {
136 layout::FieldPlacement::Array { stride: _, count } => u32::try_from(count).unwrap(),
137 _ => unreachable!("lane_type_and_count({:?})", layout),
139 let lane_layout = layout.field(&ty::layout::LayoutCx {
141 param_env: ParamEnv::reveal_all(),
143 (lane_layout, lane_count)
146 fn simd_for_each_lane<'tcx, B: Backend>(
147 fx: &mut FunctionCx<'_, 'tcx, B>,
152 &mut FunctionCx<'_, 'tcx, B>,
158 let layout = val.layout();
160 let (lane_layout, lane_count) = lane_type_and_count(fx.tcx, layout);
161 let (ret_lane_layout, ret_lane_count) = lane_type_and_count(fx.tcx, ret.layout());
162 assert_eq!(lane_count, ret_lane_count);
164 for lane_idx in 0..lane_count {
165 let lane_idx = mir::Field::new(lane_idx.try_into().unwrap());
166 let lane = val.value_field(fx, lane_idx).load_scalar(fx);
168 let res_lane = f(fx, lane_layout, ret_lane_layout, lane);
170 ret.place_field(fx, lane_idx).write_cvalue(fx, res_lane);
174 fn simd_pair_for_each_lane<'tcx, B: Backend>(
175 fx: &mut FunctionCx<'_, 'tcx, B>,
181 &mut FunctionCx<'_, 'tcx, B>,
188 assert_eq!(x.layout(), y.layout());
189 let layout = x.layout();
191 let (lane_layout, lane_count) = lane_type_and_count(fx.tcx, layout);
192 let (ret_lane_layout, ret_lane_count) = lane_type_and_count(fx.tcx, ret.layout());
193 assert_eq!(lane_count, ret_lane_count);
195 for lane in 0..lane_count {
196 let lane = mir::Field::new(lane.try_into().unwrap());
197 let x_lane = x.value_field(fx, lane).load_scalar(fx);
198 let y_lane = y.value_field(fx, lane).load_scalar(fx);
200 let res_lane = f(fx, lane_layout, ret_lane_layout, x_lane, y_lane);
202 ret.place_field(fx, lane).write_cvalue(fx, res_lane);
206 fn bool_to_zero_or_max_uint<'tcx>(
207 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
208 layout: TyLayout<'tcx>,
211 let ty = fx.clif_type(layout.ty).unwrap();
213 let int_ty = match ty {
214 types::F32 => types::I32,
215 types::F64 => types::I64,
219 let zero = fx.bcx.ins().iconst(int_ty, 0);
223 .iconst(int_ty, (u64::max_value() >> (64 - int_ty.bits())) as i64);
224 let mut res = fx.bcx.ins().select(val, max, zero);
227 res = fx.bcx.ins().bitcast(ty, res);
230 CValue::by_val(res, layout)
234 ($fx:expr, $intrinsic:expr, $cc:ident($x:ident, $y:ident) -> $ret:ident) => {
235 simd_pair_for_each_lane(
241 |fx, lane_layout, res_lane_layout, x_lane, y_lane| {
242 let res_lane = match lane_layout.ty.kind {
243 ty::Uint(_) | ty::Int(_) => codegen_icmp(fx, IntCC::$cc, x_lane, y_lane),
244 _ => unreachable!("{:?}", lane_layout.ty),
246 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
250 ($fx:expr, $intrinsic:expr, $cc_u:ident|$cc_s:ident($x:ident, $y:ident) -> $ret:ident) => {
251 simd_pair_for_each_lane(
257 |fx, lane_layout, res_lane_layout, x_lane, y_lane| {
258 let res_lane = match lane_layout.ty.kind {
259 ty::Uint(_) => codegen_icmp(fx, IntCC::$cc_u, x_lane, y_lane),
260 ty::Int(_) => codegen_icmp(fx, IntCC::$cc_s, x_lane, y_lane),
261 _ => unreachable!("{:?}", lane_layout.ty),
263 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
269 macro simd_int_binop {
270 ($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) => {
271 simd_int_binop!($fx, $op|$op($x, $y) -> $ret);
273 ($fx:expr, $op_u:ident|$op_s:ident($x:ident, $y:ident) -> $ret:ident) => {
274 let (lane_layout, lane_count) = lane_type_and_count($fx.tcx, $x.layout());
275 let x_val = $x.load_scalar($fx);
276 let y_val = $y.load_scalar($fx);
278 let res = match lane_layout.ty.kind {
279 ty::Uint(_) => $fx.bcx.ins().$op_u(x_val, y_val),
280 ty::Int(_) => $fx.bcx.ins().$op_s(x_val, y_val),
281 _ => unreachable!("{:?}", lane_layout.ty),
283 $ret.write_cvalue($fx, CValue::by_val(res, $ret.layout()));
287 macro simd_int_flt_binop {
288 ($fx:expr, $op:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
289 simd_int_flt_binop!($fx, $op|$op|$op_f($x, $y) -> $ret);
291 ($fx:expr, $op_u:ident|$op_s:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
292 let (lane_layout, lane_count) = lane_type_and_count($fx.tcx, $x.layout());
293 let x_val = $x.load_scalar($fx);
294 let y_val = $y.load_scalar($fx);
296 let res = match lane_layout.ty.kind {
297 ty::Uint(_) => $fx.bcx.ins().$op_u(x_val, y_val),
298 ty::Int(_) => $fx.bcx.ins().$op_s(x_val, y_val),
299 ty::Float(_) => $fx.bcx.ins().$op_f(x_val, y_val),
300 _ => unreachable!("{:?}", lane_layout.ty),
302 $ret.write_cvalue($fx, CValue::by_val(res, $ret.layout()));
306 macro simd_flt_binop($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) {
307 let (lane_layout, lane_count) = lane_type_and_count($fx.tcx, $x.layout());
308 let x_val = $x.load_scalar($fx);
309 let y_val = $y.load_scalar($fx);
311 let res = match lane_layout.ty.kind {
312 ty::Float(_) => $fx.bcx.ins().$op(x_val, y_val),
313 _ => unreachable!("{:?}", lane_layout.ty),
315 $ret.write_cvalue($fx, CValue::by_val(res, $ret.layout()));
318 pub fn codegen_intrinsic_call<'tcx>(
319 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
320 instance: Instance<'tcx>,
321 args: &[mir::Operand<'tcx>],
322 destination: Option<(CPlace<'tcx>, BasicBlock)>,
325 let def_id = instance.def_id();
326 let substs = instance.substs;
328 let intrinsic = fx.tcx.item_name(def_id).as_str();
329 let intrinsic = &intrinsic[..];
331 let ret = match destination {
332 Some((place, _)) => place,
334 // Insert non returning intrinsics here
337 trap_panic(fx, "Called intrinsic::abort.");
340 trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
345 "[corruption] Called intrinsic::transmute with uninhabited argument.",
348 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
354 if intrinsic.starts_with("simd_") {
355 self::simd::codegen_simd_intrinsic_call(fx, instance, args, ret, span);
356 let ret_ebb = fx.get_ebb(destination.expect("SIMD intrinsics don't diverge").1);
357 fx.bcx.ins().jump(ret_ebb, &[]);
361 let usize_layout = fx.layout_of(fx.tcx.types.usize);
363 call_intrinsic_match! {
364 fx, intrinsic, substs, ret, destination, args,
365 expf32(flt) -> f32 => expf,
366 expf64(flt) -> f64 => exp,
367 exp2f32(flt) -> f32 => exp2f,
368 exp2f64(flt) -> f64 => exp2,
369 sqrtf32(flt) -> f32 => sqrtf,
370 sqrtf64(flt) -> f64 => sqrt,
371 powif32(a, x) -> f32 => __powisf2, // compiler-builtins
372 powif64(a, x) -> f64 => __powidf2, // compiler-builtins
373 powf32(a, x) -> f32 => powf,
374 powf64(a, x) -> f64 => pow,
375 logf32(flt) -> f32 => logf,
376 logf64(flt) -> f64 => log,
377 log2f32(flt) -> f32 => log2f,
378 log2f64(flt) -> f64 => log2,
379 log10f32(flt) -> f32 => log10f,
380 log10f64(flt) -> f64 => log10,
381 fabsf32(flt) -> f32 => fabsf,
382 fabsf64(flt) -> f64 => fabs,
383 fmaf32(x, y, z) -> f32 => fmaf,
384 fmaf64(x, y, z) -> f64 => fma,
385 copysignf32(x, y) -> f32 => copysignf,
386 copysignf64(x, y) -> f64 => copysign,
389 // FIXME use clif insts
390 floorf32(flt) -> f32 => floorf,
391 floorf64(flt) -> f64 => floor,
392 ceilf32(flt) -> f32 => ceilf,
393 ceilf64(flt) -> f64 => ceil,
394 truncf32(flt) -> f32 => truncf,
395 truncf64(flt) -> f64 => trunc,
396 roundf32(flt) -> f32 => roundf,
397 roundf64(flt) -> f64 => round,
400 sinf32(flt) -> f32 => sinf,
401 sinf64(flt) -> f64 => sin,
402 cosf32(flt) -> f32 => cosf,
403 cosf64(flt) -> f64 => cos,
404 tanf32(flt) -> f32 => tanf,
405 tanf64(flt) -> f64 => tan,
409 fx, intrinsic, substs, args,
411 unimpl!("unsupported intrinsic {}", intrinsic)
415 likely | unlikely, (c a) {
416 ret.write_cvalue(fx, a);
419 fx.bcx.ins().debugtrap();
421 copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
422 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
426 .iconst(fx.pointer_type, elem_size as i64);
427 assert_eq!(args.len(), 3);
428 let byte_amount = fx.bcx.ins().imul(count, elem_size);
430 if intrinsic.ends_with("_nonoverlapping") {
431 fx.bcx.call_memcpy(fx.module.target_config(), dst, src, byte_amount);
433 fx.bcx.call_memmove(fx.module.target_config(), dst, src, byte_amount);
436 discriminant_value, (c ptr) {
437 let pointee_layout = fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
438 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), pointee_layout);
439 let discr = crate::discriminant::codegen_get_discriminant(fx, val, ret.layout());
440 ret.write_cvalue(fx, discr);
442 size_of_val, <T> (c ptr) {
443 let layout = fx.layout_of(T);
444 let size = if layout.is_unsized() {
445 let (_ptr, info) = ptr.load_scalar_pair(fx);
446 let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
452 .iconst(fx.pointer_type, layout.size.bytes() as i64)
454 ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
456 min_align_of_val, <T> (c ptr) {
457 let layout = fx.layout_of(T);
458 let align = if layout.is_unsized() {
459 let (_ptr, info) = ptr.load_scalar_pair(fx);
460 let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
466 .iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
468 ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
471 _ if intrinsic.starts_with("unchecked_") || intrinsic == "exact_div", (c x, c y) {
472 // FIXME trap on overflow
473 let bin_op = match intrinsic {
474 "unchecked_sub" => BinOp::Sub,
475 "unchecked_div" | "exact_div" => BinOp::Div,
476 "unchecked_rem" => BinOp::Rem,
477 "unchecked_shl" => BinOp::Shl,
478 "unchecked_shr" => BinOp::Shr,
479 _ => unimplemented!("intrinsic {}", intrinsic),
481 let res = crate::num::trans_int_binop(fx, bin_op, x, y);
482 ret.write_cvalue(fx, res);
484 _ if intrinsic.ends_with("_with_overflow"), (c x, c y) {
485 assert_eq!(x.layout().ty, y.layout().ty);
486 let bin_op = match intrinsic {
487 "add_with_overflow" => BinOp::Add,
488 "sub_with_overflow" => BinOp::Sub,
489 "mul_with_overflow" => BinOp::Mul,
490 _ => unimplemented!("intrinsic {}", intrinsic),
493 let res = crate::num::trans_checked_int_binop(
499 ret.write_cvalue(fx, res);
501 _ if intrinsic.starts_with("wrapping_"), (c x, c y) {
502 assert_eq!(x.layout().ty, y.layout().ty);
503 let bin_op = match intrinsic {
504 "wrapping_add" => BinOp::Add,
505 "wrapping_sub" => BinOp::Sub,
506 "wrapping_mul" => BinOp::Mul,
507 _ => unimplemented!("intrinsic {}", intrinsic),
509 let res = crate::num::trans_int_binop(
515 ret.write_cvalue(fx, res);
517 _ if intrinsic.starts_with("saturating_"), <T> (c lhs, c rhs) {
518 assert_eq!(lhs.layout().ty, rhs.layout().ty);
519 let bin_op = match intrinsic {
520 "saturating_add" => BinOp::Add,
521 "saturating_sub" => BinOp::Sub,
522 _ => unimplemented!("intrinsic {}", intrinsic),
525 let signed = type_sign(T);
527 let checked_res = crate::num::trans_checked_int_binop(
534 let (val, has_overflow) = checked_res.load_scalar_pair(fx);
535 let clif_ty = fx.clif_type(T).unwrap();
537 // `select.i8` is not implemented by Cranelift.
538 let has_overflow = fx.bcx.ins().uextend(types::I32, has_overflow);
540 let (min, max) = type_min_max_value(clif_ty, signed);
541 let min = fx.bcx.ins().iconst(clif_ty, min);
542 let max = fx.bcx.ins().iconst(clif_ty, max);
544 let val = match (intrinsic, signed) {
545 ("saturating_add", false) => fx.bcx.ins().select(has_overflow, max, val),
546 ("saturating_sub", false) => fx.bcx.ins().select(has_overflow, min, val),
547 ("saturating_add", true) => {
548 let rhs = rhs.load_scalar(fx);
549 let rhs_ge_zero = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
550 let sat_val = fx.bcx.ins().select(rhs_ge_zero, max, min);
551 fx.bcx.ins().select(has_overflow, sat_val, val)
553 ("saturating_sub", true) => {
554 let rhs = rhs.load_scalar(fx);
555 let rhs_ge_zero = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
556 let sat_val = fx.bcx.ins().select(rhs_ge_zero, min, max);
557 fx.bcx.ins().select(has_overflow, sat_val, val)
562 let res = CValue::by_val(val, fx.layout_of(T));
564 ret.write_cvalue(fx, res);
566 rotate_left, <T>(v x, v y) {
567 let layout = fx.layout_of(T);
568 let res = fx.bcx.ins().rotl(x, y);
569 ret.write_cvalue(fx, CValue::by_val(res, layout));
571 rotate_right, <T>(v x, v y) {
572 let layout = fx.layout_of(T);
573 let res = fx.bcx.ins().rotr(x, y);
574 ret.write_cvalue(fx, CValue::by_val(res, layout));
577 // The only difference between offset and arith_offset is regarding UB. Because Cranelift
578 // doesn't have UB both are codegen'ed the same way
579 offset | arith_offset, (c base, v offset) {
580 let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
581 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
582 let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
583 let base_val = base.load_scalar(fx);
584 let res = fx.bcx.ins().iadd(base_val, ptr_diff);
585 ret.write_cvalue(fx, CValue::by_val(res, base.layout()));
588 transmute, <src_ty, dst_ty> (c from) {
589 assert_eq!(from.layout().ty, src_ty);
590 let addr = from.force_stack(fx);
591 let dst_layout = fx.layout_of(dst_ty);
592 ret.write_cvalue(fx, CValue::by_ref(addr, dst_layout))
595 let layout = ret.layout();
596 if layout.abi == Abi::Uninhabited {
597 crate::trap::trap_panic(fx, "[panic] Called intrinsic::init for uninhabited type.");
602 CPlaceInner::NoPlace => {}
603 CPlaceInner::Var(var) => {
604 let clif_ty = fx.clif_type(layout.ty).unwrap();
605 let val = match clif_ty {
606 types::I8 | types::I16 | types::I32 | types::I64 => fx.bcx.ins().iconst(clif_ty, 0),
608 let zero = fx.bcx.ins().iconst(types::I64, 0);
609 fx.bcx.ins().iconcat(zero, zero)
612 let zero = fx.bcx.ins().iconst(types::I32, 0);
613 fx.bcx.ins().bitcast(types::F32, zero)
616 let zero = fx.bcx.ins().iconst(types::I64, 0);
617 fx.bcx.ins().bitcast(types::F64, zero)
619 _ => panic!("clif_type returned {}", clif_ty),
621 fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::from_u32(var.as_u32()));
622 fx.bcx.def_var(mir_var(var), val);
625 let addr = ret.to_ptr(fx).get_addr(fx);
626 let layout = ret.layout();
627 fx.bcx.emit_small_memset(fx.module.target_config(), addr, 0, layout.size.bytes(), 1);
632 let layout = ret.layout();
633 if layout.abi == Abi::Uninhabited {
634 crate::trap::trap_panic(fx, "[panic] Called intrinsic::uninit for uninhabited type.");
638 CPlaceInner::NoPlace => {},
639 CPlaceInner::Var(var) => {
640 let clif_ty = fx.clif_type(layout.ty).unwrap();
641 let val = match clif_ty {
642 types::I8 | types::I16 | types::I32 | types::I64 => fx.bcx.ins().iconst(clif_ty, 42),
644 let zero = fx.bcx.ins().iconst(types::I64, 0);
645 let fourty_two = fx.bcx.ins().iconst(types::I64, 42);
646 fx.bcx.ins().iconcat(fourty_two, zero)
649 let zero = fx.bcx.ins().iconst(types::I32, 0xdeadbeef);
650 fx.bcx.ins().bitcast(types::F32, zero)
653 let zero = fx.bcx.ins().iconst(types::I64, 0xcafebabedeadbeefu64 as i64);
654 fx.bcx.ins().bitcast(types::F64, zero)
656 _ => panic!("clif_type returned {}", clif_ty),
658 fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::from_u32(var.as_u32()));
659 fx.bcx.def_var(mir_var(var), val);
661 CPlaceInner::Addr(_, _) => {
662 // Don't write to `ret`, as the destination memory is already uninitialized.
666 write_bytes, (c dst, v val, v count) {
667 let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
668 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
669 let count = fx.bcx.ins().imul_imm(count, pointee_size as i64);
670 let dst_ptr = dst.load_scalar(fx);
671 fx.bcx.call_memset(fx.module.target_config(), dst_ptr, val, count);
673 ctlz | ctlz_nonzero, <T> (v arg) {
674 // FIXME trap on `ctlz_nonzero` with zero arg.
675 let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
676 // FIXME verify this algorithm is correct
677 let (lsb, msb) = fx.bcx.ins().isplit(arg);
678 let lsb_lz = fx.bcx.ins().clz(lsb);
679 let msb_lz = fx.bcx.ins().clz(msb);
680 let msb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, msb, 0);
681 let lsb_lz_plus_64 = fx.bcx.ins().iadd_imm(lsb_lz, 64);
682 let res = fx.bcx.ins().select(msb_is_zero, lsb_lz_plus_64, msb_lz);
683 fx.bcx.ins().uextend(types::I128, res)
685 fx.bcx.ins().clz(arg)
687 let res = CValue::by_val(res, fx.layout_of(T));
688 ret.write_cvalue(fx, res);
690 cttz | cttz_nonzero, <T> (v arg) {
691 // FIXME trap on `cttz_nonzero` with zero arg.
692 let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
693 // FIXME verify this algorithm is correct
694 let (lsb, msb) = fx.bcx.ins().isplit(arg);
695 let lsb_tz = fx.bcx.ins().ctz(lsb);
696 let msb_tz = fx.bcx.ins().ctz(msb);
697 let lsb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, lsb, 0);
698 let msb_tz_plus_64 = fx.bcx.ins().iadd_imm(msb_tz, 64);
699 let res = fx.bcx.ins().select(lsb_is_zero, msb_tz_plus_64, lsb_tz);
700 fx.bcx.ins().uextend(types::I128, res)
702 fx.bcx.ins().ctz(arg)
704 let res = CValue::by_val(res, fx.layout_of(T));
705 ret.write_cvalue(fx, res);
708 let res = fx.bcx.ins().popcnt(arg);
709 let res = CValue::by_val(res, fx.layout_of(T));
710 ret.write_cvalue(fx, res);
712 bitreverse, <T> (v arg) {
713 let res = fx.bcx.ins().bitrev(arg);
714 let res = CValue::by_val(res, fx.layout_of(T));
715 ret.write_cvalue(fx, res);
718 // FIXME(CraneStation/cranelift#794) add bswap instruction to cranelift
719 fn swap(bcx: &mut FunctionBuilder, v: Value) -> Value {
720 match bcx.func.dfg.value_type(v) {
723 // https://code.woboq.org/gcc/include/bits/byteswap.h.html
725 let tmp1 = bcx.ins().ishl_imm(v, 8);
726 let n1 = bcx.ins().band_imm(tmp1, 0xFF00);
728 let tmp2 = bcx.ins().ushr_imm(v, 8);
729 let n2 = bcx.ins().band_imm(tmp2, 0x00FF);
731 bcx.ins().bor(n1, n2)
734 let tmp1 = bcx.ins().ishl_imm(v, 24);
735 let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000);
737 let tmp2 = bcx.ins().ishl_imm(v, 8);
738 let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000);
740 let tmp3 = bcx.ins().ushr_imm(v, 8);
741 let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00);
743 let tmp4 = bcx.ins().ushr_imm(v, 24);
744 let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF);
746 let or_tmp1 = bcx.ins().bor(n1, n2);
747 let or_tmp2 = bcx.ins().bor(n3, n4);
748 bcx.ins().bor(or_tmp1, or_tmp2)
751 let tmp1 = bcx.ins().ishl_imm(v, 56);
752 let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000_0000_0000u64 as i64);
754 let tmp2 = bcx.ins().ishl_imm(v, 40);
755 let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000_0000_0000u64 as i64);
757 let tmp3 = bcx.ins().ishl_imm(v, 24);
758 let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00_0000_0000u64 as i64);
760 let tmp4 = bcx.ins().ishl_imm(v, 8);
761 let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF_0000_0000u64 as i64);
763 let tmp5 = bcx.ins().ushr_imm(v, 8);
764 let n5 = bcx.ins().band_imm(tmp5, 0x0000_0000_FF00_0000u64 as i64);
766 let tmp6 = bcx.ins().ushr_imm(v, 24);
767 let n6 = bcx.ins().band_imm(tmp6, 0x0000_0000_00FF_0000u64 as i64);
769 let tmp7 = bcx.ins().ushr_imm(v, 40);
770 let n7 = bcx.ins().band_imm(tmp7, 0x0000_0000_0000_FF00u64 as i64);
772 let tmp8 = bcx.ins().ushr_imm(v, 56);
773 let n8 = bcx.ins().band_imm(tmp8, 0x0000_0000_0000_00FFu64 as i64);
775 let or_tmp1 = bcx.ins().bor(n1, n2);
776 let or_tmp2 = bcx.ins().bor(n3, n4);
777 let or_tmp3 = bcx.ins().bor(n5, n6);
778 let or_tmp4 = bcx.ins().bor(n7, n8);
780 let or_tmp5 = bcx.ins().bor(or_tmp1, or_tmp2);
781 let or_tmp6 = bcx.ins().bor(or_tmp3, or_tmp4);
782 bcx.ins().bor(or_tmp5, or_tmp6)
785 let (lo, hi) = bcx.ins().isplit(v);
786 let lo = swap(bcx, lo);
787 let hi = swap(bcx, hi);
788 bcx.ins().iconcat(hi, lo)
790 ty => unimplemented!("bswap {}", ty),
793 let res = CValue::by_val(swap(&mut fx.bcx, arg), fx.layout_of(T));
794 ret.write_cvalue(fx, res);
796 panic_if_uninhabited, <T> () {
797 if fx.layout_of(T).abi.is_uninhabited() {
798 crate::trap::trap_panic(fx, "[panic] Called intrinsic::panic_if_uninhabited for uninhabited type.");
803 volatile_load, (c ptr) {
804 // Cranelift treats loads as volatile by default
806 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
807 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
808 ret.write_cvalue(fx, val);
810 volatile_store, (v ptr, c val) {
811 // Cranelift treats stores as volatile by default
812 let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
813 dest.write_cvalue(fx, val);
816 size_of | pref_align_of | min_align_of | needs_drop | type_id | type_name, () {
818 fx.tcx.const_eval_instance(ParamEnv::reveal_all(), instance, None).unwrap();
819 let val = crate::constant::trans_const_value(fx, const_val);
820 ret.write_cvalue(fx, val);
823 ptr_offset_from, <T> (v ptr, v base) {
824 let isize_layout = fx.layout_of(fx.tcx.types.isize);
826 let pointee_size: u64 = fx.layout_of(T).size.bytes();
827 let diff = fx.bcx.ins().isub(ptr, base);
828 // FIXME this can be an exact division.
829 let val = CValue::by_val(fx.bcx.ins().udiv_imm(diff, pointee_size as i64), isize_layout);
830 ret.write_cvalue(fx, val);
833 caller_location, () {
834 let caller_location = fx.get_caller_location(span);
835 ret.write_cvalue(fx, caller_location);
838 _ if intrinsic.starts_with("atomic_fence"), () {};
839 _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
840 _ if intrinsic.starts_with("atomic_load"), (c ptr) {
842 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
843 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
844 ret.write_cvalue(fx, val);
846 _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
847 let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
848 dest.write_cvalue(fx, val);
850 _ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
852 let clif_ty = fx.clif_type(T).unwrap();
853 let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
854 ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
857 let dest = CPlace::for_ptr(Pointer::new(ptr), src.layout());
858 dest.write_cvalue(fx, src);
860 _ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*
862 let clif_ty = fx.clif_type(T).unwrap();
863 let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
866 let is_eq = codegen_icmp(fx, IntCC::Equal, old, test_old);
867 let new = fx.bcx.ins().select(is_eq, new, old); // Keep old if not equal to test_old
870 fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
872 let ret_val = CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
873 ret.write_cvalue(fx, ret_val);
876 _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
877 atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
879 _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
880 atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
882 _ if intrinsic.starts_with("atomic_and"), <T> (v ptr, v src) {
883 atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
885 _ if intrinsic.starts_with("atomic_nand"), <T> (v ptr, v src) {
886 let clif_ty = fx.clif_type(T).unwrap();
887 let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
888 let and = fx.bcx.ins().band(old, src);
889 let new = fx.bcx.ins().bnot(and);
890 fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
891 ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
893 _ if intrinsic.starts_with("atomic_or"), <T> (v ptr, v src) {
894 atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
896 _ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, v src) {
897 atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
900 _ if intrinsic.starts_with("atomic_max"), <T> (v ptr, v src) {
901 atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
903 _ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, v src) {
904 atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
906 _ if intrinsic.starts_with("atomic_min"), <T> (v ptr, v src) {
907 atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
909 _ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, v src) {
910 atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
913 minnumf32, (v a, v b) {
914 let val = fx.bcx.ins().fmin(a, b);
915 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
916 ret.write_cvalue(fx, val);
918 minnumf64, (v a, v b) {
919 let val = fx.bcx.ins().fmin(a, b);
920 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
921 ret.write_cvalue(fx, val);
923 maxnumf32, (v a, v b) {
924 let val = fx.bcx.ins().fmax(a, b);
925 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
926 ret.write_cvalue(fx, val);
928 maxnumf64, (v a, v b) {
929 let val = fx.bcx.ins().fmax(a, b);
930 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
931 ret.write_cvalue(fx, val);
934 try, (v f, v data, v _local_ptr) {
935 // FIXME once unwinding is supported, change this to actually catch panics
936 let f_sig = fx.bcx.func.import_signature(Signature {
937 call_conv: CallConv::triple_default(fx.triple()),
938 params: vec![AbiParam::new(fx.bcx.func.dfg.value_type(data))],
942 fx.bcx.ins().call_indirect(f_sig, f, &[data]);
944 let ret_val = CValue::const_val(fx, ret.layout().ty, 0);
945 ret.write_cvalue(fx, ret_val);
949 if let Some((_, dest)) = destination {
950 let ret_ebb = fx.get_ebb(dest);
951 fx.bcx.ins().jump(ret_ebb, &[]);
953 trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");