<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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] missed chance to turn select (cmov) into logic ops"
   href="https://llvm.org/bugs/show_bug.cgi?id=28968">28968</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[x86] missed chance to turn select (cmov) into logic ops
          </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>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>spatel+llvm@rotateright.com
          </td>
        </tr>

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

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Forking this off of <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - [SimplifyCFG] failure to sink common function call below if/else"
   href="show_bug.cgi?id=28964">bug 28964</a>.

Notice that:
int goo(int x) {
  if (x == 1)
    x = 1;
  else
    x = -1;
  return x;
}

Could be rewritten as:
int hoo(int x) {
  if (x != 1)
    x = -1;
  return x;
}

Or as IR:

define i32 @hoo(i32 %x) {
  %cmp = icmp ne i32 %x, 1
  %sel = select i1 %cmp, i32 -1, i32 1
  ret i32 %sel
}

$ ./llc -o - cmpsel.ll 
    cmpl    $1, %edi
    movl    $-1, %eax
    cmovel    %edi, %eax
    retq


According to godbolt ( <a href="https://godbolt.org/g/TSTeIo">https://godbolt.org/g/TSTeIo</a> ), gcc learned to avoid a
cmov on this at v6.1:

foo(int):
        xorl    %eax, %eax
        cmpl    $1, %edi
        setne   %al
        negl    %eax
        orl     $1, %eax
        ret

Note that gcc does this at -O2 but not -O1 or -Os.

Filing this as an x86 bug for now, but it might be useful for other targets
too.</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>