<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 opportunity: transform comparisons with constants to a mask test"
   href="https://bugs.llvm.org/show_bug.cgi?id=40611">40611</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed optimization opportunity: transform comparisons with constants to a mask test
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

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

        <tr>
          <th>Hardware</th>
          <td>All
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>nok.raven@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>LLVM does not recognize that:

bool zot(unsigned i) {
    return i == 0 || i == 2;
}

Can be rewritten as:

bool zot_handopt(unsigned i) {
    return !(~2u & i);
}

<a href="https://rise4fun.com/Alive/PXsF">https://rise4fun.com/Alive/PXsF</a>

With more values the optimization even more useful:

bool ztfos(unsigned i) {
    return i == 0 || i == 2 || i == 4 || i == 6;
}
bool ztfos_handopt(unsigned i) {
    return !(~6u & i);
}

More complex example that greatly benefits from such optimization:

bool is_sign(char c) {
    return c == '-' || c == '+';
}
bool is_sign_handopt(char c) {
    unsigned char i = c;
    i -= '+';
    return !(~2u & i);
}

<a href="https://rise4fun.com/Alive/nKIp">https://rise4fun.com/Alive/nKIp</a>

GCC and MSVC recognize and optimize all the examples above
<a href="https://godbolt.org/z/_Pmy0S">https://godbolt.org/z/_Pmy0S</a>
The problem is not target specific (checked x86, x86-64, ppc32, ppc64, arm, and
arm64).</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>