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