use super::CseCtx;
use crate::ir_inner::model::program::Program;
#[must_use]
#[inline]
#[expect(
clippy::needless_pass_by_value,
reason = "CSE consumes Program to reuse its allocation-preserving with_rewritten_entry path"
)]
pub fn cse_into(program: Program, ctx: &mut CseCtx) -> Program {
ctx.clear();
program.with_rewritten_entry(ctx.nodes(program.entry()))
}
thread_local! {
static CSE_CTX: std::cell::RefCell<CseCtx> = std::cell::RefCell::new(CseCtx::default());
}
#[must_use]
#[inline]
pub fn cse(program: Program) -> Program {
CSE_CTX.with(|cell| cse_into(program, &mut cell.borrow_mut()))
}