]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/context.rs
0a780fecf41de768019dfa228b38ac0aec57403b
[rust.git] / src / librustc_codegen_llvm / context.rs
1 use crate::attributes;
2 use crate::llvm;
3 use crate::debuginfo;
4 use crate::value::Value;
5 use rustc::dep_graph::DepGraphSafe;
6 use rustc::hir;
7
8 use crate::type_::Type;
9 use rustc_codegen_ssa::traits::*;
10
11 use rustc_data_structures::base_n;
12 use rustc_data_structures::small_c_str::SmallCStr;
13 use rustc::mir::mono::CodegenUnit;
14 use rustc::session::config::{self, DebugInfo};
15 use rustc::session::Session;
16 use rustc::ty::layout::{
17     LayoutError, LayoutOf, PointeeInfo, Size, TyLayout, VariantIdx, HasParamEnv
18 };
19 use rustc::ty::{self, Ty, TyCtxt, Instance};
20 use rustc::util::nodemap::FxHashMap;
21 use rustc_target::spec::{HasTargetSpec, Target};
22 use rustc_codegen_ssa::callee::resolve_and_get_fn;
23 use rustc_codegen_ssa::base::wants_msvc_seh;
24 use crate::callee::get_fn;
25
26 use std::ffi::CStr;
27 use std::cell::{Cell, RefCell};
28 use std::iter;
29 use std::str;
30 use std::sync::Arc;
31 use syntax::symbol::LocalInternedString;
32 use crate::abi::Abi;
33
34 /// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
35 /// `llvm::Context` so that several compilation units may be optimized in parallel.
36 /// All other LLVM data structures in the `CodegenCx` are tied to that `llvm::Context`.
37 pub struct CodegenCx<'ll, 'tcx: 'll> {
38     pub tcx: TyCtxt<'tcx, 'tcx>,
39     pub check_overflow: bool,
40     pub use_dll_storage_attrs: bool,
41     pub tls_model: llvm::ThreadLocalMode,
42
43     pub llmod: &'ll llvm::Module,
44     pub llcx: &'ll llvm::Context,
45     pub codegen_unit: Arc<CodegenUnit<'tcx>>,
46
47     /// Cache instances of monomorphic and polymorphic items
48     pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
49     /// Cache generated vtables
50     pub vtables:
51         RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>,
52     /// Cache of constant strings,
53     pub const_cstr_cache: RefCell<FxHashMap<LocalInternedString, &'ll Value>>,
54
55     /// Reverse-direction for const ptrs cast from globals.
56     /// Key is a Value holding a *T,
57     /// Val is a Value holding a *[T].
58     ///
59     /// Needed because LLVM loses pointer->pointee association
60     /// when we ptrcast, and we have to ptrcast during codegen
61     /// of a [T] const because we form a slice, a (*T,usize) pair, not
62     /// a pointer to an LLVM array type. Similar for trait objects.
63     pub const_unsized: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
64
65     /// Cache of emitted const globals (value -> global)
66     pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
67
68     /// List of globals for static variables which need to be passed to the
69     /// LLVM function ReplaceAllUsesWith (RAUW) when codegen is complete.
70     /// (We have to make sure we don't invalidate any Values referring
71     /// to constants.)
72     pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
73
74     /// Statics that will be placed in the llvm.used variable
75     /// See <http://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
76     pub used_statics: RefCell<Vec<&'ll Value>>,
77
78     pub lltypes: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
79     pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
80     pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
81     pub isize_ty: &'ll Type,
82
83     pub dbg_cx: Option<debuginfo::CrateDebugContext<'ll, 'tcx>>,
84
85     eh_personality: Cell<Option<&'ll Value>>,
86     eh_unwind_resume: Cell<Option<&'ll Value>>,
87     pub rust_try_fn: Cell<Option<&'ll Value>>,
88
89     intrinsics: RefCell<FxHashMap<&'static str, &'ll Value>>,
90
91     /// A counter that is used for generating local symbol names
92     local_gen_sym_counter: Cell<usize>,
93 }
94
95 impl<'ll, 'tcx> DepGraphSafe for CodegenCx<'ll, 'tcx> {}
96
97 pub fn get_reloc_model(sess: &Session) -> llvm::RelocMode {
98     let reloc_model_arg = match sess.opts.cg.relocation_model {
99         Some(ref s) => &s[..],
100         None => &sess.target.target.options.relocation_model[..],
101     };
102
103     match crate::back::write::RELOC_MODEL_ARGS.iter().find(
104         |&&arg| arg.0 == reloc_model_arg) {
105         Some(x) => x.1,
106         _ => {
107             sess.err(&format!("{:?} is not a valid relocation mode",
108                               reloc_model_arg));
109             sess.abort_if_errors();
110             bug!();
111         }
112     }
113 }
114
115 fn get_tls_model(sess: &Session) -> llvm::ThreadLocalMode {
116     let tls_model_arg = match sess.opts.debugging_opts.tls_model {
117         Some(ref s) => &s[..],
118         None => &sess.target.target.options.tls_model[..],
119     };
120
121     match crate::back::write::TLS_MODEL_ARGS.iter().find(
122         |&&arg| arg.0 == tls_model_arg) {
123         Some(x) => x.1,
124         _ => {
125             sess.err(&format!("{:?} is not a valid TLS model",
126                               tls_model_arg));
127             sess.abort_if_errors();
128             bug!();
129         }
130     }
131 }
132
133 fn is_any_library(sess: &Session) -> bool {
134     sess.crate_types.borrow().iter().any(|ty| {
135         *ty != config::CrateType::Executable
136     })
137 }
138
139 pub fn is_pie_binary(sess: &Session) -> bool {
140     !is_any_library(sess) && get_reloc_model(sess) == llvm::RelocMode::PIC
141 }
142
143 pub unsafe fn create_module(
144     tcx: TyCtxt<'_, '_>,
145     llcx: &'ll llvm::Context,
146     mod_name: &str,
147 ) -> &'ll llvm::Module {
148     let sess = tcx.sess;
149     let mod_name = SmallCStr::new(mod_name);
150     let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
151
152     // Ensure the data-layout values hardcoded remain the defaults.
153     if sess.target.target.options.is_builtin {
154         let tm = crate::back::write::create_informational_target_machine(&tcx.sess, false);
155         llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
156         llvm::LLVMRustDisposeTargetMachine(tm);
157
158         let data_layout = llvm::LLVMGetDataLayout(llmod);
159         let data_layout = str::from_utf8(CStr::from_ptr(data_layout).to_bytes())
160             .ok().expect("got a non-UTF8 data-layout from LLVM");
161
162         // Unfortunately LLVM target specs change over time, and right now we
163         // don't have proper support to work with any more than one
164         // `data_layout` than the one that is in the rust-lang/rust repo. If
165         // this compiler is configured against a custom LLVM, we may have a
166         // differing data layout, even though we should update our own to use
167         // that one.
168         //
169         // As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
170         // disable this check entirely as we may be configured with something
171         // that has a different target layout.
172         //
173         // Unsure if this will actually cause breakage when rustc is configured
174         // as such.
175         //
176         // FIXME(#34960)
177         let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
178         let custom_llvm_used = cfg_llvm_root.trim() != "";
179
180         if !custom_llvm_used && sess.target.target.data_layout != data_layout {
181             bug!("data-layout for builtin `{}` target, `{}`, \
182                   differs from LLVM default, `{}`",
183                  sess.target.target.llvm_target,
184                  sess.target.target.data_layout,
185                  data_layout);
186         }
187     }
188
189     let data_layout = SmallCStr::new(&sess.target.target.data_layout);
190     llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
191
192     let llvm_target = SmallCStr::new(&sess.target.target.llvm_target);
193     llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
194
195     if is_pie_binary(sess) {
196         llvm::LLVMRustSetModulePIELevel(llmod);
197     }
198
199     // If skipping the PLT is enabled, we need to add some module metadata
200     // to ensure intrinsic calls don't use it.
201     if !sess.needs_plt() {
202         let avoid_plt = "RtLibUseGOT\0".as_ptr() as *const _;
203         llvm::LLVMRustAddModuleFlag(llmod, avoid_plt, 1);
204     }
205
206     llmod
207 }
208
209 impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
210     crate fn new(
211         tcx: TyCtxt<'tcx, 'tcx>,
212         codegen_unit: Arc<CodegenUnit<'tcx>>,
213         llvm_module: &'ll crate::ModuleLlvm,
214     ) -> Self {
215         // An interesting part of Windows which MSVC forces our hand on (and
216         // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
217         // attributes in LLVM IR as well as native dependencies (in C these
218         // correspond to `__declspec(dllimport)`).
219         //
220         // Whenever a dynamic library is built by MSVC it must have its public
221         // interface specified by functions tagged with `dllexport` or otherwise
222         // they're not available to be linked against. This poses a few problems
223         // for the compiler, some of which are somewhat fundamental, but we use
224         // the `use_dll_storage_attrs` variable below to attach the `dllexport`
225         // attribute to all LLVM functions that are exported e.g., they're
226         // already tagged with external linkage). This is suboptimal for a few
227         // reasons:
228         //
229         // * If an object file will never be included in a dynamic library,
230         //   there's no need to attach the dllexport attribute. Most object
231         //   files in Rust are not destined to become part of a dll as binaries
232         //   are statically linked by default.
233         // * If the compiler is emitting both an rlib and a dylib, the same
234         //   source object file is currently used but with MSVC this may be less
235         //   feasible. The compiler may be able to get around this, but it may
236         //   involve some invasive changes to deal with this.
237         //
238         // The flipside of this situation is that whenever you link to a dll and
239         // you import a function from it, the import should be tagged with
240         // `dllimport`. At this time, however, the compiler does not emit
241         // `dllimport` for any declarations other than constants (where it is
242         // required), which is again suboptimal for even more reasons!
243         //
244         // * Calling a function imported from another dll without using
245         //   `dllimport` causes the linker/compiler to have extra overhead (one
246         //   `jmp` instruction on x86) when calling the function.
247         // * The same object file may be used in different circumstances, so a
248         //   function may be imported from a dll if the object is linked into a
249         //   dll, but it may be just linked against if linked into an rlib.
250         // * The compiler has no knowledge about whether native functions should
251         //   be tagged dllimport or not.
252         //
253         // For now the compiler takes the perf hit (I do not have any numbers to
254         // this effect) by marking very little as `dllimport` and praying the
255         // linker will take care of everything. Fixing this problem will likely
256         // require adding a few attributes to Rust itself (feature gated at the
257         // start) and then strongly recommending static linkage on MSVC!
258         let use_dll_storage_attrs = tcx.sess.target.target.options.is_like_msvc;
259
260         let check_overflow = tcx.sess.overflow_checks();
261
262         let tls_model = get_tls_model(&tcx.sess);
263
264         let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
265
266         let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
267             let dctx = debuginfo::CrateDebugContext::new(llmod);
268             debuginfo::metadata::compile_unit_metadata(tcx,
269                                                        &codegen_unit.name().as_str(),
270                                                        &dctx);
271             Some(dctx)
272         } else {
273             None
274         };
275
276         let isize_ty = Type::ix_llcx(llcx, tcx.data_layout.pointer_size.bits());
277
278         CodegenCx {
279             tcx,
280             check_overflow,
281             use_dll_storage_attrs,
282             tls_model,
283             llmod,
284             llcx,
285             codegen_unit,
286             instances: Default::default(),
287             vtables: Default::default(),
288             const_cstr_cache: Default::default(),
289             const_unsized: Default::default(),
290             const_globals: Default::default(),
291             statics_to_rauw: RefCell::new(Vec::new()),
292             used_statics: RefCell::new(Vec::new()),
293             lltypes: Default::default(),
294             scalar_lltypes: Default::default(),
295             pointee_infos: Default::default(),
296             isize_ty,
297             dbg_cx,
298             eh_personality: Cell::new(None),
299             eh_unwind_resume: Cell::new(None),
300             rust_try_fn: Cell::new(None),
301             intrinsics: Default::default(),
302             local_gen_sym_counter: Cell::new(0),
303         }
304     }
305
306     crate fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
307         &self.statics_to_rauw
308     }
309 }
310
311 impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
312     fn vtables(&self) -> &RefCell<FxHashMap<(Ty<'tcx>,
313                                 Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>
314     {
315         &self.vtables
316     }
317
318     fn instances(&self) -> &RefCell<FxHashMap<Instance<'tcx>, &'ll Value>> {
319         &self.instances
320     }
321
322     fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
323         get_fn(self, instance)
324     }
325
326     fn eh_personality(&self) -> &'ll Value {
327         // The exception handling personality function.
328         //
329         // If our compilation unit has the `eh_personality` lang item somewhere
330         // within it, then we just need to codegen that. Otherwise, we're
331         // building an rlib which will depend on some upstream implementation of
332         // this function, so we just codegen a generic reference to it. We don't
333         // specify any of the types for the function, we just make it a symbol
334         // that LLVM can later use.
335         //
336         // Note that MSVC is a little special here in that we don't use the
337         // `eh_personality` lang item at all. Currently LLVM has support for
338         // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
339         // *name of the personality function* to decide what kind of unwind side
340         // tables/landing pads to emit. It looks like Dwarf is used by default,
341         // injecting a dependency on the `_Unwind_Resume` symbol for resuming
342         // an "exception", but for MSVC we want to force SEH. This means that we
343         // can't actually have the personality function be our standard
344         // `rust_eh_personality` function, but rather we wired it up to the
345         // CRT's custom personality function, which forces LLVM to consider
346         // landing pads as "landing pads for SEH".
347         if let Some(llpersonality) = self.eh_personality.get() {
348             return llpersonality
349         }
350         let tcx = self.tcx;
351         let llfn = match tcx.lang_items().eh_personality() {
352             Some(def_id) if !wants_msvc_seh(self.sess()) => {
353                 resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]))
354             }
355             _ => {
356                 let name = if wants_msvc_seh(self.sess()) {
357                     "__CxxFrameHandler3"
358                 } else {
359                     "rust_eh_personality"
360                 };
361                 let fty = self.type_variadic_func(&[], self.type_i32());
362                 self.declare_cfn(name, fty)
363             }
364         };
365         attributes::apply_target_cpu_attr(self, llfn);
366         self.eh_personality.set(Some(llfn));
367         llfn
368     }
369
370     // Returns a Value of the "eh_unwind_resume" lang item if one is defined,
371     // otherwise declares it as an external function.
372     fn eh_unwind_resume(&self) -> &'ll Value {
373         let unwresume = &self.eh_unwind_resume;
374         if let Some(llfn) = unwresume.get() {
375             return llfn;
376         }
377
378         let tcx = self.tcx;
379         assert!(self.sess().target.target.options.custom_unwind_resume);
380         if let Some(def_id) = tcx.lang_items().eh_unwind_resume() {
381             let llfn = resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]));
382             unwresume.set(Some(llfn));
383             return llfn;
384         }
385
386         let sig = ty::Binder::bind(tcx.mk_fn_sig(
387             iter::once(tcx.mk_mut_ptr(tcx.types.u8)),
388             tcx.types.never,
389             false,
390             hir::Unsafety::Unsafe,
391             Abi::C
392         ));
393
394         let llfn = self.declare_fn("rust_eh_unwind_resume", sig);
395         attributes::apply_target_cpu_attr(self, llfn);
396         unwresume.set(Some(llfn));
397         llfn
398     }
399
400     fn sess(&self) -> &Session {
401         &self.tcx.sess
402     }
403
404     fn check_overflow(&self) -> bool {
405         self.check_overflow
406     }
407
408     fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>> {
409         &self.codegen_unit
410     }
411
412     fn used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
413         &self.used_statics
414     }
415
416     fn set_frame_pointer_elimination(&self, llfn: &'ll Value) {
417         attributes::set_frame_pointer_elimination(self, llfn)
418     }
419
420     fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
421         attributes::apply_target_cpu_attr(self, llfn)
422     }
423
424     fn create_used_variable(&self) {
425         let name = const_cstr!("llvm.used");
426         let section = const_cstr!("llvm.metadata");
427         let array = self.const_array(
428             &self.type_ptr_to(self.type_i8()),
429             &*self.used_statics.borrow()
430         );
431
432         unsafe {
433             let g = llvm::LLVMAddGlobal(self.llmod,
434                                         self.val_ty(array),
435                                         name.as_ptr());
436             llvm::LLVMSetInitializer(g, array);
437             llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
438             llvm::LLVMSetSection(g, section.as_ptr());
439         }
440     }
441 }
442
443 impl CodegenCx<'b, 'tcx> {
444     crate fn get_intrinsic(&self, key: &str) -> &'b Value {
445         if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
446             return v;
447         }
448
449         self.declare_intrinsic(key).unwrap_or_else(|| bug!("unknown intrinsic '{}'", key))
450     }
451
452     fn insert_intrinsic(
453         &self, name: &'static str, args: Option<&[&'b llvm::Type]>, ret: &'b llvm::Type
454     ) -> &'b llvm::Value {
455         let fn_ty = if let Some(args) = args {
456             self.type_func(args, ret)
457         } else {
458             self.type_variadic_func(&[], ret)
459         };
460         let f = self.declare_cfn(name, fn_ty);
461         llvm::SetUnnamedAddr(f, false);
462         self.intrinsics.borrow_mut().insert(name, f.clone());
463         f
464     }
465
466     fn declare_intrinsic(
467         &self,
468         key: &str
469     ) -> Option<&'b Value> {
470         macro_rules! ifn {
471             ($name:expr, fn() -> $ret:expr) => (
472                 if key == $name {
473                     return Some(self.insert_intrinsic($name, Some(&[]), $ret));
474                 }
475             );
476             ($name:expr, fn(...) -> $ret:expr) => (
477                 if key == $name {
478                     return Some(self.insert_intrinsic($name, None, $ret));
479                 }
480             );
481             ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
482                 if key == $name {
483                     return Some(self.insert_intrinsic($name, Some(&[$($arg),*]), $ret));
484                 }
485             );
486         }
487         macro_rules! mk_struct {
488             ($($field_ty:expr),*) => (self.type_struct( &[$($field_ty),*], false))
489         }
490
491         let i8p = self.type_i8p();
492         let void = self.type_void();
493         let i1 = self.type_i1();
494         let t_i8 = self.type_i8();
495         let t_i16 = self.type_i16();
496         let t_i32 = self.type_i32();
497         let t_i64 = self.type_i64();
498         let t_i128 = self.type_i128();
499         let t_f32 = self.type_f32();
500         let t_f64 = self.type_f64();
501
502         macro_rules! vector_types {
503             ($id_out:ident: $elem_ty:ident, $len:expr) => {
504                 let $id_out = self.type_vector($elem_ty, $len);
505             };
506             ($($id_out:ident: $elem_ty:ident, $len:expr;)*) => {
507                 $(vector_types!($id_out: $elem_ty, $len);)*
508             }
509         }
510         vector_types! {
511             t_v2f32: t_f32, 2;
512             t_v4f32: t_f32, 4;
513             t_v8f32: t_f32, 8;
514             t_v16f32: t_f32, 16;
515
516             t_v2f64: t_f64, 2;
517             t_v4f64: t_f64, 4;
518             t_v8f64: t_f64, 8;
519         }
520
521         ifn!("llvm.memset.p0i8.i16", fn(i8p, t_i8, t_i16, t_i32, i1) -> void);
522         ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
523         ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
524
525         ifn!("llvm.trap", fn() -> void);
526         ifn!("llvm.debugtrap", fn() -> void);
527         ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
528
529         ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
530         ifn!("llvm.powi.v2f32", fn(t_v2f32, t_i32) -> t_v2f32);
531         ifn!("llvm.powi.v4f32", fn(t_v4f32, t_i32) -> t_v4f32);
532         ifn!("llvm.powi.v8f32", fn(t_v8f32, t_i32) -> t_v8f32);
533         ifn!("llvm.powi.v16f32", fn(t_v16f32, t_i32) -> t_v16f32);
534         ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
535         ifn!("llvm.powi.v2f64", fn(t_v2f64, t_i32) -> t_v2f64);
536         ifn!("llvm.powi.v4f64", fn(t_v4f64, t_i32) -> t_v4f64);
537         ifn!("llvm.powi.v8f64", fn(t_v8f64, t_i32) -> t_v8f64);
538
539         ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
540         ifn!("llvm.pow.v2f32", fn(t_v2f32, t_v2f32) -> t_v2f32);
541         ifn!("llvm.pow.v4f32", fn(t_v4f32, t_v4f32) -> t_v4f32);
542         ifn!("llvm.pow.v8f32", fn(t_v8f32, t_v8f32) -> t_v8f32);
543         ifn!("llvm.pow.v16f32", fn(t_v16f32, t_v16f32) -> t_v16f32);
544         ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
545         ifn!("llvm.pow.v2f64", fn(t_v2f64, t_v2f64) -> t_v2f64);
546         ifn!("llvm.pow.v4f64", fn(t_v4f64, t_v4f64) -> t_v4f64);
547         ifn!("llvm.pow.v8f64", fn(t_v8f64, t_v8f64) -> t_v8f64);
548
549         ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
550         ifn!("llvm.sqrt.v2f32", fn(t_v2f32) -> t_v2f32);
551         ifn!("llvm.sqrt.v4f32", fn(t_v4f32) -> t_v4f32);
552         ifn!("llvm.sqrt.v8f32", fn(t_v8f32) -> t_v8f32);
553         ifn!("llvm.sqrt.v16f32", fn(t_v16f32) -> t_v16f32);
554         ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
555         ifn!("llvm.sqrt.v2f64", fn(t_v2f64) -> t_v2f64);
556         ifn!("llvm.sqrt.v4f64", fn(t_v4f64) -> t_v4f64);
557         ifn!("llvm.sqrt.v8f64", fn(t_v8f64) -> t_v8f64);
558
559         ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
560         ifn!("llvm.sin.v2f32", fn(t_v2f32) -> t_v2f32);
561         ifn!("llvm.sin.v4f32", fn(t_v4f32) -> t_v4f32);
562         ifn!("llvm.sin.v8f32", fn(t_v8f32) -> t_v8f32);
563         ifn!("llvm.sin.v16f32", fn(t_v16f32) -> t_v16f32);
564         ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
565         ifn!("llvm.sin.v2f64", fn(t_v2f64) -> t_v2f64);
566         ifn!("llvm.sin.v4f64", fn(t_v4f64) -> t_v4f64);
567         ifn!("llvm.sin.v8f64", fn(t_v8f64) -> t_v8f64);
568
569         ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
570         ifn!("llvm.cos.v2f32", fn(t_v2f32) -> t_v2f32);
571         ifn!("llvm.cos.v4f32", fn(t_v4f32) -> t_v4f32);
572         ifn!("llvm.cos.v8f32", fn(t_v8f32) -> t_v8f32);
573         ifn!("llvm.cos.v16f32", fn(t_v16f32) -> t_v16f32);
574         ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
575         ifn!("llvm.cos.v2f64", fn(t_v2f64) -> t_v2f64);
576         ifn!("llvm.cos.v4f64", fn(t_v4f64) -> t_v4f64);
577         ifn!("llvm.cos.v8f64", fn(t_v8f64) -> t_v8f64);
578
579         ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
580         ifn!("llvm.exp.v2f32", fn(t_v2f32) -> t_v2f32);
581         ifn!("llvm.exp.v4f32", fn(t_v4f32) -> t_v4f32);
582         ifn!("llvm.exp.v8f32", fn(t_v8f32) -> t_v8f32);
583         ifn!("llvm.exp.v16f32", fn(t_v16f32) -> t_v16f32);
584         ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
585         ifn!("llvm.exp.v2f64", fn(t_v2f64) -> t_v2f64);
586         ifn!("llvm.exp.v4f64", fn(t_v4f64) -> t_v4f64);
587         ifn!("llvm.exp.v8f64", fn(t_v8f64) -> t_v8f64);
588
589         ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
590         ifn!("llvm.exp2.v2f32", fn(t_v2f32) -> t_v2f32);
591         ifn!("llvm.exp2.v4f32", fn(t_v4f32) -> t_v4f32);
592         ifn!("llvm.exp2.v8f32", fn(t_v8f32) -> t_v8f32);
593         ifn!("llvm.exp2.v16f32", fn(t_v16f32) -> t_v16f32);
594         ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
595         ifn!("llvm.exp2.v2f64", fn(t_v2f64) -> t_v2f64);
596         ifn!("llvm.exp2.v4f64", fn(t_v4f64) -> t_v4f64);
597         ifn!("llvm.exp2.v8f64", fn(t_v8f64) -> t_v8f64);
598
599         ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
600         ifn!("llvm.log.v2f32", fn(t_v2f32) -> t_v2f32);
601         ifn!("llvm.log.v4f32", fn(t_v4f32) -> t_v4f32);
602         ifn!("llvm.log.v8f32", fn(t_v8f32) -> t_v8f32);
603         ifn!("llvm.log.v16f32", fn(t_v16f32) -> t_v16f32);
604         ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
605         ifn!("llvm.log.v2f64", fn(t_v2f64) -> t_v2f64);
606         ifn!("llvm.log.v4f64", fn(t_v4f64) -> t_v4f64);
607         ifn!("llvm.log.v8f64", fn(t_v8f64) -> t_v8f64);
608
609         ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
610         ifn!("llvm.log10.v2f32", fn(t_v2f32) -> t_v2f32);
611         ifn!("llvm.log10.v4f32", fn(t_v4f32) -> t_v4f32);
612         ifn!("llvm.log10.v8f32", fn(t_v8f32) -> t_v8f32);
613         ifn!("llvm.log10.v16f32", fn(t_v16f32) -> t_v16f32);
614         ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
615         ifn!("llvm.log10.v2f64", fn(t_v2f64) -> t_v2f64);
616         ifn!("llvm.log10.v4f64", fn(t_v4f64) -> t_v4f64);
617         ifn!("llvm.log10.v8f64", fn(t_v8f64) -> t_v8f64);
618
619         ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
620         ifn!("llvm.log2.v2f32", fn(t_v2f32) -> t_v2f32);
621         ifn!("llvm.log2.v4f32", fn(t_v4f32) -> t_v4f32);
622         ifn!("llvm.log2.v8f32", fn(t_v8f32) -> t_v8f32);
623         ifn!("llvm.log2.v16f32", fn(t_v16f32) -> t_v16f32);
624         ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
625         ifn!("llvm.log2.v2f64", fn(t_v2f64) -> t_v2f64);
626         ifn!("llvm.log2.v4f64", fn(t_v4f64) -> t_v4f64);
627         ifn!("llvm.log2.v8f64", fn(t_v8f64) -> t_v8f64);
628
629         ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
630         ifn!("llvm.fma.v2f32", fn(t_v2f32, t_v2f32, t_v2f32) -> t_v2f32);
631         ifn!("llvm.fma.v4f32", fn(t_v4f32, t_v4f32, t_v4f32) -> t_v4f32);
632         ifn!("llvm.fma.v8f32", fn(t_v8f32, t_v8f32, t_v8f32) -> t_v8f32);
633         ifn!("llvm.fma.v16f32", fn(t_v16f32, t_v16f32, t_v16f32) -> t_v16f32);
634         ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
635         ifn!("llvm.fma.v2f64", fn(t_v2f64, t_v2f64, t_v2f64) -> t_v2f64);
636         ifn!("llvm.fma.v4f64", fn(t_v4f64, t_v4f64, t_v4f64) -> t_v4f64);
637         ifn!("llvm.fma.v8f64", fn(t_v8f64, t_v8f64, t_v8f64) -> t_v8f64);
638
639         ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
640         ifn!("llvm.fabs.v2f32", fn(t_v2f32) -> t_v2f32);
641         ifn!("llvm.fabs.v4f32", fn(t_v4f32) -> t_v4f32);
642         ifn!("llvm.fabs.v8f32", fn(t_v8f32) -> t_v8f32);
643         ifn!("llvm.fabs.v16f32", fn(t_v16f32) -> t_v16f32);
644         ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
645         ifn!("llvm.fabs.v2f64", fn(t_v2f64) -> t_v2f64);
646         ifn!("llvm.fabs.v4f64", fn(t_v4f64) -> t_v4f64);
647         ifn!("llvm.fabs.v8f64", fn(t_v8f64) -> t_v8f64);
648
649         ifn!("llvm.minnum.f32", fn(t_f32, t_f32) -> t_f32);
650         ifn!("llvm.minnum.f64", fn(t_f64, t_f64) -> t_f64);
651         ifn!("llvm.maxnum.f32", fn(t_f32, t_f32) -> t_f32);
652         ifn!("llvm.maxnum.f64", fn(t_f64, t_f64) -> t_f64);
653
654         ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
655         ifn!("llvm.floor.v2f32", fn(t_v2f32) -> t_v2f32);
656         ifn!("llvm.floor.v4f32", fn(t_v4f32) -> t_v4f32);
657         ifn!("llvm.floor.v8f32", fn(t_v8f32) -> t_v8f32);
658         ifn!("llvm.floor.v16f32", fn(t_v16f32) -> t_v16f32);
659         ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
660         ifn!("llvm.floor.v2f64", fn(t_v2f64) -> t_v2f64);
661         ifn!("llvm.floor.v4f64", fn(t_v4f64) -> t_v4f64);
662         ifn!("llvm.floor.v8f64", fn(t_v8f64) -> t_v8f64);
663
664         ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
665         ifn!("llvm.ceil.v2f32", fn(t_v2f32) -> t_v2f32);
666         ifn!("llvm.ceil.v4f32", fn(t_v4f32) -> t_v4f32);
667         ifn!("llvm.ceil.v8f32", fn(t_v8f32) -> t_v8f32);
668         ifn!("llvm.ceil.v16f32", fn(t_v16f32) -> t_v16f32);
669         ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
670         ifn!("llvm.ceil.v2f64", fn(t_v2f64) -> t_v2f64);
671         ifn!("llvm.ceil.v4f64", fn(t_v4f64) -> t_v4f64);
672         ifn!("llvm.ceil.v8f64", fn(t_v8f64) -> t_v8f64);
673
674         ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
675         ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
676
677         ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
678         ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
679         ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
680         ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
681
682         ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
683         ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
684         ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
685         ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
686
687         ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
688         ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
689         ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
690         ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
691         ifn!("llvm.ctpop.i128", fn(t_i128) -> t_i128);
692
693         ifn!("llvm.ctlz.i8", fn(t_i8 , i1) -> t_i8);
694         ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
695         ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
696         ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
697         ifn!("llvm.ctlz.i128", fn(t_i128, i1) -> t_i128);
698
699         ifn!("llvm.cttz.i8", fn(t_i8 , i1) -> t_i8);
700         ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
701         ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
702         ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
703         ifn!("llvm.cttz.i128", fn(t_i128, i1) -> t_i128);
704
705         ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
706         ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
707         ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
708         ifn!("llvm.bswap.i128", fn(t_i128) -> t_i128);
709
710         ifn!("llvm.bitreverse.i8", fn(t_i8) -> t_i8);
711         ifn!("llvm.bitreverse.i16", fn(t_i16) -> t_i16);
712         ifn!("llvm.bitreverse.i32", fn(t_i32) -> t_i32);
713         ifn!("llvm.bitreverse.i64", fn(t_i64) -> t_i64);
714         ifn!("llvm.bitreverse.i128", fn(t_i128) -> t_i128);
715
716         ifn!("llvm.fshl.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
717         ifn!("llvm.fshl.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
718         ifn!("llvm.fshl.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
719         ifn!("llvm.fshl.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
720         ifn!("llvm.fshl.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
721
722         ifn!("llvm.fshr.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
723         ifn!("llvm.fshr.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
724         ifn!("llvm.fshr.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
725         ifn!("llvm.fshr.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
726         ifn!("llvm.fshr.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
727
728         ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
729         ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
730         ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
731         ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
732         ifn!("llvm.sadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
733
734         ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
735         ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
736         ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
737         ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
738         ifn!("llvm.uadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
739
740         ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
741         ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
742         ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
743         ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
744         ifn!("llvm.ssub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
745
746         ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
747         ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
748         ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
749         ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
750         ifn!("llvm.usub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
751
752         ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
753         ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
754         ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
755         ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
756         ifn!("llvm.smul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
757
758         ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
759         ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
760         ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
761         ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
762         ifn!("llvm.umul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
763
764         ifn!("llvm.sadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
765         ifn!("llvm.sadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
766         ifn!("llvm.sadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
767         ifn!("llvm.sadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
768         ifn!("llvm.sadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
769
770         ifn!("llvm.uadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
771         ifn!("llvm.uadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
772         ifn!("llvm.uadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
773         ifn!("llvm.uadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
774         ifn!("llvm.uadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
775
776         ifn!("llvm.ssub.sat.i8", fn(t_i8, t_i8) -> t_i8);
777         ifn!("llvm.ssub.sat.i16", fn(t_i16, t_i16) -> t_i16);
778         ifn!("llvm.ssub.sat.i32", fn(t_i32, t_i32) -> t_i32);
779         ifn!("llvm.ssub.sat.i64", fn(t_i64, t_i64) -> t_i64);
780         ifn!("llvm.ssub.sat.i128", fn(t_i128, t_i128) -> t_i128);
781
782         ifn!("llvm.usub.sat.i8", fn(t_i8, t_i8) -> t_i8);
783         ifn!("llvm.usub.sat.i16", fn(t_i16, t_i16) -> t_i16);
784         ifn!("llvm.usub.sat.i32", fn(t_i32, t_i32) -> t_i32);
785         ifn!("llvm.usub.sat.i64", fn(t_i64, t_i64) -> t_i64);
786         ifn!("llvm.usub.sat.i128", fn(t_i128, t_i128) -> t_i128);
787
788         ifn!("llvm.lifetime.start", fn(t_i64,i8p) -> void);
789         ifn!("llvm.lifetime.end", fn(t_i64, i8p) -> void);
790
791         ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
792         ifn!("llvm.eh.typeid.for", fn(i8p) -> t_i32);
793         ifn!("llvm.localescape", fn(...) -> void);
794         ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
795         ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);
796
797         ifn!("llvm.assume", fn(i1) -> void);
798         ifn!("llvm.prefetch", fn(i8p, t_i32, t_i32, t_i32) -> void);
799
800         // variadic intrinsics
801         ifn!("llvm.va_start", fn(i8p) -> void);
802         ifn!("llvm.va_end", fn(i8p) -> void);
803         ifn!("llvm.va_copy", fn(i8p, i8p) -> void);
804
805         if self.sess().opts.debuginfo != DebugInfo::None {
806             ifn!("llvm.dbg.declare", fn(self.type_metadata(), self.type_metadata()) -> void);
807             ifn!("llvm.dbg.value", fn(self.type_metadata(), t_i64, self.type_metadata()) -> void);
808         }
809         return None;
810     }
811 }
812
813 impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
814     /// Generates a new symbol name with the given prefix. This symbol name must
815     /// only be used for definitions with `internal` or `private` linkage.
816     pub fn generate_local_symbol_name(&self, prefix: &str) -> String {
817         let idx = self.local_gen_sym_counter.get();
818         self.local_gen_sym_counter.set(idx + 1);
819         // Include a '.' character, so there can be no accidental conflicts with
820         // user defined names
821         let mut name = String::with_capacity(prefix.len() + 6);
822         name.push_str(prefix);
823         name.push_str(".");
824         base_n::push_str(idx as u128, base_n::ALPHANUMERIC_ONLY, &mut name);
825         name
826     }
827 }
828
829 impl ty::layout::HasDataLayout for CodegenCx<'ll, 'tcx> {
830     fn data_layout(&self) -> &ty::layout::TargetDataLayout {
831         &self.tcx.data_layout
832     }
833 }
834
835 impl HasTargetSpec for CodegenCx<'ll, 'tcx> {
836     fn target_spec(&self) -> &Target {
837         &self.tcx.sess.target.target
838     }
839 }
840
841 impl ty::layout::HasTyCtxt<'tcx> for CodegenCx<'ll, 'tcx> {
842     fn tcx(&self) -> TyCtxt<'tcx, 'tcx> {
843         self.tcx
844     }
845 }
846
847 impl LayoutOf for CodegenCx<'ll, 'tcx> {
848     type Ty = Ty<'tcx>;
849     type TyLayout = TyLayout<'tcx>;
850
851     fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
852         self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty))
853             .unwrap_or_else(|e| if let LayoutError::SizeOverflow(_) = e {
854                 self.sess().fatal(&e.to_string())
855             } else {
856                 bug!("failed to get layout for `{}`: {}", ty, e)
857             })
858     }
859 }
860
861 impl<'tcx, 'll> HasParamEnv<'tcx> for CodegenCx<'ll, 'tcx> {
862     fn param_env(&self) -> ty::ParamEnv<'tcx> {
863         ty::ParamEnv::reveal_all()
864     }
865 }