5
5
use crate :: mir:: interpret:: {
6
6
AllocRange , ConstAllocation , ConstValue , GlobalAlloc , LitToConstInput , Scalar ,
7
7
} ;
8
- use crate :: mir:: traversal:: PostorderCache ;
9
8
use crate :: mir:: visit:: MirVisitable ;
10
9
use crate :: ty:: codec:: { TyDecoder , TyEncoder } ;
11
10
use crate :: ty:: fold:: { FallibleTypeFolder , TypeFoldable , TypeSuperFoldable } ;
@@ -28,7 +27,6 @@ use polonius_engine::Atom;
28
27
pub use rustc_ast:: Mutability ;
29
28
use rustc_data_structures:: fx:: FxHashSet ;
30
29
use rustc_data_structures:: graph:: dominators:: { dominators, Dominators } ;
31
- use rustc_data_structures:: graph:: { self , GraphSuccessors } ;
32
30
use rustc_index:: bit_set:: BitMatrix ;
33
31
use rustc_index:: vec:: { Idx , IndexVec } ;
34
32
use rustc_serialize:: { Decodable , Encodable } ;
@@ -43,11 +41,12 @@ use std::fmt::{self, Debug, Display, Formatter, Write};
43
41
use std:: ops:: { ControlFlow , Index , IndexMut } ;
44
42
use std:: { iter, mem} ;
45
43
46
- use self :: graph_cyclic_cache:: GraphIsCyclicCache ;
47
- use self :: predecessors:: { PredecessorCache , Predecessors } ;
44
+ use self :: predecessors:: Predecessors ;
48
45
pub use self :: query:: * ;
49
- use self :: switch_sources:: { SwitchSourceCache , SwitchSources } ;
46
+ use self :: switch_sources:: SwitchSources ;
47
+ pub use basic_blocks:: BasicBlocks ;
50
48
49
+ mod basic_blocks;
51
50
pub mod coverage;
52
51
mod generic_graph;
53
52
pub mod generic_graphviz;
@@ -189,7 +188,7 @@ pub struct GeneratorInfo<'tcx> {
189
188
pub struct Body < ' tcx > {
190
189
/// A list of basic blocks. References to basic block use a newtyped index type [`BasicBlock`]
191
190
/// that indexes into this vector.
192
- basic_blocks : IndexVec < BasicBlock , BasicBlockData < ' tcx > > ,
191
+ pub basic_blocks : BasicBlocks < ' tcx > ,
193
192
194
193
/// Records how far through the "desugaring and optimization" process this particular
195
194
/// MIR has traversed. This is particularly useful when inlining, since in that context
@@ -257,11 +256,6 @@ pub struct Body<'tcx> {
257
256
/// potentially allow things like `[u8; std::mem::size_of::<T>() * 0]` due to this.
258
257
pub is_polymorphic : bool ,
259
258
260
- predecessor_cache : PredecessorCache ,
261
- switch_source_cache : SwitchSourceCache ,
262
- is_cyclic : GraphIsCyclicCache ,
263
- postorder_cache : PostorderCache ,
264
-
265
259
pub tainted_by_errors : Option < ErrorGuaranteed > ,
266
260
}
267
261
@@ -289,7 +283,7 @@ impl<'tcx> Body<'tcx> {
289
283
let mut body = Body {
290
284
phase : MirPhase :: Built ,
291
285
source,
292
- basic_blocks,
286
+ basic_blocks : BasicBlocks :: new ( basic_blocks ) ,
293
287
source_scopes,
294
288
generator : generator_kind. map ( |generator_kind| {
295
289
Box :: new ( GeneratorInfo {
@@ -307,10 +301,6 @@ impl<'tcx> Body<'tcx> {
307
301
span,
308
302
required_consts : Vec :: new ( ) ,
309
303
is_polymorphic : false ,
310
- predecessor_cache : PredecessorCache :: new ( ) ,
311
- switch_source_cache : SwitchSourceCache :: new ( ) ,
312
- is_cyclic : GraphIsCyclicCache :: new ( ) ,
313
- postorder_cache : PostorderCache :: new ( ) ,
314
304
tainted_by_errors,
315
305
} ;
316
306
body. is_polymorphic = body. has_param_types_or_consts ( ) ;
@@ -326,7 +316,7 @@ impl<'tcx> Body<'tcx> {
326
316
let mut body = Body {
327
317
phase : MirPhase :: Built ,
328
318
source : MirSource :: item ( CRATE_DEF_ID . to_def_id ( ) ) ,
329
- basic_blocks,
319
+ basic_blocks : BasicBlocks :: new ( basic_blocks ) ,
330
320
source_scopes : IndexVec :: new ( ) ,
331
321
generator : None ,
332
322
local_decls : IndexVec :: new ( ) ,
@@ -337,10 +327,6 @@ impl<'tcx> Body<'tcx> {
337
327
required_consts : Vec :: new ( ) ,
338
328
var_debug_info : Vec :: new ( ) ,
339
329
is_polymorphic : false ,
340
- predecessor_cache : PredecessorCache :: new ( ) ,
341
- switch_source_cache : SwitchSourceCache :: new ( ) ,
342
- is_cyclic : GraphIsCyclicCache :: new ( ) ,
343
- postorder_cache : PostorderCache :: new ( ) ,
344
330
tainted_by_errors : None ,
345
331
} ;
346
332
body. is_polymorphic = body. has_param_types_or_consts ( ) ;
@@ -354,74 +340,13 @@ impl<'tcx> Body<'tcx> {
354
340
355
341
#[ inline]
356
342
pub fn basic_blocks_mut ( & mut self ) -> & mut IndexVec < BasicBlock , BasicBlockData < ' tcx > > {
357
- // Because the user could mutate basic block terminators via this reference, we need to
358
- // invalidate the caches.
359
- //
360
- // FIXME: Use a finer-grained API for this, so only transformations that alter terminators
361
- // invalidate the caches.
362
- self . invalidate_cfg_cache ( ) ;
363
- & mut self . basic_blocks
364
- }
365
-
366
- #[ inline]
367
- pub fn basic_blocks_and_local_decls_mut (
368
- & mut self ,
369
- ) -> ( & mut IndexVec < BasicBlock , BasicBlockData < ' tcx > > , & mut LocalDecls < ' tcx > ) {
370
- self . invalidate_cfg_cache ( ) ;
371
- ( & mut self . basic_blocks , & mut self . local_decls )
372
- }
373
-
374
- #[ inline]
375
- pub fn basic_blocks_local_decls_mut_and_var_debug_info (
376
- & mut self ,
377
- ) -> (
378
- & mut IndexVec < BasicBlock , BasicBlockData < ' tcx > > ,
379
- & mut LocalDecls < ' tcx > ,
380
- & mut Vec < VarDebugInfo < ' tcx > > ,
381
- ) {
382
- self . invalidate_cfg_cache ( ) ;
383
- ( & mut self . basic_blocks , & mut self . local_decls , & mut self . var_debug_info )
384
- }
385
-
386
- /// Get mutable access to parts of the Body without invalidating the CFG cache.
387
- ///
388
- /// By calling this method instead of eg [`Body::basic_blocks_mut`], you promise not to change
389
- /// the CFG. This means that
390
- ///
391
- /// 1) The number of basic blocks remains unchanged
392
- /// 2) The set of successors of each terminator remains unchanged.
393
- /// 3) For each `TerminatorKind::SwitchInt`, the `targets` remains the same and the terminator
394
- /// kind is not changed.
395
- ///
396
- /// If any of these conditions cannot be upheld, you should call [`Body::invalidate_cfg_cache`].
397
- #[ inline]
398
- pub fn basic_blocks_local_decls_mut_and_var_debug_info_no_invalidate (
399
- & mut self ,
400
- ) -> (
401
- & mut IndexVec < BasicBlock , BasicBlockData < ' tcx > > ,
402
- & mut LocalDecls < ' tcx > ,
403
- & mut Vec < VarDebugInfo < ' tcx > > ,
404
- ) {
405
- ( & mut self . basic_blocks , & mut self . local_decls , & mut self . var_debug_info )
406
- }
407
-
408
- /// Invalidates cached information about the CFG.
409
- ///
410
- /// You will only ever need this if you have also called
411
- /// [`Body::basic_blocks_local_decls_mut_and_var_debug_info_no_invalidate`]. All other methods
412
- /// that allow you to mutate the body also call this method themselves, thereby avoiding any
413
- /// risk of accidentaly cache invalidation.
414
- pub fn invalidate_cfg_cache ( & mut self ) {
415
- self . predecessor_cache . invalidate ( ) ;
416
- self . switch_source_cache . invalidate ( ) ;
417
- self . is_cyclic . invalidate ( ) ;
418
- self . postorder_cache . invalidate ( ) ;
343
+ self . basic_blocks . as_mut ( )
419
344
}
420
345
421
346
/// Returns `true` if a cycle exists in the control-flow graph that is reachable from the
422
347
/// `START_BLOCK`.
423
348
pub fn is_cfg_cyclic ( & self ) -> bool {
424
- self . is_cyclic . is_cyclic ( self )
349
+ self . basic_blocks . is_cfg_cyclic ( )
425
350
}
426
351
427
352
#[ inline]
@@ -495,14 +420,6 @@ impl<'tcx> Body<'tcx> {
495
420
self . local_decls . drain ( self . arg_count + 1 ..)
496
421
}
497
422
498
- /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
499
- /// invalidating statement indices in `Location`s.
500
- pub fn make_statement_nop ( & mut self , location : Location ) {
501
- let block = & mut self . basic_blocks [ location. block ] ;
502
- debug_assert ! ( location. statement_index < block. statements. len( ) ) ;
503
- block. statements [ location. statement_index ] . make_nop ( )
504
- }
505
-
506
423
/// Returns the source info associated with `location`.
507
424
pub fn source_info ( & self , location : Location ) -> & SourceInfo {
508
425
let block = & self [ location. block ] ;
@@ -540,19 +457,19 @@ impl<'tcx> Body<'tcx> {
540
457
541
458
#[ inline]
542
459
pub fn predecessors ( & self ) -> & Predecessors {
543
- self . predecessor_cache . compute ( & self . basic_blocks )
460
+ self . basic_blocks . predecessors ( )
544
461
}
545
462
546
463
/// `body.switch_sources()[&(target, switch)]` returns a list of switch
547
464
/// values that lead to a `target` block from a `switch` block.
548
465
#[ inline]
549
466
pub fn switch_sources ( & self ) -> & SwitchSources {
550
- self . switch_source_cache . compute ( & self . basic_blocks )
467
+ self . basic_blocks . switch_sources ( )
551
468
}
552
469
553
470
#[ inline]
554
471
pub fn dominators ( & self ) -> Dominators < BasicBlock > {
555
- dominators ( self )
472
+ dominators ( & self . basic_blocks )
556
473
}
557
474
558
475
#[ inline]
@@ -599,7 +516,7 @@ impl<'tcx> Index<BasicBlock> for Body<'tcx> {
599
516
impl < ' tcx > IndexMut < BasicBlock > for Body < ' tcx > {
600
517
#[ inline]
601
518
fn index_mut ( & mut self , index : BasicBlock ) -> & mut BasicBlockData < ' tcx > {
602
- & mut self . basic_blocks_mut ( ) [ index]
519
+ & mut self . basic_blocks . as_mut ( ) [ index]
603
520
}
604
521
}
605
522
@@ -2890,48 +2807,6 @@ fn pretty_print_const_value<'tcx>(
2890
2807
} )
2891
2808
}
2892
2809
2893
- impl < ' tcx > graph:: DirectedGraph for Body < ' tcx > {
2894
- type Node = BasicBlock ;
2895
- }
2896
-
2897
- impl < ' tcx > graph:: WithNumNodes for Body < ' tcx > {
2898
- #[ inline]
2899
- fn num_nodes ( & self ) -> usize {
2900
- self . basic_blocks . len ( )
2901
- }
2902
- }
2903
-
2904
- impl < ' tcx > graph:: WithStartNode for Body < ' tcx > {
2905
- #[ inline]
2906
- fn start_node ( & self ) -> Self :: Node {
2907
- START_BLOCK
2908
- }
2909
- }
2910
-
2911
- impl < ' tcx > graph:: WithSuccessors for Body < ' tcx > {
2912
- #[ inline]
2913
- fn successors ( & self , node : Self :: Node ) -> <Self as GraphSuccessors < ' _ > >:: Iter {
2914
- self . basic_blocks [ node] . terminator ( ) . successors ( )
2915
- }
2916
- }
2917
-
2918
- impl < ' a , ' b > graph:: GraphSuccessors < ' b > for Body < ' a > {
2919
- type Item = BasicBlock ;
2920
- type Iter = Successors < ' b > ;
2921
- }
2922
-
2923
- impl < ' tcx , ' graph > graph:: GraphPredecessors < ' graph > for Body < ' tcx > {
2924
- type Item = BasicBlock ;
2925
- type Iter = std:: iter:: Copied < std:: slice:: Iter < ' graph , BasicBlock > > ;
2926
- }
2927
-
2928
- impl < ' tcx > graph:: WithPredecessors for Body < ' tcx > {
2929
- #[ inline]
2930
- fn predecessors ( & self , node : Self :: Node ) -> <Self as graph:: GraphPredecessors < ' _ > >:: Iter {
2931
- self . predecessors ( ) [ node] . iter ( ) . copied ( )
2932
- }
2933
- }
2934
-
2935
2810
/// `Location` represents the position of the start of the statement; or, if
2936
2811
/// `statement_index` equals the number of statements, then the start of the
2937
2812
/// terminator.
0 commit comments