Skip to content

Commit a54d5f7

Browse files
committed
[JSC] B3 foldPathConstants should treat a boolean branch condition as 1 on the taken edge
https://bugs.webkit.org/show_bug.cgi?id=327633 rdar://190084203 Reviewed by Keith Miller. When you have a code like this, x === 32 || x === 9 || .. the graph likes this is generated. BB#0: (root) Int32 @0 = ArgumentReg(x0) // x Int32 @1 = Const32(36) Int32 @2 = Equal(@0, @1) Void @3 = Branch(@2), Successors: Then:#1, Else:#2 BB#1: (firstTaken) preds: #0 Void @4 = Upsilon(@2, ^10) // passes the compare itself into the Phi Void @5 = Jump, Successors: #4 BB#2: (secondCompare) preds: #0 Int32 @6 = Const32(95) Int32 @7 = Equal(@0, @6) Void @8 = Branch(@7), Successors: Then:#3, Else:#6 BB#3: (secondTaken) preds: #2 Void @9 = Upsilon(@7, ^10) Void @11 = Jump, Successors: #4 BB#4: (join) preds: #1, #3 Int32 @10 = Phi // the merged "||" result ... Void @15 = Branch(@10), Successors: Then:#5, Else:#6 BB#5: (matched) ... BB#6: (unmatched) preds: #2, #4 Void @17 = Return($5) In foldPathConstants, we can install 1 for taken path if the Branch's input returns bool (e.g. `===`). Then in the above case, @4 and @9 are replaced with Constant 1, then @10 will be folded into 1 since both are the same constant input, and @15 will become jump since input is always 1. Tests: Source/JavaScriptCore/b3/testb3_1.cpp Source/JavaScriptCore/b3/testb3_5.cpp * Source/JavaScriptCore/b3/B3FoldPathConstants.cpp: * Source/JavaScriptCore/b3/testb3.h: * Source/JavaScriptCore/b3/testb3_1.cpp: (run): * Source/JavaScriptCore/b3/testb3_5.cpp: (testBranchOnPhiOfTakenComparesFoldPathConstant): Canonical link: https://commits.webkit.org/323859@main
1 parent a2e1c23 commit a54d5f7

4 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎Source/JavaScriptCore/b3/B3FoldPathConstants.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ class FoldPathConstants {
9595
continue;
9696
addOverride(
9797
block, branch->child(0),
98-
Override::nonZero(block->successorBlock(0)));
98+
branch->child(0)->returnsBool()
99+
? Override::constant(block->successorBlock(0), 1)
100+
: Override::nonZero(block->successorBlock(0)));
99101
addOverride(
100102
block, branch->child(0),
101103
Override::constant(block->successorBlock(1), 0));

‎Source/JavaScriptCore/b3/testb3.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,7 @@ void testSwitchSameCaseAsDefault();
12221222
void testSwitchChillDiv(unsigned degree, unsigned gap);
12231223
void testSwitchTargettingSameBlock();
12241224
void testSwitchTargettingSameBlockFoldPathConstant();
1225+
void testBranchOnPhiOfTakenComparesFoldPathConstant();
12251226
void testSwitchOnConstant(int64_t key, bool is64Bit);
12261227
void testSwitchSparseI64RangeOverflow();
12271228
void testTruncFold(int64_t value);

‎Source/JavaScriptCore/b3/testb3_1.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,7 @@ void run(const TestConfig* config)
941941

942942
RUN(testSwitchTargettingSameBlock());
943943
RUN(testSwitchTargettingSameBlockFoldPathConstant());
944+
RUN(testBranchOnPhiOfTakenComparesFoldPathConstant());
944945
for (bool is64Bit : { false, true }) {
945946
RUN(testSwitchOnConstant(3, is64Bit));
946947
RUN(testSwitchOnConstant(13, is64Bit));

‎Source/JavaScriptCore/b3/testb3_5.cpp‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,6 +2945,68 @@ void testSwitchTargettingSameBlockFoldPathConstant()
29452945
}
29462946
}
29472947

