From c2622b52536c5206a396de589de0961967df6956 Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Thu, 11 Apr 2024 10:37:56 -0400 Subject: [PATCH] YJIT: x64: Remove register shuffle with `opt_and` and friends (#10498) This is best understood by looking at the change to the output: ```diff # Insn: 0002 opt_and (stack_size: 2) - mov rax, rsi - and rax, rdi - mov rsi, rax + and rsi, rdi ``` It's a bit awkward to match against due to how stack operands are lowered, but hey, it's nice to save the 2 unnecessary MOVs. --- yjit/src/backend/x86_64/mod.rs | 17 +++++++++++++++++ yjit/src/codegen.rs | 6 +++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/yjit/src/backend/x86_64/mod.rs b/yjit/src/backend/x86_64/mod.rs index 19e604a2f8..d52ed265bd 100644 --- a/yjit/src/backend/x86_64/mod.rs +++ b/yjit/src/backend/x86_64/mod.rs @@ -181,6 +181,23 @@ impl Assembler iterator.map_insn_index(&mut asm); iterator.next_unmapped(); // Pop merged Insn::Mov } + (Opnd::Reg(_), Opnd::Reg(_), Some(Insn::Mov { dest, src })) + if out == src && live_ranges[index] == index + 1 && { + // We want to do `dest == left`, but `left` has already gone + // through lower_stack_opnd() while `dest` has not. So we + // lower `dest` before comparing. + let lowered_dest = if let Opnd::Stack { .. } = dest { + asm.lower_stack_opnd(dest) + } else { + *dest + }; + lowered_dest == *left + } => { + *out = *dest; + asm.push_insn(insn); + iterator.map_insn_index(&mut asm); + iterator.next_unmapped(); // Pop merged Insn::Mov + } _ => { match (unmapped_opnds[0], unmapped_opnds[1]) { (Opnd::Mem(_), Opnd::Mem(_)) => { diff --git a/yjit/src/codegen.rs b/yjit/src/codegen.rs index be719023c0..d60f4f0ec1 100644 --- a/yjit/src/codegen.rs +++ b/yjit/src/codegen.rs @@ -3827,7 +3827,7 @@ fn gen_opt_and( // Push the output on the stack let dst = asm.stack_push(Type::Fixnum); - asm.store(dst, val); + asm.mov(dst, val); Some(KeepCompiling) } else { @@ -3867,7 +3867,7 @@ fn gen_opt_or( // Push the output on the stack let dst = asm.stack_push(Type::Fixnum); - asm.store(dst, val); + asm.mov(dst, val); Some(KeepCompiling) } else { @@ -3909,7 +3909,7 @@ fn gen_opt_minus( // Push the output on the stack let dst = asm.stack_push(Type::Fixnum); - asm.store(dst, val); + asm.mov(dst, val); Some(KeepCompiling) } else {