Skip to content

Commit bd50166

Browse files
committed
[JSC] Fuse null tests into backward conditional jumps
https://bugs.webkit.org/show_bug.cgi?id=328623 rdar://190806461 Reviewed by Dan Hecht. There is no reason not to fuse backward conditional jumps with null tests. This patch fixes it. Test: JSTests/stress/loop-latch-null-test-fused-into-jump.js * JSTests/stress/loop-latch-negated-compare-fused-into-jump.js: Added. (shouldBe): (untilLess): (untilLessEq): (untilGreater): (untilGreaterEq): (untilEq): (untilStrictEq): (untilNotEq): (untilNotStrictEq): (untilBelow): (untilBelowEq): * JSTests/stress/loop-latch-null-test-fused-into-jump.js: Added. (shouldBe): (countUntilNull): (countWhileNull): (countNodes): (countUntilNullNegated): (countWhileNullNegated): (countNodesNegated): * Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp: (JSC::BytecodeGenerator::emitJumpIfTrue): (JSC::BytecodeGenerator::emitJumpIfFalse): Canonical link: https://commits.webkit.org/324588@main
1 parent 7d2854a commit bd50166

3 files changed

Lines changed: 214 additions & 16 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error("bad value: " + actual + " expected: " + expected);
4+
}
5+
6+
function untilLess(values, bound) {
7+
let index = 0;
8+
let value;
9+
do {
10+
value = values[index++];
11+
} while (!(value < bound));
12+
return index;
13+
}
14+
15+
function untilLessEq(values, bound) {
16+
let index = 0;
17+
let value;
18+
do {
19+
value = values[index++];
20+
} while (!(value <= bound));
21+
return index;
22+
}
23+
24+
function untilGreater(values, bound) {
25+
let index = 0;
26+
let value;
27+
do {
28+
value = values[index++];
29+
} while (!(value > bound));
30+
return index;
31+
}
32+
33+
function untilGreaterEq(values, bound) {
34+
let index = 0;
35+
let value;
36+
do {
37+
value = values[index++];
38+
} while (!(value >= bound));
39+
return index;
40+
}
41+
42+
function untilEq(values, bound) {
43+
let index = 0;
44+
let value;
45+
do {
46+
value = values[index++];
47+
} while (!(value == bound));
48+
return index;
49+
}
50+
51+
function untilStrictEq(values, bound) {
52+
let index = 0;
53+
let value;
54+
do {
55+
value = values[index++];
56+
} while (!(value === bound));
57+
return index;
58+
}
59+
60+
function untilNotEq(values, bound) {
61+
let index = 0;
62+
let value;
63+
do {
64+
value = values[index++];
65+
} while (!(value != bound));
66+
return index;
67+
}
68+
69+
function untilNotStrictEq(values, bound) {
70+
let index = 0;
71+
let value;
72+
do {
73+
value = values[index++];
74+
} while (!(value !== bound));
75+
return index;
76+
}
77+
78+
function untilBelow(values, bound) {
79+
let index = 0;
80+
let value;
81+
do {
82+
value = values[index++];
83+
} while (!((value >>> 0) < (bound >>> 0)));
84+
return index;
85+
}
86+
87+
function untilBelowEq(values, bound) {
88+
let index = 0;
89+
let value;
90+
do {
91+
value = values[index++];
92+
} while (!((value >>> 0) <= (bound >>> 0)));
93+
return index;
94+
}
95+
96+
for (let i = 0; i < testLoopCount; ++i) {
97+
shouldBe(untilLess([5, NaN, 3, 2], 3), 4);
98+
shouldBe(untilLess([5.5, 2.5], 3.5), 2);
99+
shouldBe(untilLessEq([5, NaN, 4, 3], 3), 4);
100+
shouldBe(untilGreater([1, NaN, 3, 4], 3), 4);
101+
shouldBe(untilGreaterEq([1, NaN, 2, 3], 3), 4);
102+
shouldBe(untilEq([1, "2", NaN, "3"], 3), 4);
103+
shouldBe(untilStrictEq(["3", NaN, 2, 3], 3), 4);
104+
shouldBe(untilNotEq([3, "3", 3, 4], 3), 4);
105+
shouldBe(untilNotStrictEq([3, 3, 3, "3"], 3), 4);
106+
shouldBe(untilBelow([-1, 5, 3, 2], 3), 4);
107+
shouldBe(untilBelowEq([-1, 5, 4, 3], 3), 4);
108+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error("bad value: " + actual + " expected: " + expected);
4+
}
5+
6+
function countUntilNull(list) {
7+
let count = 0;
8+
let node = list;
9+
while (node != null) {
10+
++count;
11+
node = node.next;
12+
}
13+
return count;
14+
}
15+
16+
function countWhileNull(values) {
17+
let count = 0;
18+
let index = 0;
19+
let value;
20+
do {
21+
value = values[index++];
22+
++count;
23+
} while (value == null);
24+
return count;
25+
}
26+
27+
function countNodes(list) {
28+
let count = 0;
29+
let node = list;
30+
do {
31+
++count;
32+
node = node.next;
33+
} while (node != null);
34+
return count;
35+
}
36+
37+
function countUntilNullNegated(list) {
38+
let count = 0;
39+
let node = list;
40+
while (!(node == null)) {
41+
++count;
42+
node = node.next;
43+
}
44+
return count;
45+
}
46+
47+
function countWhileNullNegated(values) {
48+
let count = 0;
49+
let index = 0;
50+
let value;
51+
do {
52+
value = values[index++];
53+
++count;
54+
} while (!(value != null));
55+
return count;
56+
}
57+
58+
function countNodesNegated(list) {
59+
let count = 0;
60+
let node = list;
61+
do {
62+
++count;
63+
node = node.next;
64+
} while (!(node == null));
65+
return count;
66+
}
67+
68+
let masquerader = makeMasquerader();
69+
let list = { next: { next: { next: null } } };
70+
let undefinedTerminated = { next: { next: { } } };
71+
let masqueraderTerminated = { next: { next: masquerader } };
72+
73+
for (let i = 0; i < testLoopCount; ++i) {
74+
shouldBe(countUntilNull(list), 3);
75+
shouldBe(countUntilNull(undefinedTerminated), 3);
76+
shouldBe(countUntilNull(masquerader), 0);
77+
shouldBe(countWhileNull([null, undefined, masquerader, 0]), 4);
78+
shouldBe(countWhileNull([1]), 1);
79+
shouldBe(countNodes(list), 3);
80+
shouldBe(countNodes(undefinedTerminated), 3);
81+
shouldBe(countNodes(masqueraderTerminated), 2);
82+
shouldBe(countUntilNullNegated(list), 3);
83+
shouldBe(countUntilNullNegated(undefinedTerminated), 3);
84+
shouldBe(countUntilNullNegated(masquerader), 0);
85+
shouldBe(countWhileNullNegated([null, undefined, masquerader, 0]), 4);
86+
shouldBe(countWhileNullNegated([1]), 1);
87+
shouldBe(countNodesNegated(list), 3);
88+
shouldBe(countNodesNegated(undefinedTerminated), 3);
89+
shouldBe(countNodesNegated(masqueraderTerminated), 2);
90+
}

