<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 - Failure to remove redundant check"
   href="https://bugs.llvm.org/show_bug.cgi?id=34282">34282</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Failure to remove redundant check
          </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>Windows NT
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>aivchenk@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>For the following function:
int foo(char* C,  int a, int b, int c, int d, int e, int f) {
  int y1 = C[0] ? a : b;
  int y2 = C[0] || C[1] ? c : d;
  int y3 = C[0] || (C[1] && C[2]) ? e : f;
  return C[0] ? y1 + y2 : C[1] ? y2 : y3;
}
<span class="quote">> </span >
.. we generate bad code:

# BB#0:
        cmpb    $0, (%rdi)
        je      .LBB0_1
# BB#5:
        addl    %esi, %ecx
        movl    %ecx, %eax
        retq
.LBB0_1:
        movl    8(%rsp), %eax
        movb    1(%rdi), %dl
        testb   %dl, %dl
        je      .LBB0_3
# BB#2:
        cmpb    $0, 2(%rdi)
        cmovnel %r9d, %eax
.LBB0_3:
        testb   %dl, %dl
        cmovel  %eax, %ecx
        movl    %ecx, %eax
        retq

Just for comparison, here is GCC version:
        cmpb    $0, (%rdi)                                                      
        leal    (%rsi,%rcx), %eax                                               
        jne     .L1                                                             
        cmpb    $0, 1(%rdi)                                                     
        movl    %ecx, %eax                                                      
        cmove   8(%rsp), %eax                                                   
.L1:                                                                            
        rep ret

The main problem with LLVM code is that we do redundant "cmpb    $0, 2(%rdi)"
check (while GCC removed that): C[2] does not influence the function return
value. As far as I can say, all other problems with our code, like doing testb
twice for cmov condition, are the result of not deleting the C[2] check</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>