[PATCH] peephole optimization in switch table lookup: reuse the guarding table comparison if possible

Erik Eckstein eeckstein at apple.com
Tue Nov 18 07:53:01 PST 2014


Hi Hans,

I have another patch for the switch table lookup generation. It tries to reuse the generated compare instruction, if there is a comparison agains the default value after the switch.

Example:

switch (x) {
  case 0: r = 10; break;
  case 1: r = 11; break;
  ...
  default: r = 0; break; // 0 does not appear in any case value.
}
if (r == 0) {
  do_something;
}

transforms to:

if (x < table_size) {
  r = table[x];
} else {
  r = 0;
  do_something;
}

Now I must admit, that this pattern is quite a corner case (only a single occurrence in the test-suite). My trigger for this optimization is that in our project we are generating such IR patterns.

Please let me know your comments.

Thanks,
Erik

-------------- next part --------------
A non-text attachment was scrubbed...
Name: llvm-switch-cmp-reuse.patch
Type: application/octet-stream
Size: 8081 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141118/a2647344/attachment.obj>


More information about the llvm-commits mailing list