‎Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,13 +1564,13 @@ void BytecodeGenerator::emitJumpIfTrue(RegisterID* cond, Label& target)
15641564
} else if (m_lastOpcodeID == op_beloweq) {
15651565
if (fuseCompareAndJump<OpBeloweq, OpJbeloweq>(cond, target))
15661566
return;
1567-
} else if (m_lastOpcodeID == op_eq_null && target.isForward()) {
1567+
} else if (m_lastOpcodeID == op_eq_null) {
15681568
if (fuseTestAndJmp<OpEqNull, OpJeqNull>(cond, target))
15691569
return;
1570-
} else if (m_lastOpcodeID == op_neq_null && target.isForward()) {
1570+
} else if (m_lastOpcodeID == op_neq_null) {
15711571
if (fuseTestAndJmp<OpNeqNull, OpJneqNull>(cond, target))
15721572
return;
1573-
} else if (m_lastOpcodeID == op_is_undefined_or_null && target.isForward()) {
1573+
} else if (m_lastOpcodeID == op_is_undefined_or_null) {
15741574
if (fuseTestAndJmp<OpIsUndefinedOrNull, OpJundefinedOrNull>(cond, target))
15751575
return;
15761576
}
@@ -1582,46 +1582,46 @@ void BytecodeGenerator::emitJumpIfTrue(RegisterID* cond, Label& target)
15821582
void BytecodeGenerator::emitJumpIfFalse(RegisterID* cond, Label& target)
15831583
{
15841584
if (canDoPeepholeOptimization()) {
1585-
if (m_lastOpcodeID == op_less && target.isForward()) {
1585+
if (m_lastOpcodeID == op_less) {
15861586
if (fuseCompareAndJump<OpLess, OpJnless>(cond, target))
15871587
return;
1588-
} else if (m_lastOpcodeID == op_lesseq && target.isForward()) {
1588+
} else if (m_lastOpcodeID == op_lesseq) {
15891589
if (fuseCompareAndJump<OpLesseq, OpJnlesseq>(cond, target))
15901590
return;
1591-
} else if (m_lastOpcodeID == op_greater && target.isForward()) {
1591+
} else if (m_lastOpcodeID == op_greater) {
15921592
if (fuseCompareAndJump<OpGreater, OpJngreater>(cond, target))
15931593
return;
1594-
} else if (m_lastOpcodeID == op_greatereq && target.isForward()) {
1594+
} else if (m_lastOpcodeID == op_greatereq) {
15951595
if (fuseCompareAndJump<OpGreatereq, OpJngreatereq>(cond, target))
15961596
return;
1597-
} else if (m_lastOpcodeID == op_eq && target.isForward()) {
1597+
} else if (m_lastOpcodeID == op_eq) {
15981598
if (fuseCompareAndJump<OpEq, OpJneq>(cond, target))
15991599
return;
1600-
} else if (m_lastOpcodeID == op_stricteq && target.isForward()) {
1600+
} else if (m_lastOpcodeID == op_stricteq) {
16011601
if (fuseCompareAndJump<OpStricteq, OpJnstricteq>(cond, target))
16021602
return;
1603-
} else if (m_lastOpcodeID == op_neq && target.isForward()) {
1603+
} else if (m_lastOpcodeID == op_neq) {
16041604
if (fuseCompareAndJump<OpNeq, OpJeq>(cond, target))
16051605
return;
1606-
} else if (m_lastOpcodeID == op_nstricteq && target.isForward()) {
1606+
} else if (m_lastOpcodeID == op_nstricteq) {
16071607
if (fuseCompareAndJump<OpNstricteq, OpJstricteq>(cond, target))
16081608
return;
1609-
} else if (m_lastOpcodeID == op_below && target.isForward()) {
1609+
} else if (m_lastOpcodeID == op_below) {
16101610
if (fuseCompareAndJump<OpBelow, OpJbeloweq>(cond, target, true))
16111611
return;
1612-
} else if (m_lastOpcodeID == op_beloweq && target.isForward()) {
1612+
} else if (m_lastOpcodeID == op_beloweq) {
16131613
if (fuseCompareAndJump<OpBeloweq, OpJbelow>(cond, target, true))
16141614
return;
16151615
} else if (m_lastOpcodeID == op_not) {
16161616
if (fuseTestAndJmp<OpNot, OpJtrue>(cond, target))
16171617
return;
1618-
} else if (m_lastOpcodeID == op_eq_null && target.isForward()) {
1618+
} else if (m_lastOpcodeID == op_eq_null) {
16191619
if (fuseTestAndJmp<OpEqNull, OpJneqNull>(cond, target))
16201620
return;
1621-
} else if (m_lastOpcodeID == op_neq_null && target.isForward()) {
1621+
} else if (m_lastOpcodeID == op_neq_null) {
16221622
if (fuseTestAndJmp<OpNeqNull, OpJeqNull>(cond, target))
16231623
return;
1624-
} else if (m_lastOpcodeID == op_is_undefined_or_null && target.isForward()) {
1624+
} else if (m_lastOpcodeID == op_is_undefined_or_null) {
16251625
if (fuseTestAndJmp<OpIsUndefinedOrNull, OpJnundefinedOrNull>(cond, target))
16261626
return;
16271627
}

0 commit comments

Comments
 (0)