Auto merge of #96862 - oli-obk:enum_cast_mir, r=RalfJung
Change enum->int casts to not go through MIR casts. follow-up to https://github.com/rust-lang/rust/pull/96814 this simplifies all backends and even gives LLVM more information about the return value of `Rvalue::Discriminant`, enabling optimizations in more cases.
This commit is contained in:
commit
53792b9c5c
21 changed files with 238 additions and 143 deletions
|
@ -204,6 +204,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
|
|||
}
|
||||
|
||||
/// Obtain the actual discriminant of a value.
|
||||
#[instrument(level = "trace", skip(bx))]
|
||||
pub fn codegen_get_discr<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
||||
self,
|
||||
bx: &mut Bx,
|
||||
|
@ -420,12 +421,12 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
|
|||
}
|
||||
|
||||
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
||||
#[instrument(level = "trace", skip(self, bx))]
|
||||
pub fn codegen_place(
|
||||
&mut self,
|
||||
bx: &mut Bx,
|
||||
place_ref: mir::PlaceRef<'tcx>,
|
||||
) -> PlaceRef<'tcx, Bx::Value> {
|
||||
debug!("codegen_place(place_ref={:?})", place_ref);
|
||||
let cx = self.cx;
|
||||
let tcx = self.cx.tcx();
|
||||
|
||||
|
|
|
@ -12,17 +12,15 @@ use rustc_middle::ty::cast::{CastTy, IntTy};
|
|||
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
|
||||
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, Ty, TyCtxt};
|
||||
use rustc_span::source_map::{Span, DUMMY_SP};
|
||||
use rustc_target::abi::{Abi, Int, Variants};
|
||||
|
||||
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
||||
#[instrument(level = "trace", skip(self, bx))]
|
||||
pub fn codegen_rvalue(
|
||||
&mut self,
|
||||
mut bx: Bx,
|
||||
dest: PlaceRef<'tcx, Bx::Value>,
|
||||
rvalue: &mir::Rvalue<'tcx>,
|
||||
) -> Bx {
|
||||
debug!("codegen_rvalue(dest.llval={:?}, rvalue={:?})", dest.llval, rvalue);
|
||||
|
||||
match *rvalue {
|
||||
mir::Rvalue::Use(ref operand) => {
|
||||
let cg_operand = self.codegen_operand(&mut bx, operand);
|
||||
|
@ -285,74 +283,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
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");
|
||||
let ll_t_in = bx.cx().immediate_backend_type(operand.layout);
|
||||
match operand.layout.variants {
|
||||
Variants::Single { index } => {
|
||||
if let Some(discr) =
|
||||
operand.layout.ty.discriminant_for_variant(bx.tcx(), index)
|
||||
{
|
||||
let discr_layout = bx.cx().layout_of(discr.ty);
|
||||
let discr_t = bx.cx().immediate_backend_type(discr_layout);
|
||||
let discr_val = bx.cx().const_uint_big(discr_t, discr.val);
|
||||
let discr_val =
|
||||
bx.intcast(discr_val, ll_t_out, discr.ty.is_signed());
|
||||
|
||||
return (
|
||||
bx,
|
||||
OperandRef {
|
||||
val: OperandValue::Immediate(discr_val),
|
||||
layout: cast,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Variants::Multiple { .. } => {}
|
||||
}
|
||||
let llval = operand.immediate();
|
||||
|
||||
let mut signed = false;
|
||||
if let Abi::Scalar(scalar) = operand.layout.abi {
|
||||
if let Int(_, s) = scalar.primitive() {
|
||||
// We use `i1` for bytes that are always `0` or `1`,
|
||||
// e.g., `#[repr(i8)] enum E { A, B }`, but we can't
|
||||
// let LLVM interpret the `i1` as signed, because
|
||||
// then `i1 1` (i.e., E::B) is effectively `i8 -1`.
|
||||
signed = !scalar.is_bool() && s;
|
||||
|
||||
if !scalar.is_always_valid(bx.cx())
|
||||
&& scalar.valid_range(bx.cx()).end
|
||||
>= scalar.valid_range(bx.cx()).start
|
||||
{
|
||||
// We want `table[e as usize ± k]` to not
|
||||
// have bound checks, and this is the most
|
||||
// convenient place to put the `assume`s.
|
||||
if scalar.valid_range(bx.cx()).start > 0 {
|
||||
let enum_value_lower_bound = bx.cx().const_uint_big(
|
||||
ll_t_in,
|
||||
scalar.valid_range(bx.cx()).start,
|
||||
);
|
||||
let cmp_start = bx.icmp(
|
||||
IntPredicate::IntUGE,
|
||||
llval,
|
||||
enum_value_lower_bound,
|
||||
);
|
||||
bx.assume(cmp_start);
|
||||
}
|
||||
|
||||
let enum_value_upper_bound = bx
|
||||
.cx()
|
||||
.const_uint_big(ll_t_in, scalar.valid_range(bx.cx()).end);
|
||||
let cmp_end = bx.icmp(
|
||||
IntPredicate::IntULE,
|
||||
llval,
|
||||
enum_value_upper_bound,
|
||||
);
|
||||
bx.assume(cmp_end);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let newval = match (r_t_in, r_t_out) {
|
||||
(CastTy::Int(_), CastTy::Int(_)) => bx.intcast(llval, ll_t_out, signed),
|
||||
(CastTy::Int(i), CastTy::Int(_)) => {
|
||||
bx.intcast(llval, ll_t_out, i.is_signed())
|
||||
}
|
||||
(CastTy::Float, CastTy::Float) => {
|
||||
let srcsz = bx.cx().float_width(ll_t_in);
|
||||
let dstsz = bx.cx().float_width(ll_t_out);
|
||||
|
@ -364,8 +300,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
llval
|
||||
}
|
||||
}
|
||||
(CastTy::Int(_), CastTy::Float) => {
|
||||
if signed {
|
||||
(CastTy::Int(i), CastTy::Float) => {
|
||||
if i.is_signed() {
|
||||
bx.sitofp(llval, ll_t_out)
|
||||
} else {
|
||||
bx.uitofp(llval, ll_t_out)
|
||||
|
@ -374,8 +310,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
(CastTy::Ptr(_) | CastTy::FnPtr, CastTy::Ptr(_)) => {
|
||||
bx.pointercast(llval, ll_t_out)
|
||||
}
|
||||
(CastTy::Int(_), CastTy::Ptr(_)) => {
|
||||
let usize_llval = bx.intcast(llval, bx.cx().type_isize(), signed);
|
||||
(CastTy::Int(i), CastTy::Ptr(_)) => {
|
||||
let usize_llval =
|
||||
bx.intcast(llval, bx.cx().type_isize(), i.is_signed());
|
||||
bx.inttoptr(usize_llval, ll_t_out)
|
||||
}
|
||||
(CastTy::Float, CastTy::Int(IntTy::I)) => {
|
||||
|
|
|
@ -6,9 +6,8 @@ use crate::traits::BuilderMethods;
|
|||
use crate::traits::*;
|
||||
|
||||
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
||||
#[instrument(level = "debug", skip(self, bx))]
|
||||
pub fn codegen_statement(&mut self, mut bx: Bx, statement: &mir::Statement<'tcx>) -> Bx {
|
||||
debug!("codegen_statement(statement={:?})", statement);
|
||||
|
||||
self.set_debug_loc(&mut bx, statement.source_info);
|
||||
match statement.kind {
|
||||
mir::StatementKind::Assign(box (ref place, ref rvalue)) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue