<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 - [MIPS] Missing ANDI optimization"
   href="https://bugs.llvm.org/show_bug.cgi?id=43481">43481</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[MIPS] Missing ANDI optimization
          </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: MIPS
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>arichardson.kde@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>We noticed that clang generates unnecessary SLL instructions in one of our
benchmark hot paths (and apparently GCC does not): <a href="https://godbolt.org/z/xKcCqT">https://godbolt.org/z/xKcCqT</a>


Consider
```
#include <stdint.h>

uint64_t foo(uint64_t a)
{
    uint64_t b = a / 16;
    uint64_t c = b & 0x7UL;
    return ((uint64_t)1UL << c);
}
```

At present, the MIPS backend produces:
```
foo(unsigned long):                                # @foo(unsigned long)
        sll     $1, $4, 0
        srl     $1, $1, 4
        andi    $1, $1, 7
        daddiu  $2, $zero, 1
        jr      $ra
        dsllv   $2, $2, $1
```

The `sll` is introduced because the `srl` requires that its input be zero
extended (which, well, seems silly, but so it goes).  In any case, because
`andi` has a 16-bit immediate, some arithmetic and lookahead could find that
`0x7 << 4` fits and so make this be

```
        andi    $1, $4, 0x70
        srl     $1, $1, 4
        daddiu  $2, $zero, 1
        jr      $ra
        dsllv   $2, $2, $1
```

Alternatively, whatever's concluding that it can use 32-bit values internally
could stop doing that and this could instead just be

```
        dsrl    $1, $4, 4
        andi    $1, $1, 7
        daddiu  $2, $zero, 1
        jr      $ra
        dsllv   $2, $2, $1
```

This problem was found by Nathaniel Wesley Filardo and reported as
<a href="https://github.com/CTSRD-CHERI/llvm-project/issues/343">https://github.com/CTSRD-CHERI/llvm-project/issues/343</a></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>