2 use crate::callee::get_fn;
6 use crate::type_::Type;
7 use crate::value::Value;
9 use rustc_codegen_ssa::base::wants_msvc_seh;
10 use rustc_codegen_ssa::traits::*;
11 use rustc_data_structures::base_n;
12 use rustc_data_structures::const_cstr;
13 use rustc_data_structures::fx::FxHashMap;
14 use rustc_data_structures::small_c_str::SmallCStr;
15 use rustc_middle::bug;
16 use rustc_middle::mir::mono::CodegenUnit;
17 use rustc_middle::ty::layout::{HasParamEnv, LayoutError, TyAndLayout};
18 use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
19 use rustc_session::config::{CFGuard, CrateType, DebugInfo};
20 use rustc_session::Session;
21 use rustc_span::source_map::{Span, DUMMY_SP};
22 use rustc_span::symbol::Symbol;
23 use rustc_target::abi::{HasDataLayout, LayoutOf, PointeeInfo, Size, TargetDataLayout, VariantIdx};
24 use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
26 use std::cell::{Cell, RefCell};
30 /// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
31 /// `llvm::Context` so that several compilation units may be optimized in parallel.
32 /// All other LLVM data structures in the `CodegenCx` are tied to that `llvm::Context`.
33 pub struct CodegenCx<'ll, 'tcx> {
34 pub tcx: TyCtxt<'tcx>,
35 pub check_overflow: bool,
36 pub use_dll_storage_attrs: bool,
37 pub tls_model: llvm::ThreadLocalMode,
39 pub llmod: &'ll llvm::Module,
40 pub llcx: &'ll llvm::Context,
41 pub codegen_unit: &'tcx CodegenUnit<'tcx>,
43 /// Cache instances of monomorphic and polymorphic items
44 pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
45 /// Cache generated vtables
47 RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>,
48 /// Cache of constant strings,
49 pub const_cstr_cache: RefCell<FxHashMap<Symbol, &'ll Value>>,
51 /// Reverse-direction for const ptrs cast from globals.
53 /// Key is a Value holding a `*T`,
54 /// Val is a Value holding a `*[T]`.
56 /// Needed because LLVM loses pointer->pointee association
57 /// when we ptrcast, and we have to ptrcast during codegen
58 /// of a `[T]` const because we form a slice, a `(*T,usize)` pair, not
59 /// a pointer to an LLVM array type. Similar for trait objects.
60 pub const_unsized: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
62 /// Cache of emitted const globals (value -> global)
63 pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
65 /// List of globals for static variables which need to be passed to the
66 /// LLVM function ReplaceAllUsesWith (RAUW) when codegen is complete.
67 /// (We have to make sure we don't invalidate any Values referring
69 pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
71 /// Statics that will be placed in the llvm.used variable
72 /// See <http://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
73 pub used_statics: RefCell<Vec<&'ll Value>>,
75 pub lltypes: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
76 pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
77 pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
78 pub isize_ty: &'ll Type,
80 pub dbg_cx: Option<debuginfo::CrateDebugContext<'ll, 'tcx>>,
82 eh_personality: Cell<Option<&'ll Value>>,
83 pub rust_try_fn: Cell<Option<&'ll Value>>,
85 intrinsics: RefCell<FxHashMap<&'static str, &'ll Value>>,
87 /// A counter that is used for generating local symbol names
88 local_gen_sym_counter: Cell<usize>,
91 fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
93 TlsModel::GeneralDynamic => llvm::ThreadLocalMode::GeneralDynamic,
94 TlsModel::LocalDynamic => llvm::ThreadLocalMode::LocalDynamic,
95 TlsModel::InitialExec => llvm::ThreadLocalMode::InitialExec,
96 TlsModel::LocalExec => llvm::ThreadLocalMode::LocalExec,
100 fn strip_function_ptr_alignment(data_layout: String) -> String {
101 // FIXME: Make this more general.
102 data_layout.replace("-Fi8-", "-")
105 fn strip_x86_address_spaces(data_layout: String) -> String {
106 data_layout.replace("-p270:32:32-p271:32:32-p272:64:64-", "-")
109 pub unsafe fn create_module(
111 llcx: &'ll llvm::Context,
113 ) -> &'ll llvm::Module {
115 let mod_name = SmallCStr::new(mod_name);
116 let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
118 let mut target_data_layout = sess.target.target.data_layout.clone();
119 if llvm_util::get_major_version() < 9 {
120 target_data_layout = strip_function_ptr_alignment(target_data_layout);
122 if llvm_util::get_major_version() < 10 {
123 if sess.target.target.arch == "x86" || sess.target.target.arch == "x86_64" {
124 target_data_layout = strip_x86_address_spaces(target_data_layout);
128 // Ensure the data-layout values hardcoded remain the defaults.
129 if sess.target.target.options.is_builtin {
130 let tm = crate::back::write::create_informational_target_machine(tcx.sess);
131 llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
132 llvm::LLVMRustDisposeTargetMachine(tm);
134 let llvm_data_layout = llvm::LLVMGetDataLayoutStr(llmod);
135 let llvm_data_layout = str::from_utf8(CStr::from_ptr(llvm_data_layout).to_bytes())
136 .expect("got a non-UTF8 data-layout from LLVM");
138 // Unfortunately LLVM target specs change over time, and right now we
139 // don't have proper support to work with any more than one
140 // `data_layout` than the one that is in the rust-lang/rust repo. If
141 // this compiler is configured against a custom LLVM, we may have a
142 // differing data layout, even though we should update our own to use
145 // As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
146 // disable this check entirely as we may be configured with something
147 // that has a different target layout.
149 // Unsure if this will actually cause breakage when rustc is configured
153 let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
154 let custom_llvm_used = cfg_llvm_root.trim() != "";
156 if !custom_llvm_used && target_data_layout != llvm_data_layout {
158 "data-layout for builtin `{}` target, `{}`, \
159 differs from LLVM default, `{}`",
160 sess.target.target.llvm_target,
167 let data_layout = SmallCStr::new(&target_data_layout);
168 llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
170 let llvm_target = SmallCStr::new(&sess.target.target.llvm_target);
171 llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
173 if sess.relocation_model() == RelocModel::Pic {
174 llvm::LLVMRustSetModulePICLevel(llmod);
175 // PIE is potentially more effective than PIC, but can only be used in executables.
176 // If all our outputs are executables, then we can relax PIC to PIE.
177 if sess.crate_types().iter().all(|ty| *ty == CrateType::Executable) {
178 llvm::LLVMRustSetModulePIELevel(llmod);
182 // If skipping the PLT is enabled, we need to add some module metadata
183 // to ensure intrinsic calls don't use it.
184 if !sess.needs_plt() {
185 let avoid_plt = "RtLibUseGOT\0".as_ptr().cast();
186 llvm::LLVMRustAddModuleFlag(llmod, avoid_plt, 1);
189 // Set module flags to enable Windows Control Flow Guard (/guard:cf) metadata
190 // only (`cfguard=1`) or metadata and checks (`cfguard=2`).
191 match sess.opts.debugging_opts.control_flow_guard {
192 CFGuard::Disabled => {}
193 CFGuard::NoChecks => {
194 llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 1)
196 CFGuard::Checks => llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 2),
202 impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
205 codegen_unit: &'tcx CodegenUnit<'tcx>,
206 llvm_module: &'ll crate::ModuleLlvm,
208 // An interesting part of Windows which MSVC forces our hand on (and
209 // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
210 // attributes in LLVM IR as well as native dependencies (in C these
211 // correspond to `__declspec(dllimport)`).
213 // Whenever a dynamic library is built by MSVC it must have its public
214 // interface specified by functions tagged with `dllexport` or otherwise
215 // they're not available to be linked against. This poses a few problems
216 // for the compiler, some of which are somewhat fundamental, but we use
217 // the `use_dll_storage_attrs` variable below to attach the `dllexport`
218 // attribute to all LLVM functions that are exported e.g., they're
219 // already tagged with external linkage). This is suboptimal for a few
222 // * If an object file will never be included in a dynamic library,
223 // there's no need to attach the dllexport attribute. Most object
224 // files in Rust are not destined to become part of a dll as binaries
225 // are statically linked by default.
226 // * If the compiler is emitting both an rlib and a dylib, the same
227 // source object file is currently used but with MSVC this may be less
228 // feasible. The compiler may be able to get around this, but it may
229 // involve some invasive changes to deal with this.
231 // The flipside of this situation is that whenever you link to a dll and
232 // you import a function from it, the import should be tagged with
233 // `dllimport`. At this time, however, the compiler does not emit
234 // `dllimport` for any declarations other than constants (where it is
235 // required), which is again suboptimal for even more reasons!
237 // * Calling a function imported from another dll without using
238 // `dllimport` causes the linker/compiler to have extra overhead (one
239 // `jmp` instruction on x86) when calling the function.
240 // * The same object file may be used in different circumstances, so a
241 // function may be imported from a dll if the object is linked into a
242 // dll, but it may be just linked against if linked into an rlib.
243 // * The compiler has no knowledge about whether native functions should
244 // be tagged dllimport or not.
246 // For now the compiler takes the perf hit (I do not have any numbers to
247 // this effect) by marking very little as `dllimport` and praying the
248 // linker will take care of everything. Fixing this problem will likely
249 // require adding a few attributes to Rust itself (feature gated at the
250 // start) and then strongly recommending static linkage on MSVC!
251 let use_dll_storage_attrs = tcx.sess.target.target.options.is_like_msvc;
253 let check_overflow = tcx.sess.overflow_checks();
255 let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
257 let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
259 let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
260 let dctx = debuginfo::CrateDebugContext::new(llmod);
261 debuginfo::metadata::compile_unit_metadata(tcx, &codegen_unit.name().as_str(), &dctx);
267 let isize_ty = Type::ix_llcx(llcx, tcx.data_layout.pointer_size.bits());
272 use_dll_storage_attrs,
277 instances: Default::default(),
278 vtables: Default::default(),
279 const_cstr_cache: Default::default(),
280 const_unsized: Default::default(),
281 const_globals: Default::default(),
282 statics_to_rauw: RefCell::new(Vec::new()),
283 used_statics: RefCell::new(Vec::new()),
284 lltypes: Default::default(),
285 scalar_lltypes: Default::default(),
286 pointee_infos: Default::default(),
289 eh_personality: Cell::new(None),
290 rust_try_fn: Cell::new(None),
291 intrinsics: Default::default(),
292 local_gen_sym_counter: Cell::new(0),
296 crate fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
297 &self.statics_to_rauw
301 impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
304 ) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>
309 fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
310 get_fn(self, instance)
313 fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
314 get_fn(self, instance)
317 fn eh_personality(&self) -> &'ll Value {
318 // The exception handling personality function.
320 // If our compilation unit has the `eh_personality` lang item somewhere
321 // within it, then we just need to codegen that. Otherwise, we're
322 // building an rlib which will depend on some upstream implementation of
323 // this function, so we just codegen a generic reference to it. We don't
324 // specify any of the types for the function, we just make it a symbol
325 // that LLVM can later use.
327 // Note that MSVC is a little special here in that we don't use the
328 // `eh_personality` lang item at all. Currently LLVM has support for
329 // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
330 // *name of the personality function* to decide what kind of unwind side
331 // tables/landing pads to emit. It looks like Dwarf is used by default,
332 // injecting a dependency on the `_Unwind_Resume` symbol for resuming
333 // an "exception", but for MSVC we want to force SEH. This means that we
334 // can't actually have the personality function be our standard
335 // `rust_eh_personality` function, but rather we wired it up to the
336 // CRT's custom personality function, which forces LLVM to consider
337 // landing pads as "landing pads for SEH".
338 if let Some(llpersonality) = self.eh_personality.get() {
339 return llpersonality;
342 let llfn = match tcx.lang_items().eh_personality() {
343 Some(def_id) if !wants_msvc_seh(self.sess()) => self.get_fn_addr(
344 ty::Instance::resolve(
346 ty::ParamEnv::reveal_all(),
348 tcx.intern_substs(&[]),
354 let name = if wants_msvc_seh(self.sess()) {
357 "rust_eh_personality"
359 let fty = self.type_variadic_func(&[], self.type_i32());
360 self.declare_cfn(name, fty)
363 attributes::apply_target_cpu_attr(self, llfn);
364 self.eh_personality.set(Some(llfn));
368 fn sess(&self) -> &Session {
372 fn check_overflow(&self) -> bool {
376 fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
380 fn used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
384 fn set_frame_pointer_elimination(&self, llfn: &'ll Value) {
385 attributes::set_frame_pointer_elimination(self, llfn)
388 fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
389 attributes::apply_target_cpu_attr(self, llfn)
392 fn create_used_variable(&self) {
393 let name = const_cstr!("llvm.used");
394 let section = const_cstr!("llvm.metadata");
396 self.const_array(&self.type_ptr_to(self.type_i8()), &*self.used_statics.borrow());
399 let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
400 llvm::LLVMSetInitializer(g, array);
401 llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
402 llvm::LLVMSetSection(g, section.as_ptr());
407 impl CodegenCx<'b, 'tcx> {
408 crate fn get_intrinsic(&self, key: &str) -> &'b Value {
409 if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
413 self.declare_intrinsic(key).unwrap_or_else(|| bug!("unknown intrinsic '{}'", key))
419 args: Option<&[&'b llvm::Type]>,
421 ) -> &'b llvm::Value {
422 let fn_ty = if let Some(args) = args {
423 self.type_func(args, ret)
425 self.type_variadic_func(&[], ret)
427 let f = self.declare_cfn(name, fn_ty);
428 llvm::SetUnnamedAddress(f, llvm::UnnamedAddr::No);
429 self.intrinsics.borrow_mut().insert(name, f);
433 fn declare_intrinsic(&self, key: &str) -> Option<&'b Value> {
435 ($name:expr, fn() -> $ret:expr) => (
437 return Some(self.insert_intrinsic($name, Some(&[]), $ret));
440 ($name:expr, fn(...) -> $ret:expr) => (
442 return Some(self.insert_intrinsic($name, None, $ret));
445 ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
447 return Some(self.insert_intrinsic($name, Some(&[$($arg),*]), $ret));
451 macro_rules! mk_struct {
452 ($($field_ty:expr),*) => (self.type_struct( &[$($field_ty),*], false))
455 let i8p = self.type_i8p();
456 let void = self.type_void();
457 let i1 = self.type_i1();
458 let t_i8 = self.type_i8();
459 let t_i16 = self.type_i16();
460 let t_i32 = self.type_i32();
461 let t_i64 = self.type_i64();
462 let t_i128 = self.type_i128();
463 let t_f32 = self.type_f32();
464 let t_f64 = self.type_f64();
466 macro_rules! vector_types {
467 ($id_out:ident: $elem_ty:ident, $len:expr) => {
468 let $id_out = self.type_vector($elem_ty, $len);
470 ($($id_out:ident: $elem_ty:ident, $len:expr;)*) => {
471 $(vector_types!($id_out: $elem_ty, $len);)*
485 ifn!("llvm.trap", fn() -> void);
486 ifn!("llvm.debugtrap", fn() -> void);
487 ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
488 ifn!("llvm.sideeffect", fn() -> void);
490 ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
491 ifn!("llvm.powi.v2f32", fn(t_v2f32, t_i32) -> t_v2f32);
492 ifn!("llvm.powi.v4f32", fn(t_v4f32, t_i32) -> t_v4f32);
493 ifn!("llvm.powi.v8f32", fn(t_v8f32, t_i32) -> t_v8f32);
494 ifn!("llvm.powi.v16f32", fn(t_v16f32, t_i32) -> t_v16f32);
495 ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
496 ifn!("llvm.powi.v2f64", fn(t_v2f64, t_i32) -> t_v2f64);
497 ifn!("llvm.powi.v4f64", fn(t_v4f64, t_i32) -> t_v4f64);
498 ifn!("llvm.powi.v8f64", fn(t_v8f64, t_i32) -> t_v8f64);
500 ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
501 ifn!("llvm.pow.v2f32", fn(t_v2f32, t_v2f32) -> t_v2f32);
502 ifn!("llvm.pow.v4f32", fn(t_v4f32, t_v4f32) -> t_v4f32);
503 ifn!("llvm.pow.v8f32", fn(t_v8f32, t_v8f32) -> t_v8f32);
504 ifn!("llvm.pow.v16f32", fn(t_v16f32, t_v16f32) -> t_v16f32);
505 ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
506 ifn!("llvm.pow.v2f64", fn(t_v2f64, t_v2f64) -> t_v2f64);
507 ifn!("llvm.pow.v4f64", fn(t_v4f64, t_v4f64) -> t_v4f64);
508 ifn!("llvm.pow.v8f64", fn(t_v8f64, t_v8f64) -> t_v8f64);
510 ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
511 ifn!("llvm.sqrt.v2f32", fn(t_v2f32) -> t_v2f32);
512 ifn!("llvm.sqrt.v4f32", fn(t_v4f32) -> t_v4f32);
513 ifn!("llvm.sqrt.v8f32", fn(t_v8f32) -> t_v8f32);
514 ifn!("llvm.sqrt.v16f32", fn(t_v16f32) -> t_v16f32);
515 ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
516 ifn!("llvm.sqrt.v2f64", fn(t_v2f64) -> t_v2f64);
517 ifn!("llvm.sqrt.v4f64", fn(t_v4f64) -> t_v4f64);
518 ifn!("llvm.sqrt.v8f64", fn(t_v8f64) -> t_v8f64);
520 ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
521 ifn!("llvm.sin.v2f32", fn(t_v2f32) -> t_v2f32);
522 ifn!("llvm.sin.v4f32", fn(t_v4f32) -> t_v4f32);
523 ifn!("llvm.sin.v8f32", fn(t_v8f32) -> t_v8f32);
524 ifn!("llvm.sin.v16f32", fn(t_v16f32) -> t_v16f32);
525 ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
526 ifn!("llvm.sin.v2f64", fn(t_v2f64) -> t_v2f64);
527 ifn!("llvm.sin.v4f64", fn(t_v4f64) -> t_v4f64);
528 ifn!("llvm.sin.v8f64", fn(t_v8f64) -> t_v8f64);
530 ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
531 ifn!("llvm.cos.v2f32", fn(t_v2f32) -> t_v2f32);
532 ifn!("llvm.cos.v4f32", fn(t_v4f32) -> t_v4f32);
533 ifn!("llvm.cos.v8f32", fn(t_v8f32) -> t_v8f32);
534 ifn!("llvm.cos.v16f32", fn(t_v16f32) -> t_v16f32);
535 ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
536 ifn!("llvm.cos.v2f64", fn(t_v2f64) -> t_v2f64);
537 ifn!("llvm.cos.v4f64", fn(t_v4f64) -> t_v4f64);
538 ifn!("llvm.cos.v8f64", fn(t_v8f64) -> t_v8f64);
540 ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
541 ifn!("llvm.exp.v2f32", fn(t_v2f32) -> t_v2f32);
542 ifn!("llvm.exp.v4f32", fn(t_v4f32) -> t_v4f32);
543 ifn!("llvm.exp.v8f32", fn(t_v8f32) -> t_v8f32);
544 ifn!("llvm.exp.v16f32", fn(t_v16f32) -> t_v16f32);
545 ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
546 ifn!("llvm.exp.v2f64", fn(t_v2f64) -> t_v2f64);
547 ifn!("llvm.exp.v4f64", fn(t_v4f64) -> t_v4f64);
548 ifn!("llvm.exp.v8f64", fn(t_v8f64) -> t_v8f64);
550 ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
551 ifn!("llvm.exp2.v2f32", fn(t_v2f32) -> t_v2f32);
552 ifn!("llvm.exp2.v4f32", fn(t_v4f32) -> t_v4f32);
553 ifn!("llvm.exp2.v8f32", fn(t_v8f32) -> t_v8f32);
554 ifn!("llvm.exp2.v16f32", fn(t_v16f32) -> t_v16f32);
555 ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
556 ifn!("llvm.exp2.v2f64", fn(t_v2f64) -> t_v2f64);
557 ifn!("llvm.exp2.v4f64", fn(t_v4f64) -> t_v4f64);
558 ifn!("llvm.exp2.v8f64", fn(t_v8f64) -> t_v8f64);
560 ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
561 ifn!("llvm.log.v2f32", fn(t_v2f32) -> t_v2f32);
562 ifn!("llvm.log.v4f32", fn(t_v4f32) -> t_v4f32);
563 ifn!("llvm.log.v8f32", fn(t_v8f32) -> t_v8f32);
564 ifn!("llvm.log.v16f32", fn(t_v16f32) -> t_v16f32);
565 ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
566 ifn!("llvm.log.v2f64", fn(t_v2f64) -> t_v2f64);
567 ifn!("llvm.log.v4f64", fn(t_v4f64) -> t_v4f64);
568 ifn!("llvm.log.v8f64", fn(t_v8f64) -> t_v8f64);
570 ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
571 ifn!("llvm.log10.v2f32", fn(t_v2f32) -> t_v2f32);
572 ifn!("llvm.log10.v4f32", fn(t_v4f32) -> t_v4f32);
573 ifn!("llvm.log10.v8f32", fn(t_v8f32) -> t_v8f32);
574 ifn!("llvm.log10.v16f32", fn(t_v16f32) -> t_v16f32);
575 ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
576 ifn!("llvm.log10.v2f64", fn(t_v2f64) -> t_v2f64);
577 ifn!("llvm.log10.v4f64", fn(t_v4f64) -> t_v4f64);
578 ifn!("llvm.log10.v8f64", fn(t_v8f64) -> t_v8f64);
580 ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
581 ifn!("llvm.log2.v2f32", fn(t_v2f32) -> t_v2f32);
582 ifn!("llvm.log2.v4f32", fn(t_v4f32) -> t_v4f32);
583 ifn!("llvm.log2.v8f32", fn(t_v8f32) -> t_v8f32);
584 ifn!("llvm.log2.v16f32", fn(t_v16f32) -> t_v16f32);
585 ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
586 ifn!("llvm.log2.v2f64", fn(t_v2f64) -> t_v2f64);
587 ifn!("llvm.log2.v4f64", fn(t_v4f64) -> t_v4f64);
588 ifn!("llvm.log2.v8f64", fn(t_v8f64) -> t_v8f64);
590 ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
591 ifn!("llvm.fma.v2f32", fn(t_v2f32, t_v2f32, t_v2f32) -> t_v2f32);
592 ifn!("llvm.fma.v4f32", fn(t_v4f32, t_v4f32, t_v4f32) -> t_v4f32);
593 ifn!("llvm.fma.v8f32", fn(t_v8f32, t_v8f32, t_v8f32) -> t_v8f32);
594 ifn!("llvm.fma.v16f32", fn(t_v16f32, t_v16f32, t_v16f32) -> t_v16f32);
595 ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
596 ifn!("llvm.fma.v2f64", fn(t_v2f64, t_v2f64, t_v2f64) -> t_v2f64);
597 ifn!("llvm.fma.v4f64", fn(t_v4f64, t_v4f64, t_v4f64) -> t_v4f64);
598 ifn!("llvm.fma.v8f64", fn(t_v8f64, t_v8f64, t_v8f64) -> t_v8f64);
600 ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
601 ifn!("llvm.fabs.v2f32", fn(t_v2f32) -> t_v2f32);
602 ifn!("llvm.fabs.v4f32", fn(t_v4f32) -> t_v4f32);
603 ifn!("llvm.fabs.v8f32", fn(t_v8f32) -> t_v8f32);
604 ifn!("llvm.fabs.v16f32", fn(t_v16f32) -> t_v16f32);
605 ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
606 ifn!("llvm.fabs.v2f64", fn(t_v2f64) -> t_v2f64);
607 ifn!("llvm.fabs.v4f64", fn(t_v4f64) -> t_v4f64);
608 ifn!("llvm.fabs.v8f64", fn(t_v8f64) -> t_v8f64);
610 ifn!("llvm.minnum.f32", fn(t_f32, t_f32) -> t_f32);
611 ifn!("llvm.minnum.f64", fn(t_f64, t_f64) -> t_f64);
612 ifn!("llvm.maxnum.f32", fn(t_f32, t_f32) -> t_f32);
613 ifn!("llvm.maxnum.f64", fn(t_f64, t_f64) -> t_f64);
615 ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
616 ifn!("llvm.floor.v2f32", fn(t_v2f32) -> t_v2f32);
617 ifn!("llvm.floor.v4f32", fn(t_v4f32) -> t_v4f32);
618 ifn!("llvm.floor.v8f32", fn(t_v8f32) -> t_v8f32);
619 ifn!("llvm.floor.v16f32", fn(t_v16f32) -> t_v16f32);
620 ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
621 ifn!("llvm.floor.v2f64", fn(t_v2f64) -> t_v2f64);
622 ifn!("llvm.floor.v4f64", fn(t_v4f64) -> t_v4f64);
623 ifn!("llvm.floor.v8f64", fn(t_v8f64) -> t_v8f64);
625 ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
626 ifn!("llvm.ceil.v2f32", fn(t_v2f32) -> t_v2f32);
627 ifn!("llvm.ceil.v4f32", fn(t_v4f32) -> t_v4f32);
628 ifn!("llvm.ceil.v8f32", fn(t_v8f32) -> t_v8f32);
629 ifn!("llvm.ceil.v16f32", fn(t_v16f32) -> t_v16f32);
630 ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
631 ifn!("llvm.ceil.v2f64", fn(t_v2f64) -> t_v2f64);
632 ifn!("llvm.ceil.v4f64", fn(t_v4f64) -> t_v4f64);
633 ifn!("llvm.ceil.v8f64", fn(t_v8f64) -> t_v8f64);
635 ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
636 ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
638 ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
639 ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
640 ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
641 ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
643 ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
644 ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
645 ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
646 ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
648 ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
649 ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
650 ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
651 ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
652 ifn!("llvm.ctpop.i128", fn(t_i128) -> t_i128);
654 ifn!("llvm.ctlz.i8", fn(t_i8, i1) -> t_i8);
655 ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
656 ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
657 ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
658 ifn!("llvm.ctlz.i128", fn(t_i128, i1) -> t_i128);
660 ifn!("llvm.cttz.i8", fn(t_i8, i1) -> t_i8);
661 ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
662 ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
663 ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
664 ifn!("llvm.cttz.i128", fn(t_i128, i1) -> t_i128);
666 ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
667 ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
668 ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
669 ifn!("llvm.bswap.i128", fn(t_i128) -> t_i128);
671 ifn!("llvm.bitreverse.i8", fn(t_i8) -> t_i8);
672 ifn!("llvm.bitreverse.i16", fn(t_i16) -> t_i16);
673 ifn!("llvm.bitreverse.i32", fn(t_i32) -> t_i32);
674 ifn!("llvm.bitreverse.i64", fn(t_i64) -> t_i64);
675 ifn!("llvm.bitreverse.i128", fn(t_i128) -> t_i128);
677 ifn!("llvm.fshl.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
678 ifn!("llvm.fshl.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
679 ifn!("llvm.fshl.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
680 ifn!("llvm.fshl.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
681 ifn!("llvm.fshl.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
683 ifn!("llvm.fshr.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
684 ifn!("llvm.fshr.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
685 ifn!("llvm.fshr.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
686 ifn!("llvm.fshr.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
687 ifn!("llvm.fshr.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
689 ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
690 ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
691 ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
692 ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
693 ifn!("llvm.sadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
695 ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
696 ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
697 ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
698 ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
699 ifn!("llvm.uadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
701 ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
702 ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
703 ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
704 ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
705 ifn!("llvm.ssub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
707 ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
708 ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
709 ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
710 ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
711 ifn!("llvm.usub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
713 ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
714 ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
715 ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
716 ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
717 ifn!("llvm.smul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
719 ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
720 ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
721 ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
722 ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
723 ifn!("llvm.umul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
725 ifn!("llvm.sadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
726 ifn!("llvm.sadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
727 ifn!("llvm.sadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
728 ifn!("llvm.sadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
729 ifn!("llvm.sadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
731 ifn!("llvm.uadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
732 ifn!("llvm.uadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
733 ifn!("llvm.uadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
734 ifn!("llvm.uadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
735 ifn!("llvm.uadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
737 ifn!("llvm.ssub.sat.i8", fn(t_i8, t_i8) -> t_i8);
738 ifn!("llvm.ssub.sat.i16", fn(t_i16, t_i16) -> t_i16);
739 ifn!("llvm.ssub.sat.i32", fn(t_i32, t_i32) -> t_i32);
740 ifn!("llvm.ssub.sat.i64", fn(t_i64, t_i64) -> t_i64);
741 ifn!("llvm.ssub.sat.i128", fn(t_i128, t_i128) -> t_i128);
743 ifn!("llvm.usub.sat.i8", fn(t_i8, t_i8) -> t_i8);
744 ifn!("llvm.usub.sat.i16", fn(t_i16, t_i16) -> t_i16);
745 ifn!("llvm.usub.sat.i32", fn(t_i32, t_i32) -> t_i32);
746 ifn!("llvm.usub.sat.i64", fn(t_i64, t_i64) -> t_i64);
747 ifn!("llvm.usub.sat.i128", fn(t_i128, t_i128) -> t_i128);
749 ifn!("llvm.lifetime.start.p0i8", fn(t_i64, i8p) -> void);
750 ifn!("llvm.lifetime.end.p0i8", fn(t_i64, i8p) -> void);
752 ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
753 ifn!("llvm.eh.typeid.for", fn(i8p) -> t_i32);
754 ifn!("llvm.localescape", fn(...) -> void);
755 ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
756 ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);
758 ifn!("llvm.assume", fn(i1) -> void);
759 ifn!("llvm.prefetch", fn(i8p, t_i32, t_i32, t_i32) -> void);
761 // variadic intrinsics
762 ifn!("llvm.va_start", fn(i8p) -> void);
763 ifn!("llvm.va_end", fn(i8p) -> void);
764 ifn!("llvm.va_copy", fn(i8p, i8p) -> void);
766 if self.sess().opts.debuginfo != DebugInfo::None {
767 ifn!("llvm.dbg.declare", fn(self.type_metadata(), self.type_metadata()) -> void);
768 ifn!("llvm.dbg.value", fn(self.type_metadata(), t_i64, self.type_metadata()) -> void);
774 impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
775 /// Generates a new symbol name with the given prefix. This symbol name must
776 /// only be used for definitions with `internal` or `private` linkage.
777 pub fn generate_local_symbol_name(&self, prefix: &str) -> String {
778 let idx = self.local_gen_sym_counter.get();
779 self.local_gen_sym_counter.set(idx + 1);
780 // Include a '.' character, so there can be no accidental conflicts with
781 // user defined names
782 let mut name = String::with_capacity(prefix.len() + 6);
783 name.push_str(prefix);
785 base_n::push_str(idx as u128, base_n::ALPHANUMERIC_ONLY, &mut name);
790 impl HasDataLayout for CodegenCx<'ll, 'tcx> {
791 fn data_layout(&self) -> &TargetDataLayout {
792 &self.tcx.data_layout
796 impl HasTargetSpec for CodegenCx<'ll, 'tcx> {
797 fn target_spec(&self) -> &Target {
798 &self.tcx.sess.target.target
802 impl ty::layout::HasTyCtxt<'tcx> for CodegenCx<'ll, 'tcx> {
803 fn tcx(&self) -> TyCtxt<'tcx> {
808 impl LayoutOf for CodegenCx<'ll, 'tcx> {
810 type TyAndLayout = TyAndLayout<'tcx>;
812 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
813 self.spanned_layout_of(ty, DUMMY_SP)
816 fn spanned_layout_of(&self, ty: Ty<'tcx>, span: Span) -> Self::TyAndLayout {
817 self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).unwrap_or_else(|e| {
818 if let LayoutError::SizeOverflow(_) = e {
819 self.sess().span_fatal(span, &e.to_string())
821 bug!("failed to get layout for `{}`: {}", ty, e)
827 impl<'tcx, 'll> HasParamEnv<'tcx> for CodegenCx<'ll, 'tcx> {
828 fn param_env(&self) -> ty::ParamEnv<'tcx> {
829 ty::ParamEnv::reveal_all()