1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use rustc::dep_graph::DepGraphSafe;
17 use monomorphize::Instance;
20 use monomorphize::partitioning::CodegenUnit;
22 use type_of::PointeeInfo;
23 use rustc_codegen_ssa::traits::*;
26 use rustc_data_structures::base_n;
27 use rustc_data_structures::small_c_str::SmallCStr;
28 use rustc::mir::mono::Stats;
29 use rustc::session::config::{self, DebugInfo};
30 use rustc::session::Session;
31 use rustc::ty::layout::{LayoutError, LayoutOf, Size, TyLayout, VariantIdx};
32 use rustc::ty::{self, Ty, TyCtxt};
33 use rustc::util::nodemap::FxHashMap;
34 use rustc_target::spec::{HasTargetSpec, Target};
35 use rustc_codegen_ssa::callee::resolve_and_get_fn;
36 use rustc_codegen_ssa::base::wants_msvc_seh;
40 use std::cell::{Cell, RefCell};
44 use syntax::symbol::LocalInternedString;
47 /// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
48 /// `llvm::Context` so that several compilation units may be optimized in parallel.
49 /// All other LLVM data structures in the `CodegenCx` are tied to that `llvm::Context`.
50 pub struct CodegenCx<'ll, 'tcx: 'll> {
51 pub tcx: TyCtxt<'ll, 'tcx, 'tcx>,
52 pub check_overflow: bool,
53 pub use_dll_storage_attrs: bool,
54 pub tls_model: llvm::ThreadLocalMode,
56 pub llmod: &'ll llvm::Module,
57 pub llcx: &'ll llvm::Context,
58 pub stats: RefCell<Stats>,
59 pub codegen_unit: Arc<CodegenUnit<'tcx>>,
61 /// Cache instances of monomorphic and polymorphic items
62 pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
63 /// Cache generated vtables
64 pub vtables: RefCell<FxHashMap<(Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>), &'ll Value>>,
65 /// Cache of constant strings,
66 pub const_cstr_cache: RefCell<FxHashMap<LocalInternedString, &'ll Value>>,
68 /// Reverse-direction for const ptrs cast from globals.
69 /// Key is a Value holding a *T,
70 /// Val is a Value holding a *[T].
72 /// Needed because LLVM loses pointer->pointee association
73 /// when we ptrcast, and we have to ptrcast during codegen
74 /// of a [T] const because we form a slice, a (*T,usize) pair, not
75 /// a pointer to an LLVM array type. Similar for trait objects.
76 pub const_unsized: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
78 /// Cache of emitted const globals (value -> global)
79 pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
81 /// List of globals for static variables which need to be passed to the
82 /// LLVM function ReplaceAllUsesWith (RAUW) when codegen is complete.
83 /// (We have to make sure we don't invalidate any Values referring
85 pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
87 /// Statics that will be placed in the llvm.used variable
88 /// See http://llvm.org/docs/LangRef.html#the-llvm-used-global-variable for details
89 pub used_statics: RefCell<Vec<&'ll Value>>,
91 pub lltypes: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
92 pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
93 pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
94 pub isize_ty: &'ll Type,
96 pub dbg_cx: Option<debuginfo::CrateDebugContext<'ll, 'tcx>>,
98 eh_personality: Cell<Option<&'ll Value>>,
99 eh_unwind_resume: Cell<Option<&'ll Value>>,
100 pub rust_try_fn: Cell<Option<&'ll Value>>,
102 intrinsics: RefCell<FxHashMap<&'static str, &'ll Value>>,
104 /// A counter that is used for generating local symbol names
105 local_gen_sym_counter: Cell<usize>,
108 impl<'ll, 'tcx> DepGraphSafe for CodegenCx<'ll, 'tcx> {}
110 pub fn get_reloc_model(sess: &Session) -> llvm::RelocMode {
111 let reloc_model_arg = match sess.opts.cg.relocation_model {
112 Some(ref s) => &s[..],
113 None => &sess.target.target.options.relocation_model[..],
116 match ::back::write::RELOC_MODEL_ARGS.iter().find(
117 |&&arg| arg.0 == reloc_model_arg) {
120 sess.err(&format!("{:?} is not a valid relocation mode",
122 sess.abort_if_errors();
128 fn get_tls_model(sess: &Session) -> llvm::ThreadLocalMode {
129 let tls_model_arg = match sess.opts.debugging_opts.tls_model {
130 Some(ref s) => &s[..],
131 None => &sess.target.target.options.tls_model[..],
134 match ::back::write::TLS_MODEL_ARGS.iter().find(
135 |&&arg| arg.0 == tls_model_arg) {
138 sess.err(&format!("{:?} is not a valid TLS model",
140 sess.abort_if_errors();
146 fn is_any_library(sess: &Session) -> bool {
147 sess.crate_types.borrow().iter().any(|ty| {
148 *ty != config::CrateType::Executable
152 pub fn is_pie_binary(sess: &Session) -> bool {
153 !is_any_library(sess) && get_reloc_model(sess) == llvm::RelocMode::PIC
156 pub unsafe fn create_module(
158 llcx: &'ll llvm::Context,
160 ) -> &'ll llvm::Module {
161 let mod_name = SmallCStr::new(mod_name);
162 let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
164 // Ensure the data-layout values hardcoded remain the defaults.
165 if sess.target.target.options.is_builtin {
166 let tm = ::back::write::create_target_machine(sess, false);
167 llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
168 llvm::LLVMRustDisposeTargetMachine(tm);
170 let data_layout = llvm::LLVMGetDataLayout(llmod);
171 let data_layout = str::from_utf8(CStr::from_ptr(data_layout).to_bytes())
172 .ok().expect("got a non-UTF8 data-layout from LLVM");
174 // Unfortunately LLVM target specs change over time, and right now we
175 // don't have proper support to work with any more than one
176 // `data_layout` than the one that is in the rust-lang/rust repo. If
177 // this compiler is configured against a custom LLVM, we may have a
178 // differing data layout, even though we should update our own to use
181 // As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
182 // disable this check entirely as we may be configured with something
183 // that has a different target layout.
185 // Unsure if this will actually cause breakage when rustc is configured
189 let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
190 let custom_llvm_used = cfg_llvm_root.trim() != "";
192 if !custom_llvm_used && sess.target.target.data_layout != data_layout {
193 bug!("data-layout for builtin `{}` target, `{}`, \
194 differs from LLVM default, `{}`",
195 sess.target.target.llvm_target,
196 sess.target.target.data_layout,
201 let data_layout = SmallCStr::new(&sess.target.target.data_layout);
202 llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
204 let llvm_target = SmallCStr::new(&sess.target.target.llvm_target);
205 llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
207 if is_pie_binary(sess) {
208 llvm::LLVMRustSetModulePIELevel(llmod);
211 // If skipping the PLT is enabled, we need to add some module metadata
212 // to ensure intrinsic calls don't use it.
213 if !sess.needs_plt() {
214 let avoid_plt = "RtLibUseGOT\0".as_ptr() as *const _;
215 llvm::LLVMRustAddModuleFlag(llmod, avoid_plt, 1);
221 impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
222 crate fn new(tcx: TyCtxt<'ll, 'tcx, 'tcx>,
223 codegen_unit: Arc<CodegenUnit<'tcx>>,
224 llvm_module: &'ll ::ModuleLlvm)
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)`).
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
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.
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!
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.
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;
271 let check_overflow = tcx.sess.overflow_checks();
273 let tls_model = get_tls_model(&tcx.sess);
275 let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
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(),
287 let isize_ty = Type::ix_llcx(llcx, tcx.data_layout.pointer_size.bits());
292 use_dll_storage_attrs,
296 stats: RefCell::new(Stats::default()),
298 instances: Default::default(),
299 vtables: Default::default(),
300 const_cstr_cache: Default::default(),
301 const_unsized: Default::default(),
302 const_globals: Default::default(),
303 statics_to_rauw: RefCell::new(Vec::new()),
304 used_statics: RefCell::new(Vec::new()),
305 lltypes: Default::default(),
306 scalar_lltypes: Default::default(),
307 pointee_infos: Default::default(),
310 eh_personality: Cell::new(None),
311 eh_unwind_resume: Cell::new(None),
312 rust_try_fn: Cell::new(None),
313 intrinsics: Default::default(),
314 local_gen_sym_counter: Cell::new(0),
318 crate fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
319 &self.statics_to_rauw
323 impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
324 fn vtables(&self) -> &RefCell<FxHashMap<(Ty<'tcx>,
325 ty::PolyExistentialTraitRef<'tcx>), &'ll Value>>
330 fn instances(&self) -> &RefCell<FxHashMap<Instance<'tcx>, &'ll Value>> {
334 fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
335 get_fn(self, instance)
338 fn get_param(&self, llfn: &'ll Value, index: c_uint) -> &'ll Value {
339 llvm::get_param(llfn, index)
342 fn eh_personality(&self) -> &'ll Value {
343 // The exception handling personality function.
345 // If our compilation unit has the `eh_personality` lang item somewhere
346 // within it, then we just need to codegen that. Otherwise, we're
347 // building an rlib which will depend on some upstream implementation of
348 // this function, so we just codegen a generic reference to it. We don't
349 // specify any of the types for the function, we just make it a symbol
350 // that LLVM can later use.
352 // Note that MSVC is a little special here in that we don't use the
353 // `eh_personality` lang item at all. Currently LLVM has support for
354 // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
355 // *name of the personality function* to decide what kind of unwind side
356 // tables/landing pads to emit. It looks like Dwarf is used by default,
357 // injecting a dependency on the `_Unwind_Resume` symbol for resuming
358 // an "exception", but for MSVC we want to force SEH. This means that we
359 // can't actually have the personality function be our standard
360 // `rust_eh_personality` function, but rather we wired it up to the
361 // CRT's custom personality function, which forces LLVM to consider
362 // landing pads as "landing pads for SEH".
363 if let Some(llpersonality) = self.eh_personality.get() {
367 let llfn = match tcx.lang_items().eh_personality() {
368 Some(def_id) if !wants_msvc_seh(self.sess()) => {
369 resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]))
372 let name = if wants_msvc_seh(self.sess()) {
375 "rust_eh_personality"
377 let fty = self.type_variadic_func(&[], self.type_i32());
378 self.declare_cfn(name, fty)
381 attributes::apply_target_cpu_attr(self, llfn);
382 self.eh_personality.set(Some(llfn));
386 // Returns a Value of the "eh_unwind_resume" lang item if one is defined,
387 // otherwise declares it as an external function.
388 fn eh_unwind_resume(&self) -> &'ll Value {
390 let unwresume = &self.eh_unwind_resume;
391 if let Some(llfn) = unwresume.get() {
396 assert!(self.sess().target.target.options.custom_unwind_resume);
397 if let Some(def_id) = tcx.lang_items().eh_unwind_resume() {
398 let llfn = resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]));
399 unwresume.set(Some(llfn));
403 let sig = ty::Binder::bind(tcx.mk_fn_sig(
404 iter::once(tcx.mk_mut_ptr(tcx.types.u8)),
407 hir::Unsafety::Unsafe,
411 let llfn = self.declare_fn("rust_eh_unwind_resume", sig);
412 attributes::unwind(llfn, true);
413 attributes::apply_target_cpu_attr(self, llfn);
414 unwresume.set(Some(llfn));
418 fn sess(&self) -> &Session {
422 fn check_overflow(&self) -> bool {
426 fn stats(&self) -> &RefCell<Stats> {
430 fn consume_stats(self) -> RefCell<Stats> {
434 fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>> {
438 fn used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
442 fn set_frame_pointer_elimination(&self, llfn: &'ll Value) {
443 attributes::set_frame_pointer_elimination(self, llfn)
446 fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
447 attributes::apply_target_cpu_attr(self, llfn)
450 fn closure_env_needs_indirect_debuginfo(&self) -> bool {
451 llvm_util::get_major_version() < 6
454 fn create_used_variable(&self) {
455 let name = const_cstr!("llvm.used");
456 let section = const_cstr!("llvm.metadata");
457 let array = self.const_array(
458 &self.type_ptr_to(self.type_i8()),
459 &*self.used_statics.borrow()
463 let g = llvm::LLVMAddGlobal(self.llmod,
466 llvm::LLVMSetInitializer(g, array);
467 llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
468 llvm::LLVMSetSection(g, section.as_ptr());
473 impl CodegenCx<'b, 'tcx> {
474 crate fn get_intrinsic(&self, key: &str) -> &'b Value {
475 if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
479 self.declare_intrinsic(key).unwrap_or_else(|| bug!("unknown intrinsic '{}'", key))
482 fn declare_intrinsic(
485 ) -> Option<&'b Value> {
487 ($name:expr, fn() -> $ret:expr) => (
489 let f = self.declare_cfn($name, self.type_func(&[], $ret));
490 llvm::SetUnnamedAddr(f, false);
491 self.intrinsics.borrow_mut().insert($name, f.clone());
495 ($name:expr, fn(...) -> $ret:expr) => (
497 let f = self.declare_cfn($name, self.type_variadic_func(&[], $ret));
498 llvm::SetUnnamedAddr(f, false);
499 self.intrinsics.borrow_mut().insert($name, f.clone());
503 ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
505 let f = self.declare_cfn($name, self.type_func(&[$($arg),*], $ret));
506 llvm::SetUnnamedAddr(f, false);
507 self.intrinsics.borrow_mut().insert($name, f.clone());
512 macro_rules! mk_struct {
513 ($($field_ty:expr),*) => (self.type_struct( &[$($field_ty),*], false))
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();
527 let t_v2f32 = self.type_vector(t_f32, 2);
528 let t_v4f32 = self.type_vector(t_f32, 4);
529 let t_v8f32 = self.type_vector(t_f32, 8);
530 let t_v16f32 = self.type_vector(t_f32, 16);
532 let t_v2f64 = self.type_vector(t_f64, 2);
533 let t_v4f64 = self.type_vector(t_f64, 4);
534 let t_v8f64 = self.type_vector(t_f64, 8);
536 ifn!("llvm.memset.p0i8.i16", fn(i8p, t_i8, t_i16, t_i32, i1) -> void);
537 ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
538 ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
540 ifn!("llvm.trap", fn() -> void);
541 ifn!("llvm.debugtrap", fn() -> void);
542 ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
544 ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
545 ifn!("llvm.powi.v2f32", fn(t_v2f32, t_i32) -> t_v2f32);
546 ifn!("llvm.powi.v4f32", fn(t_v4f32, t_i32) -> t_v4f32);
547 ifn!("llvm.powi.v8f32", fn(t_v8f32, t_i32) -> t_v8f32);
548 ifn!("llvm.powi.v16f32", fn(t_v16f32, t_i32) -> t_v16f32);
549 ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
550 ifn!("llvm.powi.v2f64", fn(t_v2f64, t_i32) -> t_v2f64);
551 ifn!("llvm.powi.v4f64", fn(t_v4f64, t_i32) -> t_v4f64);
552 ifn!("llvm.powi.v8f64", fn(t_v8f64, t_i32) -> t_v8f64);
554 ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
555 ifn!("llvm.pow.v2f32", fn(t_v2f32, t_v2f32) -> t_v2f32);
556 ifn!("llvm.pow.v4f32", fn(t_v4f32, t_v4f32) -> t_v4f32);
557 ifn!("llvm.pow.v8f32", fn(t_v8f32, t_v8f32) -> t_v8f32);
558 ifn!("llvm.pow.v16f32", fn(t_v16f32, t_v16f32) -> t_v16f32);
559 ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
560 ifn!("llvm.pow.v2f64", fn(t_v2f64, t_v2f64) -> t_v2f64);
561 ifn!("llvm.pow.v4f64", fn(t_v4f64, t_v4f64) -> t_v4f64);
562 ifn!("llvm.pow.v8f64", fn(t_v8f64, t_v8f64) -> t_v8f64);
564 ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
565 ifn!("llvm.sqrt.v2f32", fn(t_v2f32) -> t_v2f32);
566 ifn!("llvm.sqrt.v4f32", fn(t_v4f32) -> t_v4f32);
567 ifn!("llvm.sqrt.v8f32", fn(t_v8f32) -> t_v8f32);
568 ifn!("llvm.sqrt.v16f32", fn(t_v16f32) -> t_v16f32);
569 ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
570 ifn!("llvm.sqrt.v2f64", fn(t_v2f64) -> t_v2f64);
571 ifn!("llvm.sqrt.v4f64", fn(t_v4f64) -> t_v4f64);
572 ifn!("llvm.sqrt.v8f64", fn(t_v8f64) -> t_v8f64);
574 ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
575 ifn!("llvm.sin.v2f32", fn(t_v2f32) -> t_v2f32);
576 ifn!("llvm.sin.v4f32", fn(t_v4f32) -> t_v4f32);
577 ifn!("llvm.sin.v8f32", fn(t_v8f32) -> t_v8f32);
578 ifn!("llvm.sin.v16f32", fn(t_v16f32) -> t_v16f32);
579 ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
580 ifn!("llvm.sin.v2f64", fn(t_v2f64) -> t_v2f64);
581 ifn!("llvm.sin.v4f64", fn(t_v4f64) -> t_v4f64);
582 ifn!("llvm.sin.v8f64", fn(t_v8f64) -> t_v8f64);
584 ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
585 ifn!("llvm.cos.v2f32", fn(t_v2f32) -> t_v2f32);
586 ifn!("llvm.cos.v4f32", fn(t_v4f32) -> t_v4f32);
587 ifn!("llvm.cos.v8f32", fn(t_v8f32) -> t_v8f32);
588 ifn!("llvm.cos.v16f32", fn(t_v16f32) -> t_v16f32);
589 ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
590 ifn!("llvm.cos.v2f64", fn(t_v2f64) -> t_v2f64);
591 ifn!("llvm.cos.v4f64", fn(t_v4f64) -> t_v4f64);
592 ifn!("llvm.cos.v8f64", fn(t_v8f64) -> t_v8f64);
594 ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
595 ifn!("llvm.exp.v2f32", fn(t_v2f32) -> t_v2f32);
596 ifn!("llvm.exp.v4f32", fn(t_v4f32) -> t_v4f32);
597 ifn!("llvm.exp.v8f32", fn(t_v8f32) -> t_v8f32);
598 ifn!("llvm.exp.v16f32", fn(t_v16f32) -> t_v16f32);
599 ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
600 ifn!("llvm.exp.v2f64", fn(t_v2f64) -> t_v2f64);
601 ifn!("llvm.exp.v4f64", fn(t_v4f64) -> t_v4f64);
602 ifn!("llvm.exp.v8f64", fn(t_v8f64) -> t_v8f64);
604 ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
605 ifn!("llvm.exp2.v2f32", fn(t_v2f32) -> t_v2f32);
606 ifn!("llvm.exp2.v4f32", fn(t_v4f32) -> t_v4f32);
607 ifn!("llvm.exp2.v8f32", fn(t_v8f32) -> t_v8f32);
608 ifn!("llvm.exp2.v16f32", fn(t_v16f32) -> t_v16f32);
609 ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
610 ifn!("llvm.exp2.v2f64", fn(t_v2f64) -> t_v2f64);
611 ifn!("llvm.exp2.v4f64", fn(t_v4f64) -> t_v4f64);
612 ifn!("llvm.exp2.v8f64", fn(t_v8f64) -> t_v8f64);
614 ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
615 ifn!("llvm.log.v2f32", fn(t_v2f32) -> t_v2f32);
616 ifn!("llvm.log.v4f32", fn(t_v4f32) -> t_v4f32);
617 ifn!("llvm.log.v8f32", fn(t_v8f32) -> t_v8f32);
618 ifn!("llvm.log.v16f32", fn(t_v16f32) -> t_v16f32);
619 ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
620 ifn!("llvm.log.v2f64", fn(t_v2f64) -> t_v2f64);
621 ifn!("llvm.log.v4f64", fn(t_v4f64) -> t_v4f64);
622 ifn!("llvm.log.v8f64", fn(t_v8f64) -> t_v8f64);
624 ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
625 ifn!("llvm.log10.v2f32", fn(t_v2f32) -> t_v2f32);
626 ifn!("llvm.log10.v4f32", fn(t_v4f32) -> t_v4f32);
627 ifn!("llvm.log10.v8f32", fn(t_v8f32) -> t_v8f32);
628 ifn!("llvm.log10.v16f32", fn(t_v16f32) -> t_v16f32);
629 ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
630 ifn!("llvm.log10.v2f64", fn(t_v2f64) -> t_v2f64);
631 ifn!("llvm.log10.v4f64", fn(t_v4f64) -> t_v4f64);
632 ifn!("llvm.log10.v8f64", fn(t_v8f64) -> t_v8f64);
634 ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
635 ifn!("llvm.log2.v2f32", fn(t_v2f32) -> t_v2f32);
636 ifn!("llvm.log2.v4f32", fn(t_v4f32) -> t_v4f32);
637 ifn!("llvm.log2.v8f32", fn(t_v8f32) -> t_v8f32);
638 ifn!("llvm.log2.v16f32", fn(t_v16f32) -> t_v16f32);
639 ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
640 ifn!("llvm.log2.v2f64", fn(t_v2f64) -> t_v2f64);
641 ifn!("llvm.log2.v4f64", fn(t_v4f64) -> t_v4f64);
642 ifn!("llvm.log2.v8f64", fn(t_v8f64) -> t_v8f64);
644 ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
645 ifn!("llvm.fma.v2f32", fn(t_v2f32, t_v2f32, t_v2f32) -> t_v2f32);
646 ifn!("llvm.fma.v4f32", fn(t_v4f32, t_v4f32, t_v4f32) -> t_v4f32);
647 ifn!("llvm.fma.v8f32", fn(t_v8f32, t_v8f32, t_v8f32) -> t_v8f32);
648 ifn!("llvm.fma.v16f32", fn(t_v16f32, t_v16f32, t_v16f32) -> t_v16f32);
649 ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
650 ifn!("llvm.fma.v2f64", fn(t_v2f64, t_v2f64, t_v2f64) -> t_v2f64);
651 ifn!("llvm.fma.v4f64", fn(t_v4f64, t_v4f64, t_v4f64) -> t_v4f64);
652 ifn!("llvm.fma.v8f64", fn(t_v8f64, t_v8f64, t_v8f64) -> t_v8f64);
654 ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
655 ifn!("llvm.fabs.v2f32", fn(t_v2f32) -> t_v2f32);
656 ifn!("llvm.fabs.v4f32", fn(t_v4f32) -> t_v4f32);
657 ifn!("llvm.fabs.v8f32", fn(t_v8f32) -> t_v8f32);
658 ifn!("llvm.fabs.v16f32", fn(t_v16f32) -> t_v16f32);
659 ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
660 ifn!("llvm.fabs.v2f64", fn(t_v2f64) -> t_v2f64);
661 ifn!("llvm.fabs.v4f64", fn(t_v4f64) -> t_v4f64);
662 ifn!("llvm.fabs.v8f64", fn(t_v8f64) -> t_v8f64);
664 ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
665 ifn!("llvm.floor.v2f32", fn(t_v2f32) -> t_v2f32);
666 ifn!("llvm.floor.v4f32", fn(t_v4f32) -> t_v4f32);
667 ifn!("llvm.floor.v8f32", fn(t_v8f32) -> t_v8f32);
668 ifn!("llvm.floor.v16f32", fn(t_v16f32) -> t_v16f32);
669 ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
670 ifn!("llvm.floor.v2f64", fn(t_v2f64) -> t_v2f64);
671 ifn!("llvm.floor.v4f64", fn(t_v4f64) -> t_v4f64);
672 ifn!("llvm.floor.v8f64", fn(t_v8f64) -> t_v8f64);
674 ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
675 ifn!("llvm.ceil.v2f32", fn(t_v2f32) -> t_v2f32);
676 ifn!("llvm.ceil.v4f32", fn(t_v4f32) -> t_v4f32);
677 ifn!("llvm.ceil.v8f32", fn(t_v8f32) -> t_v8f32);
678 ifn!("llvm.ceil.v16f32", fn(t_v16f32) -> t_v16f32);
679 ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
680 ifn!("llvm.ceil.v2f64", fn(t_v2f64) -> t_v2f64);
681 ifn!("llvm.ceil.v4f64", fn(t_v4f64) -> t_v4f64);
682 ifn!("llvm.ceil.v8f64", fn(t_v8f64) -> t_v8f64);
684 ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
685 ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
687 ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
688 ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
689 ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
690 ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
692 ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
693 ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
694 ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
695 ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
697 ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
698 ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
699 ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
700 ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
701 ifn!("llvm.ctpop.i128", fn(t_i128) -> t_i128);
703 ifn!("llvm.ctlz.i8", fn(t_i8 , i1) -> t_i8);
704 ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
705 ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
706 ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
707 ifn!("llvm.ctlz.i128", fn(t_i128, i1) -> t_i128);
709 ifn!("llvm.cttz.i8", fn(t_i8 , i1) -> t_i8);
710 ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
711 ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
712 ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
713 ifn!("llvm.cttz.i128", fn(t_i128, i1) -> t_i128);
715 ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
716 ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
717 ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
718 ifn!("llvm.bswap.i128", fn(t_i128) -> t_i128);
720 ifn!("llvm.bitreverse.i8", fn(t_i8) -> t_i8);
721 ifn!("llvm.bitreverse.i16", fn(t_i16) -> t_i16);
722 ifn!("llvm.bitreverse.i32", fn(t_i32) -> t_i32);
723 ifn!("llvm.bitreverse.i64", fn(t_i64) -> t_i64);
724 ifn!("llvm.bitreverse.i128", fn(t_i128) -> t_i128);
726 ifn!("llvm.fshl.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
727 ifn!("llvm.fshl.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
728 ifn!("llvm.fshl.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
729 ifn!("llvm.fshl.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
730 ifn!("llvm.fshl.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
732 ifn!("llvm.fshr.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
733 ifn!("llvm.fshr.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
734 ifn!("llvm.fshr.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
735 ifn!("llvm.fshr.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
736 ifn!("llvm.fshr.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
738 ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
739 ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
740 ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
741 ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
742 ifn!("llvm.sadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
744 ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
745 ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
746 ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
747 ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
748 ifn!("llvm.uadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
750 ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
751 ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
752 ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
753 ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
754 ifn!("llvm.ssub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
756 ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
757 ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
758 ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
759 ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
760 ifn!("llvm.usub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
762 ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
763 ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
764 ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
765 ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
766 ifn!("llvm.smul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
768 ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
769 ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
770 ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
771 ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
772 ifn!("llvm.umul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
774 ifn!("llvm.lifetime.start", fn(t_i64,i8p) -> void);
775 ifn!("llvm.lifetime.end", fn(t_i64, i8p) -> void);
777 ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
778 ifn!("llvm.eh.typeid.for", fn(i8p) -> t_i32);
779 ifn!("llvm.localescape", fn(...) -> void);
780 ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
781 ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);
783 ifn!("llvm.assume", fn(i1) -> void);
784 ifn!("llvm.prefetch", fn(i8p, t_i32, t_i32, t_i32) -> void);
786 // variadic intrinsics
787 ifn!("llvm.va_start", fn(i8p) -> void);
788 ifn!("llvm.va_end", fn(i8p) -> void);
789 ifn!("llvm.va_copy", fn(i8p, i8p) -> void);
791 if self.sess().opts.debuginfo != DebugInfo::None {
792 ifn!("llvm.dbg.declare", fn(self.type_metadata(), self.type_metadata()) -> void);
793 ifn!("llvm.dbg.value", fn(self.type_metadata(), t_i64, self.type_metadata()) -> void);
799 impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
800 /// Generate a new symbol name with the given prefix. This symbol name must
801 /// only be used for definitions with `internal` or `private` linkage.
802 pub fn generate_local_symbol_name(&self, prefix: &str) -> String {
803 let idx = self.local_gen_sym_counter.get();
804 self.local_gen_sym_counter.set(idx + 1);
805 // Include a '.' character, so there can be no accidental conflicts with
806 // user defined names
807 let mut name = String::with_capacity(prefix.len() + 6);
808 name.push_str(prefix);
810 base_n::push_str(idx as u128, base_n::ALPHANUMERIC_ONLY, &mut name);
815 impl ty::layout::HasDataLayout for CodegenCx<'ll, 'tcx> {
816 fn data_layout(&self) -> &ty::layout::TargetDataLayout {
817 &self.tcx.data_layout
821 impl HasTargetSpec for CodegenCx<'ll, 'tcx> {
822 fn target_spec(&self) -> &Target {
823 &self.tcx.sess.target.target
827 impl ty::layout::HasTyCtxt<'tcx> for CodegenCx<'ll, 'tcx> {
828 fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
833 impl LayoutOf for CodegenCx<'ll, 'tcx> {
835 type TyLayout = TyLayout<'tcx>;
837 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
838 self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty))
839 .unwrap_or_else(|e| if let LayoutError::SizeOverflow(_) = e {
840 self.sess().fatal(&e.to_string())
842 bug!("failed to get layout for `{}`: {}", ty, e)