Skip to content

Commit 0da8676

Browse files
committed
Run InstSimplify before UnreachablePropagation
1 parent 5baf1e1 commit 0da8676

File tree

57 files changed

+147
-149
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+147
-149
lines changed

compiler/rustc_mir_transform/src/instsimplify.rs

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,59 @@ use rustc_span::symbol::Symbol;
1111
use rustc_target::abi::FieldIdx;
1212
use rustc_target::spec::abi::Abi;
1313

14-
pub struct InstSimplify;
14+
pub enum InstSimplify {
15+
BeforeUnreachablePropagation,
16+
Final,
17+
}
1518

1619
impl<'tcx> MirPass<'tcx> for InstSimplify {
20+
fn name(&self) -> &'static str {
21+
match &self {
22+
InstSimplify::BeforeUnreachablePropagation => {
23+
"InstSimplify-before-unreachable-propagation"
24+
}
25+
InstSimplify::Final => "InstSimplify-final",
26+
}
27+
}
28+
1729
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
1830
sess.mir_opt_level() > 0
1931
}
2032

2133
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
22-
let ctx = InstSimplifyContext {
23-
tcx,
24-
local_decls: &body.local_decls,
25-
param_env: tcx.param_env_reveal_all_normalized(body.source.def_id()),
26-
};
27-
let preserve_ub_checks =
28-
attr::contains_name(tcx.hir().krate_attrs(), sym::rustc_preserve_ub_checks);
29-
for block in body.basic_blocks.as_mut() {
30-
for statement in block.statements.iter_mut() {
31-
match statement.kind {
32-
StatementKind::Assign(box (_place, ref mut rvalue)) => {
33-
if !preserve_ub_checks {
34-
ctx.simplify_ub_check(&statement.source_info, rvalue);
35-
}
36-
ctx.simplify_bool_cmp(&statement.source_info, rvalue);
37-
ctx.simplify_ref_deref(&statement.source_info, rvalue);
38-
ctx.simplify_len(&statement.source_info, rvalue);
39-
ctx.simplify_cast(rvalue);
34+
inst_simplify(tcx, body);
35+
}
36+
}
37+
38+
fn inst_simplify<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
39+
let ctx = InstSimplifyContext {
40+
tcx,
41+
local_decls: &body.local_decls,
42+
param_env: tcx.param_env_reveal_all_normalized(body.source.def_id()),
43+
};
44+
let preserve_ub_checks =
45+
attr::contains_name(tcx.hir().krate_attrs(), sym::rustc_preserve_ub_checks);
46+
for block in body.basic_blocks.as_mut() {
47+
for statement in block.statements.iter_mut() {
48+
match statement.kind {
49+
StatementKind::Assign(box (_place, ref mut rvalue)) => {
50+
if !preserve_ub_checks {
51+
ctx.simplify_ub_check(&statement.source_info, rvalue);
4052
}
41-
_ => {}
53+
ctx.simplify_bool_cmp(&statement.source_info, rvalue);
54+
ctx.simplify_ref_deref(&statement.source_info, rvalue);
55+
ctx.simplify_len(&statement.source_info, rvalue);
56+
ctx.simplify_cast(rvalue);
4257
}
58+
_ => {}
4359
}
44-
45-
ctx.simplify_primitive_clone(block.terminator.as_mut().unwrap(), &mut block.statements);
46-
ctx.simplify_intrinsic_assert(block.terminator.as_mut().unwrap());
47-
ctx.simplify_nounwind_call(block.terminator.as_mut().unwrap());
48-
simplify_duplicate_switch_targets(block.terminator.as_mut().unwrap());
4960
}
61+
62+
let terminator = block.terminator.as_mut().unwrap();
63+
ctx.simplify_primitive_clone(terminator, &mut block.statements);
64+
ctx.simplify_intrinsic_assert(terminator);
65+
ctx.simplify_nounwind_call(terminator);
66+
simplify_duplicate_switch_targets(terminator);
5067
}
5168
}
5269

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
581581
&remove_unneeded_drops::RemoveUnneededDrops,
582582
// Type instantiation may create uninhabited enums.
583583
&uninhabited_enum_branching::UninhabitedEnumBranching,
584+
&instsimplify::InstSimplify::BeforeUnreachablePropagation,
584585
&unreachable_prop::UnreachablePropagation,
585586
&o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching),
586587
// Inlining may have introduced a lot of redundant code and a large move pattern.
@@ -591,9 +592,9 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
591592
&ref_prop::ReferencePropagation,
592593
&sroa::ScalarReplacementOfAggregates,
593594
&match_branches::MatchBranchSimplification,
594-
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
595+
// InstSimplify is after MatchBranchSimplification to clean up Ne(_1, false)
595596
&multiple_return_terminators::MultipleReturnTerminators,
596-
&instsimplify::InstSimplify,
597+
&instsimplify::InstSimplify::Final,
597598
&simplify::SimplifyLocals::BeforeConstProp,
598599
&dead_store_elimination::DeadStoreElimination::Initial,
599600
&gvn::GVN,

