Skip to content

Commit 148f5c1

Browse files
authored
Rollup merge of #116549 - DaniPopes:miropts-let-chains, r=oli-obk
Simplify some mir passes by using let chains
2 parents 2266e79 + 47ebffa commit 148f5c1

File tree

3 files changed

+43
-86
lines changed

3 files changed

+43
-86
lines changed

compiler/rustc_mir_transform/src/lower_intrinsics.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
33
use crate::MirPass;
44
use rustc_middle::mir::*;
5-
use rustc_middle::ty::GenericArgsRef;
6-
use rustc_middle::ty::{self, Ty, TyCtxt};
7-
use rustc_span::symbol::{sym, Symbol};
5+
use rustc_middle::ty::{self, TyCtxt};
6+
use rustc_span::symbol::sym;
87
use rustc_target::abi::{FieldIdx, VariantIdx};
98

109
pub struct LowerIntrinsics;
@@ -16,12 +15,10 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
1615
let terminator = block.terminator.as_mut().unwrap();
1716
if let TerminatorKind::Call { func, args, destination, target, .. } =
1817
&mut terminator.kind
18+
&& let ty::FnDef(def_id, generic_args) = *func.ty(local_decls, tcx).kind()
19+
&& tcx.is_intrinsic(def_id)
1920
{
20-
let func_ty = func.ty(local_decls, tcx);
21-
let Some((intrinsic_name, generic_args)) = resolve_rust_intrinsic(tcx, func_ty)
22-
else {
23-
continue;
24-
};
21+
let intrinsic_name = tcx.item_name(def_id);
2522
match intrinsic_name {
2623
sym::unreachable => {
2724
terminator.kind = TerminatorKind::Unreachable;
@@ -309,15 +306,3 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
309306
}
310307
}
311308
}
312-
313-
fn resolve_rust_intrinsic<'tcx>(
314-
tcx: TyCtxt<'tcx>,
315-
func_ty: Ty<'tcx>,
316-
) -> Option<(Symbol, GenericArgsRef<'tcx>)> {
317-
if let ty::FnDef(def_id, args) = *func_ty.kind() {
318-
if tcx.is_intrinsic(def_id) {
319-
return Some((tcx.item_name(def_id), args));
320-
}
321-
}
322-
None
323-
}

compiler/rustc_mir_transform/src/lower_slice_len.rs

+28-51
Original file line numberDiff line numberDiff line change
@@ -34,67 +34,44 @@ pub fn lower_slice_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3434
}
3535
}
3636

37-
struct SliceLenPatchInformation<'tcx> {
38-
add_statement: Statement<'tcx>,
39-
new_terminator_kind: TerminatorKind<'tcx>,
40-
}
41-
4237
fn lower_slice_len_call<'tcx>(
4338
tcx: TyCtxt<'tcx>,
4439
block: &mut BasicBlockData<'tcx>,
4540
local_decls: &IndexSlice<Local, LocalDecl<'tcx>>,
4641
slice_len_fn_item_def_id: DefId,
4742
) {
48-
let mut patch_found: Option<SliceLenPatchInformation<'_>> = None;
49-
5043
let terminator = block.terminator();
51-
match &terminator.kind {
52-
TerminatorKind::Call {
53-
func,
54-
args,
55-
destination,
56-
target: Some(bb),
57-
call_source: CallSource::Normal,
58-
..
59-
} => {
60-
// some heuristics for fast rejection
61-
if args.len() != 1 {
62-
return;
63-
}
64-
let Some(arg) = args[0].place() else { return };
65-
let func_ty = func.ty(local_decls, tcx);
66-
match func_ty.kind() {
67-
ty::FnDef(fn_def_id, _) if fn_def_id == &slice_len_fn_item_def_id => {
68-
// perform modifications
69-
// from something like `_5 = core::slice::<impl [u8]>::len(move _6) -> bb1`
70-
// into:
71-
// ```
72-
// _5 = Len(*_6)
73-
// goto bb1
74-
// ```
44+
if let TerminatorKind::Call {
45+
func,
46+
args,
47+
destination,
48+
target: Some(bb),
49+
call_source: CallSource::Normal,
50+
..
51+
} = &terminator.kind
52+
// some heuristics for fast rejection
53+
&& let [arg] = &args[..]
54+
&& let Some(arg) = arg.place()
55+
&& let ty::FnDef(fn_def_id, _) = func.ty(local_decls, tcx).kind()
56+
&& *fn_def_id == slice_len_fn_item_def_id
57+
{
58+
// perform modifications from something like:
59+
// _5 = core::slice::<impl [u8]>::len(move _6) -> bb1
60+
// into:
61+
// _5 = Len(*_6)
62+
// goto bb1
7563

76-
// make new RValue for Len
77-
let deref_arg = tcx.mk_place_deref(arg);
78-
let r_value = Rvalue::Len(deref_arg);
79-
let len_statement_kind =
80-
StatementKind::Assign(Box::new((*destination, r_value)));
81-
let add_statement =
82-
Statement { kind: len_statement_kind, source_info: terminator.source_info };
64+
// make new RValue for Len
65+
let deref_arg = tcx.mk_place_deref(arg);
66+
let r_value = Rvalue::Len(deref_arg);
67+
let len_statement_kind =
68+
StatementKind::Assign(Box::new((*destination, r_value)));
69+
let add_statement =
70+
Statement { kind: len_statement_kind, source_info: terminator.source_info };
8371

84-
// modify terminator into simple Goto
85-
let new_terminator_kind = TerminatorKind::Goto { target: *bb };
86-
87-
let patch = SliceLenPatchInformation { add_statement, new_terminator_kind };
88-
89-
patch_found = Some(patch);
90-
}
91-
_ => {}
92-
}
93-
}
94-
_ => {}
95-
}
72+
// modify terminator into simple Goto
73+
let new_terminator_kind = TerminatorKind::Goto { target: *bb };
9674

97-
if let Some(SliceLenPatchInformation { add_statement, new_terminator_kind }) = patch_found {
9875
block.statements.push(add_statement);
9976
block.terminator_mut().kind = new_terminator_kind;
10077
}

compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,17 @@ fn get_switched_on_type<'tcx>(
3030
let terminator = block_data.terminator();
3131

3232
// Only bother checking blocks which terminate by switching on a local.
33-
if let Some(local) = get_discriminant_local(&terminator.kind) {
34-
let stmt_before_term = (!block_data.statements.is_empty())
35-
.then(|| &block_data.statements[block_data.statements.len() - 1].kind);
36-
37-
if let Some(StatementKind::Assign(box (l, Rvalue::Discriminant(place)))) = stmt_before_term
38-
{
39-
if l.as_local() == Some(local) {
40-
let ty = place.ty(body, tcx).ty;
41-
if ty.is_enum() {
42-
return Some(ty);
43-
}
44-
}
45-
}
33+
if let Some(local) = get_discriminant_local(&terminator.kind)
34+
&& let [.., stmt_before_term] = &block_data.statements[..]
35+
&& let StatementKind::Assign(box (l, Rvalue::Discriminant(place))) = stmt_before_term.kind
36+
&& l.as_local() == Some(local)
37+
&& let ty = place.ty(body, tcx).ty
38+
&& ty.is_enum()
39+
{
40+
Some(ty)
41+
} else {
42+
None
4643
}
47-
48-
None
4944
}
5045

5146
fn variant_discriminants<'tcx>(

0 commit comments

Comments
 (0)