2015-10-21 17:42:25 -04:00
|
|
|
// Copyright 2012-2014 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.
|
|
|
|
|
2018-06-25 18:46:02 +02:00
|
|
|
use rustc::mir::interpret::ConstEvalErr;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir;
|
2018-08-02 13:16:53 +02:00
|
|
|
use rustc::mir::interpret::{ConstValue, ScalarMaybeUndef};
|
2018-01-30 09:40:46 +01:00
|
|
|
use rustc::ty;
|
2018-01-16 09:31:48 +01:00
|
|
|
use rustc::ty::layout::{self, Align, LayoutOf, TyLayout};
|
2016-06-07 17:28:36 +03:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2018-06-25 17:41:20 +02:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2016-06-07 17:28:36 +03:00
|
|
|
|
2016-03-22 19:23:36 +02:00
|
|
|
use base;
|
2018-07-20 18:43:46 +02:00
|
|
|
use common::{CodegenCx, C_undef, C_usize};
|
2018-05-11 12:26:32 +02:00
|
|
|
use builder::{Builder, MemFlags};
|
2016-03-22 19:23:36 +02:00
|
|
|
use value::Value;
|
2017-06-26 14:57:50 +03:00
|
|
|
use type_of::LayoutLlvmExt;
|
2018-05-29 00:12:55 +09:00
|
|
|
use type_::Type;
|
|
|
|
use glue;
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-02-18 19:49:45 +02:00
|
|
|
use std::fmt;
|
|
|
|
|
2018-01-05 07:34:28 +02:00
|
|
|
use super::{FunctionCx, LocalRef};
|
2018-07-22 01:01:07 +02:00
|
|
|
use super::constant::scalar_to_llvm;
|
2017-12-01 19:16:39 +02:00
|
|
|
use super::place::PlaceRef;
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2015-11-13 00:12:50 +02:00
|
|
|
/// The representation of a Rust value. The enum variant is in fact
|
|
|
|
/// uniquely determined by the value's type, but is kept as a
|
2015-11-10 22:05:11 +02:00
|
|
|
/// safety check.
|
2018-07-10 13:28:39 +03:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum OperandValue<'ll> {
|
2015-11-10 22:05:11 +02:00
|
|
|
/// A reference to the actual operand. The data is guaranteed
|
|
|
|
/// to be valid for the operand's lifetime.
|
2018-07-10 13:28:39 +03:00
|
|
|
Ref(&'ll Value, Align),
|
2018-05-29 00:12:55 +09:00
|
|
|
/// A reference to the unsized operand. The data is guaranteed
|
|
|
|
/// to be valid for the operand's lifetime.
|
|
|
|
/// The second field is the extra.
|
|
|
|
UnsizedRef(&'ll Value, &'ll Value),
|
2015-11-10 22:05:11 +02:00
|
|
|
/// A single LLVM value.
|
2018-07-10 13:28:39 +03:00
|
|
|
Immediate(&'ll Value),
|
2016-05-25 11:55:44 +03:00
|
|
|
/// A pair of immediate LLVM values. Used by fat pointers too.
|
2018-07-10 13:28:39 +03:00
|
|
|
Pair(&'ll Value, &'ll Value)
|
2017-09-20 18:17:23 +03:00
|
|
|
}
|
|
|
|
|
2015-11-13 00:12:50 +02:00
|
|
|
/// An `OperandRef` is an "SSA" reference to a Rust value, along with
|
|
|
|
/// its type.
|
|
|
|
///
|
|
|
|
/// NOTE: unless you know a value's type exactly, you should not
|
|
|
|
/// generate LLVM opcodes acting on it and instead act via methods,
|
2017-06-25 12:41:24 +03:00
|
|
|
/// to avoid nasty edge cases. In particular, using `Builder::store`
|
|
|
|
/// directly is sure to cause problems -- use `OperandRef::store`
|
2016-02-01 11:04:46 +01:00
|
|
|
/// instead.
|
2015-11-03 06:35:09 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2018-07-10 13:28:39 +03:00
|
|
|
pub struct OperandRef<'ll, 'tcx> {
|
2015-11-13 00:12:50 +02:00
|
|
|
// The value.
|
2018-07-10 13:28:39 +03:00
|
|
|
pub val: OperandValue<'ll>,
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2017-09-20 18:17:23 +03:00
|
|
|
// The layout of value, based on its Rust type.
|
2017-09-21 20:40:50 +03:00
|
|
|
pub layout: TyLayout<'tcx>,
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
impl fmt::Debug for OperandRef<'ll, 'tcx> {
|
2016-02-18 19:49:45 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2017-09-20 18:17:23 +03:00
|
|
|
write!(f, "OperandRef({:?} @ {:?})", self.val, self.layout)
|
2015-11-10 22:05:11 +02:00
|
|
|
}
|
2016-02-18 19:49:45 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
impl OperandRef<'ll, 'tcx> {
|
|
|
|
pub fn new_zst(cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
layout: TyLayout<'tcx>) -> OperandRef<'ll, 'tcx> {
|
2017-09-20 18:17:23 +03:00
|
|
|
assert!(layout.is_zst());
|
2017-10-05 04:22:23 +03:00
|
|
|
OperandRef {
|
2018-01-05 07:04:08 +02:00
|
|
|
val: OperandValue::Immediate(C_undef(layout.immediate_llvm_type(cx))),
|
2017-10-05 04:22:23 +03:00
|
|
|
layout
|
|
|
|
}
|
2017-03-08 20:03:04 +02:00
|
|
|
}
|
|
|
|
|
2018-07-02 17:52:53 +03:00
|
|
|
pub fn from_const(bx: &Builder<'a, 'll, 'tcx>,
|
2018-06-25 20:53:02 +02:00
|
|
|
val: &'tcx ty::Const<'tcx>)
|
2018-07-10 13:28:39 +03:00
|
|
|
-> Result<OperandRef<'ll, 'tcx>, Lrc<ConstEvalErr<'tcx>>> {
|
2018-06-25 20:53:02 +02:00
|
|
|
let layout = bx.cx.layout_of(val.ty);
|
2018-01-16 09:31:48 +01:00
|
|
|
|
|
|
|
if layout.is_zst() {
|
2018-01-16 10:16:38 +01:00
|
|
|
return Ok(OperandRef::new_zst(bx.cx, layout));
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-25 20:53:02 +02:00
|
|
|
let val = match val.val {
|
|
|
|
ConstValue::Unevaluated(..) => bug!(),
|
2018-05-20 23:46:30 +02:00
|
|
|
ConstValue::Scalar(x) => {
|
2018-01-16 09:31:48 +01:00
|
|
|
let scalar = match layout.abi {
|
|
|
|
layout::Abi::Scalar(ref x) => x,
|
|
|
|
_ => bug!("from_const: invalid ByVal layout: {:#?}", layout)
|
|
|
|
};
|
2018-05-22 19:30:16 +02:00
|
|
|
let llval = scalar_to_llvm(
|
2018-01-16 10:16:38 +01:00
|
|
|
bx.cx,
|
2018-01-16 09:31:48 +01:00
|
|
|
x,
|
|
|
|
scalar,
|
2018-01-16 10:16:38 +01:00
|
|
|
layout.immediate_llvm_type(bx.cx),
|
2018-01-16 09:31:48 +01:00
|
|
|
);
|
|
|
|
OperandValue::Immediate(llval)
|
|
|
|
},
|
2018-05-20 23:46:30 +02:00
|
|
|
ConstValue::ScalarPair(a, b) => {
|
2018-01-16 09:31:48 +01:00
|
|
|
let (a_scalar, b_scalar) = match layout.abi {
|
|
|
|
layout::Abi::ScalarPair(ref a, ref b) => (a, b),
|
2018-05-20 23:46:30 +02:00
|
|
|
_ => bug!("from_const: invalid ScalarPair layout: {:#?}", layout)
|
2018-01-16 09:31:48 +01:00
|
|
|
};
|
2018-05-22 19:30:16 +02:00
|
|
|
let a_llval = scalar_to_llvm(
|
2018-01-16 10:16:38 +01:00
|
|
|
bx.cx,
|
2018-01-16 09:31:48 +01:00
|
|
|
a,
|
|
|
|
a_scalar,
|
Store scalar pair bools as i8 in memory
We represent `bool` as `i1` in a `ScalarPair`, unlike other aggregates,
to optimize IR for checked operators and the like. With this patch, we
still do so when the pair is an immediate value, but we use the `i8`
memory type when the value is loaded or stored as an LLVM aggregate.
So `(bool, bool)` looks like an `{ i1, i1 }` immediate, but `{ i8, i8 }`
in memory. When a pair is a direct function argument, `PassMode::Pair`,
it is still passed using the immediate `i1` type, but as a return value
it will use the `i8` memory type. Also, `bool`-like` enum tags will now
use scalar pairs when possible, where they were previously excluded due
to optimization issues.
2018-06-15 15:47:54 -07:00
|
|
|
layout.scalar_pair_element_llvm_type(bx.cx, 0, true),
|
2018-01-16 09:31:48 +01:00
|
|
|
);
|
2018-08-02 13:16:53 +02:00
|
|
|
let b_layout = layout.scalar_pair_element_llvm_type(bx.cx, 1, true);
|
|
|
|
let b_llval = match b {
|
|
|
|
ScalarMaybeUndef::Scalar(b) => scalar_to_llvm(
|
|
|
|
bx.cx,
|
|
|
|
b,
|
|
|
|
b_scalar,
|
|
|
|
b_layout,
|
|
|
|
),
|
|
|
|
ScalarMaybeUndef::Undef => C_undef(b_layout),
|
|
|
|
};
|
2018-01-16 09:31:48 +01:00
|
|
|
OperandValue::Pair(a_llval, b_llval)
|
|
|
|
},
|
2018-05-14 18:54:24 +02:00
|
|
|
ConstValue::ByRef(alloc, offset) => {
|
2018-07-22 01:01:07 +02:00
|
|
|
return Ok(PlaceRef::from_const_alloc(bx, layout, alloc, offset).load(bx));
|
2018-01-16 09:31:48 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(OperandRef {
|
|
|
|
val,
|
|
|
|
layout
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-18 19:49:45 +02:00
|
|
|
/// Asserts that this operand refers to a scalar and returns
|
|
|
|
/// a reference to its value.
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn immediate(self) -> &'ll Value {
|
2016-02-18 19:49:45 +02:00
|
|
|
match self.val {
|
|
|
|
OperandValue::Immediate(s) => s,
|
2016-12-31 04:55:29 +02:00
|
|
|
_ => bug!("not immediate: {:?}", self)
|
2016-02-18 19:49:45 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-25 11:55:44 +03:00
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn deref(self, cx: &CodegenCx<'ll, 'tcx>) -> PlaceRef<'ll, 'tcx> {
|
2018-01-28 23:29:40 +02:00
|
|
|
let projected_ty = self.layout.ty.builtin_deref(true)
|
2017-05-18 18:43:52 +03:00
|
|
|
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", self)).ty;
|
2017-03-14 01:08:21 +02:00
|
|
|
let (llptr, llextra) = match self.val {
|
2018-07-10 13:28:39 +03:00
|
|
|
OperandValue::Immediate(llptr) => (llptr, None),
|
|
|
|
OperandValue::Pair(llptr, llextra) => (llptr, Some(llextra)),
|
2018-05-29 00:12:55 +09:00
|
|
|
OperandValue::Ref(..) |
|
|
|
|
OperandValue::UnsizedRef(..) => bug!("Deref of by-Ref operand {:?}", self)
|
2017-03-14 01:08:21 +02:00
|
|
|
};
|
2018-01-05 07:04:08 +02:00
|
|
|
let layout = cx.layout_of(projected_ty);
|
2017-12-01 14:31:47 +02:00
|
|
|
PlaceRef {
|
2017-03-14 01:08:21 +02:00
|
|
|
llval: llptr,
|
2017-08-06 22:54:09 -07:00
|
|
|
llextra,
|
2017-12-01 19:16:39 +02:00
|
|
|
layout,
|
|
|
|
align: layout.align,
|
2017-03-14 01:08:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 04:22:23 +03:00
|
|
|
/// If this operand is a `Pair`, we return an aggregate with the two values.
|
|
|
|
/// For other cases, see `immediate`.
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn immediate_or_packed_pair(self, bx: &Builder<'a, 'll, 'tcx>) -> &'ll Value {
|
2016-05-25 11:55:44 +03:00
|
|
|
if let OperandValue::Pair(a, b) = self.val {
|
2018-01-05 07:12:32 +02:00
|
|
|
let llty = self.layout.llvm_type(bx.cx);
|
2017-10-05 04:22:23 +03:00
|
|
|
debug!("Operand::immediate_or_packed_pair: packing {:?} into {:?}",
|
|
|
|
self, llty);
|
2017-09-15 22:42:23 +03:00
|
|
|
// Reconstruct the immediate aggregate.
|
2017-06-18 16:59:51 +03:00
|
|
|
let mut llpair = C_undef(llty);
|
Store scalar pair bools as i8 in memory
We represent `bool` as `i1` in a `ScalarPair`, unlike other aggregates,
to optimize IR for checked operators and the like. With this patch, we
still do so when the pair is an immediate value, but we use the `i8`
memory type when the value is loaded or stored as an LLVM aggregate.
So `(bool, bool)` looks like an `{ i1, i1 }` immediate, but `{ i8, i8 }`
in memory. When a pair is a direct function argument, `PassMode::Pair`,
it is still passed using the immediate `i1` type, but as a return value
it will use the `i8` memory type. Also, `bool`-like` enum tags will now
use scalar pairs when possible, where they were previously excluded due
to optimization issues.
2018-06-15 15:47:54 -07:00
|
|
|
llpair = bx.insert_value(llpair, base::from_immediate(bx, a), 0);
|
|
|
|
llpair = bx.insert_value(llpair, base::from_immediate(bx, b), 1);
|
2017-10-05 04:22:23 +03:00
|
|
|
llpair
|
|
|
|
} else {
|
|
|
|
self.immediate()
|
2016-05-25 11:55:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 04:22:23 +03:00
|
|
|
/// If the type is a pair, we return a `Pair`, otherwise, an `Immediate`.
|
2018-07-02 17:52:53 +03:00
|
|
|
pub fn from_immediate_or_packed_pair(bx: &Builder<'a, 'll, 'tcx>,
|
2018-07-10 13:28:39 +03:00
|
|
|
llval: &'ll Value,
|
2017-10-05 04:22:23 +03:00
|
|
|
layout: TyLayout<'tcx>)
|
2018-07-10 13:28:39 +03:00
|
|
|
-> OperandRef<'ll, 'tcx> {
|
Store scalar pair bools as i8 in memory
We represent `bool` as `i1` in a `ScalarPair`, unlike other aggregates,
to optimize IR for checked operators and the like. With this patch, we
still do so when the pair is an immediate value, but we use the `i8`
memory type when the value is loaded or stored as an LLVM aggregate.
So `(bool, bool)` looks like an `{ i1, i1 }` immediate, but `{ i8, i8 }`
in memory. When a pair is a direct function argument, `PassMode::Pair`,
it is still passed using the immediate `i1` type, but as a return value
it will use the `i8` memory type. Also, `bool`-like` enum tags will now
use scalar pairs when possible, where they were previously excluded due
to optimization issues.
2018-06-15 15:47:54 -07:00
|
|
|
let val = if let layout::Abi::ScalarPair(ref a, ref b) = layout.abi {
|
2017-10-05 04:22:23 +03:00
|
|
|
debug!("Operand::from_immediate_or_packed_pair: unpacking {:?} @ {:?}",
|
|
|
|
llval, layout);
|
2016-06-20 23:55:14 +03:00
|
|
|
|
2017-10-05 04:22:23 +03:00
|
|
|
// Deconstruct the immediate aggregate.
|
Store scalar pair bools as i8 in memory
We represent `bool` as `i1` in a `ScalarPair`, unlike other aggregates,
to optimize IR for checked operators and the like. With this patch, we
still do so when the pair is an immediate value, but we use the `i8`
memory type when the value is loaded or stored as an LLVM aggregate.
So `(bool, bool)` looks like an `{ i1, i1 }` immediate, but `{ i8, i8 }`
in memory. When a pair is a direct function argument, `PassMode::Pair`,
it is still passed using the immediate `i1` type, but as a return value
it will use the `i8` memory type. Also, `bool`-like` enum tags will now
use scalar pairs when possible, where they were previously excluded due
to optimization issues.
2018-06-15 15:47:54 -07:00
|
|
|
let a_llval = base::to_immediate_scalar(bx, bx.extract_value(llval, 0), a);
|
|
|
|
let b_llval = base::to_immediate_scalar(bx, bx.extract_value(llval, 1), b);
|
|
|
|
OperandValue::Pair(a_llval, b_llval)
|
2017-10-05 04:22:23 +03:00
|
|
|
} else {
|
|
|
|
OperandValue::Immediate(llval)
|
|
|
|
};
|
|
|
|
OperandRef { val, layout }
|
2016-05-25 11:55:44 +03:00
|
|
|
}
|
2017-10-09 19:56:41 +03:00
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn extract_field(&self, bx: &Builder<'a, 'll, 'tcx>, i: usize) -> OperandRef<'ll, 'tcx> {
|
2018-01-05 07:12:32 +02:00
|
|
|
let field = self.layout.field(bx.cx, i);
|
2017-10-09 19:56:41 +03:00
|
|
|
let offset = self.layout.fields.offset(i);
|
|
|
|
|
|
|
|
let mut val = match (self.val, &self.layout.abi) {
|
2018-05-10 19:24:06 +03:00
|
|
|
// If the field is ZST, it has no data.
|
|
|
|
_ if field.is_zst() => {
|
|
|
|
return OperandRef::new_zst(bx.cx, field);
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
|
|
|
|
2017-12-13 01:57:56 +02:00
|
|
|
// Newtype of a scalar, scalar pair or vector.
|
2017-10-09 19:56:41 +03:00
|
|
|
(OperandValue::Immediate(_), _) |
|
|
|
|
(OperandValue::Pair(..), _) if field.size == self.layout.size => {
|
|
|
|
assert_eq!(offset.bytes(), 0);
|
|
|
|
self.val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract a scalar component from a pair.
|
|
|
|
(OperandValue::Pair(a_llval, b_llval), &layout::Abi::ScalarPair(ref a, ref b)) => {
|
|
|
|
if offset.bytes() == 0 {
|
2018-01-05 07:12:32 +02:00
|
|
|
assert_eq!(field.size, a.value.size(bx.cx));
|
2017-10-09 19:56:41 +03:00
|
|
|
OperandValue::Immediate(a_llval)
|
|
|
|
} else {
|
2018-01-05 07:12:32 +02:00
|
|
|
assert_eq!(offset, a.value.size(bx.cx)
|
|
|
|
.abi_align(b.value.align(bx.cx)));
|
|
|
|
assert_eq!(field.size, b.value.size(bx.cx));
|
2017-10-09 19:56:41 +03:00
|
|
|
OperandValue::Immediate(b_llval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `#[repr(simd)]` types are also immediate.
|
2017-12-13 01:57:56 +02:00
|
|
|
(OperandValue::Immediate(llval), &layout::Abi::Vector { .. }) => {
|
2017-10-09 19:56:41 +03:00
|
|
|
OperandValue::Immediate(
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.extract_element(llval, C_usize(bx.cx, i as u64)))
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => bug!("OperandRef::extract_field({:?}): not applicable", self)
|
|
|
|
};
|
|
|
|
|
|
|
|
// HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
|
|
|
|
match val {
|
|
|
|
OperandValue::Immediate(ref mut llval) => {
|
2018-01-05 07:12:32 +02:00
|
|
|
*llval = bx.bitcast(*llval, field.immediate_llvm_type(bx.cx));
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
|
|
|
OperandValue::Pair(ref mut a, ref mut b) => {
|
Store scalar pair bools as i8 in memory
We represent `bool` as `i1` in a `ScalarPair`, unlike other aggregates,
to optimize IR for checked operators and the like. With this patch, we
still do so when the pair is an immediate value, but we use the `i8`
memory type when the value is loaded or stored as an LLVM aggregate.
So `(bool, bool)` looks like an `{ i1, i1 }` immediate, but `{ i8, i8 }`
in memory. When a pair is a direct function argument, `PassMode::Pair`,
it is still passed using the immediate `i1` type, but as a return value
it will use the `i8` memory type. Also, `bool`-like` enum tags will now
use scalar pairs when possible, where they were previously excluded due
to optimization issues.
2018-06-15 15:47:54 -07:00
|
|
|
*a = bx.bitcast(*a, field.scalar_pair_element_llvm_type(bx.cx, 0, true));
|
|
|
|
*b = bx.bitcast(*b, field.scalar_pair_element_llvm_type(bx.cx, 1, true));
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
2018-05-29 00:12:55 +09:00
|
|
|
OperandValue::Ref(..) |
|
|
|
|
OperandValue::UnsizedRef(..) => bug!()
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
OperandRef {
|
|
|
|
val,
|
|
|
|
layout: field
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
impl OperandValue<'ll> {
|
|
|
|
pub fn store(self, bx: &Builder<'a, 'll, 'tcx>, dest: PlaceRef<'ll, 'tcx>) {
|
2018-05-11 12:26:32 +02:00
|
|
|
self.store_with_flags(bx, dest, MemFlags::empty());
|
2018-05-11 11:26:51 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn volatile_store(self, bx: &Builder<'a, 'll, 'tcx>, dest: PlaceRef<'ll, 'tcx>) {
|
2018-05-11 12:26:32 +02:00
|
|
|
self.store_with_flags(bx, dest, MemFlags::VOLATILE);
|
2018-05-11 11:26:51 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn unaligned_volatile_store(self, bx: &Builder<'a, 'll, 'tcx>, dest: PlaceRef<'ll, 'tcx>) {
|
2018-07-14 23:28:39 +01:00
|
|
|
self.store_with_flags(bx, dest, MemFlags::VOLATILE | MemFlags::UNALIGNED);
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn nontemporal_store(self, bx: &Builder<'a, 'll, 'tcx>, dest: PlaceRef<'ll, 'tcx>) {
|
2018-05-11 12:26:32 +02:00
|
|
|
self.store_with_flags(bx, dest, MemFlags::NONTEMPORAL);
|
|
|
|
}
|
|
|
|
|
2018-07-17 18:26:58 +03:00
|
|
|
fn store_with_flags(
|
|
|
|
self,
|
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
|
|
|
dest: PlaceRef<'ll, 'tcx>,
|
|
|
|
flags: MemFlags,
|
|
|
|
) {
|
2017-06-25 12:41:24 +03:00
|
|
|
debug!("OperandRef::store: operand={:?}, dest={:?}", self, dest);
|
|
|
|
// Avoid generating stores of zero-sized values, because the only way to have a zero-sized
|
|
|
|
// value is through `undef`, and store itself is useless.
|
2017-09-20 18:17:23 +03:00
|
|
|
if dest.layout.is_zst() {
|
2017-06-25 12:41:24 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
match self {
|
2018-05-11 12:26:32 +02:00
|
|
|
OperandValue::Ref(r, source_align) => {
|
2018-01-05 07:12:32 +02:00
|
|
|
base::memcpy_ty(bx, dest.llval, r, dest.layout,
|
2018-05-11 12:26:32 +02:00
|
|
|
source_align.min(dest.align), flags)
|
|
|
|
}
|
2018-05-29 00:12:55 +09:00
|
|
|
OperandValue::UnsizedRef(..) => {
|
|
|
|
bug!("cannot directly store unsized values");
|
|
|
|
}
|
2017-06-25 12:41:24 +03:00
|
|
|
OperandValue::Immediate(s) => {
|
2018-05-11 11:26:51 +02:00
|
|
|
let val = base::from_immediate(bx, s);
|
2018-05-11 12:26:32 +02:00
|
|
|
bx.store_with_flags(val, dest.llval, dest.align, flags);
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
|
|
|
OperandValue::Pair(a, b) => {
|
2017-10-05 04:22:23 +03:00
|
|
|
for (i, &x) in [a, b].iter().enumerate() {
|
Store scalar pair bools as i8 in memory
We represent `bool` as `i1` in a `ScalarPair`, unlike other aggregates,
to optimize IR for checked operators and the like. With this patch, we
still do so when the pair is an immediate value, but we use the `i8`
memory type when the value is loaded or stored as an LLVM aggregate.
So `(bool, bool)` looks like an `{ i1, i1 }` immediate, but `{ i8, i8 }`
in memory. When a pair is a direct function argument, `PassMode::Pair`,
it is still passed using the immediate `i1` type, but as a return value
it will use the `i8` memory type. Also, `bool`-like` enum tags will now
use scalar pairs when possible, where they were previously excluded due
to optimization issues.
2018-06-15 15:47:54 -07:00
|
|
|
let llptr = bx.struct_gep(dest.llval, i as u64);
|
2018-05-11 11:26:51 +02:00
|
|
|
let val = base::from_immediate(bx, x);
|
2018-05-11 12:26:32 +02:00
|
|
|
bx.store_with_flags(val, llptr, dest.align, flags);
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
}
|
2018-05-29 00:12:55 +09:00
|
|
|
|
|
|
|
pub fn store_unsized(self, bx: &Builder<'a, 'll, 'tcx>, indirect_dest: PlaceRef<'ll, 'tcx>) {
|
|
|
|
debug!("OperandRef::store_unsized: operand={:?}, indirect_dest={:?}", self, indirect_dest);
|
|
|
|
let flags = MemFlags::empty();
|
|
|
|
|
|
|
|
// `indirect_dest` must have `*mut T` type. We extract `T` out of it.
|
|
|
|
let unsized_ty = indirect_dest.layout.ty.builtin_deref(true)
|
|
|
|
.unwrap_or_else(|| bug!("indirect_dest has non-pointer type: {:?}", indirect_dest)).ty;
|
|
|
|
|
|
|
|
let (llptr, llextra) =
|
|
|
|
if let OperandValue::UnsizedRef(llptr, llextra) = self {
|
|
|
|
(llptr, llextra)
|
|
|
|
} else {
|
|
|
|
bug!("store_unsized called with a sized value")
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: choose an appropriate alignment, or use dynamic align somehow
|
|
|
|
let max_align = Align::from_bits(128, 128).unwrap();
|
|
|
|
let min_align = Align::from_bits(8, 8).unwrap();
|
|
|
|
|
|
|
|
// Allocate an appropriate region on the stack, and copy the value into it
|
|
|
|
let (llsize, _) = glue::size_and_align_of_dst(&bx, unsized_ty, Some(llextra));
|
|
|
|
let lldst = bx.array_alloca(Type::i8(bx.cx), llsize, "unsized_tmp", max_align);
|
|
|
|
base::call_memcpy(&bx, lldst, llptr, llsize, min_align, flags);
|
|
|
|
|
|
|
|
// Store the allocated region and the extra to the indirect place.
|
|
|
|
let indirect_operand = OperandValue::Pair(lldst, llextra);
|
|
|
|
indirect_operand.store(&bx, indirect_dest);
|
|
|
|
}
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
|
2018-07-02 17:52:53 +03:00
|
|
|
impl FunctionCx<'a, 'll, 'tcx> {
|
2018-05-08 16:10:16 +03:00
|
|
|
fn maybe_codegen_consume_direct(&mut self,
|
2018-07-02 17:52:53 +03:00
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
2017-12-01 14:39:51 +02:00
|
|
|
place: &mir::Place<'tcx>)
|
2018-07-10 13:28:39 +03:00
|
|
|
-> Option<OperandRef<'ll, 'tcx>>
|
2015-10-21 17:42:25 -04:00
|
|
|
{
|
2018-05-08 16:10:16 +03:00
|
|
|
debug!("maybe_codegen_consume_direct(place={:?})", place);
|
2016-06-09 18:13:16 +03:00
|
|
|
|
2016-06-20 23:55:14 +03:00
|
|
|
// watch out for locals that do not have an
|
2016-06-09 18:13:16 +03:00
|
|
|
// alloca; they are handled somewhat differently
|
2017-12-01 14:39:51 +02:00
|
|
|
if let mir::Place::Local(index) = *place {
|
2016-06-20 23:55:14 +03:00
|
|
|
match self.locals[index] {
|
|
|
|
LocalRef::Operand(Some(o)) => {
|
2017-10-09 00:38:10 +03:00
|
|
|
return Some(o);
|
2016-06-09 18:13:16 +03:00
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Operand(None) => {
|
2017-12-01 14:39:51 +02:00
|
|
|
bug!("use of {:?} before def", place);
|
2015-11-03 06:35:09 -05:00
|
|
|
}
|
2018-05-29 00:12:55 +09:00
|
|
|
LocalRef::Place(..) | LocalRef::UnsizedPlace(..) => {
|
2016-06-09 18:13:16 +03:00
|
|
|
// use path below
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 06:35:09 -05:00
|
|
|
|
2017-10-09 19:56:41 +03:00
|
|
|
// Moves out of scalar and scalar pair fields are trivial.
|
2017-12-01 14:39:51 +02:00
|
|
|
if let &mir::Place::Projection(ref proj) = place {
|
2018-05-08 16:10:16 +03:00
|
|
|
if let Some(o) = self.maybe_codegen_consume_direct(bx, &proj.base) {
|
2017-12-26 03:20:35 +02:00
|
|
|
match proj.elem {
|
|
|
|
mir::ProjectionElem::Field(ref f, _) => {
|
2018-01-05 07:12:32 +02:00
|
|
|
return Some(o.extract_field(bx, f.index()));
|
2017-12-26 03:20:35 +02:00
|
|
|
}
|
|
|
|
mir::ProjectionElem::Index(_) |
|
|
|
|
mir::ProjectionElem::ConstantIndex { .. } => {
|
|
|
|
// ZSTs don't require any actual memory access.
|
|
|
|
// FIXME(eddyb) deduplicate this with the identical
|
2018-05-08 16:10:16 +03:00
|
|
|
// checks in `codegen_consume` and `extract_field`.
|
2018-01-05 07:12:32 +02:00
|
|
|
let elem = o.layout.field(bx.cx, 0);
|
2017-12-26 03:20:35 +02:00
|
|
|
if elem.is_zst() {
|
2018-01-05 07:12:32 +02:00
|
|
|
return Some(OperandRef::new_zst(bx.cx, elem));
|
2017-12-26 03:20:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2016-05-25 11:58:08 +03:00
|
|
|
}
|
2016-06-09 18:13:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 00:38:10 +03:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
pub fn codegen_consume(&mut self,
|
2018-07-02 17:52:53 +03:00
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
2017-12-01 14:39:51 +02:00
|
|
|
place: &mir::Place<'tcx>)
|
2018-07-10 13:28:39 +03:00
|
|
|
-> OperandRef<'ll, 'tcx>
|
2017-10-09 00:38:10 +03:00
|
|
|
{
|
2018-05-08 16:10:16 +03:00
|
|
|
debug!("codegen_consume(place={:?})", place);
|
2017-10-09 00:38:10 +03:00
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
let ty = self.monomorphized_place_ty(place);
|
2018-01-05 07:12:32 +02:00
|
|
|
let layout = bx.cx.layout_of(ty);
|
2017-10-09 00:38:10 +03:00
|
|
|
|
|
|
|
// ZSTs don't require any actual memory access.
|
|
|
|
if layout.is_zst() {
|
2018-01-05 07:12:32 +02:00
|
|
|
return OperandRef::new_zst(bx.cx, layout);
|
2017-10-09 00:38:10 +03:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
if let Some(o) = self.maybe_codegen_consume_direct(bx, place) {
|
2017-10-09 00:38:10 +03:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
// for most places, to consume them we just load them
|
2016-06-09 18:13:16 +03:00
|
|
|
// out from their home
|
2018-05-08 16:10:16 +03:00
|
|
|
self.codegen_place(bx, place).load(bx)
|
2016-06-09 18:13:16 +03:00
|
|
|
}
|
2016-05-25 11:58:08 +03:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
pub fn codegen_operand(&mut self,
|
2018-07-02 17:52:53 +03:00
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
2016-06-09 18:13:16 +03:00
|
|
|
operand: &mir::Operand<'tcx>)
|
2018-07-10 13:28:39 +03:00
|
|
|
-> OperandRef<'ll, 'tcx>
|
2016-06-09 18:13:16 +03:00
|
|
|
{
|
2018-05-08 16:10:16 +03:00
|
|
|
debug!("codegen_operand(operand={:?})", operand);
|
2016-06-09 18:13:16 +03:00
|
|
|
|
|
|
|
match *operand {
|
2017-12-01 14:39:51 +02:00
|
|
|
mir::Operand::Copy(ref place) |
|
|
|
|
mir::Operand::Move(ref place) => {
|
2018-05-08 16:10:16 +03:00
|
|
|
self.codegen_consume(bx, place)
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::Operand::Constant(ref constant) => {
|
2018-01-16 09:31:48 +01:00
|
|
|
let ty = self.monomorphize(&constant.ty);
|
2018-06-25 20:53:02 +02:00
|
|
|
self.eval_mir_constant(bx, constant)
|
|
|
|
.and_then(|c| OperandRef::from_const(bx, c))
|
2018-01-16 09:31:48 +01:00
|
|
|
.unwrap_or_else(|err| {
|
2018-07-22 01:01:07 +02:00
|
|
|
err.report_as_error(
|
|
|
|
bx.tcx().at(constant.span),
|
|
|
|
"could not evaluate constant operand",
|
|
|
|
);
|
|
|
|
// Allow RalfJ to sleep soundly knowing that even refactorings that remove
|
|
|
|
// the above error (or silence it under some conditions) will not cause UB
|
2018-07-20 18:43:46 +02:00
|
|
|
let fnname = bx.cx.get_intrinsic(&("llvm.trap"));
|
|
|
|
bx.call(fnname, &[], None);
|
2018-01-16 09:31:48 +01:00
|
|
|
// We've errored, so we don't have to produce working code.
|
|
|
|
let layout = bx.cx.layout_of(ty);
|
|
|
|
PlaceRef::new_sized(
|
2018-07-20 18:43:46 +02:00
|
|
|
C_undef(layout.llvm_type(bx.cx).ptr_to()),
|
2018-01-16 09:31:48 +01:00
|
|
|
layout,
|
|
|
|
layout.align,
|
|
|
|
).load(bx)
|
|
|
|
})
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|