reassoc: Keep conditional-compare chains linear for AArch64
Improves AArch64 code generation for chained comparisons.
This commit enhances the reassociation pass in GCC to keep chains of conditional comparisons linear when appropriate, particularly benefiting the AArch64 architecture. Previously, reassociation would aggressively rebalance these chains into trees, which is inefficient for comparisons that depend on previously set flags. The change ensures that linear chains are maintained when operands are scalar comparisons within the same block and at least one comparison can be folded into flags, leading to more efficient instruction sequences, such as single ccmp sequences on AArch64.
In Details
The reassociate_bb pass, when processing boolean expressions, normally rebalances chains of operations into trees. However, for conditional comparisons (CCMP on AArch64), this tree-like reassociation is suboptimal because each comparison depends on the flags set by the previous one. This patch introduces logic to identify complete conditional-compare chains where operands are scalar comparisons in the same block and at least one comparison can be folded into flags. For such chains, it maintains linearity and strategically places a potentially forwarded trapping comparison in the innermost…
- reassociation
- A compiler optimization pass that rearranges associative operations (like addition or multiplication) to potentially improve performance or enable other optimizations.
- conditional compare (CCMP)
- An AArch64 instruction that conditionally updates a register based on the condition flags set by a previous comparison, often used for chaining comparisons.
- flags
- Special processor registers that store the results of arithmetic and logical operations, such as zero, negative, or carry.
- tree-ssa
- Static Single Assignment form, an intermediate representation used by GCC that simplifies many optimization passes.
- AArch64
- The 64-bit ARM architecture instruction set.