Skip to content

Commit 4878688

Browse files
committed
Visit bodies in RPO for const-prop.
1 parent cc4d1e5 commit 4878688

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
103103
// That would require a uniform one-def no-mutation analysis
104104
// and RPO (or recursing when needing the value of a local).
105105
let mut optimization_finder = ConstPropagator::new(body, dummy_body, tcx);
106-
optimization_finder.visit_body(body);
106+
107+
// Traverse the body in reverse post-order, to ensure that `FullConstProp` locals are
108+
// assigned before being read.
109+
let postorder = body.basic_blocks.postorder().to_vec();
110+
for bb in postorder.into_iter().rev() {
111+
let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb];
112+
optimization_finder.visit_basic_block_data(bb, data);
113+
}
107114

108115
trace!("ConstProp done for {:?}", def_id);
109116
}
@@ -790,12 +797,6 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
790797
self.tcx
791798
}
792799

793-
fn visit_body(&mut self, body: &mut Body<'tcx>) {
794-
for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
795-
self.visit_basic_block_data(bb, data);
796-
}
797-
}
798-
799800
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
800801
self.super_operand(operand, location);
801802

0 commit comments

Comments
 (0)