<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Missed optimization: store coalescing"
   href="https://bugs.llvm.org/show_bug.cgi?id=41761">41761</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed optimization: store coalescing
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>costan@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>LLVM coalesces consecutive byte loads into larger loads when appropriate, but
doesn't do so for stores. This is unfortunate -- having these optimizations
would allow me to write platform-independent code for loading/storing
{little,big}-endian integers. Without the optimizations, I need to write an
explicit fast path for little-endian platforms.

The details below are also in the Godbolt example at
<a href="https://godbolt.org/z/45S0ID">https://godbolt.org/z/45S0ID</a>


The following functions get optimized to single mov instructions on x864_64.

uint32_t DecodeFixed32(const char* ptr) noexcept {
  const uint8_t* buffer = reinterpret_cast<const uint8_t*>(ptr);
  return ((static_cast<uint32_t>(buffer[0])) |
          (static_cast<uint32_t>(buffer[1]) << 8) |
          (static_cast<uint32_t>(buffer[2]) << 16) |
          (static_cast<uint32_t>(buffer[3]) << 24));
}

uint64_t DecodeFixed64(const char* ptr) noexcept {
  const uint8_t* buffer = reinterpret_cast<const uint8_t*>(ptr);
  return ((static_cast<uint64_t>(buffer[0])) |
          (static_cast<uint64_t>(buffer[1]) << 8) |
          (static_cast<uint64_t>(buffer[2]) << 16) |
          (static_cast<uint64_t>(buffer[3]) << 24) |
          (static_cast<uint64_t>(buffer[4]) << 32) |
          (static_cast<uint64_t>(buffer[5]) << 40) |
          (static_cast<uint64_t>(buffer[6]) << 48) |
          (static_cast<uint64_t>(buffer[7]) << 56));
}


However, the following functions do not get optimized to mov instructions.

void EncodeFixed32(char* dst, uint32_t value) noexcept {
  uint8_t* buffer = reinterpret_cast<uint8_t*>(dst);
  buffer[0] = static_cast<uint8_t>(value);
  buffer[1] = static_cast<uint8_t>(value >> 8);
  buffer[2] = static_cast<uint8_t>(value >> 16);
  buffer[3] = static_cast<uint8_t>(value >> 24);
}

void EncodeFixed64(char* dst, uint64_t value) noexcept {
  uint8_t* buffer = reinterpret_cast<uint8_t*>(dst);
  buffer[0] = static_cast<uint8_t>(value);
  buffer[1] = static_cast<uint8_t>(value >> 8);
  buffer[2] = static_cast<uint8_t>(value >> 16);
  buffer[3] = static_cast<uint8_t>(value >> 24);
  buffer[4] = static_cast<uint8_t>(value >> 32);
  buffer[5] = static_cast<uint8_t>(value >> 40);
  buffer[6] = static_cast<uint8_t>(value >> 48);
  buffer[7] = static_cast<uint8_t>(value >> 56);
}


GCC versions 8 and above optimize stores. Lower versions only optimize loads.
MSVC does not optimize loads or stores.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>