1
Fork 0

Reorder passes.

This commit is contained in:
Camille GILLOT 2023-07-22 15:34:54 +00:00
parent 31bc7e2c47
commit ed27cb0f49
7 changed files with 68 additions and 42 deletions

View file

@ -568,10 +568,11 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&[ &[
&check_alignment::CheckAlignment, &check_alignment::CheckAlignment,
&lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first &lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first
&unreachable_prop::UnreachablePropagation,
&uninhabited_enum_branching::UninhabitedEnumBranching,
&o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching),
&inline::Inline, &inline::Inline,
// Substitutions during inlining may introduce switch on enums with uninhabited branches.
&uninhabited_enum_branching::UninhabitedEnumBranching,
&unreachable_prop::UnreachablePropagation,
&o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching),
&remove_storage_markers::RemoveStorageMarkers, &remove_storage_markers::RemoveStorageMarkers,
&remove_zsts::RemoveZsts, &remove_zsts::RemoveZsts,
&normalize_array_len::NormalizeArrayLen, // has to run after `slice::len` lowering &normalize_array_len::NormalizeArrayLen, // has to run after `slice::len` lowering

View file

@ -2,7 +2,6 @@
//! when all of their successors are unreachable. This is achieved through a //! when all of their successors are unreachable. This is achieved through a
//! post-order traversal of the blocks. //! post-order traversal of the blocks.
use crate::simplify;
use crate::MirPass; use crate::MirPass;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_middle::mir::*; use rustc_middle::mir::*;
@ -52,8 +51,6 @@ impl MirPass<'_> for UnreachablePropagation {
body.basic_blocks_mut()[bb].statements.clear(); body.basic_blocks_mut()[bb].statements.clear();
} }
let replaced = !replacements.is_empty();
for (bb, terminator_kind) in replacements { for (bb, terminator_kind) in replacements {
if !tcx.consider_optimizing(|| { if !tcx.consider_optimizing(|| {
format!("UnreachablePropagation {:?} ", body.source.def_id()) format!("UnreachablePropagation {:?} ", body.source.def_id())
@ -64,9 +61,7 @@ impl MirPass<'_> for UnreachablePropagation {
body.basic_blocks_mut()[bb].terminator_mut().kind = terminator_kind; body.basic_blocks_mut()[bb].terminator_mut().kind = terminator_kind;
} }
if replaced { // Do not remove dead blocks, let `SimplifyCfg` do it.
simplify::remove_dead_blocks(body);
}
} }
} }

View file