tests/mir-opt/const_prop/slice_len.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ unit-test: GVN
2-
//@ compile-flags: -Zmir-enable-passes=+InstSimplify
2+
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-unreachable-propagation
33
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
44
// EMIT_MIR_FOR_EACH_BIT_WIDTH
55

tests/mir-opt/dataflow-const-prop/slice_len.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
22
//@ unit-test: DataflowConstProp
3-
//@ compile-flags: -Zmir-enable-passes=+InstSimplify
3+
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-unreachable-propagation
44
// EMIT_MIR_FOR_EACH_BIT_WIDTH
55

66
// EMIT_MIR slice_len.main.DataflowConstProp.diff

tests/mir-opt/instsimplify/bool_compare.eq_false.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.eq_false.InstSimplify-before-unreachable-propagation.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `eq_false` before InstSimplify
2-
+ // MIR for `eq_false` after InstSimplify
1+
- // MIR for `eq_false` before InstSimplify-before-unreachable-propagation
2+
+ // MIR for `eq_false` after InstSimplify-before-unreachable-propagation
33

44
fn eq_false(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.eq_true.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.eq_true.InstSimplify-before-unreachable-propagation.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `eq_true` before InstSimplify
2-
+ // MIR for `eq_true` after InstSimplify
1+
- // MIR for `eq_true` before InstSimplify-before-unreachable-propagation
2+
+ // MIR for `eq_true` after InstSimplify-before-unreachable-propagation
33

44
fn eq_true(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.false_eq.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.false_eq.InstSimplify-before-unreachable-propagation.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `false_eq` before InstSimplify
2-
+ // MIR for `false_eq` after InstSimplify
1+
- // MIR for `false_eq` before InstSimplify-before-unreachable-propagation
2+
+ // MIR for `false_eq` after InstSimplify-before-unreachable-propagation
33

44
fn false_eq(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.false_ne.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.false_ne.InstSimplify-before-unreachable-propagation.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `false_ne` before InstSimplify
2-
+ // MIR for `false_ne` after InstSimplify
1+
- // MIR for `false_ne` before InstSimplify-before-unreachable-propagation
2+
+ // MIR for `false_ne` after InstSimplify-before-unreachable-propagation
33

44
fn false_ne(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.ne_false.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.ne_false.InstSimplify-before-unreachable-propagation.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `ne_false` before InstSimplify
2-
+ // MIR for `ne_false` after InstSimplify
1+
- // MIR for `ne_false` before InstSimplify-before-unreachable-propagation
2+
+ // MIR for `ne_false` after InstSimplify-before-unreachable-propagation
33

44
fn ne_false(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.ne_true.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.ne_true.InstSimplify-before-unreachable-propagation.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `ne_true` before InstSimplify
2-
+ // MIR for `ne_true` after InstSimplify
1+
- // MIR for `ne_true` before InstSimplify-before-unreachable-propagation
2+
+ // MIR for `ne_true` after InstSimplify-before-unreachable-propagation
33

44
fn ne_true(_1: bool) -> u32 {
55
debug x => _1;

0 commit comments

Comments
 (0)