Skip to content

Commit ffbe288

Browse files
authored
ZJIT: constant fold bitwise XOR (^) operations (ruby#16225)
1 parent 107699d commit ffbe288

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

zjit/src/hir.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,6 +4550,12 @@ impl Function {
45504550
_ => None,
45514551
})
45524552
}
4553+
Insn::FixnumXor { left, right, .. } => {
4554+
self.fold_fixnum_bop(insn_id, left, right, |l, r| match (l, r) {
4555+
(Some(l), Some(r)) => Some(l ^ r),
4556+
_ => None,
4557+
})
4558+
}
45534559
Insn::FixnumEq { left, right, .. } => {
45544560
self.fold_fixnum_pred(insn_id, left, right, |l, r| match (l, r) {
45554561
(Some(l), Some(r)) => Some(l == r),

zjit/src/hir/opt_tests.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,64 @@ mod hir_opt_tests {
450450
");
451451
}
452452

453+
#[test]
454+
fn test_fold_fixnum_xor() {
455+
eval("
456+
def test
457+
2 ^ 5
458+
end
459+
");
460+
461+
assert_snapshot!(hir_string("test"), @"
462+
fn test@<compiled>:3:
463+
bb1():
464+
EntryPoint interpreter
465+
v1:BasicObject = LoadSelf
466+
Jump bb3(v1)
467+
bb2():
468+
EntryPoint JIT(0)
469+
v4:BasicObject = LoadArg :self@0
470+
Jump bb3(v4)
471+
bb3(v6:BasicObject):
472+
v10:Fixnum[2] = Const Value(2)
473+
v12:Fixnum[5] = Const Value(5)
474+
PatchPoint MethodRedefined(Integer@0x1000, ^@0x1008, cme:0x1010)
475+
v24:Fixnum[7] = Const Value(7)
476+
IncrCounter inline_cfunc_optimized_send_count
477+
CheckInterrupts
478+
Return v24
479+
");
480+
}
481+
482+
#[test]
483+
fn test_fold_fixnum_xor_same_negative_number() {
484+
eval("
485+
def test
486+
123 ^ -123
487+
end
488+
");
489+
490+
assert_snapshot!(hir_string("test"), @"
491+
fn test@<compiled>:3:
492+
bb1():
493+
EntryPoint interpreter
494+
v1:BasicObject = LoadSelf
495+
Jump bb3(v1)
496+
bb2():
497+
EntryPoint JIT(0)
498+
v4:BasicObject = LoadArg :self@0
499+
Jump bb3(v4)
500+
bb3(v6:BasicObject):
501+
v10:Fixnum[123] = Const Value(123)
502+
v12:Fixnum[-123] = Const Value(-123)
503+
PatchPoint MethodRedefined(Integer@0x1000, ^@0x1008, cme:0x1010)
504+
v24:Fixnum[-2] = Const Value(-2)
505+
IncrCounter inline_cfunc_optimized_send_count
506+
CheckInterrupts
507+
Return v24
508+
");
509+
}
510+
453511
#[test]
454512
fn test_fold_fixnum_less() {
455513
eval("

0 commit comments

Comments
 (0)