[PATCH] D52002: Switch optimization for known maximum switch values

Ayonam Ray via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 18 02:41:39 PDT 2018


ayonam added a comment.

In https://reviews.llvm.org/D52002#1237991, @hans wrote:

> If the default basic block is unreachable, that implies that one of the other cases must always be taken. Otherwise, the default wouldn't be unreachable after all.
>
> For example:
>
>   int func(int x) {
>       switch (x) {
>       case 0: return f();
>       case 1: return g();
>       case 2: return h();
>       case 3: return f();
>       case 4: return h();
>       }
>   }
>
>
> The default is unreachable here because it would "fall off" the end of the function without returning, which is undefined behaviour. So the switch lowering should be able to remove the range check for the jump table.


In the case above, if there were to be more statements beyond the switch block, isn't the control supposed to reach the end of the switch block and continue with the rest of the statements if the value of x doesn't match any of the cases?

Actually, I think, the behaviour in such cases is undefined, isn't it?  And that's what is being exploited when the lowering is redirecting the unreachable default to the most popular destination.  Another approach could have been to directed it to the end of the switch block.


Repository:
  rL LLVM

https://reviews.llvm.org/D52002





More information about the llvm-commits mailing list