Skip to content

Commit 290ba26

Browse files
committed
[JSC] DFG backwards propagation should not request negative zero for the operand of Branch
https://bugs.webkit.org/show_bug.cgi?id=327815 Reviewed by Yusuke Suzuki. Branch only converts its operand to a boolean, so it cannot observe -0, same as LogicalNot (323639@main). This patch applies LogicalNot's rule to Branch. Without it, code like `if (!(x % 1))`, which Ajv generates for `type: "integer"`, keeps exiting for negative integers and ends up with double ArithMod. Tests: JSTests/stress/branch-does-not-observe-negative-zero.js JSTests/stress/branch-on-negative-zero-should-not-recompile.js * JSTests/stress/branch-does-not-observe-negative-zero.js: Added. (shouldBe): (ifMod): (ifNotMod): (ifInt52Mod): (ifMul): (ifDiv): (ifNegate): (ifAdd): (modAfterBranch): (mulAfterBranch): (modAcrossBlocks): * JSTests/stress/branch-on-negative-zero-should-not-recompile.js: Added. (ifMod): (ifNotMod): (ternaryMod): (whileMod): * Source/JavaScriptCore/dfg/DFGBackwardsPropagationPhase.cpp: (JSC::DFG::BackwardsPropagationPhase::propagate): Canonical link: https://commits.webkit.org/323940@main
1 parent 3a082bf commit 290ba26

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function shouldBe(actual, expected, what) {
2+
if (!Object.is(actual, expected))
3+
throw new Error(what + ": bad value " + actual + ", expected " + expected);
4+
}
5+
6+
function ifMod(x, y) { if (x % y) return 1; return 0; }
7+
noInline(ifMod);
8+
9+
function ifNotMod(x, y) { if (!(x % y)) return 1; return 0; }
10+
noInline(ifNotMod);
11+
12+
function ifInt52Mod(x, y) { if ((x * 4294967296) % y) return 1; return 0; }
13+
noInline(ifInt52Mod);
14+
15+
function ifMul(x, y) { return (x * y) ? 1 : 0; }
16+
noInline(ifMul);
17+
18+
function ifDiv(x, y) { if (x / y) return 1; return 0; }
19+
noInline(ifDiv);
20+
21+
function ifNegate(x) { if (-x) return 1; return 0; }
22+
noInline(ifNegate);
23+
24+
function ifAdd(x, y) { if (x + y) return 1; return 0; }
25+
noInline(ifAdd);
26+
27+
function modAfterBranch(x, y) { var r = x % y; if (r) return 1; return r; }
28+
noInline(modAfterBranch);
29+
30+
function mulAfterBranch(x, y) { var r = x * y; if (!r) return r; return 1; }
31+
noInline(mulAfterBranch);
32+
33+
function modAcrossBlocks(x, y, z) { var r = z ? x % y : x; if (r) return 1; return r; }
34+
noInline(modAcrossBlocks);
35+
36+
for (let i = 1; i < testLoopCount; ++i) {
37+
shouldBe(ifMod(-i, i), 0, "if (-i % i)");
38+
shouldBe(ifMod(-i, i + 1), 1, "if (-i % (i + 1))");
39+
shouldBe(ifNotMod(-i, i), 1, "if (!(-i % i))");
40+
shouldBe(ifNotMod(-i, i + 1), 0, "if (!(-i % (i + 1)))");
41+
shouldBe(ifInt52Mod(-i, i), 0, "if ((-i * 2^32) % i)");
42+
shouldBe(ifInt52Mod(-3, 4294967297), 1, "if ((-3 * 2^32) % (2^32 + 1))");
43+
shouldBe(ifMul(i, 0), 0, "if (i * 0)");
44+
shouldBe(ifMul(-i, 1), 1, "if (-i * 1)");
45+
shouldBe(ifDiv(0, i), 0, "if (0 / i)");
46+
shouldBe(ifDiv(-i, i), 1, "if (-i / i)");
47+
shouldBe(ifNegate(i), 1, "if (-i)");
48+
shouldBe(ifAdd(-i, i), 0, "if (-i + i)");
49+
shouldBe(ifAdd(i, i), 1, "if (i + i)");
50+
shouldBe(modAfterBranch(i, i), 0, "i % i after a branch");
51+
shouldBe(modAfterBranch(-i, i), -0, "-i % i after a branch");
52+
shouldBe(mulAfterBranch(i, 0), 0, "i * 0 after a branch");
53+
shouldBe(mulAfterBranch(-i, 1), 1, "-i * 1 after a branch");
54+
shouldBe(modAcrossBlocks(i, i, true), 0, "i % i across blocks");
55+
shouldBe(modAcrossBlocks(-i, i, true), -0, "-i % i across blocks");
56+
}
57+
58+
shouldBe(ifMul(-5, 0), 0, "if (-5 * 0)");
59+
shouldBe(ifDiv(0, -5), 0, "if (0 / -5)");
60+
shouldBe(ifNegate(0), 0, "if (-0)");
61+
shouldBe(mulAfterBranch(-5, 0), -0, "-5 * 0 after a branch");
62+
shouldBe(ifMod(5, 0), 0, "if (5 % 0)");
63+
shouldBe(ifNotMod(5, 0), 1, "if (!(5 % 0))");
64+
shouldBe(ifInt52Mod(5, 0), 0, "if ((5 * 2^32) % 0)");
65+
shouldBe(ifMod(-2147483648, -1), 0, "if (-2^31 % -1)");
66+
shouldBe(ifMod(0.5, 1), 1, "if (0.5 % 1)");
67+
shouldBe(ifMul(65536, 65536), 1, "if (2^16 * 2^16)");
68+
shouldBe(ifMul(Infinity, 0), 0, "if (Infinity * 0)");
69+
shouldBe(ifDiv(1, 2), 1, "if (1 / 2)");
70+
shouldBe(ifDiv(1, 0), 1, "if (1 / 0)");
71+
shouldBe(ifDiv(0, 0), 0, "if (0 / 0)");
72+
shouldBe(ifNegate(-2147483648), 1, "if (-(-2^31))");
73+
shouldBe(ifNegate(NaN), 0, "if (-NaN)");
74+
shouldBe(ifAdd(-2147483648, -2147483648), 1, "if (-2^31 + -2^31)");
75+
shouldBe(ifAdd(Infinity, -Infinity), 0, "if (Infinity + -Infinity)");
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ skip if not $jitTests
2+
//@ runDefault("--useConcurrentJIT=0")
3+
4+
function ifMod(x) { if (x % 3) return 1; return 0; }
5+
noInline(ifMod);
6+
7+
function ifNotMod(x) { if (!(x % 3)) return 1; return 0; }
8+
noInline(ifNotMod);
9+
10+
function ternaryMod(x) { return (x % 3) ? 1 : 0; }
11+
noInline(ternaryMod);
12+
13+
function whileMod(x) { while (x % 3) x++; return x; }
14+
noInline(whileMod);
15+
16+
for (let i = -testLoopCount; i < testLoopCount; ++i) {
17+
ifMod(i);
18+
ifNotMod(i);
19+
ternaryMod(i);
20+
whileMod(i);
21+
}
22+
23+
for (let f of [ifMod, ifNotMod, ternaryMod, whileMod]) {
24+
if (numberOfDFGCompiles(f) > 2)
25+
throw new Error(f.name + " was compiled " + numberOfDFGCompiles(f) + " times");
26+
}

‎Source/JavaScriptCore/dfg/DFGBackwardsPropagationPhase.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ class BackwardsPropagationPhase : public Phase {
561561
break;
562562
}
563563

564+
case Branch:
564565
case LogicalNot: {
565566
// The result is a boolean, so how it is used says nothing about how the operand is
566567
// observed. The operand is compared against zero, so truncation is observable but -0 is not.

0 commit comments

Comments
 (0)