2015-02-28 23:53:12 +02:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
//! Set and unset common attributes on LLVM values.
|
|
|
|
|
2016-08-03 00:25:19 +03:00
|
|
|
use llvm::{self, Attribute, ValueRef};
|
|
|
|
use llvm::AttributePlace::Function;
|
2015-09-14 21:58:20 +12:00
|
|
|
pub use syntax::attr::InlineAttr;
|
|
|
|
use syntax::ast;
|
2016-03-22 19:23:36 +02:00
|
|
|
use context::CrateContext;
|
2015-02-28 23:53:12 +02:00
|
|
|
|
|
|
|
/// Mark LLVM function to use provided inline heuristic.
|
|
|
|
#[inline]
|
|
|
|
pub fn inline(val: ValueRef, inline: InlineAttr) {
|
2015-03-04 01:03:25 +02:00
|
|
|
use self::InlineAttr::*;
|
2015-02-28 23:53:12 +02:00
|
|
|
match inline {
|
2016-08-03 00:25:19 +03:00
|
|
|
Hint => Attribute::InlineHint.apply_llfn(Function, val),
|
|
|
|
Always => Attribute::AlwaysInline.apply_llfn(Function, val),
|
|
|
|
Never => Attribute::NoInline.apply_llfn(Function, val),
|
2015-03-04 01:03:25 +02:00
|
|
|
None => {
|
2016-08-03 00:25:19 +03:00
|
|
|
let attr = Attribute::InlineHint |
|
|
|
|
Attribute::AlwaysInline |
|
|
|
|
Attribute::NoInline;
|
|
|
|
attr.unapply_llfn(Function, val)
|
2015-02-28 23:53:12 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
|
|
|
|
#[inline]
|
|
|
|
pub fn emit_uwtable(val: ValueRef, emit: bool) {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::UWTable.toggle_llfn(Function, val, emit);
|
2015-02-28 23:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell LLVM whether the function can or cannot unwind.
|
|
|
|
#[inline]
|
|
|
|
pub fn unwind(val: ValueRef, can_unwind: bool) {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
|
2015-02-28 23:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell LLVM whether it should optimise function for size.
|
|
|
|
#[inline]
|
|
|
|
#[allow(dead_code)] // possibly useful function
|
|
|
|
pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::OptimizeForSize.toggle_llfn(Function, val, optimize);
|
2015-02-28 23:53:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 21:01:08 +01:00
|
|
|
/// Tell LLVM if this function should be 'naked', i.e. skip the epilogue and prologue.
|
|
|
|
#[inline]
|
|
|
|
pub fn naked(val: ValueRef, is_naked: bool) {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::Naked.toggle_llfn(Function, val, is_naked);
|
2016-03-21 21:01:08 +01:00
|
|
|
}
|
|
|
|
|
2016-05-27 13:27:56 -04:00
|
|
|
pub fn set_frame_pointer_elimination(ccx: &CrateContext, llfn: ValueRef) {
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 12:10:43 -07:00
|
|
|
// FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
|
|
|
|
// parameter.
|
2016-05-27 13:27:56 -04:00
|
|
|
if ccx.sess().must_not_eliminate_frame_pointers() {
|
2016-08-03 00:25:19 +03:00
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
|
|
|
"no-frame-pointer-elim\0",
|
|
|
|
"true\0")
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 12:10:43 -07:00
|
|
|
}
|
2016-05-27 13:27:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
|
|
|
|
/// attributes.
|
|
|
|
pub fn from_fn_attrs(ccx: &CrateContext, attrs: &[ast::Attribute], llfn: ValueRef) {
|
|
|
|
use syntax::attr::*;
|
|
|
|
inline(llfn, find_inline_attr(Some(ccx.sess().diagnostic()), attrs));
|
|
|
|
|
|
|
|
set_frame_pointer_elimination(ccx, llfn);
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 12:10:43 -07:00
|
|
|
|
2015-02-28 23:53:12 +02:00
|
|
|
for attr in attrs {
|
2015-07-27 13:41:35 -07:00
|
|
|
if attr.check_name("cold") {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::Cold.apply_llfn(Function, llfn);
|
2016-03-21 21:01:08 +01:00
|
|
|
} else if attr.check_name("naked") {
|
|
|
|
naked(llfn, true);
|
2015-02-28 23:53:12 +02:00
|
|
|
} else if attr.check_name("allocator") {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::NoAlias.apply_llfn(
|
|
|
|
llvm::AttributePlace::ReturnValue(), llfn);
|
2015-09-11 20:09:19 +02:00
|
|
|
} else if attr.check_name("unwind") {
|
|
|
|
unwind(llfn, true);
|
2015-02-28 23:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|