[bug] Unexpected optimization strategy (arm-none-eabi-gcc 15)

#include <stdint.h>
#include <stddef.h>

/**
 * Name:    CRC-32/MPEG-2  x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
 * Poly:    0x4C11DB7
 * Init:    0xFFFFFFF
 * Refin:   False
 * Refout:  False
 * Xorout:  0x0000000
 * Note:
 */
uint32_t crc32(const uint8_t *data, size_t length)
{
    uint8_t i;
    uint32_t crc = 0xffffffff; // Initial value
    while (length--) {
        crc ^= (uint32_t)(*data++) << 24;
        for (i = 0; i < 8; ++i) {
            if (crc & 0x80000000)
                crc = (crc << 1) ^ 0x04C11DB7;
            else
                crc <<= 1;
        }
    }
    return crc;
}

For this code, the Os/Oz option results in generating larger code than O3.

https://godbolt.org/z/MEqqK1q5T

Section size for -Os options

```

|     .text |    .data |     .bss |  .rodata |
|    44(+0) |    0(+0) |    0(+0) | 1024(+0) |

```


Section size for -O3 options
```

|     .text |    .data |     .bss |  .rodata |
|   104(+0) |    0(+0) |    0(+0) |    0(+0) |

```

The behavior is normal on gcc 10.3.1.

Parents
  • I think it should be this option: -foptimize-crc

    Yes, seems correct for me.

    Was implemented on 11. November 2024 with git commit 062ad209e496a69925b1ac1d80d9b99c54801830:

    commit 062ad209e496a69925b1ac1d80d9b99c54801830
    Author: Mariam Arutunian <mariamarutunian@gmail.com>
    Date:   Mon Nov 11 12:59:04 2024 -0700
    
        [PATCH v7 08/12] Add a new pass for naive CRC loops detection
        
        This patch adds a new compiler pass aimed at identifying naive CRC
        implementations, characterized by the presence of a loop calculating
        a CRC (polynomial long division).  Upon detection of a potential CRC,
        the pass prints an informational message.
        
        Performs CRC optimization if optimization level is >= 2 and if
        fno_gimple_crc_optimization given.
        
        This pass is added for the detection and optimization of naive CRC
        implementations, improving the efficiency of CRC-related computations.
        
        This patch includes only initial fast checks for filtering out non-CRCs,
        detected possible CRCs verification and optimization parts will be
        provided in subsequent patches.
        
        gcc/
        
                * Makefile.in (OBJS): Add gimple-crc-optimization.o.
                * common.opt (foptimize-crc): New option.
                * common.opt.urls: Regenerate to add foptimize-crc.
                * doc/invoke.texi (-foptimize-crc): Add documentation.
                * gimple-crc-optimization.cc: New file.
                * opts.cc (default_options_table): Add OPT_foptimize_crc.
                (enable_fdo_optimizations): Enable optimize_crc.
                * passes.def (pass_crc_optimization): Add new pass.
                * timevar.def (TV_GIMPLE_CRC_OPTIMIZATION): New timevar.
                * tree-pass.h (make_pass_crc_optimization): New extern function
                declaration.
    

    GCC 10.3.x was released on 8. April 2021:

    commit f00b5710a30f22efc3171c393e56aeb335c3cd39 (HEAD, tag: releases/gcc-10.3.0)
    Author: Richard Biener <rguenther@suse.de>
    Date:   Thu Apr 8 11:56:48 2021 +0000
    
        Update ChangeLog and version files for release
    

Reply
  • I think it should be this option: -foptimize-crc

    Yes, seems correct for me.

    Was implemented on 11. November 2024 with git commit 062ad209e496a69925b1ac1d80d9b99c54801830:

    commit 062ad209e496a69925b1ac1d80d9b99c54801830
    Author: Mariam Arutunian <mariamarutunian@gmail.com>
    Date:   Mon Nov 11 12:59:04 2024 -0700
    
        [PATCH v7 08/12] Add a new pass for naive CRC loops detection
        
        This patch adds a new compiler pass aimed at identifying naive CRC
        implementations, characterized by the presence of a loop calculating
        a CRC (polynomial long division).  Upon detection of a potential CRC,
        the pass prints an informational message.
        
        Performs CRC optimization if optimization level is >= 2 and if
        fno_gimple_crc_optimization given.
        
        This pass is added for the detection and optimization of naive CRC
        implementations, improving the efficiency of CRC-related computations.
        
        This patch includes only initial fast checks for filtering out non-CRCs,
        detected possible CRCs verification and optimization parts will be
        provided in subsequent patches.
        
        gcc/
        
                * Makefile.in (OBJS): Add gimple-crc-optimization.o.
                * common.opt (foptimize-crc): New option.
                * common.opt.urls: Regenerate to add foptimize-crc.
                * doc/invoke.texi (-foptimize-crc): Add documentation.
                * gimple-crc-optimization.cc: New file.
                * opts.cc (default_options_table): Add OPT_foptimize_crc.
                (enable_fdo_optimizations): Enable optimize_crc.
                * passes.def (pass_crc_optimization): Add new pass.
                * timevar.def (TV_GIMPLE_CRC_OPTIMIZATION): New timevar.
                * tree-pass.h (make_pass_crc_optimization): New extern function
                declaration.
    

    GCC 10.3.x was released on 8. April 2021:

    commit f00b5710a30f22efc3171c393e56aeb335c3cd39 (HEAD, tag: releases/gcc-10.3.0)
    Author: Richard Biener <rguenther@suse.de>
    Date:   Thu Apr 8 11:56:48 2021 +0000
    
        Update ChangeLog and version files for release
    

Children
No data