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>,
151 &mut FunctionCx<'_, 'tcx, B>,
157 let layout = val.layout();
159 let (lane_layout, lane_count) = lane_type_and_count(fx.tcx, layout);
160 let (ret_lane_layout, ret_lane_count) = lane_type_and_count(fx.tcx, ret.layout());
161 assert_eq!(lane_count, ret_lane_count);
163 for lane_idx in 0..lane_count {
164 let lane_idx = mir::Field::new(lane_idx.try_into().unwrap());
165 let lane = val.value_field(fx, lane_idx).load_scalar(fx);
167 let res_lane = f(fx, lane_layout, ret_lane_layout, lane);
169 ret.place_field(fx, lane_idx).write_cvalue(fx, res_lane);
173 fn simd_pair_for_each_lane<'tcx, B: Backend>(
174 fx: &mut FunctionCx<'_, 'tcx, B>,
179 &mut FunctionCx<'_, 'tcx, B>,
186 assert_eq!(x.layout(), y.layout());
187 let layout = x.layout();
189 let (lane_layout, lane_count) = lane_type_and_count(fx.tcx, layout);
190 let (ret_lane_layout, ret_lane_count) = lane_type_and_count(fx.tcx, ret.layout());
191 assert_eq!(lane_count, ret_lane_count);
193 for lane in 0..lane_count {
194 let lane = mir::Field::new(lane.try_into().unwrap());
195 let x_lane = x.value_field(fx, lane).load_scalar(fx);
196 let y_lane = y.value_field(fx, lane).load_scalar(fx);
198 let res_lane = f(fx, lane_layout, ret_lane_layout, x_lane, y_lane);
200 ret.place_field(fx, lane).write_cvalue(fx, res_lane);
204 fn bool_to_zero_or_max_uint<'tcx>(
205 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
206 layout: TyLayout<'tcx>,
209 let ty = fx.clif_type(layout.ty).unwrap();
211 let int_ty = match ty {
212 types::F32 => types::I32,
213 types::F64 => types::I64,
217 let zero = fx.bcx.ins().iconst(int_ty, 0);
221 .iconst(int_ty, (u64::max_value() >> (64 - int_ty.bits())) as i64);
222 let mut res = fx.bcx.ins().select(val, max, zero);
225 res = fx.bcx.ins().bitcast(ty, res);
228 CValue::by_val(res, layout)
232 ($fx:expr, $cc:ident($x:ident, $y:ident) -> $ret:ident) => {
233 simd_pair_for_each_lane(
238 |fx, lane_layout, res_lane_layout, x_lane, y_lane| {
239 let res_lane = match lane_layout.ty.kind {
240 ty::Uint(_) | ty::Int(_) => codegen_icmp(fx, IntCC::$cc, x_lane, y_lane),
241 _ => unreachable!("{:?}", lane_layout.ty),
243 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
247 ($fx:expr, $cc_u:ident|$cc_s:ident($x:ident, $y:ident) -> $ret:ident) => {
248 simd_pair_for_each_lane(
253 |fx, lane_layout, res_lane_layout, x_lane, y_lane| {
254 let res_lane = match lane_layout.ty.kind {
255 ty::Uint(_) => codegen_icmp(fx, IntCC::$cc_u, x_lane, y_lane),
256 ty::Int(_) => codegen_icmp(fx, IntCC::$cc_s, x_lane, y_lane),
257 _ => unreachable!("{:?}", lane_layout.ty),
259 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
265 macro simd_int_binop {
266 ($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) => {
267 simd_int_binop!($fx, $op|$op($x, $y) -> $ret);
269 ($fx:expr, $op_u:ident|$op_s:ident($x:ident, $y:ident) -> $ret:ident) => {
270 let (lane_layout, lane_count) = lane_type_and_count($fx.tcx, $x.layout());
271 let x_val = $x.load_scalar($fx);
272 let y_val = $y.load_scalar($fx);
274 let res = match lane_layout.ty.kind {
275 ty::Uint(_) => $fx.bcx.ins().$op_u(x_val, y_val),
276 ty::Int(_) => $fx.bcx.ins().$op_s(x_val, y_val),
277 _ => unreachable!("{:?}", lane_layout.ty),
279 $ret.write_cvalue($fx, CValue::by_val(res, $ret.layout()));
283 macro simd_int_flt_binop {
284 ($fx:expr, $op:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
285 simd_int_flt_binop!($fx, $op|$op|$op_f($x, $y) -> $ret);
287 ($fx:expr, $op_u:ident|$op_s:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
288 let (lane_layout, lane_count) = lane_type_and_count($fx.tcx, $x.layout());
289 let x_val = $x.load_scalar($fx);
290 let y_val = $y.load_scalar($fx);
292 let res = match lane_layout.ty.kind {
293 ty::Uint(_) => $fx.bcx.ins().$op_u(x_val, y_val),
294 ty::Int(_) => $fx.bcx.ins().$op_s(x_val, y_val),
295 ty::Float(_) => $fx.bcx.ins().$op_f(x_val, y_val),
296 _ => unreachable!("{:?}", lane_layout.ty),
298 $ret.write_cvalue($fx, CValue::by_val(res, $ret.layout()));
302 macro simd_flt_binop($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) {
303 let (lane_layout, lane_count) = lane_type_and_count($fx.tcx, $x.layout());
304 let x_val = $x.load_scalar($fx);
305 let y_val = $y.load_scalar($fx);
307 let res = match lane_layout.ty.kind {
308 ty::Float(_) => $fx.bcx.ins().$op(x_val, y_val),
309 _ => unreachable!("{:?}", lane_layout.ty),
311 $ret.write_cvalue($fx, CValue::by_val(res, $ret.layout()));
314 pub fn codegen_intrinsic_call<'tcx>(
315 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
316 instance: Instance<'tcx>,
317 args: &[mir::Operand<'tcx>],
318 destination: Option<(CPlace<'tcx>, BasicBlock)>,
321 let def_id = instance.def_id();
322 let substs = instance.substs;
324 let intrinsic = fx.tcx.item_name(def_id).as_str();
325 let intrinsic = &intrinsic[..];
327 let ret = match destination {
328 Some((place, _)) => place,
330 // Insert non returning intrinsics here
333 trap_panic(fx, "Called intrinsic::abort.");
336 trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
341 "[corruption] Called intrinsic::transmute with uninhabited argument.",
344 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
350 if intrinsic.starts_with("simd_") {
351 self::simd::codegen_simd_intrinsic_call(fx, instance, args, ret, span);
352 let ret_ebb = fx.get_ebb(destination.expect("SIMD intrinsics don't diverge").1);
353 fx.bcx.ins().jump(ret_ebb, &[]);
357 let usize_layout = fx.layout_of(fx.tcx.types.usize);
359 call_intrinsic_match! {
360 fx, intrinsic, substs, ret, destination, args,
361 expf32(flt) -> f32 => expf,
362 expf64(flt) -> f64 => exp,
363 exp2f32(flt) -> f32 => exp2f,
364 exp2f64(flt) -> f64 => exp2,
365 sqrtf32(flt) -> f32 => sqrtf,
366 sqrtf64(flt) -> f64 => sqrt,
367 powif32(a, x) -> f32 => __powisf2, // compiler-builtins
368 powif64(a, x) -> f64 => __powidf2, // compiler-builtins
369 powf32(a, x) -> f32 => powf,
370 powf64(a, x) -> f64 => pow,
371 logf32(flt) -> f32 => logf,
372 logf64(flt) -> f64 => log,
373 log2f32(flt) -> f32 => log2f,
374 log2f64(flt) -> f64 => log2,
375 log10f32(flt) -> f32 => log10f,
376 log10f64(flt) -> f64 => log10,
377 fabsf32(flt) -> f32 => fabsf,
378 fabsf64(flt) -> f64 => fabs,
379 fmaf32(x, y, z) -> f32 => fmaf,
380 fmaf64(x, y, z) -> f64 => fma,
381 copysignf32(x, y) -> f32 => copysignf,
382 copysignf64(x, y) -> f64 => copysign,
385 // FIXME use clif insts
386 floorf32(flt) -> f32 => floorf,
387 floorf64(flt) -> f64 => floor,
388 ceilf32(flt) -> f32 => ceilf,
389 ceilf64(flt) -> f64 => ceil,
390 truncf32(flt) -> f32 => truncf,
391 truncf64(flt) -> f64 => trunc,
392 roundf32(flt) -> f32 => roundf,
393 roundf64(flt) -> f64 => round,
396 sinf32(flt) -> f32 => sinf,
397 sinf64(flt) -> f64 => sin,
398 cosf32(flt) -> f32 => cosf,
399 cosf64(flt) -> f64 => cos,
400 tanf32(flt) -> f32 => tanf,
401 tanf64(flt) -> f64 => tan,
405 fx, intrinsic, substs, args,
407 unimpl!("unsupported intrinsic {}", intrinsic)
411 likely | unlikely, (c a) {
412 ret.write_cvalue(fx, a);
415 fx.bcx.ins().debugtrap();
417 copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
418 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
422 .iconst(fx.pointer_type, elem_size as i64);
423 assert_eq!(args.len(), 3);
424 let byte_amount = fx.bcx.ins().imul(count, elem_size);
426 if intrinsic.ends_with("_nonoverlapping") {
427 fx.bcx.call_memcpy(fx.module.target_config(), dst, src, byte_amount);
429 fx.bcx.call_memmove(fx.module.target_config(), dst, src, byte_amount);
432 discriminant_value, (c ptr) {
433 let pointee_layout = fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
434 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), pointee_layout);
435 let discr = crate::discriminant::codegen_get_discriminant(fx, val, ret.layout());
436 ret.write_cvalue(fx, discr);
438 size_of_val, <T> (c ptr) {
439 let layout = fx.layout_of(T);
440 let size = if layout.is_unsized() {
441 let (_ptr, info) = ptr.load_scalar_pair(fx);
442 let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
448 .iconst(fx.pointer_type, layout.size.bytes() as i64)
450 ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
452 min_align_of_val, <T> (c ptr) {
453 let layout = fx.layout_of(T);
454 let align = if layout.is_unsized() {
455 let (_ptr, info) = ptr.load_scalar_pair(fx);
456 let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
462 .iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
464 ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
467 _ if intrinsic.starts_with("unchecked_") || intrinsic == "exact_div", (c x, c y) {
468 // FIXME trap on overflow
469 let bin_op = match intrinsic {
470 "unchecked_sub" => BinOp::Sub,
471 "unchecked_div" | "exact_div" => BinOp::Div,
472 "unchecked_rem" => BinOp::Rem,
473 "unchecked_shl" => BinOp::Shl,
474 "unchecked_shr" => BinOp::Shr,
475 _ => unimplemented!("intrinsic {}", intrinsic),
477 let res = crate::num::trans_int_binop(fx, bin_op, x, y);
478 ret.write_cvalue(fx, res);
480 _ if intrinsic.ends_with("_with_overflow"), (c x, c y) {
481 assert_eq!(x.layout().ty, y.layout().ty);
482 let bin_op = match intrinsic {
483 "add_with_overflow" => BinOp::Add,
484 "sub_with_overflow" => BinOp::Sub,
485 "mul_with_overflow" => BinOp::Mul,
486 _ => unimplemented!("intrinsic {}", intrinsic),
489 let res = crate::num::trans_checked_int_binop(
495 ret.write_cvalue(fx, res);
497 _ if intrinsic.starts_with("wrapping_"), (c x, c y) {
498 assert_eq!(x.layout().ty, y.layout().ty);
499 let bin_op = match intrinsic {
500 "wrapping_add" => BinOp::Add,
501 "wrapping_sub" => BinOp::Sub,
502 "wrapping_mul" => BinOp::Mul,
503 _ => unimplemented!("intrinsic {}", intrinsic),
505 let res = crate::num::trans_int_binop(
511 ret.write_cvalue(fx, res);
513 _ if intrinsic.starts_with("saturating_"), <T> (c lhs, c rhs) {
514 assert_eq!(lhs.layout().ty, rhs.layout().ty);
515 let bin_op = match intrinsic {
516 "saturating_add" => BinOp::Add,
517 "saturating_sub" => BinOp::Sub,
518 _ => unimplemented!("intrinsic {}", intrinsic),
521 let signed = type_sign(T);
523 let checked_res = crate::num::trans_checked_int_binop(
530 let (val, has_overflow) = checked_res.load_scalar_pair(fx);
531 let clif_ty = fx.clif_type(T).unwrap();
533 // `select.i8` is not implemented by Cranelift.
534 let has_overflow = fx.bcx.ins().uextend(types::I32, has_overflow);
536 let (min, max) = type_min_max_value(clif_ty, signed);
537 let min = fx.bcx.ins().iconst(clif_ty, min);
538 let max = fx.bcx.ins().iconst(clif_ty, max);
540 let val = match (intrinsic, signed) {
541 ("saturating_add", false) => fx.bcx.ins().select(has_overflow, max, val),
542 ("saturating_sub", false) => fx.bcx.ins().select(has_overflow, min, val),
543 ("saturating_add", true) => {
544 let rhs = rhs.load_scalar(fx);
545 let rhs_ge_zero = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
546 let sat_val = fx.bcx.ins().select(rhs_ge_zero, max, min);
547 fx.bcx.ins().select(has_overflow, sat_val, val)
549 ("saturating_sub", true) => {
550 let rhs = rhs.load_scalar(fx);
551 let rhs_ge_zero = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
552 let sat_val = fx.bcx.ins().select(rhs_ge_zero, min, max);
553 fx.bcx.ins().select(has_overflow, sat_val, val)
558 let res = CValue::by_val(val, fx.layout_of(T));
560 ret.write_cvalue(fx, res);
562 rotate_left, <T>(v x, v y) {
563 let layout = fx.layout_of(T);
564 let res = fx.bcx.ins().rotl(x, y);
565 ret.write_cvalue(fx, CValue::by_val(res, layout));
567 rotate_right, <T>(v x, v y) {
568 let layout = fx.layout_of(T);
569 let res = fx.bcx.ins().rotr(x, y);
570 ret.write_cvalue(fx, CValue::by_val(res, layout));
573 // The only difference between offset and arith_offset is regarding UB. Because Cranelift
574 // doesn't have UB both are codegen'ed the same way
575 offset | arith_offset, (c base, v offset) {
576 let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
577 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
578 let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
579 let base_val = base.load_scalar(fx);
580 let res = fx.bcx.ins().iadd(base_val, ptr_diff);
581 ret.write_cvalue(fx, CValue::by_val(res, base.layout()));
584 transmute, <src_ty, dst_ty> (c from) {
585 assert_eq!(from.layout().ty, src_ty);
586 let addr = from.force_stack(fx);
587 let dst_layout = fx.layout_of(dst_ty);
588 ret.write_cvalue(fx, CValue::by_ref(addr, dst_layout))
591 let layout = ret.layout();
592 if layout.abi == Abi::Uninhabited {
593 crate::trap::trap_panic(fx, "[panic] Called intrinsic::init for uninhabited type.");
598 CPlaceInner::NoPlace => {}
599 CPlaceInner::Var(var) => {
600 let clif_ty = fx.clif_type(layout.ty).unwrap();
601 let val = match clif_ty {
602 types::I8 | types::I16 | types::I32 | types::I64 => fx.bcx.ins().iconst(clif_ty, 0),
604 let zero = fx.bcx.ins().iconst(types::I64, 0);
605 fx.bcx.ins().iconcat(zero, zero)
608 let zero = fx.bcx.ins().iconst(types::I32, 0);
609 fx.bcx.ins().bitcast(types::F32, zero)
612 let zero = fx.bcx.ins().iconst(types::I64, 0);
613 fx.bcx.ins().bitcast(types::F64, zero)
615 _ => panic!("clif_type returned {}", clif_ty),
617 fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::from_u32(var.as_u32()));
618 fx.bcx.def_var(mir_var(var), val);
621 let addr = ret.to_ptr(fx).get_addr(fx);
622 let layout = ret.layout();
623 fx.bcx.emit_small_memset(fx.module.target_config(), addr, 0, layout.size.bytes(), 1);
628 let layout = ret.layout();
629 if layout.abi == Abi::Uninhabited {
630 crate::trap::trap_panic(fx, "[panic] Called intrinsic::uninit for uninhabited type.");
634 CPlaceInner::NoPlace => {},
635 CPlaceInner::Var(var) => {
636 let clif_ty = fx.clif_type(layout.ty).unwrap();
637 let val = match clif_ty {
638 types::I8 | types::I16 | types::I32 | types::I64 => fx.bcx.ins().iconst(clif_ty, 42),
640 let zero = fx.bcx.ins().iconst(types::I64, 0);
641 let fourty_two = fx.bcx.ins().iconst(types::I64, 42);
642 fx.bcx.ins().iconcat(fourty_two, zero)
645 let zero = fx.bcx.ins().iconst(types::I32, 0xdeadbeef);
646 fx.bcx.ins().bitcast(types::F32, zero)
649 let zero = fx.bcx.ins().iconst(types::I64, 0xcafebabedeadbeefu64 as i64);
650 fx.bcx.ins().bitcast(types::F64, zero)
652 _ => panic!("clif_type returned {}", clif_ty),
654 fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::from_u32(var.as_u32()));
655 fx.bcx.def_var(mir_var(var), val);
657 CPlaceInner::Addr(_, _) => {
658 // Don't write to `ret`, as the destination memory is already uninitialized.
662 write_bytes, (c dst, v val, v count) {
663 let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
664 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
665 let count = fx.bcx.ins().imul_imm(count, pointee_size as i64);
666 let dst_ptr = dst.load_scalar(fx);
667 fx.bcx.call_memset(fx.module.target_config(), dst_ptr, val, count);
669 ctlz | ctlz_nonzero, <T> (v arg) {
670 // FIXME trap on `ctlz_nonzero` with zero arg.
671 let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
672 // FIXME verify this algorithm is correct
673 let (lsb, msb) = fx.bcx.ins().isplit(arg);
674 let lsb_lz = fx.bcx.ins().clz(lsb);
675 let msb_lz = fx.bcx.ins().clz(msb);
676 let msb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, msb, 0);
677 let lsb_lz_plus_64 = fx.bcx.ins().iadd_imm(lsb_lz, 64);
678 let res = fx.bcx.ins().select(msb_is_zero, lsb_lz_plus_64, msb_lz);
679 fx.bcx.ins().uextend(types::I128, res)
681 fx.bcx.ins().clz(arg)
683 let res = CValue::by_val(res, fx.layout_of(T));
684 ret.write_cvalue(fx, res);
686 cttz | cttz_nonzero, <T> (v arg) {
687 // FIXME trap on `cttz_nonzero` with zero arg.
688 let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
689 // FIXME verify this algorithm is correct
690 let (lsb, msb) = fx.bcx.ins().isplit(arg);
691 let lsb_tz = fx.bcx.ins().ctz(lsb);
692 let msb_tz = fx.bcx.ins().ctz(msb);
693 let lsb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, lsb, 0);
694 let msb_tz_plus_64 = fx.bcx.ins().iadd_imm(msb_tz, 64);
695 let res = fx.bcx.ins().select(lsb_is_zero, msb_tz_plus_64, lsb_tz);
696 fx.bcx.ins().uextend(types::I128, res)
698 fx.bcx.ins().ctz(arg)
700 let res = CValue::by_val(res, fx.layout_of(T));
701 ret.write_cvalue(fx, res);
704 let res = fx.bcx.ins().popcnt(arg);
705 let res = CValue::by_val(res, fx.layout_of(T));
706 ret.write_cvalue(fx, res);
708 bitreverse, <T> (v arg) {
709 let res = fx.bcx.ins().bitrev(arg);
710 let res = CValue::by_val(res, fx.layout_of(T));
711 ret.write_cvalue(fx, res);
714 // FIXME(CraneStation/cranelift#794) add bswap instruction to cranelift
715 fn swap(bcx: &mut FunctionBuilder, v: Value) -> Value {
716 match bcx.func.dfg.value_type(v) {
719 // https://code.woboq.org/gcc/include/bits/byteswap.h.html
721 let tmp1 = bcx.ins().ishl_imm(v, 8);
722 let n1 = bcx.ins().band_imm(tmp1, 0xFF00);
724 let tmp2 = bcx.ins().ushr_imm(v, 8);
725 let n2 = bcx.ins().band_imm(tmp2, 0x00FF);
727 bcx.ins().bor(n1, n2)
730 let tmp1 = bcx.ins().ishl_imm(v, 24);
731 let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000);
733 let tmp2 = bcx.ins().ishl_imm(v, 8);
734 let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000);
736 let tmp3 = bcx.ins().ushr_imm(v, 8);
737 let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00);
739 let tmp4 = bcx.ins().ushr_imm(v, 24);
740 let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF);
742 let or_tmp1 = bcx.ins().bor(n1, n2);
743 let or_tmp2 = bcx.ins().bor(n3, n4);
744 bcx.ins().bor(or_tmp1, or_tmp2)
747 let tmp1 = bcx.ins().ishl_imm(v, 56);
748 let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000_0000_0000u64 as i64);
750 let tmp2 = bcx.ins().ishl_imm(v, 40);
751 let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000_0000_0000u64 as i64);
753 let tmp3 = bcx.ins().ishl_imm(v, 24);
754 let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00_0000_0000u64 as i64);
756 let tmp4 = bcx.ins().ishl_imm(v, 8);
757 let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF_0000_0000u64 as i64);
759 let tmp5 = bcx.ins().ushr_imm(v, 8);
760 let n5 = bcx.ins().band_imm(tmp5, 0x0000_0000_FF00_0000u64 as i64);
762 let tmp6 = bcx.ins().ushr_imm(v, 24);
763 let n6 = bcx.ins().band_imm(tmp6, 0x0000_0000_00FF_0000u64 as i64);
765 let tmp7 = bcx.ins().ushr_imm(v, 40);
766 let n7 = bcx.ins().band_imm(tmp7, 0x0000_0000_0000_FF00u64 as i64);
768 let tmp8 = bcx.ins().ushr_imm(v, 56);
769 let n8 = bcx.ins().band_imm(tmp8, 0x0000_0000_0000_00FFu64 as i64);
771 let or_tmp1 = bcx.ins().bor(n1, n2);
772 let or_tmp2 = bcx.ins().bor(n3, n4);
773 let or_tmp3 = bcx.ins().bor(n5, n6);
774 let or_tmp4 = bcx.ins().bor(n7, n8);
776 let or_tmp5 = bcx.ins().bor(or_tmp1, or_tmp2);
777 let or_tmp6 = bcx.ins().bor(or_tmp3, or_tmp4);
778 bcx.ins().bor(or_tmp5, or_tmp6)
781 let (lo, hi) = bcx.ins().isplit(v);
782 let lo = swap(bcx, lo);
783 let hi = swap(bcx, hi);
784 bcx.ins().iconcat(hi, lo)
786 ty => unimplemented!("bswap {}", ty),
789 let res = CValue::by_val(swap(&mut fx.bcx, arg), fx.layout_of(T));
790 ret.write_cvalue(fx, res);
792 panic_if_uninhabited, <T> () {
793 if fx.layout_of(T).abi.is_uninhabited() {
794 crate::trap::trap_panic(fx, "[panic] Called intrinsic::panic_if_uninhabited for uninhabited type.");
799 volatile_load, (c ptr) {
800 // Cranelift treats loads as volatile by default
802 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
803 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
804 ret.write_cvalue(fx, val);
806 volatile_store, (v ptr, c val) {
807 // Cranelift treats stores as volatile by default
808 let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
809 dest.write_cvalue(fx, val);
812 size_of | pref_align_of | min_align_of | needs_drop | type_id | type_name, () {
814 fx.tcx.const_eval_instance(ParamEnv::reveal_all(), instance, None).unwrap();
815 let val = crate::constant::trans_const_value(fx, const_val);
816 ret.write_cvalue(fx, val);
819 ptr_offset_from, <T> (v ptr, v base) {
820 let isize_layout = fx.layout_of(fx.tcx.types.isize);
822 let pointee_size: u64 = fx.layout_of(T).size.bytes();
823 let diff = fx.bcx.ins().isub(ptr, base);
824 // FIXME this can be an exact division.
825 let val = CValue::by_val(fx.bcx.ins().udiv_imm(diff, pointee_size as i64), isize_layout);
826 ret.write_cvalue(fx, val);
829 caller_location, () {
830 let caller_location = fx.get_caller_location(span);
831 ret.write_cvalue(fx, caller_location);
834 _ if intrinsic.starts_with("atomic_fence"), () {};
835 _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
836 _ if intrinsic.starts_with("atomic_load"), (c ptr) {
838 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
839 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
840 ret.write_cvalue(fx, val);
842 _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
843 let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
844 dest.write_cvalue(fx, val);
846 _ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
848 let clif_ty = fx.clif_type(T).unwrap();
849 let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
850 ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
853 let dest = CPlace::for_ptr(Pointer::new(ptr), src.layout());
854 dest.write_cvalue(fx, src);
856 _ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*
858 let clif_ty = fx.clif_type(T).unwrap();
859 let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
862 let is_eq = codegen_icmp(fx, IntCC::Equal, old, test_old);
863 let new = fx.bcx.ins().select(is_eq, new, old); // Keep old if not equal to test_old
866 fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
868 let ret_val = CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
869 ret.write_cvalue(fx, ret_val);
872 _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
873 atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
875 _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
876 atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
878 _ if intrinsic.starts_with("atomic_and"), <T> (v ptr, v src) {
879 atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
881 _ if intrinsic.starts_with("atomic_nand"), <T> (v ptr, v src) {
882 let clif_ty = fx.clif_type(T).unwrap();
883 let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
884 let and = fx.bcx.ins().band(old, src);
885 let new = fx.bcx.ins().bnot(and);
886 fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
887 ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
889 _ if intrinsic.starts_with("atomic_or"), <T> (v ptr, v src) {
890 atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
892 _ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, v src) {
893 atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
896 _ if intrinsic.starts_with("atomic_max"), <T> (v ptr, v src) {
897 atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
899 _ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, v src) {
900 atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
902 _ if intrinsic.starts_with("atomic_min"), <T> (v ptr, v src) {
903 atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
905 _ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, v src) {
906 atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
909 minnumf32, (v a, v b) {
910 let val = fx.bcx.ins().fmin(a, b);
911 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
912 ret.write_cvalue(fx, val);
914 minnumf64, (v a, v b) {
915 let val = fx.bcx.ins().fmin(a, b);
916 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
917 ret.write_cvalue(fx, val);
919 maxnumf32, (v a, v b) {
920 let val = fx.bcx.ins().fmax(a, b);
921 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
922 ret.write_cvalue(fx, val);
924 maxnumf64, (v a, v b) {
925 let val = fx.bcx.ins().fmax(a, b);
926 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
927 ret.write_cvalue(fx, val);
930 try, (v f, v data, v _local_ptr) {
931 // FIXME once unwinding is supported, change this to actually catch panics
932 let f_sig = fx.bcx.func.import_signature(Signature {
933 call_conv: CallConv::triple_default(fx.triple()),
934 params: vec![AbiParam::new(fx.bcx.func.dfg.value_type(data))],
938 fx.bcx.ins().call_indirect(f_sig, f, &[data]);
940 let ret_val = CValue::const_val(fx, ret.layout().ty, 0);
941 ret.write_cvalue(fx, ret_val);
945 if let Some((_, dest)) = destination {
946 let ret_ebb = fx.get_ebb(dest);
947 fx.bcx.ins().jump(ret_ebb, &[]);
949 trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");