This optimization 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:
cond = x < table_size;
if (cond) {
r = table[x];
} else {
r = 0;
}
if (cond) {
do_something;
}
Jump threading can then eliminate the second if (cond):
if (x < table_size) {
r = table[x];
} else {
r = 0;
do_something;
}
This helper function should probably be static.