1 // Copyright 2012-2015 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.
10 //! Set and unset common attributes on LLVM values.
12 use libc::{c_uint, c_ulonglong};
13 use llvm::{self, ValueRef, AttrHelper};
16 use session::config::NoDebugInfo;
19 pub use rustc_front::attr::InlineAttr;
22 use trans::context::CrateContext;
26 /// Mark LLVM function to use provided inline heuristic.
28 pub fn inline(val: ValueRef, inline: InlineAttr) {
29 use self::InlineAttr::*;
31 Hint => llvm::SetFunctionAttribute(val, llvm::Attribute::InlineHint),
32 Always => llvm::SetFunctionAttribute(val, llvm::Attribute::AlwaysInline),
33 Never => llvm::SetFunctionAttribute(val, llvm::Attribute::NoInline),
35 let attr = llvm::Attribute::InlineHint |
36 llvm::Attribute::AlwaysInline |
37 llvm::Attribute::NoInline;
39 llvm::LLVMRemoveFunctionAttr(val, attr.bits() as c_ulonglong)
45 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
47 pub fn emit_uwtable(val: ValueRef, emit: bool) {
49 llvm::SetFunctionAttribute(val, llvm::Attribute::UWTable);
52 llvm::LLVMRemoveFunctionAttr(
54 llvm::Attribute::UWTable.bits() as c_ulonglong,
60 /// Tell LLVM whether the function can or cannot unwind.
62 pub fn unwind(val: ValueRef, can_unwind: bool) {
65 llvm::LLVMRemoveFunctionAttr(
67 llvm::Attribute::NoUnwind.bits() as c_ulonglong,
71 llvm::SetFunctionAttribute(val, llvm::Attribute::NoUnwind);
75 /// Tell LLVM whether it should optimise function for size.
77 #[allow(dead_code)] // possibly useful function
78 pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
80 llvm::SetFunctionAttribute(val, llvm::Attribute::OptimizeForSize);
83 llvm::LLVMRemoveFunctionAttr(
85 llvm::Attribute::OptimizeForSize.bits() as c_ulonglong,
91 /// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
93 pub fn from_fn_attrs(ccx: &CrateContext, attrs: &[hir::Attribute], llfn: ValueRef) {
94 use rustc_front::attr::*;
95 inline(llfn, find_inline_attr(Some(ccx.sess().diagnostic()), attrs));
97 // FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
99 let no_fp_elim = (ccx.sess().opts.debuginfo != NoDebugInfo) ||
100 !ccx.sess().target.target.options.eliminate_frame_pointer;
103 let attr = "no-frame-pointer-elim\0".as_ptr() as *const _;
104 let val = "true\0".as_ptr() as *const _;
105 llvm::LLVMAddFunctionAttrStringValue(llfn,
106 llvm::FunctionIndex as c_uint,
112 if attr.check_name("cold") {
114 llvm::LLVMAddFunctionAttribute(llfn,
115 llvm::FunctionIndex as c_uint,
116 llvm::ColdAttribute as u64)
118 } else if attr.check_name("allocator") {
119 llvm::Attribute::NoAlias.apply_llfn(llvm::ReturnIndex as c_uint, llfn);
120 } else if attr.check_name("unwind") {
126 /// Composite function which converts function type into LLVM attributes for the function.
127 pub fn from_fn_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_type: ty::Ty<'tcx>)
128 -> llvm::AttrBuilder {
129 use middle::ty::{BrAnon, ReLateBound};
132 let (fn_sig, abi, env_ty) = match fn_type.sty {
133 ty::TyBareFn(_, ref f) => (&f.sig, f.abi, None),
134 ty::TyClosure(closure_did, ref substs) => {
135 let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables);
136 function_type = infcx.closure_type(closure_did, substs);
137 let self_type = base::self_type_for_closure(ccx, closure_did, fn_type);
138 (&function_type.sig, abi::RustCall, Some(self_type))
140 _ => ccx.sess().bug("expected closure or function.")
143 let fn_sig = ccx.tcx().erase_late_bound_regions(fn_sig);
145 let mut attrs = llvm::AttrBuilder::new();
146 let ret_ty = fn_sig.output;
148 // These have an odd calling convention, so we need to manually
149 // unpack the input ty's
150 let input_tys = match fn_type.sty {
151 ty::TyClosure(..) => {
152 assert!(abi == abi::RustCall);
154 match fn_sig.inputs[0].sty {
155 ty::TyTuple(ref inputs) => {
156 let mut full_inputs = vec![env_ty.expect("Missing closure environment")];
157 full_inputs.push_all(inputs);
160 _ => ccx.sess().bug("expected tuple'd inputs")
163 ty::TyBareFn(..) if abi == abi::RustCall => {
164 let mut inputs = vec![fn_sig.inputs[0]];
166 match fn_sig.inputs[1].sty {
167 ty::TyTuple(ref t_in) => {
168 inputs.push_all(&t_in[..]);
171 _ => ccx.sess().bug("expected tuple'd inputs")
174 _ => fn_sig.inputs.clone()
177 // Index 0 is the return value of the llvm func, so we start at 1
179 if let ty::FnConverging(ret_ty) = ret_ty {
180 // A function pointer is called without the declaration
181 // available, so we have to apply any attributes with ABI
182 // implications directly to the call instruction. Right now,
183 // the only attribute we need to worry about is `sret`.
184 if type_of::return_uses_outptr(ccx, ret_ty) {
185 let llret_sz = machine::llsize_of_real(ccx, type_of::type_of(ccx, ret_ty));
187 // The outptr can be noalias and nocapture because it's entirely
188 // invisible to the program. We also know it's nonnull as well
189 // as how many bytes we can dereference
190 attrs.arg(1, llvm::Attribute::StructRet)
191 .arg(1, llvm::Attribute::NoAlias)
192 .arg(1, llvm::Attribute::NoCapture)
193 .arg(1, llvm::DereferenceableAttribute(llret_sz));
195 // Add one more since there's an outptr
198 // The `noalias` attribute on the return value is useful to a
199 // function ptr caller.
201 // `Box` pointer return values never alias because ownership
203 ty::TyBox(it) if common::type_is_sized(ccx.tcx(), it) => {
204 attrs.ret(llvm::Attribute::NoAlias);
209 // We can also mark the return value as `dereferenceable` in certain cases
211 // These are not really pointers but pairs, (pointer, len)
212 ty::TyRef(_, ty::TypeAndMut { ty: inner, .. })
213 | ty::TyBox(inner) if common::type_is_sized(ccx.tcx(), inner) => {
214 let llret_sz = machine::llsize_of_real(ccx, type_of::type_of(ccx, inner));
215 attrs.ret(llvm::DereferenceableAttribute(llret_sz));
220 if let ty::TyBool = ret_ty.sty {
221 attrs.ret(llvm::Attribute::ZExt);
226 for &t in input_tys.iter() {
228 _ if type_of::arg_is_indirect(ccx, t) => {
229 let llarg_sz = machine::llsize_of_real(ccx, type_of::type_of(ccx, t));
231 // For non-immediate arguments the callee gets its own copy of
232 // the value on the stack, so there are no aliases. It's also
233 // program-invisible so can't possibly capture
234 attrs.arg(idx, llvm::Attribute::NoAlias)
235 .arg(idx, llvm::Attribute::NoCapture)
236 .arg(idx, llvm::DereferenceableAttribute(llarg_sz));
240 attrs.arg(idx, llvm::Attribute::ZExt);
243 // `Box` pointer parameters never alias because ownership is transferred
244 ty::TyBox(inner) => {
245 attrs.arg(idx, llvm::Attribute::NoAlias);
247 if common::type_is_sized(ccx.tcx(), inner) {
248 let llsz = machine::llsize_of_real(ccx, type_of::type_of(ccx, inner));
249 attrs.arg(idx, llvm::DereferenceableAttribute(llsz));
251 attrs.arg(idx, llvm::NonNullAttribute);
252 if inner.is_trait() {
253 attrs.arg(idx + 1, llvm::NonNullAttribute);
258 ty::TyRef(b, mt) => {
259 // `&mut` pointer parameters never alias other parameters, or mutable global data
261 // `&T` where `T` contains no `UnsafeCell<U>` is immutable, and can be marked as
262 // both `readonly` and `noalias`, as LLVM's definition of `noalias` is based solely
263 // on memory dependencies rather than pointer equality
264 let interior_unsafe = mt.ty.type_contents(ccx.tcx()).interior_unsafe();
266 if mt.mutbl == hir::MutMutable || !interior_unsafe {
267 attrs.arg(idx, llvm::Attribute::NoAlias);
270 if mt.mutbl == hir::MutImmutable && !interior_unsafe {
271 attrs.arg(idx, llvm::Attribute::ReadOnly);
274 // & pointer parameters are also never null and for sized types we also know
275 // exactly how many bytes we can dereference
276 if common::type_is_sized(ccx.tcx(), mt.ty) {
277 let llsz = machine::llsize_of_real(ccx, type_of::type_of(ccx, mt.ty));
278 attrs.arg(idx, llvm::DereferenceableAttribute(llsz));
280 attrs.arg(idx, llvm::NonNullAttribute);
281 if mt.ty.is_trait() {
282 attrs.arg(idx + 1, llvm::NonNullAttribute);
286 // When a reference in an argument has no named lifetime, it's
287 // impossible for that reference to escape this function
288 // (returned or stored beyond the call by a closure).
289 if let ReLateBound(_, BrAnon(_)) = *b {
290 attrs.arg(idx, llvm::Attribute::NoCapture);
297 if common::type_is_fat_ptr(ccx.tcx(), t) {