Switch lowering: add heuristic for filling leaf nodes in the weight-balanced binary...
authorHans Wennborg <hans@hanshq.net>
Sat, 20 Jun 2015 17:14:07 +0000 (17:14 +0000)
committerHans Wennborg <hans@hanshq.net>
Sat, 20 Jun 2015 17:14:07 +0000 (17:14 +0000)
commitee16995e3db7dcec7ba656839f89f06af3542c56
tree474e54cf7d86b95fa78aa2772b3ae930fc57e7a6
parent1eabc9fb9dd993b927e3ce8ac2ce18a7afdd7864
Switch lowering: add heuristic for filling leaf nodes in the weight-balanced binary search tree

Sparse switches with profile info are lowered as weight-balanced BSTs. For
example, if the node weights are {1,1,1,1,1,1000}, the right-most node would
end up in a tree by itself, bringing it closer to the top.

However, a leaf in this BST can contain up to 3 cases, and having a single
case in a leaf node as in the example means the tree might become
unnecessarily high.

This patch adds a heauristic to the pivot selection algorithm that moves more
cases into leaf nodes unless that would lower their rank. It still doesn't
yield the optimal tree in every case, but I believe it's conservatibely correct.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240224 91177308-0d34-0410-b5e6-96231b3b80d8
lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h
test/CodeGen/X86/switch.ll