1 use crate::abi::{Abi, FnAbi, LlvmType, PassMode};
2 use crate::builder::Builder;
3 use crate::context::CodegenCx;
5 use crate::type_::Type;
6 use crate::type_of::LayoutLlvmExt;
7 use crate::va_arg::emit_va_arg;
8 use crate::value::Value;
13 use rustc_codegen_ssa::base::{compare_simd_types, to_immediate, wants_msvc_seh};
14 use rustc_codegen_ssa::common::span_invalid_monomorphization_error;
15 use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
16 use rustc_codegen_ssa::coverageinfo::CounterOp;
17 use rustc_codegen_ssa::glue;
18 use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
19 use rustc_codegen_ssa::mir::place::PlaceRef;
20 use rustc_codegen_ssa::traits::*;
21 use rustc_codegen_ssa::MemFlags;
23 use rustc_middle::mir::coverage;
24 use rustc_middle::mir::Operand;
25 use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt};
26 use rustc_middle::ty::{self, Ty};
27 use rustc_middle::{bug, span_bug};
28 use rustc_span::{sym, symbol::kw, Span, Symbol};
29 use rustc_target::abi::{self, HasDataLayout, LayoutOf, Primitive};
30 use rustc_target::spec::PanicStrategy;
32 use std::cmp::Ordering;
35 fn get_simple_intrinsic(cx: &CodegenCx<'ll, '_>, name: Symbol) -> Option<&'ll Value> {
36 let llvm_name = match name {
37 sym::sqrtf32 => "llvm.sqrt.f32",
38 sym::sqrtf64 => "llvm.sqrt.f64",
39 sym::powif32 => "llvm.powi.f32",
40 sym::powif64 => "llvm.powi.f64",
41 sym::sinf32 => "llvm.sin.f32",
42 sym::sinf64 => "llvm.sin.f64",
43 sym::cosf32 => "llvm.cos.f32",
44 sym::cosf64 => "llvm.cos.f64",
45 sym::powf32 => "llvm.pow.f32",
46 sym::powf64 => "llvm.pow.f64",
47 sym::expf32 => "llvm.exp.f32",
48 sym::expf64 => "llvm.exp.f64",
49 sym::exp2f32 => "llvm.exp2.f32",
50 sym::exp2f64 => "llvm.exp2.f64",
51 sym::logf32 => "llvm.log.f32",
52 sym::logf64 => "llvm.log.f64",
53 sym::log10f32 => "llvm.log10.f32",
54 sym::log10f64 => "llvm.log10.f64",
55 sym::log2f32 => "llvm.log2.f32",
56 sym::log2f64 => "llvm.log2.f64",
57 sym::fmaf32 => "llvm.fma.f32",
58 sym::fmaf64 => "llvm.fma.f64",
59 sym::fabsf32 => "llvm.fabs.f32",
60 sym::fabsf64 => "llvm.fabs.f64",
61 sym::minnumf32 => "llvm.minnum.f32",
62 sym::minnumf64 => "llvm.minnum.f64",
63 sym::maxnumf32 => "llvm.maxnum.f32",
64 sym::maxnumf64 => "llvm.maxnum.f64",
65 sym::copysignf32 => "llvm.copysign.f32",
66 sym::copysignf64 => "llvm.copysign.f64",
67 sym::floorf32 => "llvm.floor.f32",
68 sym::floorf64 => "llvm.floor.f64",
69 sym::ceilf32 => "llvm.ceil.f32",
70 sym::ceilf64 => "llvm.ceil.f64",
71 sym::truncf32 => "llvm.trunc.f32",
72 sym::truncf64 => "llvm.trunc.f64",
73 sym::rintf32 => "llvm.rint.f32",
74 sym::rintf64 => "llvm.rint.f64",
75 sym::nearbyintf32 => "llvm.nearbyint.f32",
76 sym::nearbyintf64 => "llvm.nearbyint.f64",
77 sym::roundf32 => "llvm.round.f32",
78 sym::roundf64 => "llvm.round.f64",
79 sym::assume => "llvm.assume",
80 sym::abort => "llvm.trap",
83 Some(cx.get_intrinsic(&llvm_name))
86 impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
87 fn is_codegen_intrinsic(
90 args: &Vec<Operand<'tcx>>,
91 caller_instance: ty::Instance<'tcx>,
93 if self.tcx.sess.opts.debugging_opts.instrument_coverage {
94 // Add the coverage information from the MIR to the Codegen context. Some coverage
95 // intrinsics are used only to pass along the coverage information (returns `false`
96 // for `is_codegen_intrinsic()`), but `count_code_region` is also converted into an
97 // LLVM intrinsic to increment a coverage counter.
99 sym::count_code_region => {
100 use coverage::count_code_region_args::*;
101 self.add_counter_region(
103 op_to_u64(&args[FUNCTION_SOURCE_HASH]),
104 op_to_u32(&args[COUNTER_INDEX]),
105 op_to_u32(&args[START_BYTE_POS]),
106 op_to_u32(&args[END_BYTE_POS]),
108 return true; // Also inject the counter increment in the backend
110 sym::coverage_counter_add | sym::coverage_counter_subtract => {
111 use coverage::coverage_counter_expression_args::*;
112 self.add_counter_expression_region(
114 op_to_u32(&args[COUNTER_EXPRESSION_INDEX]),
115 op_to_u32(&args[LEFT_INDEX]),
116 if intrinsic == sym::coverage_counter_add {
121 op_to_u32(&args[RIGHT_INDEX]),
122 op_to_u32(&args[START_BYTE_POS]),
123 op_to_u32(&args[END_BYTE_POS]),
125 return false; // Does not inject backend code
127 sym::coverage_unreachable => {
128 use coverage::coverage_unreachable_args::*;
129 self.add_unreachable_region(
131 op_to_u32(&args[START_BYTE_POS]),
132 op_to_u32(&args[END_BYTE_POS]),
134 return false; // Does not inject backend code
139 // NOT self.tcx.sess.opts.debugging_opts.instrument_coverage
140 if intrinsic == sym::count_code_region {
141 // An external crate may have been pre-compiled with coverage instrumentation, and
142 // some references from the current crate to the external crate might carry along
143 // the call terminators to coverage intrinsics, like `count_code_region` (for
144 // example, when instantiating a generic function). If the current crate has
145 // `instrument_coverage` disabled, the `count_code_region` call terminators should
147 return false; // Do not inject coverage counters inlined from external crates
150 true // Unhandled intrinsics should be passed to `codegen_intrinsic_call()`
153 fn codegen_intrinsic_call(
155 instance: ty::Instance<'tcx>,
156 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
157 args: &[OperandRef<'tcx, &'ll Value>],
158 llresult: &'ll Value,
160 caller_instance: ty::Instance<'tcx>,
163 let callee_ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
165 let (def_id, substs) = match callee_ty.kind {
166 ty::FnDef(def_id, substs) => (def_id, substs),
167 _ => bug!("expected fn item type, found {}", callee_ty),
170 let sig = callee_ty.fn_sig(tcx);
171 let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
172 let arg_tys = sig.inputs();
173 let ret_ty = sig.output();
174 let name = tcx.item_name(def_id);
175 let name_str = &*name.as_str();
177 let llret_ty = self.layout_of(ret_ty).llvm_type(self);
178 let result = PlaceRef::new_sized(llresult, fn_abi.ret.layout);
180 let simple = get_simple_intrinsic(self, name);
181 let llval = match name {
182 _ if simple.is_some() => self.call(
184 &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
187 sym::unreachable => {
191 let expect = self.get_intrinsic(&("llvm.expect.i1"));
192 self.call(expect, &[args[0].immediate(), self.const_bool(true)], None)
195 let expect = self.get_intrinsic(&("llvm.expect.i1"));
196 self.call(expect, &[args[0].immediate(), self.const_bool(false)], None)
209 let llfn = self.get_intrinsic(&("llvm.debugtrap"));
210 self.call(llfn, &[], None)
212 sym::count_code_region => {
213 // FIXME(richkadel): The current implementation assumes the MIR for the given
214 // caller_instance represents a single function. Validate and/or correct if inlining
215 // and/or monomorphization invalidates these assumptions.
216 let coverageinfo = tcx.coverageinfo(caller_instance.def_id());
217 let mangled_fn = tcx.symbol_name(caller_instance);
218 let (mangled_fn_name, _len_val) = self.const_str(Symbol::intern(mangled_fn.name));
219 let num_counters = self.const_u32(coverageinfo.num_counters);
220 use coverage::count_code_region_args::*;
221 let hash = args[FUNCTION_SOURCE_HASH].immediate();
222 let index = args[COUNTER_INDEX].immediate();
224 "translating Rust intrinsic `count_code_region()` to LLVM intrinsic: \
225 instrprof.increment(fn_name={}, hash={:?}, num_counters={:?}, index={:?})",
226 mangled_fn.name, hash, num_counters, index,
228 self.instrprof_increment(mangled_fn_name, hash, num_counters, index)
230 sym::va_start => self.va_start(args[0].immediate()),
231 sym::va_end => self.va_end(args[0].immediate()),
233 let intrinsic = self.cx().get_intrinsic(&("llvm.va_copy"));
234 self.call(intrinsic, &[args[0].immediate(), args[1].immediate()], None)
237 match fn_abi.ret.layout.abi {
238 abi::Abi::Scalar(ref scalar) => {
240 Primitive::Int(..) => {
241 if self.cx().size_of(ret_ty).bytes() < 4 {
242 // `va_arg` should not be called on a integer type
243 // less than 4 bytes in length. If it is, promote
244 // the integer to a `i32` and truncate the result
245 // back to the smaller type.
246 let promoted_result = emit_va_arg(self, args[0], tcx.types.i32);
247 self.trunc(promoted_result, llret_ty)
249 emit_va_arg(self, args[0], ret_ty)
252 Primitive::F64 | Primitive::Pointer => {
253 emit_va_arg(self, args[0], ret_ty)
255 // `va_arg` should never be used with the return type f32.
256 Primitive::F32 => bug!("the va_arg intrinsic does not work with `f32`"),
259 _ => bug!("the va_arg intrinsic does not work with non-scalar types"),
262 sym::size_of_val => {
263 let tp_ty = substs.type_at(0);
264 if let OperandValue::Pair(_, meta) = args[0].val {
265 let (llsize, _) = glue::size_and_align_of_dst(self, tp_ty, Some(meta));
268 self.const_usize(self.size_of(tp_ty).bytes())
271 sym::min_align_of_val => {
272 let tp_ty = substs.type_at(0);
273 if let OperandValue::Pair(_, meta) = args[0].val {
274 let (_, llalign) = glue::size_and_align_of_dst(self, tp_ty, Some(meta));
277 self.const_usize(self.align_of(tp_ty).bytes())
286 | sym::variant_count => {
289 .const_eval_instance(ty::ParamEnv::reveal_all(), instance, None)
291 OperandRef::from_const(self, value, ret_ty).immediate_or_packed_pair(self)
298 let ptr = args[0].immediate();
299 let offset = args[1].immediate();
300 self.inbounds_gep(ptr, &[offset])
302 sym::arith_offset => {
303 let ptr = args[0].immediate();
304 let offset = args[1].immediate();
305 self.gep(ptr, &[offset])
308 sym::copy_nonoverlapping => {
332 sym::write_bytes => {
344 sym::volatile_copy_nonoverlapping_memory => {
356 sym::volatile_copy_memory => {
368 sym::volatile_set_memory => {
379 sym::volatile_load | sym::unaligned_volatile_load => {
380 let tp_ty = substs.type_at(0);
381 let mut ptr = args[0].immediate();
382 if let PassMode::Cast(ty) = fn_abi.ret.mode {
383 ptr = self.pointercast(ptr, self.type_ptr_to(ty.llvm_type(self)));
385 let load = self.volatile_load(ptr);
386 let align = if name == sym::unaligned_volatile_load {
389 self.align_of(tp_ty).bytes() as u32
392 llvm::LLVMSetAlignment(load, align);
394 to_immediate(self, load, self.layout_of(tp_ty))
396 sym::volatile_store => {
397 let dst = args[0].deref(self.cx());
398 args[1].val.volatile_store(self, dst);
401 sym::unaligned_volatile_store => {
402 let dst = args[0].deref(self.cx());
403 args[1].val.unaligned_volatile_store(self, dst);
406 sym::prefetch_read_data
407 | sym::prefetch_write_data
408 | sym::prefetch_read_instruction
409 | sym::prefetch_write_instruction => {
410 let expect = self.get_intrinsic(&("llvm.prefetch"));
411 let (rw, cache_type) = match name {
412 sym::prefetch_read_data => (0, 1),
413 sym::prefetch_write_data => (1, 1),
414 sym::prefetch_read_instruction => (0, 0),
415 sym::prefetch_write_instruction => (1, 0),
424 self.const_i32(cache_type),
436 | sym::add_with_overflow
437 | sym::sub_with_overflow
438 | sym::mul_with_overflow
452 | sym::saturating_add
453 | sym::saturating_sub => {
455 match int_type_width_signed(ty, self) {
456 Some((width, signed)) => match name {
457 sym::ctlz | sym::cttz => {
458 let y = self.const_bool(false);
459 let llfn = self.get_intrinsic(&format!("llvm.{}.i{}", name, width));
460 self.call(llfn, &[args[0].immediate(), y], None)
462 sym::ctlz_nonzero | sym::cttz_nonzero => {
463 let y = self.const_bool(true);
464 let llvm_name = &format!("llvm.{}.i{}", &name_str[..4], width);
465 let llfn = self.get_intrinsic(llvm_name);
466 self.call(llfn, &[args[0].immediate(), y], None)
468 sym::ctpop => self.call(
469 self.get_intrinsic(&format!("llvm.ctpop.i{}", width)),
470 &[args[0].immediate()],
475 args[0].immediate() // byte swap a u8/i8 is just a no-op
478 self.get_intrinsic(&format!("llvm.bswap.i{}", width)),
479 &[args[0].immediate()],
484 sym::bitreverse => self.call(
485 self.get_intrinsic(&format!("llvm.bitreverse.i{}", width)),
486 &[args[0].immediate()],
489 sym::add_with_overflow
490 | sym::sub_with_overflow
491 | sym::mul_with_overflow => {
492 let intrinsic = format!(
493 "llvm.{}{}.with.overflow.i{}",
494 if signed { 's' } else { 'u' },
498 let llfn = self.get_intrinsic(&intrinsic);
500 // Convert `i1` to a `bool`, and write it to the out parameter
502 self.call(llfn, &[args[0].immediate(), args[1].immediate()], None);
503 let val = self.extract_value(pair, 0);
504 let overflow = self.extract_value(pair, 1);
505 let overflow = self.zext(overflow, self.type_bool());
507 let dest = result.project_field(self, 0);
508 self.store(val, dest.llval, dest.align);
509 let dest = result.project_field(self, 1);
510 self.store(overflow, dest.llval, dest.align);
514 sym::wrapping_add => self.add(args[0].immediate(), args[1].immediate()),
515 sym::wrapping_sub => self.sub(args[0].immediate(), args[1].immediate()),
516 sym::wrapping_mul => self.mul(args[0].immediate(), args[1].immediate()),
519 self.exactsdiv(args[0].immediate(), args[1].immediate())
521 self.exactudiv(args[0].immediate(), args[1].immediate())
524 sym::unchecked_div => {
526 self.sdiv(args[0].immediate(), args[1].immediate())
528 self.udiv(args[0].immediate(), args[1].immediate())
531 sym::unchecked_rem => {
533 self.srem(args[0].immediate(), args[1].immediate())
535 self.urem(args[0].immediate(), args[1].immediate())
538 sym::unchecked_shl => self.shl(args[0].immediate(), args[1].immediate()),
539 sym::unchecked_shr => {
541 self.ashr(args[0].immediate(), args[1].immediate())
543 self.lshr(args[0].immediate(), args[1].immediate())
546 sym::unchecked_add => {
548 self.unchecked_sadd(args[0].immediate(), args[1].immediate())
550 self.unchecked_uadd(args[0].immediate(), args[1].immediate())
553 sym::unchecked_sub => {
555 self.unchecked_ssub(args[0].immediate(), args[1].immediate())
557 self.unchecked_usub(args[0].immediate(), args[1].immediate())
560 sym::unchecked_mul => {
562 self.unchecked_smul(args[0].immediate(), args[1].immediate())
564 self.unchecked_umul(args[0].immediate(), args[1].immediate())
567 sym::rotate_left | sym::rotate_right => {
568 let is_left = name == sym::rotate_left;
569 let val = args[0].immediate();
570 let raw_shift = args[1].immediate();
571 // rotate = funnel shift with first two args the same
573 &format!("llvm.fsh{}.i{}", if is_left { 'l' } else { 'r' }, width);
574 let llfn = self.get_intrinsic(llvm_name);
575 self.call(llfn, &[val, val, raw_shift], None)
577 sym::saturating_add | sym::saturating_sub => {
578 let is_add = name == sym::saturating_add;
579 let lhs = args[0].immediate();
580 let rhs = args[1].immediate();
581 let llvm_name = &format!(
583 if signed { 's' } else { 'u' },
584 if is_add { "add" } else { "sub" },
587 let llfn = self.get_intrinsic(llvm_name);
588 self.call(llfn, &[lhs, rhs], None)
593 span_invalid_monomorphization_error(
597 "invalid monomorphization of `{}` intrinsic: \
598 expected basic integer type, found `{}`",
606 sym::fadd_fast | sym::fsub_fast | sym::fmul_fast | sym::fdiv_fast | sym::frem_fast => {
607 match float_type_width(arg_tys[0]) {
608 Some(_width) => match name {
609 sym::fadd_fast => self.fadd_fast(args[0].immediate(), args[1].immediate()),
610 sym::fsub_fast => self.fsub_fast(args[0].immediate(), args[1].immediate()),
611 sym::fmul_fast => self.fmul_fast(args[0].immediate(), args[1].immediate()),
612 sym::fdiv_fast => self.fdiv_fast(args[0].immediate(), args[1].immediate()),
613 sym::frem_fast => self.frem_fast(args[0].immediate(), args[1].immediate()),
617 span_invalid_monomorphization_error(
621 "invalid monomorphization of `{}` intrinsic: \
622 expected basic float type, found `{}`",
631 sym::float_to_int_unchecked => {
632 let float_width = match float_type_width(arg_tys[0]) {
633 Some(width) => width,
635 span_invalid_monomorphization_error(
639 "invalid monomorphization of `float_to_int_unchecked` \
640 intrinsic: expected basic float type, \
648 let (width, signed) = match int_type_width_signed(ret_ty, self.cx) {
651 span_invalid_monomorphization_error(
655 "invalid monomorphization of `float_to_int_unchecked` \
656 intrinsic: expected basic integer type, \
665 // The LLVM backend can reorder and speculate `fptosi` and
666 // `fptoui`, so on WebAssembly the codegen for this instruction
667 // is quite heavyweight. To avoid this heavyweight codegen we
668 // instead use the raw wasm intrinsics which will lower to one
669 // instruction in WebAssembly (`iNN.trunc_fMM_{s,u}`). This one
670 // instruction will trap if the operand is out of bounds, but
671 // that's ok since this intrinsic is UB if the operands are out
672 // of bounds, so the behavior can be different on WebAssembly
673 // than other targets.
675 // Note, however, that when the `nontrapping-fptoint` feature is
676 // enabled in LLVM then LLVM will lower `fptosi` to
677 // `iNN.trunc_sat_fMM_{s,u}`, so if that's the case we don't
678 // bother with intrinsics.
679 let mut result = None;
680 if self.sess().target.target.arch == "wasm32"
681 && !self.sess().target_features.contains(&sym::nontrapping_dash_fptoint)
683 let name = match (width, float_width, signed) {
684 (32, 32, true) => Some("llvm.wasm.trunc.signed.i32.f32"),
685 (32, 64, true) => Some("llvm.wasm.trunc.signed.i32.f64"),
686 (64, 32, true) => Some("llvm.wasm.trunc.signed.i64.f32"),
687 (64, 64, true) => Some("llvm.wasm.trunc.signed.i64.f64"),
688 (32, 32, false) => Some("llvm.wasm.trunc.unsigned.i32.f32"),
689 (32, 64, false) => Some("llvm.wasm.trunc.unsigned.i32.f64"),
690 (64, 32, false) => Some("llvm.wasm.trunc.unsigned.i64.f32"),
691 (64, 64, false) => Some("llvm.wasm.trunc.unsigned.i64.f64"),
694 if let Some(name) = name {
695 let intrinsic = self.get_intrinsic(name);
696 result = Some(self.call(intrinsic, &[args[0].immediate()], None));
699 result.unwrap_or_else(|| {
701 self.fptosi(args[0].immediate(), self.cx.type_ix(width))
703 self.fptoui(args[0].immediate(), self.cx.type_ix(width))
708 sym::discriminant_value => {
709 if ret_ty.is_integral() {
710 args[0].deref(self.cx()).codegen_get_discr(self, ret_ty)
712 span_bug!(span, "Invalid discriminant type for `{:?}`", arg_tys[0])
716 _ if name_str.starts_with("simd_") => {
717 match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) {
722 // This requires that atomic intrinsics follow a specific naming pattern:
723 // "atomic_<operation>[_<ordering>]", and no ordering means SeqCst
724 name if name_str.starts_with("atomic_") => {
725 use rustc_codegen_ssa::common::AtomicOrdering::*;
726 use rustc_codegen_ssa::common::{AtomicRmwBinOp, SynchronizationScope};
728 let split: Vec<&str> = name_str.split('_').collect();
730 let is_cxchg = split[1] == "cxchg" || split[1] == "cxchgweak";
731 let (order, failorder) = match split.len() {
732 2 => (SequentiallyConsistent, SequentiallyConsistent),
733 3 => match split[2] {
734 "unordered" => (Unordered, Unordered),
735 "relaxed" => (Monotonic, Monotonic),
736 "acq" => (Acquire, Acquire),
737 "rel" => (Release, Monotonic),
738 "acqrel" => (AcquireRelease, Acquire),
739 "failrelaxed" if is_cxchg => (SequentiallyConsistent, Monotonic),
740 "failacq" if is_cxchg => (SequentiallyConsistent, Acquire),
741 _ => self.sess().fatal("unknown ordering in atomic intrinsic"),
743 4 => match (split[2], split[3]) {
744 ("acq", "failrelaxed") if is_cxchg => (Acquire, Monotonic),
745 ("acqrel", "failrelaxed") if is_cxchg => (AcquireRelease, Monotonic),
746 _ => self.sess().fatal("unknown ordering in atomic intrinsic"),
748 _ => self.sess().fatal("Atomic intrinsic not in correct format"),
751 let invalid_monomorphization = |ty| {
752 span_invalid_monomorphization_error(
756 "invalid monomorphization of `{}` intrinsic: \
757 expected basic integer type, found `{}`",
764 "cxchg" | "cxchgweak" => {
765 let ty = substs.type_at(0);
766 if int_type_width_signed(ty, self).is_some() {
767 let weak = split[1] == "cxchgweak";
768 let pair = self.atomic_cmpxchg(
776 let val = self.extract_value(pair, 0);
777 let success = self.extract_value(pair, 1);
778 let success = self.zext(success, self.type_bool());
780 let dest = result.project_field(self, 0);
781 self.store(val, dest.llval, dest.align);
782 let dest = result.project_field(self, 1);
783 self.store(success, dest.llval, dest.align);
786 return invalid_monomorphization(ty);
791 let ty = substs.type_at(0);
792 if int_type_width_signed(ty, self).is_some() {
793 let size = self.size_of(ty);
794 self.atomic_load(args[0].immediate(), order, size)
796 return invalid_monomorphization(ty);
801 let ty = substs.type_at(0);
802 if int_type_width_signed(ty, self).is_some() {
803 let size = self.size_of(ty);
812 return invalid_monomorphization(ty);
817 self.atomic_fence(order, SynchronizationScope::CrossThread);
821 "singlethreadfence" => {
822 self.atomic_fence(order, SynchronizationScope::SingleThread);
826 // These are all AtomicRMW ops
828 let atom_op = match op {
829 "xchg" => AtomicRmwBinOp::AtomicXchg,
830 "xadd" => AtomicRmwBinOp::AtomicAdd,
831 "xsub" => AtomicRmwBinOp::AtomicSub,
832 "and" => AtomicRmwBinOp::AtomicAnd,
833 "nand" => AtomicRmwBinOp::AtomicNand,
834 "or" => AtomicRmwBinOp::AtomicOr,
835 "xor" => AtomicRmwBinOp::AtomicXor,
836 "max" => AtomicRmwBinOp::AtomicMax,
837 "min" => AtomicRmwBinOp::AtomicMin,
838 "umax" => AtomicRmwBinOp::AtomicUMax,
839 "umin" => AtomicRmwBinOp::AtomicUMin,
840 _ => self.sess().fatal("unknown atomic operation"),
843 let ty = substs.type_at(0);
844 if int_type_width_signed(ty, self).is_some() {
852 return invalid_monomorphization(ty);
858 sym::nontemporal_store => {
859 let dst = args[0].deref(self.cx());
860 args[1].val.nontemporal_store(self, dst);
864 sym::ptr_guaranteed_eq | sym::ptr_guaranteed_ne => {
865 let a = args[0].immediate();
866 let b = args[1].immediate();
867 if name == sym::ptr_guaranteed_eq {
868 self.icmp(IntPredicate::IntEQ, a, b)
870 self.icmp(IntPredicate::IntNE, a, b)
874 sym::ptr_offset_from => {
875 let ty = substs.type_at(0);
876 let pointee_size = self.size_of(ty);
878 // This is the same sequence that Clang emits for pointer subtraction.
879 // It can be neither `nsw` nor `nuw` because the input is treated as
880 // unsigned but then the output is treated as signed, so neither works.
881 let a = args[0].immediate();
882 let b = args[1].immediate();
883 let a = self.ptrtoint(a, self.type_isize());
884 let b = self.ptrtoint(b, self.type_isize());
885 let d = self.sub(a, b);
886 let pointee_size = self.const_usize(pointee_size.bytes());
887 // this is where the signed magic happens (notice the `s` in `exactsdiv`)
888 self.exactsdiv(d, pointee_size)
891 _ => bug!("unknown intrinsic '{}'", name),
894 if !fn_abi.ret.is_ignore() {
895 if let PassMode::Cast(ty) = fn_abi.ret.mode {
896 let ptr_llty = self.type_ptr_to(ty.llvm_type(self));
897 let ptr = self.pointercast(result.llval, ptr_llty);
898 self.store(llval, ptr, result.align);
900 OperandRef::from_immediate_or_packed_pair(self, llval, result.layout)
902 .store(self, result);
907 fn abort(&mut self) {
908 let fnname = self.get_intrinsic(&("llvm.trap"));
909 self.call(fnname, &[], None);
912 fn assume(&mut self, val: Self::Value) {
913 let assume_intrinsic = self.get_intrinsic("llvm.assume");
914 self.call(assume_intrinsic, &[val], None);
917 fn expect(&mut self, cond: Self::Value, expected: bool) -> Self::Value {
918 let expect = self.get_intrinsic(&"llvm.expect.i1");
919 self.call(expect, &[cond, self.const_bool(expected)], None)
922 fn sideeffect(&mut self) {
923 if self.tcx.sess.opts.debugging_opts.insert_sideeffect {
924 let fnname = self.get_intrinsic(&("llvm.sideeffect"));
925 self.call(fnname, &[], None);
929 fn va_start(&mut self, va_list: &'ll Value) -> &'ll Value {
930 let intrinsic = self.cx().get_intrinsic("llvm.va_start");
931 self.call(intrinsic, &[va_list], None)
934 fn va_end(&mut self, va_list: &'ll Value) -> &'ll Value {
935 let intrinsic = self.cx().get_intrinsic("llvm.va_end");
936 self.call(intrinsic, &[va_list], None)
941 bx: &mut Builder<'a, 'll, 'tcx>,
949 let (size, align) = bx.size_and_align_of(ty);
950 let size = bx.mul(bx.const_usize(size.bytes()), count);
951 let flags = if volatile { MemFlags::VOLATILE } else { MemFlags::empty() };
953 bx.memmove(dst, align, src, align, size, flags);
955 bx.memcpy(dst, align, src, align, size, flags);
960 bx: &mut Builder<'a, 'll, 'tcx>,
967 let (size, align) = bx.size_and_align_of(ty);
968 let size = bx.mul(bx.const_usize(size.bytes()), count);
969 let flags = if volatile { MemFlags::VOLATILE } else { MemFlags::empty() };
970 bx.memset(dst, val, size, align, flags);
974 bx: &mut Builder<'a, 'll, 'tcx>,
975 try_func: &'ll Value,
977 catch_func: &'ll Value,
980 if bx.sess().panic_strategy() == PanicStrategy::Abort {
981 bx.call(try_func, &[data], None);
982 // Return 0 unconditionally from the intrinsic call;
983 // we can never unwind.
984 let ret_align = bx.tcx().data_layout.i32_align.abi;
985 bx.store(bx.const_i32(0), dest, ret_align);
986 } else if wants_msvc_seh(bx.sess()) {
987 codegen_msvc_try(bx, try_func, data, catch_func, dest);
989 codegen_gnu_try(bx, try_func, data, catch_func, dest);
993 // MSVC's definition of the `rust_try` function.
995 // This implementation uses the new exception handling instructions in LLVM
996 // which have support in LLVM for SEH on MSVC targets. Although these
997 // instructions are meant to work for all targets, as of the time of this
998 // writing, however, LLVM does not recommend the usage of these new instructions
999 // as the old ones are still more optimized.
1000 fn codegen_msvc_try(
1001 bx: &mut Builder<'a, 'll, 'tcx>,
1002 try_func: &'ll Value,
1004 catch_func: &'ll Value,
1007 let llfn = get_rust_try_fn(bx, &mut |mut bx| {
1008 bx.set_personality_fn(bx.eh_personality());
1011 let mut normal = bx.build_sibling_block("normal");
1012 let mut catchswitch = bx.build_sibling_block("catchswitch");
1013 let mut catchpad = bx.build_sibling_block("catchpad");
1014 let mut caught = bx.build_sibling_block("caught");
1016 let try_func = llvm::get_param(bx.llfn(), 0);
1017 let data = llvm::get_param(bx.llfn(), 1);
1018 let catch_func = llvm::get_param(bx.llfn(), 2);
1020 // We're generating an IR snippet that looks like:
1022 // declare i32 @rust_try(%try_func, %data, %catch_func) {
1023 // %slot = alloca u8*
1024 // invoke %try_func(%data) to label %normal unwind label %catchswitch
1030 // %cs = catchswitch within none [%catchpad] unwind to caller
1033 // %tok = catchpad within %cs [%type_descriptor, 0, %slot]
1034 // %ptr = load %slot
1035 // call %catch_func(%data, %ptr)
1036 // catchret from %tok to label %caught
1042 // This structure follows the basic usage of throw/try/catch in LLVM.
1043 // For example, compile this C++ snippet to see what LLVM generates:
1045 // #include <stdint.h>
1047 // struct rust_panic {
1048 // rust_panic(const rust_panic&);
1055 // void (*try_func)(void*),
1057 // void (*catch_func)(void*, void*) noexcept
1062 // } catch(rust_panic& a) {
1063 // catch_func(data, &a);
1068 // More information can be found in libstd's seh.rs implementation.
1069 let ptr_align = bx.tcx().data_layout.pointer_align.abi;
1070 let slot = bx.alloca(bx.type_i8p(), ptr_align);
1071 bx.invoke(try_func, &[data], normal.llbb(), catchswitch.llbb(), None);
1073 normal.ret(bx.const_i32(0));
1075 let cs = catchswitch.catch_switch(None, None, 1);
1076 catchswitch.add_handler(cs, catchpad.llbb());
1078 // We can't use the TypeDescriptor defined in libpanic_unwind because it
1079 // might be in another DLL and the SEH encoding only supports specifying
1080 // a TypeDescriptor from the current module.
1082 // However this isn't an issue since the MSVC runtime uses string
1083 // comparison on the type name to match TypeDescriptors rather than
1084 // pointer equality.
1086 // So instead we generate a new TypeDescriptor in each module that uses
1087 // `try` and let the linker merge duplicate definitions in the same
1090 // When modifying, make sure that the type_name string exactly matches
1091 // the one used in src/libpanic_unwind/seh.rs.
1092 let type_info_vtable = bx.declare_global("??_7type_info@@6B@", bx.type_i8p());
1093 let type_name = bx.const_bytes(b"rust_panic\0");
1095 bx.const_struct(&[type_info_vtable, bx.const_null(bx.type_i8p()), type_name], false);
1096 let tydesc = bx.declare_global("__rust_panic_type_info", bx.val_ty(type_info));
1098 llvm::LLVMRustSetLinkage(tydesc, llvm::Linkage::LinkOnceODRLinkage);
1099 llvm::SetUniqueComdat(bx.llmod, tydesc);
1100 llvm::LLVMSetInitializer(tydesc, type_info);
1103 // The flag value of 8 indicates that we are catching the exception by
1104 // reference instead of by value. We can't use catch by value because
1105 // that requires copying the exception object, which we don't support
1106 // since our exception object effectively contains a Box.
1108 // Source: MicrosoftCXXABI::getAddrOfCXXCatchHandlerType in clang
1109 let flags = bx.const_i32(8);
1110 let funclet = catchpad.catch_pad(cs, &[tydesc, flags, slot]);
1111 let ptr = catchpad.load(slot, ptr_align);
1112 catchpad.call(catch_func, &[data, ptr], Some(&funclet));
1114 catchpad.catch_ret(&funclet, caught.llbb());
1116 caught.ret(bx.const_i32(1));
1119 // Note that no invoke is used here because by definition this function
1120 // can't panic (that's what it's catching).
1121 let ret = bx.call(llfn, &[try_func, data, catch_func], None);
1122 let i32_align = bx.tcx().data_layout.i32_align.abi;
1123 bx.store(ret, dest, i32_align);
1126 // Definition of the standard `try` function for Rust using the GNU-like model
1127 // of exceptions (e.g., the normal semantics of LLVM's `landingpad` and `invoke`
1130 // This codegen is a little surprising because we always call a shim
1131 // function instead of inlining the call to `invoke` manually here. This is done
1132 // because in LLVM we're only allowed to have one personality per function
1133 // definition. The call to the `try` intrinsic is being inlined into the
1134 // function calling it, and that function may already have other personality
1135 // functions in play. By calling a shim we're guaranteed that our shim will have
1136 // the right personality function.
1138 bx: &mut Builder<'a, 'll, 'tcx>,
1139 try_func: &'ll Value,
1141 catch_func: &'ll Value,
1144 let llfn = get_rust_try_fn(bx, &mut |mut bx| {
1145 // Codegens the shims described above:
1148 // invoke %try_func(%data) normal %normal unwind %catch
1154 // (%ptr, _) = landingpad
1155 // call %catch_func(%data, %ptr)
1160 let mut then = bx.build_sibling_block("then");
1161 let mut catch = bx.build_sibling_block("catch");
1163 let try_func = llvm::get_param(bx.llfn(), 0);
1164 let data = llvm::get_param(bx.llfn(), 1);
1165 let catch_func = llvm::get_param(bx.llfn(), 2);
1166 bx.invoke(try_func, &[data], then.llbb(), catch.llbb(), None);
1167 then.ret(bx.const_i32(0));
1169 // Type indicator for the exception being thrown.
1171 // The first value in this tuple is a pointer to the exception object
1172 // being thrown. The second value is a "selector" indicating which of
1173 // the landing pad clauses the exception's type had been matched to.
1174 // rust_try ignores the selector.
1175 let lpad_ty = bx.type_struct(&[bx.type_i8p(), bx.type_i32()], false);
1176 let vals = catch.landing_pad(lpad_ty, bx.eh_personality(), 1);
1177 let tydesc = match bx.tcx().lang_items().eh_catch_typeinfo() {
1179 let tydesc = bx.get_static(tydesc);
1180 bx.bitcast(tydesc, bx.type_i8p())
1182 None => bx.const_null(bx.type_i8p()),
1184 catch.add_clause(vals, tydesc);
1185 let ptr = catch.extract_value(vals, 0);
1186 catch.call(catch_func, &[data, ptr], None);
1187 catch.ret(bx.const_i32(1));
1190 // Note that no invoke is used here because by definition this function
1191 // can't panic (that's what it's catching).
1192 let ret = bx.call(llfn, &[try_func, data, catch_func], None);
1193 let i32_align = bx.tcx().data_layout.i32_align.abi;
1194 bx.store(ret, dest, i32_align);
1197 // Helper function to give a Block to a closure to codegen a shim function.
1198 // This is currently primarily used for the `try` intrinsic functions above.
1199 fn gen_fn<'ll, 'tcx>(
1200 cx: &CodegenCx<'ll, 'tcx>,
1202 inputs: Vec<Ty<'tcx>>,
1204 codegen: &mut dyn FnMut(Builder<'_, 'll, 'tcx>),
1206 let rust_fn_sig = ty::Binder::bind(cx.tcx.mk_fn_sig(
1210 hir::Unsafety::Unsafe,
1213 let fn_abi = FnAbi::of_fn_ptr(cx, rust_fn_sig, &[]);
1214 let llfn = cx.declare_fn(name, &fn_abi);
1215 cx.set_frame_pointer_elimination(llfn);
1216 cx.apply_target_cpu_attr(llfn);
1217 // FIXME(eddyb) find a nicer way to do this.
1218 unsafe { llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::InternalLinkage) };
1219 let bx = Builder::new_block(cx, llfn, "entry-block");
1224 // Helper function used to get a handle to the `__rust_try` function used to
1225 // catch exceptions.
1227 // This function is only generated once and is then cached.
1228 fn get_rust_try_fn<'ll, 'tcx>(
1229 cx: &CodegenCx<'ll, 'tcx>,
1230 codegen: &mut dyn FnMut(Builder<'_, 'll, 'tcx>),
1232 if let Some(llfn) = cx.rust_try_fn.get() {
1236 // Define the type up front for the signature of the rust_try function.
1238 let i8p = tcx.mk_mut_ptr(tcx.types.i8);
1239 let try_fn_ty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
1243 hir::Unsafety::Unsafe,
1246 let catch_fn_ty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
1247 [i8p, i8p].iter().cloned(),
1250 hir::Unsafety::Unsafe,
1253 let output = tcx.types.i32;
1254 let rust_try = gen_fn(cx, "__rust_try", vec![try_fn_ty, i8p, catch_fn_ty], output, codegen);
1255 cx.rust_try_fn.set(Some(rust_try));
1259 fn generic_simd_intrinsic(
1260 bx: &mut Builder<'a, 'll, 'tcx>,
1262 callee_ty: Ty<'tcx>,
1263 args: &[OperandRef<'tcx, &'ll Value>],
1265 llret_ty: &'ll Type,
1267 ) -> Result<&'ll Value, ()> {
1268 // macros for error handling:
1269 macro_rules! emit_error {
1273 ($msg: tt, $($fmt: tt)*) => {
1274 span_invalid_monomorphization_error(
1276 &format!(concat!("invalid monomorphization of `{}` intrinsic: ", $msg),
1281 macro_rules! return_error {
1284 emit_error!($($fmt)*);
1290 macro_rules! require {
1291 ($cond: expr, $($fmt: tt)*) => {
1293 return_error!($($fmt)*);
1298 macro_rules! require_simd {
1299 ($ty: expr, $position: expr) => {
1300 require!($ty.is_simd(), "expected SIMD {} type, found non-SIMD `{}`", $position, $ty)
1306 .normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &callee_ty.fn_sig(tcx));
1307 let arg_tys = sig.inputs();
1308 let name_str = &*name.as_str();
1310 if name == sym::simd_select_bitmask {
1311 let in_ty = arg_tys[0];
1312 let m_len = match in_ty.kind {
1313 // Note that this `.unwrap()` crashes for isize/usize, that's sort
1314 // of intentional as there's not currently a use case for that.
1315 ty::Int(i) => i.bit_width().unwrap(),
1316 ty::Uint(i) => i.bit_width().unwrap(),
1317 _ => return_error!("`{}` is not an integral type", in_ty),
1319 require_simd!(arg_tys[1], "argument");
1320 let v_len = arg_tys[1].simd_size(tcx);
1323 "mismatched lengths: mask length `{}` != other vector length `{}`",
1327 let i1 = bx.type_i1();
1328 let i1xn = bx.type_vector(i1, m_len);
1329 let m_i1s = bx.bitcast(args[0].immediate(), i1xn);
1330 return Ok(bx.select(m_i1s, args[1].immediate(), args[2].immediate()));
1333 // every intrinsic below takes a SIMD vector as its first argument
1334 require_simd!(arg_tys[0], "input");
1335 let in_ty = arg_tys[0];
1336 let in_elem = arg_tys[0].simd_type(tcx);
1337 let in_len = arg_tys[0].simd_size(tcx);
1339 let comparison = match name {
1340 sym::simd_eq => Some(hir::BinOpKind::Eq),
1341 sym::simd_ne => Some(hir::BinOpKind::Ne),
1342 sym::simd_lt => Some(hir::BinOpKind::Lt),
1343 sym::simd_le => Some(hir::BinOpKind::Le),
1344 sym::simd_gt => Some(hir::BinOpKind::Gt),
1345 sym::simd_ge => Some(hir::BinOpKind::Ge),
1349 if let Some(cmp_op) = comparison {
1350 require_simd!(ret_ty, "return");
1352 let out_len = ret_ty.simd_size(tcx);
1355 "expected return type with length {} (same as input type `{}`), \
1356 found `{}` with length {}",
1363 bx.type_kind(bx.element_type(llret_ty)) == TypeKind::Integer,
1364 "expected return type with integer elements, found `{}` with non-integer `{}`",
1366 ret_ty.simd_type(tcx)
1369 return Ok(compare_simd_types(
1371 args[0].immediate(),
1372 args[1].immediate(),
1379 if name_str.starts_with("simd_shuffle") {
1380 let n: u64 = name_str["simd_shuffle".len()..].parse().unwrap_or_else(|_| {
1381 span_bug!(span, "bad `simd_shuffle` instruction only caught in codegen?")
1384 require_simd!(ret_ty, "return");
1386 let out_len = ret_ty.simd_size(tcx);
1389 "expected return type of length {}, found `{}` with length {}",
1395 in_elem == ret_ty.simd_type(tcx),
1396 "expected return element type `{}` (element of input `{}`), \
1397 found `{}` with element type `{}`",
1401 ret_ty.simd_type(tcx)
1404 let total_len = u128::from(in_len) * 2;
1406 let vector = args[2].immediate();
1408 let indices: Option<Vec<_>> = (0..n)
1411 let val = bx.const_get_elt(vector, i as u64);
1412 match bx.const_to_opt_u128(val, true) {
1414 emit_error!("shuffle index #{} is not a constant", arg_idx);
1417 Some(idx) if idx >= total_len => {
1419 "shuffle index #{} is out of bounds (limit {})",
1425 Some(idx) => Some(bx.const_i32(idx as i32)),
1429 let indices = match indices {
1431 None => return Ok(bx.const_null(llret_ty)),
1434 return Ok(bx.shuffle_vector(
1435 args[0].immediate(),
1436 args[1].immediate(),
1437 bx.const_vector(&indices),
1441 if name == sym::simd_insert {
1443 in_elem == arg_tys[2],
1444 "expected inserted type `{}` (element of input `{}`), found `{}`",
1449 return Ok(bx.insert_element(
1450 args[0].immediate(),
1451 args[2].immediate(),
1452 args[1].immediate(),
1455 if name == sym::simd_extract {
1458 "expected return type `{}` (element of input `{}`), found `{}`",
1463 return Ok(bx.extract_element(args[0].immediate(), args[1].immediate()));
1466 if name == sym::simd_select {
1467 let m_elem_ty = in_elem;
1469 require_simd!(arg_tys[1], "argument");
1470 let v_len = arg_tys[1].simd_size(tcx);
1473 "mismatched lengths: mask length `{}` != other vector length `{}`",
1477 match m_elem_ty.kind {
1479 _ => return_error!("mask element type is `{}`, expected `i_`", m_elem_ty),
1481 // truncate the mask to a vector of i1s
1482 let i1 = bx.type_i1();
1483 let i1xn = bx.type_vector(i1, m_len as u64);
1484 let m_i1s = bx.trunc(args[0].immediate(), i1xn);
1485 return Ok(bx.select(m_i1s, args[1].immediate(), args[2].immediate()));
1488 if name == sym::simd_bitmask {
1489 // The `fn simd_bitmask(vector) -> unsigned integer` intrinsic takes a
1490 // vector mask and returns an unsigned integer containing the most
1491 // significant bit (MSB) of each lane.
1493 // If the vector has less than 8 lanes, an u8 is returned with zeroed
1495 let expected_int_bits = in_len.max(8);
1497 ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => (),
1498 _ => return_error!("bitmask `{}`, expected `u{}`", ret_ty, expected_int_bits),
1501 // Integer vector <i{in_bitwidth} x in_len>:
1502 let (i_xn, in_elem_bitwidth) = match in_elem.kind {
1504 (args[0].immediate(), i.bit_width().unwrap_or(bx.data_layout().pointer_size.bits()))
1507 (args[0].immediate(), i.bit_width().unwrap_or(bx.data_layout().pointer_size.bits()))
1510 "vector argument `{}`'s element type `{}`, expected integer element type",
1516 // Shift the MSB to the right by "in_elem_bitwidth - 1" into the first bit position.
1519 bx.cx.const_int(bx.type_ix(in_elem_bitwidth), (in_elem_bitwidth - 1) as _);
1522 let i_xn_msb = bx.lshr(i_xn, bx.const_vector(shift_indices.as_slice()));
1523 // Truncate vector to an <i1 x N>
1524 let i1xn = bx.trunc(i_xn_msb, bx.type_vector(bx.type_i1(), in_len));
1525 // Bitcast <i1 x N> to iN:
1526 let i_ = bx.bitcast(i1xn, bx.type_ix(in_len));
1527 // Zero-extend iN to the bitmask type:
1528 return Ok(bx.zext(i_, bx.type_ix(expected_int_bits)));
1531 fn simd_simple_float_intrinsic(
1533 in_elem: &::rustc_middle::ty::TyS<'_>,
1534 in_ty: &::rustc_middle::ty::TyS<'_>,
1536 bx: &mut Builder<'a, 'll, 'tcx>,
1538 args: &[OperandRef<'tcx, &'ll Value>],
1539 ) -> Result<&'ll Value, ()> {
1540 macro_rules! emit_error {
1544 ($msg: tt, $($fmt: tt)*) => {
1545 span_invalid_monomorphization_error(
1547 &format!(concat!("invalid monomorphization of `{}` intrinsic: ", $msg),
1551 macro_rules! return_error {
1554 emit_error!($($fmt)*);
1559 let ety = match in_elem.kind {
1560 ty::Float(f) if f.bit_width() == 32 => {
1561 if in_len < 2 || in_len > 16 {
1563 "unsupported floating-point vector `{}` with length `{}` \
1564 out-of-range [2, 16]",
1571 ty::Float(f) if f.bit_width() == 64 => {
1572 if in_len < 2 || in_len > 8 {
1574 "unsupported floating-point vector `{}` with length `{}` \
1575 out-of-range [2, 8]",
1584 "unsupported element type `{}` of floating-point vector `{}`",
1590 return_error!("`{}` is not a floating-point type", in_ty);
1594 let llvm_name = &format!("llvm.{0}.v{1}{2}", name, in_len, ety);
1595 let intrinsic = bx.get_intrinsic(&llvm_name);
1597 bx.call(intrinsic, &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(), None);
1598 unsafe { llvm::LLVMRustSetHasUnsafeAlgebra(c) };
1603 sym::simd_fsqrt => {
1604 return simd_simple_float_intrinsic("sqrt", in_elem, in_ty, in_len, bx, span, args);
1607 return simd_simple_float_intrinsic("sin", in_elem, in_ty, in_len, bx, span, args);
1610 return simd_simple_float_intrinsic("cos", in_elem, in_ty, in_len, bx, span, args);
1613 return simd_simple_float_intrinsic("fabs", in_elem, in_ty, in_len, bx, span, args);
1615 sym::simd_floor => {
1616 return simd_simple_float_intrinsic("floor", in_elem, in_ty, in_len, bx, span, args);
1619 return simd_simple_float_intrinsic("ceil", in_elem, in_ty, in_len, bx, span, args);
1622 return simd_simple_float_intrinsic("exp", in_elem, in_ty, in_len, bx, span, args);
1624 sym::simd_fexp2 => {
1625 return simd_simple_float_intrinsic("exp2", in_elem, in_ty, in_len, bx, span, args);
1627 sym::simd_flog10 => {
1628 return simd_simple_float_intrinsic("log10", in_elem, in_ty, in_len, bx, span, args);
1630 sym::simd_flog2 => {
1631 return simd_simple_float_intrinsic("log2", in_elem, in_ty, in_len, bx, span, args);
1634 return simd_simple_float_intrinsic("log", in_elem, in_ty, in_len, bx, span, args);
1636 sym::simd_fpowi => {
1637 return simd_simple_float_intrinsic("powi", in_elem, in_ty, in_len, bx, span, args);
1640 return simd_simple_float_intrinsic("pow", in_elem, in_ty, in_len, bx, span, args);
1643 return simd_simple_float_intrinsic("fma", in_elem, in_ty, in_len, bx, span, args);
1645 _ => { /* fallthrough */ }
1649 // https://github.com/llvm-mirror/llvm/blob/master/include/llvm/IR/Function.h#L182
1650 // https://github.com/llvm-mirror/llvm/blob/master/include/llvm/IR/Intrinsics.h#L81
1651 fn llvm_vector_str(elem_ty: Ty<'_>, vec_len: u64, no_pointers: usize) -> String {
1652 let p0s: String = "p0".repeat(no_pointers);
1653 match elem_ty.kind {
1654 ty::Int(v) => format!("v{}{}i{}", vec_len, p0s, v.bit_width().unwrap()),
1655 ty::Uint(v) => format!("v{}{}i{}", vec_len, p0s, v.bit_width().unwrap()),
1656 ty::Float(v) => format!("v{}{}f{}", vec_len, p0s, v.bit_width()),
1657 _ => unreachable!(),
1662 cx: &CodegenCx<'ll, '_>,
1665 mut no_pointers: usize,
1667 // FIXME: use cx.layout_of(ty).llvm_type() ?
1668 let mut elem_ty = match elem_ty.kind {
1669 ty::Int(v) => cx.type_int_from_ty(v),
1670 ty::Uint(v) => cx.type_uint_from_ty(v),
1671 ty::Float(v) => cx.type_float_from_ty(v),
1672 _ => unreachable!(),
1674 while no_pointers > 0 {
1675 elem_ty = cx.type_ptr_to(elem_ty);
1678 cx.type_vector(elem_ty, vec_len)
1681 if name == sym::simd_gather {
1682 // simd_gather(values: <N x T>, pointers: <N x *_ T>,
1683 // mask: <N x i{M}>) -> <N x T>
1684 // * N: number of elements in the input vectors
1685 // * T: type of the element to load
1686 // * M: any integer width is supported, will be truncated to i1
1688 // All types must be simd vector types
1689 require_simd!(in_ty, "first");
1690 require_simd!(arg_tys[1], "second");
1691 require_simd!(arg_tys[2], "third");
1692 require_simd!(ret_ty, "return");
1694 // Of the same length:
1696 in_len == arg_tys[1].simd_size(tcx),
1697 "expected {} argument with length {} (same as input type `{}`), \
1698 found `{}` with length {}",
1703 arg_tys[1].simd_size(tcx)
1706 in_len == arg_tys[2].simd_size(tcx),
1707 "expected {} argument with length {} (same as input type `{}`), \
1708 found `{}` with length {}",
1713 arg_tys[2].simd_size(tcx)
1716 // The return type must match the first argument type
1717 require!(ret_ty == in_ty, "expected return type `{}`, found `{}`", in_ty, ret_ty);
1719 // This counts how many pointers
1720 fn ptr_count(t: Ty<'_>) -> usize {
1722 ty::RawPtr(p) => 1 + ptr_count(p.ty),
1728 fn non_ptr(t: Ty<'_>) -> Ty<'_> {
1730 ty::RawPtr(p) => non_ptr(p.ty),
1735 // The second argument must be a simd vector with an element type that's a pointer
1736 // to the element type of the first argument
1737 let (pointer_count, underlying_ty) = match arg_tys[1].simd_type(tcx).kind {
1738 ty::RawPtr(p) if p.ty == in_elem => {
1739 (ptr_count(arg_tys[1].simd_type(tcx)), non_ptr(arg_tys[1].simd_type(tcx)))
1744 "expected element type `{}` of second argument `{}` \
1745 to be a pointer to the element type `{}` of the first \
1746 argument `{}`, found `{}` != `*_ {}`",
1747 arg_tys[1].simd_type(tcx),
1751 arg_tys[1].simd_type(tcx),
1757 assert!(pointer_count > 0);
1758 assert_eq!(pointer_count - 1, ptr_count(arg_tys[0].simd_type(tcx)));
1759 assert_eq!(underlying_ty, non_ptr(arg_tys[0].simd_type(tcx)));
1761 // The element type of the third argument must be a signed integer type of any width:
1762 match arg_tys[2].simd_type(tcx).kind {
1767 "expected element type `{}` of third argument `{}` \
1768 to be a signed integer type",
1769 arg_tys[2].simd_type(tcx),
1775 // Alignment of T, must be a constant integer value:
1776 let alignment_ty = bx.type_i32();
1777 let alignment = bx.const_i32(bx.align_of(in_elem).bytes() as i32);
1779 // Truncate the mask vector to a vector of i1s:
1780 let (mask, mask_ty) = {
1781 let i1 = bx.type_i1();
1782 let i1xn = bx.type_vector(i1, in_len);
1783 (bx.trunc(args[2].immediate(), i1xn), i1xn)
1786 // Type of the vector of pointers:
1787 let llvm_pointer_vec_ty = llvm_vector_ty(bx, underlying_ty, in_len, pointer_count);
1788 let llvm_pointer_vec_str = llvm_vector_str(underlying_ty, in_len, pointer_count);
1790 // Type of the vector of elements:
1791 let llvm_elem_vec_ty = llvm_vector_ty(bx, underlying_ty, in_len, pointer_count - 1);
1792 let llvm_elem_vec_str = llvm_vector_str(underlying_ty, in_len, pointer_count - 1);
1794 let llvm_intrinsic =
1795 format!("llvm.masked.gather.{}.{}", llvm_elem_vec_str, llvm_pointer_vec_str);
1796 let f = bx.declare_cfn(
1799 &[llvm_pointer_vec_ty, alignment_ty, mask_ty, llvm_elem_vec_ty],
1803 llvm::SetUnnamedAddress(f, llvm::UnnamedAddr::No);
1804 let v = bx.call(f, &[args[1].immediate(), alignment, mask, args[0].immediate()], None);
1808 if name == sym::simd_scatter {
1809 // simd_scatter(values: <N x T>, pointers: <N x *mut T>,
1810 // mask: <N x i{M}>) -> ()
1811 // * N: number of elements in the input vectors
1812 // * T: type of the element to load
1813 // * M: any integer width is supported, will be truncated to i1
1815 // All types must be simd vector types
1816 require_simd!(in_ty, "first");
1817 require_simd!(arg_tys[1], "second");
1818 require_simd!(arg_tys[2], "third");
1820 // Of the same length:
1822 in_len == arg_tys[1].simd_size(tcx),
1823 "expected {} argument with length {} (same as input type `{}`), \
1824 found `{}` with length {}",
1829 arg_tys[1].simd_size(tcx)
1832 in_len == arg_tys[2].simd_size(tcx),
1833 "expected {} argument with length {} (same as input type `{}`), \
1834 found `{}` with length {}",
1839 arg_tys[2].simd_size(tcx)
1842 // This counts how many pointers
1843 fn ptr_count(t: Ty<'_>) -> usize {
1845 ty::RawPtr(p) => 1 + ptr_count(p.ty),
1851 fn non_ptr(t: Ty<'_>) -> Ty<'_> {
1853 ty::RawPtr(p) => non_ptr(p.ty),
1858 // The second argument must be a simd vector with an element type that's a pointer
1859 // to the element type of the first argument
1860 let (pointer_count, underlying_ty) = match arg_tys[1].simd_type(tcx).kind {
1861 ty::RawPtr(p) if p.ty == in_elem && p.mutbl == hir::Mutability::Mut => {
1862 (ptr_count(arg_tys[1].simd_type(tcx)), non_ptr(arg_tys[1].simd_type(tcx)))
1867 "expected element type `{}` of second argument `{}` \
1868 to be a pointer to the element type `{}` of the first \
1869 argument `{}`, found `{}` != `*mut {}`",
1870 arg_tys[1].simd_type(tcx),
1874 arg_tys[1].simd_type(tcx),
1880 assert!(pointer_count > 0);
1881 assert_eq!(pointer_count - 1, ptr_count(arg_tys[0].simd_type(tcx)));
1882 assert_eq!(underlying_ty, non_ptr(arg_tys[0].simd_type(tcx)));
1884 // The element type of the third argument must be a signed integer type of any width:
1885 match arg_tys[2].simd_type(tcx).kind {
1890 "expected element type `{}` of third argument `{}` \
1891 to be a signed integer type",
1892 arg_tys[2].simd_type(tcx),
1898 // Alignment of T, must be a constant integer value:
1899 let alignment_ty = bx.type_i32();
1900 let alignment = bx.const_i32(bx.align_of(in_elem).bytes() as i32);
1902 // Truncate the mask vector to a vector of i1s:
1903 let (mask, mask_ty) = {
1904 let i1 = bx.type_i1();
1905 let i1xn = bx.type_vector(i1, in_len);
1906 (bx.trunc(args[2].immediate(), i1xn), i1xn)
1909 let ret_t = bx.type_void();
1911 // Type of the vector of pointers:
1912 let llvm_pointer_vec_ty = llvm_vector_ty(bx, underlying_ty, in_len, pointer_count);
1913 let llvm_pointer_vec_str = llvm_vector_str(underlying_ty, in_len, pointer_count);
1915 // Type of the vector of elements:
1916 let llvm_elem_vec_ty = llvm_vector_ty(bx, underlying_ty, in_len, pointer_count - 1);
1917 let llvm_elem_vec_str = llvm_vector_str(underlying_ty, in_len, pointer_count - 1);
1919 let llvm_intrinsic =
1920 format!("llvm.masked.scatter.{}.{}", llvm_elem_vec_str, llvm_pointer_vec_str);
1921 let f = bx.declare_cfn(
1923 bx.type_func(&[llvm_elem_vec_ty, llvm_pointer_vec_ty, alignment_ty, mask_ty], ret_t),
1925 llvm::SetUnnamedAddress(f, llvm::UnnamedAddr::No);
1926 let v = bx.call(f, &[args[0].immediate(), args[1].immediate(), alignment, mask], None);
1930 macro_rules! arith_red {
1931 ($name:ident : $integer_reduce:ident, $float_reduce:ident, $ordered:expr, $op:ident,
1932 $identity:expr) => {
1933 if name == sym::$name {
1936 "expected return type `{}` (element of input `{}`), found `{}`",
1941 return match in_elem.kind {
1942 ty::Int(_) | ty::Uint(_) => {
1943 let r = bx.$integer_reduce(args[0].immediate());
1945 // if overflow occurs, the result is the
1946 // mathematical result modulo 2^n:
1947 Ok(bx.$op(args[1].immediate(), r))
1949 Ok(bx.$integer_reduce(args[0].immediate()))
1953 let acc = if $ordered {
1954 // ordered arithmetic reductions take an accumulator
1957 // unordered arithmetic reductions use the identity accumulator
1958 match f.bit_width() {
1959 32 => bx.const_real(bx.type_f32(), $identity),
1960 64 => bx.const_real(bx.type_f64(), $identity),
1963 unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#,
1972 Ok(bx.$float_reduce(acc, args[0].immediate()))
1975 "unsupported {} from `{}` with element `{}` to `{}`",
1986 arith_red!(simd_reduce_add_ordered: vector_reduce_add, vector_reduce_fadd, true, add, 0.0);
1987 arith_red!(simd_reduce_mul_ordered: vector_reduce_mul, vector_reduce_fmul, true, mul, 1.0);
1989 simd_reduce_add_unordered: vector_reduce_add,
1990 vector_reduce_fadd_fast,
1996 simd_reduce_mul_unordered: vector_reduce_mul,
1997 vector_reduce_fmul_fast,
2003 macro_rules! minmax_red {
2004 ($name:ident: $int_red:ident, $float_red:ident) => {
2005 if name == sym::$name {
2008 "expected return type `{}` (element of input `{}`), found `{}`",
2013 return match in_elem.kind {
2014 ty::Int(_i) => Ok(bx.$int_red(args[0].immediate(), true)),
2015 ty::Uint(_u) => Ok(bx.$int_red(args[0].immediate(), false)),
2016 ty::Float(_f) => Ok(bx.$float_red(args[0].immediate())),
2018 "unsupported {} from `{}` with element `{}` to `{}`",
2029 minmax_red!(simd_reduce_min: vector_reduce_min, vector_reduce_fmin);
2030 minmax_red!(simd_reduce_max: vector_reduce_max, vector_reduce_fmax);
2032 minmax_red!(simd_reduce_min_nanless: vector_reduce_min, vector_reduce_fmin_fast);
2033 minmax_red!(simd_reduce_max_nanless: vector_reduce_max, vector_reduce_fmax_fast);
2035 macro_rules! bitwise_red {
2036 ($name:ident : $red:ident, $boolean:expr) => {
2037 if name == sym::$name {
2038 let input = if !$boolean {
2041 "expected return type `{}` (element of input `{}`), found `{}`",
2048 match in_elem.kind {
2049 ty::Int(_) | ty::Uint(_) => {}
2051 "unsupported {} from `{}` with element `{}` to `{}`",
2059 // boolean reductions operate on vectors of i1s:
2060 let i1 = bx.type_i1();
2061 let i1xn = bx.type_vector(i1, in_len as u64);
2062 bx.trunc(args[0].immediate(), i1xn)
2064 return match in_elem.kind {
2065 ty::Int(_) | ty::Uint(_) => {
2066 let r = bx.$red(input);
2067 Ok(if !$boolean { r } else { bx.zext(r, bx.type_bool()) })
2070 "unsupported {} from `{}` with element `{}` to `{}`",
2081 bitwise_red!(simd_reduce_and: vector_reduce_and, false);
2082 bitwise_red!(simd_reduce_or: vector_reduce_or, false);
2083 bitwise_red!(simd_reduce_xor: vector_reduce_xor, false);
2084 bitwise_red!(simd_reduce_all: vector_reduce_and, true);
2085 bitwise_red!(simd_reduce_any: vector_reduce_or, true);
2087 if name == sym::simd_cast {
2088 require_simd!(ret_ty, "return");
2089 let out_len = ret_ty.simd_size(tcx);
2092 "expected return type with length {} (same as input type `{}`), \
2093 found `{}` with length {}",
2099 // casting cares about nominal type, not just structural type
2100 let out_elem = ret_ty.simd_type(tcx);
2102 if in_elem == out_elem {
2103 return Ok(args[0].immediate());
2108 Int(/* is signed? */ bool),
2112 let (in_style, in_width) = match in_elem.kind {
2113 // vectors of pointer-sized integers should've been
2114 // disallowed before here, so this unwrap is safe.
2115 ty::Int(i) => (Style::Int(true), i.bit_width().unwrap()),
2116 ty::Uint(u) => (Style::Int(false), u.bit_width().unwrap()),
2117 ty::Float(f) => (Style::Float, f.bit_width()),
2118 _ => (Style::Unsupported, 0),
2120 let (out_style, out_width) = match out_elem.kind {
2121 ty::Int(i) => (Style::Int(true), i.bit_width().unwrap()),
2122 ty::Uint(u) => (Style::Int(false), u.bit_width().unwrap()),
2123 ty::Float(f) => (Style::Float, f.bit_width()),
2124 _ => (Style::Unsupported, 0),
2127 match (in_style, out_style) {
2128 (Style::Int(in_is_signed), Style::Int(_)) => {
2129 return Ok(match in_width.cmp(&out_width) {
2130 Ordering::Greater => bx.trunc(args[0].immediate(), llret_ty),
2131 Ordering::Equal => args[0].immediate(),
2134 bx.sext(args[0].immediate(), llret_ty)
2136 bx.zext(args[0].immediate(), llret_ty)
2141 (Style::Int(in_is_signed), Style::Float) => {
2142 return Ok(if in_is_signed {
2143 bx.sitofp(args[0].immediate(), llret_ty)
2145 bx.uitofp(args[0].immediate(), llret_ty)
2148 (Style::Float, Style::Int(out_is_signed)) => {
2149 return Ok(if out_is_signed {
2150 bx.fptosi(args[0].immediate(), llret_ty)
2152 bx.fptoui(args[0].immediate(), llret_ty)
2155 (Style::Float, Style::Float) => {
2156 return Ok(match in_width.cmp(&out_width) {
2157 Ordering::Greater => bx.fptrunc(args[0].immediate(), llret_ty),
2158 Ordering::Equal => args[0].immediate(),
2159 Ordering::Less => bx.fpext(args[0].immediate(), llret_ty),
2162 _ => { /* Unsupported. Fallthrough. */ }
2166 "unsupported cast from `{}` with element `{}` to `{}` with element `{}`",
2173 macro_rules! arith {
2174 ($($name: ident: $($($p: ident),* => $call: ident),*;)*) => {
2175 $(if name == sym::$name {
2176 match in_elem.kind {
2177 $($(ty::$p(_))|* => {
2178 return Ok(bx.$call(args[0].immediate(), args[1].immediate()))
2183 "unsupported operation on `{}` with element `{}`",
2190 simd_add: Uint, Int => add, Float => fadd;
2191 simd_sub: Uint, Int => sub, Float => fsub;
2192 simd_mul: Uint, Int => mul, Float => fmul;
2193 simd_div: Uint => udiv, Int => sdiv, Float => fdiv;
2194 simd_rem: Uint => urem, Int => srem, Float => frem;
2195 simd_shl: Uint, Int => shl;
2196 simd_shr: Uint => lshr, Int => ashr;
2197 simd_and: Uint, Int => and;
2198 simd_or: Uint, Int => or;
2199 simd_xor: Uint, Int => xor;
2200 simd_fmax: Float => maxnum;
2201 simd_fmin: Float => minnum;
2205 if name == sym::simd_saturating_add || name == sym::simd_saturating_sub {
2206 let lhs = args[0].immediate();
2207 let rhs = args[1].immediate();
2208 let is_add = name == sym::simd_saturating_add;
2209 let ptr_bits = bx.tcx().data_layout.pointer_size.bits() as _;
2210 let (signed, elem_width, elem_ty) = match in_elem.kind {
2211 ty::Int(i) => (true, i.bit_width().unwrap_or(ptr_bits), bx.cx.type_int_from_ty(i)),
2212 ty::Uint(i) => (false, i.bit_width().unwrap_or(ptr_bits), bx.cx.type_uint_from_ty(i)),
2215 "expected element type `{}` of vector type `{}` \
2216 to be a signed or unsigned integer type",
2217 arg_tys[0].simd_type(tcx),
2222 let llvm_intrinsic = &format!(
2223 "llvm.{}{}.sat.v{}i{}",
2224 if signed { 's' } else { 'u' },
2225 if is_add { "add" } else { "sub" },
2229 let vec_ty = bx.cx.type_vector(elem_ty, in_len as u64);
2231 let f = bx.declare_cfn(&llvm_intrinsic, bx.type_func(&[vec_ty, vec_ty], vec_ty));
2232 llvm::SetUnnamedAddress(f, llvm::UnnamedAddr::No);
2233 let v = bx.call(f, &[lhs, rhs], None);
2237 span_bug!(span, "unknown SIMD intrinsic");
2240 // Returns the width of an int Ty, and if it's signed or not
2241 // Returns None if the type is not an integer
2242 // FIXME: there’s multiple of this functions, investigate using some of the already existing
2244 fn int_type_width_signed(ty: Ty<'_>, cx: &CodegenCx<'_, '_>) -> Option<(u64, bool)> {
2246 ty::Int(t) => Some((
2248 ast::IntTy::Isize => u64::from(cx.tcx.sess.target.ptr_width),
2249 ast::IntTy::I8 => 8,
2250 ast::IntTy::I16 => 16,
2251 ast::IntTy::I32 => 32,
2252 ast::IntTy::I64 => 64,
2253 ast::IntTy::I128 => 128,
2257 ty::Uint(t) => Some((
2259 ast::UintTy::Usize => u64::from(cx.tcx.sess.target.ptr_width),
2260 ast::UintTy::U8 => 8,
2261 ast::UintTy::U16 => 16,
2262 ast::UintTy::U32 => 32,
2263 ast::UintTy::U64 => 64,
2264 ast::UintTy::U128 => 128,
2272 // Returns the width of a float Ty
2273 // Returns None if the type is not a float
2274 fn float_type_width(ty: Ty<'_>) -> Option<u64> {
2276 ty::Float(t) => Some(t.bit_width()),
2281 fn op_to_u32<'tcx>(op: &Operand<'tcx>) -> u32 {
2282 Operand::scalar_from_const(op).to_u32().expect("Scalar is u32")
2285 fn op_to_u64<'tcx>(op: &Operand<'tcx>) -> u64 {
2286 Operand::scalar_from_const(op).to_u64().expect("Scalar is u64")