make set_n pass more generic

This commit is contained in:
nora 2022-04-16 00:03:03 +02:00
parent 46229fd74f
commit 904356eb4e

View file

@ -156,7 +156,7 @@ fn pass_find_set_null(ir: &mut Ir<'_>) {
} }
} }
/// pass that replaces `SetN(0) Add(5)` with `SetN(5)` /// pass that replaces `SetN(n) Add(m)` with `SetN(n + m)`
fn pass_set_n(ir: &mut Ir<'_>) { fn pass_set_n(ir: &mut Ir<'_>) {
let stmts = &mut ir.stmts; let stmts = &mut ir.stmts;
for i in 0..stmts.len() { for i in 0..stmts.len() {
@ -170,11 +170,11 @@ fn pass_set_n(ir: &mut Ir<'_>) {
} }
let a = &stmts[i]; let a = &stmts[i];
if let StmtKind::SetN(0) = a.kind() { if let StmtKind::SetN(before) = a.kind() {
let b = &stmts[i + 1]; let b = &stmts[i + 1];
let new = match b.kind() { let new = match b.kind() {
StmtKind::Add(n) => StmtKind::SetN(*n), StmtKind::Add(n) => StmtKind::SetN(before.wrapping_add(*n)),
StmtKind::Sub(n) => StmtKind::SetN(0u8.wrapping_sub(*n)), StmtKind::Sub(n) => StmtKind::SetN(before.wrapping_sub(*n)),
_ => { _ => {
continue; continue;
} }