Skip to content

Commit

Permalink
Change Direction::{is_forward,is_backward} functions into constants
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tmiasko committed Jun 7, 2022
1 parent 7fe2c4b commit 39de03d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
8 changes: 4 additions & 4 deletions compiler/rustc_mir_dataflow/src/framework/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}

Expand All @@ -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),
Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_mir_dataflow/src/framework/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_dataflow/src/framework/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -200,7 +200,7 @@ where
let mut dirty_queue: WorkQueue<BasicBlock> =
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);
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_dataflow/src/framework/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_dataflow/src/framework/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<D: Direction> 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())
Expand All @@ -153,7 +153,7 @@ impl<D: Direction> 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();
Expand Down

0 comments on commit 39de03d

Please sign in to comment.