]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/context.rs
Merge branch 'refactor-select' of https://github.com/aravind-pg/rust into update...
[rust.git] / src / librustc_trans / context.rs
1 // Copyright 2013 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 use common;
12 use llvm;
13 use llvm::{ContextRef, ModuleRef, ValueRef};
14 use rustc::dep_graph::DepGraphSafe;
15 use rustc::hir;
16 use rustc::hir::def_id::DefId;
17 use rustc::traits;
18 use debuginfo;
19 use callee;
20 use base;
21 use declare;
22 use monomorphize::Instance;
23
24 use monomorphize::partitioning::CodegenUnit;
25 use type_::Type;
26 use type_of::PointeeInfo;
27
28 use rustc_data_structures::base_n;
29 use rustc::mir::mono::Stats;
30 use rustc::session::config::{self, NoDebugInfo};
31 use rustc::session::Session;
32 use rustc::ty::layout::{LayoutError, LayoutOf, Size, TyLayout};
33 use rustc::ty::{self, Ty, TyCtxt};
34 use rustc::util::nodemap::FxHashMap;
35
36 use std::ffi::{CStr, CString};
37 use std::cell::{Cell, RefCell};
38 use std::ptr;
39 use std::iter;
40 use std::str;
41 use std::sync::Arc;
42 use syntax::symbol::InternedString;
43 use abi::Abi;
44
45 /// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
46 /// `ContextRef` so that several compilation units may be optimized in parallel.
47 /// All other LLVM data structures in the `CodegenCx` are tied to that `ContextRef`.
48 pub struct CodegenCx<'a, 'tcx: 'a> {
49     pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
50     pub check_overflow: bool,
51     pub use_dll_storage_attrs: bool,
52     pub tls_model: llvm::ThreadLocalMode,
53
54     pub llmod: ModuleRef,
55     pub llcx: ContextRef,
56     pub stats: RefCell<Stats>,
57     pub codegen_unit: Arc<CodegenUnit<'tcx>>,
58
59     /// Cache instances of monomorphic and polymorphic items
60     pub instances: RefCell<FxHashMap<Instance<'tcx>, ValueRef>>,
61     /// Cache generated vtables
62     pub vtables: RefCell<FxHashMap<(Ty<'tcx>,
63                                 Option<ty::PolyExistentialTraitRef<'tcx>>), ValueRef>>,
64     /// Cache of constant strings,
65     pub const_cstr_cache: RefCell<FxHashMap<InternedString, ValueRef>>,
66
67     /// Reverse-direction for const ptrs cast from globals.
68     /// Key is a ValueRef holding a *T,
69     /// Val is a ValueRef holding a *[T].
70     ///
71     /// Needed because LLVM loses pointer->pointee association
72     /// when we ptrcast, and we have to ptrcast during translation
73     /// of a [T] const because we form a slice, a (*T,usize) pair, not
74     /// a pointer to an LLVM array type. Similar for trait objects.
75     pub const_unsized: RefCell<FxHashMap<ValueRef, ValueRef>>,
76
77     /// Cache of emitted const globals (value -> global)
78     pub const_globals: RefCell<FxHashMap<ValueRef, ValueRef>>,
79
80     /// Mapping from static definitions to their DefId's.
81     pub statics: RefCell<FxHashMap<ValueRef, DefId>>,
82
83     /// List of globals for static variables which need to be passed to the
84     /// LLVM function ReplaceAllUsesWith (RAUW) when translation is complete.
85     /// (We have to make sure we don't invalidate any ValueRefs referring
86     /// to constants.)
87     pub statics_to_rauw: RefCell<Vec<(ValueRef, ValueRef)>>,
88
89     /// Statics that will be placed in the llvm.used variable
90     /// See http://llvm.org/docs/LangRef.html#the-llvm-used-global-variable for details
91     pub used_statics: RefCell<Vec<ValueRef>>,
92
93     pub lltypes: RefCell<FxHashMap<(Ty<'tcx>, Option<usize>), Type>>,
94     pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, Type>>,
95     pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
96     pub isize_ty: Type,
97
98     pub dbg_cx: Option<debuginfo::CrateDebugContext<'tcx>>,
99
100     eh_personality: Cell<Option<ValueRef>>,
101     eh_unwind_resume: Cell<Option<ValueRef>>,
102     pub rust_try_fn: Cell<Option<ValueRef>>,
103
104     intrinsics: RefCell<FxHashMap<&'static str, ValueRef>>,
105
106     /// A counter that is used for generating local symbol names
107     local_gen_sym_counter: Cell<usize>,
108 }
109
110 impl<'a, 'tcx> DepGraphSafe for CodegenCx<'a, 'tcx> {
111 }
112
113 pub fn get_reloc_model(sess: &Session) -> llvm::RelocMode {
114     let reloc_model_arg = match sess.opts.cg.relocation_model {
115         Some(ref s) => &s[..],
116         None => &sess.target.target.options.relocation_model[..],
117     };
118
119     match ::back::write::RELOC_MODEL_ARGS.iter().find(
120         |&&arg| arg.0 == reloc_model_arg) {
121         Some(x) => x.1,
122         _ => {
123             sess.err(&format!("{:?} is not a valid relocation mode",
124                               reloc_model_arg));
125             sess.abort_if_errors();
126             bug!();
127         }
128     }
129 }
130
131 fn get_tls_model(sess: &Session) -> llvm::ThreadLocalMode {
132     let tls_model_arg = match sess.opts.debugging_opts.tls_model {
133         Some(ref s) => &s[..],
134         None => &sess.target.target.options.tls_model[..],
135     };
136
137     match ::back::write::TLS_MODEL_ARGS.iter().find(
138         |&&arg| arg.0 == tls_model_arg) {
139         Some(x) => x.1,
140         _ => {
141             sess.err(&format!("{:?} is not a valid TLS model",
142                               tls_model_arg));
143             sess.abort_if_errors();
144             bug!();
145         }
146     }
147 }
148
149 fn is_any_library(sess: &Session) -> bool {
150     sess.crate_types.borrow().iter().any(|ty| {
151         *ty != config::CrateTypeExecutable
152     })
153 }
154
155 pub fn is_pie_binary(sess: &Session) -> bool {
156     !is_any_library(sess) && get_reloc_model(sess) == llvm::RelocMode::PIC
157 }
158
159 pub unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
160     let llcx = llvm::LLVMRustContextCreate(sess.fewer_names());
161     let mod_name = CString::new(mod_name).unwrap();
162     let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
163
164     // Ensure the data-layout values hardcoded remain the defaults.
165     if sess.target.target.options.is_builtin {
166         let tm = ::back::write::create_target_machine(sess);
167         llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
168         llvm::LLVMRustDisposeTargetMachine(tm);
169
170         let data_layout = llvm::LLVMGetDataLayout(llmod);
171         let data_layout = str::from_utf8(CStr::from_ptr(data_layout).to_bytes())
172             .ok().expect("got a non-UTF8 data-layout from LLVM");
173
174         // Unfortunately LLVM target specs change over time, and right now we
175         // don't have proper support to work with any more than one
176         // `data_layout` than the one that is in the rust-lang/rust repo. If
177         // this compiler is configured against a custom LLVM, we may have a
178         // differing data layout, even though we should update our own to use
179         // that one.
180         //
181         // As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
182         // disable this check entirely as we may be configured with something
183         // that has a different target layout.
184         //
185         // Unsure if this will actually cause breakage when rustc is configured
186         // as such.
187         //
188         // FIXME(#34960)
189         let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
190         let custom_llvm_used = cfg_llvm_root.trim() != "";
191
192         if !custom_llvm_used && sess.target.target.data_layout != data_layout {
193             bug!("data-layout for builtin `{}` target, `{}`, \
194                   differs from LLVM default, `{}`",
195                  sess.target.target.llvm_target,
196                  sess.target.target.data_layout,
197                  data_layout);
198         }
199     }
200
201     let data_layout = CString::new(&sess.target.target.data_layout[..]).unwrap();
202     llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
203
204     let llvm_target = sess.target.target.llvm_target.as_bytes();
205     let llvm_target = CString::new(llvm_target).unwrap();
206     llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
207
208     if is_pie_binary(sess) {
209         llvm::LLVMRustSetModulePIELevel(llmod);
210     }
211
212     (llcx, llmod)
213 }
214
215 impl<'a, 'tcx> CodegenCx<'a, 'tcx> {
216     pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>,
217                codegen_unit: Arc<CodegenUnit<'tcx>>,
218                llmod_id: &str)
219                -> CodegenCx<'a, 'tcx> {
220         // An interesting part of Windows which MSVC forces our hand on (and
221         // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
222         // attributes in LLVM IR as well as native dependencies (in C these
223         // correspond to `__declspec(dllimport)`).
224         //
225         // Whenever a dynamic library is built by MSVC it must have its public
226         // interface specified by functions tagged with `dllexport` or otherwise
227         // they're not available to be linked against. This poses a few problems
228         // for the compiler, some of which are somewhat fundamental, but we use
229         // the `use_dll_storage_attrs` variable below to attach the `dllexport`
230         // attribute to all LLVM functions that are exported e.g. they're
231         // already tagged with external linkage). This is suboptimal for a few
232         // reasons:
233         //
234         // * If an object file will never be included in a dynamic library,
235         //   there's no need to attach the dllexport attribute. Most object
236         //   files in Rust are not destined to become part of a dll as binaries
237         //   are statically linked by default.
238         // * If the compiler is emitting both an rlib and a dylib, the same
239         //   source object file is currently used but with MSVC this may be less
240         //   feasible. The compiler may be able to get around this, but it may
241         //   involve some invasive changes to deal with this.
242         //
243         // The flipside of this situation is that whenever you link to a dll and
244         // you import a function from it, the import should be tagged with
245         // `dllimport`. At this time, however, the compiler does not emit
246         // `dllimport` for any declarations other than constants (where it is
247         // required), which is again suboptimal for even more reasons!
248         //
249         // * Calling a function imported from another dll without using
250         //   `dllimport` causes the linker/compiler to have extra overhead (one
251         //   `jmp` instruction on x86) when calling the function.
252         // * The same object file may be used in different circumstances, so a
253         //   function may be imported from a dll if the object is linked into a
254         //   dll, but it may be just linked against if linked into an rlib.
255         // * The compiler has no knowledge about whether native functions should
256         //   be tagged dllimport or not.
257         //
258         // For now the compiler takes the perf hit (I do not have any numbers to
259         // this effect) by marking very little as `dllimport` and praying the
260         // linker will take care of everything. Fixing this problem will likely
261         // require adding a few attributes to Rust itself (feature gated at the
262         // start) and then strongly recommending static linkage on MSVC!
263         let use_dll_storage_attrs = tcx.sess.target.target.options.is_like_msvc;
264
265         let check_overflow = tcx.sess.overflow_checks();
266
267         let tls_model = get_tls_model(&tcx.sess);
268
269         unsafe {
270             let (llcx, llmod) = create_context_and_module(&tcx.sess,
271                                                           &llmod_id[..]);
272
273             let dbg_cx = if tcx.sess.opts.debuginfo != NoDebugInfo {
274                 let dctx = debuginfo::CrateDebugContext::new(llmod);
275                 debuginfo::metadata::compile_unit_metadata(tcx,
276                                                            codegen_unit.name(),
277                                                            &dctx);
278                 Some(dctx)
279             } else {
280                 None
281             };
282
283             let mut cx = CodegenCx {
284                 tcx,
285                 check_overflow,
286                 use_dll_storage_attrs,
287                 tls_model,
288                 llmod,
289                 llcx,
290                 stats: RefCell::new(Stats::default()),
291                 codegen_unit,
292                 instances: RefCell::new(FxHashMap()),
293                 vtables: RefCell::new(FxHashMap()),
294                 const_cstr_cache: RefCell::new(FxHashMap()),
295                 const_unsized: RefCell::new(FxHashMap()),
296                 const_globals: RefCell::new(FxHashMap()),
297                 statics: RefCell::new(FxHashMap()),
298                 statics_to_rauw: RefCell::new(Vec::new()),
299                 used_statics: RefCell::new(Vec::new()),
300                 lltypes: RefCell::new(FxHashMap()),
301                 scalar_lltypes: RefCell::new(FxHashMap()),
302                 pointee_infos: RefCell::new(FxHashMap()),
303                 isize_ty: Type::from_ref(ptr::null_mut()),
304                 dbg_cx,
305                 eh_personality: Cell::new(None),
306                 eh_unwind_resume: Cell::new(None),
307                 rust_try_fn: Cell::new(None),
308                 intrinsics: RefCell::new(FxHashMap()),
309                 local_gen_sym_counter: Cell::new(0),
310             };
311             cx.isize_ty = Type::isize(&cx);
312             cx
313         }
314     }
315
316     pub fn into_stats(self) -> Stats {
317         self.stats.into_inner()
318     }
319 }
320
321 impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
322     pub fn sess<'a>(&'a self) -> &'a Session {
323         &self.tcx.sess
324     }
325
326     pub fn get_intrinsic(&self, key: &str) -> ValueRef {
327         if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
328             return v;
329         }
330         match declare_intrinsic(self, key) {
331             Some(v) => return v,
332             None => bug!("unknown intrinsic '{}'", key)
333         }
334     }
335
336     /// Generate a new symbol name with the given prefix. This symbol name must
337     /// only be used for definitions with `internal` or `private` linkage.
338     pub fn generate_local_symbol_name(&self, prefix: &str) -> String {
339         let idx = self.local_gen_sym_counter.get();
340         self.local_gen_sym_counter.set(idx + 1);
341         // Include a '.' character, so there can be no accidental conflicts with
342         // user defined names
343         let mut name = String::with_capacity(prefix.len() + 6);
344         name.push_str(prefix);
345         name.push_str(".");
346         base_n::push_str(idx as u128, base_n::ALPHANUMERIC_ONLY, &mut name);
347         name
348     }
349
350     pub fn eh_personality(&self) -> ValueRef {
351         // The exception handling personality function.
352         //
353         // If our compilation unit has the `eh_personality` lang item somewhere
354         // within it, then we just need to translate that. Otherwise, we're
355         // building an rlib which will depend on some upstream implementation of
356         // this function, so we just codegen a generic reference to it. We don't
357         // specify any of the types for the function, we just make it a symbol
358         // that LLVM can later use.
359         //
360         // Note that MSVC is a little special here in that we don't use the
361         // `eh_personality` lang item at all. Currently LLVM has support for
362         // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
363         // *name of the personality function* to decide what kind of unwind side
364         // tables/landing pads to emit. It looks like Dwarf is used by default,
365         // injecting a dependency on the `_Unwind_Resume` symbol for resuming
366         // an "exception", but for MSVC we want to force SEH. This means that we
367         // can't actually have the personality function be our standard
368         // `rust_eh_personality` function, but rather we wired it up to the
369         // CRT's custom personality function, which forces LLVM to consider
370         // landing pads as "landing pads for SEH".
371         if let Some(llpersonality) = self.eh_personality.get() {
372             return llpersonality
373         }
374         let tcx = self.tcx;
375         let llfn = match tcx.lang_items().eh_personality() {
376             Some(def_id) if !base::wants_msvc_seh(self.sess()) => {
377                 callee::resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]))
378             }
379             _ => {
380                 let name = if base::wants_msvc_seh(self.sess()) {
381                     "__CxxFrameHandler3"
382                 } else {
383                     "rust_eh_personality"
384                 };
385                 let fty = Type::variadic_func(&[], &Type::i32(self));
386                 declare::declare_cfn(self, name, fty)
387             }
388         };
389         self.eh_personality.set(Some(llfn));
390         llfn
391     }
392
393     // Returns a ValueRef of the "eh_unwind_resume" lang item if one is defined,
394     // otherwise declares it as an external function.
395     pub fn eh_unwind_resume(&self) -> ValueRef {
396         use attributes;
397         let unwresume = &self.eh_unwind_resume;
398         if let Some(llfn) = unwresume.get() {
399             return llfn;
400         }
401
402         let tcx = self.tcx;
403         assert!(self.sess().target.target.options.custom_unwind_resume);
404         if let Some(def_id) = tcx.lang_items().eh_unwind_resume() {
405             let llfn = callee::resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]));
406             unwresume.set(Some(llfn));
407             return llfn;
408         }
409
410         let ty = tcx.mk_fn_ptr(ty::Binder(tcx.mk_fn_sig(
411             iter::once(tcx.mk_mut_ptr(tcx.types.u8)),
412             tcx.types.never,
413             false,
414             hir::Unsafety::Unsafe,
415             Abi::C
416         )));
417
418         let llfn = declare::declare_fn(self, "rust_eh_unwind_resume", ty);
419         attributes::unwind(llfn, true);
420         unwresume.set(Some(llfn));
421         llfn
422     }
423
424     pub fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
425         common::type_needs_drop(self.tcx, ty)
426     }
427
428     pub fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
429         common::type_is_sized(self.tcx, ty)
430     }
431
432     pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
433         common::type_is_freeze(self.tcx, ty)
434     }
435
436     pub fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
437         use syntax_pos::DUMMY_SP;
438         if ty.is_sized(self.tcx.at(DUMMY_SP), ty::ParamEnv::empty(traits::Reveal::All)) {
439             return false;
440         }
441
442         let tail = self.tcx.struct_tail(ty);
443         match tail.sty {
444             ty::TyForeign(..) => false,
445             ty::TyStr | ty::TySlice(..) | ty::TyDynamic(..) => true,
446             _ => bug!("unexpected unsized tail: {:?}", tail.sty),
447         }
448     }
449 }
450
451 impl<'a, 'tcx> ty::layout::HasDataLayout for &'a CodegenCx<'a, 'tcx> {
452     fn data_layout(&self) -> &ty::layout::TargetDataLayout {
453         &self.tcx.data_layout
454     }
455 }
456
457 impl<'a, 'tcx> ty::layout::HasTyCtxt<'tcx> for &'a CodegenCx<'a, 'tcx> {
458     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
459         self.tcx
460     }
461 }
462
463 impl<'a, 'tcx> LayoutOf<Ty<'tcx>> for &'a CodegenCx<'a, 'tcx> {
464     type TyLayout = TyLayout<'tcx>;
465
466     fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
467         self.tcx.layout_of(ty::ParamEnv::empty(traits::Reveal::All).and(ty))
468             .unwrap_or_else(|e| match e {
469                 LayoutError::SizeOverflow(_) => self.sess().fatal(&e.to_string()),
470                 _ => bug!("failed to get layout for `{}`: {}", ty, e)
471             })
472     }
473 }
474
475 /// Declare any llvm intrinsics that you might need
476 fn declare_intrinsic(cx: &CodegenCx, key: &str) -> Option<ValueRef> {
477     macro_rules! ifn {
478         ($name:expr, fn() -> $ret:expr) => (
479             if key == $name {
480                 let f = declare::declare_cfn(cx, $name, Type::func(&[], &$ret));
481                 llvm::SetUnnamedAddr(f, false);
482                 cx.intrinsics.borrow_mut().insert($name, f.clone());
483                 return Some(f);
484             }
485         );
486         ($name:expr, fn(...) -> $ret:expr) => (
487             if key == $name {
488                 let f = declare::declare_cfn(cx, $name, Type::variadic_func(&[], &$ret));
489                 llvm::SetUnnamedAddr(f, false);
490                 cx.intrinsics.borrow_mut().insert($name, f.clone());
491                 return Some(f);
492             }
493         );
494         ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
495             if key == $name {
496                 let f = declare::declare_cfn(cx, $name, Type::func(&[$($arg),*], &$ret));
497                 llvm::SetUnnamedAddr(f, false);
498                 cx.intrinsics.borrow_mut().insert($name, f.clone());
499                 return Some(f);
500             }
501         );
502     }
503     macro_rules! mk_struct {
504         ($($field_ty:expr),*) => (Type::struct_(cx, &[$($field_ty),*], false))
505     }
506
507     let i8p = Type::i8p(cx);
508     let void = Type::void(cx);
509     let i1 = Type::i1(cx);
510     let t_i8 = Type::i8(cx);
511     let t_i16 = Type::i16(cx);
512     let t_i32 = Type::i32(cx);
513     let t_i64 = Type::i64(cx);
514     let t_i128 = Type::i128(cx);
515     let t_f32 = Type::f32(cx);
516     let t_f64 = Type::f64(cx);
517
518     ifn!("llvm.memcpy.p0i8.p0i8.i16", fn(i8p, i8p, t_i16, t_i32, i1) -> void);
519     ifn!("llvm.memcpy.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
520     ifn!("llvm.memcpy.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
521     ifn!("llvm.memmove.p0i8.p0i8.i16", fn(i8p, i8p, t_i16, t_i32, i1) -> void);
522     ifn!("llvm.memmove.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
523     ifn!("llvm.memmove.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
524     ifn!("llvm.memset.p0i8.i16", fn(i8p, t_i8, t_i16, t_i32, i1) -> void);
525     ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
526     ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
527
528     ifn!("llvm.trap", fn() -> void);
529     ifn!("llvm.debugtrap", fn() -> void);
530     ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
531
532     ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
533     ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
534     ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
535     ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
536
537     ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
538     ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
539     ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
540     ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
541     ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
542     ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
543     ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
544     ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
545     ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
546     ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
547     ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
548     ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
549     ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
550     ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
551     ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
552     ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
553
554     ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
555     ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
556
557     ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
558     ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
559
560     ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
561     ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
562     ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
563     ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
564     ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
565     ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
566
567     ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
568     ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
569     ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
570     ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
571
572     ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
573     ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
574     ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
575     ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
576
577     ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
578     ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
579     ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
580     ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
581     ifn!("llvm.ctpop.i128", fn(t_i128) -> t_i128);
582
583     ifn!("llvm.ctlz.i8", fn(t_i8 , i1) -> t_i8);
584     ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
585     ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
586     ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
587     ifn!("llvm.ctlz.i128", fn(t_i128, i1) -> t_i128);
588
589     ifn!("llvm.cttz.i8", fn(t_i8 , i1) -> t_i8);
590     ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
591     ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
592     ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
593     ifn!("llvm.cttz.i128", fn(t_i128, i1) -> t_i128);
594
595     ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
596     ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
597     ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
598     ifn!("llvm.bswap.i128", fn(t_i128) -> t_i128);
599
600     ifn!("llvm.bitreverse.i8", fn(t_i8) -> t_i8);
601     ifn!("llvm.bitreverse.i16", fn(t_i16) -> t_i16);
602     ifn!("llvm.bitreverse.i32", fn(t_i32) -> t_i32);
603     ifn!("llvm.bitreverse.i64", fn(t_i64) -> t_i64);
604     ifn!("llvm.bitreverse.i128", fn(t_i128) -> t_i128);
605
606     ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
607     ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
608     ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
609     ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
610     ifn!("llvm.sadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
611
612     ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
613     ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
614     ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
615     ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
616     ifn!("llvm.uadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
617
618     ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
619     ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
620     ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
621     ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
622     ifn!("llvm.ssub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
623
624     ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
625     ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
626     ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
627     ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
628     ifn!("llvm.usub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
629
630     ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
631     ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
632     ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
633     ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
634     ifn!("llvm.smul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
635
636     ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
637     ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
638     ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
639     ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
640     ifn!("llvm.umul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
641
642     ifn!("llvm.lifetime.start", fn(t_i64,i8p) -> void);
643     ifn!("llvm.lifetime.end", fn(t_i64, i8p) -> void);
644
645     ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
646     ifn!("llvm.eh.typeid.for", fn(i8p) -> t_i32);
647     ifn!("llvm.localescape", fn(...) -> void);
648     ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
649     ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);
650
651     ifn!("llvm.assume", fn(i1) -> void);
652     ifn!("llvm.prefetch", fn(i8p, t_i32, t_i32, t_i32) -> void);
653
654     if cx.sess().opts.debuginfo != NoDebugInfo {
655         ifn!("llvm.dbg.declare", fn(Type::metadata(cx), Type::metadata(cx)) -> void);
656         ifn!("llvm.dbg.value", fn(Type::metadata(cx), t_i64, Type::metadata(cx)) -> void);
657     }
658     return None;
659 }