<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 - PowerPC: Invalid merging of immediates for D-Form"
   href="https://bugs.llvm.org/show_bug.cgi?id=49640">49640</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>PowerPC: Invalid merging of immediates for D-Form
          </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>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>Backend: PowerPC
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>nemanja.i.ibm@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, nemanja.i.ibm@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=24661" name="attach_24661" title="Eigen test reduced by creduce">attachment 24661</a> <a href="attachment.cgi?id=24661&action=edit" title="Eigen test reduced by creduce">[details]</a></span>
Eigen test reduced by creduce

When a D-Form instruction is fed by an add-immediate, we attempt to merge the
two immediates to form a single displacement so we can remove the
add-immediate.

However, we don't check whether the new displacement fits into a 16-bit signed
immediate field early enough. Namely, we do a sign-extend from 16 bits first
which will discard high bits and then we check whether the result is a 16-bit
signed immediate. It of course will always be.

The check needs to be moved prior to the sign-extend.

Reduced test case from Eigen attached.

To reproduce the problem:
clang -S -O3 -mcpu=pwr9 badDForm.cpp -o - 2>/dev/null | grep stfs
        stfs 0, 25400(7)
        stfs 0, 30480(7)
        stfs 0, -29976(7)
        stfs 0, -24896(7)
        stfs 0, -19816(7)

The correct values (with the peephole turned off):
clang -S -O3 -mcpu=pwr9 badDForm.cpp -o - -mllvm --disable-ppc-peephole
2>/dev/null | grep stfs
        stfs 0, 0(7)
        stfs 0, 5080(7)
        stfs 0, 10160(7)
        stfs 0, 15240(7)
        stfs 0, 20320(7)</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>