@ -52,7 +52,7 @@
StorageLive(_10); StorageLive(_10);
StorageLive(_11); StorageLive(_11);
_9 = discriminant(_1); _9 = discriminant(_1);
switchInt(move _9) -> [0: bb7, 1: bb5, otherwise: bb6]; switchInt(move _9) -> [0: bb6, 1: bb5, otherwise: bb3];
} }
bb1: { bb1: {
@ -92,10 +92,6 @@
} }
bb6: { bb6: {
unreachable;
}
bb7: {
_10 = ((_1 as Ok).0: i32); _10 = ((_1 as Ok).0: i32);
_3 = ControlFlow::<Result<Infallible, i32>, i32>::Continue(move _10); _3 = ControlFlow::<Result<Infallible, i32>, i32>::Continue(move _10);
goto -> bb1; goto -> bb1;

View file

@ -24,8 +24,7 @@
bb1: { bb1: {
_2 = discriminant(_1); _2 = discriminant(_1);
- switchInt(move _2) -> [1: bb2, otherwise: bb6]; switchInt(move _2) -> [1: bb2, otherwise: bb6];
+ switchInt(move _2) -> [1: bb2, otherwise: bb3];
} }
bb2: { bb2: {
@ -36,29 +35,31 @@
- StorageLive(_6); - StorageLive(_6);
- _6 = const true; - _6 = const true;
- switchInt(move _6) -> [0: bb4, otherwise: bb3]; - switchInt(move _6) -> [0: bb4, otherwise: bb3];
- } + unreachable;
- }
- bb3: {
bb3: {
- _4 = const 21_i32; - _4 = const 21_i32;
- _5 = const (); - _5 = const ();
- goto -> bb5; - goto -> bb5;
- } + unreachable;
- }
- bb4: {
bb4: {
- _4 = const 42_i32; - _4 = const 42_i32;
- _5 = const (); - _5 = const ();
- goto -> bb5; - goto -> bb5;
- } + unreachable;
- }
- bb5: {
bb5: {
- StorageDead(_6); - StorageDead(_6);
- StorageDead(_5); - StorageDead(_5);
- StorageLive(_7); - StorageLive(_7);
unreachable; unreachable;
} }
- bb6: { bb6: {
+ bb3: {
_0 = const (); _0 = const ();
StorageDead(_1); StorageDead(_1);
return; return;

View file

@ -24,8 +24,7 @@
bb1: { bb1: {
_2 = discriminant(_1); _2 = discriminant(_1);
- switchInt(move _2) -> [1: bb2, otherwise: bb6]; switchInt(move _2) -> [1: bb2, otherwise: bb6];
+ switchInt(move _2) -> [1: bb2, otherwise: bb3];
} }
bb2: { bb2: {
@ -36,29 +35,31 @@
- StorageLive(_6); - StorageLive(_6);
- _6 = const true; - _6 = const true;
- switchInt(move _6) -> [0: bb4, otherwise: bb3]; - switchInt(move _6) -> [0: bb4, otherwise: bb3];
- } + unreachable;
- }
- bb3: {
bb3: {
- _4 = const 21_i32; - _4 = const 21_i32;
- _5 = const (); - _5 = const ();
- goto -> bb5; - goto -> bb5;
- } + unreachable;
- }
- bb4: {
bb4: {
- _4 = const 42_i32; - _4 = const 42_i32;
- _5 = const (); - _5 = const ();
- goto -> bb5; - goto -> bb5;
- } + unreachable;
- }
- bb5: {
bb5: {
- StorageDead(_6); - StorageDead(_6);
- StorageDead(_5); - StorageDead(_5);
- StorageLive(_7); - StorageLive(_7);
unreachable; unreachable;
} }
- bb6: { bb6: {
+ bb3: {
_0 = const (); _0 = const ();
StorageDead(_1); StorageDead(_1);
return; return;

View file

@ -1,5 +1,6 @@
// skip-filecheck // unit-test: UnreachablePropagation
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY // EMIT_MIR_FOR_EACH_PANIC_STRATEGY
enum Empty {} enum Empty {}
fn empty() -> Option<Empty> { fn empty() -> Option<Empty> {
@ -8,6 +9,21 @@ fn empty() -> Option<Empty> {
// EMIT_MIR unreachable.main.UnreachablePropagation.diff // EMIT_MIR unreachable.main.UnreachablePropagation.diff
fn main() { fn main() {
// CHECK-LABEL: fn main(
// CHECK: bb0: {
// CHECK: {{_.*}} = empty()
// CHECK: bb1: {
// CHECK: switchInt({{.*}}) -> [1: bb2, otherwise: bb6];
// CHECK: bb2: {
// CHECK: unreachable;
// CHECK: bb3: {
// CHECK: unreachable;
// CHECK: bb4: {
// CHECK: unreachable;
// CHECK: bb5: {
// CHECK: unreachable;
// CHECK: bb6: {
// CHECK: return;
if let Some(_x) = empty() { if let Some(_x) = empty() {
let mut _y; let mut _y;

View file

@ -1,5 +1,6 @@
// skip-filecheck // unit-test: UnreachablePropagation
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY // EMIT_MIR_FOR_EACH_PANIC_STRATEGY
pub enum Empty {} pub enum Empty {}
fn empty() -> Option<Empty> { fn empty() -> Option<Empty> {
@ -12,6 +13,21 @@ fn loop_forever() {
// EMIT_MIR unreachable_diverging.main.UnreachablePropagation.diff // EMIT_MIR unreachable_diverging.main.UnreachablePropagation.diff
fn main() { fn main() {
// CHECK-LABEL: fn main(
// CHECK: bb0: {
// CHECK: {{_.*}} = empty()
// CHECK: bb1: {
// CHECK: switchInt({{.*}}) -> [1: bb2, otherwise: bb6];
// CHECK: bb2: {
// CHECK: switchInt({{.*}}) -> [0: bb4, otherwise: bb3];
// CHECK: bb3: {
// CHECK: {{_.*}} = loop_forever()
// CHECK: bb4: {
// CHECK: unreachable;
// CHECK: bb5: {
// CHECK: unreachable;
// CHECK: bb6: {
// CHECK: return;
let x = true; let x = true;
if let Some(bomb) = empty() { if let Some(bomb) = empty() {
if x { if x {