]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/attributes.rs
c77cb49144818b4e94c3d3b3f896b503e0eb30ff
[rust.git] / src / librustc_trans / trans / attributes.rs
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.
4 //
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.
11
12 use libc::{c_uint, c_ulonglong};
13 use llvm::{self, ValueRef, AttrHelper};
14 use middle::ty;
15 use middle::infer;
16 use session::config::NoDebugInfo;
17 use syntax::abi;
18 use rustc_front::hir;
19 pub use rustc_front::attr::InlineAttr;
20 use trans::base;
21 use trans::common;
22 use trans::context::CrateContext;
23 use trans::machine;
24 use trans::type_of;
25
26 /// Mark LLVM function to use provided inline heuristic.
27 #[inline]
28 pub fn inline(val: ValueRef, inline: InlineAttr) {
29     use self::InlineAttr::*;
30     match inline {
31         Hint   => llvm::SetFunctionAttribute(val, llvm::Attribute::InlineHint),
32         Always => llvm::SetFunctionAttribute(val, llvm::Attribute::AlwaysInline),
33         Never  => llvm::SetFunctionAttribute(val, llvm::Attribute::NoInline),
34         None   => {
35             let attr = llvm::Attribute::InlineHint |
36                        llvm::Attribute::AlwaysInline |
37                        llvm::Attribute::NoInline;
38             unsafe {
39                 llvm::LLVMRemoveFunctionAttr(val, attr.bits() as c_ulonglong)
40             }
41         },
42     };
43 }
44
45 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
46 #[inline]
47 pub fn emit_uwtable(val: ValueRef, emit: bool) {
48     if emit {
49         llvm::SetFunctionAttribute(val, llvm::Attribute::UWTable);
50     } else {
51         unsafe {
52             llvm::LLVMRemoveFunctionAttr(
53                 val,
54                 llvm::Attribute::UWTable.bits() as c_ulonglong,
55             );
56         }
57     }
58 }
59
60 /// Tell LLVM whether the function can or cannot unwind.
61 #[inline]
62 pub fn unwind(val: ValueRef, can_unwind: bool) {
63     if can_unwind {
64         unsafe {
65             llvm::LLVMRemoveFunctionAttr(
66                 val,
67                 llvm::Attribute::NoUnwind.bits() as c_ulonglong,
68             );
69         }
70     } else {
71         llvm::SetFunctionAttribute(val, llvm::Attribute::NoUnwind);
72     }
73 }
74
75 /// Tell LLVM whether it should optimise function for size.
76 #[inline]
77 #[allow(dead_code)] // possibly useful function
78 pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
79     if optimize {
80         llvm::SetFunctionAttribute(val, llvm::Attribute::OptimizeForSize);
81     } else {
82         unsafe {
83             llvm::LLVMRemoveFunctionAttr(
84                 val,
85                 llvm::Attribute::OptimizeForSize.bits() as c_ulonglong,
86             );
87         }
88     }
89 }
90
91 /// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
92 /// attributes.
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));
96
97     // FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
98     // parameter.
99     let no_fp_elim = (ccx.sess().opts.debuginfo != NoDebugInfo) ||
100                      !ccx.sess().target.target.options.eliminate_frame_pointer;
101     if no_fp_elim {
102         unsafe {
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,
107                                                  attr, val);
108         }
109     }
110
111     for attr in attrs {
112         if attr.check_name("cold") {
113             unsafe {
114                 llvm::LLVMAddFunctionAttribute(llfn,
115                                                llvm::FunctionIndex as c_uint,
116                                                llvm::ColdAttribute as u64)
117             }
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") {
121             unwind(llfn, true);
122         }
123     }
124 }
125
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};
130
131     let function_type;
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))
139         }
140         _ => ccx.sess().bug("expected closure or function.")
141     };
142
143     let fn_sig = ccx.tcx().erase_late_bound_regions(fn_sig);
144
145     let mut attrs = llvm::AttrBuilder::new();
146     let ret_ty = fn_sig.output;
147
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);
153
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);
158                     full_inputs
159                 }
160                 _ => ccx.sess().bug("expected tuple'd inputs")
161             }
162         },
163         ty::TyBareFn(..) if abi == abi::RustCall => {
164             let mut inputs = vec![fn_sig.inputs[0]];
165
166             match fn_sig.inputs[1].sty {
167                 ty::TyTuple(ref t_in) => {
168                     inputs.push_all(&t_in[..]);
169                     inputs
170                 }
171                 _ => ccx.sess().bug("expected tuple'd inputs")
172             }
173         }
174         _ => fn_sig.inputs.clone()
175     };
176
177     // Index 0 is the return value of the llvm func, so we start at 1
178     let mut idx = 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));
186
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));
194
195             // Add one more since there's an outptr
196             idx += 1;
197         } else {
198             // The `noalias` attribute on the return value is useful to a
199             // function ptr caller.
200             match ret_ty.sty {
201                 // `Box` pointer return values never alias because ownership
202                 // is transferred
203                 ty::TyBox(it) if common::type_is_sized(ccx.tcx(), it) => {
204                     attrs.ret(llvm::Attribute::NoAlias);
205                 }
206                 _ => {}
207             }
208
209             // We can also mark the return value as `dereferenceable` in certain cases
210             match ret_ty.sty {
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));
216                 }
217                 _ => {}
218             }
219
220             if let ty::TyBool = ret_ty.sty {
221                 attrs.ret(llvm::Attribute::ZExt);
222             }
223         }
224     }
225
226     for &t in input_tys.iter() {
227         match t.sty {
228             _ if type_of::arg_is_indirect(ccx, t) => {
229                 let llarg_sz = machine::llsize_of_real(ccx, type_of::type_of(ccx, t));
230
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));
237             }
238
239             ty::TyBool => {
240                 attrs.arg(idx, llvm::Attribute::ZExt);
241             }
242
243             // `Box` pointer parameters never alias because ownership is transferred
244             ty::TyBox(inner) => {
245                 attrs.arg(idx, llvm::Attribute::NoAlias);
246
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));
250                 } else {
251                     attrs.arg(idx, llvm::NonNullAttribute);
252                     if inner.is_trait() {
253                         attrs.arg(idx + 1, llvm::NonNullAttribute);
254                     }
255                 }
256             }
257
258             ty::TyRef(b, mt) => {
259                 // `&mut` pointer parameters never alias other parameters, or mutable global data
260                 //
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();
265
266                 if mt.mutbl == hir::MutMutable || !interior_unsafe {
267                     attrs.arg(idx, llvm::Attribute::NoAlias);
268                 }
269
270                 if mt.mutbl == hir::MutImmutable && !interior_unsafe {
271                     attrs.arg(idx, llvm::Attribute::ReadOnly);
272                 }
273
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));
279                 } else {
280                     attrs.arg(idx, llvm::NonNullAttribute);
281                     if mt.ty.is_trait() {
282                         attrs.arg(idx + 1, llvm::NonNullAttribute);
283                     }
284                 }
285
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);
291                 }
292             }
293
294             _ => ()
295         }
296
297         if common::type_is_fat_ptr(ccx.tcx(), t) {
298             idx += 2;
299         } else {
300             idx += 1;
301         }
302     }
303
304     attrs
305 }