Switch containing holes via table lookup

Hans Wennborg hans at chromium.org
Wed Feb 12 11:03:40 PST 2014


Hi Jasper,

Sorry for the slow reply, I'm way behind on my email :/

On Sun, Feb 9, 2014 at 3:08 AM, Jasper Neumann <jn at sirrida.de> wrote:
> Hi folks!
>
> There is code which converts a switch statement to a table lookup but has
> problems when there are holes in the cases and the default case can not be
> served with the table.
>
> My first attempt to fix this almost but unfortunately not always works.
> The affected file is /lib/Transforms/Utils/SimplifyCFG.cpp .
> This is done by additionally testing a small set.
>
> As an example the function
> ==>
> unsigned test(unsigned x) {
>   switch (x) {
>     case 100: return 0;
>     case 101: return 1;
>     case 103: return 2;
>     case 105: return 3;
>     case 107: return 4;
>     case 109: return 5;
>     case 110: return 6;
>     default: return x*3;
>     }
>   }

Actually, this case could be handled by the table, because "x*3" is
constant in each "hole". This is something I've been meaning to fix :)

But your patch is of course still relevant when the default case truly
doesn't yield a constant. Have you run any benchmarks to see how this
compares to the jump table we'd get otherwise?

One crazy idea for avoiding to build the bitmask ourselves would be to
implement the "hole check" with a switch and then run
SwitchToLookupTable on it. That will build a bitmask if it fits in a
register or use an array otherwise. If this makes the code less
complex, it might be worth a try. What do you think?

Cheers,
Hans



More information about the llvm-commits mailing list