[llvm-commits] [Patch] Switch-to-lookup tables: do simple constant propagation

Hans Wennborg hans at chromium.org
Wed Oct 3 05:39:29 PDT 2012


On Wed, Oct 3, 2012 at 12:51 PM, Duncan Sands <baldrick at free.fr> wrote:
> Hi Hans,
>
>
>> A patch [1] by Craig earlier this week pointed out that Clang fails to
>> build lookup tables for switches like this:
>>
>>    switch(x) {
>>      case 1: return 5;
>>      case 2: return 42;
>>      case 3: case 4: case 5:
>>        return x - 123;
>>      default: return 123;
>>    }
>>
>> Where the resulting value for the 3, 4, 5 cases is "x - 123", rather
>> than a constant.
>
>
> maybe it is better to do this as part of some more generic switch
> simplification
> logic rather than during table construction.

That would mean cloning the basic block with "return x - 123" into one
block for each case, and then constant propagation could take care of
it. Or rather, we should only clone it if it can get constant folded.

Would that always be profitable, though? If the switch doesn't get
turned into a lookup table, we've replaced one basic block with lots
of small ones. At least for code size, it seems that could be counter
productive?

Thanks,
Hans



More information about the llvm-commits mailing list