-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Open
Labels
Description
The compare instruction count should be less than the branch count when default branch is undefined: https://llvm.godbolt.org/z/x6ETdfY79.
Related code:
llvm-project/llvm/lib/Analysis/InlineCost.cpp
Lines 524 to 541 in fd3e7e3
// Considering forming a binary search, we should find the number of nodes | |
// which is same as the number of comparisons when lowered. For a given | |
// number of clusters, n, we can define a recursive function, f(n), to find | |
// the number of nodes in the tree. The recursion is : | |
// f(n) = 1 + f(n/2) + f (n - n/2), when n > 3, | |
// and f(n) = n, when n <= 3. | |
// This will lead a binary tree where the leaf should be either f(2) or f(3) | |
// when n > 3. So, the number of comparisons from leaves should be n, while | |
// the number of non-leaf should be : | |
// 2^(log2(n) - 1) - 1 | |
// = 2^log2(n) * 2^-1 - 1 | |
// = n / 2 - 1. | |
// Considering comparisons from leaf and non-leaf nodes, we can estimate the | |
// number of comparisons in a simple closed form : | |
// n + n / 2 - 1 = n * 3 / 2 - 1 | |
int64_t getExpectedNumberOfCompare(int NumCaseCluster) { | |
return 3 * static_cast<int64_t>(NumCaseCluster) / 2 - 1; | |
} |
Perhaps we can do these things:
- Reduce one comparison for the default branch that is undefined.
- The number of comparison instructions can be linear or even fewer.