<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 - Inefficient lowering of constant, negative, anyext return values"
   href="https://bugs.llvm.org/show_bug.cgi?id=39092">39092</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Inefficient lowering of constant, negative, anyext return values
          </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>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>asb@lowrisc.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider a function returning a constant negative value, e.g.

define i16 @neg_const() nounwind {
  ret i16 -2047
}

Compile this on a target for which i16 is not a legal type, e.g. PowerPC. For
llc -mtriple=powerpc, the following assembly is generated:
        lis 3, 0
        ori 3, 3, 65436
        blr


We'd rather see:
        li 3, -100
        blr

The issue is that SelectionDAGBuilder::visitRet will call
SelectionDAG::getCopyToParts with ExtendKind = ISD::ANY_EXTEND. This calls
SelectionDAG::getNode with ISD::ANY_EXTEND, which will recognise it has a
constant argument and choose to zero-extend it. This generates the i32 constant
65436 (i.e. zext of int16(-100)) rather than the preferable -100.

By the time target instruction selection code is reached, there is no way to
know that a sign-extended form of the original constant could have been used.

As a quick experiment I changed the constant-folding code in
SelectionDAG::getNode so it performs a sign-extend for ANY_EXTEND input. That
fixes this particular issue as expected but leads to incorrect codegen
elsewhere, though I haven't yet had a chance to explore in more detail.</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>