2 use crate::back::write::to_llvm_code_model;
3 use crate::callee::get_fn;
4 use crate::coverageinfo;
8 use crate::type_::Type;
9 use crate::value::Value;
12 use rustc_codegen_ssa::base::wants_msvc_seh;
13 use rustc_codegen_ssa::traits::*;
14 use rustc_data_structures::base_n;
15 use rustc_data_structures::fx::FxHashMap;
16 use rustc_data_structures::small_c_str::SmallCStr;
17 use rustc_middle::mir::mono::CodegenUnit;
18 use rustc_middle::ty::layout::{
19 FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, LayoutError, LayoutOfHelpers,
22 use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
23 use rustc_middle::{bug, span_bug};
24 use rustc_session::config::{CFGuard, CrateType, DebugInfo};
25 use rustc_session::Session;
26 use rustc_span::source_map::Span;
27 use rustc_span::symbol::Symbol;
28 use rustc_target::abi::{
29 call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx,
31 use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
32 use smallvec::SmallVec;
34 use std::cell::{Cell, RefCell};
38 /// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
39 /// `llvm::Context` so that several compilation units may be optimized in parallel.
40 /// All other LLVM data structures in the `CodegenCx` are tied to that `llvm::Context`.
41 pub struct CodegenCx<'ll, 'tcx> {
42 pub tcx: TyCtxt<'tcx>,
43 pub check_overflow: bool,
44 pub use_dll_storage_attrs: bool,
45 pub tls_model: llvm::ThreadLocalMode,
47 pub llmod: &'ll llvm::Module,
48 pub llcx: &'ll llvm::Context,
49 pub codegen_unit: &'tcx CodegenUnit<'tcx>,
51 /// Cache instances of monomorphic and polymorphic items
52 pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
53 /// Cache generated vtables
55 RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>,
56 /// Cache of constant strings,
57 pub const_cstr_cache: RefCell<FxHashMap<Symbol, &'ll Value>>,
59 /// Reverse-direction for const ptrs cast from globals.
61 /// Key is a Value holding a `*T`,
62 /// Val is a Value holding a `*[T]`.
64 /// Needed because LLVM loses pointer->pointee association
65 /// when we ptrcast, and we have to ptrcast during codegen
66 /// of a `[T]` const because we form a slice, a `(*T,usize)` pair, not
67 /// a pointer to an LLVM array type. Similar for trait objects.
68 pub const_unsized: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
70 /// Cache of emitted const globals (value -> global)
71 pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
73 /// List of globals for static variables which need to be passed to the
74 /// LLVM function ReplaceAllUsesWith (RAUW) when codegen is complete.
75 /// (We have to make sure we don't invalidate any Values referring
77 pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
79 /// Statics that will be placed in the llvm.used variable
80 /// See <https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
81 pub used_statics: RefCell<Vec<&'ll Value>>,
83 /// Statics that will be placed in the llvm.compiler.used variable
84 /// See <https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable> for details
85 pub compiler_used_statics: RefCell<Vec<&'ll Value>>,
87 /// Mapping of non-scalar types to llvm types and field remapping if needed.
88 pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), TypeLowering<'ll>>>,
90 /// Mapping of scalar types to llvm types.
91 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 coverage_cx: Option<coverageinfo::CrateCoverageContext<'ll, 'tcx>>,
97 pub dbg_cx: Option<debuginfo::CrateDebugContext<'ll, 'tcx>>,
99 eh_personality: Cell<Option<&'ll Value>>,
100 eh_catch_typeinfo: Cell<Option<&'ll Value>>,
101 pub rust_try_fn: Cell<Option<(&'ll Type, &'ll Value)>>,
103 intrinsics: RefCell<FxHashMap<&'static str, (&'ll Type, &'ll Value)>>,
105 /// A counter that is used for generating local symbol names
106 local_gen_sym_counter: Cell<usize>,
109 pub struct TypeLowering<'ll> {
110 /// Associated LLVM type
111 pub lltype: &'ll Type,
113 /// If padding is used the slice maps fields from source order
115 pub field_remapping: Option<SmallVec<[u32; 4]>>,
118 fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
120 TlsModel::GeneralDynamic => llvm::ThreadLocalMode::GeneralDynamic,
121 TlsModel::LocalDynamic => llvm::ThreadLocalMode::LocalDynamic,
122 TlsModel::InitialExec => llvm::ThreadLocalMode::InitialExec,
123 TlsModel::LocalExec => llvm::ThreadLocalMode::LocalExec,
127 pub unsafe fn create_module(
129 llcx: &'ll llvm::Context,
131 ) -> &'ll llvm::Module {
133 let mod_name = SmallCStr::new(mod_name);
134 let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
136 let mut target_data_layout = sess.target.data_layout.clone();
137 if llvm_util::get_version() < (12, 0, 0) && sess.target.arch == "powerpc64" {
138 target_data_layout = target_data_layout.replace("-v256:256:256-v512:512:512", "");
140 if llvm_util::get_version() < (13, 0, 0) {
141 if sess.target.arch == "powerpc64" {
142 target_data_layout = target_data_layout.replace("-S128", "");
144 if sess.target.arch == "wasm32" {
145 target_data_layout = "e-m:e-p:32:32-i64:64-n32:64-S128".to_string();
147 if sess.target.arch == "wasm64" {
148 target_data_layout = "e-m:e-p:64:64-i64:64-n32:64-S128".to_string();
152 // Ensure the data-layout values hardcoded remain the defaults.
153 if sess.target.is_builtin {
154 let tm = crate::back::write::create_informational_target_machine(tcx.sess);
155 llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
156 llvm::LLVMRustDisposeTargetMachine(tm);
158 let llvm_data_layout = llvm::LLVMGetDataLayoutStr(llmod);
159 let llvm_data_layout = str::from_utf8(CStr::from_ptr(llvm_data_layout).to_bytes())
160 .expect("got a non-UTF8 data-layout from LLVM");
162 // Unfortunately LLVM target specs change over time, and right now we
163 // don't have proper support to work with any more than one
164 // `data_layout` than the one that is in the rust-lang/rust repo. If
165 // this compiler is configured against a custom LLVM, we may have a
166 // differing data layout, even though we should update our own to use
169 // As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
170 // disable this check entirely as we may be configured with something
171 // that has a different target layout.
173 // Unsure if this will actually cause breakage when rustc is configured
177 let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
178 let custom_llvm_used = cfg_llvm_root.trim() != "";
180 if !custom_llvm_used && target_data_layout != llvm_data_layout {
182 "data-layout for target `{rustc_target}`, `{rustc_layout}`, \
183 differs from LLVM target's `{llvm_target}` default layout, `{llvm_layout}`",
184 rustc_target = sess.opts.target_triple,
185 rustc_layout = target_data_layout,
186 llvm_target = sess.target.llvm_target,
187 llvm_layout = llvm_data_layout
192 let data_layout = SmallCStr::new(&target_data_layout);
193 llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
195 let llvm_target = SmallCStr::new(&sess.target.llvm_target);
196 llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
198 let reloc_model = sess.relocation_model();
199 if matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
200 llvm::LLVMRustSetModulePICLevel(llmod);
201 // PIE is potentially more effective than PIC, but can only be used in executables.
202 // If all our outputs are executables, then we can relax PIC to PIE.
203 if reloc_model == RelocModel::Pie
204 || sess.crate_types().iter().all(|ty| *ty == CrateType::Executable)
206 llvm::LLVMRustSetModulePIELevel(llmod);
210 // Linking object files with different code models is undefined behavior
211 // because the compiler would have to generate additional code (to span
212 // longer jumps) if a larger code model is used with a smaller one.
214 // See https://reviews.llvm.org/D52322 and https://reviews.llvm.org/D52323.
215 llvm::LLVMRustSetModuleCodeModel(llmod, to_llvm_code_model(sess.code_model()));
217 // If skipping the PLT is enabled, we need to add some module metadata
218 // to ensure intrinsic calls don't use it.
219 if !sess.needs_plt() {
220 let avoid_plt = "RtLibUseGOT\0".as_ptr().cast();
221 llvm::LLVMRustAddModuleFlag(llmod, avoid_plt, 1);
224 // Control Flow Guard is currently only supported by the MSVC linker on Windows.
225 if sess.target.is_like_msvc {
226 match sess.opts.cg.control_flow_guard {
227 CFGuard::Disabled => {}
228 CFGuard::NoChecks => {
229 // Set `cfguard=1` module flag to emit metadata only.
230 llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 1)
233 // Set `cfguard=2` module flag to emit metadata and checks.
234 llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 2)
242 impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
245 codegen_unit: &'tcx CodegenUnit<'tcx>,
246 llvm_module: &'ll crate::ModuleLlvm,
248 // An interesting part of Windows which MSVC forces our hand on (and
249 // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
250 // attributes in LLVM IR as well as native dependencies (in C these
251 // correspond to `__declspec(dllimport)`).
253 // LD (BFD) in MinGW mode can often correctly guess `dllexport` but
254 // relying on that can result in issues like #50176.
255 // LLD won't support that and expects symbols with proper attributes.
256 // Because of that we make MinGW target emit dllexport just like MSVC.
257 // When it comes to dllimport we use it for constants but for functions
258 // rely on the linker to do the right thing. Opposed to dllexport this
259 // task is easy for them (both LD and LLD) and allows us to easily use
260 // symbols from static libraries in shared libraries.
262 // Whenever a dynamic library is built on Windows it must have its public
263 // interface specified by functions tagged with `dllexport` or otherwise
264 // they're not available to be linked against. This poses a few problems
265 // for the compiler, some of which are somewhat fundamental, but we use
266 // the `use_dll_storage_attrs` variable below to attach the `dllexport`
267 // attribute to all LLVM functions that are exported e.g., they're
268 // already tagged with external linkage). This is suboptimal for a few
271 // * If an object file will never be included in a dynamic library,
272 // there's no need to attach the dllexport attribute. Most object
273 // files in Rust are not destined to become part of a dll as binaries
274 // are statically linked by default.
275 // * If the compiler is emitting both an rlib and a dylib, the same
276 // source object file is currently used but with MSVC this may be less
277 // feasible. The compiler may be able to get around this, but it may
278 // involve some invasive changes to deal with this.
280 // The flipside of this situation is that whenever you link to a dll and
281 // you import a function from it, the import should be tagged with
282 // `dllimport`. At this time, however, the compiler does not emit
283 // `dllimport` for any declarations other than constants (where it is
284 // required), which is again suboptimal for even more reasons!
286 // * Calling a function imported from another dll without using
287 // `dllimport` causes the linker/compiler to have extra overhead (one
288 // `jmp` instruction on x86) when calling the function.
289 // * The same object file may be used in different circumstances, so a
290 // function may be imported from a dll if the object is linked into a
291 // dll, but it may be just linked against if linked into an rlib.
292 // * The compiler has no knowledge about whether native functions should
293 // be tagged dllimport or not.
295 // For now the compiler takes the perf hit (I do not have any numbers to
296 // this effect) by marking very little as `dllimport` and praying the
297 // linker will take care of everything. Fixing this problem will likely
298 // require adding a few attributes to Rust itself (feature gated at the
299 // start) and then strongly recommending static linkage on Windows!
300 let use_dll_storage_attrs = tcx.sess.target.is_like_windows;
302 let check_overflow = tcx.sess.overflow_checks();
304 let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
306 let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
308 let coverage_cx = if tcx.sess.instrument_coverage() {
309 let covctx = coverageinfo::CrateCoverageContext::new();
315 let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
316 let dctx = debuginfo::CrateDebugContext::new(llmod);
317 debuginfo::metadata::compile_unit_metadata(tcx, &codegen_unit.name().as_str(), &dctx);
323 let isize_ty = Type::ix_llcx(llcx, tcx.data_layout.pointer_size.bits());
328 use_dll_storage_attrs,
333 instances: Default::default(),
334 vtables: Default::default(),
335 const_cstr_cache: Default::default(),
336 const_unsized: Default::default(),
337 const_globals: Default::default(),
338 statics_to_rauw: RefCell::new(Vec::new()),
339 used_statics: RefCell::new(Vec::new()),
340 compiler_used_statics: RefCell::new(Vec::new()),
341 type_lowering: Default::default(),
342 scalar_lltypes: Default::default(),
343 pointee_infos: Default::default(),
347 eh_personality: Cell::new(None),
348 eh_catch_typeinfo: Cell::new(None),
349 rust_try_fn: Cell::new(None),
350 intrinsics: Default::default(),
351 local_gen_sym_counter: Cell::new(0),
355 crate fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
356 &self.statics_to_rauw
360 pub fn coverage_context(&'a self) -> Option<&'a coverageinfo::CrateCoverageContext<'ll, 'tcx>> {
361 self.coverage_cx.as_ref()
364 fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
365 let section = cstr!("llvm.metadata");
366 let array = self.const_array(self.type_ptr_to(self.type_i8()), values);
369 let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
370 llvm::LLVMSetInitializer(g, array);
371 llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
372 llvm::LLVMSetSection(g, section.as_ptr());
377 impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
380 ) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>
385 fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
386 get_fn(self, instance)
389 fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
390 get_fn(self, instance)
393 fn eh_personality(&self) -> &'ll Value {
394 // The exception handling personality function.
396 // If our compilation unit has the `eh_personality` lang item somewhere
397 // within it, then we just need to codegen that. Otherwise, we're
398 // building an rlib which will depend on some upstream implementation of
399 // this function, so we just codegen a generic reference to it. We don't
400 // specify any of the types for the function, we just make it a symbol
401 // that LLVM can later use.
403 // Note that MSVC is a little special here in that we don't use the
404 // `eh_personality` lang item at all. Currently LLVM has support for
405 // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
406 // *name of the personality function* to decide what kind of unwind side
407 // tables/landing pads to emit. It looks like Dwarf is used by default,
408 // injecting a dependency on the `_Unwind_Resume` symbol for resuming
409 // an "exception", but for MSVC we want to force SEH. This means that we
410 // can't actually have the personality function be our standard
411 // `rust_eh_personality` function, but rather we wired it up to the
412 // CRT's custom personality function, which forces LLVM to consider
413 // landing pads as "landing pads for SEH".
414 if let Some(llpersonality) = self.eh_personality.get() {
415 return llpersonality;
418 let llfn = match tcx.lang_items().eh_personality() {
419 Some(def_id) if !wants_msvc_seh(self.sess()) => self.get_fn_addr(
420 ty::Instance::resolve(
422 ty::ParamEnv::reveal_all(),
424 tcx.intern_substs(&[]),
430 let name = if wants_msvc_seh(self.sess()) {
433 "rust_eh_personality"
435 if let Some(llfn) = self.get_declared_value(name) {
438 let fty = self.type_variadic_func(&[], self.type_i32());
439 let llfn = self.declare_cfn(name, llvm::UnnamedAddr::Global, fty);
440 attributes::apply_target_cpu_attr(self, llfn);
445 self.eh_personality.set(Some(llfn));
449 fn sess(&self) -> &Session {
453 fn check_overflow(&self) -> bool {
457 fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
461 fn used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
465 fn compiler_used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
466 &self.compiler_used_statics
469 fn set_frame_pointer_type(&self, llfn: &'ll Value) {
470 attributes::set_frame_pointer_type(self, llfn)
473 fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
474 attributes::apply_target_cpu_attr(self, llfn);
475 attributes::apply_tune_cpu_attr(self, llfn);
478 fn create_used_variable(&self) {
479 self.create_used_variable_impl(cstr!("llvm.used"), &*self.used_statics.borrow());
482 fn create_compiler_used_variable(&self) {
483 self.create_used_variable_impl(
484 cstr!("llvm.compiler.used"),
485 &*self.compiler_used_statics.borrow(),
489 fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
490 if self.get_declared_value("main").is_none() {
491 Some(self.declare_cfn("main", llvm::UnnamedAddr::Global, fn_type))
493 // If the symbol already exists, it is an error: for example, the user wrote
494 // #[no_mangle] extern "C" fn main(..) {..}
495 // instead of #[start]
501 impl CodegenCx<'b, 'tcx> {
502 crate fn get_intrinsic(&self, key: &str) -> (&'b Type, &'b Value) {
503 if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
507 self.declare_intrinsic(key).unwrap_or_else(|| bug!("unknown intrinsic '{}'", key))
513 args: Option<&[&'b llvm::Type]>,
515 ) -> (&'b llvm::Type, &'b llvm::Value) {
516 let fn_ty = if let Some(args) = args {
517 self.type_func(args, ret)
519 self.type_variadic_func(&[], ret)
521 let f = self.declare_cfn(name, llvm::UnnamedAddr::No, fn_ty);
522 self.intrinsics.borrow_mut().insert(name, (fn_ty, f));
526 fn declare_intrinsic(&self, key: &str) -> Option<(&'b Type, &'b Value)> {
528 ($name:expr, fn() -> $ret:expr) => (
530 return Some(self.insert_intrinsic($name, Some(&[]), $ret));
533 ($name:expr, fn(...) -> $ret:expr) => (
535 return Some(self.insert_intrinsic($name, None, $ret));
538 ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
540 return Some(self.insert_intrinsic($name, Some(&[$($arg),*]), $ret));
544 macro_rules! mk_struct {
545 ($($field_ty:expr),*) => (self.type_struct( &[$($field_ty),*], false))
548 let i8p = self.type_i8p();
549 let void = self.type_void();
550 let i1 = self.type_i1();
551 let t_i8 = self.type_i8();
552 let t_i16 = self.type_i16();
553 let t_i32 = self.type_i32();
554 let t_i64 = self.type_i64();
555 let t_i128 = self.type_i128();
556 let t_isize = self.type_isize();
557 let t_f32 = self.type_f32();
558 let t_f64 = self.type_f64();
560 ifn!("llvm.wasm.trunc.unsigned.i32.f32", fn(t_f32) -> t_i32);
561 ifn!("llvm.wasm.trunc.unsigned.i32.f64", fn(t_f64) -> t_i32);
562 ifn!("llvm.wasm.trunc.unsigned.i64.f32", fn(t_f32) -> t_i64);
563 ifn!("llvm.wasm.trunc.unsigned.i64.f64", fn(t_f64) -> t_i64);
564 ifn!("llvm.wasm.trunc.signed.i32.f32", fn(t_f32) -> t_i32);
565 ifn!("llvm.wasm.trunc.signed.i32.f64", fn(t_f64) -> t_i32);
566 ifn!("llvm.wasm.trunc.signed.i64.f32", fn(t_f32) -> t_i64);
567 ifn!("llvm.wasm.trunc.signed.i64.f64", fn(t_f64) -> t_i64);
569 ifn!("llvm.fptosi.sat.i8.f32", fn(t_f32) -> t_i8);
570 ifn!("llvm.fptosi.sat.i16.f32", fn(t_f32) -> t_i16);
571 ifn!("llvm.fptosi.sat.i32.f32", fn(t_f32) -> t_i32);
572 ifn!("llvm.fptosi.sat.i64.f32", fn(t_f32) -> t_i64);
573 ifn!("llvm.fptosi.sat.i128.f32", fn(t_f32) -> t_i128);
574 ifn!("llvm.fptosi.sat.i8.f64", fn(t_f64) -> t_i8);
575 ifn!("llvm.fptosi.sat.i16.f64", fn(t_f64) -> t_i16);
576 ifn!("llvm.fptosi.sat.i32.f64", fn(t_f64) -> t_i32);
577 ifn!("llvm.fptosi.sat.i64.f64", fn(t_f64) -> t_i64);
578 ifn!("llvm.fptosi.sat.i128.f64", fn(t_f64) -> t_i128);
580 ifn!("llvm.fptoui.sat.i8.f32", fn(t_f32) -> t_i8);
581 ifn!("llvm.fptoui.sat.i16.f32", fn(t_f32) -> t_i16);
582 ifn!("llvm.fptoui.sat.i32.f32", fn(t_f32) -> t_i32);
583 ifn!("llvm.fptoui.sat.i64.f32", fn(t_f32) -> t_i64);
584 ifn!("llvm.fptoui.sat.i128.f32", fn(t_f32) -> t_i128);
585 ifn!("llvm.fptoui.sat.i8.f64", fn(t_f64) -> t_i8);
586 ifn!("llvm.fptoui.sat.i16.f64", fn(t_f64) -> t_i16);
587 ifn!("llvm.fptoui.sat.i32.f64", fn(t_f64) -> t_i32);
588 ifn!("llvm.fptoui.sat.i64.f64", fn(t_f64) -> t_i64);
589 ifn!("llvm.fptoui.sat.i128.f64", fn(t_f64) -> t_i128);
591 ifn!("llvm.trap", fn() -> void);
592 ifn!("llvm.debugtrap", fn() -> void);
593 ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
594 ifn!("llvm.sideeffect", fn() -> void);
596 ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
597 ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
599 ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
600 ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
602 ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
603 ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
605 ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
606 ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
608 ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
609 ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
611 ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
612 ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
614 ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
615 ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
617 ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
618 ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
620 ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
621 ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
623 ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
624 ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
626 ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
627 ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
629 ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
630 ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
632 ifn!("llvm.minnum.f32", fn(t_f32, t_f32) -> t_f32);
633 ifn!("llvm.minnum.f64", fn(t_f64, t_f64) -> t_f64);
634 ifn!("llvm.maxnum.f32", fn(t_f32, t_f32) -> t_f32);
635 ifn!("llvm.maxnum.f64", fn(t_f64, t_f64) -> t_f64);
637 ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
638 ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
640 ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
641 ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
643 ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
644 ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
646 ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
647 ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
648 ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
649 ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
651 ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
652 ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
653 ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
654 ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
656 ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
657 ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
658 ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
659 ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
660 ifn!("llvm.ctpop.i128", fn(t_i128) -> t_i128);
662 ifn!("llvm.ctlz.i8", fn(t_i8, i1) -> t_i8);
663 ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
664 ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
665 ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
666 ifn!("llvm.ctlz.i128", fn(t_i128, i1) -> t_i128);
668 ifn!("llvm.cttz.i8", fn(t_i8, i1) -> t_i8);
669 ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
670 ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
671 ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
672 ifn!("llvm.cttz.i128", fn(t_i128, i1) -> t_i128);
674 ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
675 ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
676 ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
677 ifn!("llvm.bswap.i128", fn(t_i128) -> t_i128);
679 ifn!("llvm.bitreverse.i8", fn(t_i8) -> t_i8);
680 ifn!("llvm.bitreverse.i16", fn(t_i16) -> t_i16);
681 ifn!("llvm.bitreverse.i32", fn(t_i32) -> t_i32);
682 ifn!("llvm.bitreverse.i64", fn(t_i64) -> t_i64);
683 ifn!("llvm.bitreverse.i128", fn(t_i128) -> t_i128);
685 ifn!("llvm.fshl.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
686 ifn!("llvm.fshl.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
687 ifn!("llvm.fshl.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
688 ifn!("llvm.fshl.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
689 ifn!("llvm.fshl.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
691 ifn!("llvm.fshr.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
692 ifn!("llvm.fshr.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
693 ifn!("llvm.fshr.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
694 ifn!("llvm.fshr.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
695 ifn!("llvm.fshr.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
697 ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
698 ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
699 ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
700 ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
701 ifn!("llvm.sadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
703 ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
704 ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
705 ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
706 ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
707 ifn!("llvm.uadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
709 ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
710 ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
711 ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
712 ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
713 ifn!("llvm.ssub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
715 ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
716 ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
717 ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
718 ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
719 ifn!("llvm.usub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
721 ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
722 ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
723 ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
724 ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
725 ifn!("llvm.smul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
727 ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
728 ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
729 ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
730 ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
731 ifn!("llvm.umul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
733 ifn!("llvm.sadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
734 ifn!("llvm.sadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
735 ifn!("llvm.sadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
736 ifn!("llvm.sadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
737 ifn!("llvm.sadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
739 ifn!("llvm.uadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
740 ifn!("llvm.uadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
741 ifn!("llvm.uadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
742 ifn!("llvm.uadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
743 ifn!("llvm.uadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
745 ifn!("llvm.ssub.sat.i8", fn(t_i8, t_i8) -> t_i8);
746 ifn!("llvm.ssub.sat.i16", fn(t_i16, t_i16) -> t_i16);
747 ifn!("llvm.ssub.sat.i32", fn(t_i32, t_i32) -> t_i32);
748 ifn!("llvm.ssub.sat.i64", fn(t_i64, t_i64) -> t_i64);
749 ifn!("llvm.ssub.sat.i128", fn(t_i128, t_i128) -> t_i128);
751 ifn!("llvm.usub.sat.i8", fn(t_i8, t_i8) -> t_i8);
752 ifn!("llvm.usub.sat.i16", fn(t_i16, t_i16) -> t_i16);
753 ifn!("llvm.usub.sat.i32", fn(t_i32, t_i32) -> t_i32);
754 ifn!("llvm.usub.sat.i64", fn(t_i64, t_i64) -> t_i64);
755 ifn!("llvm.usub.sat.i128", fn(t_i128, t_i128) -> t_i128);
757 ifn!("llvm.lifetime.start.p0i8", fn(t_i64, i8p) -> void);
758 ifn!("llvm.lifetime.end.p0i8", fn(t_i64, i8p) -> void);
760 ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
761 ifn!("llvm.eh.typeid.for", fn(i8p) -> t_i32);
762 ifn!("llvm.localescape", fn(...) -> void);
763 ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
764 ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);
766 ifn!("llvm.assume", fn(i1) -> void);
767 ifn!("llvm.prefetch", fn(i8p, t_i32, t_i32, t_i32) -> void);
769 // This isn't an "LLVM intrinsic", but LLVM's optimization passes
770 // recognize it like one and we assume it exists in `core::slice::cmp`
771 ifn!("memcmp", fn(i8p, i8p, t_isize) -> t_i32);
773 // variadic intrinsics
774 ifn!("llvm.va_start", fn(i8p) -> void);
775 ifn!("llvm.va_end", fn(i8p) -> void);
776 ifn!("llvm.va_copy", fn(i8p, i8p) -> void);
778 if self.sess().instrument_coverage() {
779 ifn!("llvm.instrprof.increment", fn(i8p, t_i64, t_i32, t_i32) -> void);
782 if self.sess().opts.debuginfo != DebugInfo::None {
783 ifn!("llvm.dbg.declare", fn(self.type_metadata(), self.type_metadata()) -> void);
784 ifn!("llvm.dbg.value", fn(self.type_metadata(), t_i64, self.type_metadata()) -> void);
789 crate fn eh_catch_typeinfo(&self) -> &'b Value {
790 if let Some(eh_catch_typeinfo) = self.eh_catch_typeinfo.get() {
791 return eh_catch_typeinfo;
794 assert!(self.sess().target.is_like_emscripten);
795 let eh_catch_typeinfo = match tcx.lang_items().eh_catch_typeinfo() {
796 Some(def_id) => self.get_static(def_id),
799 .type_struct(&[self.type_ptr_to(self.type_isize()), self.type_i8p()], false);
800 self.declare_global("rust_eh_catch_typeinfo", ty)
803 let eh_catch_typeinfo = self.const_bitcast(eh_catch_typeinfo, self.type_i8p());
804 self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
809 impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
810 /// Generates a new symbol name with the given prefix. This symbol name must
811 /// only be used for definitions with `internal` or `private` linkage.
812 pub fn generate_local_symbol_name(&self, prefix: &str) -> String {
813 let idx = self.local_gen_sym_counter.get();
814 self.local_gen_sym_counter.set(idx + 1);
815 // Include a '.' character, so there can be no accidental conflicts with
816 // user defined names
817 let mut name = String::with_capacity(prefix.len() + 6);
818 name.push_str(prefix);
820 base_n::push_str(idx as u128, base_n::ALPHANUMERIC_ONLY, &mut name);
825 impl HasDataLayout for CodegenCx<'ll, 'tcx> {
827 fn data_layout(&self) -> &TargetDataLayout {
828 &self.tcx.data_layout
832 impl HasTargetSpec for CodegenCx<'ll, 'tcx> {
834 fn target_spec(&self) -> &Target {
835 &self.tcx.sess.target
839 impl ty::layout::HasTyCtxt<'tcx> for CodegenCx<'ll, 'tcx> {
841 fn tcx(&self) -> TyCtxt<'tcx> {
846 impl<'tcx, 'll> HasParamEnv<'tcx> for CodegenCx<'ll, 'tcx> {
847 fn param_env(&self) -> ty::ParamEnv<'tcx> {
848 ty::ParamEnv::reveal_all()
852 impl LayoutOfHelpers<'tcx> for CodegenCx<'ll, 'tcx> {
853 type LayoutOfResult = TyAndLayout<'tcx>;
856 fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
857 if let LayoutError::SizeOverflow(_) = err {
858 self.sess().span_fatal(span, &err.to_string())
860 span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
865 impl FnAbiOfHelpers<'tcx> for CodegenCx<'ll, 'tcx> {
866 type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
869 fn handle_fn_abi_err(
871 err: FnAbiError<'tcx>,
873 fn_abi_request: FnAbiRequest<'tcx>,
875 if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
876 self.sess().span_fatal(span, &err.to_string())
878 match fn_abi_request {
879 FnAbiRequest::OfFnPtr { sig, extra_args } => {
882 "`fn_abi_of_fn_ptr({}, {:?})` failed: {}",
888 FnAbiRequest::OfInstance { instance, extra_args } => {
891 "`fn_abi_of_instance({}, {:?})` failed: {}",