https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85560

            Bug ID: 85560
           Summary: Missed optimization in bit-by-bit variable zeroing
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vlad at ivanov dot email
  Target Milestone: ---

The following C++ code loops until a variable is cleared bit-by-bit:

#include <cstdint>

uint32_t b;
uint32_t test() {
    uint32_t a = b;
    uint32_t bits = 1;

    while (a != 0) {
        a &= ~bits;
        bits <<= 1;
    }

    return 0;
}

After looping, it returns 0. While the loop is useless, it still exists in the
assembly even with -O3.

GCC version: 9.0.0 20180427 
Demo: https://godbolt.org/g/7gRMqu

Notably, the loop is optimized away on ICC 18

Reply via email to