1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 //! A module for propagating forward dataflow information. The analysis
13 //! assumes that the items to be propagated can be represented as bits
14 //! and thus uses bitvectors. Your job is simply to specify the so-called
15 //! GEN and KILL bits for each expression.
17 pub use self::EntryOrExit::*;
20 use middle::cfg::CFGIndex;
25 use syntax::ast_util::IdRange;
27 use syntax::print::{pp, pprust};
28 use util::nodemap::NodeMap;
31 pub enum EntryOrExit { Entry, Exit }
34 pub struct DataFlowContext<'a, 'tcx: 'a, O> {
35 tcx: &'a ty::ctxt<'tcx>,
37 /// a name for the analysis using this dataflow instance
38 analysis_name: &'static str,
40 /// the data flow operator
43 /// number of bits to propagate per id
46 /// number of words we will use to store bits_per_id.
47 /// equal to bits_per_id/uint::BITS rounded up.
50 // mapping from node to cfg node index
51 // FIXME (#6298): Shouldn't this go with CFG?
52 nodeid_to_index: NodeMap<CFGIndex>,
54 // Bit sets per cfg node. The following three fields (`gens`, `kills`,
55 // and `on_entry`) all have the same structure. For each id in
56 // `id_range`, there is a range of words equal to `words_per_id`.
57 // So, to access the bits for any given id, you take a slice of
58 // the full vector (see the method `compute_id_range()`).
60 /// bits generated as we exit the cfg node. Updated by `add_gen()`.
63 /// bits killed as we exit the cfg node. Updated by `add_kill()`.
66 /// bits that are valid on entry to the cfg node. Updated by
71 pub trait BitwiseOperator {
72 /// Joins two predecessor bits together, typically either `|` or `&`
73 fn join(&self, succ: uint, pred: uint) -> uint;
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;
82 struct PropagationContext<'a, 'b: 'a, 'tcx: 'b, O: 'a> {
83 dfcx: &'a mut DataFlowContext<'b, 'tcx, O>,
87 fn to_cfgidx_or_die(id: ast::NodeId, index: &NodeMap<CFGIndex>) -> CFGIndex {
88 let opt_cfgindex = index.get(&id).map(|&i|i);
89 opt_cfgindex.unwrap_or_else(|| {
90 panic!("nodeid_to_index does not have entry for NodeId {}", id);
94 impl<'a, 'tcx, O:DataFlowOperator> DataFlowContext<'a, 'tcx, O> {
95 fn has_bitset_for_nodeid(&self, n: ast::NodeId) -> bool {
96 assert!(n != ast::DUMMY_NODE_ID);
97 self.nodeid_to_index.contains_key(&n)
101 impl<'a, 'tcx, O:DataFlowOperator> pprust::PpAnn for DataFlowContext<'a, 'tcx, O> {
103 ps: &mut pprust::State,
104 node: pprust::AnnNode) -> io::IoResult<()> {
105 let id = match node {
106 pprust::NodeIdent(_) | pprust::NodeName(_) => 0,
107 pprust::NodeExpr(expr) => expr.id,
108 pprust::NodeBlock(blk) => blk.id,
109 pprust::NodeItem(_) => 0,
110 pprust::NodePat(pat) => pat.id
113 if self.has_bitset_for_nodeid(id) {
114 assert!(self.bits_per_id > 0);
115 let cfgidx = to_cfgidx_or_die(id, &self.nodeid_to_index);
116 let (start, end) = self.compute_id_range(cfgidx);
117 let on_entry = self.on_entry.slice(start, end);
118 let entry_str = bits_to_string(on_entry);
120 let gens = self.gens.slice(start, end);
121 let gens_str = if gens.iter().any(|&u| u != 0) {
122 format!(" gen: {}", bits_to_string(gens))
127 let kills = self.kills.slice(start, end);
128 let kills_str = if kills.iter().any(|&u| u != 0) {
129 format!(" kill: {}", bits_to_string(kills))
134 try!(ps.synth_comment(format!("id {}: {}{}{}", id, entry_str,
135 gens_str, kills_str)));
136 try!(pp::space(&mut ps.s));
142 fn build_nodeid_to_index(decl: Option<&ast::FnDecl>,
143 cfg: &cfg::CFG) -> NodeMap<CFGIndex> {
144 let mut index = NodeMap::new();
146 // FIXME (#6298): Would it be better to fold formals from decl
147 // into cfg itself? i.e. introduce a fn-based flow-graph in
148 // addition to the current block-based flow-graph, rather than
149 // have to put traversals like this here?
152 Some(decl) => add_entries_from_fn_decl(&mut index, decl, cfg.entry)
155 cfg.graph.each_node(|node_idx, node| {
156 if node.data.id != ast::DUMMY_NODE_ID {
157 index.insert(node.data.id, node_idx);
164 fn add_entries_from_fn_decl(index: &mut NodeMap<CFGIndex>,
167 //! add mappings from the ast nodes for the formal bindings to
168 //! the entry-node in the graph.
171 index: &'a mut NodeMap<CFGIndex>,
173 let mut formals = Formals { entry: entry, index: index };
174 visit::walk_fn_decl(&mut formals, decl);
175 impl<'a, 'v> visit::Visitor<'v> for Formals<'a> {
176 fn visit_pat(&mut self, p: &ast::Pat) {
177 self.index.insert(p.id, self.entry);
178 visit::walk_pat(self, p)
184 impl<'a, 'tcx, O:DataFlowOperator> DataFlowContext<'a, 'tcx, O> {
185 pub fn new(tcx: &'a ty::ctxt<'tcx>,
186 analysis_name: &'static str,
187 decl: Option<&ast::FnDecl>,
191 bits_per_id: uint) -> DataFlowContext<'a, 'tcx, O> {
192 let words_per_id = (bits_per_id + uint::BITS - 1) / uint::BITS;
193 let num_nodes = cfg.graph.all_nodes().len();
195 debug!("DataFlowContext::new(analysis_name: {}, id_range={}, \
196 bits_per_id={}, words_per_id={}) \
198 analysis_name, id_range, bits_per_id, words_per_id,
201 let entry = if oper.initial_value() { uint::MAX } else {0};
203 let gens = Vec::from_elem(num_nodes * words_per_id, 0);
204 let kills = Vec::from_elem(num_nodes * words_per_id, 0);
205 let on_entry = Vec::from_elem(num_nodes * words_per_id, entry);
207 let nodeid_to_index = build_nodeid_to_index(decl, cfg);
211 analysis_name: analysis_name,
212 words_per_id: words_per_id,
213 nodeid_to_index: nodeid_to_index,
214 bits_per_id: bits_per_id,
222 pub fn add_gen(&mut self, id: ast::NodeId, bit: uint) {
223 //! Indicates that `id` generates `bit`
224 debug!("{} add_gen(id={}, bit={})",
225 self.analysis_name, id, bit);
226 assert!(self.nodeid_to_index.contains_key(&id));
227 assert!(self.bits_per_id > 0);
229 let cfgidx = to_cfgidx_or_die(id, &self.nodeid_to_index);
230 let (start, end) = self.compute_id_range(cfgidx);
231 let gens = self.gens.slice_mut(start, end);
235 pub fn add_kill(&mut self, id: ast::NodeId, bit: uint) {
236 //! Indicates that `id` kills `bit`
237 debug!("{} add_kill(id={}, bit={})",
238 self.analysis_name, id, bit);
239 assert!(self.nodeid_to_index.contains_key(&id));
240 assert!(self.bits_per_id > 0);
242 let cfgidx = to_cfgidx_or_die(id, &self.nodeid_to_index);
243 let (start, end) = self.compute_id_range(cfgidx);
244 let kills = self.kills.slice_mut(start, end);
248 fn apply_gen_kill(&self, cfgidx: CFGIndex, bits: &mut [uint]) {
249 //! Applies the gen and kill sets for `cfgidx` to `bits`
250 debug!("{} apply_gen_kill(cfgidx={}, bits={}) [before]",
251 self.analysis_name, cfgidx, mut_bits_to_string(bits));
252 assert!(self.bits_per_id > 0);
254 let (start, end) = self.compute_id_range(cfgidx);
255 let gens = self.gens.slice(start, end);
256 bitwise(bits, gens, &Union);
257 let kills = self.kills.slice(start, end);
258 bitwise(bits, kills, &Subtract);
260 debug!("{} apply_gen_kill(cfgidx={}, bits={}) [after]",
261 self.analysis_name, cfgidx, mut_bits_to_string(bits));
264 fn compute_id_range(&self, cfgidx: CFGIndex) -> (uint, uint) {
265 let n = cfgidx.node_id();
266 let start = n * self.words_per_id;
267 let end = start + self.words_per_id;
269 assert!(start < self.gens.len());
270 assert!(end <= self.gens.len());
271 assert!(self.gens.len() == self.kills.len());
272 assert!(self.gens.len() == self.on_entry.len());
278 pub fn each_bit_on_entry(&self,
282 //! Iterates through each bit that is set on entry to `id`.
283 //! Only useful after `propagate()` has been called.
284 if !self.has_bitset_for_nodeid(id) {
287 let cfgidx = to_cfgidx_or_die(id, &self.nodeid_to_index);
288 self.each_bit_for_node(Entry, cfgidx, f)
291 pub fn each_bit_for_node(&self,
296 //! Iterates through each bit that is set on entry/exit to `cfgidx`.
297 //! Only useful after `propagate()` has been called.
299 if self.bits_per_id == 0 {
300 // Skip the surprisingly common degenerate case. (Note
301 // compute_id_range requires self.words_per_id > 0.)
305 let (start, end) = self.compute_id_range(cfgidx);
306 let on_entry = self.on_entry.slice(start, end);
308 let slice = match e {
311 let mut t = on_entry.to_vec();
312 self.apply_gen_kill(cfgidx, t.as_mut_slice());
317 debug!("{} each_bit_for_node({}, cfgidx={}) bits={}",
318 self.analysis_name, e, cfgidx, bits_to_string(slice));
319 self.each_bit(slice, f)
322 pub fn each_gen_bit(&self, id: ast::NodeId, f: |uint| -> bool)
324 //! Iterates through each bit in the gen set for `id`.
325 if !self.has_bitset_for_nodeid(id) {
329 if self.bits_per_id == 0 {
330 // Skip the surprisingly common degenerate case. (Note
331 // compute_id_range requires self.words_per_id > 0.)
335 let cfgidx = to_cfgidx_or_die(id, &self.nodeid_to_index);
336 let (start, end) = self.compute_id_range(cfgidx);
337 let gens = self.gens.slice(start, end);
338 debug!("{} each_gen_bit(id={}, gens={})",
339 self.analysis_name, id, bits_to_string(gens));
340 self.each_bit(gens, f)
343 fn each_bit(&self, words: &[uint], f: |uint| -> bool) -> bool {
344 //! Helper for iterating over the bits in a bit set.
345 //! Returns false on the first call to `f` that returns false;
346 //! if all calls to `f` return true, then returns true.
348 for (word_index, &word) in words.iter().enumerate() {
350 let base_index = word_index * uint::BITS;
351 for offset in range(0u, uint::BITS) {
352 let bit = 1 << offset;
353 if (word & bit) != 0 {
354 // NB: we round up the total number of bits
355 // that we store in any given bit set so that
356 // it is an even multiple of uint::BITS. This
357 // means that there may be some stray bits at
358 // the end that do not correspond to any
359 // actual value. So before we callback, check
360 // whether the bit_index is greater than the
361 // actual value the user specified and stop
363 let bit_index = base_index + offset;
364 if bit_index >= self.bits_per_id {
366 } else if !f(bit_index) {
376 pub fn add_kills_from_flow_exits(&mut self, cfg: &cfg::CFG) {
377 //! Whenever you have a `break` or `continue` statement, flow
378 //! exits through any number of enclosing scopes on its way to
379 //! the new destination. This function infers the kill bits of
380 //! those control operators based on the kill bits associated
381 //! with those scopes.
383 //! This is usually called (if it is called at all), after
384 //! all add_gen and add_kill calls, but before propagate.
386 debug!("{} add_kills_from_flow_exits", self.analysis_name);
387 if self.bits_per_id == 0 {
388 // Skip the surprisingly common degenerate case. (Note
389 // compute_id_range requires self.words_per_id > 0.)
392 cfg.graph.each_edge(|_edge_index, edge| {
393 let flow_exit = edge.source();
394 let (start, end) = self.compute_id_range(flow_exit);
395 let mut orig_kills = self.kills.slice(start, end).to_vec();
397 let mut changed = false;
398 for &node_id in edge.data.exiting_scopes.iter() {
399 let opt_cfg_idx = self.nodeid_to_index.get(&node_id).map(|&i|i);
402 let (start, end) = self.compute_id_range(cfg_idx);
403 let kills = self.kills.slice(start, end);
404 if bitwise(orig_kills.as_mut_slice(), kills, &Union) {
409 debug!("{} add_kills_from_flow_exits flow_exit={} \
410 no cfg_idx for exiting_scope={}",
411 self.analysis_name, flow_exit, node_id);
417 let bits = self.kills.slice_mut(start, end);
418 debug!("{} add_kills_from_flow_exits flow_exit={} bits={} [before]",
419 self.analysis_name, flow_exit, mut_bits_to_string(bits));
420 bits.clone_from_slice(orig_kills.as_slice());
421 debug!("{} add_kills_from_flow_exits flow_exit={} bits={} [after]",
422 self.analysis_name, flow_exit, mut_bits_to_string(bits));
429 impl<'a, 'tcx, O:DataFlowOperator+Clone+'static> DataFlowContext<'a, 'tcx, O> {
430 // ^^^^^^^^^^^^^ only needed for pretty printing
431 pub fn propagate(&mut self, cfg: &cfg::CFG, blk: &ast::Block) {
432 //! Performs the data flow analysis.
434 if self.bits_per_id == 0 {
435 // Optimize the surprisingly common degenerate case.
440 let words_per_id = self.words_per_id;
441 let mut propcx = PropagationContext {
446 let mut temp = Vec::from_elem(words_per_id, 0u);
447 while propcx.changed {
448 propcx.changed = false;
449 propcx.reset(temp.as_mut_slice());
450 propcx.walk_cfg(cfg, temp.as_mut_slice());
454 debug!("Dataflow result for {}:", self.analysis_name);
456 self.pretty_print_to(box io::stderr(), blk).unwrap();
461 fn pretty_print_to(&self, wr: Box<io::Writer+'static>,
462 blk: &ast::Block) -> io::IoResult<()> {
463 let mut ps = pprust::rust_printer_annotated(wr, self);
464 try!(ps.cbox(pprust::indent_unit));
466 try!(ps.print_block(blk));
471 impl<'a, 'b, 'tcx, O:DataFlowOperator> PropagationContext<'a, 'b, 'tcx, O> {
472 fn walk_cfg(&mut self,
474 in_out: &mut [uint]) {
475 debug!("DataFlowContext::walk_cfg(in_out={}) {}",
476 bits_to_string(in_out), self.dfcx.analysis_name);
477 assert!(self.dfcx.bits_per_id > 0);
479 cfg.graph.each_node(|node_index, node| {
480 debug!("DataFlowContext::walk_cfg idx={} id={} begin in_out={}",
481 node_index, node.data.id, bits_to_string(in_out));
483 let (start, end) = self.dfcx.compute_id_range(node_index);
485 // Initialize local bitvector with state on-entry.
486 in_out.clone_from_slice(self.dfcx.on_entry.slice(start, end));
488 // Compute state on-exit by applying transfer function to
490 self.dfcx.apply_gen_kill(node_index, in_out);
492 // Propagate state on-exit from node into its successors.
493 self.propagate_bits_into_graph_successors_of(in_out, cfg, node_index);
494 true // continue to next node
498 fn reset(&mut self, bits: &mut [uint]) {
499 let e = if self.dfcx.oper.initial_value() {uint::MAX} else {0};
500 for b in bits.iter_mut() {
505 fn propagate_bits_into_graph_successors_of(&mut self,
509 cfg.graph.each_outgoing_edge(cfgidx, |_e_idx, edge| {
510 self.propagate_bits_into_entry_set_for(pred_bits, edge);
515 fn propagate_bits_into_entry_set_for(&mut self,
517 edge: &cfg::CFGEdge) {
518 let source = edge.source();
519 let cfgidx = edge.target();
520 debug!("{} propagate_bits_into_entry_set_for(pred_bits={}, {} to {})",
521 self.dfcx.analysis_name, bits_to_string(pred_bits), source, cfgidx);
522 assert!(self.dfcx.bits_per_id > 0);
524 let (start, end) = self.dfcx.compute_id_range(cfgidx);
526 // (scoping mutable borrow of self.dfcx.on_entry)
527 let on_entry = self.dfcx.on_entry.slice_mut(start, end);
528 bitwise(on_entry, pred_bits, &self.dfcx.oper)
531 debug!("{} changed entry set for {} to {}",
532 self.dfcx.analysis_name, cfgidx,
533 bits_to_string(self.dfcx.on_entry.slice(start, end)));
539 fn mut_bits_to_string(words: &mut [uint]) -> String {
540 bits_to_string(words)
543 fn bits_to_string(words: &[uint]) -> String {
544 let mut result = String::new();
547 // Note: this is a little endian printout of bytes.
549 for &word in words.iter() {
551 for _ in range(0u, uint::BYTES) {
553 result.push_str(format!("{:02x}", v & 0xFF).as_slice());
563 fn bitwise<Op:BitwiseOperator>(out_vec: &mut [uint],
566 assert_eq!(out_vec.len(), in_vec.len());
567 let mut changed = false;
568 for (out_elt, in_elt) in out_vec.iter_mut().zip(in_vec.iter()) {
569 let old_val = *out_elt;
570 let new_val = op.join(old_val, *in_elt);
572 changed |= old_val != new_val;
577 fn set_bit(words: &mut [uint], bit: uint) -> bool {
578 debug!("set_bit: words={} bit={}",
579 mut_bits_to_string(words), bit_str(bit));
580 let word = bit / uint::BITS;
581 let bit_in_word = bit % uint::BITS;
582 let bit_mask = 1 << bit_in_word;
583 debug!("word={} bit_in_word={} bit_mask={}", word, bit_in_word, word);
584 let oldv = words[word];
585 let newv = oldv | bit_mask;
590 fn bit_str(bit: uint) -> String {
592 let lobits = 1u << (bit & 0xFF);
593 format!("[{}:{}-{:02x}]", bit, byte, lobits)
597 impl BitwiseOperator for Union {
598 fn join(&self, a: uint, b: uint) -> uint { a | b }
601 impl BitwiseOperator for Subtract {
602 fn join(&self, a: uint, b: uint) -> uint { a & !b }