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