Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove_redundant_nops_and_jumps
  • Loading branch information
ShaharNaveh committed Jun 24, 2026
commit fc9bc37e62c802372dfaf87b84cab19c1f252023
32 changes: 16 additions & 16 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2525,7 +2525,7 @@ impl Blocks {
self[block_idx].next = cold_blocks;

if cold_blocks != BlockIdx::NULL {
remove_redundant_nops_and_jumps(self)?;
self.remove_redundant_nops_and_jumps()?;
}
Ok(())
}
Expand Down Expand Up @@ -2951,6 +2951,19 @@ impl Blocks {
}
true
}

fn remove_redundant_nops_and_jumps(&mut self) -> crate::InternalResult<()> {
loop {
// Convergence is guaranteed because the number of redundant jumps and
// nops only decreases.
let removed_nops = self.remove_redundant_nops()?;
let removed_jumps = self.remove_redundant_jumps()?;
if removed_nops + removed_jumps == 0 {
break;
}
}
Ok(())
}
}

impl From<Vec<Block>> for Blocks {
Expand Down Expand Up @@ -3524,7 +3537,7 @@ fn optimize_cfg(
// redundant NOP/jump chains before _PyCfg_OptimizeCodeUnit() prunes
// unused constants.
blocks.remove_unreachable()?;
remove_redundant_nops_and_jumps(blocks)?;
blocks.remove_redundant_nops_and_jumps()?;
#[cfg(debug_assertions)]
assert!(blocks.no_redundant_jumps());
Ok(())
Expand Down Expand Up @@ -5981,19 +5994,6 @@ fn is_conditional_jump_opcode(instr: AnyInstruction) -> bool {
)
}

fn remove_redundant_nops_and_jumps(blocks: &mut Blocks) -> crate::InternalResult<()> {
loop {
// Convergence is guaranteed because the number of redundant jumps and
// nops only decreases.
let removed_nops = blocks.remove_redundant_nops()?;
let removed_jumps = blocks.remove_redundant_jumps()?;
if removed_nops + removed_jumps == 0 {
break;
}
}
Ok(())
}

fn blocks_new_block(blocks: &mut Blocks) -> crate::InternalResult<BlockIdx> {
blocks
.try_reserve(1)
Expand Down Expand Up @@ -6757,7 +6757,7 @@ pub(crate) fn convert_pseudo_ops(blocks: &mut Blocks) -> crate::InternalResult<(
}
// CPython flowgraph.c::convert_pseudo_ops() finishes by calling
// remove_redundant_nops_and_jumps().
remove_redundant_nops_and_jumps(blocks)
blocks.remove_redundant_nops_and_jumps()
}

/// flowgraph.c build_cellfixedoffsets
Expand Down