GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc

Limit outer-loop unswitching by duplicated code size

The compiler avoids excessive code duplication by limiting outer-loop unswitching based on duplicated code size.

GCC’s loop unswitching optimization now considers the code size impact of hoisting unswitch predicates from inner loops to outer loops. The compiler estimates the size of the duplicated outer-loop bodies and avoids unswitching if the size exceeds param_max_unswitch_insns, preventing excessive code growth. A new test case, gcc.dg/loop-unswitch-19.c, has been added to verify this.

In Details

The commit introduces estimate_loop_insns to estimate loop instruction size. init_loop_unswitch_info now checks if the duplicated outer-body size exceeds param_max_unswitch_insns before selecting an outer loop for unswitching. This prevents excessive code duplication during loop unswitching, a transformation within the tree-ssa-loop-unswitch.cc pass.

For Context

Loop unswitching is an optimization technique where a loop containing a conditional statement is transformed into multiple loops, each executing a specific branch of the conditional. Moving the conditional outside the loop can improve performance, but it also duplicates the loop's body. This commit limits the hoisting of unswitch predicates to outer loops, considering the code size impact to prevent excessive code growth.

Filed Under: gccoptimizationloop unswitching