]> git.lizzy.rs Git - rust.git/blob - src/librustc_borrowck/dataflow.rs
Run `rustfmt --file-lines ...` for changes from previous commits.
[rust.git] / src / librustc_borrowck / dataflow.rs
1 //! A module for propagating forward dataflow information. The analysis
2 //! assumes that the items to be propagated can be represented as bits
3 //! and thus uses bitvectors. Your job is simply to specify the so-called
4 //! GEN and KILL bits for each expression.
5
6 use rustc::cfg;
7 use rustc::cfg::CFGIndex;
8 use rustc::ty::TyCtxt;
9 use std::io;
10 use std::mem;
11 use std::usize;
12 use syntax::print::pprust::PrintState;
13 use log::debug;
14
15 use rustc_data_structures::graph::implementation::OUTGOING;
16
17 use rustc::util::nodemap::FxHashMap;
18 use rustc::hir;
19 use rustc::hir::intravisit;
20 use rustc::hir::print as pprust;
21
22 #[derive(Copy, Clone, Debug)]
23 pub enum EntryOrExit {
24     Entry,
25     Exit,
26 }
27
28 #[derive(Clone)]
29 pub struct DataFlowContext<'tcx, O> {
30     tcx: TyCtxt<'tcx, 'tcx>,
31
32     /// a name for the analysis using this dataflow instance
33     analysis_name: &'static str,
34
35     /// the data flow operator
36     oper: O,
37
38     /// number of bits to propagate per id
39     bits_per_id: usize,
40
41     /// number of words we will use to store bits_per_id.
42     /// equal to bits_per_id/usize::BITS rounded up.
43     words_per_id: usize,
44
45     // mapping from node to cfg node index
46     // FIXME (#6298): Shouldn't this go with CFG?
47     local_id_to_index: FxHashMap<hir::ItemLocalId, Vec<CFGIndex>>,
48
49     // Bit sets per cfg node.  The following three fields (`gens`, `kills`,
50     // and `on_entry`) all have the same structure. For each id in
51     // `id_range`, there is a range of words equal to `words_per_id`.
52     // So, to access the bits for any given id, you take a slice of
53     // the full vector (see the method `compute_id_range()`).
54     /// bits generated as we exit the cfg node. Updated by `add_gen()`.
55     gens: Vec<usize>,
56
57     /// bits killed as we exit the cfg node, or non-locally jump over
58     /// it. Updated by `add_kill(KillFrom::ScopeEnd)`.
59     scope_kills: Vec<usize>,
60
61     /// bits killed as we exit the cfg node directly; if it is jumped
62     /// over, e.g., via `break`, the kills are not reflected in the
63     /// jump's effects. Updated by `add_kill(KillFrom::Execution)`.
64     action_kills: Vec<usize>,
65
66     /// bits that are valid on entry to the cfg node. Updated by
67     /// `propagate()`.
68     on_entry: Vec<usize>,
69 }
70
71 pub trait BitwiseOperator {
72     /// Joins two predecessor bits together, typically either `|` or `&`
73     fn join(&self, succ: usize, pred: usize) -> usize;
74 }
75
76 /// Parameterization for the precise form of data flow that is used.
77 pub trait DataFlowOperator : BitwiseOperator {
78     /// Specifies the initial value for each bit in the `on_entry` set
79     fn initial_value(&self) -> bool;
80 }
81
82 struct PropagationContext<'a, 'tcx, O> {
83     dfcx: &'a mut DataFlowContext<'tcx, O>,
84     changed: bool,
85 }
86
87 fn get_cfg_indices<'a>(id: hir::ItemLocalId,
88                        index: &'a FxHashMap<hir::ItemLocalId, Vec<CFGIndex>>)
89                        -> &'a [CFGIndex] {
90     index.get(&id).map_or(&[], |v| &v[..])
91 }
92
93 impl<'tcx, O: DataFlowOperator> DataFlowContext<'tcx, O> {
94     fn has_bitset_for_local_id(&self, n: hir::ItemLocalId) -> bool {
95         assert!(n != hir::DUMMY_ITEM_LOCAL_ID);
96         self.local_id_to_index.contains_key(&n)
97     }
98 }
99
100 impl<'tcx, O: DataFlowOperator> pprust::PpAnn for DataFlowContext<'tcx, O> {
101     fn nested(&self, state: &mut pprust::State<'_>, nested: pprust::Nested) -> io::Result<()> {
102         pprust::PpAnn::nested(self.tcx.hir(), state, nested)
103     }
104     fn pre(&self,
105            ps: &mut pprust::State<'_>,
106            node: pprust::AnnNode<'_>) -> io::Result<()> {
107         let id = match node {
108             pprust::AnnNode::Name(_) => return Ok(()),
109             pprust::AnnNode::Expr(expr) => expr.hir_id.local_id,
110             pprust::AnnNode::Block(blk) => blk.hir_id.local_id,
111             pprust::AnnNode::Item(_) |
112             pprust::AnnNode::SubItem(_) => return Ok(()),
113             pprust::AnnNode::Pat(pat) => pat.hir_id.local_id
114         };
115
116         if !self.has_bitset_for_local_id(id) {
117             return Ok(());
118         }
119
120         assert!(self.bits_per_id > 0);
121         let indices = get_cfg_indices(id, &self.local_id_to_index);
122         for &cfgidx in indices {
123             let (start, end) = self.compute_id_range(cfgidx);
124             let on_entry = &self.on_entry[start.. end];
125             let entry_str = bits_to_string(on_entry);
126
127             let gens = &self.gens[start.. end];
128             let gens_str = if gens.iter().any(|&u| u != 0) {
129                 format!(" gen: {}", bits_to_string(gens))
130             } else {
131                 String::new()
132             };
133
134             let action_kills = &self.action_kills[start .. end];
135             let action_kills_str = if action_kills.iter().any(|&u| u != 0) {
136                 format!(" action_kill: {}", bits_to_string(action_kills))
137             } else {
138                 String::new()
139             };
140
141             let scope_kills = &self.scope_kills[start .. end];
142             let scope_kills_str = if scope_kills.iter().any(|&u| u != 0) {
143                 format!(" scope_kill: {}", bits_to_string(scope_kills))
144             } else {
145                 String::new()
146             };
147
148             ps.synth_comment(
149                 format!("id {}: {}{}{}{}", id.as_usize(), entry_str,
150                         gens_str, action_kills_str, scope_kills_str))?;
151             ps.s.space()?;
152         }
153         Ok(())
154     }
155 }
156
157 fn build_local_id_to_index(body: Option<&hir::Body>,
158                            cfg: &cfg::CFG)
159                            -> FxHashMap<hir::ItemLocalId, Vec<CFGIndex>> {
160     let mut index = FxHashMap::default();
161
162     // FIXME(#15020) Would it be better to fold formals from decl
163     // into cfg itself?  i.e., introduce a fn-based flow-graph in
164     // addition to the current block-based flow-graph, rather than
165     // have to put traversals like this here?
166     if let Some(body) = body {
167         add_entries_from_fn_body(&mut index, body, cfg.entry);
168     }
169
170     cfg.graph.each_node(|node_idx, node| {
171         if let cfg::CFGNodeData::AST(id) = node.data {
172             index.entry(id).or_default().push(node_idx);
173         }
174         true
175     });
176
177     return index;
178
179     /// Adds mappings from the ast nodes for the formal bindings to
180     /// the entry-node in the graph.
181     fn add_entries_from_fn_body(index: &mut FxHashMap<hir::ItemLocalId, Vec<CFGIndex>>,
182                                 body: &hir::Body,
183                                 entry: CFGIndex) {
184         use rustc::hir::intravisit::Visitor;
185
186         struct Formals<'a> {
187             entry: CFGIndex,
188             index: &'a mut FxHashMap<hir::ItemLocalId, Vec<CFGIndex>>,
189         }
190         let mut formals = Formals { entry: entry, index: index };
191         for arg in &body.arguments {
192             formals.visit_pat(&arg.pat);
193         }
194         impl<'a, 'v> Visitor<'v> for Formals<'a> {
195             fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'v> {
196                 intravisit::NestedVisitorMap::None
197             }
198
199             fn visit_pat(&mut self, p: &hir::Pat) {
200                 self.index.entry(p.hir_id.local_id).or_default().push(self.entry);
201                 intravisit::walk_pat(self, p)
202             }
203         }
204     }
205 }
206
207 /// Flag used by `add_kill` to indicate whether the provided kill
208 /// takes effect only when control flows directly through the node in
209 /// question, or if the kill's effect is associated with any
210 /// control-flow directly through or indirectly over the node.
211 #[derive(Copy, Clone, PartialEq, Debug)]
212 pub enum KillFrom {
213     /// A `ScopeEnd` kill is one that takes effect when any control
214     /// flow goes over the node. A kill associated with the end of the
215     /// scope of a variable declaration `let x;` is an example of a
216     /// `ScopeEnd` kill.
217     ScopeEnd,
218
219     /// An `Execution` kill is one that takes effect only when control
220     /// flow goes through the node to completion. A kill associated
221     /// with an assignment statement `x = expr;` is an example of an
222     /// `Execution` kill.
223     Execution,
224 }
225
226 impl<'tcx, O: DataFlowOperator> DataFlowContext<'tcx, O> {
227     pub fn new(
228         tcx: TyCtxt<'tcx, 'tcx>,
229         analysis_name: &'static str,
230         body: Option<&hir::Body>,
231         cfg: &cfg::CFG,
232         oper: O,
233         bits_per_id: usize,
234     ) -> DataFlowContext<'tcx, O> {
235         let usize_bits = mem::size_of::<usize>() * 8;
236         let words_per_id = (bits_per_id + usize_bits - 1) / usize_bits;
237         let num_nodes = cfg.graph.all_nodes().len();
238
239         debug!("DataFlowContext::new(analysis_name: {}, \
240                                      bits_per_id={}, words_per_id={}) \
241                                      num_nodes: {}",
242                analysis_name, bits_per_id, words_per_id,
243                num_nodes);
244
245         let entry = if oper.initial_value() { usize::MAX } else {0};
246
247         let zeroes = vec![0; num_nodes * words_per_id];
248         let gens = zeroes.clone();
249         let kills1 = zeroes.clone();
250         let kills2 = zeroes;
251         let on_entry = vec![entry; num_nodes * words_per_id];
252
253         let local_id_to_index = build_local_id_to_index(body, cfg);
254
255         DataFlowContext {
256             tcx,
257             analysis_name,
258             words_per_id,
259             local_id_to_index,
260             bits_per_id,
261             oper,
262             gens,
263             action_kills: kills1,
264             scope_kills: kills2,
265             on_entry,
266         }
267     }
268
269     pub fn add_gen(&mut self, id: hir::ItemLocalId, bit: usize) {
270         //! Indicates that `id` generates `bit`
271         debug!("{} add_gen(id={:?}, bit={})",
272                self.analysis_name, id, bit);
273         assert!(self.local_id_to_index.contains_key(&id));
274         assert!(self.bits_per_id > 0);
275
276         let indices = get_cfg_indices(id, &self.local_id_to_index);
277         for &cfgidx in indices {
278             let (start, end) = self.compute_id_range(cfgidx);
279             let gens = &mut self.gens[start.. end];
280             set_bit(gens, bit);
281         }
282     }
283
284     pub fn add_kill(&mut self, kind: KillFrom, id: hir::ItemLocalId, bit: usize) {
285         //! Indicates that `id` kills `bit`
286         debug!("{} add_kill(id={:?}, bit={})",
287                self.analysis_name, id, bit);
288         assert!(self.local_id_to_index.contains_key(&id));
289         assert!(self.bits_per_id > 0);
290
291         let indices = get_cfg_indices(id, &self.local_id_to_index);
292         for &cfgidx in indices {
293             let (start, end) = self.compute_id_range(cfgidx);
294             let kills = match kind {
295                 KillFrom::Execution => &mut self.action_kills[start.. end],
296                 KillFrom::ScopeEnd =>  &mut self.scope_kills[start.. end],
297             };
298             set_bit(kills, bit);
299         }
300     }
301
302     fn apply_gen_kill(&self, cfgidx: CFGIndex, bits: &mut [usize]) {
303         //! Applies the gen and kill sets for `cfgidx` to `bits`
304         debug!("{} apply_gen_kill(cfgidx={:?}, bits={}) [before]",
305                self.analysis_name, cfgidx, mut_bits_to_string(bits));
306         assert!(self.bits_per_id > 0);
307
308         let (start, end) = self.compute_id_range(cfgidx);
309         let gens = &self.gens[start.. end];
310         bitwise(bits, gens, &Union);
311         let kills = &self.action_kills[start.. end];
312         bitwise(bits, kills, &Subtract);
313         let kills = &self.scope_kills[start.. end];
314         bitwise(bits, kills, &Subtract);
315
316         debug!("{} apply_gen_kill(cfgidx={:?}, bits={}) [after]",
317                self.analysis_name, cfgidx, mut_bits_to_string(bits));
318     }
319
320     fn compute_id_range(&self, cfgidx: CFGIndex) -> (usize, usize) {
321         let n = cfgidx.node_id();
322         let start = n * self.words_per_id;
323         let end = start + self.words_per_id;
324
325         assert!(start < self.gens.len());
326         assert!(end <= self.gens.len());
327         assert!(self.gens.len() == self.action_kills.len());
328         assert!(self.gens.len() == self.scope_kills.len());
329         assert!(self.gens.len() == self.on_entry.len());
330
331         (start, end)
332     }
333
334
335     pub fn each_bit_on_entry<F>(&self, id: hir::ItemLocalId, mut f: F) -> bool where
336         F: FnMut(usize) -> bool,
337     {
338         //! Iterates through each bit that is set on entry to `id`.
339         //! Only useful after `propagate()` has been called.
340         if !self.has_bitset_for_local_id(id) {
341             return true;
342         }
343         let indices = get_cfg_indices(id, &self.local_id_to_index);
344         for &cfgidx in indices {
345             if !self.each_bit_for_node(EntryOrExit::Entry, cfgidx, |i| f(i)) {
346                 return false;
347             }
348         }
349         return true;
350     }
351
352     pub fn each_bit_for_node<F>(&self, e: EntryOrExit, cfgidx: CFGIndex, f: F) -> bool where
353         F: FnMut(usize) -> bool,
354     {
355         //! Iterates through each bit that is set on entry/exit to `cfgidx`.
356         //! Only useful after `propagate()` has been called.
357
358         if self.bits_per_id == 0 {
359             // Skip the surprisingly common degenerate case.  (Note
360             // compute_id_range requires self.words_per_id > 0.)
361             return true;
362         }
363
364         let (start, end) = self.compute_id_range(cfgidx);
365         let on_entry = &self.on_entry[start.. end];
366         let temp_bits;
367         let slice = match e {
368             EntryOrExit::Entry => on_entry,
369             EntryOrExit::Exit => {
370                 let mut t = on_entry.to_vec();
371                 self.apply_gen_kill(cfgidx, &mut t);
372                 temp_bits = t;
373                 &temp_bits[..]
374             }
375         };
376         debug!("{} each_bit_for_node({:?}, cfgidx={:?}) bits={}",
377                self.analysis_name, e, cfgidx, bits_to_string(slice));
378         self.each_bit(slice, f)
379     }
380
381     pub fn each_gen_bit<F>(&self, id: hir::ItemLocalId, mut f: F) -> bool where
382         F: FnMut(usize) -> bool,
383     {
384         //! Iterates through each bit in the gen set for `id`.
385         if !self.has_bitset_for_local_id(id) {
386             return true;
387         }
388
389         if self.bits_per_id == 0 {
390             // Skip the surprisingly common degenerate case.  (Note
391             // compute_id_range requires self.words_per_id > 0.)
392             return true;
393         }
394
395         let indices = get_cfg_indices(id, &self.local_id_to_index);
396         for &cfgidx in indices {
397             let (start, end) = self.compute_id_range(cfgidx);
398             let gens = &self.gens[start.. end];
399             debug!("{} each_gen_bit(id={:?}, gens={})",
400                    self.analysis_name, id, bits_to_string(gens));
401             if !self.each_bit(gens, |i| f(i)) {
402                 return false;
403             }
404         }
405         return true;
406     }
407
408     fn each_bit<F>(&self, words: &[usize], mut f: F) -> bool where
409         F: FnMut(usize) -> bool,
410     {
411         //! Helper for iterating over the bits in a bit set.
412         //! Returns false on the first call to `f` that returns false;
413         //! if all calls to `f` return true, then returns true.
414
415         let usize_bits = mem::size_of::<usize>() * 8;
416         for (word_index, &word) in words.iter().enumerate() {
417             if word != 0 {
418                 let base_index = word_index * usize_bits;
419                 for offset in 0..usize_bits {
420                     let bit = 1 << offset;
421                     if (word & bit) != 0 {
422                         // N.B., we round up the total number of bits
423                         // that we store in any given bit set so that
424                         // it is an even multiple of usize::BITS.  This
425                         // means that there may be some stray bits at
426                         // the end that do not correspond to any
427                         // actual value.  So before we callback, check
428                         // whether the bit_index is greater than the
429                         // actual value the user specified and stop
430                         // iterating if so.
431                         let bit_index = base_index + offset as usize;
432                         if bit_index >= self.bits_per_id {
433                             return true;
434                         } else if !f(bit_index) {
435                             return false;
436                         }
437                     }
438                 }
439             }
440         }
441         return true;
442     }
443
444     pub fn add_kills_from_flow_exits(&mut self, cfg: &cfg::CFG) {
445         //! Whenever you have a `break` or `continue` statement, flow
446         //! exits through any number of enclosing scopes on its way to
447         //! the new destination. This function infers the kill bits of
448         //! those control operators based on the kill bits associated
449         //! with those scopes.
450         //!
451         //! This is usually called (if it is called at all), after
452         //! all add_gen and add_kill calls, but before propagate.
453
454         debug!("{} add_kills_from_flow_exits", self.analysis_name);
455         if self.bits_per_id == 0 {
456             // Skip the surprisingly common degenerate case.  (Note
457             // compute_id_range requires self.words_per_id > 0.)
458             return;
459         }
460         cfg.graph.each_edge(|_edge_index, edge| {
461             let flow_exit = edge.source();
462             let (start, end) = self.compute_id_range(flow_exit);
463             let mut orig_kills = self.scope_kills[start.. end].to_vec();
464
465             let mut changed = false;
466             for &id in &edge.data.exiting_scopes {
467                 let opt_cfg_idx = self.local_id_to_index.get(&id);
468                 match opt_cfg_idx {
469                     Some(indices) => {
470                         for &cfg_idx in indices {
471                             let (start, end) = self.compute_id_range(cfg_idx);
472                             let kills = &self.scope_kills[start.. end];
473                             if bitwise(&mut orig_kills, kills, &Union) {
474                                 debug!("scope exits: scope id={:?} \
475                                         (node={:?} of {:?}) added killset: {}",
476                                        id, cfg_idx, indices,
477                                        bits_to_string(kills));
478                                 changed = true;
479                             }
480                         }
481                     }
482                     None => {
483                         debug!("{} add_kills_from_flow_exits flow_exit={:?} \
484                                 no cfg_idx for exiting_scope={:?}",
485                                self.analysis_name, flow_exit, id);
486                     }
487                 }
488             }
489
490             if changed {
491                 let bits = &mut self.scope_kills[start.. end];
492                 debug!("{} add_kills_from_flow_exits flow_exit={:?} bits={} [before]",
493                        self.analysis_name, flow_exit, mut_bits_to_string(bits));
494                 bits.copy_from_slice(&orig_kills[..]);
495                 debug!("{} add_kills_from_flow_exits flow_exit={:?} bits={} [after]",
496                        self.analysis_name, flow_exit, mut_bits_to_string(bits));
497             }
498             true
499         });
500     }
501 }
502
503 // N.B. `Clone + 'static` only needed for pretty printing.
504 impl<'tcx, O: DataFlowOperator + Clone + 'static> DataFlowContext<'tcx, O> {
505     pub fn propagate(&mut self, cfg: &cfg::CFG, body: &hir::Body) {
506         //! Performs the data flow analysis.
507
508         if self.bits_per_id == 0 {
509             // Optimize the surprisingly common degenerate case.
510             return;
511         }
512
513         {
514             let words_per_id = self.words_per_id;
515             let mut propcx = PropagationContext {
516                 dfcx: &mut *self,
517                 changed: true
518             };
519
520             let nodes_po = cfg.graph.nodes_in_postorder(OUTGOING, cfg.entry);
521             let mut temp = vec![0; words_per_id];
522             let mut num_passes = 0;
523             while propcx.changed {
524                 num_passes += 1;
525                 propcx.changed = false;
526                 propcx.reset(&mut temp);
527                 propcx.walk_cfg(cfg, &nodes_po, &mut temp);
528             }
529             debug!("finished in {} iterations", num_passes);
530         }
531
532         debug!("Dataflow result for {}:", self.analysis_name);
533         debug!("{}", pprust::to_string(self, |s| {
534             s.cbox(pprust::indent_unit)?;
535             s.ibox(0)?;
536             s.print_expr(&body.value)
537         }));
538     }
539 }
540
541 impl<O: DataFlowOperator> PropagationContext<'_, 'tcx, O> {
542     fn walk_cfg(&mut self,
543                 cfg: &cfg::CFG,
544                 nodes_po: &[CFGIndex],
545                 in_out: &mut [usize]) {
546         debug!("DataFlowContext::walk_cfg(in_out={}) {}",
547                bits_to_string(in_out), self.dfcx.analysis_name);
548         assert!(self.dfcx.bits_per_id > 0);
549
550         // Iterate over nodes in reverse post-order.
551         for &node_index in nodes_po.iter().rev() {
552             let node = cfg.graph.node(node_index);
553             debug!("DataFlowContext::walk_cfg idx={:?} id={:?} begin in_out={}",
554                    node_index, node.data.id(), bits_to_string(in_out));
555
556             let (start, end) = self.dfcx.compute_id_range(node_index);
557
558             // Initialize local bitvector with state on-entry.
559             in_out.copy_from_slice(&self.dfcx.on_entry[start.. end]);
560
561             // Compute state on-exit by applying transfer function to
562             // state on-entry.
563             self.dfcx.apply_gen_kill(node_index, in_out);
564
565             // Propagate state on-exit from node into its successors.
566             self.propagate_bits_into_graph_successors_of(in_out, cfg, node_index);
567         }
568     }
569
570     fn reset(&mut self, bits: &mut [usize]) {
571         let e = if self.dfcx.oper.initial_value() {usize::MAX} else {0};
572         for b in bits {
573             *b = e;
574         }
575     }
576
577     fn propagate_bits_into_graph_successors_of(&mut self,
578                                                pred_bits: &[usize],
579                                                cfg: &cfg::CFG,
580                                                cfgidx: CFGIndex) {
581         for (_, edge) in cfg.graph.outgoing_edges(cfgidx) {
582             self.propagate_bits_into_entry_set_for(pred_bits, edge);
583         }
584     }
585
586     fn propagate_bits_into_entry_set_for(&mut self,
587                                          pred_bits: &[usize],
588                                          edge: &cfg::CFGEdge) {
589         let source = edge.source();
590         let cfgidx = edge.target();
591         debug!("{} propagate_bits_into_entry_set_for(pred_bits={}, {:?} to {:?})",
592                self.dfcx.analysis_name, bits_to_string(pred_bits), source, cfgidx);
593         assert!(self.dfcx.bits_per_id > 0);
594
595         let (start, end) = self.dfcx.compute_id_range(cfgidx);
596         let changed = {
597             // (scoping mutable borrow of self.dfcx.on_entry)
598             let on_entry = &mut self.dfcx.on_entry[start.. end];
599             bitwise(on_entry, pred_bits, &self.dfcx.oper)
600         };
601         if changed {
602             debug!("{} changed entry set for {:?} to {}",
603                    self.dfcx.analysis_name, cfgidx,
604                    bits_to_string(&self.dfcx.on_entry[start.. end]));
605             self.changed = true;
606         }
607     }
608 }
609
610 fn mut_bits_to_string(words: &mut [usize]) -> String {
611     bits_to_string(words)
612 }
613
614 fn bits_to_string(words: &[usize]) -> String {
615     let mut result = String::new();
616     let mut sep = '[';
617
618     // Note: this is a little endian printout of bytes.
619
620     for &word in words {
621         let mut v = word;
622         for _ in 0..mem::size_of::<usize>() {
623             result.push(sep);
624             result.push_str(&format!("{:02x}", v & 0xFF));
625             v >>= 8;
626             sep = '-';
627         }
628     }
629     result.push(']');
630     return result
631 }
632
633 #[inline]
634 fn bitwise<Op: BitwiseOperator>(out_vec: &mut [usize],
635                                 in_vec: &[usize],
636                                 op: &Op) -> bool {
637     assert_eq!(out_vec.len(), in_vec.len());
638     let mut changed = false;
639     for (out_elt, in_elt) in out_vec.iter_mut().zip(in_vec) {
640         let old_val = *out_elt;
641         let new_val = op.join(old_val, *in_elt);
642         *out_elt = new_val;
643         changed |= old_val != new_val;
644     }
645     changed
646 }
647
648 fn set_bit(words: &mut [usize], bit: usize) -> bool {
649     debug!("set_bit: words={} bit={}",
650            mut_bits_to_string(words), bit_str(bit));
651     let usize_bits = mem::size_of::<usize>() * 8;
652     let word = bit / usize_bits;
653     let bit_in_word = bit % usize_bits;
654     let bit_mask = 1 << bit_in_word;
655     debug!("word={} bit_in_word={} bit_mask={}", word, bit_in_word, bit_mask);
656     let oldv = words[word];
657     let newv = oldv | bit_mask;
658     words[word] = newv;
659     oldv != newv
660 }
661
662 fn bit_str(bit: usize) -> String {
663     let byte = bit >> 3;
664     let lobits = 1 << (bit & 0b111);
665     format!("[{}:{}-{:02x}]", bit, byte, lobits)
666 }
667
668 struct Union;
669 impl BitwiseOperator for Union {
670     fn join(&self, a: usize, b: usize) -> usize { a | b }
671 }
672 struct Subtract;
673 impl BitwiseOperator for Subtract {
674     fn join(&self, a: usize, b: usize) -> usize { a & !b }
675 }