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