<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61366>61366</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            SimplifyCFG could emit comparisons instead of tables in simple cases
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          scottmcm
      </td>
    </tr>
</table>

<pre>
    I was experimenting with various forms of this to see what's best, so I tried the "just do it the obvious way":
```cpp
#include <stdint.h>
bool is_ge(int8_t c) {
 switch (c) {
        case -1: return false;
        case 0: return true;
        case 1: return true;
    }

 __builtin_unreachable();
}
```
But that comes out with surprisingly-complicated codegen: <https://cpp.godbolt.org/z/bfYb91hdj>
```nasm
is_ge(signed char): # @is_ge(signed char)
        inc     dil
 movzx   eax, dil
        and     eax, 7
        mov     ecx, 6
 bt      ecx, eax
        setb    al
        ret
```

This particular case could of course just be `x ULT 2`, but even the "make a table and shift" could be better.  For example, I think it could avoid the range shifting by using [something like `((0x3 >> (c&3)) & 1) == 0`](https://alive2.llvm.org/ce/z/Xbwws2), for smaller constants and fewer instructions
```nasm
is_ge_alt(unsigned char): # @is_ge_alt(unsigned char)
        and     edi, 3
        mov     eax, 3
        bt      eax, edi
        setb    al
        ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVU1vIjkQ_TXmUgpqbKDhwCGBYTXS3nZW2j0h211Ne-K2W3YZyPz6ld2dZBIlOWwriqFeVXV9vGdkjObsEHds9cBWh5lM1Pmwi9oT9bqfKd887b7DVUbA24DB9OjIuDNcDXVwkcH4FKH1oY_gW6DORCAPERGunSTG6wgKIzG-h-jhO1Aw2AB1CIzznykSNB4MFYtXl5LuKp8Y50zcs-rAqnu2rsY_PQyThQvjtE0NAhP7SI1xNO-Y-DbCynsLJp7OyPjGONqcCDTjW2D1w-gB8WpId8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