common: Enable -fduplicate-switch-dispatch at -O2 for performance
The -fduplicate-switch-dispatch optimization is now enabled by default at -O2 for speed-focused builds.
The -fduplicate-switch-dispatch optimization, which improves dispatch speed at the cost of code size, is now enabled by default for -O2 builds. This option complements the existing computed-goto unfactoring and has shown significant performance improvements on benchmarks like mawk and QuickJS-ng on AArch64 systems. It remains disabled for size-optimized builds (-Os, -Oz) and size-optimized functions.
In Details
The duplicate-switch-dispatch transformation optimizes switch statements by creating duplicated code paths for frequently used jump targets, trading code size for faster dispatch. This commit enables it by default at -O2 on targets supporting TARGET_CAN_SHARE_JUMP_TABLE_P, where it overlaps with computed-goto unfactoring. This aims to improve runtime performance for applications with heavy switch statement usage, as demonstrated by improvements in benchmarks like mawk and QuickJS-ng on AArch64.
- -fduplicate-switch-dispatch
- A GCC optimization flag that duplicates code paths within switch statements to accelerate dispatching to target labels.
- switch statement
- A control flow statement that allows a program to execute different code blocks based on the value of an expression.
- dispatch speed
- The time it takes for a program to determine and transfer control to the correct code path, especially in structures like switch statements or virtual function calls.
- computed goto
- A programming construct (supported by GCC) that allows indirect jumps to labels determined at runtime, often used to optimize switch statements.
- jump tables
- A data structure used to implement switch statements efficiently, mapping case values to target addresses.
- AArch64
- The 64-bit ARM architecture instruction set.
- -O2
- A GCC optimization level that enables a significant set of optimizations for speed, without excessive compile time or code size increase.
- -Os
- A GCC optimization level that prioritizes code size reduction over execution speed.