]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/declare.rs
Rollup merge of #67507 - Mark-Simulacrum:purge-uninit, r=Centril
[rust.git] / src / librustc_codegen_llvm / declare.rs
1 //! Declare various LLVM values.
2 //!
3 //! Prefer using functions and methods from this module rather than calling LLVM
4 //! functions directly. These functions do some additional work to ensure we do
5 //! the right thing given the preconceptions of codegen.
6 //!
7 //! Some useful guidelines:
8 //!
9 //! * Use declare_* family of methods if you are declaring, but are not
10 //!   interested in defining the Value they return.
11 //! * Use define_* family of methods when you might be defining the Value.
12 //! * When in doubt, define.
13
14 use crate::abi::{FnAbi, FnAbiLlvmExt};
15 use crate::attributes;
16 use crate::context::CodegenCx;
17 use crate::llvm;
18 use crate::llvm::AttributePlace::Function;
19 use crate::type_::Type;
20 use crate::value::Value;
21 use log::debug;
22 use rustc::session::config::Sanitizer;
23 use rustc::ty::Ty;
24 use rustc_codegen_ssa::traits::*;
25 use rustc_data_structures::small_c_str::SmallCStr;
26
27 /// Declare a function.
28 ///
29 /// If there’s a value with the same name already declared, the function will
30 /// update the declaration and return existing Value instead.
31 fn declare_raw_fn(
32     cx: &CodegenCx<'ll, '_>,
33     name: &str,
34     callconv: llvm::CallConv,
35     ty: &'ll Type,
36 ) -> &'ll Value {
37     debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
38     let namebuf = SmallCStr::new(name);
39     let llfn = unsafe { llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty) };
40
41     llvm::SetFunctionCallConv(llfn, callconv);
42     // Function addresses in Rust are never significant, allowing functions to
43     // be merged.
44     llvm::SetUnnamedAddr(llfn, true);
45
46     if cx.tcx.sess.opts.cg.no_redzone.unwrap_or(cx.tcx.sess.target.target.options.disable_redzone) {
47         llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
48     }
49
50     if let Some(ref sanitizer) = cx.tcx.sess.opts.debugging_opts.sanitizer {
51         match *sanitizer {
52             Sanitizer::Address => {
53                 llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
54             }
55             Sanitizer::Memory => {
56                 llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
57             }
58             Sanitizer::Thread => {
59                 llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
60             }
61             _ => {}
62         }
63     }
64
65     attributes::default_optimisation_attrs(cx.tcx.sess, llfn);
66     attributes::non_lazy_bind(cx.sess(), llfn);
67     llfn
68 }
69
70 impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
71     fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
72         debug!("declare_global(name={:?})", name);
73         unsafe { llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty) }
74     }
75
76     fn declare_cfn(&self, name: &str, fn_type: &'ll Type) -> &'ll Value {
77         declare_raw_fn(self, name, llvm::CCallConv, fn_type)
78     }
79
80     fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Value {
81         debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
82
83         let llfn = declare_raw_fn(self, name, fn_abi.llvm_cconv(), fn_abi.llvm_type(self));
84         fn_abi.apply_attrs_llfn(self, llfn);
85         llfn
86     }
87
88     fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
89         if self.get_defined_value(name).is_some() {
90             None
91         } else {
92             Some(self.declare_global(name, ty))
93         }
94     }
95
96     fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
97         unsafe { llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty) }
98     }
99
100     fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
101         debug!("get_declared_value(name={:?})", name);
102         let namebuf = SmallCStr::new(name);
103         unsafe { llvm::LLVMRustGetNamedValue(self.llmod, namebuf.as_ptr()) }
104     }
105
106     fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
107         self.get_declared_value(name).and_then(|val| {
108             let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
109             if !declaration { Some(val) } else { None }
110         })
111     }
112 }