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

            Bug ID: 81396
           Summary: Optimization of reading Little-Endian 64-bit number
                    with portable code has a regression
           Product: gcc
           Version: 7.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: qrczakmk at gmail dot com
  Target Milestone: ---

Consider this source code:

#include <stdint.h>

uint64_t ReadLittleEndian64(uint64_t word) {
  const unsigned char* const ptr =
      reinterpret_cast<const unsigned char*>(&word);
  return uint64_t{ptr[0]} |
         (uint64_t{ptr[1]} << 8) |
         (uint64_t{ptr[2]} << (8 * 2)) |
         (uint64_t{ptr[3]} << (8 * 3)) |
         (uint64_t{ptr[4]} << (8 * 4)) |
         (uint64_t{ptr[5]} << (8 * 5)) |
         (uint64_t{ptr[6]} << (8 * 6)) |
         (uint64_t{ptr[7]} << (8 * 7));
}

gcc-6.3 generates good machine code on x86-64 with -O2:

ReadLittleEndian64(unsigned long):
        mov     rax, rdi
        ret

gcc-7.1 leaves out one byte from the optimization:

ReadLittleEndian64(unsigned long):
        movabs  rax, 72057594037927935
        and     rax, rdi
        shr     rdi, 56
        sal     rdi, 56
        or      rax, rdi
        ret

Proof:
https://gcc.godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAKxAEZSBnVAV2OUxAHIBSAJgGY8AO2QAbZlgDU3fgGEGBfEIIA6BDOzcADAEFtO5sIIA2ACwB9ApIBKmAIboAMoQKicQ/HaFmIh5WctJAHcSdABKaQB2ACF9SUk0IQVJZiS8YCFMdASEO2IAKgTUJKsABwJiaX4AETj4%2BuJMI0xiUsaCc2Q7BRlZROTUhnTM7ORcgo0IPmMQ4nCZWN149tYhFKMAgm4Y8uJuAFZorQPayOqo2Tr6698Niy2dioPo2hPt897eyQAOCO3LpbXG5%2BEz3bbRXbPXhvM5VS5ySQQb6SQq8MJ/SIAnRA4F3SzgyGHfgwj5yL5IlGSfjoi5XHG3fxgx57Q6mElw8nIwqmGn/OlAhmg/HM577dmfBEUwr7XmY/m4xnCiFPQ7GcVkyVcyTGWVYnH1QWbAkq6KRdXw2SIrWRdELfTvfScMKkURcfacUhCLhaD2oLiXXixQOSJisdjSAS0D0Eb1O50AaxA/H4KmTafTGeMLq4pg9Xs4PtIfs4HoYIC0pBjBadpDgsCQaAAtqU8G4yBQIE2W22QMBjPxSAAzVsEFpliAAI1jpCbjcwygA8kJRABPaf4RrIAh4ABumDL1fIykwrurztQ5TwxQPAFoF7xSyw2BxaM7T%2B7PdPiwAPb7GG9mJIwDIMg2qpoiuCECQEb8PQkiyKgzati0MGvtGsboqQibJqmGZ4cmWanrmn6HsWpblpWGG1jAiAoIh3YtOQlBdshxC9pE9DDqIo7EOOU6HrO84EEuq7rngm7bnuB6Fs0p4%2Buel7Xlwd4PowT7sHQb5unmX5cL%2B/6AcBoGRCotAQfgRCVHwsGkPB9GsTBaLoWeCZJim%2BH4dmnDEfmhZkYwFFVvJWmcKpvm%2BlwznBaQe68VeXqmEAA%3D%3D

Reply via email to