Bug 35362 - Splitting up a switch table into smaller ones (where there a huge gaps between the clusters)
Summary: Splitting up a switch table into smaller ones (where there a huge gaps betwee...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.3.0
: P3 enhancement
Target Milestone: ---
Assignee: Steven Bosscher
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2008-02-25 05:37 UTC by davidxl
Modified: 2019-03-01 14:57 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-03-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description davidxl 2008-02-25 05:37:42 UTC
// 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;
}
Comment 1 Richard Biener 2008-02-25 11:37:33 UTC
I bet there is a duplicate report for this.  Also see various GCC summit papers
on switch expansion.
Comment 2 Steven Bosscher 2019-03-01 12:54:38 UTC
Now a gimple optimization opportunity (tree-switch-conversion.c).
Comment 3 Martin Liška 2019-03-01 14:57:30 UTC
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.