1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(non_upper_case_globals)]
14 use intrinsics::{self, Intrinsic};
15 use llvm::{self, TypeKind};
17 use abi::{Abi, FnType, LlvmType, PassMode};
18 use mir::place::PlaceRef;
19 use mir::operand::{OperandRef, OperandValue};
22 use context::CodegenCx;
26 use type_of::LayoutLlvmExt;
27 use rustc::ty::{self, Ty};
28 use rustc::ty::layout::LayoutOf;
31 use syntax::symbol::Symbol;
35 use interfaces::{BuilderMethods, CommonMethods};
37 use rustc::session::Session;
40 use std::cmp::Ordering;
43 fn get_simple_intrinsic(cx: &CodegenCx<'ll, '_>, name: &str) -> Option<&'ll Value> {
44 let llvm_name = match name {
45 "sqrtf32" => "llvm.sqrt.f32",
46 "sqrtf64" => "llvm.sqrt.f64",
47 "powif32" => "llvm.powi.f32",
48 "powif64" => "llvm.powi.f64",
49 "sinf32" => "llvm.sin.f32",
50 "sinf64" => "llvm.sin.f64",
51 "cosf32" => "llvm.cos.f32",
52 "cosf64" => "llvm.cos.f64",
53 "powf32" => "llvm.pow.f32",
54 "powf64" => "llvm.pow.f64",
55 "expf32" => "llvm.exp.f32",
56 "expf64" => "llvm.exp.f64",
57 "exp2f32" => "llvm.exp2.f32",
58 "exp2f64" => "llvm.exp2.f64",
59 "logf32" => "llvm.log.f32",
60 "logf64" => "llvm.log.f64",
61 "log10f32" => "llvm.log10.f32",
62 "log10f64" => "llvm.log10.f64",
63 "log2f32" => "llvm.log2.f32",
64 "log2f64" => "llvm.log2.f64",
65 "fmaf32" => "llvm.fma.f32",
66 "fmaf64" => "llvm.fma.f64",
67 "fabsf32" => "llvm.fabs.f32",
68 "fabsf64" => "llvm.fabs.f64",
69 "copysignf32" => "llvm.copysign.f32",
70 "copysignf64" => "llvm.copysign.f64",
71 "floorf32" => "llvm.floor.f32",
72 "floorf64" => "llvm.floor.f64",
73 "ceilf32" => "llvm.ceil.f32",
74 "ceilf64" => "llvm.ceil.f64",
75 "truncf32" => "llvm.trunc.f32",
76 "truncf64" => "llvm.trunc.f64",
77 "rintf32" => "llvm.rint.f32",
78 "rintf64" => "llvm.rint.f64",
79 "nearbyintf32" => "llvm.nearbyint.f32",
80 "nearbyintf64" => "llvm.nearbyint.f64",
81 "roundf32" => "llvm.round.f32",
82 "roundf64" => "llvm.round.f64",
83 "assume" => "llvm.assume",
84 "abort" => "llvm.trap",
87 Some(cx.get_intrinsic(&llvm_name))
90 /// Remember to add all intrinsics here, in librustc_typeck/check/mod.rs,
91 /// and in libcore/intrinsics.rs; if you need access to any llvm intrinsics,
92 /// add them to librustc_codegen_llvm/context.rs
93 pub fn codegen_intrinsic_call(
94 bx: &Builder<'a, 'll, 'tcx>,
96 fn_ty: &FnType<'tcx, Ty<'tcx>>,
97 args: &[OperandRef<'tcx, &'ll Value>],
104 let (def_id, substs) = match callee_ty.sty {
105 ty::FnDef(def_id, substs) => (def_id, substs),
106 _ => bug!("expected fn item type, found {}", callee_ty)
109 let sig = callee_ty.fn_sig(tcx);
110 let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
111 let arg_tys = sig.inputs();
112 let ret_ty = sig.output();
113 let name = &*tcx.item_name(def_id).as_str();
115 let llret_ty = cx.layout_of(ret_ty).llvm_type(cx);
116 let result = PlaceRef::new_sized(llresult, fn_ty.ret.layout, fn_ty.ret.layout.align);
118 let simple = get_simple_intrinsic(cx, name);
119 let llval = match name {
120 _ if simple.is_some() => {
121 bx.call(simple.unwrap(),
122 &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
129 let expect = cx.get_intrinsic(&("llvm.expect.i1"));
130 bx.call(expect, &[args[0].immediate(), CodegenCx::c_bool(cx, true)], None)
133 let expect = cx.get_intrinsic(&("llvm.expect.i1"));
134 bx.call(expect, &[args[0].immediate(), CodegenCx::c_bool(cx, false)], None)
137 try_intrinsic(bx, cx,
145 let llfn = cx.get_intrinsic(&("llvm.debugtrap"));
146 bx.call(llfn, &[], None)
149 let tp_ty = substs.type_at(0);
150 CodegenCx::c_usize(cx, cx.size_of(tp_ty).bytes())
153 let tp_ty = substs.type_at(0);
154 if let OperandValue::Pair(_, meta) = args[0].val {
156 glue::size_and_align_of_dst(bx, tp_ty, Some(meta));
159 CodegenCx::c_usize(cx, cx.size_of(tp_ty).bytes())
163 let tp_ty = substs.type_at(0);
164 CodegenCx::c_usize(cx, cx.align_of(tp_ty).abi())
166 "min_align_of_val" => {
167 let tp_ty = substs.type_at(0);
168 if let OperandValue::Pair(_, meta) = args[0].val {
170 glue::size_and_align_of_dst(bx, tp_ty, Some(meta));
173 CodegenCx::c_usize(cx, cx.align_of(tp_ty).abi())
177 let tp_ty = substs.type_at(0);
178 CodegenCx::c_usize(cx, cx.align_of(tp_ty).pref())
181 let tp_ty = substs.type_at(0);
182 let ty_name = Symbol::intern(&tp_ty.to_string()).as_str();
183 CodegenCx::c_str_slice(cx, ty_name)
186 CodegenCx::c_u64(cx, cx.tcx.type_id_hash(substs.type_at(0)))
189 let ty = substs.type_at(0);
190 if !cx.layout_of(ty).is_zst() {
191 // Just zero out the stack slot.
192 // If we store a zero constant, LLVM will drown in vreg allocation for large data
193 // structures, and the generated code will be awful. (A telltale sign of this is
194 // large quantities of `mov [byte ptr foo],0` in the generated code.)
200 CodegenCx::c_u8(cx, 0),
201 CodegenCx::c_usize(cx, 1)
206 // Effectively no-ops
207 "uninit" | "forget" => {
211 let tp_ty = substs.type_at(0);
213 CodegenCx::c_bool(cx, bx.cx.type_needs_drop(tp_ty))
216 let ptr = args[0].immediate();
217 let offset = args[1].immediate();
218 bx.inbounds_gep(ptr, &[offset])
221 let ptr = args[0].immediate();
222 let offset = args[1].immediate();
223 bx.gep(ptr, &[offset])
226 "copy_nonoverlapping" => {
227 copy_intrinsic(bx, false, false, substs.type_at(0),
228 args[1].immediate(), args[0].immediate(), args[2].immediate())
231 copy_intrinsic(bx, true, false, substs.type_at(0),
232 args[1].immediate(), args[0].immediate(), args[2].immediate())
235 memset_intrinsic(bx, false, substs.type_at(0),
236 args[0].immediate(), args[1].immediate(), args[2].immediate())
239 "volatile_copy_nonoverlapping_memory" => {
240 copy_intrinsic(bx, false, true, substs.type_at(0),
241 args[0].immediate(), args[1].immediate(), args[2].immediate())
243 "volatile_copy_memory" => {
244 copy_intrinsic(bx, true, true, substs.type_at(0),
245 args[0].immediate(), args[1].immediate(), args[2].immediate())
247 "volatile_set_memory" => {
248 memset_intrinsic(bx, true, substs.type_at(0),
249 args[0].immediate(), args[1].immediate(), args[2].immediate())
251 "volatile_load" | "unaligned_volatile_load" => {
252 let tp_ty = substs.type_at(0);
253 let mut ptr = args[0].immediate();
254 if let PassMode::Cast(ty) = fn_ty.ret.mode {
255 ptr = bx.pointercast(ptr, ty.llvm_type(cx).ptr_to());
257 let load = bx.volatile_load(ptr);
258 let align = if name == "unaligned_volatile_load" {
261 cx.align_of(tp_ty).abi() as u32
264 llvm::LLVMSetAlignment(load, align);
266 to_immediate(bx, load, cx.layout_of(tp_ty))
268 "volatile_store" => {
269 let dst = args[0].deref(bx.cx);
270 args[1].val.volatile_store(bx, dst);
273 "unaligned_volatile_store" => {
274 let dst = args[0].deref(bx.cx);
275 args[1].val.unaligned_volatile_store(bx, dst);
278 "prefetch_read_data" | "prefetch_write_data" |
279 "prefetch_read_instruction" | "prefetch_write_instruction" => {
280 let expect = cx.get_intrinsic(&("llvm.prefetch"));
281 let (rw, cache_type) = match name {
282 "prefetch_read_data" => (0, 1),
283 "prefetch_write_data" => (1, 1),
284 "prefetch_read_instruction" => (0, 0),
285 "prefetch_write_instruction" => (1, 0),
290 CodegenCx::c_i32(cx, rw),
292 CodegenCx::c_i32(cx, cache_type)
295 "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" | "ctpop" | "bswap" |
296 "bitreverse" | "add_with_overflow" | "sub_with_overflow" |
297 "mul_with_overflow" | "overflowing_add" | "overflowing_sub" | "overflowing_mul" |
298 "unchecked_div" | "unchecked_rem" | "unchecked_shl" | "unchecked_shr" | "exact_div" |
299 "rotate_left" | "rotate_right" => {
301 match int_type_width_signed(ty, cx) {
302 Some((width, signed)) =>
305 let y = CodegenCx::c_bool(bx.cx, false);
306 let llfn = cx.get_intrinsic(&format!("llvm.{}.i{}", name, width));
307 bx.call(llfn, &[args[0].immediate(), y], None)
309 "ctlz_nonzero" | "cttz_nonzero" => {
310 let y = CodegenCx::c_bool(bx.cx, true);
311 let llvm_name = &format!("llvm.{}.i{}", &name[..4], width);
312 let llfn = cx.get_intrinsic(llvm_name);
313 bx.call(llfn, &[args[0].immediate(), y], None)
315 "ctpop" => bx.call(cx.get_intrinsic(&format!("llvm.ctpop.i{}", width)),
316 &[args[0].immediate()], None),
319 args[0].immediate() // byte swap a u8/i8 is just a no-op
321 bx.call(cx.get_intrinsic(&format!("llvm.bswap.i{}", width)),
322 &[args[0].immediate()], None)
326 bx.call(cx.get_intrinsic(&format!("llvm.bitreverse.i{}", width)),
327 &[args[0].immediate()], None)
329 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" => {
330 let intrinsic = format!("llvm.{}{}.with.overflow.i{}",
331 if signed { 's' } else { 'u' },
333 let llfn = bx.cx.get_intrinsic(&intrinsic);
335 // Convert `i1` to a `bool`, and write it to the out parameter
336 let pair = bx.call(llfn, &[
340 let val = bx.extract_value(pair, 0);
341 let overflow = bx.zext(bx.extract_value(pair, 1), Type::bool(cx));
343 let dest = result.project_field(bx, 0);
344 bx.store(val, dest.llval, dest.align);
345 let dest = result.project_field(bx, 1);
346 bx.store(overflow, dest.llval, dest.align);
350 "overflowing_add" => bx.add(args[0].immediate(), args[1].immediate()),
351 "overflowing_sub" => bx.sub(args[0].immediate(), args[1].immediate()),
352 "overflowing_mul" => bx.mul(args[0].immediate(), args[1].immediate()),
355 bx.exactsdiv(args[0].immediate(), args[1].immediate())
357 bx.exactudiv(args[0].immediate(), args[1].immediate())
361 bx.sdiv(args[0].immediate(), args[1].immediate())
363 bx.udiv(args[0].immediate(), args[1].immediate())
367 bx.srem(args[0].immediate(), args[1].immediate())
369 bx.urem(args[0].immediate(), args[1].immediate())
371 "unchecked_shl" => bx.shl(args[0].immediate(), args[1].immediate()),
374 bx.ashr(args[0].immediate(), args[1].immediate())
376 bx.lshr(args[0].immediate(), args[1].immediate())
378 "rotate_left" | "rotate_right" => {
379 let is_left = name == "rotate_left";
380 let val = args[0].immediate();
381 let raw_shift = args[1].immediate();
382 if llvm_util::get_major_version() >= 7 {
383 // rotate = funnel shift with first two args the same
384 let llvm_name = &format!("llvm.fsh{}.i{}",
385 if is_left { 'l' } else { 'r' }, width);
386 let llfn = cx.get_intrinsic(llvm_name);
387 bx.call(llfn, &[val, val, raw_shift], None)
389 // rotate_left: (X << (S % BW)) | (X >> ((BW - S) % BW))
390 // rotate_right: (X << ((BW - S) % BW)) | (X >> (S % BW))
391 let width = CodegenCx::c_uint(Type::ix(cx, width), width);
392 let shift = bx.urem(raw_shift, width);
393 let inv_shift = bx.urem(bx.sub(width, raw_shift), width);
394 let shift1 = bx.shl(val, if is_left { shift } else { inv_shift });
395 let shift2 = bx.lshr(val, if !is_left { shift } else { inv_shift });
396 bx.or(shift1, shift2)
402 span_invalid_monomorphization_error(
404 &format!("invalid monomorphization of `{}` intrinsic: \
405 expected basic integer type, found `{}`", name, ty));
410 "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" => {
411 let sty = &arg_tys[0].sty;
412 match float_type_width(sty) {
415 "fadd_fast" => bx.fadd_fast(args[0].immediate(), args[1].immediate()),
416 "fsub_fast" => bx.fsub_fast(args[0].immediate(), args[1].immediate()),
417 "fmul_fast" => bx.fmul_fast(args[0].immediate(), args[1].immediate()),
418 "fdiv_fast" => bx.fdiv_fast(args[0].immediate(), args[1].immediate()),
419 "frem_fast" => bx.frem_fast(args[0].immediate(), args[1].immediate()),
423 span_invalid_monomorphization_error(
425 &format!("invalid monomorphization of `{}` intrinsic: \
426 expected basic float type, found `{}`", name, sty));
433 "discriminant_value" => {
434 args[0].deref(bx.cx).codegen_get_discr(bx, ret_ty)
437 name if name.starts_with("simd_") => {
438 match generic_simd_intrinsic(bx, name,
447 // This requires that atomic intrinsics follow a specific naming pattern:
448 // "atomic_<operation>[_<ordering>]", and no ordering means SeqCst
449 name if name.starts_with("atomic_") => {
450 use self::AtomicOrdering::*;
452 let split: Vec<&str> = name.split('_').collect();
454 let is_cxchg = split[1] == "cxchg" || split[1] == "cxchgweak";
455 let (order, failorder) = match split.len() {
456 2 => (SequentiallyConsistent, SequentiallyConsistent),
457 3 => match split[2] {
458 "unordered" => (Unordered, Unordered),
459 "relaxed" => (Monotonic, Monotonic),
460 "acq" => (Acquire, Acquire),
461 "rel" => (Release, Monotonic),
462 "acqrel" => (AcquireRelease, Acquire),
463 "failrelaxed" if is_cxchg =>
464 (SequentiallyConsistent, Monotonic),
465 "failacq" if is_cxchg =>
466 (SequentiallyConsistent, Acquire),
467 _ => cx.sess().fatal("unknown ordering in atomic intrinsic")
469 4 => match (split[2], split[3]) {
470 ("acq", "failrelaxed") if is_cxchg =>
471 (Acquire, Monotonic),
472 ("acqrel", "failrelaxed") if is_cxchg =>
473 (AcquireRelease, Monotonic),
474 _ => cx.sess().fatal("unknown ordering in atomic intrinsic")
476 _ => cx.sess().fatal("Atomic intrinsic not in correct format"),
479 let invalid_monomorphization = |ty| {
480 span_invalid_monomorphization_error(tcx.sess, span,
481 &format!("invalid monomorphization of `{}` intrinsic: \
482 expected basic integer type, found `{}`", name, ty));
486 "cxchg" | "cxchgweak" => {
487 let ty = substs.type_at(0);
488 if int_type_width_signed(ty, cx).is_some() {
489 let weak = split[1] == "cxchgweak";
490 let pair = bx.atomic_cmpxchg(
497 let val = bx.extract_value(pair, 0);
498 let success = bx.zext(bx.extract_value(pair, 1), Type::bool(bx.cx));
500 let dest = result.project_field(bx, 0);
501 bx.store(val, dest.llval, dest.align);
502 let dest = result.project_field(bx, 1);
503 bx.store(success, dest.llval, dest.align);
506 return invalid_monomorphization(ty);
511 let ty = substs.type_at(0);
512 if int_type_width_signed(ty, cx).is_some() {
513 let size = cx.size_of(ty);
514 bx.atomic_load(args[0].immediate(), order, size)
516 return invalid_monomorphization(ty);
521 let ty = substs.type_at(0);
522 if int_type_width_signed(ty, cx).is_some() {
523 let size = cx.size_of(ty);
524 bx.atomic_store(args[1].immediate(), args[0].immediate(), order, size);
527 return invalid_monomorphization(ty);
532 bx.atomic_fence(order, SynchronizationScope::CrossThread);
536 "singlethreadfence" => {
537 bx.atomic_fence(order, SynchronizationScope::SingleThread);
541 // These are all AtomicRMW ops
543 let atom_op = match op {
544 "xchg" => AtomicRmwBinOp::AtomicXchg,
545 "xadd" => AtomicRmwBinOp::AtomicAdd,
546 "xsub" => AtomicRmwBinOp::AtomicSub,
547 "and" => AtomicRmwBinOp::AtomicAnd,
548 "nand" => AtomicRmwBinOp::AtomicNand,
549 "or" => AtomicRmwBinOp::AtomicOr,
550 "xor" => AtomicRmwBinOp::AtomicXor,
551 "max" => AtomicRmwBinOp::AtomicMax,
552 "min" => AtomicRmwBinOp::AtomicMin,
553 "umax" => AtomicRmwBinOp::AtomicUMax,
554 "umin" => AtomicRmwBinOp::AtomicUMin,
555 _ => cx.sess().fatal("unknown atomic operation")
558 let ty = substs.type_at(0);
559 if int_type_width_signed(ty, cx).is_some() {
560 bx.atomic_rmw(atom_op, args[0].immediate(), args[1].immediate(), order)
562 return invalid_monomorphization(ty);
568 "nontemporal_store" => {
569 let dst = args[0].deref(bx.cx);
570 args[1].val.nontemporal_store(bx, dst);
575 let intr = Intrinsic::find(&name).unwrap_or_else(||
576 bug!("unknown intrinsic '{}'", name));
578 fn one<T>(x: Vec<T>) -> T {
579 assert_eq!(x.len(), 1);
580 x.into_iter().next().unwrap()
582 fn ty_to_type(cx: &CodegenCx<'ll, '_>, t: &intrinsics::Type) -> Vec<&'ll Type> {
583 use intrinsics::Type::*;
585 Void => vec![Type::void(cx)],
586 Integer(_signed, _width, llvm_width) => {
587 vec![Type::ix(cx, llvm_width as u64)]
591 32 => vec![Type::f32(cx)],
592 64 => vec![Type::f64(cx)],
596 Pointer(ref t, ref llvm_elem, _const) => {
597 let t = llvm_elem.as_ref().unwrap_or(t);
598 let elem = one(ty_to_type(cx, t));
601 Vector(ref t, ref llvm_elem, length) => {
602 let t = llvm_elem.as_ref().unwrap_or(t);
603 let elem = one(ty_to_type(cx, t));
604 vec![Type::vector(elem, length as u64)]
606 Aggregate(false, ref contents) => {
607 let elems = contents.iter()
608 .map(|t| one(ty_to_type(cx, t)))
609 .collect::<Vec<_>>();
610 vec![Type::struct_(cx, &elems, false)]
612 Aggregate(true, ref contents) => {
614 .flat_map(|t| ty_to_type(cx, t))
620 // This allows an argument list like `foo, (bar, baz),
621 // qux` to be converted into `foo, bar, baz, qux`, integer
622 // arguments to be truncated as needed and pointers to be
625 bx: &Builder<'a, 'll, 'tcx>,
626 t: &intrinsics::Type,
627 arg: &OperandRef<'tcx, &'ll Value>,
628 ) -> Vec<&'ll Value> {
630 intrinsics::Type::Aggregate(true, ref contents) => {
631 // We found a tuple that needs squishing! So
632 // run over the tuple and load each field.
634 // This assumes the type is "simple", i.e. no
635 // destructors, and the contents are SIMD
637 assert!(!bx.cx.type_needs_drop(arg.layout.ty));
638 let (ptr, align) = match arg.val {
639 OperandValue::Ref(ptr, None, align) => (ptr, align),
642 let arg = PlaceRef::new_sized(ptr, arg.layout, align);
643 (0..contents.len()).map(|i| {
644 arg.project_field(bx, i).load(bx).immediate()
647 intrinsics::Type::Pointer(_, Some(ref llvm_elem), _) => {
648 let llvm_elem = one(ty_to_type(bx.cx, llvm_elem));
649 vec![bx.pointercast(arg.immediate(), llvm_elem.ptr_to())]
651 intrinsics::Type::Vector(_, Some(ref llvm_elem), length) => {
652 let llvm_elem = one(ty_to_type(bx.cx, llvm_elem));
654 bx.bitcast(arg.immediate(),
655 Type::vector(llvm_elem, length as u64))
658 intrinsics::Type::Integer(_, width, llvm_width) if width != llvm_width => {
659 // the LLVM intrinsic uses a smaller integer
660 // size than the C intrinsic's signature, so
661 // we have to trim it down here.
662 vec![bx.trunc(arg.immediate(), Type::ix(bx.cx, llvm_width as u64))]
664 _ => vec![arg.immediate()],
669 let inputs = intr.inputs.iter()
670 .flat_map(|t| ty_to_type(cx, t))
671 .collect::<Vec<_>>();
673 let outputs = one(ty_to_type(cx, &intr.output));
675 let llargs: Vec<_> = intr.inputs.iter().zip(args).flat_map(|(t, arg)| {
676 modify_as_needed(bx, t, arg)
678 assert_eq!(inputs.len(), llargs.len());
680 let val = match intr.definition {
681 intrinsics::IntrinsicDef::Named(name) => {
682 let f = declare::declare_cfn(cx,
684 Type::func(&inputs, outputs));
685 bx.call(f, &llargs, None)
690 intrinsics::Type::Aggregate(flatten, ref elems) => {
691 // the output is a tuple so we need to munge it properly
694 for i in 0..elems.len() {
695 let dest = result.project_field(bx, i);
696 let val = bx.extract_value(val, i as u64);
697 bx.store(val, dest.llval, dest.align);
706 if !fn_ty.ret.is_ignore() {
707 if let PassMode::Cast(ty) = fn_ty.ret.mode {
708 let ptr = bx.pointercast(result.llval, ty.llvm_type(cx).ptr_to());
709 bx.store(llval, ptr, result.align);
711 OperandRef::from_immediate_or_packed_pair(bx, llval, result.layout)
712 .val.store(bx, result);
718 bx: &Builder<'a, 'll, 'tcx>,
727 let (size, align) = cx.size_and_align_of(ty);
728 let size = CodegenCx::c_usize(cx, size.bytes());
729 let align = align.abi();
730 let dst_ptr = bx.pointercast(dst, Type::i8p(cx));
731 let src_ptr = bx.pointercast(src, Type::i8p(cx));
733 bx.memmove(dst_ptr, align, src_ptr, align, bx.mul(size, count), volatile)
735 bx.memcpy(dst_ptr, align, src_ptr, align, bx.mul(size, count), volatile)
740 bx: &Builder<'a, 'll, 'tcx>,
748 let (size, align) = cx.size_and_align_of(ty);
749 let size = CodegenCx::c_usize(cx, size.bytes());
750 let align = CodegenCx::c_i32(cx, align.abi() as i32);
751 let dst = bx.pointercast(dst, Type::i8p(cx));
752 call_memset(bx, dst, val, bx.mul(size, count), align, volatile)
756 bx: &Builder<'a, 'll, 'tcx>,
757 cx: &CodegenCx<'ll, 'tcx>,
760 local_ptr: &'ll Value,
763 if bx.sess().no_landing_pads() {
764 bx.call(func, &[data], None);
765 let ptr_align = bx.tcx().data_layout.pointer_align;
766 bx.store(CodegenCx::c_null(Type::i8p(&bx.cx)), dest, ptr_align);
767 } else if wants_msvc_seh(bx.sess()) {
768 codegen_msvc_try(bx, cx, func, data, local_ptr, dest);
770 codegen_gnu_try(bx, cx, func, data, local_ptr, dest);
774 // MSVC's definition of the `rust_try` function.
776 // This implementation uses the new exception handling instructions in LLVM
777 // which have support in LLVM for SEH on MSVC targets. Although these
778 // instructions are meant to work for all targets, as of the time of this
779 // writing, however, LLVM does not recommend the usage of these new instructions
780 // as the old ones are still more optimized.
782 bx: &Builder<'a, 'll, 'tcx>,
783 cx: &CodegenCx<'ll, 'tcx>,
786 local_ptr: &'ll Value,
789 let llfn = get_rust_try_fn(cx, &mut |bx| {
792 bx.set_personality_fn(bx.cx.eh_personality());
794 let normal = bx.build_sibling_block("normal");
795 let catchswitch = bx.build_sibling_block("catchswitch");
796 let catchpad = bx.build_sibling_block("catchpad");
797 let caught = bx.build_sibling_block("caught");
799 let func = llvm::get_param(bx.llfn(), 0);
800 let data = llvm::get_param(bx.llfn(), 1);
801 let local_ptr = llvm::get_param(bx.llfn(), 2);
803 // We're generating an IR snippet that looks like:
805 // declare i32 @rust_try(%func, %data, %ptr) {
806 // %slot = alloca i64*
807 // invoke %func(%data) to label %normal unwind label %catchswitch
813 // %cs = catchswitch within none [%catchpad] unwind to caller
816 // %tok = catchpad within %cs [%type_descriptor, 0, %slot]
817 // %ptr[0] = %slot[0]
818 // %ptr[1] = %slot[1]
819 // catchret from %tok to label %caught
825 // This structure follows the basic usage of throw/try/catch in LLVM.
826 // For example, compile this C++ snippet to see what LLVM generates:
828 // #include <stdint.h>
830 // int bar(void (*foo)(void), uint64_t *ret) {
834 // } catch(uint64_t a[2]) {
841 // More information can be found in libstd's seh.rs implementation.
842 let i64p = Type::i64(cx).ptr_to();
843 let ptr_align = bx.tcx().data_layout.pointer_align;
844 let slot = bx.alloca(i64p, "slot", ptr_align);
845 bx.invoke(func, &[data], normal.llbb(), catchswitch.llbb(), None);
847 normal.ret(CodegenCx::c_i32(cx, 0));
849 let cs = catchswitch.catch_switch(None, None, 1);
850 catchswitch.add_handler(cs, catchpad.llbb());
853 let tydesc = match tcx.lang_items().msvc_try_filter() {
854 Some(did) => ::consts::get_static(cx, did),
855 None => bug!("msvc_try_filter not defined"),
857 let tok = catchpad.catch_pad(cs, &[tydesc, CodegenCx::c_i32(cx, 0), slot]);
858 let addr = catchpad.load(slot, ptr_align);
860 let i64_align = bx.tcx().data_layout.i64_align;
861 let arg1 = catchpad.load(addr, i64_align);
862 let val1 = CodegenCx::c_i32(cx, 1);
863 let arg2 = catchpad.load(catchpad.inbounds_gep(addr, &[val1]), i64_align);
864 let local_ptr = catchpad.bitcast(local_ptr, i64p);
865 catchpad.store(arg1, local_ptr, i64_align);
866 catchpad.store(arg2, catchpad.inbounds_gep(local_ptr, &[val1]), i64_align);
867 catchpad.catch_ret(tok, caught.llbb());
869 caught.ret(CodegenCx::c_i32(cx, 1));
872 // Note that no invoke is used here because by definition this function
873 // can't panic (that's what it's catching).
874 let ret = bx.call(llfn, &[func, data, local_ptr], None);
875 let i32_align = bx.tcx().data_layout.i32_align;
876 bx.store(ret, dest, i32_align);
879 // Definition of the standard "try" function for Rust using the GNU-like model
880 // of exceptions (e.g. the normal semantics of LLVM's landingpad and invoke
883 // This codegen is a little surprising because we always call a shim
884 // function instead of inlining the call to `invoke` manually here. This is done
885 // because in LLVM we're only allowed to have one personality per function
886 // definition. The call to the `try` intrinsic is being inlined into the
887 // function calling it, and that function may already have other personality
888 // functions in play. By calling a shim we're guaranteed that our shim will have
889 // the right personality function.
891 bx: &Builder<'a, 'll, 'tcx>,
892 cx: &CodegenCx<'ll, 'tcx>,
895 local_ptr: &'ll Value,
898 let llfn = get_rust_try_fn(cx, &mut |bx| {
901 // Codegens the shims described above:
904 // invoke %func(%args...) normal %normal unwind %catch
910 // (ptr, _) = landingpad
911 // store ptr, %local_ptr
914 // Note that the `local_ptr` data passed into the `try` intrinsic is
915 // expected to be `*mut *mut u8` for this to actually work, but that's
916 // managed by the standard library.
918 let then = bx.build_sibling_block("then");
919 let catch = bx.build_sibling_block("catch");
921 let func = llvm::get_param(bx.llfn(), 0);
922 let data = llvm::get_param(bx.llfn(), 1);
923 let local_ptr = llvm::get_param(bx.llfn(), 2);
924 bx.invoke(func, &[data], then.llbb(), catch.llbb(), None);
925 then.ret(CodegenCx::c_i32(cx, 0));
927 // Type indicator for the exception being thrown.
929 // The first value in this tuple is a pointer to the exception object
930 // being thrown. The second value is a "selector" indicating which of
931 // the landing pad clauses the exception's type had been matched to.
932 // rust_try ignores the selector.
933 let lpad_ty = Type::struct_(cx, &[Type::i8p(cx), Type::i32(cx)], false);
934 let vals = catch.landing_pad(lpad_ty, bx.cx.eh_personality(), 1);
935 catch.add_clause(vals, CodegenCx::c_null(Type::i8p(cx)));
936 let ptr = catch.extract_value(vals, 0);
937 let ptr_align = bx.tcx().data_layout.pointer_align;
938 catch.store(ptr, catch.bitcast(local_ptr, Type::i8p(cx).ptr_to()), ptr_align);
939 catch.ret(CodegenCx::c_i32(cx, 1));
942 // Note that no invoke is used here because by definition this function
943 // can't panic (that's what it's catching).
944 let ret = bx.call(llfn, &[func, data, local_ptr], None);
945 let i32_align = bx.tcx().data_layout.i32_align;
946 bx.store(ret, dest, i32_align);
949 // Helper function to give a Block to a closure to codegen a shim function.
950 // This is currently primarily used for the `try` intrinsic functions above.
951 fn gen_fn<'ll, 'tcx>(
952 cx: &CodegenCx<'ll, 'tcx>,
954 inputs: Vec<Ty<'tcx>>,
956 codegen: &mut dyn FnMut(Builder<'_, 'll, 'tcx>),
958 let rust_fn_sig = ty::Binder::bind(cx.tcx.mk_fn_sig(
962 hir::Unsafety::Unsafe,
965 let llfn = declare::define_internal_fn(cx, name, rust_fn_sig);
966 attributes::from_fn_attrs(cx, llfn, None);
967 let bx = Builder::new_block(cx, llfn, "entry-block");
972 // Helper function used to get a handle to the `__rust_try` function used to
975 // This function is only generated once and is then cached.
976 fn get_rust_try_fn<'ll, 'tcx>(
977 cx: &CodegenCx<'ll, 'tcx>,
978 codegen: &mut dyn FnMut(Builder<'_, 'll, 'tcx>),
980 if let Some(llfn) = cx.rust_try_fn.get() {
984 // Define the type up front for the signature of the rust_try function.
986 let i8p = tcx.mk_mut_ptr(tcx.types.i8);
987 let fn_ty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
991 hir::Unsafety::Unsafe,
994 let output = tcx.types.i32;
995 let rust_try = gen_fn(cx, "__rust_try", vec![fn_ty, i8p, i8p], output, codegen);
996 cx.rust_try_fn.set(Some(rust_try));
1000 fn span_invalid_monomorphization_error(a: &Session, b: Span, c: &str) {
1001 span_err!(a, b, E0511, "{}", c);
1004 fn generic_simd_intrinsic(
1005 bx: &Builder<'a, 'll, 'tcx>,
1007 callee_ty: Ty<'tcx>,
1008 args: &[OperandRef<'tcx, &'ll Value>],
1010 llret_ty: &'ll Type,
1012 ) -> Result<&'ll Value, ()> {
1013 // macros for error handling:
1014 macro_rules! emit_error {
1018 ($msg: tt, $($fmt: tt)*) => {
1019 span_invalid_monomorphization_error(
1021 &format!(concat!("invalid monomorphization of `{}` intrinsic: ", $msg),
1026 macro_rules! return_error {
1029 emit_error!($($fmt)*);
1035 macro_rules! require {
1036 ($cond: expr, $($fmt: tt)*) => {
1038 return_error!($($fmt)*);
1043 macro_rules! require_simd {
1044 ($ty: expr, $position: expr) => {
1045 require!($ty.is_simd(), "expected SIMD {} type, found non-SIMD `{}`", $position, $ty)
1050 let sig = tcx.normalize_erasing_late_bound_regions(
1051 ty::ParamEnv::reveal_all(),
1052 &callee_ty.fn_sig(tcx),
1054 let arg_tys = sig.inputs();
1056 // every intrinsic takes a SIMD vector as its first argument
1057 require_simd!(arg_tys[0], "input");
1058 let in_ty = arg_tys[0];
1059 let in_elem = arg_tys[0].simd_type(tcx);
1060 let in_len = arg_tys[0].simd_size(tcx);
1062 let comparison = match name {
1063 "simd_eq" => Some(hir::BinOpKind::Eq),
1064 "simd_ne" => Some(hir::BinOpKind::Ne),
1065 "simd_lt" => Some(hir::BinOpKind::Lt),
1066 "simd_le" => Some(hir::BinOpKind::Le),
1067 "simd_gt" => Some(hir::BinOpKind::Gt),
1068 "simd_ge" => Some(hir::BinOpKind::Ge),
1072 if let Some(cmp_op) = comparison {
1073 require_simd!(ret_ty, "return");
1075 let out_len = ret_ty.simd_size(tcx);
1076 require!(in_len == out_len,
1077 "expected return type with length {} (same as input type `{}`), \
1078 found `{}` with length {}",
1081 require!(llret_ty.element_type().kind() == TypeKind::Integer,
1082 "expected return type with integer elements, found `{}` with non-integer `{}`",
1084 ret_ty.simd_type(tcx));
1086 return Ok(compare_simd_types(bx,
1087 args[0].immediate(),
1088 args[1].immediate(),
1094 if name.starts_with("simd_shuffle") {
1095 let n: usize = name["simd_shuffle".len()..].parse().unwrap_or_else(|_|
1096 span_bug!(span, "bad `simd_shuffle` instruction only caught in codegen?"));
1098 require_simd!(ret_ty, "return");
1100 let out_len = ret_ty.simd_size(tcx);
1101 require!(out_len == n,
1102 "expected return type of length {}, found `{}` with length {}",
1103 n, ret_ty, out_len);
1104 require!(in_elem == ret_ty.simd_type(tcx),
1105 "expected return element type `{}` (element of input `{}`), \
1106 found `{}` with element type `{}`",
1108 ret_ty, ret_ty.simd_type(tcx));
1110 let total_len = in_len as u128 * 2;
1112 let vector = args[2].immediate();
1114 let indices: Option<Vec<_>> = (0..n)
1117 let val = CodegenCx::const_get_elt(vector, i as u64);
1118 match CodegenCx::const_to_opt_u128(val, true) {
1120 emit_error!("shuffle index #{} is not a constant", arg_idx);
1123 Some(idx) if idx >= total_len => {
1124 emit_error!("shuffle index #{} is out of bounds (limit {})",
1125 arg_idx, total_len);
1128 Some(idx) => Some(CodegenCx::c_i32(bx.cx, idx as i32)),
1132 let indices = match indices {
1134 None => return Ok(CodegenCx::c_null(llret_ty))
1137 return Ok(bx.shuffle_vector(args[0].immediate(),
1138 args[1].immediate(),
1139 CodegenCx::c_vector(&indices)))
1142 if name == "simd_insert" {
1143 require!(in_elem == arg_tys[2],
1144 "expected inserted type `{}` (element of input `{}`), found `{}`",
1145 in_elem, in_ty, arg_tys[2]);
1146 return Ok(bx.insert_element(args[0].immediate(),
1147 args[2].immediate(),
1148 args[1].immediate()))
1150 if name == "simd_extract" {
1151 require!(ret_ty == in_elem,
1152 "expected return type `{}` (element of input `{}`), found `{}`",
1153 in_elem, in_ty, ret_ty);
1154 return Ok(bx.extract_element(args[0].immediate(), args[1].immediate()))
1157 if name == "simd_select" {
1158 let m_elem_ty = in_elem;
1160 let v_len = arg_tys[1].simd_size(tcx);
1161 require!(m_len == v_len,
1162 "mismatched lengths: mask length `{}` != other vector length `{}`",
1165 match m_elem_ty.sty {
1167 _ => return_error!("mask element type is `{}`, expected `i_`", m_elem_ty)
1169 // truncate the mask to a vector of i1s
1170 let i1 = Type::i1(bx.cx);
1171 let i1xn = Type::vector(i1, m_len as u64);
1172 let m_i1s = bx.trunc(args[0].immediate(), i1xn);
1173 return Ok(bx.select(m_i1s, args[1].immediate(), args[2].immediate()));
1176 fn simd_simple_float_intrinsic(
1178 in_elem: &::rustc::ty::TyS,
1179 in_ty: &::rustc::ty::TyS,
1181 bx: &Builder<'a, 'll, 'tcx>,
1183 args: &[OperandRef<'tcx, &'ll Value>],
1184 ) -> Result<&'ll Value, ()> {
1185 macro_rules! emit_error {
1189 ($msg: tt, $($fmt: tt)*) => {
1190 span_invalid_monomorphization_error(
1192 &format!(concat!("invalid monomorphization of `{}` intrinsic: ", $msg),
1196 macro_rules! return_error {
1199 emit_error!($($fmt)*);
1204 let ety = match in_elem.sty {
1205 ty::Float(f) if f.bit_width() == 32 => {
1206 if in_len < 2 || in_len > 16 {
1208 "unsupported floating-point vector `{}` with length `{}` \
1209 out-of-range [2, 16]",
1214 ty::Float(f) if f.bit_width() == 64 => {
1215 if in_len < 2 || in_len > 8 {
1216 return_error!("unsupported floating-point vector `{}` with length `{}` \
1217 out-of-range [2, 8]",
1223 return_error!("unsupported element type `{}` of floating-point vector `{}`",
1227 return_error!("`{}` is not a floating-point type", in_ty);
1231 let llvm_name = &format!("llvm.{0}.v{1}{2}", name, in_len, ety);
1232 let intrinsic = bx.cx.get_intrinsic(&llvm_name);
1233 let c = bx.call(intrinsic,
1234 &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
1236 unsafe { llvm::LLVMRustSetHasUnsafeAlgebra(c) };
1242 return simd_simple_float_intrinsic("sqrt", in_elem, in_ty, in_len, bx, span, args);
1245 return simd_simple_float_intrinsic("sin", in_elem, in_ty, in_len, bx, span, args);
1248 return simd_simple_float_intrinsic("cos", in_elem, in_ty, in_len, bx, span, args);
1251 return simd_simple_float_intrinsic("fabs", in_elem, in_ty, in_len, bx, span, args);
1254 return simd_simple_float_intrinsic("floor", in_elem, in_ty, in_len, bx, span, args);
1257 return simd_simple_float_intrinsic("ceil", in_elem, in_ty, in_len, bx, span, args);
1260 return simd_simple_float_intrinsic("exp", in_elem, in_ty, in_len, bx, span, args);
1263 return simd_simple_float_intrinsic("exp2", in_elem, in_ty, in_len, bx, span, args);
1266 return simd_simple_float_intrinsic("log10", in_elem, in_ty, in_len, bx, span, args);
1269 return simd_simple_float_intrinsic("log2", in_elem, in_ty, in_len, bx, span, args);
1272 return simd_simple_float_intrinsic("log", in_elem, in_ty, in_len, bx, span, args);
1275 return simd_simple_float_intrinsic("powi", in_elem, in_ty, in_len, bx, span, args);
1278 return simd_simple_float_intrinsic("pow", in_elem, in_ty, in_len, bx, span, args);
1281 return simd_simple_float_intrinsic("fma", in_elem, in_ty, in_len, bx, span, args);
1283 _ => { /* fallthrough */ }
1287 // https://github.com/llvm-mirror/llvm/blob/master/include/llvm/IR/Function.h#L182
1288 // https://github.com/llvm-mirror/llvm/blob/master/include/llvm/IR/Intrinsics.h#L81
1289 fn llvm_vector_str(elem_ty: ty::Ty, vec_len: usize, no_pointers: usize) -> String {
1290 let p0s: String = "p0".repeat(no_pointers);
1292 ty::Int(v) => format!("v{}{}i{}", vec_len, p0s, v.bit_width().unwrap()),
1293 ty::Uint(v) => format!("v{}{}i{}", vec_len, p0s, v.bit_width().unwrap()),
1294 ty::Float(v) => format!("v{}{}f{}", vec_len, p0s, v.bit_width()),
1295 _ => unreachable!(),
1299 fn llvm_vector_ty(cx: &CodegenCx<'ll, '_>, elem_ty: ty::Ty, vec_len: usize,
1300 mut no_pointers: usize) -> &'ll Type {
1301 // FIXME: use cx.layout_of(ty).llvm_type() ?
1302 let mut elem_ty = match elem_ty.sty {
1303 ty::Int(v) => Type::int_from_ty(cx, v),
1304 ty::Uint(v) => Type::uint_from_ty(cx, v),
1305 ty::Float(v) => Type::float_from_ty(cx, v),
1306 _ => unreachable!(),
1308 while no_pointers > 0 {
1309 elem_ty = elem_ty.ptr_to();
1312 Type::vector(elem_ty, vec_len as u64)
1316 if name == "simd_gather" {
1317 // simd_gather(values: <N x T>, pointers: <N x *_ T>,
1318 // mask: <N x i{M}>) -> <N x T>
1319 // * N: number of elements in the input vectors
1320 // * T: type of the element to load
1321 // * M: any integer width is supported, will be truncated to i1
1323 // All types must be simd vector types
1324 require_simd!(in_ty, "first");
1325 require_simd!(arg_tys[1], "second");
1326 require_simd!(arg_tys[2], "third");
1327 require_simd!(ret_ty, "return");
1329 // Of the same length:
1330 require!(in_len == arg_tys[1].simd_size(tcx),
1331 "expected {} argument with length {} (same as input type `{}`), \
1332 found `{}` with length {}", "second", in_len, in_ty, arg_tys[1],
1333 arg_tys[1].simd_size(tcx));
1334 require!(in_len == arg_tys[2].simd_size(tcx),
1335 "expected {} argument with length {} (same as input type `{}`), \
1336 found `{}` with length {}", "third", in_len, in_ty, arg_tys[2],
1337 arg_tys[2].simd_size(tcx));
1339 // The return type must match the first argument type
1340 require!(ret_ty == in_ty,
1341 "expected return type `{}`, found `{}`",
1344 // This counts how many pointers
1345 fn ptr_count(t: ty::Ty) -> usize {
1347 ty::RawPtr(p) => 1 + ptr_count(p.ty),
1353 fn non_ptr(t: ty::Ty) -> ty::Ty {
1355 ty::RawPtr(p) => non_ptr(p.ty),
1360 // The second argument must be a simd vector with an element type that's a pointer
1361 // to the element type of the first argument
1362 let (pointer_count, underlying_ty) = match arg_tys[1].simd_type(tcx).sty {
1363 ty::RawPtr(p) if p.ty == in_elem => (ptr_count(arg_tys[1].simd_type(tcx)),
1364 non_ptr(arg_tys[1].simd_type(tcx))),
1366 require!(false, "expected element type `{}` of second argument `{}` \
1367 to be a pointer to the element type `{}` of the first \
1368 argument `{}`, found `{}` != `*_ {}`",
1369 arg_tys[1].simd_type(tcx).sty, arg_tys[1], in_elem, in_ty,
1370 arg_tys[1].simd_type(tcx).sty, in_elem);
1374 assert!(pointer_count > 0);
1375 assert_eq!(pointer_count - 1, ptr_count(arg_tys[0].simd_type(tcx)));
1376 assert_eq!(underlying_ty, non_ptr(arg_tys[0].simd_type(tcx)));
1378 // The element type of the third argument must be a signed integer type of any width:
1379 match arg_tys[2].simd_type(tcx).sty {
1382 require!(false, "expected element type `{}` of third argument `{}` \
1383 to be a signed integer type",
1384 arg_tys[2].simd_type(tcx).sty, arg_tys[2]);
1388 // Alignment of T, must be a constant integer value:
1389 let alignment_ty = Type::i32(bx.cx);
1390 let alignment = CodegenCx::c_i32(bx.cx, bx.cx.align_of(in_elem).abi() as i32);
1392 // Truncate the mask vector to a vector of i1s:
1393 let (mask, mask_ty) = {
1394 let i1 = Type::i1(bx.cx);
1395 let i1xn = Type::vector(i1, in_len as u64);
1396 (bx.trunc(args[2].immediate(), i1xn), i1xn)
1399 // Type of the vector of pointers:
1400 let llvm_pointer_vec_ty = llvm_vector_ty(bx.cx, underlying_ty, in_len, pointer_count);
1401 let llvm_pointer_vec_str = llvm_vector_str(underlying_ty, in_len, pointer_count);
1403 // Type of the vector of elements:
1404 let llvm_elem_vec_ty = llvm_vector_ty(bx.cx, underlying_ty, in_len, pointer_count - 1);
1405 let llvm_elem_vec_str = llvm_vector_str(underlying_ty, in_len, pointer_count - 1);
1407 let llvm_intrinsic = format!("llvm.masked.gather.{}.{}",
1408 llvm_elem_vec_str, llvm_pointer_vec_str);
1409 let f = declare::declare_cfn(bx.cx, &llvm_intrinsic,
1411 llvm_pointer_vec_ty,
1414 llvm_elem_vec_ty], llvm_elem_vec_ty));
1415 llvm::SetUnnamedAddr(f, false);
1416 let v = bx.call(f, &[args[1].immediate(), alignment, mask, args[0].immediate()],
1421 if name == "simd_scatter" {
1422 // simd_scatter(values: <N x T>, pointers: <N x *mut T>,
1423 // mask: <N x i{M}>) -> ()
1424 // * N: number of elements in the input vectors
1425 // * T: type of the element to load
1426 // * M: any integer width is supported, will be truncated to i1
1428 // All types must be simd vector types
1429 require_simd!(in_ty, "first");
1430 require_simd!(arg_tys[1], "second");
1431 require_simd!(arg_tys[2], "third");
1433 // Of the same length:
1434 require!(in_len == arg_tys[1].simd_size(tcx),
1435 "expected {} argument with length {} (same as input type `{}`), \
1436 found `{}` with length {}", "second", in_len, in_ty, arg_tys[1],
1437 arg_tys[1].simd_size(tcx));
1438 require!(in_len == arg_tys[2].simd_size(tcx),
1439 "expected {} argument with length {} (same as input type `{}`), \
1440 found `{}` with length {}", "third", in_len, in_ty, arg_tys[2],
1441 arg_tys[2].simd_size(tcx));
1443 // This counts how many pointers
1444 fn ptr_count(t: ty::Ty) -> usize {
1446 ty::RawPtr(p) => 1 + ptr_count(p.ty),
1452 fn non_ptr(t: ty::Ty) -> ty::Ty {
1454 ty::RawPtr(p) => non_ptr(p.ty),
1459 // The second argument must be a simd vector with an element type that's a pointer
1460 // to the element type of the first argument
1461 let (pointer_count, underlying_ty) = match arg_tys[1].simd_type(tcx).sty {
1462 ty::RawPtr(p) if p.ty == in_elem && p.mutbl == hir::MutMutable
1463 => (ptr_count(arg_tys[1].simd_type(tcx)),
1464 non_ptr(arg_tys[1].simd_type(tcx))),
1466 require!(false, "expected element type `{}` of second argument `{}` \
1467 to be a pointer to the element type `{}` of the first \
1468 argument `{}`, found `{}` != `*mut {}`",
1469 arg_tys[1].simd_type(tcx).sty, arg_tys[1], in_elem, in_ty,
1470 arg_tys[1].simd_type(tcx).sty, in_elem);
1474 assert!(pointer_count > 0);
1475 assert_eq!(pointer_count - 1, ptr_count(arg_tys[0].simd_type(tcx)));
1476 assert_eq!(underlying_ty, non_ptr(arg_tys[0].simd_type(tcx)));
1478 // The element type of the third argument must be a signed integer type of any width:
1479 match arg_tys[2].simd_type(tcx).sty {
1482 require!(false, "expected element type `{}` of third argument `{}` \
1483 to be a signed integer type",
1484 arg_tys[2].simd_type(tcx).sty, arg_tys[2]);
1488 // Alignment of T, must be a constant integer value:
1489 let alignment_ty = Type::i32(bx.cx);
1490 let alignment = CodegenCx::c_i32(bx.cx, bx.cx.align_of(in_elem).abi() as i32);
1492 // Truncate the mask vector to a vector of i1s:
1493 let (mask, mask_ty) = {
1494 let i1 = Type::i1(bx.cx);
1495 let i1xn = Type::vector(i1, in_len as u64);
1496 (bx.trunc(args[2].immediate(), i1xn), i1xn)
1499 let ret_t = Type::void(bx.cx);
1501 // Type of the vector of pointers:
1502 let llvm_pointer_vec_ty = llvm_vector_ty(bx.cx, underlying_ty, in_len, pointer_count);
1503 let llvm_pointer_vec_str = llvm_vector_str(underlying_ty, in_len, pointer_count);
1505 // Type of the vector of elements:
1506 let llvm_elem_vec_ty = llvm_vector_ty(bx.cx, underlying_ty, in_len, pointer_count - 1);
1507 let llvm_elem_vec_str = llvm_vector_str(underlying_ty, in_len, pointer_count - 1);
1509 let llvm_intrinsic = format!("llvm.masked.scatter.{}.{}",
1510 llvm_elem_vec_str, llvm_pointer_vec_str);
1511 let f = declare::declare_cfn(bx.cx, &llvm_intrinsic,
1512 Type::func(&[llvm_elem_vec_ty,
1513 llvm_pointer_vec_ty,
1516 llvm::SetUnnamedAddr(f, false);
1517 let v = bx.call(f, &[args[0].immediate(), args[1].immediate(), alignment, mask],
1522 macro_rules! arith_red {
1523 ($name:tt : $integer_reduce:ident, $float_reduce:ident, $ordered:expr) => {
1525 require!(ret_ty == in_elem,
1526 "expected return type `{}` (element of input `{}`), found `{}`",
1527 in_elem, in_ty, ret_ty);
1528 return match in_elem.sty {
1529 ty::Int(_) | ty::Uint(_) => {
1530 let r = bx.$integer_reduce(args[0].immediate());
1532 // if overflow occurs, the result is the
1533 // mathematical result modulo 2^n:
1534 if name.contains("mul") {
1535 Ok(bx.mul(args[1].immediate(), r))
1537 Ok(bx.add(args[1].immediate(), r))
1540 Ok(bx.$integer_reduce(args[0].immediate()))
1544 // ordered arithmetic reductions take an accumulator
1545 let acc = if $ordered {
1546 let acc = args[1].immediate();
1547 // FIXME: https://bugs.llvm.org/show_bug.cgi?id=36734
1548 // * if the accumulator of the fadd isn't 0, incorrect
1549 // code is generated
1550 // * if the accumulator of the fmul isn't 1, incorrect
1551 // code is generated
1552 match CodegenCx::const_get_real(acc) {
1553 None => return_error!("accumulator of {} is not a constant", $name),
1554 Some((v, loses_info)) => {
1555 if $name.contains("mul") && v != 1.0_f64 {
1556 return_error!("accumulator of {} is not 1.0", $name);
1557 } else if $name.contains("add") && v != 0.0_f64 {
1558 return_error!("accumulator of {} is not 0.0", $name);
1559 } else if loses_info {
1560 return_error!("accumulator of {} loses information", $name);
1566 // unordered arithmetic reductions do not:
1567 match f.bit_width() {
1568 32 => CodegenCx::c_undef(Type::f32(bx.cx)),
1569 64 => CodegenCx::c_undef(Type::f64(bx.cx)),
1572 unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#,
1573 $name, in_ty, in_elem, v, ret_ty
1578 Ok(bx.$float_reduce(acc, args[0].immediate()))
1582 "unsupported {} from `{}` with element `{}` to `{}`",
1583 $name, in_ty, in_elem, ret_ty
1591 arith_red!("simd_reduce_add_ordered": vector_reduce_add, vector_reduce_fadd_fast, true);
1592 arith_red!("simd_reduce_mul_ordered": vector_reduce_mul, vector_reduce_fmul_fast, true);
1593 arith_red!("simd_reduce_add_unordered": vector_reduce_add, vector_reduce_fadd_fast, false);
1594 arith_red!("simd_reduce_mul_unordered": vector_reduce_mul, vector_reduce_fmul_fast, false);
1596 macro_rules! minmax_red {
1597 ($name:tt: $int_red:ident, $float_red:ident) => {
1599 require!(ret_ty == in_elem,
1600 "expected return type `{}` (element of input `{}`), found `{}`",
1601 in_elem, in_ty, ret_ty);
1602 return match in_elem.sty {
1604 Ok(bx.$int_red(args[0].immediate(), true))
1607 Ok(bx.$int_red(args[0].immediate(), false))
1610 Ok(bx.$float_red(args[0].immediate()))
1613 return_error!("unsupported {} from `{}` with element `{}` to `{}`",
1614 $name, in_ty, in_elem, ret_ty)
1622 minmax_red!("simd_reduce_min": vector_reduce_min, vector_reduce_fmin);
1623 minmax_red!("simd_reduce_max": vector_reduce_max, vector_reduce_fmax);
1625 minmax_red!("simd_reduce_min_nanless": vector_reduce_min, vector_reduce_fmin_fast);
1626 minmax_red!("simd_reduce_max_nanless": vector_reduce_max, vector_reduce_fmax_fast);
1628 macro_rules! bitwise_red {
1629 ($name:tt : $red:ident, $boolean:expr) => {
1631 let input = if !$boolean {
1632 require!(ret_ty == in_elem,
1633 "expected return type `{}` (element of input `{}`), found `{}`",
1634 in_elem, in_ty, ret_ty);
1638 ty::Int(_) | ty::Uint(_) => {},
1640 return_error!("unsupported {} from `{}` with element `{}` to `{}`",
1641 $name, in_ty, in_elem, ret_ty)
1645 // boolean reductions operate on vectors of i1s:
1646 let i1 = Type::i1(bx.cx);
1647 let i1xn = Type::vector(i1, in_len as u64);
1648 bx.trunc(args[0].immediate(), i1xn)
1650 return match in_elem.sty {
1651 ty::Int(_) | ty::Uint(_) => {
1652 let r = bx.$red(input);
1657 bx.zext(r, Type::bool(bx.cx))
1662 return_error!("unsupported {} from `{}` with element `{}` to `{}`",
1663 $name, in_ty, in_elem, ret_ty)
1670 bitwise_red!("simd_reduce_and": vector_reduce_and, false);
1671 bitwise_red!("simd_reduce_or": vector_reduce_or, false);
1672 bitwise_red!("simd_reduce_xor": vector_reduce_xor, false);
1673 bitwise_red!("simd_reduce_all": vector_reduce_and, true);
1674 bitwise_red!("simd_reduce_any": vector_reduce_or, true);
1676 if name == "simd_cast" {
1677 require_simd!(ret_ty, "return");
1678 let out_len = ret_ty.simd_size(tcx);
1679 require!(in_len == out_len,
1680 "expected return type with length {} (same as input type `{}`), \
1681 found `{}` with length {}",
1684 // casting cares about nominal type, not just structural type
1685 let out_elem = ret_ty.simd_type(tcx);
1687 if in_elem == out_elem { return Ok(args[0].immediate()); }
1689 enum Style { Float, Int(/* is signed? */ bool), Unsupported }
1691 let (in_style, in_width) = match in_elem.sty {
1692 // vectors of pointer-sized integers should've been
1693 // disallowed before here, so this unwrap is safe.
1694 ty::Int(i) => (Style::Int(true), i.bit_width().unwrap()),
1695 ty::Uint(u) => (Style::Int(false), u.bit_width().unwrap()),
1696 ty::Float(f) => (Style::Float, f.bit_width()),
1697 _ => (Style::Unsupported, 0)
1699 let (out_style, out_width) = match out_elem.sty {
1700 ty::Int(i) => (Style::Int(true), i.bit_width().unwrap()),
1701 ty::Uint(u) => (Style::Int(false), u.bit_width().unwrap()),
1702 ty::Float(f) => (Style::Float, f.bit_width()),
1703 _ => (Style::Unsupported, 0)
1706 match (in_style, out_style) {
1707 (Style::Int(in_is_signed), Style::Int(_)) => {
1708 return Ok(match in_width.cmp(&out_width) {
1709 Ordering::Greater => bx.trunc(args[0].immediate(), llret_ty),
1710 Ordering::Equal => args[0].immediate(),
1711 Ordering::Less => if in_is_signed {
1712 bx.sext(args[0].immediate(), llret_ty)
1714 bx.zext(args[0].immediate(), llret_ty)
1718 (Style::Int(in_is_signed), Style::Float) => {
1719 return Ok(if in_is_signed {
1720 bx.sitofp(args[0].immediate(), llret_ty)
1722 bx.uitofp(args[0].immediate(), llret_ty)
1725 (Style::Float, Style::Int(out_is_signed)) => {
1726 return Ok(if out_is_signed {
1727 bx.fptosi(args[0].immediate(), llret_ty)
1729 bx.fptoui(args[0].immediate(), llret_ty)
1732 (Style::Float, Style::Float) => {
1733 return Ok(match in_width.cmp(&out_width) {
1734 Ordering::Greater => bx.fptrunc(args[0].immediate(), llret_ty),
1735 Ordering::Equal => args[0].immediate(),
1736 Ordering::Less => bx.fpext(args[0].immediate(), llret_ty)
1739 _ => {/* Unsupported. Fallthrough. */}
1742 "unsupported cast from `{}` with element `{}` to `{}` with element `{}`",
1746 macro_rules! arith {
1747 ($($name: ident: $($($p: ident),* => $call: ident),*;)*) => {
1748 $(if name == stringify!($name) {
1750 $($(ty::$p(_))|* => {
1751 return Ok(bx.$call(args[0].immediate(), args[1].immediate()))
1756 "unsupported operation on `{}` with element `{}`",
1763 simd_add: Uint, Int => add, Float => fadd;
1764 simd_sub: Uint, Int => sub, Float => fsub;
1765 simd_mul: Uint, Int => mul, Float => fmul;
1766 simd_div: Uint => udiv, Int => sdiv, Float => fdiv;
1767 simd_rem: Uint => urem, Int => srem, Float => frem;
1768 simd_shl: Uint, Int => shl;
1769 simd_shr: Uint => lshr, Int => ashr;
1770 simd_and: Uint, Int => and;
1771 simd_or: Uint, Int => or;
1772 simd_xor: Uint, Int => xor;
1773 simd_fmax: Float => maxnum;
1774 simd_fmin: Float => minnum;
1776 span_bug!(span, "unknown SIMD intrinsic");
1779 // Returns the width of an int Ty, and if it's signed or not
1780 // Returns None if the type is not an integer
1781 // FIXME: there’s multiple of this functions, investigate using some of the already existing
1783 fn int_type_width_signed(ty: Ty, cx: &CodegenCx) -> Option<(u64, bool)> {
1785 ty::Int(t) => Some((match t {
1786 ast::IntTy::Isize => cx.tcx.sess.target.isize_ty.bit_width().unwrap() as u64,
1787 ast::IntTy::I8 => 8,
1788 ast::IntTy::I16 => 16,
1789 ast::IntTy::I32 => 32,
1790 ast::IntTy::I64 => 64,
1791 ast::IntTy::I128 => 128,
1793 ty::Uint(t) => Some((match t {
1794 ast::UintTy::Usize => cx.tcx.sess.target.usize_ty.bit_width().unwrap() as u64,
1795 ast::UintTy::U8 => 8,
1796 ast::UintTy::U16 => 16,
1797 ast::UintTy::U32 => 32,
1798 ast::UintTy::U64 => 64,
1799 ast::UintTy::U128 => 128,
1805 // Returns the width of a float TypeVariant
1806 // Returns None if the type is not a float
1807 fn float_type_width<'tcx>(sty: &ty::TyKind<'tcx>) -> Option<u64> {
1809 ty::Float(t) => Some(t.bit_width() as u64),