match.pd: Fold SAD_EXPR with identical inputs to simplify reduction loops
Optimizes vector reduction loops by folding redundant absolute difference calculations.
This commit adds a new pattern to match.pd that folds SAD_EXPR (Sum of Absolute Differences) operations where the first two operands are identical. Such cases, which evaluate to zero, can arise in vectorization of small reduction loops. By folding SAD(x, x, acc) into just acc, the compiler avoids unnecessary calculations, potentially improving the efficiency of vectorized reduction operations.
In Details
A new rule is added to match.pd to simplify SAD_EXPR where the first (op1) and second (op2) operands are identical. The pattern SAD(op1, op2, acc) is folded to acc when op1 == op2. This situation can occur during vectorization of reduction loops where an absolute difference is computed on a value with itself, resulting in zero. Folding this redundant SAD_EXPR avoids unnecessary computation and simplifies the generated code.
- match.pd
- A GCC file containing pattern-matching rules used by the compiler's instruction selector to map intermediate representation operations to specific machine instructions.
- SAD_EXPR
- An intermediate representation (IR) construct likely representing a Sum of Absolute Differences operation, often used in contexts like audio processing or vector operations.
- vectorization
- An optimization technique where a single instruction operates on multiple data elements simultaneously, often by using special CPU instructions (SIMD).
- reduction loop
- A loop that accumulates a single value from all iterations, such as summing elements of an array.