Fold CRC builtins at compile time
GCC now computes CRC values at compile time if arguments are constant, avoiding runtime computation.
GCC’s middle-end can now perform constant folding for various CRC (Cyclic Redundancy Check) built-in functions. This change computes CRC values at compile time when all arguments are compile-time constants, replacing runtime table-based computations with a simpler loop calculation. This optimization avoids runtime overhead for CRC operations, especially beneficial in performance-sensitive code. Future work will include architecture-specific CRC folding.
In Details
This commit enhances the fold_const_call pass by introducing fold_const_crc in fold-const.cc. It leverages refactored and made-public calculate_crc and calculate_reversed_crc functions in hwint.cc to compute CRC values directly during compilation when arguments are known constants. This avoids emitting code for the table-based CRC computation, reducing binary size and improving runtime performance for CRC operations.
- CRC
- Cyclic Redundancy Check. A type of checksum used for detecting errors in data transmission or storage. It involves polynomial division over a finite field.
- Constant folding
- An optimization technique where expressions with constant operands are evaluated at compile time rather than at runtime. This reduces runtime computation and can simplify code.
- Middle-end
- The phase of a compiler that operates on an intermediate representation (IR) of the code, performing optimizations before generating target-specific code. GCC's middle-end includes passes like constant folding.
- Built-in functions
- Functions provided by the compiler that are not part of the standard language but offer specific functionalities, often for performance or low-level access. Examples include
__builtin_crc*.