Switch Lowering in GCC
Web page · https://xoranth.net/gcc-switch
First seen by Oriel on .
AI summary
The article explains how GCC lowers switch statements into jump tables, comparison trees, or bit tests, and describes the heuristics and compiler passes behind those choices.
- Dense cases can become jump tables, while sparse cases may become comparison trees; the article reports a threshold of roughly eight jump-table entries per case label.
- GCC can isolate dense subsets from outlier cases, and it merges contiguous labels targeting the same code block, reducing comparisons and affecting jump-table profitability.
- For non-contiguous character cases, GCC can use a bitset and the `bt` instruction; the bit positions encode the selected case values relative to an offset.
- The `pass_lower_switch` analysis forms case clusters, tries bit-test clustering and then jump-table clustering, and sends remaining cases through decision-tree expansion.
- Bit-test clustering uses dynamic programming to minimize the number of bitsets, then applies a profitability heuristic before emitting them.
#gcc #compiler optimization #jump tables #bitsets
Generated by gpt-5.6-sol. Check the original for details.
Metric trends
Last 30 days · Up to 120 recorded samples per source and metric, with reposts kept separate · First and latest retained
Points
Score reported by the source.
- Hacker News: 210, latest observation ; +201 over observed period since
- Lobsters: 32, latest observation ; +30 over observed period since
Comments
Comment count reported by the source.
- Hacker News: 49, latest observation ; +49 over observed period since
- Lobsters: 4, latest observation ; +4 over observed period since
Aliases
| Source | External ID | URL |
|---|---|---|
| Hacker News | 50314085 | https://xoranth.net/gcc-switch/ Read discussion |
| Lobsters | ncclwy | https://xoranth.net/gcc-switch/ Read discussion |