Fixes incorrect POPCOUNT pattern identification in Hacker's Delight matcher.
A bug in the POPCOUNT pattern matching is fixed, preventing incorrect folding and operand dropping.
This commit corrects an issue in the POPCOUNT pattern matching logic, specifically within the Hacker’s Delight matcher. Previously, distinct captures for the low half of a final add (@step_4) and a shifted temporary (@step4) led to the incorrect folding of (other & 0xffff) + (x >> 16) into .POPCOUNT(x), causing the other operand to be dropped. The fix ensures both operands use the same capture (@step4), requiring the masked and shifted values to be identical.
In Details
gcc/tree-optimization: The POPCOUNT pattern matcher in match.pd (specifically the Hacker's Delight variant) incorrectly identified a POPCOUNT operation due to a mismatch in temporary variable captures. The expression (other & 0xffff) + (x >> 16) was being folded to .POPCOUNT(x) because @step_4 and @step4 were treated as distinct temporaries. By unifying these captures to @step4, the matcher now correctly enforces that the masked value and the shifted value must originate from the same temporary, thereby preventing incorrect folding and loss of operands.
- POPCOUNT
- An instruction or operation that counts the number of set bits (1s) in a binary number.
- Hacker's Delight
- A book by Henry S. Warren, Jr. containing numerous bit manipulation algorithms and techniques. GCC sometimes uses patterns inspired by or directly from this book for optimizations.
- matcher
- In the context of compilers, a component that attempts to find specific patterns in the intermediate representation (IR) to replace them with optimized code sequences or specific machine instructions.
- tree-ssa
- A compiler optimization technique that transforms the intermediate representation into Static Single Assignment (SSA) form, which simplifies many optimization passes.
- pattern matching
- The process of searching for and identifying specific structures or sequences within data, such as in compiler optimization passes to recognize optimizable code idioms.