<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 - Failure to optimize integer conversion pattern"
   href="https://bugs.llvm.org/show_bug.cgi?id=48692">48692</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Failure to optimize integer conversion pattern
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

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

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>Scalar Optimizations
          </td>
        </tr>

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

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

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>int64_t f(int64_t x)
{
    return ((x << 48 >> 48) & -65536) | (x & 65535);
}

This can be optimized to `return (x << 48 >> 48);` (i.e. `(int16_t)x`), see
also Alive2 test here: <a href="https://alive2.llvm.org/ce/z/L7z-JQ">https://alive2.llvm.org/ce/z/L7z-JQ</a>

Should be generalizable to shifts/and patterns of any size.

Note: I originally found this while investigating how GCC and LLVM optimize
this piece of code:

union reg {
    uint64_t r;
    uint16_t x;
};

reg cbw(reg dest)
{
    dest.r = (int16_t)dest.x;
    return dest;
}

Which (on little endian targets) GCC optimizes to `return (int16_t)x` and LLVM
optimizes to the sample code I gave above.</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>