[PATCH] D31085: [InlineCost] Increase the cost of Switch

Hans Wennborg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 30 05:15:28 PDT 2017


hans added a comment.

In https://reviews.llvm.org/D31085#713391, @junbuml wrote:

> The below comment by Hans is copied from  https://reviews.llvm.org/D31080 :
>
> > Before we get any further, I also would like to ask if you have done any measurements of compile-time with this set of patches. As I said before, I think this be quite an expensive hook to call for the inline cost analysis, and it would be nice to see some numbers. If it turns out that it is expensive, perhaps we could come up with some better inline cost heuristic, perhaps something based on the density of the switch.
>
> I think there are three difference cost heuristics :
>
> 1. The cheapest yet inaccurate version is https://reviews.llvm.org/D29870 in which the number of case is simply used as number of cluster.
> 2. The most accurate yet expensive version is to use a hook from https://reviews.llvm.org/D31080.
> 3. I guess what Hans suggested in above comment (something based on the density of the switch) must be somewhere in between #1 and #2.
>
>   Both #1 and #2 consider forming a BTree, and don't require to update the cost heuristic for the changes in switch lowering. I'm not clear about #3 in terms of the accuracy, compile-time, and maintenance. Hans, can you give me little bit more detail?
>
>   For me, at least #1 is still better than the current heuristic which simply count the number of distinct successor blocks. If then, would it make sense to use #1 by default and add difference levels of cost heuristics with flags so that we can come up with the most reasonable heuristic and allow others  to do experiments ?


Yes, counting the number of successor blocks doesn't seem right.

I think a decent heuristic might be:

1. Ask TTI about the native word width, and check if this switch is trivially lowered with a bit test (two successors, case range fits in a machine word); this is very cheap
2. Ask TTI about jump table density conditions; if the whole switch is dense enough, assume it's a jump table and compute a cost based on that
3. Otherwise assume it's a balanced tree and estimate the cost based on number of cases (it would be nice not to actually have to build the tree, but just compute an estimate cost using some formula)

This means the estimate will be off for switches that are lowered with a mix of jump tables, binary trees and bit-tests, but those aren't that common, and not being completely accurate is probably fine.

I think going with only #1 might not be so good because we'll overestimate the cost of many switches. But I think computing 1-3 would make for a good a cheap heuristic.


https://reviews.llvm.org/D31085





More information about the llvm-commits mailing list