Hello,
here are a bunch of optimisations to add to SimplifyCFG try to convert Switches, when possible and convenient, to selects.
Initially the optimization tries to determine if the switch instruction is just a value select operation. If it is it checks if the the value selection happens between only two values (if more than two values are selected then probably switch table is better).
If the check is successful it tries three approaches to convert Switches into selects.
The first is very simple and just checks if only two cases are present and converts the switch into a simple select.
If there are more than two cases it then tries to check if these cases are grouped in ranges of values that are not overlapped, like in:
switch(a) {
case 0...100: return 10; case 101...200: return 20;
}
if this is the case it converts the switch into a comparison with a pivot + select.
As a third approach it tires to check if there is a bit pattern with which it is possible to distinguish between two possible groups of switch cases like in:
a &= 0x3
switch (a) {
case 0: case 2: return 10; case 1: case 3: return 20;
}
where the mask 0x1 can distinguish between the two groups (if "a" masked with 0x1 is 1 then we are in the second case, otherwise the first).
If this succeeds it substitutes the switch with an "and" + compare with 0 + select.
Please help review :)
Thanks! These names are better. A comment explaining what the components of the pair represent would also be great.