Skip to content

[Inline] Improving getExpectedNumberOfCompare for scenarios with no default branch. #90929

@dianqk

Description

@dianqk

The compare instruction count should be less than the branch count when default branch is undefined: https://llvm.godbolt.org/z/x6ETdfY79.

Related code:

// 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:

  1. Reduce one comparison for the default branch that is undefined.
  2. The number of comparison instructions can be linear or even fewer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions