From 39de03d8446f839791ae223446d54b67564a3684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sun, 5 Jun 2022 00:00:00 +0000 Subject: [PATCH] Change `Direction::{is_forward,is_backward}` functions into constants Make it explicit that the analysis direction is constant. This also makes the value immediately available for optimizations. Previously those functions were neither inline nor generic and so their definition was unavailable when using data flow framework from other crates. --- .../rustc_mir_dataflow/src/framework/cursor.rs | 8 ++++---- .../rustc_mir_dataflow/src/framework/direction.rs | 14 ++++---------- .../rustc_mir_dataflow/src/framework/engine.rs | 4 ++-- .../rustc_mir_dataflow/src/framework/graphviz.rs | 8 ++++---- compiler/rustc_mir_dataflow/src/framework/tests.rs | 4 ++-- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_mir_dataflow/src/framework/cursor.rs b/compiler/rustc_mir_dataflow/src/framework/cursor.rs index 2de5a9d6991..f3b5544aa8b 100644 --- a/compiler/rustc_mir_dataflow/src/framework/cursor.rs +++ b/compiler/rustc_mir_dataflow/src/framework/cursor.rs @@ -109,7 +109,7 @@ where /// For backward analyses, this is the state that will be propagated to its /// predecessors (ignoring edge-specific effects). pub fn seek_to_block_start(&mut self, block: BasicBlock) { - if A::Direction::is_forward() { + if A::Direction::IS_FORWARD { self.seek_to_block_entry(block) } else { self.seek_after(Location { block, statement_index: 0 }, Effect::Primary) @@ -123,7 +123,7 @@ where /// For forward analyses, this is the state that will be propagated to its /// successors (ignoring edge-specific effects). pub fn seek_to_block_end(&mut self, block: BasicBlock) { - if A::Direction::is_backward() { + if A::Direction::IS_BACKWARD { self.seek_to_block_entry(block) } else { self.seek_after(self.body.terminator_loc(block), Effect::Primary) @@ -157,7 +157,7 @@ where self.seek_to_block_entry(target.block); } else if let Some(curr_effect) = self.pos.curr_effect_index { let mut ord = curr_effect.statement_index.cmp(&target.statement_index); - if A::Direction::is_backward() { + if A::Direction::IS_BACKWARD { ord = ord.reverse() } @@ -173,7 +173,7 @@ where debug_assert_eq!(target.block, self.pos.block); let block_data = &self.body[target.block]; - let next_effect = if A::Direction::is_forward() { + let next_effect = if A::Direction::IS_FORWARD { #[rustfmt::skip] self.pos.curr_effect_index.map_or_else( || Effect::Before.at_index(0), diff --git a/compiler/rustc_mir_dataflow/src/framework/direction.rs b/compiler/rustc_mir_dataflow/src/framework/direction.rs index 3a492b45849..05a4d7bbf3e 100644 --- a/compiler/rustc_mir_dataflow/src/framework/direction.rs +++ b/compiler/rustc_mir_dataflow/src/framework/direction.rs @@ -9,11 +9,9 @@ use super::{ }; pub trait Direction { - fn is_forward() -> bool; + const IS_FORWARD: bool; - fn is_backward() -> bool { - !Self::is_forward() - } + const IS_BACKWARD: bool = !Self::IS_FORWARD; /// Applies all effects between the given `EffectIndex`s. /// @@ -68,9 +66,7 @@ pub trait Direction { pub struct Backward; impl Direction for Backward { - fn is_forward() -> bool { - false - } + const IS_FORWARD: bool = false; fn apply_effects_in_block<'tcx, A>( analysis: &A, @@ -338,9 +334,7 @@ where pub struct Forward; impl Direction for Forward { - fn is_forward() -> bool { - true - } + const IS_FORWARD: bool = true; fn apply_effects_in_block<'tcx, A>( analysis: &A, diff --git a/compiler/rustc_mir_dataflow/src/framework/engine.rs b/compiler/rustc_mir_dataflow/src/framework/engine.rs index 50efb4c1dc4..20e14a77c1e 100644 --- a/compiler/rustc_mir_dataflow/src/framework/engine.rs +++ b/compiler/rustc_mir_dataflow/src/framework/engine.rs @@ -147,7 +147,7 @@ where let mut entry_sets = IndexVec::from_elem(bottom_value.clone(), body.basic_blocks()); analysis.initialize_start_block(body, &mut entry_sets[mir::START_BLOCK]); - if A::Direction::is_backward() && entry_sets[mir::START_BLOCK] != bottom_value { + if A::Direction::IS_BACKWARD && entry_sets[mir::START_BLOCK] != bottom_value { bug!("`initialize_start_block` is not yet supported for backward dataflow analyses"); } @@ -200,7 +200,7 @@ where let mut dirty_queue: WorkQueue = WorkQueue::with_none(body.basic_blocks().len()); - if A::Direction::is_forward() { + if A::Direction::IS_FORWARD { for (bb, _) in traversal::reverse_postorder(body) { dirty_queue.insert(bb); } diff --git a/compiler/rustc_mir_dataflow/src/framework/graphviz.rs b/compiler/rustc_mir_dataflow/src/framework/graphviz.rs index 3834c232ab6..59a2053ec70 100644 --- a/compiler/rustc_mir_dataflow/src/framework/graphviz.rs +++ b/compiler/rustc_mir_dataflow/src/framework/graphviz.rs @@ -216,7 +216,7 @@ where // Write the full dataflow state immediately after the terminator if it differs from the // state at block entry. self.results.seek_to_block_end(block); - if self.results.get() != &block_start_state || A::Direction::is_backward() { + if self.results.get() != &block_start_state || A::Direction::IS_BACKWARD { let after_terminator_name = match terminator.kind { mir::TerminatorKind::Call { target: Some(_), .. } => "(on unwind)", _ => "(on end)", @@ -390,7 +390,7 @@ where let mut afters = diffs.after.into_iter(); let next_in_dataflow_order = |it: &mut std::vec::IntoIter<_>| { - if A::Direction::is_forward() { it.next().unwrap() } else { it.next_back().unwrap() } + if A::Direction::IS_FORWARD { it.next().unwrap() } else { it.next_back().unwrap() } }; for (i, statement) in body[block].statements.iter().enumerate() { @@ -527,7 +527,7 @@ where _block_data: &mir::BasicBlockData<'tcx>, _block: BasicBlock, ) { - if A::Direction::is_forward() { + if A::Direction::IS_FORWARD { self.prev_state.clone_from(state); } } @@ -538,7 +538,7 @@ where _block_data: &mir::BasicBlockData<'tcx>, _block: BasicBlock, ) { - if A::Direction::is_backward() { + if A::Direction::IS_BACKWARD { self.prev_state.clone_from(state); } } diff --git a/compiler/rustc_mir_dataflow/src/framework/tests.rs b/compiler/rustc_mir_dataflow/src/framework/tests.rs index 74c3b44f425..d9461fd3abd 100644 --- a/compiler/rustc_mir_dataflow/src/framework/tests.rs +++ b/compiler/rustc_mir_dataflow/src/framework/tests.rs @@ -140,7 +140,7 @@ impl MockAnalysis<'_, D> { SeekTarget::After(loc) => Effect::Primary.at_index(loc.statement_index), }; - let mut pos = if D::is_forward() { + let mut pos = if D::IS_FORWARD { Effect::Before.at_index(0) } else { Effect::Before.at_index(self.body[block].statements.len()) @@ -153,7 +153,7 @@ impl MockAnalysis<'_, D> { return ret; } - if D::is_forward() { + if D::IS_FORWARD { pos = pos.next_in_forward_order(); } else { pos = pos.next_in_backward_order();