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)
commit6ed81cbcdb6e5f07f6ac71a446d970c2db8945a5
tree9aeb61d8c38c47e8c4ee9c0313f6572ceff2f284
parent056cbfe58d99fb8224279a948d2fd6eb41a3e5c2
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.

llvm-svn: 240224
llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h
llvm/test/CodeGen/X86/switch.ll