match.pd: Fold rounded-up average idiom, dual to floor rule
New pattern in match.pd recognizes and simplifies a common rounded-up average calculation, improving code generation for media codecs.
A new pattern has been added to match.pd to recognize and fold a specific idiom for calculating a rounded-up average: ((x >> 1) + (y >> 1)) + ((x | y) & 1). This idiom is mathematically equivalent to (x | y) - ((x ^ y) >> 1), which is a more optimized form. This change is particularly relevant for targets with averaging instructions and for vector types, where it reduces the number of instructions generated for motion-compensation averages in media codecs.
In Details
This commit adds a new pattern to match.pd that folds the identity ((x >> 1) + (y >> 1)) + ((x | y) & 1) == (x | y) - ((x ^ y) >> 1). This is the dual of an existing floor rule and specifically targets rounded-up averages, common in media codec motion compensation. On targets with an AVG_CEIL instruction, this ensures the late vector pattern can still recognize the operation. For vector types, it reduces a six-operation scalar form to four, and on architectures like ARM, it reduces a six-instruction sequence to three.
- match.pd
- A file containing pattern matching rules used by GCC's tree-based optimizers to recognize and transform specific code structures (expressions and statements) into more efficient forms.
- fold
- In compiler optimization, 'folding' refers to evaluating an expression at compile time or replacing a complex expression with a simpler, equivalent one.
- RTX
- Representation of intermediate code in GCC, distinct from GIMPLE. RTX is lower-level and closer to machine instructions, used by the backend and some middle-end passes for pattern matching and code generation.
- media codecs
- Software or hardware components that compress (encode) and decompress (decode) digital audio and video data. They often employ complex arithmetic operations, including averaging.