]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/intrinsic.rs
6cc6692c3d16b07e578356de7d598125d8424df3
[rust.git] / src / librustc_codegen_llvm / intrinsic.rs
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.
4 //
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.
10
11 #![allow(non_upper_case_globals)]
12
13 use attributes;
14 use intrinsics::{self, Intrinsic};
15 use llvm::{self, TypeKind};
16 use llvm_util;
17 use abi::{Abi, FnType, LlvmType, PassMode};
18 use mir::place::PlaceRef;
19 use mir::operand::{OperandRef, OperandValue};
20 use base::*;
21 use common::*;
22 use context::CodegenCx;
23 use declare;
24 use glue;
25 use type_::Type;
26 use type_of::LayoutLlvmExt;
27 use rustc::ty::{self, Ty};
28 use rustc::ty::layout::LayoutOf;
29 use rustc::hir;
30 use syntax::ast;
31 use syntax::symbol::Symbol;
32 use builder::Builder;
33 use value::Value;
34
35 use interfaces::{BuilderMethods, CommonMethods, TypeMethods};
36
37 use rustc::session::Session;
38 use syntax_pos::Span;
39
40 use std::cmp::Ordering;
41 use std::iter;
42
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",
85         _ => return None
86     };
87     Some(cx.get_intrinsic(&llvm_name))
88 }
89
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>,
95     callee_ty: Ty<'tcx>,
96     fn_ty: &FnType<'tcx, Ty<'tcx>>,
97     args: &[OperandRef<'tcx, &'ll Value>],
98     llresult: &'ll Value,
99     span: Span,
100 ) {
101     let cx = bx.cx();
102     let tcx = cx.tcx;
103
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)
107     };
108
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();
114
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);
117
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<_>>(),
123                     None)
124         }
125         "unreachable" => {
126             return;
127         },
128         "likely" => {
129             let expect = cx.get_intrinsic(&("llvm.expect.i1"));
130             bx.call(expect, &[args[0].immediate(), bx.cx().c_bool(true)], None)
131         }
132         "unlikely" => {
133             let expect = cx.get_intrinsic(&("llvm.expect.i1"));
134             bx.call(expect, &[args[0].immediate(), bx.cx().c_bool(false)], None)
135         }
136         "try" => {
137             try_intrinsic(bx, cx,
138                           args[0].immediate(),
139                           args[1].immediate(),
140                           args[2].immediate(),
141                           llresult);
142             return;
143         }
144         "breakpoint" => {
145             let llfn = cx.get_intrinsic(&("llvm.debugtrap"));
146             bx.call(llfn, &[], None)
147         }
148         "size_of" => {
149             let tp_ty = substs.type_at(0);
150             cx.c_usize(cx.size_of(tp_ty).bytes())
151         }
152         "size_of_val" => {
153             let tp_ty = substs.type_at(0);
154             if let OperandValue::Pair(_, meta) = args[0].val {
155                 let (llsize, _) =
156                     glue::size_and_align_of_dst(bx, tp_ty, Some(meta));
157                 llsize
158             } else {
159                 cx.c_usize(cx.size_of(tp_ty).bytes())
160             }
161         }
162         "min_align_of" => {
163             let tp_ty = substs.type_at(0);
164             cx.c_usize(cx.align_of(tp_ty).abi())
165         }
166         "min_align_of_val" => {
167             let tp_ty = substs.type_at(0);
168             if let OperandValue::Pair(_, meta) = args[0].val {
169                 let (_, llalign) =
170                     glue::size_and_align_of_dst(bx, tp_ty, Some(meta));
171                 llalign
172             } else {
173                 cx.c_usize(cx.align_of(tp_ty).abi())
174             }
175         }
176         "pref_align_of" => {
177             let tp_ty = substs.type_at(0);
178             cx.c_usize(cx.align_of(tp_ty).pref())
179         }
180         "type_name" => {
181             let tp_ty = substs.type_at(0);
182             let ty_name = Symbol::intern(&tp_ty.to_string()).as_str();
183             cx.c_str_slice(ty_name)
184         }
185         "type_id" => {
186             cx.c_u64(cx.tcx.type_id_hash(substs.type_at(0)))
187         }
188         "init" => {
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.)
195                 memset_intrinsic(
196                     bx,
197                     false,
198                     ty,
199                     llresult,
200                     cx.c_u8(0),
201                     cx.c_usize(1)
202                 );
203             }
204             return;
205         }
206         // Effectively no-ops
207         "uninit" | "forget" => {
208             return;
209         }
210         "needs_drop" => {
211             let tp_ty = substs.type_at(0);
212
213             cx.c_bool(bx.cx().type_needs_drop(tp_ty))
214         }
215         "offset" => {
216             let ptr = args[0].immediate();
217             let offset = args[1].immediate();
218             bx.inbounds_gep(ptr, &[offset])
219         }
220         "arith_offset" => {
221             let ptr = args[0].immediate();
222             let offset = args[1].immediate();
223             bx.gep(ptr, &[offset])
224         }
225
226         "copy_nonoverlapping" => {
227             copy_intrinsic(bx, false, false, substs.type_at(0),
228                            args[1].immediate(), args[0].immediate(), args[2].immediate())
229         }
230         "copy" => {
231             copy_intrinsic(bx, true, false, substs.type_at(0),
232                            args[1].immediate(), args[0].immediate(), args[2].immediate())
233         }
234         "write_bytes" => {
235             memset_intrinsic(bx, false, substs.type_at(0),
236                              args[0].immediate(), args[1].immediate(), args[2].immediate())
237         }
238
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())
242         }
243         "volatile_copy_memory" => {
244             copy_intrinsic(bx, true, true, substs.type_at(0),
245                            args[0].immediate(), args[1].immediate(), args[2].immediate())
246         }
247         "volatile_set_memory" => {
248             memset_intrinsic(bx, true, substs.type_at(0),
249                              args[0].immediate(), args[1].immediate(), args[2].immediate())
250         }
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, bx.cx().ptr_to(ty.llvm_type(cx)));
256             }
257             let load = bx.volatile_load(ptr);
258             let align = if name == "unaligned_volatile_load" {
259                 1
260             } else {
261                 cx.align_of(tp_ty).abi() as u32
262             };
263             unsafe {
264                 llvm::LLVMSetAlignment(load, align);
265             }
266             to_immediate(bx, load, cx.layout_of(tp_ty))
267         },
268         "volatile_store" => {
269             let dst = args[0].deref(bx.cx());
270             args[1].val.volatile_store(bx, dst);
271             return;
272         },
273         "unaligned_volatile_store" => {
274             let dst = args[0].deref(bx.cx());
275             args[1].val.unaligned_volatile_store(bx, dst);
276             return;
277         },
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),
286                 _ => bug!()
287             };
288             bx.call(expect, &[
289                 args[0].immediate(),
290                 cx.c_i32(rw),
291                 args[1].immediate(),
292                 cx.c_i32(cache_type)
293             ], None)
294         },
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" => {
300             let ty = arg_tys[0];
301             match int_type_width_signed(ty, cx) {
302                 Some((width, signed)) =>
303                     match name {
304                         "ctlz" | "cttz" => {
305                             let y = cx.c_bool(false);
306                             let llfn = cx.get_intrinsic(&format!("llvm.{}.i{}", name, width));
307                             bx.call(llfn, &[args[0].immediate(), y], None)
308                         }
309                         "ctlz_nonzero" | "cttz_nonzero" => {
310                             let y = cx.c_bool(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)
314                         }
315                         "ctpop" => bx.call(cx.get_intrinsic(&format!("llvm.ctpop.i{}", width)),
316                                         &[args[0].immediate()], None),
317                         "bswap" => {
318                             if width == 8 {
319                                 args[0].immediate() // byte swap a u8/i8 is just a no-op
320                             } else {
321                                 bx.call(cx.get_intrinsic(&format!("llvm.bswap.i{}", width)),
322                                         &[args[0].immediate()], None)
323                             }
324                         }
325                         "bitreverse" => {
326                             bx.call(cx.get_intrinsic(&format!("llvm.bitreverse.i{}", width)),
327                                 &[args[0].immediate()], None)
328                         }
329                         "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" => {
330                             let intrinsic = format!("llvm.{}{}.with.overflow.i{}",
331                                                     if signed { 's' } else { 'u' },
332                                                     &name[..3], width);
333                             let llfn = bx.cx().get_intrinsic(&intrinsic);
334
335                             // Convert `i1` to a `bool`, and write it to the out parameter
336                             let pair = bx.call(llfn, &[
337                                 args[0].immediate(),
338                                 args[1].immediate()
339                             ], None);
340                             let val = bx.extract_value(pair, 0);
341                             let overflow = bx.zext(bx.extract_value(pair, 1), cx.bool());
342
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);
347
348                             return;
349                         },
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()),
353                         "exact_div" =>
354                             if signed {
355                                 bx.exactsdiv(args[0].immediate(), args[1].immediate())
356                             } else {
357                                 bx.exactudiv(args[0].immediate(), args[1].immediate())
358                             },
359                         "unchecked_div" =>
360                             if signed {
361                                 bx.sdiv(args[0].immediate(), args[1].immediate())
362                             } else {
363                                 bx.udiv(args[0].immediate(), args[1].immediate())
364                             },
365                         "unchecked_rem" =>
366                             if signed {
367                                 bx.srem(args[0].immediate(), args[1].immediate())
368                             } else {
369                                 bx.urem(args[0].immediate(), args[1].immediate())
370                             },
371                         "unchecked_shl" => bx.shl(args[0].immediate(), args[1].immediate()),
372                         "unchecked_shr" =>
373                             if signed {
374                                 bx.ashr(args[0].immediate(), args[1].immediate())
375                             } else {
376                                 bx.lshr(args[0].immediate(), args[1].immediate())
377                             },
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)
388                             } else {
389                                 // rotate_left: (X << (S % BW)) | (X >> ((BW - S) % BW))
390                                 // rotate_right: (X << ((BW - S) % BW)) | (X >> (S % BW))
391                                 let width = cx.c_uint(cx.ix(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)
397                             }
398                         },
399                         _ => bug!(),
400                     },
401                 None => {
402                     span_invalid_monomorphization_error(
403                         tcx.sess, span,
404                         &format!("invalid monomorphization of `{}` intrinsic: \
405                                   expected basic integer type, found `{}`", name, ty));
406                     return;
407                 }
408             }
409         },
410         "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" => {
411             let sty = &arg_tys[0].sty;
412             match float_type_width(sty) {
413                 Some(_width) =>
414                     match name {
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()),
420                         _ => bug!(),
421                     },
422                 None => {
423                     span_invalid_monomorphization_error(
424                         tcx.sess, span,
425                         &format!("invalid monomorphization of `{}` intrinsic: \
426                                   expected basic float type, found `{}`", name, sty));
427                     return;
428                 }
429             }
430
431         },
432
433         "discriminant_value" => {
434             args[0].deref(bx.cx()).codegen_get_discr(bx, ret_ty)
435         }
436
437         name if name.starts_with("simd_") => {
438             match generic_simd_intrinsic(bx, name,
439                                          callee_ty,
440                                          args,
441                                          ret_ty, llret_ty,
442                                          span) {
443                 Ok(llval) => llval,
444                 Err(()) => return
445             }
446         }
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::*;
451
452             let split: Vec<&str> = name.split('_').collect();
453
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")
468                 },
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")
475                 },
476                 _ => cx.sess().fatal("Atomic intrinsic not in correct format"),
477             };
478
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));
483             };
484
485             match split[1] {
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(
491                             args[0].immediate(),
492                             args[1].immediate(),
493                             args[2].immediate(),
494                             order,
495                             failorder,
496                             weak);
497                         let val = bx.extract_value(pair, 0);
498                         let success = bx.zext(bx.extract_value(pair, 1), bx.cx().bool());
499
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);
504                         return;
505                     } else {
506                         return invalid_monomorphization(ty);
507                     }
508                 }
509
510                 "load" => {
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)
515                     } else {
516                         return invalid_monomorphization(ty);
517                     }
518                 }
519
520                 "store" => {
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);
525                         return;
526                     } else {
527                         return invalid_monomorphization(ty);
528                     }
529                 }
530
531                 "fence" => {
532                     bx.atomic_fence(order, SynchronizationScope::CrossThread);
533                     return;
534                 }
535
536                 "singlethreadfence" => {
537                     bx.atomic_fence(order, SynchronizationScope::SingleThread);
538                     return;
539                 }
540
541                 // These are all AtomicRMW ops
542                 op => {
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")
556                     };
557
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)
561                     } else {
562                         return invalid_monomorphization(ty);
563                     }
564                 }
565             }
566         }
567
568         "nontemporal_store" => {
569             let dst = args[0].deref(bx.cx());
570             args[1].val.nontemporal_store(bx, dst);
571             return;
572         }
573
574         _ => {
575             let intr = Intrinsic::find(&name).unwrap_or_else(||
576                 bug!("unknown intrinsic '{}'", name));
577
578             fn one<T>(x: Vec<T>) -> T {
579                 assert_eq!(x.len(), 1);
580                 x.into_iter().next().unwrap()
581             }
582             fn ty_to_type(cx: &CodegenCx<'ll, '_>, t: &intrinsics::Type) -> Vec<&'ll Type> {
583                 use intrinsics::Type::*;
584                 match *t {
585                     Void => vec![cx.void()],
586                     Integer(_signed, _width, llvm_width) => {
587                         vec![cx.ix( llvm_width as u64)]
588                     }
589                     Float(x) => {
590                         match x {
591                             32 => vec![cx.f32()],
592                             64 => vec![cx.f64()],
593                             _ => bug!()
594                         }
595                     }
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));
599                         vec![cx.ptr_to(elem)]
600                     }
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![cx.vector(elem, length as u64)]
605                     }
606                     Aggregate(false, ref contents) => {
607                         let elems = contents.iter()
608                                             .map(|t| one(ty_to_type(cx, t)))
609                                             .collect::<Vec<_>>();
610                         vec![cx.struct_( &elems, false)]
611                     }
612                     Aggregate(true, ref contents) => {
613                         contents.iter()
614                                 .flat_map(|t| ty_to_type(cx, t))
615                                 .collect()
616                     }
617                 }
618             }
619
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
623             // cast.
624             fn modify_as_needed(
625                 bx: &Builder<'a, 'll, 'tcx>,
626                 t: &intrinsics::Type,
627                 arg: &OperandRef<'tcx, &'ll Value>,
628             ) -> Vec<&'ll Value> {
629                 match *t {
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.
633                         //
634                         // This assumes the type is "simple", i.e. no
635                         // destructors, and the contents are SIMD
636                         // etc.
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),
640                             _ => bug!()
641                         };
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()
645                         }).collect()
646                     }
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(), bx.cx().ptr_to(llvm_elem))]
650                     }
651                     intrinsics::Type::Vector(_, Some(ref llvm_elem), length) => {
652                         let llvm_elem = one(ty_to_type(bx.cx(), llvm_elem));
653                         vec![
654                             bx.bitcast(arg.immediate(),
655                             bx.cx().vector(llvm_elem, length as u64))
656                         ]
657                     }
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(), bx.cx().ix(llvm_width as u64))]
663                     }
664                     _ => vec![arg.immediate()],
665                 }
666             }
667
668
669             let inputs = intr.inputs.iter()
670                                     .flat_map(|t| ty_to_type(cx, t))
671                                     .collect::<Vec<_>>();
672
673             let outputs = one(ty_to_type(cx, &intr.output));
674
675             let llargs: Vec<_> = intr.inputs.iter().zip(args).flat_map(|(t, arg)| {
676                 modify_as_needed(bx, t, arg)
677             }).collect();
678             assert_eq!(inputs.len(), llargs.len());
679
680             let val = match intr.definition {
681                 intrinsics::IntrinsicDef::Named(name) => {
682                     let f = declare::declare_cfn(cx,
683                                                  name,
684                                                  cx.func(&inputs, outputs));
685                     bx.call(f, &llargs, None)
686                 }
687             };
688
689             match *intr.output {
690                 intrinsics::Type::Aggregate(flatten, ref elems) => {
691                     // the output is a tuple so we need to munge it properly
692                     assert!(!flatten);
693
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);
698                     }
699                     return;
700                 }
701                 _ => val,
702             }
703         }
704     };
705
706     if !fn_ty.ret.is_ignore() {
707         if let PassMode::Cast(ty) = fn_ty.ret.mode {
708             let ptr = bx.pointercast(result.llval, cx.ptr_to(ty.llvm_type(cx)));
709             bx.store(llval, ptr, result.align);
710         } else {
711             OperandRef::from_immediate_or_packed_pair(bx, llval, result.layout)
712                 .val.store(bx, result);
713         }
714     }
715 }
716
717 fn copy_intrinsic(
718     bx: &Builder<'a, 'll, 'tcx>,
719     allow_overlap: bool,
720     volatile: bool,
721     ty: Ty<'tcx>,
722     dst: &'ll Value,
723     src: &'ll Value,
724     count: &'ll Value,
725 ) -> &'ll Value {
726     let cx = bx.cx();
727     let (size, align) = cx.size_and_align_of(ty);
728     let size = cx.c_usize(size.bytes());
729     let align = align.abi();
730     let dst_ptr = bx.pointercast(dst, cx.i8p());
731     let src_ptr = bx.pointercast(src, cx.i8p());
732     if allow_overlap {
733         bx.memmove(dst_ptr, align, src_ptr, align, bx.mul(size, count), volatile)
734     } else {
735         bx.memcpy(dst_ptr, align, src_ptr, align, bx.mul(size, count), volatile)
736     }
737 }
738
739 fn memset_intrinsic(
740     bx: &Builder<'a, 'll, 'tcx>,
741     volatile: bool,
742     ty: Ty<'tcx>,
743     dst: &'ll Value,
744     val: &'ll Value,
745     count: &'ll Value
746 ) -> &'ll Value {
747     let cx = bx.cx();
748     let (size, align) = cx.size_and_align_of(ty);
749     let size = cx.c_usize(size.bytes());
750     let align = cx.c_i32(align.abi() as i32);
751     let dst = bx.pointercast(dst, cx.i8p());
752     call_memset(bx, dst, val, bx.mul(size, count), align, volatile)
753 }
754
755 fn try_intrinsic(
756     bx: &Builder<'a, 'll, 'tcx>,
757     cx: &CodegenCx<'ll, 'tcx>,
758     func: &'ll Value,
759     data: &'ll Value,
760     local_ptr: &'ll Value,
761     dest: &'ll Value,
762 ) {
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(cx.c_null(cx.i8p()), dest, ptr_align);
767     } else if wants_msvc_seh(bx.sess()) {
768         codegen_msvc_try(bx, cx, func, data, local_ptr, dest);
769     } else {
770         codegen_gnu_try(bx, cx, func, data, local_ptr, dest);
771     }
772 }
773
774 // MSVC's definition of the `rust_try` function.
775 //
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.
781 fn codegen_msvc_try(
782     bx: &Builder<'a, 'll, 'tcx>,
783     cx: &CodegenCx<'ll, 'tcx>,
784     func: &'ll Value,
785     data: &'ll Value,
786     local_ptr: &'ll Value,
787     dest: &'ll Value,
788 ) {
789     let llfn = get_rust_try_fn(cx, &mut |bx| {
790         let cx = bx.cx();
791
792         bx.set_personality_fn(bx.cx().eh_personality());
793
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");
798
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);
802
803         // We're generating an IR snippet that looks like:
804         //
805         //   declare i32 @rust_try(%func, %data, %ptr) {
806         //      %slot = alloca i64*
807         //      invoke %func(%data) to label %normal unwind label %catchswitch
808         //
809         //   normal:
810         //      ret i32 0
811         //
812         //   catchswitch:
813         //      %cs = catchswitch within none [%catchpad] unwind to caller
814         //
815         //   catchpad:
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
820         //
821         //   caught:
822         //      ret i32 1
823         //   }
824         //
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:
827         //
828         //      #include <stdint.h>
829         //
830         //      int bar(void (*foo)(void), uint64_t *ret) {
831         //          try {
832         //              foo();
833         //              return 0;
834         //          } catch(uint64_t a[2]) {
835         //              ret[0] = a[0];
836         //              ret[1] = a[1];
837         //              return 1;
838         //          }
839         //      }
840         //
841         // More information can be found in libstd's seh.rs implementation.
842         let i64p = cx.ptr_to(cx.i64());
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);
846
847         normal.ret(cx.c_i32(0));
848
849         let cs = catchswitch.catch_switch(None, None, 1);
850         catchswitch.add_handler(cs, catchpad.llbb());
851
852         let tcx = cx.tcx;
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"),
856         };
857         let tok = catchpad.catch_pad(cs, &[tydesc, cx.c_i32(0), slot]);
858         let addr = catchpad.load(slot, ptr_align);
859
860         let i64_align = bx.tcx().data_layout.i64_align;
861         let arg1 = catchpad.load(addr, i64_align);
862         let val1 = cx.c_i32(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());
868
869         caught.ret(cx.c_i32(1));
870     });
871
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);
877 }
878
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
881 // instructions).
882 //
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.
890 fn codegen_gnu_try(
891     bx: &Builder<'a, 'll, 'tcx>,
892     cx: &CodegenCx<'ll, 'tcx>,
893     func: &'ll Value,
894     data: &'ll Value,
895     local_ptr: &'ll Value,
896     dest: &'ll Value,
897 ) {
898     let llfn = get_rust_try_fn(cx, &mut |bx| {
899         let cx = bx.cx();
900
901         // Codegens the shims described above:
902         //
903         //   bx:
904         //      invoke %func(%args...) normal %normal unwind %catch
905         //
906         //   normal:
907         //      ret 0
908         //
909         //   catch:
910         //      (ptr, _) = landingpad
911         //      store ptr, %local_ptr
912         //      ret 1
913         //
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.
917
918         let then = bx.build_sibling_block("then");
919         let catch = bx.build_sibling_block("catch");
920
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(cx.c_i32(0));
926
927         // Type indicator for the exception being thrown.
928         //
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 = cx.struct_(&[cx.i8p(), cx.i32()], false);
934         let vals = catch.landing_pad(lpad_ty, bx.cx().eh_personality(), 1);
935         catch.add_clause(vals, bx.cx().c_null(cx.i8p()));
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, cx.ptr_to(cx.i8p())), ptr_align);
939         catch.ret(cx.c_i32(1));
940     });
941
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);
947 }
948
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>,
953     name: &str,
954     inputs: Vec<Ty<'tcx>>,
955     output: Ty<'tcx>,
956     codegen: &mut dyn FnMut(Builder<'_, 'll, 'tcx>),
957 ) -> &'ll Value {
958     let rust_fn_sig = ty::Binder::bind(cx.tcx.mk_fn_sig(
959         inputs.into_iter(),
960         output,
961         false,
962         hir::Unsafety::Unsafe,
963         Abi::Rust
964     ));
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");
968     codegen(bx);
969     llfn
970 }
971
972 // Helper function used to get a handle to the `__rust_try` function used to
973 // catch exceptions.
974 //
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>),
979 ) -> &'ll Value {
980     if let Some(llfn) = cx.rust_try_fn.get() {
981         return llfn;
982     }
983
984     // Define the type up front for the signature of the rust_try function.
985     let tcx = cx.tcx;
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(
988         iter::once(i8p),
989         tcx.mk_unit(),
990         false,
991         hir::Unsafety::Unsafe,
992         Abi::Rust
993     )));
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));
997     rust_try
998 }
999
1000 fn span_invalid_monomorphization_error(a: &Session, b: Span, c: &str) {
1001     span_err!(a, b, E0511, "{}", c);
1002 }
1003
1004 fn generic_simd_intrinsic(
1005     bx: &Builder<'a, 'll, 'tcx>,
1006     name: &str,
1007     callee_ty: Ty<'tcx>,
1008     args: &[OperandRef<'tcx, &'ll Value>],
1009     ret_ty: Ty<'tcx>,
1010     llret_ty: &'ll Type,
1011     span: Span
1012 ) -> Result<&'ll Value, ()> {
1013     // macros for error handling:
1014     macro_rules! emit_error {
1015         ($msg: tt) => {
1016             emit_error!($msg, )
1017         };
1018         ($msg: tt, $($fmt: tt)*) => {
1019             span_invalid_monomorphization_error(
1020                 bx.sess(), span,
1021                 &format!(concat!("invalid monomorphization of `{}` intrinsic: ", $msg),
1022                          name, $($fmt)*));
1023         }
1024     }
1025
1026     macro_rules! return_error {
1027         ($($fmt: tt)*) => {
1028             {
1029                 emit_error!($($fmt)*);
1030                 return Err(());
1031             }
1032         }
1033     }
1034
1035     macro_rules! require {
1036         ($cond: expr, $($fmt: tt)*) => {
1037             if !$cond {
1038                 return_error!($($fmt)*);
1039             }
1040         };
1041     }
1042
1043     macro_rules! require_simd {
1044         ($ty: expr, $position: expr) => {
1045             require!($ty.is_simd(), "expected SIMD {} type, found non-SIMD `{}`", $position, $ty)
1046         }
1047     }
1048
1049     let tcx = bx.tcx();
1050     let sig = tcx.normalize_erasing_late_bound_regions(
1051         ty::ParamEnv::reveal_all(),
1052         &callee_ty.fn_sig(tcx),
1053     );
1054     let arg_tys = sig.inputs();
1055
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);
1061
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),
1069         _ => None
1070     };
1071
1072     if let Some(cmp_op) = comparison {
1073         require_simd!(ret_ty, "return");
1074
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 {}",
1079                  in_len, in_ty,
1080                  ret_ty, out_len);
1081         require!(bx.cx().kind(bx.cx().element_type(llret_ty)) == TypeKind::Integer,
1082                  "expected return type with integer elements, found `{}` with non-integer `{}`",
1083                  ret_ty,
1084                  ret_ty.simd_type(tcx));
1085
1086         return Ok(compare_simd_types(bx,
1087                                      args[0].immediate(),
1088                                      args[1].immediate(),
1089                                      in_elem,
1090                                      llret_ty,
1091                                      cmp_op))
1092     }
1093
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?"));
1097
1098         require_simd!(ret_ty, "return");
1099
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 `{}`",
1107                  in_elem, in_ty,
1108                  ret_ty, ret_ty.simd_type(tcx));
1109
1110         let total_len = in_len as u128 * 2;
1111
1112         let vector = args[2].immediate();
1113
1114         let indices: Option<Vec<_>> = (0..n)
1115             .map(|i| {
1116                 let arg_idx = i;
1117                 let val = bx.cx().const_get_elt(vector, i as u64);
1118                 match bx.cx().const_to_opt_u128(val, true) {
1119                     None => {
1120                         emit_error!("shuffle index #{} is not a constant", arg_idx);
1121                         None
1122                     }
1123                     Some(idx) if idx >= total_len => {
1124                         emit_error!("shuffle index #{} is out of bounds (limit {})",
1125                                     arg_idx, total_len);
1126                         None
1127                     }
1128                     Some(idx) => Some(bx.cx().c_i32(idx as i32)),
1129                 }
1130             })
1131             .collect();
1132         let indices = match indices {
1133             Some(i) => i,
1134             None => return Ok(bx.cx().c_null(llret_ty))
1135         };
1136
1137         return Ok(bx.shuffle_vector(args[0].immediate(),
1138                                     args[1].immediate(),
1139                                     bx.cx().c_vector(&indices)))
1140     }
1141
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()))
1149     }
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()))
1155     }
1156
1157     if name == "simd_select" {
1158         let m_elem_ty = in_elem;
1159         let m_len = in_len;
1160         let v_len = arg_tys[1].simd_size(tcx);
1161         require!(m_len == v_len,
1162                  "mismatched lengths: mask length `{}` != other vector length `{}`",
1163                  m_len, v_len
1164         );
1165         match m_elem_ty.sty {
1166             ty::Int(_) => {},
1167             _ => return_error!("mask element type is `{}`, expected `i_`", m_elem_ty)
1168         }
1169         // truncate the mask to a vector of i1s
1170         let i1 = bx.cx().i1();
1171         let i1xn = bx.cx().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()));
1174     }
1175
1176     fn simd_simple_float_intrinsic(
1177         name: &str,
1178         in_elem: &::rustc::ty::TyS,
1179         in_ty: &::rustc::ty::TyS,
1180         in_len: usize,
1181         bx: &Builder<'a, 'll, 'tcx>,
1182         span: Span,
1183         args: &[OperandRef<'tcx, &'ll Value>],
1184     ) -> Result<&'ll Value, ()> {
1185         macro_rules! emit_error {
1186             ($msg: tt) => {
1187                 emit_error!($msg, )
1188             };
1189             ($msg: tt, $($fmt: tt)*) => {
1190                 span_invalid_monomorphization_error(
1191                     bx.sess(), span,
1192                     &format!(concat!("invalid monomorphization of `{}` intrinsic: ", $msg),
1193                              name, $($fmt)*));
1194             }
1195         }
1196         macro_rules! return_error {
1197             ($($fmt: tt)*) => {
1198                 {
1199                     emit_error!($($fmt)*);
1200                     return Err(());
1201                 }
1202             }
1203         }
1204         let ety = match in_elem.sty {
1205             ty::Float(f) if f.bit_width() == 32 => {
1206                 if in_len < 2 || in_len > 16 {
1207                     return_error!(
1208                         "unsupported floating-point vector `{}` with length `{}` \
1209                          out-of-range [2, 16]",
1210                         in_ty, in_len);
1211                 }
1212                 "f32"
1213             },
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]",
1218                                   in_ty, in_len);
1219                 }
1220                 "f64"
1221             },
1222             ty::Float(f) => {
1223                 return_error!("unsupported element type `{}` of floating-point vector `{}`",
1224                               f, in_ty);
1225             },
1226             _ => {
1227                 return_error!("`{}` is not a floating-point type", in_ty);
1228             }
1229         };
1230
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<_>>(),
1235                         None);
1236         unsafe { llvm::LLVMRustSetHasUnsafeAlgebra(c) };
1237         Ok(c)
1238     }
1239
1240     match name {
1241         "simd_fsqrt" => {
1242             return simd_simple_float_intrinsic("sqrt", in_elem, in_ty, in_len, bx, span, args);
1243         }
1244         "simd_fsin" => {
1245             return simd_simple_float_intrinsic("sin", in_elem, in_ty, in_len, bx, span, args);
1246         }
1247         "simd_fcos" => {
1248             return simd_simple_float_intrinsic("cos", in_elem, in_ty, in_len, bx, span, args);
1249         }
1250         "simd_fabs" => {
1251             return simd_simple_float_intrinsic("fabs", in_elem, in_ty, in_len, bx, span, args);
1252         }
1253         "simd_floor" => {
1254             return simd_simple_float_intrinsic("floor", in_elem, in_ty, in_len, bx, span, args);
1255         }
1256         "simd_ceil" => {
1257             return simd_simple_float_intrinsic("ceil", in_elem, in_ty, in_len, bx, span, args);
1258         }
1259         "simd_fexp" => {
1260             return simd_simple_float_intrinsic("exp", in_elem, in_ty, in_len, bx, span, args);
1261         }
1262         "simd_fexp2" => {
1263             return simd_simple_float_intrinsic("exp2", in_elem, in_ty, in_len, bx, span, args);
1264         }
1265         "simd_flog10" => {
1266             return simd_simple_float_intrinsic("log10", in_elem, in_ty, in_len, bx, span, args);
1267         }
1268         "simd_flog2" => {
1269             return simd_simple_float_intrinsic("log2", in_elem, in_ty, in_len, bx, span, args);
1270         }
1271         "simd_flog" => {
1272             return simd_simple_float_intrinsic("log", in_elem, in_ty, in_len, bx, span, args);
1273         }
1274         "simd_fpowi" => {
1275             return simd_simple_float_intrinsic("powi", in_elem, in_ty, in_len, bx, span, args);
1276         }
1277         "simd_fpow" => {
1278             return simd_simple_float_intrinsic("pow", in_elem, in_ty, in_len, bx, span, args);
1279         }
1280         "simd_fma" => {
1281             return simd_simple_float_intrinsic("fma", in_elem, in_ty, in_len, bx, span, args);
1282         }
1283         _ => { /* fallthrough */ }
1284     }
1285
1286     // FIXME: use:
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);
1291         match elem_ty.sty {
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!(),
1296         }
1297     }
1298
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) => cx.int_from_ty( v),
1304             ty::Uint(v) => cx.uint_from_ty( v),
1305             ty::Float(v) => cx.float_from_ty( v),
1306             _ => unreachable!(),
1307         };
1308         while no_pointers > 0 {
1309             elem_ty = cx.ptr_to(elem_ty);
1310             no_pointers -= 1;
1311         }
1312         cx.vector(elem_ty, vec_len as u64)
1313     }
1314
1315
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
1322
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");
1328
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));
1338
1339         // The return type must match the first argument type
1340         require!(ret_ty == in_ty,
1341                  "expected return type `{}`, found `{}`",
1342                  in_ty, ret_ty);
1343
1344         // This counts how many pointers
1345         fn ptr_count(t: ty::Ty) -> usize {
1346             match t.sty {
1347                 ty::RawPtr(p) => 1 + ptr_count(p.ty),
1348                 _ => 0,
1349             }
1350         }
1351
1352         // Non-ptr type
1353         fn non_ptr(t: ty::Ty) -> ty::Ty {
1354             match t.sty {
1355                 ty::RawPtr(p) => non_ptr(p.ty),
1356                 _ => t,
1357             }
1358         }
1359
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))),
1365             _ => {
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);
1371                 unreachable!();
1372             }
1373         };
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)));
1377
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 {
1380             ty::Int(_) => (),
1381             _ => {
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]);
1385             }
1386         }
1387
1388         // Alignment of T, must be a constant integer value:
1389         let alignment_ty = bx.cx().i32();
1390         let alignment = bx.cx().c_i32(bx.cx().align_of(in_elem).abi() as i32);
1391
1392         // Truncate the mask vector to a vector of i1s:
1393         let (mask, mask_ty) = {
1394             let i1 = bx.cx().i1();
1395             let i1xn = bx.cx().vector(i1, in_len as u64);
1396             (bx.trunc(args[2].immediate(), i1xn), i1xn)
1397         };
1398
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);
1402
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);
1406
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,
1410                                      bx.cx().func(&[
1411                                          llvm_pointer_vec_ty,
1412                                          alignment_ty,
1413                                          mask_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()],
1417                         None);
1418         return Ok(v);
1419     }
1420
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
1427
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");
1432
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));
1442
1443         // This counts how many pointers
1444         fn ptr_count(t: ty::Ty) -> usize {
1445             match t.sty {
1446                 ty::RawPtr(p) => 1 + ptr_count(p.ty),
1447                 _ => 0,
1448             }
1449         }
1450
1451         // Non-ptr type
1452         fn non_ptr(t: ty::Ty) -> ty::Ty {
1453             match t.sty {
1454                 ty::RawPtr(p) => non_ptr(p.ty),
1455                 _ => t,
1456             }
1457         }
1458
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))),
1465             _ => {
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);
1471                 unreachable!();
1472             }
1473         };
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)));
1477
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 {
1480             ty::Int(_) => (),
1481             _ => {
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]);
1485             }
1486         }
1487
1488         // Alignment of T, must be a constant integer value:
1489         let alignment_ty = bx.cx().i32();
1490         let alignment = bx.cx().c_i32(bx.cx().align_of(in_elem).abi() as i32);
1491
1492         // Truncate the mask vector to a vector of i1s:
1493         let (mask, mask_ty) = {
1494             let i1 = bx.cx().i1();
1495             let i1xn = bx.cx().vector(i1, in_len as u64);
1496             (bx.trunc(args[2].immediate(), i1xn), i1xn)
1497         };
1498
1499         let ret_t = bx.cx().void();
1500
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);
1504
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);
1508
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                                      bx.cx().func(&[llvm_elem_vec_ty,
1513                                                   llvm_pointer_vec_ty,
1514                                                   alignment_ty,
1515                                                   mask_ty], ret_t));
1516         llvm::SetUnnamedAddr(f, false);
1517         let v = bx.call(f, &[args[0].immediate(), args[1].immediate(), alignment, mask],
1518                         None);
1519         return Ok(v);
1520     }
1521
1522     macro_rules! arith_red {
1523         ($name:tt : $integer_reduce:ident, $float_reduce:ident, $ordered:expr) => {
1524             if name == $name {
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());
1531                         if $ordered {
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))
1536                             } else {
1537                                 Ok(bx.add(args[1].immediate(), r))
1538                             }
1539                         } else {
1540                             Ok(bx.$integer_reduce(args[0].immediate()))
1541                         }
1542                     },
1543                     ty::Float(f) => {
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 bx.cx().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);
1561                                     }
1562                                 }
1563                             }
1564                             acc
1565                         } else {
1566                             // unordered arithmetic reductions do not:
1567                             match f.bit_width() {
1568                                 32 => bx.cx().c_undef(bx.cx().f32()),
1569                                 64 => bx.cx().c_undef(bx.cx().f64()),
1570                                 v => {
1571                                     return_error!(r#"
1572 unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#,
1573                                         $name, in_ty, in_elem, v, ret_ty
1574                                     )
1575                                 }
1576                             }
1577                         };
1578                         Ok(bx.$float_reduce(acc, args[0].immediate()))
1579                     }
1580                     _ => {
1581                         return_error!(
1582                             "unsupported {} from `{}` with element `{}` to `{}`",
1583                             $name, in_ty, in_elem, ret_ty
1584                         )
1585                     },
1586                 }
1587             }
1588         }
1589     }
1590
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);
1595
1596     macro_rules! minmax_red {
1597         ($name:tt: $int_red:ident, $float_red:ident) => {
1598             if name == $name {
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 {
1603                     ty::Int(_i) => {
1604                         Ok(bx.$int_red(args[0].immediate(), true))
1605                     },
1606                     ty::Uint(_u) => {
1607                         Ok(bx.$int_red(args[0].immediate(), false))
1608                     },
1609                     ty::Float(_f) => {
1610                         Ok(bx.$float_red(args[0].immediate()))
1611                     }
1612                     _ => {
1613                         return_error!("unsupported {} from `{}` with element `{}` to `{}`",
1614                                       $name, in_ty, in_elem, ret_ty)
1615                     },
1616                 }
1617             }
1618
1619         }
1620     }
1621
1622     minmax_red!("simd_reduce_min": vector_reduce_min, vector_reduce_fmin);
1623     minmax_red!("simd_reduce_max": vector_reduce_max, vector_reduce_fmax);
1624
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);
1627
1628     macro_rules! bitwise_red {
1629         ($name:tt : $red:ident, $boolean:expr) => {
1630             if name == $name {
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);
1635                     args[0].immediate()
1636                 } else {
1637                     match in_elem.sty {
1638                         ty::Int(_) | ty::Uint(_) => {},
1639                         _ => {
1640                             return_error!("unsupported {} from `{}` with element `{}` to `{}`",
1641                                           $name, in_ty, in_elem, ret_ty)
1642                         }
1643                     }
1644
1645                     // boolean reductions operate on vectors of i1s:
1646                     let i1 = bx.cx().i1();
1647                     let i1xn = bx.cx().vector(i1, in_len as u64);
1648                     bx.trunc(args[0].immediate(), i1xn)
1649                 };
1650                 return match in_elem.sty {
1651                     ty::Int(_) | ty::Uint(_) => {
1652                         let r = bx.$red(input);
1653                         Ok(
1654                             if !$boolean {
1655                                 r
1656                             } else {
1657                                 bx.zext(r, bx.cx().bool())
1658                             }
1659                         )
1660                     },
1661                     _ => {
1662                         return_error!("unsupported {} from `{}` with element `{}` to `{}`",
1663                                       $name, in_ty, in_elem, ret_ty)
1664                     },
1665                 }
1666             }
1667         }
1668     }
1669
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);
1675
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 {}",
1682                  in_len, in_ty,
1683                  ret_ty, out_len);
1684         // casting cares about nominal type, not just structural type
1685         let out_elem = ret_ty.simd_type(tcx);
1686
1687         if in_elem == out_elem { return Ok(args[0].immediate()); }
1688
1689         enum Style { Float, Int(/* is signed? */ bool), Unsupported }
1690
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)
1698         };
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)
1704         };
1705
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)
1713                     } else {
1714                         bx.zext(args[0].immediate(), llret_ty)
1715                     }
1716                 })
1717             }
1718             (Style::Int(in_is_signed), Style::Float) => {
1719                 return Ok(if in_is_signed {
1720                     bx.sitofp(args[0].immediate(), llret_ty)
1721                 } else {
1722                     bx.uitofp(args[0].immediate(), llret_ty)
1723                 })
1724             }
1725             (Style::Float, Style::Int(out_is_signed)) => {
1726                 return Ok(if out_is_signed {
1727                     bx.fptosi(args[0].immediate(), llret_ty)
1728                 } else {
1729                     bx.fptoui(args[0].immediate(), llret_ty)
1730                 })
1731             }
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)
1737                 })
1738             }
1739             _ => {/* Unsupported. Fallthrough. */}
1740         }
1741         require!(false,
1742                  "unsupported cast from `{}` with element `{}` to `{}` with element `{}`",
1743                  in_ty, in_elem,
1744                  ret_ty, out_elem);
1745     }
1746     macro_rules! arith {
1747         ($($name: ident: $($($p: ident),* => $call: ident),*;)*) => {
1748             $(if name == stringify!($name) {
1749                 match in_elem.sty {
1750                     $($(ty::$p(_))|* => {
1751                         return Ok(bx.$call(args[0].immediate(), args[1].immediate()))
1752                     })*
1753                     _ => {},
1754                 }
1755                 require!(false,
1756                          "unsupported operation on `{}` with element `{}`",
1757                          in_ty,
1758                          in_elem)
1759             })*
1760         }
1761     }
1762     arith! {
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;
1775     }
1776     span_bug!(span, "unknown SIMD intrinsic");
1777 }
1778
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
1782 // stuffs.
1783 fn int_type_width_signed(ty: Ty, cx: &CodegenCx) -> Option<(u64, bool)> {
1784     match ty.sty {
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,
1792         }, true)),
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,
1800         }, false)),
1801         _ => None,
1802     }
1803 }
1804
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> {
1808     match *sty {
1809         ty::Float(t) => Some(t.bit_width() as u64),
1810         _ => None,
1811     }
1812 }