match.pd: Fold (X | A) cmp C and (X ^ A) cmp C into X cmp C
Optimizes bitwise OR/XOR comparisons by simplifying them based on known bit patterns.
This optimization in match.pd simplifies expressions involving bitwise OR (|) or XOR (^) combined with a comparison. If a value A is known to fit within a certain bit range (e.g., less than 2^m), and the comparison constant C has specific patterns in its lower m bits (all ones or all zeros), the expression (X | A) cmp C or (X ^ A) cmp C can be folded into a simpler X cmp C. This avoids unnecessary bitwise operations when their effect on the comparison is nullified by the known properties of A and C.
In Details
The match.pd pattern matching infrastructure now recognizes and folds certain bitwise OR (|) and XOR (^) operations preceding comparisons. The fold applies when operand A is constrained to [0, 2^m-1], and the constant C has its lower m bits as all ones (for LE/GT comparisons) or all zeros (for LT/GE comparisons). In such cases, (X | A) cmp C is equivalent to X cmp C because A's bits do not affect the relevant bits of X that determine the comparison outcome. This optimization is guarded against operations that might affect the sign bit of X.
- match.pd
- A file within GCC that defines patterns used by the compiler's instruction scheduler and other optimization passes to match specific code structures and apply transformations.
- Fold
- In compiler optimization, 'folding' refers to evaluating an expression at compile time or simplifying it based on known properties of its operands.
- Scalar Evolution (SCEV)
- A compiler analysis technique that computes precise information about how scalar values (like loop counters and array indices) change across loop iterations. It's crucial for predicting memory access patterns and enabling optimizations like vectorization.
- Bitwise OR (|)
- A logical operation that compares each bit of two numbers. If either bit is 1, the resulting bit is 1; otherwise, it's 0.
- Bitwise XOR (^)
- A logical operation that compares each bit of two numbers. If the bits are different, the resulting bit is 1; otherwise, it's 0.