1 //! Codegen of intrinsics. This includes `extern "rust-intrinsic"`, `extern "platform-intrinsic"`
2 //! and LLVM intrinsics that have symbol names starting with `llvm.`.
4 macro_rules! intrinsic_args {
5 ($fx:expr, $args:expr => ($($arg:tt),*); $intrinsic:expr) => {
6 #[allow(unused_parens)]
7 let ($($arg),*) = if let [$($arg),*] = $args {
8 ($(codegen_operand($fx, $arg)),*)
10 $crate::intrinsics::bug_on_incorrect_arg_count($intrinsic);
19 pub(crate) use cpuid::codegen_cpuid_call;
20 pub(crate) use llvm::codegen_llvm_intrinsic_call;
22 use rustc_middle::ty::print::with_no_trimmed_paths;
23 use rustc_middle::ty::subst::SubstsRef;
24 use rustc_span::symbol::{kw, sym, Symbol};
26 use crate::prelude::*;
27 use cranelift_codegen::ir::AtomicRmwOp;
29 fn bug_on_incorrect_arg_count(intrinsic: impl std::fmt::Display) -> ! {
30 bug!("wrong number of args for intrinsic {}", intrinsic);
33 fn report_atomic_type_validation_error<'tcx>(
34 fx: &mut FunctionCx<'_, '_, 'tcx>,
42 "`{}` intrinsic: expected basic integer or raw pointer type, found `{:?}`",
46 // Prevent verifier error
47 crate::trap::trap_unreachable(fx, "compilation should not have succeeded");
50 pub(crate) fn clif_vector_type<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx>) -> Option<Type> {
51 let (element, count) = match layout.abi {
52 Abi::Vector { element, count } => (element, count),
56 match scalar_to_clif_type(tcx, element).by(u16::try_from(count).unwrap()) {
57 // Cranelift currently only implements icmp for 128bit vectors.
58 Some(vector_ty) if vector_ty.bits() == 128 => Some(vector_ty),
63 fn simd_for_each_lane<'tcx>(
64 fx: &mut FunctionCx<'_, '_, 'tcx>,
67 f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value) -> Value,
69 let layout = val.layout();
71 let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx);
72 let lane_layout = fx.layout_of(lane_ty);
73 let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx);
74 let ret_lane_layout = fx.layout_of(ret_lane_ty);
75 assert_eq!(lane_count, ret_lane_count);
77 for lane_idx in 0..lane_count {
78 let lane = val.value_lane(fx, lane_idx).load_scalar(fx);
80 let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, lane);
81 let res_lane = CValue::by_val(res_lane, ret_lane_layout);
83 ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane);
87 fn simd_pair_for_each_lane<'tcx>(
88 fx: &mut FunctionCx<'_, '_, 'tcx>,
92 f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value, Value) -> Value,
94 assert_eq!(x.layout(), y.layout());
95 let layout = x.layout();
97 let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx);
98 let lane_layout = fx.layout_of(lane_ty);
99 let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx);
100 let ret_lane_layout = fx.layout_of(ret_lane_ty);
101 assert_eq!(lane_count, ret_lane_count);
103 for lane_idx in 0..lane_count {
104 let x_lane = x.value_lane(fx, lane_idx).load_scalar(fx);
105 let y_lane = y.value_lane(fx, lane_idx).load_scalar(fx);
107 let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, x_lane, y_lane);
108 let res_lane = CValue::by_val(res_lane, ret_lane_layout);
110 ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane);
114 fn simd_reduce<'tcx>(
115 fx: &mut FunctionCx<'_, '_, 'tcx>,
119 f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Value, Value) -> Value,
121 let (lane_count, lane_ty) = val.layout().ty.simd_size_and_type(fx.tcx);
122 let lane_layout = fx.layout_of(lane_ty);
123 assert_eq!(lane_layout, ret.layout());
125 let (mut res_val, start_lane) =
126 if let Some(acc) = acc { (acc, 0) } else { (val.value_lane(fx, 0).load_scalar(fx), 1) };
127 for lane_idx in start_lane..lane_count {
128 let lane = val.value_lane(fx, lane_idx).load_scalar(fx);
129 res_val = f(fx, lane_layout.ty, res_val, lane);
131 let res = CValue::by_val(res_val, lane_layout);
132 ret.write_cvalue(fx, res);
135 // FIXME move all uses to `simd_reduce`
136 fn simd_reduce_bool<'tcx>(
137 fx: &mut FunctionCx<'_, '_, 'tcx>,
140 f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Value, Value) -> Value,
142 let (lane_count, _lane_ty) = val.layout().ty.simd_size_and_type(fx.tcx);
143 assert!(ret.layout().ty.is_bool());
145 let res_val = val.value_lane(fx, 0).load_scalar(fx);
146 let mut res_val = fx.bcx.ins().band_imm(res_val, 1); // mask to boolean
147 for lane_idx in 1..lane_count {
148 let lane = val.value_lane(fx, lane_idx).load_scalar(fx);
149 let lane = fx.bcx.ins().band_imm(lane, 1); // mask to boolean
150 res_val = f(fx, res_val, lane);
152 let res_val = if fx.bcx.func.dfg.value_type(res_val) != types::I8 {
153 fx.bcx.ins().ireduce(types::I8, res_val)
157 let res = CValue::by_val(res_val, ret.layout());
158 ret.write_cvalue(fx, res);
161 fn bool_to_zero_or_max_uint<'tcx>(
162 fx: &mut FunctionCx<'_, '_, 'tcx>,
166 let ty = fx.clif_type(ty).unwrap();
168 let int_ty = match ty {
169 types::F32 => types::I32,
170 types::F64 => types::I64,
174 let val = fx.bcx.ins().bint(int_ty, val);
175 let mut res = fx.bcx.ins().ineg(val);
178 res = fx.bcx.ins().bitcast(ty, res);
184 pub(crate) fn codegen_intrinsic_call<'tcx>(
185 fx: &mut FunctionCx<'_, '_, 'tcx>,
186 instance: Instance<'tcx>,
187 args: &[mir::Operand<'tcx>],
188 destination: CPlace<'tcx>,
189 target: Option<BasicBlock>,
190 source_info: mir::SourceInfo,
192 let intrinsic = fx.tcx.item_name(instance.def_id());
193 let substs = instance.substs;
195 let target = if let Some(target) = target {
198 // Insert non returning intrinsics here
201 fx.bcx.ins().trap(TrapCode::User(0));
204 crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", source_info);
206 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
211 if intrinsic.as_str().starts_with("simd_") {
212 self::simd::codegen_simd_intrinsic_call(
220 let ret_block = fx.get_block(target);
221 fx.bcx.ins().jump(ret_block, &[]);
222 } else if codegen_float_intrinsic_call(fx, intrinsic, args, destination) {
223 let ret_block = fx.get_block(target);
224 fx.bcx.ins().jump(ret_block, &[]);
226 codegen_regular_intrinsic_call(
239 fn codegen_float_intrinsic_call<'tcx>(
240 fx: &mut FunctionCx<'_, '_, 'tcx>,
242 args: &[mir::Operand<'tcx>],
245 let (name, arg_count, ty) = match intrinsic {
246 sym::expf32 => ("expf", 1, fx.tcx.types.f32),
247 sym::expf64 => ("exp", 1, fx.tcx.types.f64),
248 sym::exp2f32 => ("exp2f", 1, fx.tcx.types.f32),
249 sym::exp2f64 => ("exp2", 1, fx.tcx.types.f64),
250 sym::sqrtf32 => ("sqrtf", 1, fx.tcx.types.f32),
251 sym::sqrtf64 => ("sqrt", 1, fx.tcx.types.f64),
252 sym::powif32 => ("__powisf2", 2, fx.tcx.types.f32), // compiler-builtins
253 sym::powif64 => ("__powidf2", 2, fx.tcx.types.f64), // compiler-builtins
254 sym::powf32 => ("powf", 2, fx.tcx.types.f32),
255 sym::powf64 => ("pow", 2, fx.tcx.types.f64),
256 sym::logf32 => ("logf", 1, fx.tcx.types.f32),
257 sym::logf64 => ("log", 1, fx.tcx.types.f64),
258 sym::log2f32 => ("log2f", 1, fx.tcx.types.f32),
259 sym::log2f64 => ("log2", 1, fx.tcx.types.f64),
260 sym::log10f32 => ("log10f", 1, fx.tcx.types.f32),
261 sym::log10f64 => ("log10", 1, fx.tcx.types.f64),
262 sym::fabsf32 => ("fabsf", 1, fx.tcx.types.f32),
263 sym::fabsf64 => ("fabs", 1, fx.tcx.types.f64),
264 sym::fmaf32 => ("fmaf", 3, fx.tcx.types.f32),
265 sym::fmaf64 => ("fma", 3, fx.tcx.types.f64),
266 sym::copysignf32 => ("copysignf", 2, fx.tcx.types.f32),
267 sym::copysignf64 => ("copysign", 2, fx.tcx.types.f64),
268 sym::floorf32 => ("floorf", 1, fx.tcx.types.f32),
269 sym::floorf64 => ("floor", 1, fx.tcx.types.f64),
270 sym::ceilf32 => ("ceilf", 1, fx.tcx.types.f32),
271 sym::ceilf64 => ("ceil", 1, fx.tcx.types.f64),
272 sym::truncf32 => ("truncf", 1, fx.tcx.types.f32),
273 sym::truncf64 => ("trunc", 1, fx.tcx.types.f64),
274 sym::roundf32 => ("roundf", 1, fx.tcx.types.f32),
275 sym::roundf64 => ("round", 1, fx.tcx.types.f64),
276 sym::sinf32 => ("sinf", 1, fx.tcx.types.f32),
277 sym::sinf64 => ("sin", 1, fx.tcx.types.f64),
278 sym::cosf32 => ("cosf", 1, fx.tcx.types.f32),
279 sym::cosf64 => ("cos", 1, fx.tcx.types.f64),
283 if args.len() != arg_count {
284 bug!("wrong number of args for intrinsic {:?}", intrinsic);
288 let args = match args {
290 a = [codegen_operand(fx, x)];
294 b = [codegen_operand(fx, x), codegen_operand(fx, y)];
298 c = [codegen_operand(fx, x), codegen_operand(fx, y), codegen_operand(fx, z)];
304 let res = fx.easy_call(name, &args, ty);
305 ret.write_cvalue(fx, res);
310 fn codegen_regular_intrinsic_call<'tcx>(
311 fx: &mut FunctionCx<'_, '_, 'tcx>,
312 instance: Instance<'tcx>,
314 substs: SubstsRef<'tcx>,
315 args: &[mir::Operand<'tcx>],
317 destination: Option<BasicBlock>,
318 source_info: mir::SourceInfo,
320 let usize_layout = fx.layout_of(fx.tcx.types.usize);
324 intrinsic_args!(fx, args => (_a); intrinsic);
326 sym::likely | sym::unlikely => {
327 intrinsic_args!(fx, args => (a); intrinsic);
329 ret.write_cvalue(fx, a);
332 intrinsic_args!(fx, args => (); intrinsic);
334 fx.bcx.ins().debugtrap();
336 sym::copy | sym::copy_nonoverlapping => {
337 intrinsic_args!(fx, args => (src, dst, count); intrinsic);
338 let src = src.load_scalar(fx);
339 let dst = dst.load_scalar(fx);
340 let count = count.load_scalar(fx);
342 let elem_ty = substs.type_at(0);
343 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
344 assert_eq!(args.len(), 3);
346 if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count };
348 if intrinsic == sym::copy_nonoverlapping {
349 // FIXME emit_small_memcpy
350 fx.bcx.call_memcpy(fx.target_config, dst, src, byte_amount);
352 // FIXME emit_small_memmove
353 fx.bcx.call_memmove(fx.target_config, dst, src, byte_amount);
356 sym::volatile_copy_memory | sym::volatile_copy_nonoverlapping_memory => {
357 // NOTE: the volatile variants have src and dst swapped
358 intrinsic_args!(fx, args => (dst, src, count); intrinsic);
359 let dst = dst.load_scalar(fx);
360 let src = src.load_scalar(fx);
361 let count = count.load_scalar(fx);
363 let elem_ty = substs.type_at(0);
364 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
365 assert_eq!(args.len(), 3);
367 if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count };
369 // FIXME make the copy actually volatile when using emit_small_mem{cpy,move}
370 if intrinsic == sym::volatile_copy_nonoverlapping_memory {
371 // FIXME emit_small_memcpy
372 fx.bcx.call_memcpy(fx.target_config, dst, src, byte_amount);
374 // FIXME emit_small_memmove
375 fx.bcx.call_memmove(fx.target_config, dst, src, byte_amount);
378 sym::size_of_val => {
379 intrinsic_args!(fx, args => (ptr); intrinsic);
381 let layout = fx.layout_of(substs.type_at(0));
382 // Note: Can't use is_unsized here as truly unsized types need to take the fixed size
384 let size = if let Abi::ScalarPair(_, _) = ptr.layout().abi {
385 let (_ptr, info) = ptr.load_scalar_pair(fx);
386 let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout, info);
389 fx.bcx.ins().iconst(fx.pointer_type, layout.size.bytes() as i64)
391 ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
393 sym::min_align_of_val => {
394 intrinsic_args!(fx, args => (ptr); intrinsic);
396 let layout = fx.layout_of(substs.type_at(0));
397 // Note: Can't use is_unsized here as truly unsized types need to take the fixed size
399 let align = if let Abi::ScalarPair(_, _) = ptr.layout().abi {
400 let (_ptr, info) = ptr.load_scalar_pair(fx);
401 let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout, info);
404 fx.bcx.ins().iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
406 ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
409 sym::vtable_size => {
410 intrinsic_args!(fx, args => (vtable); intrinsic);
411 let vtable = vtable.load_scalar(fx);
413 let size = crate::vtable::size_of_obj(fx, vtable);
414 ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
417 sym::vtable_align => {
418 intrinsic_args!(fx, args => (vtable); intrinsic);
419 let vtable = vtable.load_scalar(fx);
421 let align = crate::vtable::min_align_of_obj(fx, vtable);
422 ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
432 | sym::unchecked_shr => {
433 intrinsic_args!(fx, args => (x, y); intrinsic);
435 // FIXME trap on overflow
436 let bin_op = match intrinsic {
437 sym::unchecked_add => BinOp::Add,
438 sym::unchecked_sub => BinOp::Sub,
439 sym::unchecked_mul => BinOp::Mul,
440 sym::unchecked_div | sym::exact_div => BinOp::Div,
441 sym::unchecked_rem => BinOp::Rem,
442 sym::unchecked_shl => BinOp::Shl,
443 sym::unchecked_shr => BinOp::Shr,
446 let res = crate::num::codegen_int_binop(fx, bin_op, x, y);
447 ret.write_cvalue(fx, res);
449 sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {
450 intrinsic_args!(fx, args => (x, y); intrinsic);
452 assert_eq!(x.layout().ty, y.layout().ty);
453 let bin_op = match intrinsic {
454 sym::add_with_overflow => BinOp::Add,
455 sym::sub_with_overflow => BinOp::Sub,
456 sym::mul_with_overflow => BinOp::Mul,
460 let res = crate::num::codegen_checked_int_binop(fx, bin_op, x, y);
461 ret.write_cvalue(fx, res);
463 sym::saturating_add | sym::saturating_sub => {
464 intrinsic_args!(fx, args => (lhs, rhs); intrinsic);
466 assert_eq!(lhs.layout().ty, rhs.layout().ty);
467 let bin_op = match intrinsic {
468 sym::saturating_add => BinOp::Add,
469 sym::saturating_sub => BinOp::Sub,
473 let signed = type_sign(lhs.layout().ty);
475 let checked_res = crate::num::codegen_checked_int_binop(fx, bin_op, lhs, rhs);
477 let (val, has_overflow) = checked_res.load_scalar_pair(fx);
478 let clif_ty = fx.clif_type(lhs.layout().ty).unwrap();
480 let (min, max) = type_min_max_value(&mut fx.bcx, clif_ty, signed);
482 let val = match (intrinsic, signed) {
483 (sym::saturating_add, false) => fx.bcx.ins().select(has_overflow, max, val),
484 (sym::saturating_sub, false) => fx.bcx.ins().select(has_overflow, min, val),
485 (sym::saturating_add, true) => {
486 let rhs = rhs.load_scalar(fx);
488 fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
489 let sat_val = fx.bcx.ins().select(rhs_ge_zero, max, min);
490 fx.bcx.ins().select(has_overflow, sat_val, val)
492 (sym::saturating_sub, true) => {
493 let rhs = rhs.load_scalar(fx);
495 fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
496 let sat_val = fx.bcx.ins().select(rhs_ge_zero, min, max);
497 fx.bcx.ins().select(has_overflow, sat_val, val)
502 let res = CValue::by_val(val, lhs.layout());
504 ret.write_cvalue(fx, res);
506 sym::rotate_left => {
507 intrinsic_args!(fx, args => (x, y); intrinsic);
508 let y = y.load_scalar(fx);
510 let layout = x.layout();
511 let x = x.load_scalar(fx);
512 let res = fx.bcx.ins().rotl(x, y);
513 ret.write_cvalue(fx, CValue::by_val(res, layout));
515 sym::rotate_right => {
516 intrinsic_args!(fx, args => (x, y); intrinsic);
517 let y = y.load_scalar(fx);
519 let layout = x.layout();
520 let x = x.load_scalar(fx);
521 let res = fx.bcx.ins().rotr(x, y);
522 ret.write_cvalue(fx, CValue::by_val(res, layout));
525 // The only difference between offset and arith_offset is regarding UB. Because Cranelift
526 // doesn't have UB both are codegen'ed the same way
527 sym::offset | sym::arith_offset => {
528 intrinsic_args!(fx, args => (base, offset); intrinsic);
529 let offset = offset.load_scalar(fx);
531 let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
532 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
533 let ptr_diff = if pointee_size != 1 {
534 fx.bcx.ins().imul_imm(offset, pointee_size as i64)
538 let base_val = base.load_scalar(fx);
539 let res = fx.bcx.ins().iadd(base_val, ptr_diff);
540 ret.write_cvalue(fx, CValue::by_val(res, base.layout()));
544 intrinsic_args!(fx, args => (from); intrinsic);
546 ret.write_cvalue_transmute(fx, from);
548 sym::write_bytes | sym::volatile_set_memory => {
549 intrinsic_args!(fx, args => (dst, val, count); intrinsic);
550 let val = val.load_scalar(fx);
551 let count = count.load_scalar(fx);
553 let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
554 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
555 let count = if pointee_size != 1 {
556 fx.bcx.ins().imul_imm(count, pointee_size as i64)
560 let dst_ptr = dst.load_scalar(fx);
561 // FIXME make the memset actually volatile when switching to emit_small_memset
562 // FIXME use emit_small_memset
563 fx.bcx.call_memset(fx.target_config, dst_ptr, val, count);
565 sym::ctlz | sym::ctlz_nonzero => {
566 intrinsic_args!(fx, args => (arg); intrinsic);
567 let val = arg.load_scalar(fx);
569 // FIXME trap on `ctlz_nonzero` with zero arg.
570 let res = fx.bcx.ins().clz(val);
571 let res = CValue::by_val(res, arg.layout());
572 ret.write_cvalue(fx, res);
574 sym::cttz | sym::cttz_nonzero => {
575 intrinsic_args!(fx, args => (arg); intrinsic);
576 let val = arg.load_scalar(fx);
578 // FIXME trap on `cttz_nonzero` with zero arg.
579 let res = fx.bcx.ins().ctz(val);
580 let res = CValue::by_val(res, arg.layout());
581 ret.write_cvalue(fx, res);
584 intrinsic_args!(fx, args => (arg); intrinsic);
585 let val = arg.load_scalar(fx);
587 let res = fx.bcx.ins().popcnt(val);
588 let res = CValue::by_val(res, arg.layout());
589 ret.write_cvalue(fx, res);
592 intrinsic_args!(fx, args => (arg); intrinsic);
593 let val = arg.load_scalar(fx);
595 let res = fx.bcx.ins().bitrev(val);
596 let res = CValue::by_val(res, arg.layout());
597 ret.write_cvalue(fx, res);
600 // FIXME(CraneStation/cranelift#794) add bswap instruction to cranelift
601 fn swap(bcx: &mut FunctionBuilder<'_>, v: Value) -> Value {
602 match bcx.func.dfg.value_type(v) {
605 // https://code.woboq.org/gcc/include/bits/byteswap.h.html
607 let tmp1 = bcx.ins().ishl_imm(v, 8);
608 let n1 = bcx.ins().band_imm(tmp1, 0xFF00);
610 let tmp2 = bcx.ins().ushr_imm(v, 8);
611 let n2 = bcx.ins().band_imm(tmp2, 0x00FF);
613 bcx.ins().bor(n1, n2)
616 let tmp1 = bcx.ins().ishl_imm(v, 24);
617 let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000);
619 let tmp2 = bcx.ins().ishl_imm(v, 8);
620 let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000);
622 let tmp3 = bcx.ins().ushr_imm(v, 8);
623 let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00);
625 let tmp4 = bcx.ins().ushr_imm(v, 24);
626 let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF);
628 let or_tmp1 = bcx.ins().bor(n1, n2);
629 let or_tmp2 = bcx.ins().bor(n3, n4);
630 bcx.ins().bor(or_tmp1, or_tmp2)
633 let tmp1 = bcx.ins().ishl_imm(v, 56);
634 let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000_0000_0000u64 as i64);
636 let tmp2 = bcx.ins().ishl_imm(v, 40);
637 let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000_0000_0000u64 as i64);
639 let tmp3 = bcx.ins().ishl_imm(v, 24);
640 let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00_0000_0000u64 as i64);
642 let tmp4 = bcx.ins().ishl_imm(v, 8);
643 let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF_0000_0000u64 as i64);
645 let tmp5 = bcx.ins().ushr_imm(v, 8);
646 let n5 = bcx.ins().band_imm(tmp5, 0x0000_0000_FF00_0000u64 as i64);
648 let tmp6 = bcx.ins().ushr_imm(v, 24);
649 let n6 = bcx.ins().band_imm(tmp6, 0x0000_0000_00FF_0000u64 as i64);
651 let tmp7 = bcx.ins().ushr_imm(v, 40);
652 let n7 = bcx.ins().band_imm(tmp7, 0x0000_0000_0000_FF00u64 as i64);
654 let tmp8 = bcx.ins().ushr_imm(v, 56);
655 let n8 = bcx.ins().band_imm(tmp8, 0x0000_0000_0000_00FFu64 as i64);
657 let or_tmp1 = bcx.ins().bor(n1, n2);
658 let or_tmp2 = bcx.ins().bor(n3, n4);
659 let or_tmp3 = bcx.ins().bor(n5, n6);
660 let or_tmp4 = bcx.ins().bor(n7, n8);
662 let or_tmp5 = bcx.ins().bor(or_tmp1, or_tmp2);
663 let or_tmp6 = bcx.ins().bor(or_tmp3, or_tmp4);
664 bcx.ins().bor(or_tmp5, or_tmp6)
667 let (lo, hi) = bcx.ins().isplit(v);
668 let lo = swap(bcx, lo);
669 let hi = swap(bcx, hi);
670 bcx.ins().iconcat(hi, lo)
672 ty => unreachable!("bswap {}", ty),
675 intrinsic_args!(fx, args => (arg); intrinsic);
676 let val = arg.load_scalar(fx);
678 let res = CValue::by_val(swap(&mut fx.bcx, val), arg.layout());
679 ret.write_cvalue(fx, res);
681 sym::assert_inhabited | sym::assert_zero_valid | sym::assert_uninit_valid => {
682 intrinsic_args!(fx, args => (); intrinsic);
684 let layout = fx.layout_of(substs.type_at(0));
685 if layout.abi.is_uninhabited() {
686 with_no_trimmed_paths!({
687 crate::base::codegen_panic(
689 &format!("attempted to instantiate uninhabited type `{}`", layout.ty),
696 if intrinsic == sym::assert_zero_valid && !fx.tcx.permits_zero_init(layout) {
697 with_no_trimmed_paths!({
698 crate::base::codegen_panic(
701 "attempted to zero-initialize type `{}`, which is invalid",
710 if intrinsic == sym::assert_uninit_valid && !fx.tcx.permits_uninit_init(layout) {
711 with_no_trimmed_paths!({
712 crate::base::codegen_panic(
715 "attempted to leave type `{}` uninitialized, which is invalid",
725 sym::volatile_load | sym::unaligned_volatile_load => {
726 intrinsic_args!(fx, args => (ptr); intrinsic);
728 // Cranelift treats loads as volatile by default
729 // FIXME correctly handle unaligned_volatile_load
730 let inner_layout = fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
731 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
732 ret.write_cvalue(fx, val);
734 sym::volatile_store | sym::unaligned_volatile_store => {
735 intrinsic_args!(fx, args => (ptr, val); intrinsic);
736 let ptr = ptr.load_scalar(fx);
738 // Cranelift treats stores as volatile by default
739 // FIXME correctly handle unaligned_volatile_store
740 let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
741 dest.write_cvalue(fx, val);
748 | sym::variant_count => {
749 intrinsic_args!(fx, args => (); intrinsic);
752 fx.tcx.const_eval_instance(ParamEnv::reveal_all(), instance, None).unwrap();
753 let val = crate::constant::codegen_const_value(fx, const_val, ret.layout().ty);
754 ret.write_cvalue(fx, val);
757 sym::ptr_offset_from | sym::ptr_offset_from_unsigned => {
758 intrinsic_args!(fx, args => (ptr, base); intrinsic);
759 let ptr = ptr.load_scalar(fx);
760 let base = base.load_scalar(fx);
761 let ty = substs.type_at(0);
763 let pointee_size: u64 = fx.layout_of(ty).size.bytes();
764 let diff_bytes = fx.bcx.ins().isub(ptr, base);
765 // FIXME this can be an exact division.
766 let val = if intrinsic == sym::ptr_offset_from_unsigned {
767 let usize_layout = fx.layout_of(fx.tcx.types.usize);
768 // Because diff_bytes ULE isize::MAX, this would be fine as signed,
769 // but unsigned is slightly easier to codegen, so might as well.
770 CValue::by_val(fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64), usize_layout)
772 let isize_layout = fx.layout_of(fx.tcx.types.isize);
773 CValue::by_val(fx.bcx.ins().sdiv_imm(diff_bytes, pointee_size as i64), isize_layout)
775 ret.write_cvalue(fx, val);
778 sym::ptr_guaranteed_eq => {
779 intrinsic_args!(fx, args => (a, b); intrinsic);
781 let val = crate::num::codegen_ptr_binop(fx, BinOp::Eq, a, b);
782 ret.write_cvalue(fx, val);
785 sym::ptr_guaranteed_ne => {
786 intrinsic_args!(fx, args => (a, b); intrinsic);
788 let val = crate::num::codegen_ptr_binop(fx, BinOp::Ne, a, b);
789 ret.write_cvalue(fx, val);
792 sym::caller_location => {
793 intrinsic_args!(fx, args => (); intrinsic);
795 let caller_location = fx.get_caller_location(source_info);
796 ret.write_cvalue(fx, caller_location);
799 _ if intrinsic.as_str().starts_with("atomic_fence") => {
800 intrinsic_args!(fx, args => (); intrinsic);
802 fx.bcx.ins().fence();
804 _ if intrinsic.as_str().starts_with("atomic_singlethreadfence") => {
805 intrinsic_args!(fx, args => (); intrinsic);
807 // FIXME use a compiler fence once Cranelift supports it
808 fx.bcx.ins().fence();
810 _ if intrinsic.as_str().starts_with("atomic_load") => {
811 intrinsic_args!(fx, args => (ptr); intrinsic);
812 let ptr = ptr.load_scalar(fx);
814 let ty = substs.type_at(0);
816 ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
817 // FIXME implement 128bit atomics
818 if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
819 // special case for compiler-builtins to avoid having to patch it
820 crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
821 let ret_block = fx.get_block(destination.unwrap());
822 fx.bcx.ins().jump(ret_block, &[]);
827 .span_fatal(source_info.span, "128bit atomics not yet supported");
830 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
832 report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty);
836 let clif_ty = fx.clif_type(ty).unwrap();
838 let val = fx.bcx.ins().atomic_load(clif_ty, MemFlags::trusted(), ptr);
840 let val = CValue::by_val(val, fx.layout_of(ty));
841 ret.write_cvalue(fx, val);
843 _ if intrinsic.as_str().starts_with("atomic_store") => {
844 intrinsic_args!(fx, args => (ptr, val); intrinsic);
845 let ptr = ptr.load_scalar(fx);
847 let ty = substs.type_at(0);
849 ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
850 // FIXME implement 128bit atomics
851 if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
852 // special case for compiler-builtins to avoid having to patch it
853 crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
854 let ret_block = fx.get_block(destination.unwrap());
855 fx.bcx.ins().jump(ret_block, &[]);
860 .span_fatal(source_info.span, "128bit atomics not yet supported");
863 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
865 report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty);
870 let val = val.load_scalar(fx);
872 fx.bcx.ins().atomic_store(MemFlags::trusted(), val, ptr);
874 _ if intrinsic.as_str().starts_with("atomic_xchg") => {
875 intrinsic_args!(fx, args => (ptr, new); intrinsic);
876 let ptr = ptr.load_scalar(fx);
878 let layout = new.layout();
879 match layout.ty.kind() {
880 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
882 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
886 let ty = fx.clif_type(layout.ty).unwrap();
888 let new = new.load_scalar(fx);
890 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Xchg, ptr, new);
892 let old = CValue::by_val(old, layout);
893 ret.write_cvalue(fx, old);
895 _ if intrinsic.as_str().starts_with("atomic_cxchg") => {
896 // both atomic_cxchg_* and atomic_cxchgweak_*
897 intrinsic_args!(fx, args => (ptr, test_old, new); intrinsic);
898 let ptr = ptr.load_scalar(fx);
900 let layout = new.layout();
901 match layout.ty.kind() {
902 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
904 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
909 let test_old = test_old.load_scalar(fx);
910 let new = new.load_scalar(fx);
912 let old = fx.bcx.ins().atomic_cas(MemFlags::trusted(), ptr, test_old, new);
913 let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old);
916 CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
917 ret.write_cvalue(fx, ret_val)
920 _ if intrinsic.as_str().starts_with("atomic_xadd") => {
921 intrinsic_args!(fx, args => (ptr, amount); intrinsic);
922 let ptr = ptr.load_scalar(fx);
924 let layout = amount.layout();
925 match layout.ty.kind() {
926 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
928 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
932 let ty = fx.clif_type(layout.ty).unwrap();
934 let amount = amount.load_scalar(fx);
937 fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Add, ptr, amount);
939 let old = CValue::by_val(old, layout);
940 ret.write_cvalue(fx, old);
942 _ if intrinsic.as_str().starts_with("atomic_xsub") => {
943 intrinsic_args!(fx, args => (ptr, amount); intrinsic);
944 let ptr = ptr.load_scalar(fx);
946 let layout = amount.layout();
947 match layout.ty.kind() {
948 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
950 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
954 let ty = fx.clif_type(layout.ty).unwrap();
956 let amount = amount.load_scalar(fx);
959 fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Sub, ptr, amount);
961 let old = CValue::by_val(old, layout);
962 ret.write_cvalue(fx, old);
964 _ if intrinsic.as_str().starts_with("atomic_and") => {
965 intrinsic_args!(fx, args => (ptr, src); intrinsic);
966 let ptr = ptr.load_scalar(fx);
968 let layout = src.layout();
969 match layout.ty.kind() {
970 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
972 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
976 let ty = fx.clif_type(layout.ty).unwrap();
978 let src = src.load_scalar(fx);
980 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::And, ptr, src);
982 let old = CValue::by_val(old, layout);
983 ret.write_cvalue(fx, old);
985 _ if intrinsic.as_str().starts_with("atomic_or") => {
986 intrinsic_args!(fx, args => (ptr, src); intrinsic);
987 let ptr = ptr.load_scalar(fx);
989 let layout = src.layout();
990 match layout.ty.kind() {
991 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
993 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
997 let ty = fx.clif_type(layout.ty).unwrap();
999 let src = src.load_scalar(fx);
1001 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Or, ptr, src);
1003 let old = CValue::by_val(old, layout);
1004 ret.write_cvalue(fx, old);
1006 _ if intrinsic.as_str().starts_with("atomic_xor") => {
1007 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1008 let ptr = ptr.load_scalar(fx);
1010 let layout = src.layout();
1011 match layout.ty.kind() {
1012 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1014 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1018 let ty = fx.clif_type(layout.ty).unwrap();
1020 let src = src.load_scalar(fx);
1022 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Xor, ptr, src);
1024 let old = CValue::by_val(old, layout);
1025 ret.write_cvalue(fx, old);
1027 _ if intrinsic.as_str().starts_with("atomic_nand") => {
1028 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1029 let ptr = ptr.load_scalar(fx);
1031 let layout = src.layout();
1032 match layout.ty.kind() {
1033 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1035 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1039 let ty = fx.clif_type(layout.ty).unwrap();
1041 let src = src.load_scalar(fx);
1043 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Nand, ptr, src);
1045 let old = CValue::by_val(old, layout);
1046 ret.write_cvalue(fx, old);
1048 _ if intrinsic.as_str().starts_with("atomic_max") => {
1049 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1050 let ptr = ptr.load_scalar(fx);
1052 let layout = src.layout();
1053 match layout.ty.kind() {
1054 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1056 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1060 let ty = fx.clif_type(layout.ty).unwrap();
1062 let src = src.load_scalar(fx);
1064 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Smax, ptr, src);
1066 let old = CValue::by_val(old, layout);
1067 ret.write_cvalue(fx, old);
1069 _ if intrinsic.as_str().starts_with("atomic_umax") => {
1070 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1071 let ptr = ptr.load_scalar(fx);
1073 let layout = src.layout();
1074 match layout.ty.kind() {
1075 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1077 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1081 let ty = fx.clif_type(layout.ty).unwrap();
1083 let src = src.load_scalar(fx);
1085 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Umax, ptr, src);
1087 let old = CValue::by_val(old, layout);
1088 ret.write_cvalue(fx, old);
1090 _ if intrinsic.as_str().starts_with("atomic_min") => {
1091 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1092 let ptr = ptr.load_scalar(fx);
1094 let layout = src.layout();
1095 match layout.ty.kind() {
1096 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1098 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1102 let ty = fx.clif_type(layout.ty).unwrap();
1104 let src = src.load_scalar(fx);
1106 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Smin, ptr, src);
1108 let old = CValue::by_val(old, layout);
1109 ret.write_cvalue(fx, old);
1111 _ if intrinsic.as_str().starts_with("atomic_umin") => {
1112 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1113 let ptr = ptr.load_scalar(fx);
1115 let layout = src.layout();
1116 match layout.ty.kind() {
1117 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1119 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1123 let ty = fx.clif_type(layout.ty).unwrap();
1125 let src = src.load_scalar(fx);
1127 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Umin, ptr, src);
1129 let old = CValue::by_val(old, layout);
1130 ret.write_cvalue(fx, old);
1134 intrinsic_args!(fx, args => (a, b); intrinsic);
1135 let a = a.load_scalar(fx);
1136 let b = b.load_scalar(fx);
1138 let val = crate::num::codegen_float_min(fx, a, b);
1139 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
1140 ret.write_cvalue(fx, val);
1143 intrinsic_args!(fx, args => (a, b); intrinsic);
1144 let a = a.load_scalar(fx);
1145 let b = b.load_scalar(fx);
1147 let val = crate::num::codegen_float_min(fx, a, b);
1148 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
1149 ret.write_cvalue(fx, val);
1152 intrinsic_args!(fx, args => (a, b); intrinsic);
1153 let a = a.load_scalar(fx);
1154 let b = b.load_scalar(fx);
1156 let val = crate::num::codegen_float_max(fx, a, b);
1157 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
1158 ret.write_cvalue(fx, val);
1161 intrinsic_args!(fx, args => (a, b); intrinsic);
1162 let a = a.load_scalar(fx);
1163 let b = b.load_scalar(fx);
1165 let val = crate::num::codegen_float_max(fx, a, b);
1166 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
1167 ret.write_cvalue(fx, val);
1171 intrinsic_args!(fx, args => (f, data, catch_fn); intrinsic);
1172 let f = f.load_scalar(fx);
1173 let data = data.load_scalar(fx);
1174 let _catch_fn = catch_fn.load_scalar(fx);
1176 // FIXME once unwinding is supported, change this to actually catch panics
1177 let f_sig = fx.bcx.func.import_signature(Signature {
1178 call_conv: fx.target_config.default_call_conv,
1179 params: vec![AbiParam::new(fx.bcx.func.dfg.value_type(data))],
1183 fx.bcx.ins().call_indirect(f_sig, f, &[data]);
1185 let layout = ret.layout();
1186 let ret_val = CValue::const_val(fx, layout, ty::ScalarInt::null(layout.size));
1187 ret.write_cvalue(fx, ret_val);
1190 sym::fadd_fast | sym::fsub_fast | sym::fmul_fast | sym::fdiv_fast | sym::frem_fast => {
1191 intrinsic_args!(fx, args => (x, y); intrinsic);
1193 let res = crate::num::codegen_float_binop(
1196 sym::fadd_fast => BinOp::Add,
1197 sym::fsub_fast => BinOp::Sub,
1198 sym::fmul_fast => BinOp::Mul,
1199 sym::fdiv_fast => BinOp::Div,
1200 sym::frem_fast => BinOp::Rem,
1201 _ => unreachable!(),
1206 ret.write_cvalue(fx, res);
1208 sym::float_to_int_unchecked => {
1209 intrinsic_args!(fx, args => (f); intrinsic);
1210 let f = f.load_scalar(fx);
1212 let res = crate::cast::clif_int_or_float_cast(
1216 fx.clif_type(ret.layout().ty).unwrap(),
1217 type_sign(ret.layout().ty),
1219 ret.write_cvalue(fx, CValue::by_val(res, ret.layout()));
1223 intrinsic_args!(fx, args => (lhs_ref, rhs_ref); intrinsic);
1224 let lhs_ref = lhs_ref.load_scalar(fx);
1225 let rhs_ref = rhs_ref.load_scalar(fx);
1227 let size = fx.layout_of(substs.type_at(0)).layout.size();
1228 // FIXME add and use emit_small_memcmp
1229 let is_eq_value = if size == Size::ZERO {
1230 // No bytes means they're trivially equal
1231 fx.bcx.ins().iconst(types::I8, 1)
1232 } else if let Some(clty) = size.bits().try_into().ok().and_then(Type::int) {
1233 // Can't use `trusted` for these loads; they could be unaligned.
1234 let mut flags = MemFlags::new();
1236 let lhs_val = fx.bcx.ins().load(clty, flags, lhs_ref, 0);
1237 let rhs_val = fx.bcx.ins().load(clty, flags, rhs_ref, 0);
1238 let eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_val, rhs_val);
1239 fx.bcx.ins().bint(types::I8, eq)
1241 // Just call `memcmp` (like slices do in core) when the
1242 // size is too large or it's not a power-of-two.
1243 let signed_bytes = i64::try_from(size.bytes()).unwrap();
1244 let bytes_val = fx.bcx.ins().iconst(fx.pointer_type, signed_bytes);
1245 let params = vec![AbiParam::new(fx.pointer_type); 3];
1246 let returns = vec![AbiParam::new(types::I32)];
1247 let args = &[lhs_ref, rhs_ref, bytes_val];
1248 let cmp = fx.lib_call("memcmp", params, returns, args)[0];
1249 let eq = fx.bcx.ins().icmp_imm(IntCC::Equal, cmp, 0);
1250 fx.bcx.ins().bint(types::I8, eq)
1252 ret.write_cvalue(fx, CValue::by_val(is_eq_value, ret.layout()));
1255 sym::const_allocate => {
1256 intrinsic_args!(fx, args => (_size, _align); intrinsic);
1258 // returns a null pointer at runtime.
1259 let null = fx.bcx.ins().iconst(fx.pointer_type, 0);
1260 ret.write_cvalue(fx, CValue::by_val(null, ret.layout()));
1263 sym::const_deallocate => {
1264 intrinsic_args!(fx, args => (_ptr, _size, _align); intrinsic);
1269 intrinsic_args!(fx, args => (a); intrinsic);
1271 // FIXME implement black_box semantics
1272 ret.write_cvalue(fx, a);
1275 // FIXME implement variadics in cranelift
1276 sym::va_copy | sym::va_arg | sym::va_end => {
1277 fx.tcx.sess.span_fatal(
1279 "Defining variadic functions is not yet supported by Cranelift",
1286 .span_fatal(source_info.span, &format!("unsupported intrinsic {}", intrinsic));
1290 let ret_block = fx.get_block(destination.unwrap());
1291 fx.bcx.ins().jump(ret_block, &[]);