diff options
Diffstat (limited to 'crates/parser/src')
| -rw-r--r-- | crates/parser/src/optimize/rewrite.rs | 9 | ||||
| -rw-r--r-- | crates/parser/src/visit.rs | 33 |
2 files changed, 19 insertions, 23 deletions
diff --git a/crates/parser/src/optimize/rewrite.rs b/crates/parser/src/optimize/rewrite.rs index 4a64f77..366a094 100644 --- a/crates/parser/src/optimize/rewrite.rs +++ b/crates/parser/src/optimize/rewrite.rs @@ -859,6 +859,9 @@ fn rewrite_local_tee32( replace!(output, *read, 1 => Instruction::SubConstTee32(I32LocalArg { value, local: dst })); } Instruction::LocalGet32(src) if src == dst => replace!(output, *read, 1 => Instruction::LocalGet32(src)), + Instruction::LocalGet32(src) => { + replace!(output, *read, 1 => [Instruction::LocalCopy32(src, dst), Instruction::LocalGet32(dst)]); + } Instruction::BinOpLocalLocal32(op, left, right) => { let replacement = if op == BinOp::IAdd { Instruction::AddLocalLocalTee32(LocalTripleArg { left, right, dst }) @@ -931,6 +934,9 @@ fn rewrite_local_tee64( replace!(output, *read, 1 => Instruction::SubConstTee64(PackedOp::new(dst, packed.index))); } Instruction::LocalGet64(src) if src == dst => replace!(output, *read, 1 => Instruction::LocalGet64(src)), + Instruction::LocalGet64(src) => { + replace!(output, *read, 1 => [Instruction::LocalCopy64(src, dst), Instruction::LocalGet64(dst)]); + } Instruction::BinOpLocalLocal64(op, left, right) => { let index = data.push_operand64(Operand64::<(u16, u16, u16)>::new(left, right, dst))?; replace!(output, *read, 1 => Instruction::BinOpLocalLocalTee64(PackedOp::new(op, index))); @@ -967,6 +973,9 @@ fn rewrite_local_tee128( if *read > output.block_start { match output[*read - 1] { Instruction::LocalGet128(src) if src == dst => replace!(output, *read, 1 => Instruction::LocalGet128(src)), + Instruction::LocalGet128(src) => { + replace!(output, *read, 1 => [Instruction::LocalCopy128(src, dst), Instruction::LocalGet128(dst)]); + } Instruction::BinOpLocalLocal128(op, left, right) => { let index = data.push_operand64(Operand64::<(u16, u16, u16)>::new(left, right, dst))?; replace!(output, *read, 1 => Instruction::BinOpLocalLocalTee128(PackedOp::new(op, index))); diff --git a/crates/parser/src/visit.rs b/crates/parser/src/visit.rs index 724c1bf..7daef52 100644 --- a/crates/parser/src/visit.rs +++ b/crates/parser/src/visit.rs @@ -735,29 +735,16 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder<'_> { fn visit_local_tee(&mut self, idx: u32) -> Self::Output { let (size, local_idx) = self.local(idx)?; - self.apply_effect(&[size], &[size])?; - let src = match (size, self.instructions.last()) { - (ValueLane::S32, Some(Instruction::LocalGet32(src))) => Some(*src), - (ValueLane::S64, Some(Instruction::LocalGet64(src))) => Some(*src), - (ValueLane::S128, Some(Instruction::LocalGet128(src))) => Some(*src), - _ => None, - }; - if let Some(src) = src { - self.instructions.pop(); - let instructions = match size { - ValueLane::S32 => [Instruction::LocalCopy32(src, local_idx), Instruction::LocalGet32(local_idx)], - ValueLane::S64 => [Instruction::LocalCopy64(src, local_idx), Instruction::LocalGet64(local_idx)], - ValueLane::S128 => [Instruction::LocalCopy128(src, local_idx), Instruction::LocalGet128(local_idx)], - }; - self.instructions.extend(instructions); - } else { - self.instructions.push(size.select( - Instruction::LocalTee32(local_idx), - Instruction::LocalTee64(local_idx), - Instruction::LocalTee128(local_idx), - )); - } - Ok(()) + // No peephole here: this position may be a branch target (block end, + // loop start, if/else join), and fusing with the preceding `local.get` + // would move the label past the tee. The rewriter fuses the same pair + // within a basic block, where no branch can land between them. + let instruction = size.select( + Instruction::LocalTee32(local_idx), + Instruction::LocalTee64(local_idx), + Instruction::LocalTee128(local_idx), + ); + self.emit(&[size], &[size], instruction) } fn visit_block(&mut self, blockty: wasmparser::BlockType) -> Self::Output { |
