2019-05-17 02:20:14 +01:00
|
|
|
|
use super::operand::{OperandRef, OperandValue};
|
|
|
|
|
use super::place::PlaceRef;
|
|
|
|
|
use super::{FunctionCx, LocalRef};
|
|
|
|
|
|
|
|
|
|
use crate::base;
|
2021-12-30 01:18:44 +00:00
|
|
|
|
use crate::common::{self, IntPredicate};
|
2019-05-17 02:20:14 +01:00
|
|
|
|
use crate::traits::*;
|
|
|
|
|
use crate::MemFlags;
|
|
|
|
|
|
2020-03-29 16:41:09 +02:00
|
|
|
|
use rustc_middle::mir;
|
2022-06-13 16:37:41 +03:00
|
|
|
|
use rustc_middle::mir::Operand;
|
2020-03-29 16:41:09 +02:00
|
|
|
|
use rustc_middle::ty::cast::{CastTy, IntTy};
|
2023-04-01 01:46:36 -07:00
|
|
|
|
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
|
2020-03-29 16:41:09 +02:00
|
|
|
|
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, Ty, TyCtxt};
|
2023-04-06 00:36:36 -07:00
|
|
|
|
use rustc_session::config::OptLevel;
|
2020-01-01 19:25:28 +01:00
|
|
|
|
use rustc_span::source_map::{Span, DUMMY_SP};
|
2023-03-25 18:43:03 -07:00
|
|
|
|
use rustc_target::abi::{self, FIRST_VARIANT};
|
2015-10-21 17:42:25 -04:00
|
|
|
|
|
2019-10-26 01:41:17 -04:00
|
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2022-07-05 09:27:06 +00:00
|
|
|
|
#[instrument(level = "trace", skip(self, bx))]
|
2018-09-20 15:47:22 +02:00
|
|
|
|
pub fn codegen_rvalue(
|
|
|
|
|
&mut self,
|
2022-11-09 11:04:10 +11:00
|
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
|
dest: PlaceRef<'tcx, Bx::Value>,
|
|
|
|
|
rvalue: &mir::Rvalue<'tcx>,
|
2022-11-09 11:04:10 +11:00
|
|
|
|
) {
|
2015-10-21 17:42:25 -04:00
|
|
|
|
match *rvalue {
|
2016-02-04 19:40:28 +02:00
|
|
|
|
mir::Rvalue::Use(ref operand) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let cg_operand = self.codegen_operand(bx, operand);
|
2019-05-17 02:20:14 +01:00
|
|
|
|
// FIXME: consider not copying constants through stack. (Fixable by codegen'ing
|
|
|
|
|
// constants into `OperandValue::Ref`; why don’t we do that yet if we don’t?)
|
2022-11-09 11:04:10 +11:00
|
|
|
|
cg_operand.val.store(bx, dest);
|
2016-02-04 19:40:28 +02:00
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
|
2019-04-15 19:20:44 +05:30
|
|
|
|
mir::Rvalue::Cast(mir::CastKind::Pointer(PointerCast::Unsize), ref source, _) => {
|
2017-10-10 22:04:13 +03:00
|
|
|
|
// The destination necessarily contains a fat pointer, so if
|
|
|
|
|
// it's a scalar pair, it's a fat pointer or newtype thereof.
|
2018-10-03 13:49:57 +02:00
|
|
|
|
if bx.cx().is_backend_scalar_pair(dest.layout) {
|
2019-05-17 02:20:14 +01:00
|
|
|
|
// Into-coerce of a thin pointer to a fat pointer -- just
|
2015-11-13 00:12:50 +02:00
|
|
|
|
// use the operand path.
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let temp = self.codegen_rvalue_operand(bx, rvalue);
|
|
|
|
|
temp.val.store(bx, dest);
|
|
|
|
|
return;
|
2015-11-09 02:16:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 22:02:51 +02:00
|
|
|
|
// Unsize of a nontrivial struct. I would prefer for
|
2018-05-08 16:10:16 +03:00
|
|
|
|
// this to be eliminated by MIR building, but
|
2015-11-11 22:02:51 +02:00
|
|
|
|
// `CoerceUnsized` can be passed by a where-clause,
|
|
|
|
|
// so the (generic) MIR may not be able to expand it.
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let operand = self.codegen_operand(bx, source);
|
2017-06-25 12:41:24 +03:00
|
|
|
|
match operand.val {
|
|
|
|
|
OperandValue::Pair(..) | OperandValue::Immediate(_) => {
|
2019-05-17 02:20:14 +01:00
|
|
|
|
// Unsize from an immediate structure. We don't
|
2016-12-10 20:32:44 -07:00
|
|
|
|
// really need a temporary alloca here, but
|
|
|
|
|
// avoiding it would require us to have
|
2019-05-17 02:20:14 +01:00
|
|
|
|
// `coerce_unsized_into` use `extractvalue` to
|
2016-12-10 20:32:44 -07:00
|
|
|
|
// index into the struct, and this case isn't
|
|
|
|
|
// important enough for it.
|
2018-05-08 16:10:16 +03:00
|
|
|
|
debug!("codegen_rvalue: creating ugly alloca");
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let scratch = PlaceRef::alloca(bx, operand.layout);
|
|
|
|
|
scratch.storage_live(bx);
|
|
|
|
|
operand.val.store(bx, scratch);
|
|
|
|
|
base::coerce_unsized_into(bx, scratch, dest);
|
|
|
|
|
scratch.storage_dead(bx);
|
2017-02-06 17:27:09 +01:00
|
|
|
|
}
|
2018-08-03 23:50:13 +09:00
|
|
|
|
OperandValue::Ref(llref, None, align) => {
|
2019-08-29 14:24:50 -04:00
|
|
|
|
let source = PlaceRef::new_sized_aligned(llref, operand.layout, align);
|
2022-11-09 11:04:10 +11:00
|
|
|
|
base::coerce_unsized_into(bx, source, dest);
|
2015-11-11 22:02:51 +02:00
|
|
|
|
}
|
2018-08-03 23:50:13 +09:00
|
|
|
|
OperandValue::Ref(_, Some(_), _) => {
|
2019-05-17 02:20:14 +01:00
|
|
|
|
bug!("unsized coercion on an unsized rvalue");
|
2018-05-29 00:12:55 +09:00
|
|
|
|
}
|
2017-06-25 12:41:24 +03:00
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 18:32:52 -08:00
|
|
|
|
mir::Rvalue::Cast(mir::CastKind::Transmute, ref operand, _ty) => {
|
|
|
|
|
let src = self.codegen_operand(bx, operand);
|
|
|
|
|
self.codegen_transmute(bx, src, dest);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-05 12:27:28 +03:00
|
|
|
|
mir::Rvalue::Repeat(ref elem, count) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let cg_elem = self.codegen_operand(bx, elem);
|
2017-07-26 16:27:25 +02:00
|
|
|
|
|
2017-06-25 12:41:24 +03:00
|
|
|
|
// Do not generate the loop for zero-sized elements or empty arrays.
|
2017-09-20 18:17:23 +03:00
|
|
|
|
if dest.layout.is_zst() {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
return;
|
2017-07-26 16:27:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
|
if let OperandValue::Immediate(v) = cg_elem.val {
|
2018-12-08 11:48:43 +01:00
|
|
|
|
let zero = bx.const_usize(0);
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let start = dest.project_index(bx, zero).llval;
|
2018-12-08 11:48:43 +01:00
|
|
|
|
let size = bx.const_usize(dest.layout.size.bytes());
|
2017-06-01 21:50:53 +03:00
|
|
|
|
|
2017-08-02 00:32:14 +02:00
|
|
|
|
// Use llvm.memset.p0i8.* to initialize all zero arrays
|
2022-09-09 00:00:00 +00:00
|
|
|
|
if bx.cx().const_to_opt_u128(v, false) == Some(0) {
|
2018-09-06 11:57:42 -07:00
|
|
|
|
let fill = bx.cx().const_u8(0);
|
2018-09-10 17:59:20 +02:00
|
|
|
|
bx.memset(start, fill, size, dest.align, MemFlags::empty());
|
2022-11-09 11:04:10 +11:00
|
|
|
|
return;
|
2017-07-26 16:27:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-02 00:32:14 +02:00
|
|
|
|
// Use llvm.memset.p0i8.* to initialize byte arrays
|
2020-08-29 18:10:01 +02:00
|
|
|
|
let v = bx.from_immediate(v);
|
2018-09-06 13:52:15 -07:00
|
|
|
|
if bx.cx().val_ty(v) == bx.cx().type_i8() {
|
2018-09-10 17:59:20 +02:00
|
|
|
|
bx.memset(start, v, size, dest.align, MemFlags::empty());
|
2022-11-09 11:04:10 +11:00
|
|
|
|
return;
|
2017-07-26 16:27:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 08:51:19 +00:00
|
|
|
|
let count = self
|
|
|
|
|
.monomorphize(count)
|
|
|
|
|
.eval_target_usize(bx.cx().tcx(), ty::ParamEnv::reveal_all());
|
2020-03-14 15:30:35 +01:00
|
|
|
|
|
2022-11-09 11:04:10 +11:00
|
|
|
|
bx.write_operand_repeatedly(cg_elem, count, dest);
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-25 01:02:34 +02:00
|
|
|
|
mir::Rvalue::Aggregate(ref kind, ref operands) => {
|
2023-01-29 13:00:32 +00:00
|
|
|
|
let (variant_index, variant_dest, active_field_index) = match **kind {
|
|
|
|
|
mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
|
|
|
|
|
let variant_dest = dest.project_downcast(bx, variant_index);
|
|
|
|
|
(variant_index, variant_dest, active_field_index)
|
2023-01-23 22:15:55 +00:00
|
|
|
|
}
|
2023-03-25 18:43:03 -07:00
|
|
|
|
_ => (FIRST_VARIANT, dest, None),
|
2017-06-25 12:41:24 +03:00
|
|
|
|
};
|
2023-01-29 13:00:32 +00:00
|
|
|
|
if active_field_index.is_some() {
|
|
|
|
|
assert_eq!(operands.len(), 1);
|
|
|
|
|
}
|
2023-04-01 20:11:38 -07:00
|
|
|
|
for (i, operand) in operands.iter_enumerated() {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let op = self.codegen_operand(bx, operand);
|
2017-06-25 12:41:24 +03:00
|
|
|
|
// Do not generate stores and GEPis for zero-sized fields.
|
2017-09-20 18:17:23 +03:00
|
|
|
|
if !op.layout.is_zst() {
|
2017-06-25 12:41:24 +03:00
|
|
|
|
let field_index = active_field_index.unwrap_or(i);
|
2022-06-29 14:58:03 +00:00
|
|
|
|
let field = if let mir::AggregateKind::Array(_) = **kind {
|
2023-04-01 20:11:38 -07:00
|
|
|
|
let llindex = bx.cx().const_usize(field_index.as_u32().into());
|
2023-01-29 13:00:32 +00:00
|
|
|
|
variant_dest.project_index(bx, llindex)
|
2022-06-29 14:58:03 +00:00
|
|
|
|
} else {
|
2023-04-01 20:11:38 -07:00
|
|
|
|
variant_dest.project_field(bx, field_index.as_usize())
|
2022-06-29 14:58:03 +00:00
|
|
|
|
};
|
2022-11-09 11:04:10 +11:00
|
|
|
|
op.val.store(bx, field);
|
2017-06-25 12:41:24 +03:00
|
|
|
|
}
|
2015-11-02 09:39:59 -05:00
|
|
|
|
}
|
2023-01-29 13:00:32 +00:00
|
|
|
|
dest.codegen_set_discr(bx, variant_index);
|
2015-11-02 09:39:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ => {
|
2019-10-14 01:38:38 -04:00
|
|
|
|
assert!(self.rvalue_creates_operand(rvalue, DUMMY_SP));
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let temp = self.codegen_rvalue_operand(bx, rvalue);
|
|
|
|
|
temp.val.store(bx, dest);
|
2015-11-02 09:39:59 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 18:32:52 -08:00
|
|
|
|
fn codegen_transmute(
|
|
|
|
|
&mut self,
|
|
|
|
|
bx: &mut Bx,
|
|
|
|
|
src: OperandRef<'tcx, Bx::Value>,
|
|
|
|
|
dst: PlaceRef<'tcx, Bx::Value>,
|
|
|
|
|
) {
|
|
|
|
|
// The MIR validator enforces no unsized transmutes.
|
|
|
|
|
debug_assert!(src.layout.is_sized());
|
|
|
|
|
debug_assert!(dst.layout.is_sized());
|
|
|
|
|
|
2023-04-01 01:46:36 -07:00
|
|
|
|
if let Some(val) = self.codegen_transmute_operand(bx, src, dst.layout) {
|
|
|
|
|
val.store(bx, dst);
|
2023-02-24 18:32:52 -08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match src.val {
|
2023-04-01 01:46:36 -07:00
|
|
|
|
OperandValue::Ref(..) => {
|
|
|
|
|
span_bug!(
|
|
|
|
|
self.mir.span,
|
|
|
|
|
"Operand path should have handled transmute \
|
|
|
|
|
from `Ref` {src:?} to place {dst:?}"
|
|
|
|
|
);
|
2023-02-24 18:32:52 -08:00
|
|
|
|
}
|
2023-04-01 01:46:36 -07:00
|
|
|
|
OperandValue::Immediate(..) | OperandValue::Pair(..) => {
|
2023-02-24 18:32:52 -08:00
|
|
|
|
// When we have immediate(s), the alignment of the source is irrelevant,
|
|
|
|
|
// so we can store them using the destination's alignment.
|
|
|
|
|
let llty = bx.backend_type(src.layout);
|
|
|
|
|
let cast_ptr = bx.pointercast(dst.llval, bx.type_ptr_to(llty));
|
|
|
|
|
src.val.store(bx, PlaceRef::new_sized_aligned(cast_ptr, src.layout, dst.align));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 01:46:36 -07:00
|
|
|
|
/// Attempts to transmute an `OperandValue` to another `OperandValue`.
|
|
|
|
|
///
|
|
|
|
|
/// Returns `None` for cases that can't work in that framework, such as for
|
|
|
|
|
/// `Immediate`->`Ref` that needs an `alloc` to get the location.
|
|
|
|
|
fn codegen_transmute_operand(
|
|
|
|
|
&mut self,
|
|
|
|
|
bx: &mut Bx,
|
|
|
|
|
operand: OperandRef<'tcx, Bx::Value>,
|
|
|
|
|
cast: TyAndLayout<'tcx>,
|
|
|
|
|
) -> Option<OperandValue<Bx::Value>> {
|
2023-04-06 13:53:10 -07:00
|
|
|
|
// Check for transmutes that are always UB.
|
|
|
|
|
if operand.layout.size != cast.size
|
|
|
|
|
|| operand.layout.abi.is_uninhabited()
|
|
|
|
|
|| cast.abi.is_uninhabited()
|
|
|
|
|
{
|
|
|
|
|
if !operand.layout.abi.is_uninhabited() {
|
|
|
|
|
// Since this is known statically and the input could have existed
|
|
|
|
|
// without already having hit UB, might as well trap for it.
|
|
|
|
|
bx.abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Because this transmute is UB, return something easy to generate,
|
|
|
|
|
// since it's fine that later uses of the value are probably UB.
|
|
|
|
|
return Some(OperandValue::poison(bx, cast));
|
|
|
|
|
}
|
2023-04-01 01:46:36 -07:00
|
|
|
|
|
|
|
|
|
let operand_kind = self.value_kind(operand.layout);
|
|
|
|
|
let cast_kind = self.value_kind(cast);
|
|
|
|
|
|
|
|
|
|
match operand.val {
|
|
|
|
|
OperandValue::Ref(ptr, meta, align) => {
|
|
|
|
|
debug_assert_eq!(meta, None);
|
|
|
|
|
debug_assert!(matches!(operand_kind, OperandValueKind::Ref));
|
|
|
|
|
let cast_bty = bx.backend_type(cast);
|
|
|
|
|
let cast_ptr = bx.pointercast(ptr, bx.type_ptr_to(cast_bty));
|
|
|
|
|
let fake_place = PlaceRef::new_sized_aligned(cast_ptr, cast, align);
|
|
|
|
|
Some(bx.load_operand(fake_place).val)
|
|
|
|
|
}
|
|
|
|
|
OperandValue::Immediate(imm) => {
|
|
|
|
|
let OperandValueKind::Immediate(in_scalar) = operand_kind else {
|
|
|
|
|
bug!("Found {operand_kind:?} for operand {operand:?}");
|
|
|
|
|
};
|
2023-04-06 16:24:32 -07:00
|
|
|
|
if let OperandValueKind::Immediate(out_scalar) = cast_kind {
|
|
|
|
|
match (in_scalar, out_scalar) {
|
|
|
|
|
(ScalarOrZst::Zst, ScalarOrZst::Zst) => {
|
|
|
|
|
Some(OperandRef::new_zst(bx, cast).val)
|
|
|
|
|
}
|
|
|
|
|
(ScalarOrZst::Scalar(in_scalar), ScalarOrZst::Scalar(out_scalar))
|
|
|
|
|
if in_scalar.size(self.cx) == out_scalar.size(self.cx) =>
|
|
|
|
|
{
|
2023-04-06 00:36:36 -07:00
|
|
|
|
let operand_bty = bx.backend_type(operand.layout);
|
2023-04-06 16:24:32 -07:00
|
|
|
|
let cast_bty = bx.backend_type(cast);
|
2023-04-06 00:36:36 -07:00
|
|
|
|
Some(OperandValue::Immediate(self.transmute_immediate(
|
|
|
|
|
bx,
|
|
|
|
|
imm,
|
|
|
|
|
in_scalar,
|
|
|
|
|
operand_bty,
|
|
|
|
|
out_scalar,
|
|
|
|
|
cast_bty,
|
|
|
|
|
)))
|
2023-04-06 16:24:32 -07:00
|
|
|
|
}
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
2023-04-01 01:46:36 -07:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
OperandValue::Pair(imm_a, imm_b) => {
|
|
|
|
|
let OperandValueKind::Pair(in_a, in_b) = operand_kind else {
|
|
|
|
|
bug!("Found {operand_kind:?} for operand {operand:?}");
|
|
|
|
|
};
|
2023-04-06 13:53:10 -07:00
|
|
|
|
if let OperandValueKind::Pair(out_a, out_b) = cast_kind
|
|
|
|
|
&& in_a.size(self.cx) == out_a.size(self.cx)
|
|
|
|
|
&& in_b.size(self.cx) == out_b.size(self.cx)
|
|
|
|
|
{
|
2023-04-06 00:36:36 -07:00
|
|
|
|
let in_a_ibty = bx.scalar_pair_element_backend_type(operand.layout, 0, false);
|
|
|
|
|
let in_b_ibty = bx.scalar_pair_element_backend_type(operand.layout, 1, false);
|
2023-04-01 01:46:36 -07:00
|
|
|
|
let out_a_ibty = bx.scalar_pair_element_backend_type(cast, 0, false);
|
|
|
|
|
let out_b_ibty = bx.scalar_pair_element_backend_type(cast, 1, false);
|
|
|
|
|
Some(OperandValue::Pair(
|
2023-04-06 00:36:36 -07:00
|
|
|
|
self.transmute_immediate(bx, imm_a, in_a, in_a_ibty, out_a, out_a_ibty),
|
|
|
|
|
self.transmute_immediate(bx, imm_b, in_b, in_b_ibty, out_b, out_b_ibty),
|
2023-04-01 01:46:36 -07:00
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Transmutes one of the immediates from an [`OperandValue::Immediate`]
|
|
|
|
|
/// or an [`OperandValue::Pair`] to an immediate of the target type.
|
|
|
|
|
///
|
|
|
|
|
/// `to_backend_ty` must be the *non*-immediate backend type (so it will be
|
|
|
|
|
/// `i8`, not `i1`, for `bool`-like types.)
|
|
|
|
|
fn transmute_immediate(
|
2023-04-06 13:53:10 -07:00
|
|
|
|
&self,
|
2023-04-01 01:46:36 -07:00
|
|
|
|
bx: &mut Bx,
|
|
|
|
|
mut imm: Bx::Value,
|
|
|
|
|
from_scalar: abi::Scalar,
|
2023-04-06 00:36:36 -07:00
|
|
|
|
from_backend_ty: Bx::Type,
|
2023-04-01 01:46:36 -07:00
|
|
|
|
to_scalar: abi::Scalar,
|
|
|
|
|
to_backend_ty: Bx::Type,
|
|
|
|
|
) -> Bx::Value {
|
2023-04-06 13:53:10 -07:00
|
|
|
|
debug_assert_eq!(from_scalar.size(self.cx), to_scalar.size(self.cx));
|
|
|
|
|
|
2023-04-01 01:46:36 -07:00
|
|
|
|
use abi::Primitive::*;
|
|
|
|
|
imm = bx.from_immediate(imm);
|
2023-04-19 23:14:28 -07:00
|
|
|
|
|
|
|
|
|
// When scalars are passed by value, there's no metadata recording their
|
|
|
|
|
// valid ranges. For example, `char`s are passed as just `i32`, with no
|
|
|
|
|
// way for LLVM to know that they're 0x10FFFF at most. Thus we assume
|
|
|
|
|
// the range of the input value too, not just the output range.
|
2023-04-06 00:36:36 -07:00
|
|
|
|
self.assume_scalar_range(bx, imm, from_scalar, from_backend_ty);
|
2023-04-19 23:14:28 -07:00
|
|
|
|
|
2023-04-01 01:46:36 -07:00
|
|
|
|
imm = match (from_scalar.primitive(), to_scalar.primitive()) {
|
|
|
|
|
(Int(..) | F32 | F64, Int(..) | F32 | F64) => bx.bitcast(imm, to_backend_ty),
|
|
|
|
|
(Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty),
|
|
|
|
|
(Int(..), Pointer(..)) => bx.inttoptr(imm, to_backend_ty),
|
|
|
|
|
(Pointer(..), Int(..)) => bx.ptrtoint(imm, to_backend_ty),
|
|
|
|
|
(F32 | F64, Pointer(..)) => {
|
|
|
|
|
let int_imm = bx.bitcast(imm, bx.cx().type_isize());
|
|
|
|
|
bx.inttoptr(int_imm, to_backend_ty)
|
|
|
|
|
}
|
|
|
|
|
(Pointer(..), F32 | F64) => {
|
|
|
|
|
let int_imm = bx.ptrtoint(imm, bx.cx().type_isize());
|
|
|
|
|
bx.bitcast(int_imm, to_backend_ty)
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-04-06 00:36:36 -07:00
|
|
|
|
self.assume_scalar_range(bx, imm, to_scalar, to_backend_ty);
|
2023-04-01 01:46:36 -07:00
|
|
|
|
imm = bx.to_immediate_scalar(imm, to_scalar);
|
|
|
|
|
imm
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 00:36:36 -07:00
|
|
|
|
fn assume_scalar_range(
|
|
|
|
|
&self,
|
|
|
|
|
bx: &mut Bx,
|
|
|
|
|
imm: Bx::Value,
|
|
|
|
|
scalar: abi::Scalar,
|
|
|
|
|
backend_ty: Bx::Type,
|
|
|
|
|
) {
|
|
|
|
|
if matches!(self.cx.sess().opts.optimize, OptLevel::No | OptLevel::Less)
|
2023-04-19 23:14:28 -07:00
|
|
|
|
// For now, the critical niches are all over `Int`eger values.
|
|
|
|
|
// Should floating-point values or pointers ever get more complex
|
|
|
|
|
// niches, then this code will probably want to handle them too.
|
2023-04-06 00:36:36 -07:00
|
|
|
|
|| !matches!(scalar.primitive(), abi::Primitive::Int(..))
|
|
|
|
|
|| scalar.is_always_valid(self.cx)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let abi::WrappingRange { start, end } = scalar.valid_range(self.cx);
|
|
|
|
|
|
|
|
|
|
if start <= end {
|
|
|
|
|
if start > 0 {
|
|
|
|
|
let low = bx.const_uint_big(backend_ty, start);
|
|
|
|
|
let cmp = bx.icmp(IntPredicate::IntUGE, imm, low);
|
|
|
|
|
bx.assume(cmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let type_max = scalar.size(self.cx).unsigned_int_max();
|
|
|
|
|
if end < type_max {
|
|
|
|
|
let high = bx.const_uint_big(backend_ty, end);
|
|
|
|
|
let cmp = bx.icmp(IntPredicate::IntULE, imm, high);
|
|
|
|
|
bx.assume(cmp);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let low = bx.const_uint_big(backend_ty, start);
|
|
|
|
|
let cmp_low = bx.icmp(IntPredicate::IntUGE, imm, low);
|
|
|
|
|
|
|
|
|
|
let high = bx.const_uint_big(backend_ty, end);
|
|
|
|
|
let cmp_high = bx.icmp(IntPredicate::IntULE, imm, high);
|
|
|
|
|
|
|
|
|
|
let or = bx.or(cmp_low, cmp_high);
|
|
|
|
|
bx.assume(or);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
pub fn codegen_rvalue_unsized(
|
|
|
|
|
&mut self,
|
2022-11-09 11:04:10 +11:00
|
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
|
indirect_dest: PlaceRef<'tcx, Bx::Value>,
|
|
|
|
|
rvalue: &mir::Rvalue<'tcx>,
|
2022-11-09 11:04:10 +11:00
|
|
|
|
) {
|
2018-05-29 00:12:55 +09:00
|
|
|
|
debug!(
|
|
|
|
|
"codegen_rvalue_unsized(indirect_dest.llval={:?}, rvalue={:?})",
|
|
|
|
|
indirect_dest.llval, rvalue
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
match *rvalue {
|
|
|
|
|
mir::Rvalue::Use(ref operand) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let cg_operand = self.codegen_operand(bx, operand);
|
|
|
|
|
cg_operand.val.store_unsized(bx, indirect_dest);
|
2018-05-29 00:12:55 +09:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 02:20:14 +01:00
|
|
|
|
_ => bug!("unsized assignment other than `Rvalue::Use`"),
|
2018-05-29 00:12:55 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-07 17:14:40 +02:00
|
|
|
|
pub fn codegen_rvalue_operand(
|
|
|
|
|
&mut self,
|
2022-11-09 11:04:10 +11:00
|
|
|
|
bx: &mut Bx,
|
2018-08-07 17:14:40 +02:00
|
|
|
|
rvalue: &mir::Rvalue<'tcx>,
|
2022-11-09 11:04:10 +11:00
|
|
|
|
) -> OperandRef<'tcx, Bx::Value> {
|
2019-08-03 15:59:25 -07:00
|
|
|
|
assert!(
|
2019-10-14 01:38:38 -04:00
|
|
|
|
self.rvalue_creates_operand(rvalue, DUMMY_SP),
|
2019-08-03 15:59:25 -07:00
|
|
|
|
"cannot codegen {:?} to operand",
|
|
|
|
|
rvalue,
|
|
|
|
|
);
|
2015-11-02 09:39:59 -05:00
|
|
|
|
|
|
|
|
|
match *rvalue {
|
2017-09-20 18:17:23 +03:00
|
|
|
|
mir::Rvalue::Cast(ref kind, ref source, mir_cast_ty) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let operand = self.codegen_operand(bx, source);
|
2016-02-18 19:49:45 +02:00
|
|
|
|
debug!("cast operand is {:?}", operand);
|
2020-10-24 02:21:18 +02:00
|
|
|
|
let cast = bx.cx().layout_of(self.monomorphize(mir_cast_ty));
|
2015-11-11 22:02:51 +02:00
|
|
|
|
|
|
|
|
|
let val = match *kind {
|
2022-06-01 13:24:44 -04:00
|
|
|
|
mir::CastKind::PointerExposeAddress => {
|
2022-05-31 00:00:00 +00:00
|
|
|
|
assert!(bx.cx().is_backend_immediate(cast));
|
|
|
|
|
let llptr = operand.immediate();
|
|
|
|
|
let llcast_ty = bx.cx().immediate_backend_type(cast);
|
|
|
|
|
let lladdr = bx.ptrtoint(llptr, llcast_ty);
|
|
|
|
|
OperandValue::Immediate(lladdr)
|
|
|
|
|
}
|
2019-04-15 19:20:44 +05:30
|
|
|
|
mir::CastKind::Pointer(PointerCast::ReifyFnPointer) => {
|
2020-08-03 00:49:11 +02:00
|
|
|
|
match *operand.layout.ty.kind() {
|
2018-08-22 01:35:02 +01:00
|
|
|
|
ty::FnDef(def_id, substs) => {
|
2020-06-22 13:57:03 +01:00
|
|
|
|
let instance = ty::Instance::resolve_for_fn_ptr(
|
|
|
|
|
bx.tcx(),
|
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
|
def_id,
|
|
|
|
|
substs,
|
2019-10-13 11:45:34 +02:00
|
|
|
|
)
|
2020-06-22 13:57:03 +01:00
|
|
|
|
.unwrap()
|
|
|
|
|
.polymorphize(bx.cx().tcx());
|
|
|
|
|
OperandValue::Immediate(bx.get_fn_addr(instance))
|
2016-03-06 17:32:47 +02:00
|
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
|
_ => bug!("{} cannot be reified to a fn ptr", operand.layout.ty),
|
2016-03-06 17:32:47 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-15 19:20:44 +05:30
|
|
|
|
mir::CastKind::Pointer(PointerCast::ClosureFnPointer(_)) => {
|
2020-08-03 00:49:11 +02:00
|
|
|
|
match *operand.layout.ty.kind() {
|
2018-08-22 01:35:02 +01:00
|
|
|
|
ty::Closure(def_id, substs) => {
|
2019-05-23 12:45:22 -05:00
|
|
|
|
let instance = Instance::resolve_closure(
|
2019-09-26 17:30:44 +00:00
|
|
|
|
bx.cx().tcx(),
|
|
|
|
|
def_id,
|
|
|
|
|
substs,
|
2019-09-26 16:11:23 +00:00
|
|
|
|
ty::ClosureKind::FnOnce,
|
2020-06-22 13:57:03 +01:00
|
|
|
|
)
|
2022-02-04 23:18:28 +01:00
|
|
|
|
.expect("failed to normalize and resolve closure during codegen")
|
2020-06-22 13:57:03 +01:00
|
|
|
|
.polymorphize(bx.cx().tcx());
|
2019-10-13 12:05:40 +02:00
|
|
|
|
OperandValue::Immediate(bx.cx().get_fn_addr(instance))
|
2017-02-22 01:24:16 +01:00
|
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
|
_ => bug!("{} cannot be cast to a fn ptr", operand.layout.ty),
|
2017-02-22 01:24:16 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-15 19:20:44 +05:30
|
|
|
|
mir::CastKind::Pointer(PointerCast::UnsafeFnPointer) => {
|
2019-05-17 02:20:14 +01:00
|
|
|
|
// This is a no-op at the LLVM level.
|
2015-11-11 22:02:51 +02:00
|
|
|
|
operand.val
|
|
|
|
|
}
|
2019-04-15 19:20:44 +05:30
|
|
|
|
mir::CastKind::Pointer(PointerCast::Unsize) => {
|
2018-10-03 13:49:57 +02:00
|
|
|
|
assert!(bx.cx().is_backend_scalar_pair(cast));
|
2021-07-31 22:46:23 +08:00
|
|
|
|
let (lldata, llextra) = match operand.val {
|
2016-05-25 11:55:44 +03:00
|
|
|
|
OperandValue::Pair(lldata, llextra) => {
|
2019-05-17 02:20:14 +01:00
|
|
|
|
// unsize from a fat pointer -- this is a
|
2021-07-31 22:46:23 +08:00
|
|
|
|
// "trait-object-to-supertrait" coercion.
|
|
|
|
|
(lldata, Some(llextra))
|
2015-11-11 22:02:51 +02:00
|
|
|
|
}
|
2015-11-13 00:12:50 +02:00
|
|
|
|
OperandValue::Immediate(lldata) => {
|
2015-11-11 22:02:51 +02:00
|
|
|
|
// "standard" unsize
|
2021-07-31 22:46:23 +08:00
|
|
|
|
(lldata, None)
|
2015-11-11 22:02:51 +02:00
|
|
|
|
}
|
2017-02-06 17:27:09 +01:00
|
|
|
|
OperandValue::Ref(..) => {
|
2019-05-17 02:20:14 +01:00
|
|
|
|
bug!("by-ref operand {:?} in `codegen_rvalue_operand`", operand);
|
2015-11-11 22:02:51 +02:00
|
|
|
|
}
|
2021-07-31 22:46:23 +08:00
|
|
|
|
};
|
|
|
|
|
let (lldata, llextra) =
|
2022-11-09 11:04:10 +11:00
|
|
|
|
base::unsize_ptr(bx, lldata, operand.layout.ty, cast.ty, llextra);
|
2021-07-31 22:46:23 +08:00
|
|
|
|
OperandValue::Pair(lldata, llextra)
|
2015-11-11 22:02:51 +02:00
|
|
|
|
}
|
2019-05-17 02:20:14 +01:00
|
|
|
|
mir::CastKind::Pointer(PointerCast::MutToConstPointer)
|
2022-10-04 21:39:43 +03:00
|
|
|
|
| mir::CastKind::PtrToPtr
|
2019-05-17 02:20:14 +01:00
|
|
|
|
if bx.cx().is_backend_scalar_pair(operand.layout) =>
|
|
|
|
|
{
|
2017-06-18 17:42:03 +03:00
|
|
|
|
if let OperandValue::Pair(data_ptr, meta) = operand.val {
|
2018-10-03 13:49:57 +02:00
|
|
|
|
if bx.cx().is_backend_scalar_pair(cast) {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
let data_cast = bx.pointercast(
|
|
|
|
|
data_ptr,
|
2018-09-20 15:47:22 +02:00
|
|
|
|
bx.cx().scalar_pair_element_backend_type(cast, 0, true),
|
|
|
|
|
);
|
2017-06-18 17:42:03 +03:00
|
|
|
|
OperandValue::Pair(data_cast, meta)
|
2016-08-02 03:28:50 +03:00
|
|
|
|
} else {
|
|
|
|
|
// cast to thin-ptr
|
|
|
|
|
// Cast of fat-ptr to thin-ptr is an extraction of data-ptr and
|
|
|
|
|
// pointer-cast of that pointer to desired pointer type.
|
2018-09-20 15:47:22 +02:00
|
|
|
|
let llcast_ty = bx.cx().immediate_backend_type(cast);
|
2018-01-05 07:12:32 +02:00
|
|
|
|
let llval = bx.pointercast(data_ptr, llcast_ty);
|
2016-08-02 03:28:50 +03:00
|
|
|
|
OperandValue::Immediate(llval)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-05-17 02:20:14 +01:00
|
|
|
|
bug!("unexpected non-pair operand");
|
2016-08-02 03:28:50 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-28 14:02:30 -07:00
|
|
|
|
mir::CastKind::DynStar => {
|
2022-09-14 23:28:14 +00:00
|
|
|
|
let (lldata, llextra) = match operand.val {
|
2022-06-28 14:02:30 -07:00
|
|
|
|
OperandValue::Ref(_, _, _) => todo!(),
|
2022-09-14 23:28:14 +00:00
|
|
|
|
OperandValue::Immediate(v) => (v, None),
|
|
|
|
|
OperandValue::Pair(v, l) => (v, Some(l)),
|
2022-10-03 17:35:03 -07:00
|
|
|
|
};
|
2022-09-14 23:28:14 +00:00
|
|
|
|
let (lldata, llextra) =
|
2022-11-09 11:04:10 +11:00
|
|
|
|
base::cast_to_dyn_star(bx, lldata, operand.layout, cast.ty, llextra);
|
2022-09-14 23:28:14 +00:00
|
|
|
|
OperandValue::Pair(lldata, llextra)
|
2022-06-28 14:02:30 -07:00
|
|
|
|
}
|
2020-04-16 17:38:52 -07:00
|
|
|
|
mir::CastKind::Pointer(
|
|
|
|
|
PointerCast::MutToConstPointer | PointerCast::ArrayToPointer,
|
|
|
|
|
)
|
2022-10-04 21:39:43 +03:00
|
|
|
|
| mir::CastKind::IntToInt
|
|
|
|
|
| mir::CastKind::FloatToInt
|
|
|
|
|
| mir::CastKind::FloatToFloat
|
|
|
|
|
| mir::CastKind::IntToFloat
|
|
|
|
|
| mir::CastKind::PtrToPtr
|
|
|
|
|
| mir::CastKind::FnPtrToPtr
|
|
|
|
|
|
2022-06-02 11:10:34 -04:00
|
|
|
|
// Since int2ptr can have arbitrary integer types as input (so we have to do
|
|
|
|
|
// sign extension and all that), it is currently best handled in the same code
|
|
|
|
|
// path as the other integer-to-X casts.
|
2022-06-02 09:05:37 -04:00
|
|
|
|
| mir::CastKind::PointerFromExposedAddress => {
|
2018-10-03 13:49:57 +02:00
|
|
|
|
assert!(bx.cx().is_backend_immediate(cast));
|
2018-09-20 15:47:22 +02:00
|
|
|
|
let ll_t_out = bx.cx().immediate_backend_type(cast);
|
2018-08-23 16:34:38 +02:00
|
|
|
|
if operand.layout.abi.is_uninhabited() {
|
2023-03-16 14:56:02 +01:00
|
|
|
|
let val = OperandValue::Immediate(bx.cx().const_poison(ll_t_out));
|
2022-11-09 11:04:10 +11:00
|
|
|
|
return OperandRef { val, layout: cast };
|
2018-04-11 17:25:18 +02:00
|
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
|
let r_t_in =
|
|
|
|
|
CastTy::from_ty(operand.layout.ty).expect("bad input type for cast");
|
|
|
|
|
let r_t_out = CastTy::from_ty(cast.ty).expect("bad output type for cast");
|
2018-09-20 15:47:22 +02:00
|
|
|
|
let ll_t_in = bx.cx().immediate_backend_type(operand.layout);
|
2017-01-05 12:22:58 -07:00
|
|
|
|
let llval = operand.immediate();
|
2017-09-16 16:39:53 +03:00
|
|
|
|
|
2015-11-05 13:33:06 +02:00
|
|
|
|
let newval = match (r_t_in, r_t_out) {
|
2022-06-29 14:18:55 +00:00
|
|
|
|
(CastTy::Int(i), CastTy::Int(_)) => {
|
2022-07-05 09:26:45 +00:00
|
|
|
|
bx.intcast(llval, ll_t_out, i.is_signed())
|
2022-06-29 14:18:55 +00:00
|
|
|
|
}
|
2015-11-05 13:33:06 +02:00
|
|
|
|
(CastTy::Float, CastTy::Float) => {
|
2018-09-05 14:14:03 -07:00
|
|
|
|
let srcsz = bx.cx().float_width(ll_t_in);
|
|
|
|
|
let dstsz = bx.cx().float_width(ll_t_out);
|
2015-11-05 13:33:06 +02:00
|
|
|
|
if dstsz > srcsz {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.fpext(llval, ll_t_out)
|
2015-11-05 13:33:06 +02:00
|
|
|
|
} else if srcsz > dstsz {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.fptrunc(llval, ll_t_out)
|
2015-11-05 13:33:06 +02:00
|
|
|
|
} else {
|
|
|
|
|
llval
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-29 14:18:55 +00:00
|
|
|
|
(CastTy::Int(i), CastTy::Float) => {
|
2022-07-05 09:26:45 +00:00
|
|
|
|
if i.is_signed() {
|
2019-12-15 18:17:00 +01:00
|
|
|
|
bx.sitofp(llval, ll_t_out)
|
|
|
|
|
} else {
|
|
|
|
|
bx.uitofp(llval, ll_t_out)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-16 17:38:52 -07:00
|
|
|
|
(CastTy::Ptr(_) | CastTy::FnPtr, CastTy::Ptr(_)) => {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.pointercast(llval, ll_t_out)
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2022-06-29 14:18:55 +00:00
|
|
|
|
(CastTy::Int(i), CastTy::Ptr(_)) => {
|
|
|
|
|
let usize_llval =
|
2022-07-05 09:26:45 +00:00
|
|
|
|
bx.intcast(llval, bx.cx().type_isize(), i.is_signed());
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.inttoptr(usize_llval, ll_t_out)
|
2018-01-02 12:44:54 -08:00
|
|
|
|
}
|
2015-11-05 13:33:06 +02:00
|
|
|
|
(CastTy::Float, CastTy::Int(IntTy::I)) => {
|
2021-12-30 01:18:44 +00:00
|
|
|
|
bx.cast_float_to_int(true, llval, ll_t_out)
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2015-11-05 13:33:06 +02:00
|
|
|
|
(CastTy::Float, CastTy::Int(_)) => {
|
2021-12-30 01:18:44 +00:00
|
|
|
|
bx.cast_float_to_int(false, llval, ll_t_out)
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
|
_ => bug!("unsupported cast: {:?} to {:?}", operand.layout.ty, cast.ty),
|
2015-11-05 13:33:06 +02:00
|
|
|
|
};
|
|
|
|
|
OperandValue::Immediate(newval)
|
|
|
|
|
}
|
2023-02-24 18:32:52 -08:00
|
|
|
|
mir::CastKind::Transmute => {
|
2023-04-01 01:46:36 -07:00
|
|
|
|
self.codegen_transmute_operand(bx, operand, cast).unwrap_or_else(|| {
|
|
|
|
|
bug!("Unsupported transmute-as-operand of {operand:?} to {cast:?}");
|
|
|
|
|
})
|
2023-02-24 18:32:52 -08:00
|
|
|
|
}
|
2015-11-11 22:02:51 +02:00
|
|
|
|
};
|
2022-11-09 11:04:10 +11:00
|
|
|
|
OperandRef { val, layout: cast }
|
2015-11-02 09:39:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 14:39:18 -03:00
|
|
|
|
mir::Rvalue::Ref(_, bk, place) => {
|
2018-12-23 19:00:58 +00:00
|
|
|
|
let mk_ref = move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| {
|
|
|
|
|
tcx.mk_ref(
|
|
|
|
|
tcx.lifetimes.re_erased,
|
|
|
|
|
ty::TypeAndMut { ty, mutbl: bk.to_mutbl_lossy() },
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
self.codegen_place_to_pointer(bx, place, mk_ref)
|
|
|
|
|
}
|
2015-11-11 22:02:51 +02:00
|
|
|
|
|
2022-11-09 11:04:10 +11:00
|
|
|
|
mir::Rvalue::CopyForDeref(place) => self.codegen_operand(bx, &Operand::Copy(place)),
|
2020-03-31 14:39:18 -03:00
|
|
|
|
mir::Rvalue::AddressOf(mutability, place) => {
|
2018-12-23 19:00:58 +00:00
|
|
|
|
let mk_ptr = move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| {
|
2020-02-25 18:10:34 +01:00
|
|
|
|
tcx.mk_ptr(ty::TypeAndMut { ty, mutbl: mutability })
|
2018-12-23 19:00:58 +00:00
|
|
|
|
};
|
|
|
|
|
self.codegen_place_to_pointer(bx, place, mk_ptr)
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 14:37:10 -03:00
|
|
|
|
mir::Rvalue::Len(place) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let size = self.evaluate_array_len(bx, place);
|
|
|
|
|
OperandRef {
|
2017-08-20 21:35:00 +10:00
|
|
|
|
val: OperandValue::Immediate(size),
|
2018-08-28 17:50:57 +02:00
|
|
|
|
layout: bx.cx().layout_of(bx.tcx().types.usize),
|
2022-11-09 11:04:10 +11:00
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 09:32:47 +00:00
|
|
|
|
mir::Rvalue::BinaryOp(op, box (ref lhs, ref rhs)) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let lhs = self.codegen_operand(bx, lhs);
|
|
|
|
|
let rhs = self.codegen_operand(bx, rhs);
|
2017-10-10 22:04:13 +03:00
|
|
|
|
let llresult = match (lhs.val, rhs.val) {
|
|
|
|
|
(
|
|
|
|
|
OperandValue::Pair(lhs_addr, lhs_extra),
|
|
|
|
|
OperandValue::Pair(rhs_addr, rhs_extra),
|
2018-10-05 15:08:49 +02:00
|
|
|
|
) => self.codegen_fat_ptr_binop(
|
2022-11-09 11:04:10 +11:00
|
|
|
|
bx,
|
2018-10-05 15:08:49 +02:00
|
|
|
|
op,
|
2017-10-10 22:04:13 +03:00
|
|
|
|
lhs_addr,
|
|
|
|
|
lhs_extra,
|
|
|
|
|
rhs_addr,
|
|
|
|
|
rhs_extra,
|
|
|
|
|
lhs.layout.ty,
|
|
|
|
|
),
|
2019-12-22 17:42:04 -05:00
|
|
|
|
|
2017-10-10 22:04:13 +03:00
|
|
|
|
(OperandValue::Immediate(lhs_val), OperandValue::Immediate(rhs_val)) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
self.codegen_scalar_binop(bx, op, lhs_val, rhs_val, lhs.layout.ty)
|
2017-10-10 22:04:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ => bug!(),
|
2015-10-21 17:42:25 -04:00
|
|
|
|
};
|
2022-11-09 11:04:10 +11:00
|
|
|
|
OperandRef {
|
2015-11-13 00:12:50 +02:00
|
|
|
|
val: OperandValue::Immediate(llresult),
|
2018-01-05 07:12:32 +02:00
|
|
|
|
layout: bx.cx().layout_of(op.ty(bx.tcx(), lhs.layout.ty, rhs.layout.ty)),
|
2022-11-09 11:04:10 +11:00
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
2021-03-05 09:32:47 +00:00
|
|
|
|
mir::Rvalue::CheckedBinaryOp(op, box (ref lhs, ref rhs)) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let lhs = self.codegen_operand(bx, lhs);
|
|
|
|
|
let rhs = self.codegen_operand(bx, rhs);
|
2018-10-05 15:08:49 +02:00
|
|
|
|
let result = self.codegen_scalar_checked_binop(
|
2022-11-09 11:04:10 +11:00
|
|
|
|
bx,
|
2018-10-05 15:08:49 +02:00
|
|
|
|
op,
|
2016-03-31 18:50:07 +13:00
|
|
|
|
lhs.immediate(),
|
|
|
|
|
rhs.immediate(),
|
2017-09-20 18:17:23 +03:00
|
|
|
|
lhs.layout.ty,
|
|
|
|
|
);
|
2018-01-05 07:12:32 +02:00
|
|
|
|
let val_ty = op.ty(bx.tcx(), lhs.layout.ty, rhs.layout.ty);
|
2023-02-17 14:33:08 +11:00
|
|
|
|
let operand_ty = bx.tcx().mk_tup(&[val_ty, bx.tcx().types.bool]);
|
2022-11-09 11:04:10 +11:00
|
|
|
|
OperandRef { val: result, layout: bx.cx().layout_of(operand_ty) }
|
2016-03-31 18:50:07 +13:00
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
|
|
|
|
|
mir::Rvalue::UnaryOp(op, ref operand) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let operand = self.codegen_operand(bx, operand);
|
2015-11-10 22:05:11 +02:00
|
|
|
|
let lloperand = operand.immediate();
|
2019-06-09 12:53:47 +02:00
|
|
|
|
let is_float = operand.layout.ty.is_floating_point();
|
2015-10-21 17:42:25 -04:00
|
|
|
|
let llval = match op {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
mir::UnOp::Not => bx.not(lloperand),
|
2015-10-21 17:42:25 -04:00
|
|
|
|
mir::UnOp::Neg => {
|
|
|
|
|
if is_float {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.fneg(lloperand)
|
2015-10-21 17:42:25 -04:00
|
|
|
|
} else {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.neg(lloperand)
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
|
|
|
|
};
|
2022-11-09 11:04:10 +11:00
|
|
|
|
OperandRef { val: OperandValue::Immediate(llval), layout: operand.layout }
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
|
mir::Rvalue::Discriminant(ref place) => {
|
2020-04-12 10:28:41 -07:00
|
|
|
|
let discr_ty = rvalue.ty(self.mir, bx.tcx());
|
2020-12-11 00:00:00 +00:00
|
|
|
|
let discr_ty = self.monomorphize(discr_ty);
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let discr = self.codegen_place(bx, place.as_ref()).codegen_get_discr(bx, discr_ty);
|
|
|
|
|
OperandRef {
|
|
|
|
|
val: OperandValue::Immediate(discr),
|
|
|
|
|
layout: self.cx.layout_of(discr_ty),
|
|
|
|
|
}
|
2017-01-31 01:10:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 16:06:07 +01:00
|
|
|
|
mir::Rvalue::NullaryOp(null_op, ty) => {
|
|
|
|
|
let ty = self.monomorphize(ty);
|
|
|
|
|
assert!(bx.cx().type_is_sized(ty));
|
|
|
|
|
let layout = bx.cx().layout_of(ty);
|
|
|
|
|
let val = match null_op {
|
|
|
|
|
mir::NullOp::SizeOf => layout.size.bytes(),
|
|
|
|
|
mir::NullOp::AlignOf => layout.align.abi.bytes(),
|
|
|
|
|
};
|
|
|
|
|
let val = bx.cx().const_usize(val);
|
|
|
|
|
let tcx = self.cx.tcx();
|
2022-11-09 11:04:10 +11:00
|
|
|
|
OperandRef {
|
|
|
|
|
val: OperandValue::Immediate(val),
|
|
|
|
|
layout: self.cx.layout_of(tcx.types.usize),
|
|
|
|
|
}
|
2021-09-07 16:06:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-02 21:44:25 +02:00
|
|
|
|
mir::Rvalue::ThreadLocalRef(def_id) => {
|
|
|
|
|
assert!(bx.cx().tcx().is_static(def_id));
|
|
|
|
|
let layout = bx.layout_of(bx.cx().tcx().static_ptr_ty(def_id));
|
2023-02-03 09:04:12 +01:00
|
|
|
|
let static_ = if !def_id.is_local() && bx.cx().tcx().needs_thread_local_shim(def_id)
|
|
|
|
|
{
|
|
|
|
|
let instance = ty::Instance {
|
|
|
|
|
def: ty::InstanceDef::ThreadLocalShim(def_id),
|
|
|
|
|
substs: ty::InternalSubsts::empty(),
|
|
|
|
|
};
|
|
|
|
|
let fn_ptr = bx.get_fn_addr(instance);
|
|
|
|
|
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
|
|
|
|
|
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
|
|
|
|
|
bx.call(fn_ty, Some(fn_abi), fn_ptr, &[], None)
|
|
|
|
|
} else {
|
|
|
|
|
bx.get_static(def_id)
|
|
|
|
|
};
|
2022-12-04 12:53:46 +00:00
|
|
|
|
OperandRef { val: OperandValue::Immediate(static_), layout }
|
Various improvements to MIR and LLVM IR Construction
Primarily affects the MIR construction, which indirectly improves LLVM
IR generation, but some LLVM IR changes have been made too.
* Handle "statement expressions" more intelligently. These are
expressions that always evaluate to `()`. Previously a temporary would
be generated as a destination to translate into, which is unnecessary.
This affects assignment, augmented assignment, `return`, `break` and
`continue`.
* Avoid inserting drops for non-drop types in more places. Scheduled
drops were already skipped for types that we knew wouldn't need
dropping at construction time. However manually-inserted drops like
those for `x` in `x = y;` were still generated. `build_drop` now takes
a type parameter like its `schedule_drop` counterpart and checks to
see if the type needs dropping.
* Avoid generating an extra temporary for an assignment where the types
involved don't need dropping. Previously an expression like
`a = b + 1;` would result in a temporary for `b + 1`. This is so the
RHS can be evaluated, then the LHS evaluated and dropped and have
everything work correctly. However, this isn't necessary if the `LHS`
doesn't need a drop, as we can just overwrite the existing value.
* Improves lvalue analysis to allow treating an `Rvalue::Use` as an
operand in certain conditions. The reason for it never being an
operand is so it can be zeroed/drop-filled, but this is only true for
types that need dropping.
The first two changes result in significantly fewer MIR blocks being
generated, as previously almost every statement would end up generating
a new block due to the drop of the `()` temporary being generated.
2016-04-15 12:36:16 +12:00
|
|
|
|
}
|
2022-11-09 11:04:10 +11:00
|
|
|
|
mir::Rvalue::Use(ref operand) => self.codegen_operand(bx, operand),
|
2017-02-15 21:21:36 +02:00
|
|
|
|
mir::Rvalue::Repeat(..) | mir::Rvalue::Aggregate(..) => {
|
2017-03-08 20:03:04 +02:00
|
|
|
|
// According to `rvalue_creates_operand`, only ZST
|
|
|
|
|
// aggregate rvalues are allowed to be operands.
|
2020-04-12 10:28:41 -07:00
|
|
|
|
let ty = rvalue.ty(self.mir, self.cx.tcx());
|
2022-11-09 11:04:10 +11:00
|
|
|
|
OperandRef::new_zst(bx, self.cx.layout_of(self.monomorphize(ty)))
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
2021-09-06 18:33:23 +01:00
|
|
|
|
mir::Rvalue::ShallowInitBox(ref operand, content_ty) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
|
let operand = self.codegen_operand(bx, operand);
|
2021-09-06 18:33:23 +01:00
|
|
|
|
let lloperand = operand.immediate();
|
|
|
|
|
|
|
|
|
|
let content_ty = self.monomorphize(content_ty);
|
|
|
|
|
let box_layout = bx.cx().layout_of(bx.tcx().mk_box(content_ty));
|
|
|
|
|
let llty_ptr = bx.cx().backend_type(box_layout);
|
|
|
|
|
|
|
|
|
|
let val = bx.pointercast(lloperand, llty_ptr);
|
2022-11-09 11:04:10 +11:00
|
|
|
|
OperandRef { val: OperandValue::Immediate(val), layout: box_layout }
|
2021-09-06 18:33:23 +01:00
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-10 22:05:11 +02:00
|
|
|
|
|
2020-03-31 14:37:10 -03:00
|
|
|
|
fn evaluate_array_len(&mut self, bx: &mut Bx, place: mir::Place<'tcx>) -> Bx::Value {
|
2017-08-20 21:35:00 +10:00
|
|
|
|
// ZST are passed as operands and require special handling
|
2018-05-08 16:10:16 +03:00
|
|
|
|
// because codegen_place() panics if Local is operand.
|
2019-10-20 16:09:36 -04:00
|
|
|
|
if let Some(index) = place.as_local() {
|
2023-03-24 20:36:59 -07:00
|
|
|
|
if let LocalRef::Operand(op) = self.locals[index] {
|
2020-08-03 00:49:11 +02:00
|
|
|
|
if let ty::Array(_, n) = op.layout.ty.kind() {
|
2023-02-14 08:51:19 +00:00
|
|
|
|
let n = n.eval_target_usize(bx.cx().tcx(), ty::ParamEnv::reveal_all());
|
2018-09-06 11:57:42 -07:00
|
|
|
|
return bx.cx().const_usize(n);
|
2017-08-20 21:35:00 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// use common size calculation for non zero-sized types
|
2020-01-14 01:51:59 -03:00
|
|
|
|
let cg_value = self.codegen_place(bx, place.as_ref());
|
2019-07-03 06:20:07 +02:00
|
|
|
|
cg_value.len(bx.cx())
|
2017-08-20 21:35:00 +10:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-23 19:00:58 +00:00
|
|
|
|
/// Codegen an `Rvalue::AddressOf` or `Rvalue::Ref`
|
|
|
|
|
fn codegen_place_to_pointer(
|
|
|
|
|
&mut self,
|
2022-11-09 11:04:10 +11:00
|
|
|
|
bx: &mut Bx,
|
2020-03-31 14:39:18 -03:00
|
|
|
|
place: mir::Place<'tcx>,
|
2018-12-23 19:00:58 +00:00
|
|
|
|
mk_ptr_ty: impl FnOnce(TyCtxt<'tcx>, Ty<'tcx>) -> Ty<'tcx>,
|
2022-11-09 11:04:10 +11:00
|
|
|
|
) -> OperandRef<'tcx, Bx::Value> {
|
|
|
|
|
let cg_place = self.codegen_place(bx, place.as_ref());
|
2018-12-23 19:00:58 +00:00
|
|
|
|
|
|
|
|
|
let ty = cg_place.layout.ty;
|
|
|
|
|
|
|
|
|
|
// Note: places are indirect, so storing the `llval` into the
|
|
|
|
|
// destination effectively creates a reference.
|
|
|
|
|
let val = if !bx.cx().type_has_metadata(ty) {
|
|
|
|
|
OperandValue::Immediate(cg_place.llval)
|
|
|
|
|
} else {
|
|
|
|
|
OperandValue::Pair(cg_place.llval, cg_place.llextra.unwrap())
|
|
|
|
|
};
|
2022-11-09 11:04:10 +11:00
|
|
|
|
OperandRef { val, layout: self.cx.layout_of(mk_ptr_ty(self.cx.tcx(), ty)) }
|
2018-12-23 19:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
|
pub fn codegen_scalar_binop(
|
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
|
bx: &mut Bx,
|
2018-07-10 13:28:39 +03:00
|
|
|
|
op: mir::BinOp,
|
2018-09-20 15:47:22 +02:00
|
|
|
|
lhs: Bx::Value,
|
|
|
|
|
rhs: Bx::Value,
|
2018-07-10 13:28:39 +03:00
|
|
|
|
input_ty: Ty<'tcx>,
|
2018-09-20 15:47:22 +02:00
|
|
|
|
) -> Bx::Value {
|
2019-06-09 12:53:47 +02:00
|
|
|
|
let is_float = input_ty.is_floating_point();
|
2015-11-10 22:05:11 +02:00
|
|
|
|
let is_signed = input_ty.is_signed();
|
|
|
|
|
match op {
|
|
|
|
|
mir::BinOp::Add => {
|
|
|
|
|
if is_float {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.fadd(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
} else {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.add(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2015-11-10 22:05:11 +02:00
|
|
|
|
mir::BinOp::Sub => {
|
|
|
|
|
if is_float {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.fsub(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
} else {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.sub(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2015-11-10 22:05:11 +02:00
|
|
|
|
mir::BinOp::Mul => {
|
|
|
|
|
if is_float {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.fmul(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
} else {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.mul(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2015-11-10 22:05:11 +02:00
|
|
|
|
mir::BinOp::Div => {
|
|
|
|
|
if is_float {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.fdiv(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
} else if is_signed {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.sdiv(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
} else {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.udiv(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2015-11-10 22:05:11 +02:00
|
|
|
|
mir::BinOp::Rem => {
|
|
|
|
|
if is_float {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.frem(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
} else if is_signed {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.srem(lhs, rhs)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
} else {
|
2018-01-05 07:12:32 +02:00
|
|
|
|
bx.urem(lhs, rhs)
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2015-11-10 22:05:11 +02:00
|
|
|
|
}
|
2018-01-05 07:12:32 +02:00
|
|
|
|
mir::BinOp::BitOr => bx.or(lhs, rhs),
|
|
|
|
|
mir::BinOp::BitAnd => bx.and(lhs, rhs),
|
|
|
|
|
mir::BinOp::BitXor => bx.xor(lhs, rhs),
|
2021-08-01 00:00:00 +00:00
|
|
|
|
mir::BinOp::Offset => {
|
|
|
|
|
let pointee_type = input_ty
|
|
|
|
|
.builtin_deref(true)
|
|
|
|
|
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", input_ty))
|
|
|
|
|
.ty;
|
|
|
|
|
let llty = bx.cx().backend_type(bx.cx().layout_of(pointee_type));
|
|
|
|
|
bx.inbounds_gep(llty, lhs, &[rhs])
|
|
|
|
|
}
|
2018-01-05 07:12:32 +02:00
|
|
|
|
mir::BinOp::Shl => common::build_unchecked_lshift(bx, lhs, rhs),
|
|
|
|
|
mir::BinOp::Shr => common::build_unchecked_rshift(bx, input_ty, lhs, rhs),
|
2016-10-04 18:34:03 +03:00
|
|
|
|
mir::BinOp::Ne
|
|
|
|
|
| mir::BinOp::Lt
|
|
|
|
|
| mir::BinOp::Gt
|
2019-09-08 22:17:10 -07:00
|
|
|
|
| mir::BinOp::Eq
|
|
|
|
|
| mir::BinOp::Le
|
|
|
|
|
| mir::BinOp::Ge => {
|
|
|
|
|
if is_float {
|
2016-10-04 18:34:03 +03:00
|
|
|
|
bx.fcmp(base::bin_op_to_fcmp_predicate(op.to_hir_binop()), lhs, rhs)
|
|
|
|
|
} else {
|
|
|
|
|
bx.icmp(base::bin_op_to_icmp_predicate(op.to_hir_binop(), is_signed), lhs, rhs)
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2016-10-04 18:34:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
|
pub fn codegen_fat_ptr_binop(
|
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
|
bx: &mut Bx,
|
2018-07-10 13:28:39 +03:00
|
|
|
|
op: mir::BinOp,
|
2018-09-20 15:47:22 +02:00
|
|
|
|
lhs_addr: Bx::Value,
|
|
|
|
|
lhs_extra: Bx::Value,
|
|
|
|
|
rhs_addr: Bx::Value,
|
|
|
|
|
rhs_extra: Bx::Value,
|
2018-07-10 13:28:39 +03:00
|
|
|
|
_input_ty: Ty<'tcx>,
|
2018-09-20 15:47:22 +02:00
|
|
|
|
) -> Bx::Value {
|
2016-10-04 18:34:03 +03:00
|
|
|
|
match op {
|
|
|
|
|
mir::BinOp::Eq => {
|
2018-10-05 15:08:49 +02:00
|
|
|
|
let lhs = bx.icmp(IntPredicate::IntEQ, lhs_addr, rhs_addr);
|
|
|
|
|
let rhs = bx.icmp(IntPredicate::IntEQ, lhs_extra, rhs_extra);
|
|
|
|
|
bx.and(lhs, rhs)
|
2016-10-04 18:34:03 +03:00
|
|
|
|
}
|
|
|
|
|
mir::BinOp::Ne => {
|
2018-10-05 15:08:49 +02:00
|
|
|
|
let lhs = bx.icmp(IntPredicate::IntNE, lhs_addr, rhs_addr);
|
|
|
|
|
let rhs = bx.icmp(IntPredicate::IntNE, lhs_extra, rhs_extra);
|
|
|
|
|
bx.or(lhs, rhs)
|
2016-10-04 18:34:03 +03:00
|
|
|
|
}
|
|
|
|
|
mir::BinOp::Le | mir::BinOp::Lt | mir::BinOp::Ge | mir::BinOp::Gt => {
|
|
|
|
|
// a OP b ~ a.0 STRICT(OP) b.0 | (a.0 == b.0 && a.1 OP a.1)
|
|
|
|
|
let (op, strict_op) = match op {
|
2018-08-20 18:16:51 +02:00
|
|
|
|
mir::BinOp::Lt => (IntPredicate::IntULT, IntPredicate::IntULT),
|
|
|
|
|
mir::BinOp::Le => (IntPredicate::IntULE, IntPredicate::IntULT),
|
|
|
|
|
mir::BinOp::Gt => (IntPredicate::IntUGT, IntPredicate::IntUGT),
|
|
|
|
|
mir::BinOp::Ge => (IntPredicate::IntUGE, IntPredicate::IntUGT),
|
2016-10-04 18:34:03 +03:00
|
|
|
|
_ => bug!(),
|
|
|
|
|
};
|
2018-10-05 15:08:49 +02:00
|
|
|
|
let lhs = bx.icmp(strict_op, lhs_addr, rhs_addr);
|
|
|
|
|
let and_lhs = bx.icmp(IntPredicate::IntEQ, lhs_addr, rhs_addr);
|
|
|
|
|
let and_rhs = bx.icmp(op, lhs_extra, rhs_extra);
|
|
|
|
|
let rhs = bx.and(and_lhs, and_rhs);
|
|
|
|
|
bx.or(lhs, rhs)
|
2016-10-04 18:34:03 +03:00
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
bug!("unexpected fat ptr binop");
|
2015-11-10 22:05:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-31 18:50:07 +13:00
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
pub fn codegen_scalar_checked_binop(
|
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
|
op: mir::BinOp,
|
|
|
|
|
lhs: Bx::Value,
|
|
|
|
|
rhs: Bx::Value,
|
|
|
|
|
input_ty: Ty<'tcx>,
|
|
|
|
|
) -> OperandValue<Bx::Value> {
|
2016-03-31 18:50:07 +13:00
|
|
|
|
let (val, of) = match op {
|
|
|
|
|
// These are checked using intrinsics
|
|
|
|
|
mir::BinOp::Add | mir::BinOp::Sub | mir::BinOp::Mul => {
|
|
|
|
|
let oop = match op {
|
|
|
|
|
mir::BinOp::Add => OverflowOp::Add,
|
|
|
|
|
mir::BinOp::Sub => OverflowOp::Sub,
|
|
|
|
|
mir::BinOp::Mul => OverflowOp::Mul,
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2018-11-24 16:44:17 +01:00
|
|
|
|
bx.checked_binop(oop, input_ty, lhs, rhs)
|
2016-03-31 18:50:07 +13:00
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
_ => bug!("Operator `{:?}` is not a checkable operator", op),
|
2016-03-31 18:50:07 +13:00
|
|
|
|
};
|
|
|
|
|
|
2016-05-25 11:55:44 +03:00
|
|
|
|
OperandValue::Pair(val, of)
|
2016-03-31 18:50:07 +13:00
|
|
|
|
}
|
2018-09-20 15:47:22 +02:00
|
|
|
|
}
|
2015-11-03 06:35:09 -05:00
|
|
|
|
|
2019-10-26 01:41:17 -04:00
|
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2019-11-04 19:52:19 -05:00
|
|
|
|
pub fn rvalue_creates_operand(&self, rvalue: &mir::Rvalue<'tcx>, span: Span) -> bool {
|
2017-03-08 20:03:04 +02:00
|
|
|
|
match *rvalue {
|
2023-04-01 01:46:36 -07:00
|
|
|
|
mir::Rvalue::Cast(mir::CastKind::Transmute, ref operand, cast_ty) => {
|
|
|
|
|
let operand_ty = operand.ty(self.mir, self.cx.tcx());
|
|
|
|
|
let cast_layout = self.cx.layout_of(self.monomorphize(cast_ty));
|
|
|
|
|
let operand_layout = self.cx.layout_of(self.monomorphize(operand_ty));
|
|
|
|
|
|
|
|
|
|
match (self.value_kind(operand_layout), self.value_kind(cast_layout)) {
|
|
|
|
|
// Can always load from a pointer as needed
|
|
|
|
|
(OperandValueKind::Ref, _) => true,
|
|
|
|
|
|
|
|
|
|
// Need to generate an `alloc` to get a pointer from an immediate
|
|
|
|
|
(OperandValueKind::Immediate(..) | OperandValueKind::Pair(..), OperandValueKind::Ref) => false,
|
|
|
|
|
|
2023-04-06 13:53:10 -07:00
|
|
|
|
// When we have scalar immediates, we can only convert things
|
|
|
|
|
// where the sizes match, to avoid endianness questions.
|
|
|
|
|
(OperandValueKind::Immediate(a), OperandValueKind::Immediate(b)) =>
|
|
|
|
|
a.size(self.cx) == b.size(self.cx),
|
|
|
|
|
(OperandValueKind::Pair(a0, a1), OperandValueKind::Pair(b0, b1)) =>
|
|
|
|
|
a0.size(self.cx) == b0.size(self.cx) && a1.size(self.cx) == b1.size(self.cx),
|
2023-04-01 01:46:36 -07:00
|
|
|
|
|
|
|
|
|
// Send mixings between scalars and pairs through the memory route
|
|
|
|
|
// FIXME: Maybe this could use insertvalue/extractvalue instead?
|
|
|
|
|
(OperandValueKind::Immediate(..), OperandValueKind::Pair(..)) |
|
|
|
|
|
(OperandValueKind::Pair(..), OperandValueKind::Immediate(..)) => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-08 20:03:04 +02:00
|
|
|
|
mir::Rvalue::Ref(..) |
|
2022-06-13 16:37:41 +03:00
|
|
|
|
mir::Rvalue::CopyForDeref(..) |
|
2018-12-23 19:00:58 +00:00
|
|
|
|
mir::Rvalue::AddressOf(..) |
|
2017-03-08 20:03:04 +02:00
|
|
|
|
mir::Rvalue::Len(..) |
|
|
|
|
|
mir::Rvalue::Cast(..) | // (*)
|
2021-09-06 18:33:23 +01:00
|
|
|
|
mir::Rvalue::ShallowInitBox(..) | // (*)
|
2017-03-08 20:03:04 +02:00
|
|
|
|
mir::Rvalue::BinaryOp(..) |
|
|
|
|
|
mir::Rvalue::CheckedBinaryOp(..) |
|
|
|
|
|
mir::Rvalue::UnaryOp(..) |
|
|
|
|
|
mir::Rvalue::Discriminant(..) |
|
2017-05-18 18:43:52 +03:00
|
|
|
|
mir::Rvalue::NullaryOp(..) |
|
2020-05-02 21:44:25 +02:00
|
|
|
|
mir::Rvalue::ThreadLocalRef(_) |
|
2017-03-08 20:03:04 +02:00
|
|
|
|
mir::Rvalue::Use(..) => // (*)
|
|
|
|
|
true,
|
|
|
|
|
mir::Rvalue::Repeat(..) |
|
|
|
|
|
mir::Rvalue::Aggregate(..) => {
|
2020-04-12 10:28:41 -07:00
|
|
|
|
let ty = rvalue.ty(self.mir, self.cx.tcx());
|
2020-10-24 02:21:18 +02:00
|
|
|
|
let ty = self.monomorphize(ty);
|
2019-08-03 15:59:25 -07:00
|
|
|
|
self.cx.spanned_layout_of(ty, span).is_zst()
|
2017-03-08 20:03:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-03 06:35:09 -05:00
|
|
|
|
|
2017-03-08 20:03:04 +02:00
|
|
|
|
// (*) this is only true if the type is suitable
|
|
|
|
|
}
|
2023-04-01 01:46:36 -07:00
|
|
|
|
|
|
|
|
|
/// Gets which variant of [`OperandValue`] is expected for a particular type.
|
|
|
|
|
fn value_kind(&self, layout: TyAndLayout<'tcx>) -> OperandValueKind {
|
|
|
|
|
if self.cx.is_backend_immediate(layout) {
|
|
|
|
|
debug_assert!(!self.cx.is_backend_scalar_pair(layout));
|
|
|
|
|
OperandValueKind::Immediate(match layout.abi {
|
2023-04-06 16:24:32 -07:00
|
|
|
|
abi::Abi::Scalar(s) => ScalarOrZst::Scalar(s),
|
|
|
|
|
abi::Abi::Vector { element, .. } => ScalarOrZst::Scalar(element),
|
|
|
|
|
_ if layout.is_zst() => ScalarOrZst::Zst,
|
|
|
|
|
x => span_bug!(self.mir.span, "Couldn't translate {x:?} as backend immediate"),
|
2023-04-01 01:46:36 -07:00
|
|
|
|
})
|
|
|
|
|
} else if self.cx.is_backend_scalar_pair(layout) {
|
|
|
|
|
let abi::Abi::ScalarPair(s1, s2) = layout.abi else {
|
2023-04-06 16:24:32 -07:00
|
|
|
|
span_bug!(
|
|
|
|
|
self.mir.span,
|
|
|
|
|
"Couldn't translate {:?} as backend scalar pair",
|
|
|
|
|
layout.abi,
|
|
|
|
|
);
|
2023-04-01 01:46:36 -07:00
|
|
|
|
};
|
|
|
|
|
OperandValueKind::Pair(s1, s2)
|
|
|
|
|
} else {
|
|
|
|
|
OperandValueKind::Ref
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 16:24:32 -07:00
|
|
|
|
/// The variants of this match [`OperandValue`], giving details about the
|
|
|
|
|
/// backend values that will be held in that other type.
|
2023-04-01 01:46:36 -07:00
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
|
enum OperandValueKind {
|
|
|
|
|
Ref,
|
2023-04-06 16:24:32 -07:00
|
|
|
|
Immediate(ScalarOrZst),
|
2023-04-01 01:46:36 -07:00
|
|
|
|
Pair(abi::Scalar, abi::Scalar),
|
2015-11-03 06:35:09 -05:00
|
|
|
|
}
|
2023-04-06 16:24:32 -07:00
|
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
|
enum ScalarOrZst {
|
|
|
|
|
Zst,
|
|
|
|
|
Scalar(abi::Scalar),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ScalarOrZst {
|
|
|
|
|
pub fn size(self, cx: &impl abi::HasDataLayout) -> abi::Size {
|
|
|
|
|
match self {
|
|
|
|
|
ScalarOrZst::Zst => abi::Size::ZERO,
|
|
|
|
|
ScalarOrZst::Scalar(s) => s.size(cx),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|