|
4 | 4 | //! arise naturally during IR translation when code follows an `unreachable` or |
5 | 5 | //! `return` instruction inside a Wasm structured control flow construct. |
6 | 6 |
|
7 | | -use crate::ir::{BlockId, IrBlock, IrFunction, IrTerminator}; |
| 7 | +use super::utils::terminator_successors; |
| 8 | +use crate::ir::{BlockId, IrBlock, IrFunction}; |
8 | 9 | use anyhow::{bail, Result}; |
9 | 10 | use std::collections::{HashMap, HashSet}; |
10 | 11 |
|
11 | | -/// Returns the successor block IDs for a terminator. |
12 | | -fn terminator_successors(term: &IrTerminator) -> Vec<BlockId> { |
13 | | - match term { |
14 | | - IrTerminator::Return { .. } | IrTerminator::Unreachable => vec![], |
15 | | - IrTerminator::Jump { target } => vec![*target], |
16 | | - IrTerminator::BranchIf { |
17 | | - if_true, if_false, .. |
18 | | - } => vec![*if_true, *if_false], |
19 | | - IrTerminator::BranchTable { |
20 | | - targets, default, .. |
21 | | - } => targets |
22 | | - .iter() |
23 | | - .chain(std::iter::once(default)) |
24 | | - .copied() |
25 | | - .collect(), |
26 | | - } |
27 | | -} |
28 | | - |
29 | 12 | /// Computes the set of block IDs reachable from the entry block via BFS. |
30 | 13 | fn reachable_blocks(func: &IrFunction) -> Result<HashSet<BlockId>> { |
31 | 14 | // Index blocks by ID for O(1) lookup during traversal. |
@@ -61,7 +44,7 @@ pub fn eliminate(func: &mut IrFunction) -> Result<()> { |
61 | 44 | #[cfg(test)] |
62 | 45 | mod tests { |
63 | 46 | use super::*; |
64 | | - use crate::ir::{IrInstr, IrValue, TypeIdx, VarId, WasmType}; |
| 47 | + use crate::ir::{IrInstr, IrTerminator, IrValue, TypeIdx, VarId, WasmType}; |
65 | 48 |
|
66 | 49 | /// Build a minimal `IrFunction` with the given blocks. |
67 | 50 | /// Entry block is always `BlockId(0)`. |
|
0 commit comments