Skip to content

Commit

Permalink
Optimize BitNot for Integer.
Browse files Browse the repository at this point in the history
  • Loading branch information
sisshiki1969 committed Dec 9, 2024
1 parent d573bc8 commit 394c884
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 97 deletions.
63 changes: 31 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 27 additions & 12 deletions benchmark/integer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ prelude: |
"",
(1..5)
]
i = 12345
benchmark:
to_f: |
4587.to_f
Expand All @@ -27,17 +28,31 @@ benchmark:
a[i].nil?
end
shr: |
12345 >> 12
12345 >> 12
12345 >> 12
12345 >> 12
12345 >> 12
12345 >> 12
i >> 12
i >> 12
i >> 12
i >> 12
i >> 12
i >> 12
shl: |
12345 << 12
12345 << 12
12345 << 12
12345 << 12
12345 << 12
12345 << 12
i << 12
i << 12
i << 12
i << 12
i << 12
i << 12
neg: |
-(i)
-(i)
-(i)
-(i)
-(i)
-(i)
bit_not: |
~i
~i
~i
~i
~i
~i
loop_count: 1000000
4 changes: 4 additions & 0 deletions bin/integer
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

cargo build --release
benchmark-driver -o markdown benchmark/integer.yml --rbenv '3.3.0; 3.3.0 --yjit' -e 'target/release/monoruby' -e 'target/release/monoruby --no-jit' --repeat-count 3
5 changes: 4 additions & 1 deletion monoruby/src/compiler/jitgen/asmir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,13 @@ pub(super) enum AsmInst {
},

Not,
Neg {
FixnumNeg {
reg: GP,
deopt: AsmDeopt,
},
FixnumBitNot {
reg: GP,
},
GenericUnOp {
func: UnaryOpFn,
using_xmm: UsingXmm,
Expand Down
11 changes: 10 additions & 1 deletion monoruby/src/compiler/jitgen/asmir/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ impl Codegen {
AsmInst::Not => {
self.not_rdi_to_rax();
}
AsmInst::Neg { reg, deopt } => {
AsmInst::FixnumNeg { reg, deopt } => {
let deopt = labels[deopt];
let r = reg as u64;
monoasm! { &mut self.jit,
Expand All @@ -384,6 +384,15 @@ impl Codegen {
orq R(r), 1;
}
}
AsmInst::FixnumBitNot { reg } => {
let r = reg as u64;
monoasm! { &mut self.jit,
sarq R(r), 1;
notq R(r);
salq R(r), 1;
orq R(r), 1;
}
}
AsmInst::GenericUnOp { func, using_xmm } => {
self.xmm_save(using_xmm);
self.call_unop(func);
Expand Down
45 changes: 2 additions & 43 deletions monoruby/src/compiler/jitgen/binary_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,47 +71,6 @@ impl BBContext {
}
}

pub(super) fn gen_binop_generic(
&mut self,
ir: &mut AsmIr,
pc: BytecodePtr,
kind: BinOpK,
info: BinOpInfo,
) {
self.fetch_binary(ir, info.mode);
self.generic_binop(ir, pc, kind);
self.rax2acc(ir, info.dst);
}

pub(super) fn gen_cmp_integer(
&mut self,
ir: &mut AsmIr,
pc: BytecodePtr,
kind: CmpKind,
mode: OpMode,
dst: Option<SlotId>,
) {
self.fetch_fixnum_binary(ir, pc, &mode);
ir.push(AsmInst::IntegerCmp { kind, mode });
self.rax2acc(ir, dst);
}

pub(super) fn gen_cmpbr_integer(
&mut self,
ir: &mut AsmIr,
pc: BytecodePtr,
kind: CmpKind,
mode: OpMode,
dst: Option<SlotId>,
brkind: BrKind,
branch_dest: JitLabel,
) {
self.fetch_fixnum_binary(ir, pc, &mode);
self.unlink(ir, dst);
self.clear(ir);
ir.integer_cmp_br(mode, kind, brkind, branch_dest);
}

pub(super) fn gen_cmp_generic(
&mut self,
ir: &mut AsmIr,
Expand Down Expand Up @@ -144,7 +103,7 @@ impl BBContext {
});
}

fn fetch_fixnum_binary(&mut self, ir: &mut AsmIr, pc: BytecodePtr, mode: &OpMode) {
pub(super) fn fetch_fixnum_binary(&mut self, ir: &mut AsmIr, pc: BytecodePtr, mode: &OpMode) {
let deopt = ir.new_deopt(self, pc);
match mode {
OpMode::RR(l, r) => {
Expand Down Expand Up @@ -218,7 +177,7 @@ impl BBContext {
/// - caller save registers
/// - stack
///
fn generic_binop(&self, ir: &mut AsmIr, pc: BytecodePtr, kind: BinOpK) {
pub(super) fn generic_binop(&self, ir: &mut AsmIr, pc: BytecodePtr, kind: BinOpK) {
let using_xmm = self.get_using_xmm();
let error = ir.new_error(self, pc);
ir.push(AsmInst::GenericBinOp { kind, using_xmm });
Expand Down
Loading

0 comments on commit 394c884

Please sign in to comment.