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.
13 use rustc::dep_graph::DepGraphSafe;
18 use monomorphize::Instance;
21 use monomorphize::partitioning::CodegenUnit;
23 use type_of::PointeeInfo;
27 use rustc_data_structures::base_n;
28 use rustc_data_structures::small_c_str::SmallCStr;
29 use rustc::mir::mono::Stats;
30 use rustc::session::config::{self, DebugInfo};
31 use rustc::session::Session;
32 use rustc::ty::layout::{LayoutError, LayoutOf, Size, TyLayout, VariantIdx};
33 use rustc::ty::{self, Ty, TyCtxt};
34 use rustc::util::nodemap::FxHashMap;
35 use rustc_target::spec::{HasTargetSpec, Target};
38 use std::cell::{Cell, RefCell};
42 use syntax::symbol::LocalInternedString;
45 /// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
46 /// `llvm::Context` so that several compilation units may be optimized in parallel.
47 /// All other LLVM data structures in the `CodegenCx` are tied to that `llvm::Context`.
48 pub struct CodegenCx<'ll, 'tcx: 'll, V = &'ll Value> {
49 pub tcx: TyCtxt<'ll, 'tcx, 'tcx>,
50 pub check_overflow: bool,
51 pub use_dll_storage_attrs: bool,
52 pub tls_model: llvm::ThreadLocalMode,
54 pub llmod: &'ll llvm::Module,
55 pub llcx: &'ll llvm::Context,
56 pub stats: RefCell<Stats>,
57 pub codegen_unit: Arc<CodegenUnit<'tcx>>,
59 /// Cache instances of monomorphic and polymorphic items
60 pub instances: RefCell<FxHashMap<Instance<'tcx>, V>>,
61 /// Cache generated vtables
62 pub vtables: RefCell<FxHashMap<(Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>), V>>,
63 /// Cache of constant strings,
64 pub const_cstr_cache: RefCell<FxHashMap<LocalInternedString, V>>,
66 /// Reverse-direction for const ptrs cast from globals.
67 /// Key is a Value holding a *T,
68 /// Val is a Value holding a *[T].
70 /// Needed because LLVM loses pointer->pointee association
71 /// when we ptrcast, and we have to ptrcast during codegen
72 /// of a [T] const because we form a slice, a (*T,usize) pair, not
73 /// a pointer to an LLVM array type. Similar for trait objects.
74 pub const_unsized: RefCell<FxHashMap<V, V>>,
76 /// Cache of emitted const globals (value -> global)
77 pub const_globals: RefCell<FxHashMap<V, V>>,
79 /// List of globals for static variables which need to be passed to the
80 /// LLVM function ReplaceAllUsesWith (RAUW) when codegen is complete.
81 /// (We have to make sure we don't invalidate any Values referring
83 pub statics_to_rauw: RefCell<Vec<(V, V)>>,
85 /// Statics that will be placed in the llvm.used variable
86 /// See http://llvm.org/docs/LangRef.html#the-llvm-used-global-variable for details
87 pub used_statics: RefCell<Vec<V>>,
89 pub lltypes: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
90 pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
91 pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
92 pub isize_ty: &'ll Type,
94 pub dbg_cx: Option<debuginfo::CrateDebugContext<'ll, 'tcx>>,
96 eh_personality: Cell<Option<V>>,
97 eh_unwind_resume: Cell<Option<V>>,
98 pub rust_try_fn: Cell<Option<V>>,
100 intrinsics: RefCell<FxHashMap<&'static str, V>>,
102 /// A counter that is used for generating local symbol names
103 local_gen_sym_counter: Cell<usize>,
106 impl<'ll, 'tcx> DepGraphSafe for CodegenCx<'ll, 'tcx> {}
108 pub fn get_reloc_model(sess: &Session) -> llvm::RelocMode {
109 let reloc_model_arg = match sess.opts.cg.relocation_model {
110 Some(ref s) => &s[..],
111 None => &sess.target.target.options.relocation_model[..],
114 match ::back::write::RELOC_MODEL_ARGS.iter().find(
115 |&&arg| arg.0 == reloc_model_arg) {
118 sess.err(&format!("{:?} is not a valid relocation mode",
120 sess.abort_if_errors();
126 fn get_tls_model(sess: &Session) -> llvm::ThreadLocalMode {
127 let tls_model_arg = match sess.opts.debugging_opts.tls_model {
128 Some(ref s) => &s[..],
129 None => &sess.target.target.options.tls_model[..],
132 match ::back::write::TLS_MODEL_ARGS.iter().find(
133 |&&arg| arg.0 == tls_model_arg) {
136 sess.err(&format!("{:?} is not a valid TLS model",
138 sess.abort_if_errors();
144 fn is_any_library(sess: &Session) -> bool {
145 sess.crate_types.borrow().iter().any(|ty| {
146 *ty != config::CrateType::Executable
150 pub fn is_pie_binary(sess: &Session) -> bool {
151 !is_any_library(sess) && get_reloc_model(sess) == llvm::RelocMode::PIC
154 pub unsafe fn create_module(
156 llcx: &'ll llvm::Context,
158 ) -> &'ll llvm::Module {
159 let mod_name = SmallCStr::new(mod_name);
160 let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
162 // Ensure the data-layout values hardcoded remain the defaults.
163 if sess.target.target.options.is_builtin {
164 let tm = ::back::write::create_target_machine(sess, false);
165 llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
166 llvm::LLVMRustDisposeTargetMachine(tm);
168 let data_layout = llvm::LLVMGetDataLayout(llmod);
169 let data_layout = str::from_utf8(CStr::from_ptr(data_layout).to_bytes())
170 .ok().expect("got a non-UTF8 data-layout from LLVM");
172 // Unfortunately LLVM target specs change over time, and right now we
173 // don't have proper support to work with any more than one
174 // `data_layout` than the one that is in the rust-lang/rust repo. If
175 // this compiler is configured against a custom LLVM, we may have a
176 // differing data layout, even though we should update our own to use
179 // As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
180 // disable this check entirely as we may be configured with something
181 // that has a different target layout.
183 // Unsure if this will actually cause breakage when rustc is configured
187 let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
188 let custom_llvm_used = cfg_llvm_root.trim() != "";
190 if !custom_llvm_used && sess.target.target.data_layout != data_layout {
191 bug!("data-layout for builtin `{}` target, `{}`, \
192 differs from LLVM default, `{}`",
193 sess.target.target.llvm_target,
194 sess.target.target.data_layout,
199 let data_layout = SmallCStr::new(&sess.target.target.data_layout);
200 llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
202 let llvm_target = SmallCStr::new(&sess.target.target.llvm_target);
203 llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
205 if is_pie_binary(sess) {
206 llvm::LLVMRustSetModulePIELevel(llmod);
209 // If skipping the PLT is enabled, we need to add some module metadata
210 // to ensure intrinsic calls don't use it.
211 if !sess.needs_plt() {
212 let avoid_plt = "RtLibUseGOT\0".as_ptr() as *const _;
213 llvm::LLVMRustAddModuleFlag(llmod, avoid_plt, 1);
219 impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
220 crate fn new(tcx: TyCtxt<'ll, 'tcx, 'tcx>,
221 codegen_unit: Arc<CodegenUnit<'tcx>>,
222 llvm_module: &'ll ::ModuleLlvm)
224 // An interesting part of Windows which MSVC forces our hand on (and
225 // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
226 // attributes in LLVM IR as well as native dependencies (in C these
227 // correspond to `__declspec(dllimport)`).
229 // Whenever a dynamic library is built by MSVC it must have its public
230 // interface specified by functions tagged with `dllexport` or otherwise
231 // they're not available to be linked against. This poses a few problems
232 // for the compiler, some of which are somewhat fundamental, but we use
233 // the `use_dll_storage_attrs` variable below to attach the `dllexport`
234 // attribute to all LLVM functions that are exported e.g. they're
235 // already tagged with external linkage). This is suboptimal for a few
238 // * If an object file will never be included in a dynamic library,
239 // there's no need to attach the dllexport attribute. Most object
240 // files in Rust are not destined to become part of a dll as binaries
241 // are statically linked by default.
242 // * If the compiler is emitting both an rlib and a dylib, the same
243 // source object file is currently used but with MSVC this may be less
244 // feasible. The compiler may be able to get around this, but it may
245 // involve some invasive changes to deal with this.
247 // The flipside of this situation is that whenever you link to a dll and
248 // you import a function from it, the import should be tagged with
249 // `dllimport`. At this time, however, the compiler does not emit
250 // `dllimport` for any declarations other than constants (where it is
251 // required), which is again suboptimal for even more reasons!
253 // * Calling a function imported from another dll without using
254 // `dllimport` causes the linker/compiler to have extra overhead (one
255 // `jmp` instruction on x86) when calling the function.
256 // * The same object file may be used in different circumstances, so a
257 // function may be imported from a dll if the object is linked into a
258 // dll, but it may be just linked against if linked into an rlib.
259 // * The compiler has no knowledge about whether native functions should
260 // be tagged dllimport or not.
262 // For now the compiler takes the perf hit (I do not have any numbers to
263 // this effect) by marking very little as `dllimport` and praying the
264 // linker will take care of everything. Fixing this problem will likely
265 // require adding a few attributes to Rust itself (feature gated at the
266 // start) and then strongly recommending static linkage on MSVC!
267 let use_dll_storage_attrs = tcx.sess.target.target.options.is_like_msvc;
269 let check_overflow = tcx.sess.overflow_checks();
271 let tls_model = get_tls_model(&tcx.sess);
273 let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
275 let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
276 let dctx = debuginfo::CrateDebugContext::new(llmod);
277 debuginfo::metadata::compile_unit_metadata(tcx,
278 &codegen_unit.name().as_str(),
285 let isize_ty = Type::ix_llcx(llcx, tcx.data_layout.pointer_size.bits());
290 use_dll_storage_attrs,
294 stats: RefCell::new(Stats::default()),
296 instances: Default::default(),
297 vtables: Default::default(),
298 const_cstr_cache: Default::default(),
299 const_unsized: Default::default(),
300 const_globals: Default::default(),
301 statics_to_rauw: RefCell::new(Vec::new()),
302 used_statics: RefCell::new(Vec::new()),
303 lltypes: Default::default(),
304 scalar_lltypes: Default::default(),
305 pointee_infos: Default::default(),
308 eh_personality: Cell::new(None),
309 eh_unwind_resume: Cell::new(None),
310 rust_try_fn: Cell::new(None),
311 intrinsics: Default::default(),
312 local_gen_sym_counter: Cell::new(0),
317 impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
318 fn vtables(&self) -> &RefCell<FxHashMap<(Ty<'tcx>,
319 ty::PolyExistentialTraitRef<'tcx>), &'ll Value>>
324 fn instances(&self) -> &RefCell<FxHashMap<Instance<'tcx>, &'ll Value>> {
328 fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
329 callee::get_fn(&&self,instance)
332 fn get_param(&self, llfn: &'ll Value, index: c_uint) -> &'ll Value {
333 llvm::get_param(llfn, index)
336 fn eh_personality(&self) -> &'ll Value {
337 // The exception handling personality function.
339 // If our compilation unit has the `eh_personality` lang item somewhere
340 // within it, then we just need to codegen that. Otherwise, we're
341 // building an rlib which will depend on some upstream implementation of
342 // this function, so we just codegen a generic reference to it. We don't
343 // specify any of the types for the function, we just make it a symbol
344 // that LLVM can later use.
346 // Note that MSVC is a little special here in that we don't use the
347 // `eh_personality` lang item at all. Currently LLVM has support for
348 // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
349 // *name of the personality function* to decide what kind of unwind side
350 // tables/landing pads to emit. It looks like Dwarf is used by default,
351 // injecting a dependency on the `_Unwind_Resume` symbol for resuming
352 // an "exception", but for MSVC we want to force SEH. This means that we
353 // can't actually have the personality function be our standard
354 // `rust_eh_personality` function, but rather we wired it up to the
355 // CRT's custom personality function, which forces LLVM to consider
356 // landing pads as "landing pads for SEH".
357 if let Some(llpersonality) = self.eh_personality.get() {
361 let llfn = match tcx.lang_items().eh_personality() {
362 Some(def_id) if !base::wants_msvc_seh(self.sess()) => {
363 callee::resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]))
366 let name = if base::wants_msvc_seh(self.sess()) {
369 "rust_eh_personality"
371 let fty = self.type_variadic_func(&[], self.type_i32());
372 self.declare_cfn(name, fty)
375 attributes::apply_target_cpu_attr(self, llfn);
376 self.eh_personality.set(Some(llfn));
380 // Returns a Value of the "eh_unwind_resume" lang item if one is defined,
381 // otherwise declares it as an external function.
382 fn eh_unwind_resume(&self) -> &'ll Value {
384 let unwresume = &self.eh_unwind_resume;
385 if let Some(llfn) = unwresume.get() {
390 assert!(self.sess().target.target.options.custom_unwind_resume);
391 if let Some(def_id) = tcx.lang_items().eh_unwind_resume() {
392 let llfn = callee::resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]));
393 unwresume.set(Some(llfn));
397 let sig = ty::Binder::bind(tcx.mk_fn_sig(
398 iter::once(tcx.mk_mut_ptr(tcx.types.u8)),
401 hir::Unsafety::Unsafe,
405 let llfn = self.declare_fn("rust_eh_unwind_resume", sig);
406 attributes::unwind(llfn, true);
407 attributes::apply_target_cpu_attr(self, llfn);
408 unwresume.set(Some(llfn));
412 fn sess(&self) -> &Session {
416 fn check_overflow(&self) -> bool {
420 fn stats(&self) -> &RefCell<Stats> {
424 fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>> {
428 fn set_frame_pointer_elimination(&self, llfn: &'ll Value) {
429 attributes::set_frame_pointer_elimination(self, llfn)
432 fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
433 attributes::apply_target_cpu_attr(self, llfn)
437 impl IntrinsicDeclarationMethods<'tcx> for CodegenCx<'b, 'tcx> {
438 fn get_intrinsic(&self, key: &str) -> &'b Value {
439 if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
443 self.declare_intrinsic(key).unwrap_or_else(|| bug!("unknown intrinsic '{}'", key))
446 fn declare_intrinsic(
449 ) -> Option<&'b Value> {
451 ($name:expr, fn() -> $ret:expr) => (
453 let f = self.declare_cfn($name, self.type_func(&[], $ret));
454 llvm::SetUnnamedAddr(f, false);
455 self.intrinsics.borrow_mut().insert($name, f.clone());
459 ($name:expr, fn(...) -> $ret:expr) => (
461 let f = self.declare_cfn($name, self.type_variadic_func(&[], $ret));
462 llvm::SetUnnamedAddr(f, false);
463 self.intrinsics.borrow_mut().insert($name, f.clone());
467 ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
469 let f = self.declare_cfn($name, self.type_func(&[$($arg),*], $ret));
470 llvm::SetUnnamedAddr(f, false);
471 self.intrinsics.borrow_mut().insert($name, f.clone());
476 macro_rules! mk_struct {
477 ($($field_ty:expr),*) => (self.type_struct( &[$($field_ty),*], false))
480 let i8p = self.type_i8p();
481 let void = self.type_void();
482 let i1 = self.type_i1();
483 let t_i8 = self.type_i8();
484 let t_i16 = self.type_i16();
485 let t_i32 = self.type_i32();
486 let t_i64 = self.type_i64();
487 let t_i128 = self.type_i128();
488 let t_f32 = self.type_f32();
489 let t_f64 = self.type_f64();
491 let t_v2f32 = self.type_vector(t_f32, 2);
492 let t_v4f32 = self.type_vector(t_f32, 4);
493 let t_v8f32 = self.type_vector(t_f32, 8);
494 let t_v16f32 = self.type_vector(t_f32, 16);
496 let t_v2f64 = self.type_vector(t_f64, 2);
497 let t_v4f64 = self.type_vector(t_f64, 4);
498 let t_v8f64 = self.type_vector(t_f64, 8);
500 ifn!("llvm.memset.p0i8.i16", fn(i8p, t_i8, t_i16, t_i32, i1) -> void);
501 ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
502 ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
504 ifn!("llvm.trap", fn() -> void);
505 ifn!("llvm.debugtrap", fn() -> void);
506 ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
508 ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
509 ifn!("llvm.powi.v2f32", fn(t_v2f32, t_i32) -> t_v2f32);
510 ifn!("llvm.powi.v4f32", fn(t_v4f32, t_i32) -> t_v4f32);
511 ifn!("llvm.powi.v8f32", fn(t_v8f32, t_i32) -> t_v8f32);
512 ifn!("llvm.powi.v16f32", fn(t_v16f32, t_i32) -> t_v16f32);
513 ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
514 ifn!("llvm.powi.v2f64", fn(t_v2f64, t_i32) -> t_v2f64);
515 ifn!("llvm.powi.v4f64", fn(t_v4f64, t_i32) -> t_v4f64);
516 ifn!("llvm.powi.v8f64", fn(t_v8f64, t_i32) -> t_v8f64);
518 ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
519 ifn!("llvm.pow.v2f32", fn(t_v2f32, t_v2f32) -> t_v2f32);
520 ifn!("llvm.pow.v4f32", fn(t_v4f32, t_v4f32) -> t_v4f32);
521 ifn!("llvm.pow.v8f32", fn(t_v8f32, t_v8f32) -> t_v8f32);
522 ifn!("llvm.pow.v16f32", fn(t_v16f32, t_v16f32) -> t_v16f32);
523 ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
524 ifn!("llvm.pow.v2f64", fn(t_v2f64, t_v2f64) -> t_v2f64);
525 ifn!("llvm.pow.v4f64", fn(t_v4f64, t_v4f64) -> t_v4f64);
526 ifn!("llvm.pow.v8f64", fn(t_v8f64, t_v8f64) -> t_v8f64);
528 ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
529 ifn!("llvm.sqrt.v2f32", fn(t_v2f32) -> t_v2f32);
530 ifn!("llvm.sqrt.v4f32", fn(t_v4f32) -> t_v4f32);
531 ifn!("llvm.sqrt.v8f32", fn(t_v8f32) -> t_v8f32);
532 ifn!("llvm.sqrt.v16f32", fn(t_v16f32) -> t_v16f32);
533 ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
534 ifn!("llvm.sqrt.v2f64", fn(t_v2f64) -> t_v2f64);
535 ifn!("llvm.sqrt.v4f64", fn(t_v4f64) -> t_v4f64);
536 ifn!("llvm.sqrt.v8f64", fn(t_v8f64) -> t_v8f64);
538 ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
539 ifn!("llvm.sin.v2f32", fn(t_v2f32) -> t_v2f32);
540 ifn!("llvm.sin.v4f32", fn(t_v4f32) -> t_v4f32);
541 ifn!("llvm.sin.v8f32", fn(t_v8f32) -> t_v8f32);
542 ifn!("llvm.sin.v16f32", fn(t_v16f32) -> t_v16f32);
543 ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
544 ifn!("llvm.sin.v2f64", fn(t_v2f64) -> t_v2f64);
545 ifn!("llvm.sin.v4f64", fn(t_v4f64) -> t_v4f64);
546 ifn!("llvm.sin.v8f64", fn(t_v8f64) -> t_v8f64);
548 ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
549 ifn!("llvm.cos.v2f32", fn(t_v2f32) -> t_v2f32);
550 ifn!("llvm.cos.v4f32", fn(t_v4f32) -> t_v4f32);
551 ifn!("llvm.cos.v8f32", fn(t_v8f32) -> t_v8f32);
552 ifn!("llvm.cos.v16f32", fn(t_v16f32) -> t_v16f32);
553 ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
554 ifn!("llvm.cos.v2f64", fn(t_v2f64) -> t_v2f64);
555 ifn!("llvm.cos.v4f64", fn(t_v4f64) -> t_v4f64);
556 ifn!("llvm.cos.v8f64", fn(t_v8f64) -> t_v8f64);
558 ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
559 ifn!("llvm.exp.v2f32", fn(t_v2f32) -> t_v2f32);
560 ifn!("llvm.exp.v4f32", fn(t_v4f32) -> t_v4f32);
561 ifn!("llvm.exp.v8f32", fn(t_v8f32) -> t_v8f32);
562 ifn!("llvm.exp.v16f32", fn(t_v16f32) -> t_v16f32);
563 ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
564 ifn!("llvm.exp.v2f64", fn(t_v2f64) -> t_v2f64);
565 ifn!("llvm.exp.v4f64", fn(t_v4f64) -> t_v4f64);
566 ifn!("llvm.exp.v8f64", fn(t_v8f64) -> t_v8f64);
568 ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
569 ifn!("llvm.exp2.v2f32", fn(t_v2f32) -> t_v2f32);
570 ifn!("llvm.exp2.v4f32", fn(t_v4f32) -> t_v4f32);
571 ifn!("llvm.exp2.v8f32", fn(t_v8f32) -> t_v8f32);
572 ifn!("llvm.exp2.v16f32", fn(t_v16f32) -> t_v16f32);
573 ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
574 ifn!("llvm.exp2.v2f64", fn(t_v2f64) -> t_v2f64);
575 ifn!("llvm.exp2.v4f64", fn(t_v4f64) -> t_v4f64);
576 ifn!("llvm.exp2.v8f64", fn(t_v8f64) -> t_v8f64);
578 ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
579 ifn!("llvm.log.v2f32", fn(t_v2f32) -> t_v2f32);
580 ifn!("llvm.log.v4f32", fn(t_v4f32) -> t_v4f32);
581 ifn!("llvm.log.v8f32", fn(t_v8f32) -> t_v8f32);
582 ifn!("llvm.log.v16f32", fn(t_v16f32) -> t_v16f32);
583 ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
584 ifn!("llvm.log.v2f64", fn(t_v2f64) -> t_v2f64);
585 ifn!("llvm.log.v4f64", fn(t_v4f64) -> t_v4f64);
586 ifn!("llvm.log.v8f64", fn(t_v8f64) -> t_v8f64);
588 ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
589 ifn!("llvm.log10.v2f32", fn(t_v2f32) -> t_v2f32);
590 ifn!("llvm.log10.v4f32", fn(t_v4f32) -> t_v4f32);
591 ifn!("llvm.log10.v8f32", fn(t_v8f32) -> t_v8f32);
592 ifn!("llvm.log10.v16f32", fn(t_v16f32) -> t_v16f32);
593 ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
594 ifn!("llvm.log10.v2f64", fn(t_v2f64) -> t_v2f64);
595 ifn!("llvm.log10.v4f64", fn(t_v4f64) -> t_v4f64);
596 ifn!("llvm.log10.v8f64", fn(t_v8f64) -> t_v8f64);
598 ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
599 ifn!("llvm.log2.v2f32", fn(t_v2f32) -> t_v2f32);
600 ifn!("llvm.log2.v4f32", fn(t_v4f32) -> t_v4f32);
601 ifn!("llvm.log2.v8f32", fn(t_v8f32) -> t_v8f32);
602 ifn!("llvm.log2.v16f32", fn(t_v16f32) -> t_v16f32);
603 ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
604 ifn!("llvm.log2.v2f64", fn(t_v2f64) -> t_v2f64);
605 ifn!("llvm.log2.v4f64", fn(t_v4f64) -> t_v4f64);
606 ifn!("llvm.log2.v8f64", fn(t_v8f64) -> t_v8f64);
608 ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
609 ifn!("llvm.fma.v2f32", fn(t_v2f32, t_v2f32, t_v2f32) -> t_v2f32);
610 ifn!("llvm.fma.v4f32", fn(t_v4f32, t_v4f32, t_v4f32) -> t_v4f32);
611 ifn!("llvm.fma.v8f32", fn(t_v8f32, t_v8f32, t_v8f32) -> t_v8f32);
612 ifn!("llvm.fma.v16f32", fn(t_v16f32, t_v16f32, t_v16f32) -> t_v16f32);
613 ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
614 ifn!("llvm.fma.v2f64", fn(t_v2f64, t_v2f64, t_v2f64) -> t_v2f64);
615 ifn!("llvm.fma.v4f64", fn(t_v4f64, t_v4f64, t_v4f64) -> t_v4f64);
616 ifn!("llvm.fma.v8f64", fn(t_v8f64, t_v8f64, t_v8f64) -> t_v8f64);
618 ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
619 ifn!("llvm.fabs.v2f32", fn(t_v2f32) -> t_v2f32);
620 ifn!("llvm.fabs.v4f32", fn(t_v4f32) -> t_v4f32);
621 ifn!("llvm.fabs.v8f32", fn(t_v8f32) -> t_v8f32);
622 ifn!("llvm.fabs.v16f32", fn(t_v16f32) -> t_v16f32);
623 ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
624 ifn!("llvm.fabs.v2f64", fn(t_v2f64) -> t_v2f64);
625 ifn!("llvm.fabs.v4f64", fn(t_v4f64) -> t_v4f64);
626 ifn!("llvm.fabs.v8f64", fn(t_v8f64) -> t_v8f64);
628 ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
629 ifn!("llvm.floor.v2f32", fn(t_v2f32) -> t_v2f32);
630 ifn!("llvm.floor.v4f32", fn(t_v4f32) -> t_v4f32);
631 ifn!("llvm.floor.v8f32", fn(t_v8f32) -> t_v8f32);
632 ifn!("llvm.floor.v16f32", fn(t_v16f32) -> t_v16f32);
633 ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
634 ifn!("llvm.floor.v2f64", fn(t_v2f64) -> t_v2f64);
635 ifn!("llvm.floor.v4f64", fn(t_v4f64) -> t_v4f64);
636 ifn!("llvm.floor.v8f64", fn(t_v8f64) -> t_v8f64);
638 ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
639 ifn!("llvm.ceil.v2f32", fn(t_v2f32) -> t_v2f32);
640 ifn!("llvm.ceil.v4f32", fn(t_v4f32) -> t_v4f32);
641 ifn!("llvm.ceil.v8f32", fn(t_v8f32) -> t_v8f32);
642 ifn!("llvm.ceil.v16f32", fn(t_v16f32) -> t_v16f32);
643 ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
644 ifn!("llvm.ceil.v2f64", fn(t_v2f64) -> t_v2f64);
645 ifn!("llvm.ceil.v4f64", fn(t_v4f64) -> t_v4f64);
646 ifn!("llvm.ceil.v8f64", fn(t_v8f64) -> t_v8f64);
648 ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
649 ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
651 ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
652 ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
653 ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
654 ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
656 ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
657 ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
658 ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
659 ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
661 ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
662 ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
663 ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
664 ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
665 ifn!("llvm.ctpop.i128", fn(t_i128) -> t_i128);
667 ifn!("llvm.ctlz.i8", fn(t_i8 , i1) -> t_i8);
668 ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
669 ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
670 ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
671 ifn!("llvm.ctlz.i128", fn(t_i128, i1) -> t_i128);
673 ifn!("llvm.cttz.i8", fn(t_i8 , i1) -> t_i8);
674 ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
675 ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
676 ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
677 ifn!("llvm.cttz.i128", fn(t_i128, i1) -> t_i128);
679 ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
680 ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
681 ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
682 ifn!("llvm.bswap.i128", fn(t_i128) -> t_i128);
684 ifn!("llvm.bitreverse.i8", fn(t_i8) -> t_i8);
685 ifn!("llvm.bitreverse.i16", fn(t_i16) -> t_i16);
686 ifn!("llvm.bitreverse.i32", fn(t_i32) -> t_i32);
687 ifn!("llvm.bitreverse.i64", fn(t_i64) -> t_i64);
688 ifn!("llvm.bitreverse.i128", fn(t_i128) -> t_i128);
690 ifn!("llvm.fshl.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
691 ifn!("llvm.fshl.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
692 ifn!("llvm.fshl.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
693 ifn!("llvm.fshl.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
694 ifn!("llvm.fshl.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
696 ifn!("llvm.fshr.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
697 ifn!("llvm.fshr.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
698 ifn!("llvm.fshr.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
699 ifn!("llvm.fshr.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
700 ifn!("llvm.fshr.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
702 ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
703 ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
704 ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
705 ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
706 ifn!("llvm.sadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
708 ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
709 ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
710 ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
711 ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
712 ifn!("llvm.uadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
714 ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
715 ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
716 ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
717 ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
718 ifn!("llvm.ssub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
720 ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
721 ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
722 ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
723 ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
724 ifn!("llvm.usub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
726 ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
727 ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
728 ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
729 ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
730 ifn!("llvm.smul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
732 ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
733 ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
734 ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
735 ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
736 ifn!("llvm.umul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct!{t_i128, i1});
738 ifn!("llvm.lifetime.start", fn(t_i64,i8p) -> void);
739 ifn!("llvm.lifetime.end", fn(t_i64, i8p) -> void);
741 ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
742 ifn!("llvm.eh.typeid.for", fn(i8p) -> t_i32);
743 ifn!("llvm.localescape", fn(...) -> void);
744 ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
745 ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);
747 ifn!("llvm.assume", fn(i1) -> void);
748 ifn!("llvm.prefetch", fn(i8p, t_i32, t_i32, t_i32) -> void);
750 if self.sess().opts.debuginfo != DebugInfo::None {
751 ifn!("llvm.dbg.declare", fn(self.type_metadata(), self.type_metadata()) -> void);
752 ifn!("llvm.dbg.value", fn(self.type_metadata(), t_i64, self.type_metadata()) -> void);
758 impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
759 /// Generate a new symbol name with the given prefix. This symbol name must
760 /// only be used for definitions with `internal` or `private` linkage.
761 pub fn generate_local_symbol_name(&self, prefix: &str) -> String {
762 let idx = self.local_gen_sym_counter.get();
763 self.local_gen_sym_counter.set(idx + 1);
764 // Include a '.' character, so there can be no accidental conflicts with
765 // user defined names
766 let mut name = String::with_capacity(prefix.len() + 6);
767 name.push_str(prefix);
769 base_n::push_str(idx as u128, base_n::ALPHANUMERIC_ONLY, &mut name);
774 impl ty::layout::HasDataLayout for CodegenCx<'ll, 'tcx> {
775 fn data_layout(&self) -> &ty::layout::TargetDataLayout {
776 &self.tcx.data_layout
780 impl HasTargetSpec for CodegenCx<'ll, 'tcx> {
781 fn target_spec(&self) -> &Target {
782 &self.tcx.sess.target.target
786 impl ty::layout::HasTyCtxt<'tcx> for CodegenCx<'ll, 'tcx> {
787 fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
792 impl LayoutOf for CodegenCx<'ll, 'tcx> {
794 type TyLayout = TyLayout<'tcx>;
796 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
797 self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty))
798 .unwrap_or_else(|e| if let LayoutError::SizeOverflow(_) = e {
799 self.sess().fatal(&e.to_string())
801 bug!("failed to get layout for `{}`: {}", ty, e)