Cleanup the InstSimplify MIR transformation

This commit is contained in:
Yotam Ofek 2025-04-10 15:26:27 +00:00
parent 69b3959afe
commit 9491242ff7

View file

@ -39,26 +39,26 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
attr::contains_name(tcx.hir_krate_attrs(), sym::rustc_preserve_ub_checks); attr::contains_name(tcx.hir_krate_attrs(), sym::rustc_preserve_ub_checks);
for block in body.basic_blocks.as_mut() { for block in body.basic_blocks.as_mut() {
for statement in block.statements.iter_mut() { for statement in block.statements.iter_mut() {
match statement.kind { let StatementKind::Assign(box (.., rvalue)) = &mut statement.kind else {
StatementKind::Assign(box (_place, ref mut rvalue)) => { continue;
if !preserve_ub_checks { };
ctx.simplify_ub_check(rvalue);
} if !preserve_ub_checks {
ctx.simplify_bool_cmp(rvalue); ctx.simplify_ub_check(rvalue);
ctx.simplify_ref_deref(rvalue);
ctx.simplify_ptr_aggregate(rvalue);
ctx.simplify_cast(rvalue);
ctx.simplify_repeated_aggregate(rvalue);
ctx.simplify_repeat_once(rvalue);
}
_ => {}
} }
ctx.simplify_bool_cmp(rvalue);
ctx.simplify_ref_deref(rvalue);
ctx.simplify_ptr_aggregate(rvalue);
ctx.simplify_cast(rvalue);
ctx.simplify_repeated_aggregate(rvalue);
ctx.simplify_repeat_once(rvalue);
} }
ctx.simplify_primitive_clone(block.terminator.as_mut().unwrap(), &mut block.statements); let terminator = block.terminator.as_mut().unwrap();
ctx.simplify_intrinsic_assert(block.terminator.as_mut().unwrap()); ctx.simplify_primitive_clone(terminator, &mut block.statements);
ctx.simplify_nounwind_call(block.terminator.as_mut().unwrap()); ctx.simplify_intrinsic_assert(terminator);
simplify_duplicate_switch_targets(block.terminator.as_mut().unwrap()); ctx.simplify_nounwind_call(terminator);
simplify_duplicate_switch_targets(terminator);
} }
} }
@ -105,43 +105,34 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
/// Transform boolean comparisons into logical operations. /// Transform boolean comparisons into logical operations.
fn simplify_bool_cmp(&self, rvalue: &mut Rvalue<'tcx>) { fn simplify_bool_cmp(&self, rvalue: &mut Rvalue<'tcx>) {
match rvalue { let Rvalue::BinaryOp(op @ (BinOp::Eq | BinOp::Ne), box (a, b)) = &*rvalue else { return };
Rvalue::BinaryOp(op @ (BinOp::Eq | BinOp::Ne), box (a, b)) => { *rvalue = match (op, self.try_eval_bool(a), self.try_eval_bool(b)) {
let new = match (op, self.try_eval_bool(a), self.try_eval_bool(b)) { // Transform "Eq(a, true)" ==> "a"
// Transform "Eq(a, true)" ==> "a" (BinOp::Eq, _, Some(true)) => Rvalue::Use(a.clone()),
(BinOp::Eq, _, Some(true)) => Some(Rvalue::Use(a.clone())),
// Transform "Ne(a, false)" ==> "a" // Transform "Ne(a, false)" ==> "a"
(BinOp::Ne, _, Some(false)) => Some(Rvalue::Use(a.clone())), (BinOp::Ne, _, Some(false)) => Rvalue::Use(a.clone()),
// Transform "Eq(true, b)" ==> "b" // Transform "Eq(true, b)" ==> "b"
(BinOp::Eq, Some(true), _) => Some(Rvalue::Use(b.clone())), (BinOp::Eq, Some(true), _) => Rvalue::Use(b.clone()),
// Transform "Ne(false, b)" ==> "b" // Transform "Ne(false, b)" ==> "b"
(BinOp::Ne, Some(false), _) => Some(Rvalue::Use(b.clone())), (BinOp::Ne, Some(false), _) => Rvalue::Use(b.clone()),
// Transform "Eq(false, b)" ==> "Not(b)" // Transform "Eq(false, b)" ==> "Not(b)"
(BinOp::Eq, Some(false), _) => Some(Rvalue::UnaryOp(UnOp::Not, b.clone())), (BinOp::Eq, Some(false), _) => Rvalue::UnaryOp(UnOp::Not, b.clone()),
// Transform "Ne(true, b)" ==> "Not(b)" // Transform "Ne(true, b)" ==> "Not(b)"
(BinOp::Ne, Some(true), _) => Some(Rvalue::UnaryOp(UnOp::Not, b.clone())), (BinOp::Ne, Some(true), _) => Rvalue::UnaryOp(UnOp::Not, b.clone()),
// Transform "Eq(a, false)" ==> "Not(a)" // Transform "Eq(a, false)" ==> "Not(a)"
(BinOp::Eq, _, Some(false)) => Some(Rvalue::UnaryOp(UnOp::Not, a.clone())), (BinOp::Eq, _, Some(false)) => Rvalue::UnaryOp(UnOp::Not, a.clone()),
// Transform "Ne(a, true)" ==> "Not(a)" // Transform "Ne(a, true)" ==> "Not(a)"
(BinOp::Ne, _, Some(true)) => Some(Rvalue::UnaryOp(UnOp::Not, a.clone())), (BinOp::Ne, _, Some(true)) => Rvalue::UnaryOp(UnOp::Not, a.clone()),
_ => None, _ => return,
}; };
if let Some(new) = new {
*rvalue = new;
}
}
_ => {}
}
} }
fn try_eval_bool(&self, a: &Operand<'_>) -> Option<bool> { fn try_eval_bool(&self, a: &Operand<'_>) -> Option<bool> {
@ -151,64 +142,58 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
/// Transform `&(*a)` ==> `a`. /// Transform `&(*a)` ==> `a`.
fn simplify_ref_deref(&self, rvalue: &mut Rvalue<'tcx>) { fn simplify_ref_deref(&self, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) = rvalue { if let Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) = rvalue
if let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection() { && let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection()
if rvalue.ty(self.local_decls, self.tcx) != base.ty(self.local_decls, self.tcx).ty { && rvalue.ty(self.local_decls, self.tcx) == base.ty(self.local_decls, self.tcx).ty
return; {
} *rvalue = Rvalue::Use(Operand::Copy(Place {
local: base.local,
*rvalue = Rvalue::Use(Operand::Copy(Place { projection: self.tcx.mk_place_elems(base.projection),
local: base.local, }));
projection: self.tcx.mk_place_elems(base.projection),
}));
}
} }
} }
/// Transform `Aggregate(RawPtr, [p, ()])` ==> `Cast(PtrToPtr, p)`. /// Transform `Aggregate(RawPtr, [p, ()])` ==> `Cast(PtrToPtr, p)`.
fn simplify_ptr_aggregate(&self, rvalue: &mut Rvalue<'tcx>) { fn simplify_ptr_aggregate(&self, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::Aggregate(box AggregateKind::RawPtr(pointee_ty, mutability), fields) = rvalue if let Rvalue::Aggregate(box AggregateKind::RawPtr(pointee_ty, mutability), fields) = rvalue
&& let meta_ty = fields.raw[1].ty(self.local_decls, self.tcx)
&& meta_ty.is_unit()
{ {
let meta_ty = fields.raw[1].ty(self.local_decls, self.tcx); // The mutable borrows we're holding prevent printing `rvalue` here
if meta_ty.is_unit() { let mut fields = std::mem::take(fields);
// The mutable borrows we're holding prevent printing `rvalue` here let _meta = fields.pop().unwrap();
let mut fields = std::mem::take(fields); let data = fields.pop().unwrap();
let _meta = fields.pop().unwrap(); let ptr_ty = Ty::new_ptr(self.tcx, *pointee_ty, *mutability);
let data = fields.pop().unwrap(); *rvalue = Rvalue::Cast(CastKind::PtrToPtr, data, ptr_ty);
let ptr_ty = Ty::new_ptr(self.tcx, *pointee_ty, *mutability);
*rvalue = Rvalue::Cast(CastKind::PtrToPtr, data, ptr_ty);
}
} }
} }
fn simplify_ub_check(&self, rvalue: &mut Rvalue<'tcx>) { fn simplify_ub_check(&self, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue { let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue else { return };
let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks());
let constant = ConstOperand { span: DUMMY_SP, const_, user_ty: None }; let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks());
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant))); let constant = ConstOperand { span: DUMMY_SP, const_, user_ty: None };
} *rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
} }
fn simplify_cast(&self, rvalue: &mut Rvalue<'tcx>) { fn simplify_cast(&self, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::Cast(kind, operand, cast_ty) = rvalue { let Rvalue::Cast(kind, operand, cast_ty) = rvalue else { return };
let operand_ty = operand.ty(self.local_decls, self.tcx);
if operand_ty == *cast_ty { let operand_ty = operand.ty(self.local_decls, self.tcx);
*rvalue = Rvalue::Use(operand.clone()); if operand_ty == *cast_ty {
} else if *kind == CastKind::Transmute { *rvalue = Rvalue::Use(operand.clone());
// Transmuting an integer to another integer is just a signedness cast } else if *kind == CastKind::Transmute
if let (ty::Int(int), ty::Uint(uint)) | (ty::Uint(uint), ty::Int(int)) = // Transmuting an integer to another integer is just a signedness cast
(operand_ty.kind(), cast_ty.kind()) && let (ty::Int(int), ty::Uint(uint)) | (ty::Uint(uint), ty::Int(int)) =
&& int.bit_width() == uint.bit_width() (operand_ty.kind(), cast_ty.kind())
{ && int.bit_width() == uint.bit_width()
// The width check isn't strictly necessary, as different widths {
// are UB and thus we'd be allowed to turn it into a cast anyway. // The width check isn't strictly necessary, as different widths
// But let's keep the UB around for codegen to exploit later. // are UB and thus we'd be allowed to turn it into a cast anyway.
// (If `CastKind::Transmute` ever becomes *not* UB for mismatched sizes, // But let's keep the UB around for codegen to exploit later.
// then the width check is necessary for big-endian correctness.) // (If `CastKind::Transmute` ever becomes *not* UB for mismatched sizes,
*kind = CastKind::IntToInt; // then the width check is necessary for big-endian correctness.)
return; *kind = CastKind::IntToInt;
}
}
} }
} }
@ -277,7 +262,7 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
} }
fn simplify_nounwind_call(&self, terminator: &mut Terminator<'tcx>) { fn simplify_nounwind_call(&self, terminator: &mut Terminator<'tcx>) {
let TerminatorKind::Call { func, unwind, .. } = &mut terminator.kind else { let TerminatorKind::Call { ref func, ref mut unwind, .. } = terminator.kind else {
return; return;
}; };
@ -290,7 +275,7 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
ty::FnDef(..) => body_ty.fn_sig(self.tcx).abi(), ty::FnDef(..) => body_ty.fn_sig(self.tcx).abi(),
ty::Closure(..) => ExternAbi::RustCall, ty::Closure(..) => ExternAbi::RustCall,
ty::Coroutine(..) => ExternAbi::Rust, ty::Coroutine(..) => ExternAbi::Rust,
_ => bug!("unexpected body ty: {:?}", body_ty), _ => bug!("unexpected body ty: {body_ty:?}"),
}; };
if !layout::fn_can_unwind(self.tcx, Some(def_id), body_abi) { if !layout::fn_can_unwind(self.tcx, Some(def_id), body_abi) {
@ -299,10 +284,9 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
} }
fn simplify_intrinsic_assert(&self, terminator: &mut Terminator<'tcx>) { fn simplify_intrinsic_assert(&self, terminator: &mut Terminator<'tcx>) {
let TerminatorKind::Call { func, target, .. } = &mut terminator.kind else { let TerminatorKind::Call { ref func, target: ref mut target @ Some(target_block), .. } =
return; terminator.kind
}; else {
let Some(target_block) = target else {
return; return;
}; };
let func_ty = func.ty(self.local_decls, self.tcx); let func_ty = func.ty(self.local_decls, self.tcx);
@ -310,12 +294,10 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
return; return;
}; };
// The intrinsics we are interested in have one generic parameter // The intrinsics we are interested in have one generic parameter
if args.is_empty() { let [arg, ..] = args[..] else { return };
return;
}
let known_is_valid = let known_is_valid =
intrinsic_assert_panics(self.tcx, self.typing_env, args[0], intrinsic_name); intrinsic_assert_panics(self.tcx, self.typing_env, arg, intrinsic_name);
match known_is_valid { match known_is_valid {
// We don't know the layout or it's not validity assertion at all, don't touch it // We don't know the layout or it's not validity assertion at all, don't touch it
None => {} None => {}
@ -325,7 +307,7 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
} }
Some(false) => { Some(false) => {
// If we know the assert does not panic, turn the call into a Goto // If we know the assert does not panic, turn the call into a Goto
terminator.kind = TerminatorKind::Goto { target: *target_block }; terminator.kind = TerminatorKind::Goto { target: target_block };
} }
} }
} }
@ -346,9 +328,7 @@ fn resolve_rust_intrinsic<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
func_ty: Ty<'tcx>, func_ty: Ty<'tcx>,
) -> Option<(Symbol, GenericArgsRef<'tcx>)> { ) -> Option<(Symbol, GenericArgsRef<'tcx>)> {
if let ty::FnDef(def_id, args) = *func_ty.kind() { let ty::FnDef(def_id, args) = *func_ty.kind() else { return None };
let intrinsic = tcx.intrinsic(def_id)?; let intrinsic = tcx.intrinsic(def_id)?;
return Some((intrinsic.name, args)); Some((intrinsic.name, args))
}
None
} }