]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/dataflow/mod.rs
Rollup merge of #59675 - SimonSapin:stable-alloc, r=alexcrichton
[rust.git] / src / librustc_mir / dataflow / mod.rs
1 use syntax::ast::{self, MetaItem};
2
3 use rustc_data_structures::bit_set::{BitSet, BitSetOperator, HybridBitSet};
4 use rustc_data_structures::indexed_vec::Idx;
5 use rustc_data_structures::work_queue::WorkQueue;
6
7 use rustc::hir::def_id::DefId;
8 use rustc::ty::{self, TyCtxt};
9 use rustc::mir::{self, Mir, BasicBlock, BasicBlockData, Location, Statement, Terminator};
10 use rustc::mir::traversal;
11 use rustc::session::Session;
12
13 use std::borrow::Borrow;
14 use std::fmt;
15 use std::io;
16 use std::path::PathBuf;
17 use std::usize;
18
19 pub use self::impls::{MaybeStorageLive};
20 pub use self::impls::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
21 pub use self::impls::DefinitelyInitializedPlaces;
22 pub use self::impls::EverInitializedPlaces;
23 pub use self::impls::borrows::Borrows;
24 pub use self::impls::HaveBeenBorrowedLocals;
25 pub use self::at_location::{FlowAtLocation, FlowsAtLocation};
26 pub(crate) use self::drop_flag_effects::*;
27
28 use self::move_paths::MoveData;
29
30 mod at_location;
31 pub mod drop_flag_effects;
32 mod graphviz;
33 mod impls;
34 pub mod move_paths;
35
36 pub(crate) mod indexes {
37     pub(crate) use super::{
38         move_paths::{MovePathIndex, MoveOutIndex, InitIndex},
39         impls::borrows::BorrowIndex,
40     };
41 }
42
43 pub(crate) struct DataflowBuilder<'a, 'tcx: 'a, BD>
44 where
45     BD: BitDenotation<'tcx>
46 {
47     def_id: DefId,
48     flow_state: DataflowAnalysis<'a, 'tcx, BD>,
49     print_preflow_to: Option<String>,
50     print_postflow_to: Option<String>,
51 }
52
53 /// `DebugFormatted` encapsulates the "{:?}" rendering of some
54 /// arbitrary value. This way: you pay cost of allocating an extra
55 /// string (as well as that of rendering up-front); in exchange, you
56 /// don't have to hand over ownership of your value or deal with
57 /// borrowing it.
58 pub(crate) struct DebugFormatted(String);
59
60 impl DebugFormatted {
61     pub fn new(input: &dyn fmt::Debug) -> DebugFormatted {
62         DebugFormatted(format!("{:?}", input))
63     }
64 }
65
66 impl fmt::Debug for DebugFormatted {
67     fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
68         write!(w, "{}", self.0)
69     }
70 }
71
72 pub(crate) trait Dataflow<'tcx, BD: BitDenotation<'tcx>> {
73     /// Sets up and runs the dataflow problem, using `p` to render results if
74     /// implementation so chooses.
75     fn dataflow<P>(&mut self, p: P) where P: Fn(&BD, BD::Idx) -> DebugFormatted {
76         let _ = p; // default implementation does not instrument process.
77         self.build_sets();
78         self.propagate();
79     }
80
81     /// Sets up the entry, gen, and kill sets for this instance of a dataflow problem.
82     fn build_sets(&mut self);
83
84     /// Finds a fixed-point solution to this instance of a dataflow problem.
85     fn propagate(&mut self);
86 }
87
88 impl<'a, 'tcx: 'a, BD> Dataflow<'tcx, BD> for DataflowBuilder<'a, 'tcx, BD>
89 where
90     BD: BitDenotation<'tcx>
91 {
92     fn dataflow<P>(&mut self, p: P) where P: Fn(&BD, BD::Idx) -> DebugFormatted {
93         self.flow_state.build_sets();
94         self.pre_dataflow_instrumentation(|c,i| p(c,i)).unwrap();
95         self.flow_state.propagate();
96         self.post_dataflow_instrumentation(|c,i| p(c,i)).unwrap();
97     }
98
99     fn build_sets(&mut self) { self.flow_state.build_sets(); }
100     fn propagate(&mut self) { self.flow_state.propagate(); }
101 }
102
103 pub(crate) fn has_rustc_mir_with(attrs: &[ast::Attribute], name: &str) -> Option<MetaItem> {
104     for attr in attrs {
105         if attr.check_name("rustc_mir") {
106             let items = attr.meta_item_list();
107             for item in items.iter().flat_map(|l| l.iter()) {
108                 match item.meta_item() {
109                     Some(mi) if mi.check_name(name) => return Some(mi.clone()),
110                     _ => continue
111                 }
112             }
113         }
114     }
115     return None;
116 }
117
118 pub struct MoveDataParamEnv<'gcx, 'tcx> {
119     pub(crate) move_data: MoveData<'tcx>,
120     pub(crate) param_env: ty::ParamEnv<'gcx>,
121 }
122
123 pub(crate) fn do_dataflow<'a, 'gcx, 'tcx, BD, P>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
124                                                  mir: &'a Mir<'tcx>,
125                                                  def_id: DefId,
126                                                  attributes: &[ast::Attribute],
127                                                  dead_unwinds: &BitSet<BasicBlock>,
128                                                  bd: BD,
129                                                  p: P)
130                                                  -> DataflowResults<'tcx, BD>
131     where BD: BitDenotation<'tcx> + InitialFlow,
132           P: Fn(&BD, BD::Idx) -> DebugFormatted
133 {
134     let flow_state = DataflowAnalysis::new(mir, dead_unwinds, bd);
135     flow_state.run(tcx, def_id, attributes, p)
136 }
137
138 impl<'a, 'gcx: 'tcx, 'tcx: 'a, BD> DataflowAnalysis<'a, 'tcx, BD> where BD: BitDenotation<'tcx>
139 {
140     pub(crate) fn run<P>(self,
141                          tcx: TyCtxt<'a, 'gcx, 'tcx>,
142                          def_id: DefId,
143                          attributes: &[ast::Attribute],
144                          p: P) -> DataflowResults<'tcx, BD>
145         where P: Fn(&BD, BD::Idx) -> DebugFormatted
146     {
147         let name_found = |sess: &Session, attrs: &[ast::Attribute], name| -> Option<String> {
148             if let Some(item) = has_rustc_mir_with(attrs, name) {
149                 if let Some(s) = item.value_str() {
150                     return Some(s.to_string())
151                 } else {
152                     sess.span_err(
153                         item.span,
154                         &format!("{} attribute requires a path", item.path));
155                     return None;
156                 }
157             }
158             return None;
159         };
160
161         let print_preflow_to =
162             name_found(tcx.sess, attributes, "borrowck_graphviz_preflow");
163         let print_postflow_to =
164             name_found(tcx.sess, attributes, "borrowck_graphviz_postflow");
165
166         let mut mbcx = DataflowBuilder {
167             def_id,
168             print_preflow_to, print_postflow_to, flow_state: self,
169         };
170
171         mbcx.dataflow(p);
172         mbcx.flow_state.results()
173     }
174 }
175
176 struct PropagationContext<'b, 'a: 'b, 'tcx: 'a, O> where O: 'b + BitDenotation<'tcx>
177 {
178     builder: &'b mut DataflowAnalysis<'a, 'tcx, O>,
179 }
180
181 impl<'a, 'tcx: 'a, BD> DataflowAnalysis<'a, 'tcx, BD> where BD: BitDenotation<'tcx>
182 {
183     fn propagate(&mut self) {
184         let mut temp = BitSet::new_empty(self.flow_state.sets.bits_per_block);
185         let mut propcx = PropagationContext {
186             builder: self,
187         };
188         propcx.walk_cfg(&mut temp);
189     }
190
191     fn build_sets(&mut self) {
192         // First we need to build the entry-, gen- and kill-sets.
193
194         {
195             let sets = &mut self.flow_state.sets.for_block(mir::START_BLOCK.index());
196             self.flow_state.operator.start_block_effect(&mut sets.on_entry);
197         }
198
199         for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
200             let &mir::BasicBlockData { ref statements, ref terminator, is_cleanup: _ } = data;
201
202             let mut interim_state;
203             let sets = &mut self.flow_state.sets.for_block(bb.index());
204             let track_intrablock = BD::accumulates_intrablock_state();
205             if track_intrablock {
206                 debug!("swapping in mutable on_entry, initially {:?}", sets.on_entry);
207                 interim_state = sets.on_entry.to_owned();
208                 sets.on_entry = &mut interim_state;
209             }
210             for j_stmt in 0..statements.len() {
211                 let location = Location { block: bb, statement_index: j_stmt };
212                 self.flow_state.operator.before_statement_effect(sets, location);
213                 self.flow_state.operator.statement_effect(sets, location);
214                 if track_intrablock {
215                     sets.apply_local_effect();
216                 }
217             }
218
219             if terminator.is_some() {
220                 let location = Location { block: bb, statement_index: statements.len() };
221                 self.flow_state.operator.before_terminator_effect(sets, location);
222                 self.flow_state.operator.terminator_effect(sets, location);
223                 if track_intrablock {
224                     sets.apply_local_effect();
225                 }
226             }
227         }
228     }
229 }
230
231 impl<'b, 'a: 'b, 'tcx: 'a, BD> PropagationContext<'b, 'a, 'tcx, BD> where BD: BitDenotation<'tcx>
232 {
233     fn walk_cfg(&mut self, in_out: &mut BitSet<BD::Idx>) {
234         let mut dirty_queue: WorkQueue<mir::BasicBlock> =
235             WorkQueue::with_all(self.builder.mir.basic_blocks().len());
236         let mir = self.builder.mir;
237         while let Some(bb) = dirty_queue.pop() {
238             let bb_data = &mir[bb];
239             {
240                 let sets = self.builder.flow_state.sets.for_block(bb.index());
241                 debug_assert!(in_out.words().len() == sets.on_entry.words().len());
242                 in_out.overwrite(sets.on_entry);
243                 in_out.union(sets.gen_set);
244                 in_out.subtract(sets.kill_set);
245             }
246             self.builder.propagate_bits_into_graph_successors_of(
247                 in_out, (bb, bb_data), &mut dirty_queue);
248         }
249     }
250 }
251
252 fn dataflow_path(context: &str, path: &str) -> PathBuf {
253     let mut path = PathBuf::from(path);
254     let new_file_name = {
255         let orig_file_name = path.file_name().unwrap().to_str().unwrap();
256         format!("{}_{}", context, orig_file_name)
257     };
258     path.set_file_name(new_file_name);
259     path
260 }
261
262 impl<'a, 'tcx: 'a, BD> DataflowBuilder<'a, 'tcx, BD> where BD: BitDenotation<'tcx>
263 {
264     fn pre_dataflow_instrumentation<P>(&self, p: P) -> io::Result<()>
265         where P: Fn(&BD, BD::Idx) -> DebugFormatted
266     {
267         if let Some(ref path_str) = self.print_preflow_to {
268             let path = dataflow_path(BD::name(), path_str);
269             graphviz::print_borrowck_graph_to(self, &path, p)
270         } else {
271             Ok(())
272         }
273     }
274
275     fn post_dataflow_instrumentation<P>(&self, p: P) -> io::Result<()>
276         where P: Fn(&BD, BD::Idx) -> DebugFormatted
277     {
278         if let Some(ref path_str) = self.print_postflow_to {
279             let path = dataflow_path(BD::name(), path_str);
280             graphviz::print_borrowck_graph_to(self, &path, p)
281         } else {
282             Ok(())
283         }
284     }
285 }
286
287 /// DataflowResultsConsumer abstracts over walking the MIR with some
288 /// already constructed dataflow results.
289 ///
290 /// It abstracts over the FlowState and also completely hides the
291 /// underlying flow analysis results, because it needs to handle cases
292 /// where we are combining the results of *multiple* flow analyses
293 /// (e.g., borrows + inits + uninits).
294 pub(crate) trait DataflowResultsConsumer<'a, 'tcx: 'a> {
295     type FlowState: FlowsAtLocation;
296
297     // Observation Hooks: override (at least one of) these to get analysis feedback.
298     fn visit_block_entry(&mut self,
299                          _bb: BasicBlock,
300                          _flow_state: &Self::FlowState) {}
301
302     fn visit_statement_entry(&mut self,
303                              _loc: Location,
304                              _stmt: &Statement<'tcx>,
305                              _flow_state: &Self::FlowState) {}
306
307     fn visit_terminator_entry(&mut self,
308                               _loc: Location,
309                               _term: &Terminator<'tcx>,
310                               _flow_state: &Self::FlowState) {}
311
312     // Main entry point: this drives the processing of results.
313
314     fn analyze_results(&mut self, flow_uninit: &mut Self::FlowState) {
315         let flow = flow_uninit;
316         for (bb, _) in traversal::reverse_postorder(self.mir()) {
317             flow.reset_to_entry_of(bb);
318             self.process_basic_block(bb, flow);
319         }
320     }
321
322     fn process_basic_block(&mut self, bb: BasicBlock, flow_state: &mut Self::FlowState) {
323         let BasicBlockData { ref statements, ref terminator, is_cleanup: _ } =
324             self.mir()[bb];
325         let mut location = Location { block: bb, statement_index: 0 };
326         for stmt in statements.iter() {
327             flow_state.reconstruct_statement_effect(location);
328             self.visit_statement_entry(location, stmt, flow_state);
329             flow_state.apply_local_effect(location);
330             location.statement_index += 1;
331         }
332
333         if let Some(ref term) = *terminator {
334             flow_state.reconstruct_terminator_effect(location);
335             self.visit_terminator_entry(location, term, flow_state);
336
337             // We don't need to apply the effect of the terminator,
338             // since we are only visiting dataflow state on control
339             // flow entry to the various nodes. (But we still need to
340             // reconstruct the effect, because the visit method might
341             // inspect it.)
342         }
343     }
344
345     // Delegated Hooks: Provide access to the MIR and process the flow state.
346
347     fn mir(&self) -> &'a Mir<'tcx>;
348 }
349
350 pub fn state_for_location<'tcx, T: BitDenotation<'tcx>>(loc: Location,
351                                                         analysis: &T,
352                                                         result: &DataflowResults<'tcx, T>,
353                                                         mir: &Mir<'tcx>)
354     -> BitSet<T::Idx> {
355     let mut on_entry = result.sets().on_entry_set_for(loc.block.index()).to_owned();
356     let mut kill_set = on_entry.to_hybrid();
357     let mut gen_set = kill_set.clone();
358
359     {
360         let mut sets = BlockSets {
361             on_entry: &mut on_entry,
362             kill_set: &mut kill_set,
363             gen_set: &mut gen_set,
364         };
365
366         for stmt in 0..loc.statement_index {
367             let mut stmt_loc = loc;
368             stmt_loc.statement_index = stmt;
369             analysis.before_statement_effect(&mut sets, stmt_loc);
370             analysis.statement_effect(&mut sets, stmt_loc);
371         }
372
373         // Apply the pre-statement effect of the statement we're evaluating.
374         if loc.statement_index == mir[loc.block].statements.len() {
375             analysis.before_terminator_effect(&mut sets, loc);
376         } else {
377             analysis.before_statement_effect(&mut sets, loc);
378         }
379     }
380
381     gen_set.to_dense()
382 }
383
384 pub struct DataflowAnalysis<'a, 'tcx: 'a, O> where O: BitDenotation<'tcx>
385 {
386     flow_state: DataflowState<'tcx, O>,
387     dead_unwinds: &'a BitSet<mir::BasicBlock>,
388     mir: &'a Mir<'tcx>,
389 }
390
391 impl<'a, 'tcx: 'a, O> DataflowAnalysis<'a, 'tcx, O> where O: BitDenotation<'tcx>
392 {
393     pub fn results(self) -> DataflowResults<'tcx, O> {
394         DataflowResults(self.flow_state)
395     }
396
397     pub fn mir(&self) -> &'a Mir<'tcx> { self.mir }
398 }
399
400 pub struct DataflowResults<'tcx, O>(pub(crate) DataflowState<'tcx, O>) where O: BitDenotation<'tcx>;
401
402 impl<'tcx, O: BitDenotation<'tcx>> DataflowResults<'tcx, O> {
403     pub fn sets(&self) -> &AllSets<O::Idx> {
404         &self.0.sets
405     }
406
407     pub fn operator(&self) -> &O {
408         &self.0.operator
409     }
410 }
411
412 /// State of a dataflow analysis; couples a collection of bit sets
413 /// with operator used to initialize and merge bits during analysis.
414 pub struct DataflowState<'tcx, O: BitDenotation<'tcx>>
415 {
416     /// All the sets for the analysis. (Factored into its
417     /// own structure so that we can borrow it mutably
418     /// on its own separate from other fields.)
419     pub sets: AllSets<O::Idx>,
420
421     /// operator used to initialize, combine, and interpret bits.
422     pub(crate) operator: O,
423 }
424
425 impl<'tcx, O: BitDenotation<'tcx>> DataflowState<'tcx, O> {
426     pub(crate) fn interpret_set<'c, P>(&self,
427                                        o: &'c O,
428                                        set: &BitSet<O::Idx>,
429                                        render_idx: &P)
430                                        -> Vec<DebugFormatted>
431         where P: Fn(&O, O::Idx) -> DebugFormatted
432     {
433         set.iter().map(|i| render_idx(o, i)).collect()
434     }
435
436     pub(crate) fn interpret_hybrid_set<'c, P>(&self,
437                                               o: &'c O,
438                                               set: &HybridBitSet<O::Idx>,
439                                               render_idx: &P)
440                                               -> Vec<DebugFormatted>
441         where P: Fn(&O, O::Idx) -> DebugFormatted
442     {
443         set.iter().map(|i| render_idx(o, i)).collect()
444     }
445 }
446
447 #[derive(Debug)]
448 pub struct AllSets<E: Idx> {
449     /// Analysis bitwidth for each block.
450     bits_per_block: usize,
451
452     /// For each block, bits valid on entry to the block.
453     on_entry_sets: Vec<BitSet<E>>,
454
455     /// For each block, bits generated by executing the statements +
456     /// terminator in the block -- with one caveat. In particular, for
457     /// *call terminators*, the effect of storing the destination is
458     /// not included, since that only takes effect on the **success**
459     /// edge (and not the unwind edge).
460     gen_sets: Vec<HybridBitSet<E>>,
461
462     /// For each block, bits killed by executing the statements +
463     /// terminator in the block -- with one caveat. In particular, for
464     /// *call terminators*, the effect of storing the destination is
465     /// not included, since that only takes effect on the **success**
466     /// edge (and not the unwind edge).
467     kill_sets: Vec<HybridBitSet<E>>,
468 }
469
470 /// Triple of sets associated with a given block.
471 ///
472 /// Generally, one sets up `on_entry`, `gen_set`, and `kill_set` for
473 /// each block individually, and then runs the dataflow analysis which
474 /// iteratively modifies the various `on_entry` sets (but leaves the
475 /// other two sets unchanged, since they represent the effect of the
476 /// block, which should be invariant over the course of the analysis).
477 ///
478 /// It is best to ensure that the intersection of `gen_set` and
479 /// `kill_set` is empty; otherwise the results of the dataflow will
480 /// have a hidden dependency on what order the bits are generated and
481 /// killed during the iteration. (This is such a good idea that the
482 /// `fn gen` and `fn kill` methods that set their state enforce this
483 /// for you.)
484 #[derive(Debug)]
485 pub struct BlockSets<'a, E: Idx> {
486     /// Dataflow state immediately before control flow enters the given block.
487     pub(crate) on_entry: &'a mut BitSet<E>,
488
489     /// Bits that are set to 1 by the time we exit the given block. Hybrid
490     /// because it usually contains only 0 or 1 elements.
491     pub(crate) gen_set: &'a mut HybridBitSet<E>,
492
493     /// Bits that are set to 0 by the time we exit the given block. Hybrid
494     /// because it usually contains only 0 or 1 elements.
495     pub(crate) kill_set: &'a mut HybridBitSet<E>,
496 }
497
498 impl<'a, E:Idx> BlockSets<'a, E> {
499     fn gen(&mut self, e: E) {
500         self.gen_set.insert(e);
501         self.kill_set.remove(e);
502     }
503     fn gen_all<I>(&mut self, i: I)
504         where I: IntoIterator,
505               I::Item: Borrow<E>
506     {
507         for j in i {
508             self.gen(*j.borrow());
509         }
510     }
511
512     fn kill(&mut self, e: E) {
513         self.gen_set.remove(e);
514         self.kill_set.insert(e);
515     }
516
517     fn kill_all<I>(&mut self, i: I)
518         where I: IntoIterator,
519               I::Item: Borrow<E>
520     {
521         for j in i {
522             self.kill(*j.borrow());
523         }
524     }
525
526     fn apply_local_effect(&mut self) {
527         self.on_entry.union(self.gen_set);
528         self.on_entry.subtract(self.kill_set);
529     }
530 }
531
532 impl<E:Idx> AllSets<E> {
533     pub fn bits_per_block(&self) -> usize { self.bits_per_block }
534     pub fn for_block(&mut self, block_idx: usize) -> BlockSets<'_, E> {
535         BlockSets {
536             on_entry: &mut self.on_entry_sets[block_idx],
537             gen_set: &mut self.gen_sets[block_idx],
538             kill_set: &mut self.kill_sets[block_idx],
539         }
540     }
541
542     pub fn on_entry_set_for(&self, block_idx: usize) -> &BitSet<E> {
543         &self.on_entry_sets[block_idx]
544     }
545     pub fn gen_set_for(&self, block_idx: usize) -> &HybridBitSet<E> {
546         &self.gen_sets[block_idx]
547     }
548     pub fn kill_set_for(&self, block_idx: usize) -> &HybridBitSet<E> {
549         &self.kill_sets[block_idx]
550     }
551 }
552
553 /// Parameterization for the precise form of data flow that is used.
554 /// `InitialFlow` handles initializing the bitvectors before any
555 /// code is inspected by the analysis. Analyses that need more nuanced
556 /// initialization (e.g., they need to consult the results of some other
557 /// dataflow analysis to set up the initial bitvectors) should not
558 /// implement this.
559 pub trait InitialFlow {
560     /// Specifies the initial value for each bit in the `on_entry` set
561     fn bottom_value() -> bool;
562 }
563
564 pub trait BitDenotation<'tcx>: BitSetOperator {
565     /// Specifies what index type is used to access the bitvector.
566     type Idx: Idx;
567
568     /// Some analyses want to accumulate knowledge within a block when
569     /// analyzing its statements for building the gen/kill sets. Override
570     /// this method to return true in such cases.
571     ///
572     /// When this returns true, the statement-effect (re)construction
573     /// will clone the `on_entry` state and pass along a reference via
574     /// `sets.on_entry` to that local clone into `statement_effect` and
575     /// `terminator_effect`).
576     ///
577     /// When it's false, no local clone is constructed; instead a
578     /// reference directly into `on_entry` is passed along via
579     /// `sets.on_entry` instead, which represents the flow state at
580     /// the block's start, not necessarily the state immediately prior
581     /// to the statement/terminator under analysis.
582     ///
583     /// In either case, the passed reference is mutable, but this is a
584     /// wart from using the `BlockSets` type in the API; the intention
585     /// is that the `statement_effect` and `terminator_effect` methods
586     /// mutate only the gen/kill sets.
587     //
588     // FIXME: we should consider enforcing the intention described in
589     // the previous paragraph by passing the three sets in separate
590     // parameters to encode their distinct mutabilities.
591     fn accumulates_intrablock_state() -> bool { false }
592
593     /// A name describing the dataflow analysis that this
594     /// `BitDenotation` is supporting. The name should be something
595     /// suitable for plugging in as part of a filename (i.e., avoid
596     /// space-characters or other things that tend to look bad on a
597     /// file system, like slashes or periods). It is also better for
598     /// the name to be reasonably short, again because it will be
599     /// plugged into a filename.
600     fn name() -> &'static str;
601
602     /// Size of each bitvector allocated for each block in the analysis.
603     fn bits_per_block(&self) -> usize;
604
605     /// Mutates the entry set according to the effects that
606     /// have been established *prior* to entering the start
607     /// block. This can't access the gen/kill sets, because
608     /// these won't be accounted for correctly.
609     ///
610     /// (For example, establishing the call arguments.)
611     fn start_block_effect(&self, entry_set: &mut BitSet<Self::Idx>);
612
613     /// Similar to `statement_effect`, except it applies
614     /// *just before* the statement rather than *just after* it.
615     ///
616     /// This matters for "dataflow at location" APIs, because the
617     /// before-statement effect is visible while visiting the
618     /// statement, while the after-statement effect only becomes
619     /// visible at the next statement.
620     ///
621     /// Both the before-statement and after-statement effects are
622     /// applied, in that order, before moving for the next
623     /// statement.
624     fn before_statement_effect(&self,
625                                _sets: &mut BlockSets<'_, Self::Idx>,
626                                _location: Location) {}
627
628     /// Mutates the block-sets (the flow sets for the given
629     /// basic block) according to the effects of evaluating statement.
630     ///
631     /// This is used, in particular, for building up the
632     /// "transfer-function" representing the overall-effect of the
633     /// block, represented via GEN and KILL sets.
634     ///
635     /// The statement is identified as `bb_data[idx_stmt]`, where
636     /// `bb_data` is the sequence of statements identified by `bb` in
637     /// the MIR.
638     fn statement_effect(&self,
639                         sets: &mut BlockSets<'_, Self::Idx>,
640                         location: Location);
641
642     /// Similar to `terminator_effect`, except it applies
643     /// *just before* the terminator rather than *just after* it.
644     ///
645     /// This matters for "dataflow at location" APIs, because the
646     /// before-terminator effect is visible while visiting the
647     /// terminator, while the after-terminator effect only becomes
648     /// visible at the terminator's successors.
649     ///
650     /// Both the before-terminator and after-terminator effects are
651     /// applied, in that order, before moving for the next
652     /// terminator.
653     fn before_terminator_effect(&self,
654                                 _sets: &mut BlockSets<'_, Self::Idx>,
655                                 _location: Location) {}
656
657     /// Mutates the block-sets (the flow sets for the given
658     /// basic block) according to the effects of evaluating
659     /// the terminator.
660     ///
661     /// This is used, in particular, for building up the
662     /// "transfer-function" representing the overall-effect of the
663     /// block, represented via GEN and KILL sets.
664     ///
665     /// The effects applied here cannot depend on which branch the
666     /// terminator took.
667     fn terminator_effect(&self,
668                          sets: &mut BlockSets<'_, Self::Idx>,
669                          location: Location);
670
671     /// Mutates the block-sets according to the (flow-dependent)
672     /// effect of a successful return from a Call terminator.
673     ///
674     /// If basic-block BB_x ends with a call-instruction that, upon
675     /// successful return, flows to BB_y, then this method will be
676     /// called on the exit flow-state of BB_x in order to set up the
677     /// entry flow-state of BB_y.
678     ///
679     /// This is used, in particular, as a special case during the
680     /// "propagate" loop where all of the basic blocks are repeatedly
681     /// visited. Since the effects of a Call terminator are
682     /// flow-dependent, the current MIR cannot encode them via just
683     /// GEN and KILL sets attached to the block, and so instead we add
684     /// this extra machinery to represent the flow-dependent effect.
685     //
686     // FIXME: right now this is a bit of a wart in the API. It might
687     // be better to represent this as an additional gen- and
688     // kill-sets associated with each edge coming out of the basic
689     // block.
690     fn propagate_call_return(
691         &self,
692         in_out: &mut BitSet<Self::Idx>,
693         call_bb: mir::BasicBlock,
694         dest_bb: mir::BasicBlock,
695         dest_place: &mir::Place<'tcx>,
696     );
697 }
698
699 impl<'a, 'tcx, D> DataflowAnalysis<'a, 'tcx, D> where D: BitDenotation<'tcx>
700 {
701     pub fn new(mir: &'a Mir<'tcx>,
702                dead_unwinds: &'a BitSet<mir::BasicBlock>,
703                denotation: D) -> Self where D: InitialFlow {
704         let bits_per_block = denotation.bits_per_block();
705         let num_blocks = mir.basic_blocks().len();
706
707         let on_entry_sets = if D::bottom_value() {
708             vec![BitSet::new_filled(bits_per_block); num_blocks]
709         } else {
710             vec![BitSet::new_empty(bits_per_block); num_blocks]
711         };
712         let gen_sets = vec![HybridBitSet::new_empty(bits_per_block); num_blocks];
713         let kill_sets = gen_sets.clone();
714
715         DataflowAnalysis {
716             mir,
717             dead_unwinds,
718             flow_state: DataflowState {
719                 sets: AllSets {
720                     bits_per_block,
721                     on_entry_sets,
722                     gen_sets,
723                     kill_sets,
724                 },
725                 operator: denotation,
726             }
727         }
728     }
729 }
730
731 impl<'a, 'tcx: 'a, D> DataflowAnalysis<'a, 'tcx, D> where D: BitDenotation<'tcx> {
732     /// Propagates the bits of `in_out` into all the successors of `bb`,
733     /// using bitwise operator denoted by `self.operator`.
734     ///
735     /// For most blocks, this is entirely uniform. However, for blocks
736     /// that end with a call terminator, the effect of the call on the
737     /// dataflow state may depend on whether the call returned
738     /// successfully or unwound.
739     ///
740     /// To reflect this, the `propagate_call_return` method of the
741     /// `BitDenotation` mutates `in_out` when propagating `in_out` via
742     /// a call terminator; such mutation is performed *last*, to
743     /// ensure its side-effects do not leak elsewhere (e.g., into
744     /// unwind target).
745     fn propagate_bits_into_graph_successors_of(
746         &mut self,
747         in_out: &mut BitSet<D::Idx>,
748         (bb, bb_data): (mir::BasicBlock, &mir::BasicBlockData<'tcx>),
749         dirty_list: &mut WorkQueue<mir::BasicBlock>)
750     {
751         match bb_data.terminator().kind {
752             mir::TerminatorKind::Return |
753             mir::TerminatorKind::Resume |
754             mir::TerminatorKind::Abort |
755             mir::TerminatorKind::GeneratorDrop |
756             mir::TerminatorKind::Unreachable => {}
757             mir::TerminatorKind::Goto { target } |
758             mir::TerminatorKind::Assert { target, cleanup: None, .. } |
759             mir::TerminatorKind::Yield { resume: target, drop: None, .. } |
760             mir::TerminatorKind::Drop { target, location: _, unwind: None } |
761             mir::TerminatorKind::DropAndReplace {
762                 target, value: _, location: _, unwind: None
763             } => {
764                 self.propagate_bits_into_entry_set_for(in_out, target, dirty_list);
765             }
766             mir::TerminatorKind::Yield { resume: target, drop: Some(drop), .. } => {
767                 self.propagate_bits_into_entry_set_for(in_out, target, dirty_list);
768                 self.propagate_bits_into_entry_set_for(in_out, drop, dirty_list);
769             }
770             mir::TerminatorKind::Assert { target, cleanup: Some(unwind), .. } |
771             mir::TerminatorKind::Drop { target, location: _, unwind: Some(unwind) } |
772             mir::TerminatorKind::DropAndReplace {
773                 target, value: _, location: _, unwind: Some(unwind)
774             } => {
775                 self.propagate_bits_into_entry_set_for(in_out, target, dirty_list);
776                 if !self.dead_unwinds.contains(bb) {
777                     self.propagate_bits_into_entry_set_for(in_out, unwind, dirty_list);
778                 }
779             }
780             mir::TerminatorKind::SwitchInt { ref targets, .. } => {
781                 for target in targets {
782                     self.propagate_bits_into_entry_set_for(in_out, *target, dirty_list);
783                 }
784             }
785             mir::TerminatorKind::Call { cleanup, ref destination, .. } => {
786                 if let Some(unwind) = cleanup {
787                     if !self.dead_unwinds.contains(bb) {
788                         self.propagate_bits_into_entry_set_for(in_out, unwind, dirty_list);
789                     }
790                 }
791                 if let Some((ref dest_place, dest_bb)) = *destination {
792                     // N.B.: This must be done *last*, after all other
793                     // propagation, as documented in comment above.
794                     self.flow_state.operator.propagate_call_return(
795                         in_out, bb, dest_bb, dest_place);
796                     self.propagate_bits_into_entry_set_for(in_out, dest_bb, dirty_list);
797                 }
798             }
799             mir::TerminatorKind::FalseEdges { real_target, ref imaginary_targets } => {
800                 self.propagate_bits_into_entry_set_for(in_out, real_target, dirty_list);
801                 for target in imaginary_targets {
802                     self.propagate_bits_into_entry_set_for(in_out, *target, dirty_list);
803                 }
804             }
805             mir::TerminatorKind::FalseUnwind { real_target, unwind } => {
806                 self.propagate_bits_into_entry_set_for(in_out, real_target, dirty_list);
807                 if let Some(unwind) = unwind {
808                     if !self.dead_unwinds.contains(bb) {
809                         self.propagate_bits_into_entry_set_for(in_out, unwind, dirty_list);
810                     }
811                 }
812             }
813         }
814     }
815
816     fn propagate_bits_into_entry_set_for(&mut self,
817                                          in_out: &BitSet<D::Idx>,
818                                          bb: mir::BasicBlock,
819                                          dirty_queue: &mut WorkQueue<mir::BasicBlock>) {
820         let entry_set = &mut self.flow_state.sets.for_block(bb.index()).on_entry;
821         let set_changed = self.flow_state.operator.join(entry_set, &in_out);
822         if set_changed {
823             dirty_queue.insert(bb);
824         }
825     }
826 }