Simplify bitwise comparisons involving powers of two
Optimizes bitwise OR comparisons to AND for powers of two, potentially improving performance.
This change simplifies expressions of the form (A | C) == A to (A & C) != 0 when C is a power of two. This optimization avoids a bitwise OR and replaces it with a bitwise AND and a comparison against zero, which can be more efficient on some architectures. The inverse transformation for (A | C) != A to (A & C) == 0 is also included. A new test case verifies the optimization.
In Details
The match.pd pass in GCC simplifies common expression patterns. This commit adds a new pattern to recognize when (A | C) == A can be transformed into (A & C) != 0 if C is a power of two. This leverages the property that for a power-of-two C, all bits set in C must also be set in A for the OR operation to result in A. This transforms a potentially more expensive OR and equality check into a cheaper AND and inequality check against zero. This is a standard bit manipulation optimization.
- bitwise OR
- A bitwise operation that combines two binary numbers. For each bit position, the result is 1 if either of the corresponding bits is 1, and 0 otherwise.
- bitwise AND
- A bitwise operation that combines two binary numbers. For each bit position, the result is 1 only if both of the corresponding bits are 1, and 0 otherwise.
- power of two
- An integer that can be expressed in the form 2^n, where n is a non-negative integer. In binary representation, a power of two has exactly one bit set to 1.
- GCC match pass
- A compiler pass in GCC that applies various optimizations by matching specific expression patterns in the code's intermediate representation.