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