Skip to content

Commit 8a4e67a

Browse files
rayw000danielleadams
authored andcommitted
deps: V8: cherry-pick 7ae0b77628f6
Original commit message: [interpreter] Stop jump-table optimizing switch stms when spread overflows Bug: v8:12389 Change-Id: I53c728ab0c8ba38c7dd96c7e1089f771ba44b9f0 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3289227 Reviewed-by: Leszek Swirski <[email protected]> Commit-Queue: Leszek Swirski <[email protected]> Cr-Commit-Position: refs/heads/main@{#77995} Refs: v8/v8@7ae0b77 PR-URL: #40882 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 97444a9 commit 8a4e67a

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

deps/v8/src/interpreter/bytecode-generator.cc

+22-11
Original file line numberDiff line numberDiff line change
@@ -1888,17 +1888,28 @@ bool IsSwitchOptimizable(SwitchStatement* stmt, SwitchInfo* info) {
18881888
}
18891889

18901890
// GCC also jump-table optimizes switch statements with 6 cases or more.
1891-
if (!(static_cast<int>(info->covered_cases.size()) >=
1892-
FLAG_switch_table_min_cases &&
1893-
IsSpreadAcceptable(info->MaxCase() - info->MinCase(),
1894-
cases->length()))) {
1895-
// Invariant- covered_cases has all cases and only cases that will go in the
1896-
// jump table.
1897-
info->covered_cases.clear();
1898-
return false;
1899-
} else {
1900-
return true;
1901-
}
1891+
if (static_cast<int>(info->covered_cases.size()) >=
1892+
FLAG_switch_table_min_cases) {
1893+
// Due to case spread will be used as the size of jump-table,
1894+
// we need to check if it doesn't overflow by casting its
1895+
// min and max bounds to int64_t, and calculate if the difference is less
1896+
// than or equal to INT_MAX.
1897+
int64_t min = static_cast<int64_t>(info->MinCase());
1898+
int64_t max = static_cast<int64_t>(info->MaxCase());
1899+
int64_t spread = max - min + 1;
1900+
1901+
DCHECK_GT(spread, 0);
1902+
1903+
// Check if casted spread is acceptable and doesn't overflow.
1904+
if (spread <= INT_MAX &&
1905+
IsSpreadAcceptable(static_cast<int>(spread), cases->length())) {
1906+
return true;
1907+
}
1908+
}
1909+
// Invariant- covered_cases has all cases and only cases that will go in the
1910+
// jump table.
1911+
info->covered_cases.clear();
1912+
return false;
19021913
}
19031914

19041915
} // namespace

0 commit comments

Comments
 (0)