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