[PATCH] Switch to Select optimisations

Marcello Maggioni hayarms at gmail.com
Mon Sep 29 09:20:07 PDT 2014

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 :)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D5525.14170.patch
Type: text/x-patch
Size: 22747 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140929/b07a9233/attachment.bin>

More information about the llvm-commits mailing list