// David Li For switch statement with very large case value range, but the number of actual cases are not large, gcc generated code seems suboptimal: in some cases, huge jump table is generated (with lots of duplicated entries), while in other cases, huge concatenated ifs are generated (the number of compares for a hit may be more than 3 even with binary search scheme). For those cases, mixed mode expansion may be preferred -- where both compares and jump tables are used. // Example 1: huge jump table (> 300 entries) int g; int foo(int k) { switch (k) { case 20: g += 10; break; case 18: g += 11; break; case 16: g += 12; break; case 14: g += 13; break; case 12: g += 14; break; case 10: g += 15; break; case 8: g += 3; break; case 4: g += 2; break; case 2: g -= 1; break; case 0: g += 1; break; case 120: g += 10; break; case 118: g += 11; break; case 116: g += 12; break; case 114: g += 13; break; case 112: g += 14; break; case 110: g += 15; break; case 119: g += 3; break; case 121: g += 2; break; case 122: g -= 1; break; case 123: g += 1; break; case 220: g += 10; break; case 218: g += 11; break; case 216: g += 12; break; case 214: g += 13; break; case 212: g += 14; break; case 210: g += 15; break; case 324: g += 10; break; case 323: g += 10; break; case 322: g += 10; break; case 321: g += 10; break; case 320: g += 10; break; case 318: g += 11; break; case 316: g += 12; break; case 314: g += 13; break; case 312: g += 14; break; case 310: g += 15; break; default: break; } return g; } Example 2: No jump table used: int g; int foo(int k) { switch (k) { case 20: g += 10; break; case 18: g += 11; break; case 16: g += 12; break; case 14: g += 13; break; case 12: g += 14; break; case 10: g += 15; break; case 8: g += 3; break; case 4: g += 2; break; case 2: g -= 1; break; case 0: g += 1; break; case 120: g += 10; break; case 118: g += 11; break; case 116: g += 12; break; case 114: g += 13; break; case 112: g += 14; break; case 110: g += 15; break; case 119: g += 3; break; case 121: g += 2; break; case 122: g -= 1; break; case 123: g += 1; break; case 220: g += 10; break; case 218: g += 11; break; case 216: g += 12; break; case 214: g += 13; break; case 212: g += 14; break; case 210: g += 15; break; case 324: g += 10; break; case 323: g += 10; break; case 322: g += 10; break; case 321: g += 10; break; case 320: g += 10; break; case 318: g += 11; break; case 316: g += 12; break; case 314: g += 13; break; case 312: g += 14; break; case 310: g += 15; break; case 1324: g += 10; break; case 1323: g += 10; break; case 1322: g += 10; break; case 1321: g += 10; break; case 1320: g += 10; break; case 1318: g += 11; break; default: break; } return g; }
I bet there is a duplicate report for this. Also see various GCC summit papers on switch expansion.
Now a gimple optimization opportunity (tree-switch-conversion.c).
I can confirm it's implemented. One get following clusters for the 2 tests: grep clusters pr36362.c.171t.switchlower1 ;; GIMPLE switch case clusters: JT:0-20 JT:110-220 JT:310-324 grep clusters pr36362-2.c.171t.switchlower1 ;; GIMPLE switch case clusters: JT:0-20 JT:110-220 JT:310-324 1318 1320-1324 Which looks good to me.