From 34ec4b4e44b9fdc22c28d7485a6b5e6e3cb93556 Mon Sep 17 00:00:00 2001 From: Trevor Elliott Date: Wed, 8 Feb 2023 15:42:24 -0800 Subject: [PATCH] Reuse `inst_predicates::visit_block_succs` in more places (#5750) Following up from #5730, replace some explicit matching over branch instructions with a use of inst_predicates::visit_block_succs. --- cranelift/codegen/src/dominator_tree.rs | 29 +++------------------ cranelift/codegen/src/flowgraph.rs | 34 ++++--------------------- 2 files changed, 8 insertions(+), 55 deletions(-) diff --git a/cranelift/codegen/src/dominator_tree.rs b/cranelift/codegen/src/dominator_tree.rs index 06594aaa98..23ccb1783a 100644 --- a/cranelift/codegen/src/dominator_tree.rs +++ b/cranelift/codegen/src/dominator_tree.rs @@ -2,7 +2,8 @@ use crate::entity::SecondaryMap; use crate::flowgraph::{BlockPredecessor, ControlFlowGraph}; -use crate::ir::{self, Block, ExpandedProgramPoint, Function, Inst, Layout, ProgramOrder, Value}; +use crate::inst_predicates; +use crate::ir::{Block, ExpandedProgramPoint, Function, Inst, Layout, ProgramOrder, Value}; use crate::packed_option::PackedOption; use crate::timing; use alloc::vec::Vec; @@ -350,31 +351,7 @@ impl DominatorTree { /// post-order. Split-invariant means that if a block is split in two, we get the same /// post-order except for the insertion of the new block header at the split point. fn push_successors(&mut self, func: &Function, block: Block) { - if let Some(inst) = func.layout.last_inst(block) { - match &func.dfg.insts[inst] { - ir::InstructionData::Jump { - destination: succ, .. - } => self.push_if_unseen(succ.block(&func.dfg.value_lists)), - ir::InstructionData::Brif { - blocks: [block_then, block_else], - .. - } => { - self.push_if_unseen(block_then.block(&func.dfg.value_lists)); - self.push_if_unseen(block_else.block(&func.dfg.value_lists)); - } - ir::InstructionData::BranchTable { - table: jt, - destination: dest, - .. - } => { - for succ in func.stencil.dfg.jump_tables[*jt].iter() { - self.push_if_unseen(*succ); - } - self.push_if_unseen(*dest); - } - inst => debug_assert!(!inst.opcode().is_branch()), - } - } + inst_predicates::visit_block_succs(func, block, |_, succ, _| self.push_if_unseen(succ)) } /// Push `block` onto `self.stack` if it has not already been seen. diff --git a/cranelift/codegen/src/flowgraph.rs b/cranelift/codegen/src/flowgraph.rs index 8419228006..fa62d3caeb 100644 --- a/cranelift/codegen/src/flowgraph.rs +++ b/cranelift/codegen/src/flowgraph.rs @@ -22,7 +22,8 @@ use crate::bforest; use crate::entity::SecondaryMap; -use crate::ir::{self, Block, Function, Inst}; +use crate::inst_predicates; +use crate::ir::{Block, Function, Inst}; use crate::timing; use core::mem; @@ -116,34 +117,9 @@ impl ControlFlowGraph { } fn compute_block(&mut self, func: &Function, block: Block) { - if let Some(inst) = func.layout.last_inst(block) { - match &func.dfg.insts[inst] { - ir::InstructionData::Jump { - destination: dest, .. - } => { - self.add_edge(block, inst, dest.block(&func.dfg.value_lists)); - } - ir::InstructionData::Brif { - blocks: [block_then, block_else], - .. - } => { - self.add_edge(block, inst, block_then.block(&func.dfg.value_lists)); - self.add_edge(block, inst, block_else.block(&func.dfg.value_lists)); - } - ir::InstructionData::BranchTable { - table: jt, - destination: dest, - .. - } => { - self.add_edge(block, inst, *dest); - - for dest in func.stencil.dfg.jump_tables[*jt].iter() { - self.add_edge(block, inst, *dest); - } - } - inst => debug_assert!(!inst.opcode().is_branch()), - } - } + inst_predicates::visit_block_succs(func, block, |inst, dest, _| { + self.add_edge(block, inst, dest); + }); } fn invalidate_block_successors(&mut self, block: Block) {