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 std::ffi::{CStr, CString};
14 use rustc::hir::TransFnAttrFlags;
15 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
16 use rustc::session::config::Sanitizer;
17 use rustc::ty::maps::Providers;
18 use rustc_data_structures::sync::Lrc;
20 use llvm::{self, Attribute, ValueRef};
21 use llvm::AttributePlace::Function;
23 pub use syntax::attr::{self, InlineAttr};
24 use context::CodegenCx;
26 /// Mark LLVM function to use provided inline heuristic.
28 pub fn inline(val: ValueRef, inline: InlineAttr) {
29 use self::InlineAttr::*;
31 Hint => Attribute::InlineHint.apply_llfn(Function, val),
32 Always => Attribute::AlwaysInline.apply_llfn(Function, val),
33 Never => Attribute::NoInline.apply_llfn(Function, val),
35 Attribute::InlineHint.unapply_llfn(Function, val);
36 Attribute::AlwaysInline.unapply_llfn(Function, val);
37 Attribute::NoInline.unapply_llfn(Function, val);
42 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
44 pub fn emit_uwtable(val: ValueRef, emit: bool) {
45 Attribute::UWTable.toggle_llfn(Function, val, emit);
48 /// Tell LLVM whether the function can or cannot unwind.
50 pub fn unwind(val: ValueRef, can_unwind: bool) {
51 Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
54 /// Tell LLVM whether it should optimize function for size.
56 #[allow(dead_code)] // possibly useful function
57 pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
58 Attribute::OptimizeForSize.toggle_llfn(Function, val, optimize);
61 /// Tell LLVM if this function should be 'naked', i.e. skip the epilogue and prologue.
63 pub fn naked(val: ValueRef, is_naked: bool) {
64 Attribute::Naked.toggle_llfn(Function, val, is_naked);
67 pub fn set_frame_pointer_elimination(cx: &CodegenCx, llfn: ValueRef) {
68 // FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
70 if cx.sess().must_not_eliminate_frame_pointers() {
71 llvm::AddFunctionAttrStringValue(
72 llfn, llvm::AttributePlace::Function,
73 cstr("no-frame-pointer-elim\0"), cstr("true\0"));
77 pub fn set_probestack(cx: &CodegenCx, llfn: ValueRef) {
78 // Only use stack probes if the target specification indicates that we
79 // should be using stack probes
80 if !cx.sess().target.target.options.stack_probes {
84 // Currently stack probes seem somewhat incompatible with the address
85 // sanitizer. With asan we're already protected from stack overflow anyway
86 // so we don't really need stack probes regardless.
87 match cx.sess().opts.debugging_opts.sanitizer {
88 Some(Sanitizer::Address) => return,
92 // Flag our internal `__rust_probestack` function as the stack probe symbol.
93 // This is defined in the `compiler-builtins` crate for each architecture.
94 llvm::AddFunctionAttrStringValue(
95 llfn, llvm::AttributePlace::Function,
96 cstr("probe-stack\0"), cstr("__rust_probestack\0"));
99 /// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
101 pub fn from_fn_attrs(cx: &CodegenCx, llfn: ValueRef, id: DefId) {
102 let trans_fn_attrs = cx.tcx.trans_fn_attrs(id);
104 inline(llfn, trans_fn_attrs.inline);
106 set_frame_pointer_elimination(cx, llfn);
107 set_probestack(cx, llfn);
109 if trans_fn_attrs.flags.contains(TransFnAttrFlags::COLD) {
110 Attribute::Cold.apply_llfn(Function, llfn);
112 if trans_fn_attrs.flags.contains(TransFnAttrFlags::NAKED) {
115 if trans_fn_attrs.flags.contains(TransFnAttrFlags::ALLOCATOR) {
116 Attribute::NoAlias.apply_llfn(
117 llvm::AttributePlace::ReturnValue, llfn);
119 if trans_fn_attrs.flags.contains(TransFnAttrFlags::UNWIND) {
122 if trans_fn_attrs.flags.contains(TransFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND) {
127 trans_fn_attrs.target_features
130 let feature = &*f.as_str();
131 format!("+{}", llvm_util::to_llvm_feature(cx.tcx.sess, feature))
133 .collect::<Vec<String>>()
136 if !features.is_empty() {
137 let val = CString::new(features).unwrap();
138 llvm::AddFunctionAttrStringValue(
139 llfn, llvm::AttributePlace::Function,
140 cstr("target-features\0"), &val);
144 fn cstr(s: &'static str) -> &CStr {
145 CStr::from_bytes_with_nul(s.as_bytes()).expect("null-terminated string")
148 pub fn provide(providers: &mut Providers) {
149 providers.target_features_whitelist = |tcx, cnum| {
150 assert_eq!(cnum, LOCAL_CRATE);
151 Lrc::new(llvm_util::target_feature_whitelist(tcx.sess)
153 .map(|c| c.to_string())