Fold idioms involving negation and bitwise operations.
Compiler now recognizes and simplifies arithmetic idioms involving negation and bitwise operations for improved performance.
The compiler’s pattern matching system has been enhanced to recognize and fold two arithmetic idioms that are built from the negation of a value. These optimizations, which involve combinations of addition, bitwise AND, and negation, can lead to more efficient code generation, particularly for operations like rounding up to the nearest power of two. The changes also include updates to test cases and ensure that the optimizations do not interfere with overflow sanitization.
In Details
The match.pd subsystem gained two new simplification rules. The first folds X + (X | -X) into X & (X - 1), and the second folds X + ((-X) & (C - 1)) into (X + C - 1) & -C when C is a power of two. These rules are applicable to integral types and aim to simplify common arithmetic patterns, such as those found in memory allocators. The folding is restricted to avoid interfering with overflow sanitization and uses :s to prevent unnecessary recomputation.
- idiom
- A specific pattern of code that expresses a common programming task in a concise way.
- fold
- In compiler optimization, to replace a sequence of operations with a single, equivalent operation or a simpler sequence.
- bitwise AND
- A binary operation that takes two bit patterns of equal length and performs a logical AND operation on each pair of corresponding bits. The result in each position is 1 if the corresponding bits of both operands are 1, and 0 otherwise.
- negation
- The operation of reversing the sign of a number. For example, the negation of 5 is -5, and the negation of -3 is 3.
- overflow sanitization
- A compiler feature that detects and potentially reports or handles arithmetic overflow errors during program execution.