Optimize switch lowering by deferring it for data-dependent switches.
Improves performance by deferring switch lowering for data-dependent switches, preventing unnecessary code bloat and cache misses.
This commit refines GCC’s switch lowering strategy to improve performance, particularly for complex state-machine-like switches. Previously, all switches were deferred to late threaders, which could lead to code duplication and performance degradation (e.g., 4-6% on specific benchmarks). A new early switch lowering pass is introduced that only lowers switches whose controlling value is invariant within their innermost loop. Data-dependent switches and those in straight-line code are still handled by late threaders or early lowering, respectively, balancing optimization benefits with code visibility for other passes.
In Details
To improve performance (specifically for benchmarks like 548.exchange2_r, where digits_2 grew 20% and performance suffered 4-6%), this patch introduces pass_lower_switch_early. This new pass runs before cse_sincos and only lowers GIMPLE_SWITCH statements whose controlling value is invariant in their innermost loop. Switches outside any natural loop or with data-dependent conditions are still subject to later lowering passes or handled by late threaders. This contrasts with the previous approach (post-r17-6332-gc9609467230d5d) where all switches were deferred to late threaders, potential…
- switch lowering
- The process of transforming a
GIMPLE_SWITCHstatement, which represents a multi-way branch based on a value, into simpler control flow structures like conditional branches (if-else chains) or jump tables. - GIMPLE_SWITCH
- GCC's intermediate representation for a switch statement.
- threaders
- Refers to optimization passes that analyze and transform control flow graphs, often used for tasks like loop optimizations or analyzing code paths. 'Late threaders' implies passes that run later in the optimization pipeline.
- invariant
- In the context of loops, a value or expression is invariant if it does not change its value during the execution of the loop.
- CSE
- Common Subexpression Elimination. An optimization that identifies identical expressions computed multiple times and replaces subsequent computations with the result of the first computation.
- i-cache misses
- Instruction cache misses. Occur when the CPU needs to fetch an instruction from main memory because it's not present in the faster instruction cache, leading to performance degradation.