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

Hans Wennborg hans at chromium.org
Wed Oct 3 04:46:20 PDT 2012


Hi all,

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.

The attached patch teaches the transformation to do simple constant
propagation of x in the successor block, so that it can realize the
resulting value is constant after all.

Building ToT Clang with this results in 39 new switches being
transformed to lookup tables, and a 4 kB binary size reduction.

I tried to find an utility function that could be used for the
constant propagation, but didn't find anything. Please advice if there
is some function I can reuse, or a better way to do this.

Please take a look.

Thanks,
Hans

[1] http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20121001/152057.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: switch_table_const_prop.patch
Type: application/octet-stream
Size: 8309 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20121003/f5031722/attachment.obj>


More information about the llvm-commits mailing list