Skip to content

Commit bb27b05

Browse files
Separate RemoveFalseEdges from SimplifyBranches
Otherwise dataflow state will propagate along false edges and cause things to be marked as maybe init unnecessarily. These should be separate, since `SimplifyBranches` also makes `if true {} else {}` into a `goto`, which means we wouldn't lint anything in the `else` block.
1 parent 6db0a0e commit bb27b05

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ mod match_branches;
6060
mod multiple_return_terminators;
6161
mod normalize_array_len;
6262
mod nrvo;
63+
mod remove_false_edges;
6364
mod remove_noop_landing_pads;
6465
mod remove_storage_markers;
6566
mod remove_unneeded_drops;
@@ -456,7 +457,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
456457

457458
let post_borrowck_cleanup: &[&dyn MirPass<'tcx>] = &[
458459
// Remove all things only needed by analysis
459-
&simplify_branches::SimplifyBranches::new("initial"),
460+
&simplify_branches::SimplifyConstCondition::new("initial"),
460461
&remove_noop_landing_pads::RemoveNoopLandingPads,
461462
&cleanup_post_borrowck::CleanupNonCodegenStatements,
462463
&simplify::SimplifyCfg::new("early-opt"),
@@ -515,13 +516,13 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
515516
&instcombine::InstCombine,
516517
&separate_const_switch::SeparateConstSwitch,
517518
&const_prop::ConstProp,
518-
&simplify_branches::SimplifyBranches::new("after-const-prop"),
519+
&simplify_branches::SimplifyConstCondition::new("after-const-prop"),
519520
&early_otherwise_branch::EarlyOtherwiseBranch,
520521
&simplify_comparison_integral::SimplifyComparisonIntegral,
521522
&simplify_try::SimplifyArmIdentity,
522523
&simplify_try::SimplifyBranchSame,
523524
&dest_prop::DestinationPropagation,
524-
&simplify_branches::SimplifyBranches::new("final"),
525+
&simplify_branches::SimplifyConstCondition::new("final"),
525526
&remove_noop_landing_pads::RemoveNoopLandingPads,
526527
&simplify::SimplifyCfg::new("final"),
527528
&nrvo::RenameReturnPlace,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rustc_middle::mir::{Body, TerminatorKind};
2+
use rustc_middle::ty::TyCtxt;
3+
4+
use crate::MirPass;
5+
6+
/// Removes `FalseEdge` and `FalseUnwind` terminators from the MIR.
7+
///
8+
/// These are only needed for borrow checking, and can be removed afterwards.
9+
///
10+
/// FIXME: This should probably have its own MIR phase.
11+
pub struct RemoveFalseEdges;
12+
13+
impl<'tcx> MirPass<'tcx> for RemoveFalseEdges {
14+
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
15+
for block in body.basic_blocks_mut() {
16+
let terminator = block.terminator_mut();
17+
terminator.kind = match terminator.kind {
18+
TerminatorKind::FalseEdge { real_target, .. } => {
19+
TerminatorKind::Goto { target: real_target }
20+
}
21+
TerminatorKind::FalseUnwind { real_target, .. } => {
22+
TerminatorKind::Goto { target: real_target }
23+
}
24+
25+
_ => continue,
26+
}
27+
}
28+
}
29+
}

compiler/rustc_mir_transform/src/simplify_branches.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
//! A pass that simplifies branches when their condition is known.
2-
31
use crate::MirPass;
42
use rustc_middle::mir::*;
53
use rustc_middle::ty::TyCtxt;
64

75
use std::borrow::Cow;
86

9-
pub struct SimplifyBranches {
7+
/// A pass that replaces a branch with a goto when its condition is known.
8+
pub struct SimplifyConstCondition {
109
label: String,
1110
}
1211

13-
impl SimplifyBranches {
12+
impl SimplifyConstCondition {
1413
pub fn new(label: &str) -> Self {
15-
SimplifyBranches { label: format!("SimplifyBranches-{}", label) }
14+
SimplifyConstCondition { label: format!("SimplifyConstCondition-{}", label) }
1615
}
1716
}
1817

19-
impl<'tcx> MirPass<'tcx> for SimplifyBranches {
18+
impl<'tcx> MirPass<'tcx> for SimplifyConstCondition {
2019
fn name(&self) -> Cow<'_, str> {
2120
Cow::Borrowed(&self.label)
2221
}
@@ -53,12 +52,6 @@ impl<'tcx> MirPass<'tcx> for SimplifyBranches {
5352
Some(v) if v == expected => TerminatorKind::Goto { target },
5453
_ => continue,
5554
},
56-
TerminatorKind::FalseEdge { real_target, .. } => {
57-
TerminatorKind::Goto { target: real_target }
58-
}
59-
TerminatorKind::FalseUnwind { real_target, .. } => {
60-
TerminatorKind::Goto { target: real_target }
61-
}
6255
_ => continue,
6356
};
6457
}

0 commit comments

Comments
 (0)