1
Fork 0

Store pairs in SSA

This commit is contained in:
Santiago Pastorino 2020-07-02 19:23:21 -03:00
parent 6e2ac83a0c
commit aa80ed0a2d
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
6 changed files with 131 additions and 4 deletions

View file

@ -78,6 +78,10 @@ pub(super) fn add_local_place_comments<'tcx>(
assert_eq!(local, place_local);
("ssa", Cow::Owned(format!(",var={}", var.index())))
}
CPlaceInner::VarPair(place_local, var1, var2) => {
assert_eq!(local, place_local);
("ssa", Cow::Owned(format!(",var=({}, {})", var1.index(), var2.index())))
}
CPlaceInner::Addr(ptr, meta) => {
let meta = if let Some(meta) = meta {
Cow::Owned(format!(",meta={}", meta))

View file

@ -290,7 +290,11 @@ fn local_place<'tcx>(
is_ssa: bool,
) -> CPlace<'tcx> {
let place = if is_ssa {
CPlace::new_var(fx, local, layout)
if let rustc_target::abi::Abi::ScalarPair(_, _) = layout.abi {
CPlace::new_var_pair(fx, local, layout)
} else {
CPlace::new_var(fx, local, layout)
}
} else {
CPlace::new_stack_slot(fx, layout)
};

View file

@ -11,7 +11,8 @@ pub(crate) enum SsaKind {
pub(crate) fn analyze(fx: &FunctionCx<'_, '_, impl Backend>) -> IndexVec<Local, SsaKind> {
let mut flag_map = fx.mir.local_decls.iter().map(|local_decl| {
if fx.clif_type(fx.monomorphize(&local_decl.ty)).is_some() {
let ty = fx.monomorphize(&local_decl.ty);
if fx.clif_type(ty).is_some() || fx.clif_pair_type(ty).is_some() {
SsaKind::Ssa
} else {
SsaKind::NotSsa

View file

@ -67,6 +67,19 @@ fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Typ
})
}
fn clif_pair_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<(types::Type, types::Type)> {
Some(match ty.kind {
ty::Tuple(substs) if substs.len() == 2 => {
let mut types = substs.types();
(
clif_type_from_ty(tcx, types.next().unwrap())?,
clif_type_from_ty(tcx, types.next().unwrap())?,
)
}
_ => return None,
})
}
/// Is a pointer to this type a fat ptr?
pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
let ptr_ty = tcx.mk_ptr(TypeAndMut { ty, mutbl: rustc_hir::Mutability::Not });
@ -321,6 +334,10 @@ impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
clif_type_from_ty(self.tcx, ty)
}
pub(crate) fn clif_pair_type(&self, ty: Ty<'tcx>) -> Option<(Type, Type)> {
clif_pair_type_from_ty(self.tcx, ty)
}
pub(crate) fn get_block(&self, bb: BasicBlock) -> Block {
*self.block_map.get(bb).unwrap()
}

View file

@ -405,6 +405,11 @@ fn place_location<'tcx>(
AttributeValue::Exprloc(Expression::new())
}
}
CPlaceInner::VarPair(_, _, _) => {
// FIXME implement this
AttributeValue::Exprloc(Expression::new())
}
CPlaceInner::Addr(_, _) => {
// FIXME implement this (used by arguments and returns)

View file

@ -179,12 +179,25 @@ impl<'tcx> CValue<'tcx> {
_ => unreachable!("value_field for ByVal with abi {:?}", layout.abi),
}
}
CValueInner::ByValPair(val1, val2) => {
match layout.abi {
Abi::ScalarPair(_, _) => {
let val = match field.as_u32() {
0 => val1,
1 => val2,
_ => bug!("field should be 0 or 1"),
};
let field_layout = layout.field(&*fx, usize::from(field));
CValue::by_val(val, field_layout)
}
_ => unreachable!("value_field for ByValPair with abi {:?}", layout.abi),
}
}
CValueInner::ByRef(ptr, None) => {
let (field_ptr, field_layout) = codegen_field(fx, ptr, None, layout, field);
CValue::by_ref(field_ptr, field_layout)
}
CValueInner::ByRef(_, Some(_)) => todo!(),
_ => bug!("place_field for {:?}", self),
}
}
@ -258,6 +271,7 @@ pub(crate) struct CPlace<'tcx> {
#[derive(Debug, Copy, Clone)]
pub(crate) enum CPlaceInner {
Var(Local, Variable),
VarPair(Local, Variable, Variable),
Addr(Pointer, Option<Value>),
}
@ -312,6 +326,25 @@ impl<'tcx> CPlace<'tcx> {
}
}
pub(crate) fn new_var_pair(
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
local: Local,
layout: TyAndLayout<'tcx>,
) -> CPlace<'tcx> {
let var1 = Variable::with_u32(fx.next_ssa_var);
fx.next_ssa_var += 1;
let var2 = Variable::with_u32(fx.next_ssa_var);
fx.next_ssa_var += 1;
let (ty1, ty2) = fx.clif_pair_type(layout.ty).unwrap();
fx.bcx.declare_var(var1, ty1);
fx.bcx.declare_var(var2, ty2);
CPlace {
inner: CPlaceInner::VarPair(local, var1, var2),
layout,
}
}
pub(crate) fn for_ptr(ptr: Pointer, layout: TyAndLayout<'tcx>) -> CPlace<'tcx> {
CPlace {
inner: CPlaceInner::Addr(ptr, None),
@ -334,6 +367,13 @@ impl<'tcx> CPlace<'tcx> {
fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::new(var.index()));
CValue::by_val(val, layout)
}
CPlaceInner::VarPair(_local, var1, var2) => {
let val1 = fx.bcx.use_var(var1);
fx.bcx.set_val_label(val1, cranelift_codegen::ir::ValueLabel::new(var1.index()));
let val2 = fx.bcx.use_var(var2);
fx.bcx.set_val_label(val2, cranelift_codegen::ir::ValueLabel::new(var2.index()));
CValue::by_val_pair(val1, val2, layout)
}
CPlaceInner::Addr(ptr, extra) => {
if let Some(extra) = extra {
CValue::by_ref_unsized(ptr, extra, layout)
@ -354,7 +394,8 @@ impl<'tcx> CPlace<'tcx> {
pub(crate) fn to_ptr_maybe_unsized(self) -> (Pointer, Option<Value>) {
match self.inner {
CPlaceInner::Addr(ptr, extra) => (ptr, extra),
CPlaceInner::Var(_, _) => bug!("Expected CPlace::Addr, found {:?}", self),
CPlaceInner::Var(_, _)
| CPlaceInner::VarPair(_, _, _) => bug!("Expected CPlace::Addr, found {:?}", self),
}
}
@ -466,6 +507,45 @@ impl<'tcx> CPlace<'tcx> {
fx.bcx.def_var(var, data);
return;
}
CPlaceInner::VarPair(_local, var1, var2) => {
let (data1, data2) = CValue(from.0, dst_layout).load_scalar_pair(fx);
let (dst_ty1, dst_ty2) = fx.clif_pair_type(self.layout().ty).unwrap();
let src_ty1 = fx.bcx.func.dfg.value_type(data1);
let data = match (src_ty1, dst_ty1) {
(_, _) if src_ty1 == dst_ty1 => data1,
// This is a `write_cvalue_transmute`.
(types::I32, types::F32) | (types::F32, types::I32)
| (types::I64, types::F64) | (types::F64, types::I64) => {
fx.bcx.ins().bitcast(dst_ty1, data1)
}
_ if src_ty1.is_vector() && dst_ty1.is_vector() => {
fx.bcx.ins().raw_bitcast(dst_ty1, data1)
}
_ => unreachable!("write_cvalue_transmute: {:?} -> {:?}", src_ty1, dst_ty1),
};
fx.bcx.set_val_label(data, cranelift_codegen::ir::ValueLabel::new(var1.index()));
fx.bcx.def_var(var1, data);
let src_ty2 = fx.bcx.func.dfg.value_type(data2);
let data = match (src_ty2, dst_ty2) {
(_, _) if src_ty2 == dst_ty2 => data2,
// This is a `write_cvalue_transmute`.
(types::I32, types::F32) | (types::F32, types::I32)
| (types::I64, types::F64) | (types::F64, types::I64) => {
fx.bcx.ins().bitcast(dst_ty2, data2)
}
_ if src_ty2.is_vector() && dst_ty2.is_vector() => {
fx.bcx.ins().raw_bitcast(dst_ty2, data2)
}
_ => unreachable!("write_cvalue_transmute: {:?} -> {:?}", src_ty2, dst_ty2),
};
fx.bcx.set_val_label(data, cranelift_codegen::ir::ValueLabel::new(var2.index()));
fx.bcx.def_var(var2, data);
return;
}
CPlaceInner::Addr(ptr, None) => {
if dst_layout.size == Size::ZERO || dst_layout.abi == Abi::Uninhabited {
return;
@ -529,6 +609,22 @@ impl<'tcx> CPlace<'tcx> {
field: mir::Field,
) -> CPlace<'tcx> {
let layout = self.layout();
if let CPlaceInner::VarPair(local, var1, var2) = self.inner {
let layout = layout.field(&*fx, field.index());
match field.as_u32() {
0 => return CPlace {
inner: CPlaceInner::Var(local, var1),
layout,
},
1 => return CPlace {
inner: CPlaceInner::Var(local, var2),
layout,
},
_ => unreachable!("field should be 0 or 1"),
}
}
let (base, extra) = self.to_ptr_maybe_unsized();
let (field_ptr, field_layout) = codegen_field(fx, base, extra, layout, field);