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

            Bug ID: 119943
           Summary: -O3 forgets trivial code shift. causing significant
                    slowdown
           Product: gcc
           Version: 12.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aleks at physik dot tu-berlin.de
  Target Milestone: ---

the test below caused significant slowdown (3 vs 11 us) using "-O3"
optimization on x64 arch (Intel 12 gen).
the critical line of code is a struct initialization of 4x uint8.

this indicates 2 issues:

1. why moving the intialization around changes runtime in O3 ?
gcc should detect this easy trick (low hanging fruit).

2. moving up another line  "uint8 aa,bb=0,cc=3,dd=4;" is not harmful.
struct filling is more critical, even if only {0,0,0,0} = 0u (32bit)


// the code reduced to the idea
typedef unsigned int uint32;
typedef unsigned char uint8;

// time measure via (average over 10 runs)
struct timespec ts0 = {0, 0};
clock_gettime(CLOCK_REALTIME, &ts0);

typedef struct {
    uint8 a, b, c, d;
} cfg_t;

// test (2 vs 10 us)
uint32 test1(elem_t array[], const uint32 n) {  // n=60

    // if critical line is here, then slowdown !!
    if (n <= 60u) {
        if (n > 1u) quickSort(array, 0, n - 1);
        return 0u;
    }
    // we never go here, as (n<=60)
    uint8 aa, bb = 0, cc = 3, dd = 4; // the other not harmful line

    cfg_t cfg = {0, 0, 0, 0}; // critical line !!!!!!!!!!!

    // something ABC, same in test2()
    // we use cfg struct here many times

    return n + cfg.a;
}

Reply via email to