]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/attributes.rs
Use ast attributes every where (remove HIR attributes).
[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 pub use syntax::attr::InlineAttr;
19 use syntax::ast;
20 use rustc_front::hir;
21 use trans::base;
22 use trans::common;
23 use trans::context::CrateContext;
24 use trans::machine;
25 use trans::type_of;
26
27 /// Mark LLVM function to use provided inline heuristic.
28 #[inline]
29 pub fn inline(val: ValueRef, inline: InlineAttr) {
30     use self::InlineAttr::*;
31     match inline {
32         Hint   => llvm::SetFunctionAttribute(val, llvm::Attribute::InlineHint),
33         Always => llvm::SetFunctionAttribute(val, llvm::Attribute::AlwaysInline),
34         Never  => llvm::SetFunctionAttribute(val, llvm::Attribute::NoInline),
35         None   => {
36             let attr = llvm::Attribute::InlineHint |
37                        llvm::Attribute::AlwaysInline |
38                        llvm::Attribute::NoInline;
39             unsafe {
40                 llvm::LLVMRemoveFunctionAttr(val, attr.bits() as c_ulonglong)
41             }
42         },
43     };
44 }
45
46 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
47 #[inline]
48 pub fn emit_uwtable(val: ValueRef, emit: bool) {
49     if emit {
50         llvm::SetFunctionAttribute(val, llvm::Attribute::UWTable);
51     } else {
52         unsafe {
53             llvm::LLVMRemoveFunctionAttr(
54                 val,
55                 llvm::Attribute::UWTable.bits() as c_ulonglong,
56             );
57         }
58     }
59 }
60
61 /// Tell LLVM whether the function can or cannot unwind.
62 #[inline]
63 pub fn unwind(val: ValueRef, can_unwind: bool) {
64     if can_unwind {
65         unsafe {
66             llvm::LLVMRemoveFunctionAttr(
67                 val,
68                 llvm::Attribute::NoUnwind.bits() as c_ulonglong,
69             );
70         }
71     } else {
72         llvm::SetFunctionAttribute(val, llvm::Attribute::NoUnwind);
73     }
74 }
75
76 /// Tell LLVM whether it should optimise function for size.
77 #[inline]
78 #[allow(dead_code)] // possibly useful function
79 pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
80     if optimize {
81         llvm::SetFunctionAttribute(val, llvm::Attribute::OptimizeForSize);
82     } else {
83         unsafe {
84             llvm::LLVMRemoveFunctionAttr(
85                 val,
86                 llvm::Attribute::OptimizeForSize.bits() as c_ulonglong,
87             );
88         }
89     }
90 }
91
92 /// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
93 /// attributes.
94 pub fn from_fn_attrs(ccx: &CrateContext, attrs: &[ast::Attribute], llfn: ValueRef) {
95     use syntax::attr::*;
96     inline(llfn, find_inline_attr(Some(ccx.sess().diagnostic()), attrs));
97
98     // FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
99     // parameter.
100     let no_fp_elim = (ccx.sess().opts.debuginfo != NoDebugInfo) ||
101                      !ccx.sess().target.target.options.eliminate_frame_pointer;
102     if no_fp_elim {
103         unsafe {
104             let attr = "no-frame-pointer-elim\0".as_ptr() as *const _;
105             let val = "true\0".as_ptr() as *const _;
106             llvm::LLVMAddFunctionAttrStringValue(llfn,
107                                                  llvm::FunctionIndex as c_uint,
108                                                  attr, val);
109         }
110     }
111
112     for attr in attrs {
113         if attr.check_name("cold") {
114             unsafe {
115                 llvm::LLVMAddFunctionAttribute(llfn,
116                                                llvm::FunctionIndex as c_uint,
117                                                llvm::ColdAttribute as u64)
118             }
119         } else if attr.check_name("allocator") {
120             llvm::Attribute::NoAlias.apply_llfn(llvm::ReturnIndex as c_uint, llfn);
121         } else if attr.check_name("unwind") {
122             unwind(llfn, true);
123         }
124     }
125 }
126
127 /// Composite function which converts function type into LLVM attributes for the function.
128 pub fn from_fn_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_type: ty::Ty<'tcx>)
129                               -> llvm::AttrBuilder {
130     use middle::ty::{BrAnon, ReLateBound};
131
132     let function_type;
133     let (fn_sig, abi, env_ty) = match fn_type.sty {
134         ty::TyBareFn(_, ref f) => (&f.sig, f.abi, None),
135         ty::TyClosure(closure_did, ref substs) => {
136             let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables);
137             function_type = infcx.closure_type(closure_did, substs);
138             let self_type = base::self_type_for_closure(ccx, closure_did, fn_type);
139             (&function_type.sig, abi::RustCall, Some(self_type))
140         }
141         _ => ccx.sess().bug("expected closure or function.")
142     };
143
144     let fn_sig = ccx.tcx().erase_late_bound_regions(fn_sig);
145
146     let mut attrs = llvm::AttrBuilder::new();
147     let ret_ty = fn_sig.output;
148
149     // These have an odd calling convention, so we need to manually
150     // unpack the input ty's
151     let input_tys = match fn_type.sty {
152         ty::TyClosure(..) => {
153             assert!(abi == abi::RustCall);
154
155             match fn_sig.inputs[0].sty {
156                 ty::TyTuple(ref inputs) => {
157                     let mut full_inputs = vec![env_ty.expect("Missing closure environment")];
158                     full_inputs.push_all(inputs);
159                     full_inputs
160                 }
161                 _ => ccx.sess().bug("expected tuple'd inputs")
162             }
163         },
164         ty::TyBareFn(..) if abi == abi::RustCall => {
165             let mut inputs = vec![fn_sig.inputs[0]];
166
167             match fn_sig.inputs[1].sty {
168                 ty::TyTuple(ref t_in) => {
169                     inputs.push_all(&t_in[..]);
170                     inputs
171                 }
172                 _ => ccx.sess().bug("expected tuple'd inputs")
173             }
174         }
175         _ => fn_sig.inputs.clone()
176     };
177
178     // Index 0 is the return value of the llvm func, so we start at 1
179     let mut idx = 1;
180     if let ty::FnConverging(ret_ty) = ret_ty {
181         // A function pointer is called without the declaration
182         // available, so we have to apply any attributes with ABI
183         // implications directly to the call instruction. Right now,
184         // the only attribute we need to worry about is `sret`.
185         if type_of::return_uses_outptr(ccx, ret_ty) {
186             let llret_sz = machine::llsize_of_real(ccx, type_of::type_of(ccx, ret_ty));
187
188             // The outptr can be noalias and nocapture because it's entirely
189             // invisible to the program. We also know it's nonnull as well
190             // as how many bytes we can dereference
191             attrs.arg(1, llvm::Attribute::StructRet)
192                  .arg(1, llvm::Attribute::NoAlias)
193                  .arg(1, llvm::Attribute::NoCapture)
194                  .arg(1, llvm::DereferenceableAttribute(llret_sz));
195
196             // Add one more since there's an outptr
197             idx += 1;
198         } else {
199             // The `noalias` attribute on the return value is useful to a
200             // function ptr caller.
201             match ret_ty.sty {
202                 // `Box` pointer return values never alias because ownership
203                 // is transferred
204                 ty::TyBox(it) if common::type_is_sized(ccx.tcx(), it) => {
205                     attrs.ret(llvm::Attribute::NoAlias);
206                 }
207                 _ => {}
208             }
209
210             // We can also mark the return value as `dereferenceable` in certain cases
211             match ret_ty.sty {
212                 // These are not really pointers but pairs, (pointer, len)
213                 ty::TyRef(_, ty::TypeAndMut { ty: inner, .. })
214                 | ty::TyBox(inner) if common::type_is_sized(ccx.tcx(), inner) => {
215                     let llret_sz = machine::llsize_of_real(ccx, type_of::type_of(ccx, inner));
216                     attrs.ret(llvm::DereferenceableAttribute(llret_sz));
217                 }
218                 _ => {}
219             }
220
221             if let ty::TyBool = ret_ty.sty {
222                 attrs.ret(llvm::Attribute::ZExt);
223             }
224         }
225     }
226
227     for &t in input_tys.iter() {
228         match t.sty {
229             _ if type_of::arg_is_indirect(ccx, t) => {
230                 let llarg_sz = machine::llsize_of_real(ccx, type_of::type_of(ccx, t));
231
232                 // For non-immediate arguments the callee gets its own copy of
233                 // the value on the stack, so there are no aliases. It's also
234                 // program-invisible so can't possibly capture
235                 attrs.arg(idx, llvm::Attribute::NoAlias)
236                      .arg(idx, llvm::Attribute::NoCapture)
237                      .arg(idx, llvm::DereferenceableAttribute(llarg_sz));
238             }
239
240             ty::TyBool => {
241                 attrs.arg(idx, llvm::Attribute::ZExt);
242             }
243
244             // `Box` pointer parameters never alias because ownership is transferred
245             ty::TyBox(inner) => {
246                 attrs.arg(idx, llvm::Attribute::NoAlias);
247
248                 if common::type_is_sized(ccx.tcx(), inner) {
249                     let llsz = machine::llsize_of_real(ccx, type_of::type_of(ccx, inner));
250                     attrs.arg(idx, llvm::DereferenceableAttribute(llsz));
251                 } else {
252                     attrs.arg(idx, llvm::NonNullAttribute);
253                     if inner.is_trait() {
254                         attrs.arg(idx + 1, llvm::NonNullAttribute);
255                     }
256                 }
257             }
258
259             ty::TyRef(b, mt) => {
260                 // `&mut` pointer parameters never alias other parameters, or mutable global data
261                 //
262                 // `&T` where `T` contains no `UnsafeCell<U>` is immutable, and can be marked as
263                 // both `readonly` and `noalias`, as LLVM's definition of `noalias` is based solely
264                 // on memory dependencies rather than pointer equality
265                 let interior_unsafe = mt.ty.type_contents(ccx.tcx()).interior_unsafe();
266
267                 if mt.mutbl == hir::MutMutable || !interior_unsafe {
268                     attrs.arg(idx, llvm::Attribute::NoAlias);
269                 }
270
271                 if mt.mutbl == hir::MutImmutable && !interior_unsafe {
272                     attrs.arg(idx, llvm::Attribute::ReadOnly);
273                 }
274
275                 // & pointer parameters are also never null and for sized types we also know
276                 // exactly how many bytes we can dereference
277                 if common::type_is_sized(ccx.tcx(), mt.ty) {
278                     let llsz = machine::llsize_of_real(ccx, type_of::type_of(ccx, mt.ty));
279                     attrs.arg(idx, llvm::DereferenceableAttribute(llsz));
280                 } else {
281                     attrs.arg(idx, llvm::NonNullAttribute);
282                     if mt.ty.is_trait() {
283                         attrs.arg(idx + 1, llvm::NonNullAttribute);
284                     }
285                 }
286
287                 // When a reference in an argument has no named lifetime, it's
288                 // impossible for that reference to escape this function
289                 // (returned or stored beyond the call by a closure).
290                 if let ReLateBound(_, BrAnon(_)) = *b {
291                     attrs.arg(idx, llvm::Attribute::NoCapture);
292                 }
293             }
294
295             _ => ()
296         }
297
298         if common::type_is_fat_ptr(ccx.tcx(), t) {
299             idx += 2;
300         } else {
301             idx += 1;
302         }
303     }
304
305     attrs
306 }