Skip to content

Commit 6b67829

Browse files
committed
Rename OptimizationFinder and don't propagate any borrows
1 parent 38e9fdf commit 6b67829

File tree

1 file changed

+12
-38
lines changed

1 file changed

+12
-38
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use rustc::mir::{Constant, Literal, Location, Place, Mir, Operand, Rvalue, Local};
1717
use rustc::mir::{NullOp, StatementKind, Statement, BasicBlock, LocalKind};
18-
use rustc::mir::{TerminatorKind, ClearCrossCrate, SourceInfo, BinOp, BorrowKind};
18+
use rustc::mir::{TerminatorKind, ClearCrossCrate, SourceInfo, BinOp};
1919
use rustc::mir::visit::{Visitor, PlaceContext};
2020
use rustc::ty::layout::LayoutOf;
2121
use rustc::middle::const_val::ConstVal;
@@ -40,7 +40,7 @@ impl MirPass for ConstProp {
4040
// constants, instead of just checking for const-folding succeeding.
4141
// That would require an uniform one-def no-mutation analysis
4242
// and RPO (or recursing when needing the value of a local).
43-
let mut optimization_finder = OptimizationFinder::new(mir, tcx, source);
43+
let mut optimization_finder = ConstPropagator::new(mir, tcx, source);
4444
optimization_finder.visit_mir(mir);
4545

4646
trace!("ConstProp done for {:?}", source.def_id);
@@ -50,30 +50,25 @@ impl MirPass for ConstProp {
5050
type Const<'tcx> = (Value, ty::Ty<'tcx>, Span);
5151

5252
/// Finds optimization opportunities on the MIR.
53-
struct OptimizationFinder<'b, 'a, 'tcx:'a+'b> {
53+
struct ConstPropagator<'b, 'a, 'tcx:'a+'b> {
5454
mir: &'b Mir<'tcx>,
5555
tcx: TyCtxt<'a, 'tcx, 'tcx>,
5656
source: MirSource,
5757
places: IndexVec<Local, Option<Const<'tcx>>>,
5858
can_const_prop: IndexVec<Local, bool>,
5959
}
6060

61-
impl<'b, 'a, 'tcx:'b> OptimizationFinder<'b, 'a, 'tcx> {
61+
impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> {
6262
fn new(
6363
mir: &'b Mir<'tcx>,
6464
tcx: TyCtxt<'a, 'tcx, 'tcx>,
6565
source: MirSource,
66-
) -> OptimizationFinder<'b, 'a, 'tcx> {
67-
let can_const_prop = CanConstProp::check(
68-
mir,
69-
tcx,
70-
tcx.param_env(source.def_id),
71-
);
72-
OptimizationFinder {
66+
) -> ConstPropagator<'b, 'a, 'tcx> {
67+
ConstPropagator {
7368
mir,
7469
tcx,
7570
source,
76-
can_const_prop,
71+
can_const_prop: CanConstProp::check(mir),
7772
places: IndexVec::from_elem(None, &mir.local_decls),
7873
}
7974
}
@@ -277,28 +272,18 @@ fn type_size_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
277272
(tcx, param_env).layout_of(ty).ok().map(|layout| layout.size.bytes())
278273
}
279274

280-
struct CanConstProp<'b, 'a, 'tcx:'a+'b> {
275+
struct CanConstProp {
281276
can_const_prop: IndexVec<Local, bool>,
282277
// false at the beginning, once set, there are not allowed to be any more assignments
283278
found_assignment: IndexVec<Local, bool>,
284-
mir: &'b Mir<'tcx>,
285-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
286-
param_env: ty::ParamEnv<'tcx>,
287279
}
288280

289-
impl<'b, 'a, 'tcx:'b> CanConstProp<'b, 'a, 'tcx> {
281+
impl CanConstProp {
290282
/// returns true if `local` can be propagated
291-
fn check(
292-
mir: &'b Mir<'tcx>,
293-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
294-
param_env: ty::ParamEnv<'tcx>,
295-
) -> IndexVec<Local, bool> {
283+
fn check(mir: &Mir) -> IndexVec<Local, bool> {
296284
let mut cpv = CanConstProp {
297285
can_const_prop: IndexVec::from_elem(true, &mir.local_decls),
298286
found_assignment: IndexVec::from_elem(false, &mir.local_decls),
299-
mir,
300-
tcx,
301-
param_env,
302287
};
303288
for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
304289
*val = mir.local_kind(local) != LocalKind::Arg;
@@ -308,7 +293,7 @@ impl<'b, 'a, 'tcx:'b> CanConstProp<'b, 'a, 'tcx> {
308293
}
309294
}
310295

311-
impl<'a, 'b, 'tcx> Visitor<'tcx> for CanConstProp<'a, 'b, 'tcx> {
296+
impl<'tcx> Visitor<'tcx> for CanConstProp {
312297
fn visit_local(
313298
&mut self,
314299
&local: &Local,
@@ -330,23 +315,12 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for CanConstProp<'a, 'b, 'tcx> {
330315
StorageDead | StorageLive |
331316
Validate |
332317
Inspect => {},
333-
Borrow { kind: BorrowKind::Shared, .. } => {
334-
// cannot const prop immutable borrows of types with interior mutability
335-
let has_interior_mutability = self
336-
.mir
337-
.local_decls[local]
338-
.ty
339-
.is_freeze(self.tcx, self.param_env, self.mir.span);
340-
if has_interior_mutability {
341-
self.can_const_prop[local] = false;
342-
}
343-
}
344318
_ => self.can_const_prop[local] = false,
345319
}
346320
}
347321
}
348322

349-
impl<'b, 'a, 'tcx> Visitor<'tcx> for OptimizationFinder<'b, 'a, 'tcx> {
323+
impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
350324
fn visit_constant(
351325
&mut self,
352326
constant: &Constant<'tcx>,

0 commit comments

Comments
 (0)