Fold bitfield extraction and shifting expressions
Optimizes expressions like `((X >> C1) & C2) << C3` into simpler forms for better code generation.
This patch introduces a new simplification rule in match.pd to optimize expressions of the form ((X >> C1) & C2) << C3. These expressions, which extract bits, mask them, and then shift them, are folded into equivalent, more efficient forms. The optimization applies to unsigned types under specific conditions related to shift amounts and bit precision, potentially generating more compact and faster machine code, as demonstrated by an example on aarch64.
In Details
The match.pd pattern-matching system now includes a rule to simplify complex bit manipulation expressions of the form ((X >> C1) & C2) << C3. This folding is applicable when X is unsigned and its precision matches its width, and C1/C3 are within bounds. The transformation rewrites the expression into (X >> (C1 - C3)) & (C2 << C3) (if C1 >= C3) or (X << (C3 - C1)) & (C2 << C3) (if C1 < C3), preserving leading zeros and potentially mapping to fewer or more efficient instructions, such as on aarch64.
- match.pd
- A GCC internal file or component responsible for pattern matching, likely used in the optimization or code generation phases to identify and transform specific code structures.
- bitfield extraction
- The process of isolating a specific sequence of bits within a larger integer value.
- folding
- In compiler optimization, the process of simplifying expressions by evaluating constants or combining operations into a more efficient form.
- aarch64
- The 64-bit ARM architecture, a common target for GCC.