<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 - x86: inefficient code for x == 0 || x == 1"
   href="https://bugs.llvm.org/show_bug.cgi?id=32524">32524</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>x86: inefficient code for x == 0 || x == 1
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>4.0
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>hans@chromium.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Blocks</th>
          <td>26299
          </td>
        </tr></table>
      <p>
        <div>
        <pre>For the example code:

bool f(int x) {
  return x == 0 || x == 1;
}

Clang generates (10 bytes):

 or     edi,0x1
 cmp    edi,0x1
 sete   al
 ret 

GCC and MSVC generate (7 bytes):

 cmp    edi,0x1
 setbe  al
 ret


At -Os, Clang's code gets even worse, not clear why (14 bytes):

 or     edi,0x1
 mov    eax,0x1
 cmp    edi,eax
 sete   al
 ret



I suppose LLVM notices that the conditions only differ in the least significant
bit, as in the example below where the strategy makes sense:


bool g(int x) {
  return x == 4 || x == 5;
}


Clang:

 or     edi,0x1
 cmp    edi,0x5
 sete   al
 ret

GCC:

 sub    edi,0x4
 cmp    edi,0x1
 setbe  al
 ret





A related case is this one:

bool h(int x) {
  return x == -1 || x == 0;
}

Clang (14 bytes):

h(int):
 cmp    edi,0xffffffff
 sete   cl
 test   edi,edi
 sete   al
 or     al,cl
 ret

GCC (10 bytes):

h(int):
 add    edi,0x1
 cmp    edi,0x1
 setbe  al
 ret</pre>
        </div>
      </p>

        <div id="referenced">
          <hr style="border: 1px dashed #969696">
          <b>Referenced Bugs:</b>
          <ul>
              <li>
                [<a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [meta][X86] Size optimization opportunities (in particular for 32-bit Chromium on Windows)"
   href="https://bugs.llvm.org/show_bug.cgi?id=26299">Bug 26299</a>] [meta][X86] Size optimization opportunities (in particular for 32-bit Chromium on Windows)
              </li>
          </ul>
        </div>
        <br>

      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>