2948+
void testBranchOnPhiOfTakenComparesFoldPathConstant()
2949+
{
2950+
// Models `if (x === 36 || x === 95)`: each compare's taken edge feeds the compare itself to a Phi
2951+
// that is branched on again.
2952+
Procedure proc;
2953+
BasicBlock* root = proc.addBlock();
2954+
BasicBlock* firstTaken = proc.addBlock();
2955+
BasicBlock* secondCompare = proc.addBlock();
2956+
BasicBlock* secondTaken = proc.addBlock();
2957+
BasicBlock* join = proc.addBlock();
2958+
BasicBlock* matched = proc.addBlock();
2959+
BasicBlock* unmatched = proc.addBlock();
2960+
2961+
auto arguments = cCallArgumentValues<int32_t>(proc, root);
2962+
Value* argument = arguments[0];
2963+
Value* firstEqual = root->appendNew<Value>(proc, Equal, Origin(), argument, root->appendNew<Const32Value>(proc, Origin(), 36));
2964+
root->setSuccessors(FrequentedBlock(firstTaken), FrequentedBlock(secondCompare));
2965+
root->appendNew<Value>(proc, Branch, Origin(), firstEqual);
2966+
2967+
Value* phi = join->appendNew<Value>(proc, Phi, Int32, Origin());
2968+
2969+
UpsilonValue* firstUpsilon = firstTaken->appendNew<UpsilonValue>(proc, Origin(), firstEqual);
2970+
firstUpsilon->setPhi(phi);
2971+
firstTaken->setSuccessors(FrequentedBlock(join));
2972+
firstTaken->appendNew<Value>(proc, Jump, Origin());
2973+
2974+
Value* secondEqual = secondCompare->appendNew<Value>(proc, Equal, Origin(), argument, secondCompare->appendNew<Const32Value>(proc, Origin(), 95));
2975+
secondCompare->setSuccessors(FrequentedBlock(secondTaken), FrequentedBlock(unmatched));
2976+
secondCompare->appendNew<Value>(proc, Branch, Origin(), secondEqual);
2977+
2978+
UpsilonValue* secondUpsilon = secondTaken->appendNew<UpsilonValue>(proc, Origin(), secondEqual);
2979+
secondUpsilon->setPhi(phi);
2980+
secondTaken->setSuccessors(FrequentedBlock(join));
2981+
secondTaken->appendNew<Value>(proc, Jump, Origin());
2982+
2983+
// Large enough that duplicateTails does not copy the join into its predecessors.
2984+
Value* incremented = join->appendNew<Value>(proc, Add, Origin(), argument, join->appendNew<Const32Value>(proc, Origin(), 1));
2985+
Value* squared = join->appendNew<Value>(proc, Mul, Origin(), incremented, incremented);
2986+
Value* result = join->appendNew<Value>(proc, Add, Origin(), squared, join->appendNew<Const32Value>(proc, Origin(), 7));
2987+
join->setSuccessors(FrequentedBlock(matched), FrequentedBlock(unmatched));
2988+
join->appendNew<Value>(proc, Branch, Origin(), phi);
2989+
2990+
matched->clearSuccessors();
2991+
matched->appendNew<Value>(proc, Return, Origin(), result);
2992+
unmatched->clearSuccessors();
2993+
unmatched->appendNew<Value>(proc, Return, Origin(), unmatched->appendNew<Const32Value>(proc, Origin(), 5));
2994+
2995+
auto code = compileProc(proc);
2996+
2997+
if (proc.optLevel() >= 2) {
2998+
if (isARM64())
2999+
checkDoesNotUseInstruction(*code, "cset");
3000+
if (isX86())
3001+
checkDoesNotUseInstruction(*code, "sete");
3002+
}
3003+
3004+
for (int32_t i : { 0, 35, 36, 37, 94, 95, 96, -36 }) {
3005+
int32_t expected = (i == 36 || i == 95) ? (i + 1) * (i + 1) + 7 : 5;
3006+
CHECK_EQ(invoke<int32_t>(*code, i), expected);
3007+
}
3008+
}
3009+
29483010
void testSwitchOnConstant(int64_t key, bool is64Bit)
29493011
{
29503012
Procedure proc;

0 commit comments

Comments
 (0)