15
15
16
16
use rustc:: mir:: { Constant , Literal , Location , Place , Mir , Operand , Rvalue , Local } ;
17
17
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 } ;
19
19
use rustc:: mir:: visit:: { Visitor , PlaceContext } ;
20
20
use rustc:: ty:: layout:: LayoutOf ;
21
21
use rustc:: middle:: const_val:: ConstVal ;
@@ -40,7 +40,7 @@ impl MirPass for ConstProp {
40
40
// constants, instead of just checking for const-folding succeeding.
41
41
// That would require an uniform one-def no-mutation analysis
42
42
// 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) ;
44
44
optimization_finder. visit_mir ( mir) ;
45
45
46
46
trace ! ( "ConstProp done for {:?}" , source. def_id) ;
@@ -50,30 +50,25 @@ impl MirPass for ConstProp {
50
50
type Const < ' tcx > = ( Value , ty:: Ty < ' tcx > , Span ) ;
51
51
52
52
/// Finds optimization opportunities on the MIR.
53
- struct OptimizationFinder < ' b , ' a , ' tcx : ' a +' b > {
53
+ struct ConstPropagator < ' b , ' a , ' tcx : ' a +' b > {
54
54
mir : & ' b Mir < ' tcx > ,
55
55
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
56
56
source : MirSource ,
57
57
places : IndexVec < Local , Option < Const < ' tcx > > > ,
58
58
can_const_prop : IndexVec < Local , bool > ,
59
59
}
60
60
61
- impl < ' b , ' a , ' tcx : ' b > OptimizationFinder < ' b , ' a , ' tcx > {
61
+ impl < ' b , ' a , ' tcx : ' b > ConstPropagator < ' b , ' a , ' tcx > {
62
62
fn new (
63
63
mir : & ' b Mir < ' tcx > ,
64
64
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
65
65
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 {
73
68
mir,
74
69
tcx,
75
70
source,
76
- can_const_prop,
71
+ can_const_prop : CanConstProp :: check ( mir ) ,
77
72
places : IndexVec :: from_elem ( None , & mir. local_decls ) ,
78
73
}
79
74
}
@@ -277,28 +272,18 @@ fn type_size_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
277
272
( tcx, param_env) . layout_of ( ty) . ok ( ) . map ( |layout| layout. size . bytes ( ) )
278
273
}
279
274
280
- struct CanConstProp < ' b , ' a , ' tcx : ' a + ' b > {
275
+ struct CanConstProp {
281
276
can_const_prop : IndexVec < Local , bool > ,
282
277
// false at the beginning, once set, there are not allowed to be any more assignments
283
278
found_assignment : IndexVec < Local , bool > ,
284
- mir : & ' b Mir < ' tcx > ,
285
- tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
286
- param_env : ty:: ParamEnv < ' tcx > ,
287
279
}
288
280
289
- impl < ' b , ' a , ' tcx : ' b > CanConstProp < ' b , ' a , ' tcx > {
281
+ impl CanConstProp {
290
282
/// 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 > {
296
284
let mut cpv = CanConstProp {
297
285
can_const_prop : IndexVec :: from_elem ( true , & mir. local_decls ) ,
298
286
found_assignment : IndexVec :: from_elem ( false , & mir. local_decls ) ,
299
- mir,
300
- tcx,
301
- param_env,
302
287
} ;
303
288
for ( local, val) in cpv. can_const_prop . iter_enumerated_mut ( ) {
304
289
* val = mir. local_kind ( local) != LocalKind :: Arg ;
@@ -308,7 +293,7 @@ impl<'b, 'a, 'tcx:'b> CanConstProp<'b, 'a, 'tcx> {
308
293
}
309
294
}
310
295
311
- impl < ' a , ' b , ' tcx > Visitor < ' tcx > for CanConstProp < ' a , ' b , ' tcx > {
296
+ impl < ' tcx > Visitor < ' tcx > for CanConstProp {
312
297
fn visit_local (
313
298
& mut self ,
314
299
& local: & Local ,
@@ -330,23 +315,12 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for CanConstProp<'a, 'b, 'tcx> {
330
315
StorageDead | StorageLive |
331
316
Validate |
332
317
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
- }
344
318
_ => self . can_const_prop [ local] = false ,
345
319
}
346
320
}
347
321
}
348
322
349
- impl < ' b , ' a , ' tcx > Visitor < ' tcx > for OptimizationFinder < ' b , ' a , ' tcx > {
323
+ impl < ' b , ' a , ' tcx > Visitor < ' tcx > for ConstPropagator < ' b , ' a , ' tcx > {
350
324
fn visit_constant (
351
325
& mut self ,
352
326
constant : & Constant < ' tcx > ,
0 commit comments