<html>
    <head>
      <base href="http://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 --- - Missed strlen==0 optimization"
   href="http://llvm.org/bugs/show_bug.cgi?id=19903">19903</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed strlen==0 optimization
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </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>normal
          </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>dnovillo@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dblaikie@gmail.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The optimizer will generally optimize a strlen==0 comparison, but under certain
circumstances, it will miss the opportunity:

int foo(const char *s) {
  if (strlen(s) == 0) {
    return 42;
  }
  return strlen(s);
}

Instead of calling strlen on the else branch, LLVM will float the call to
strlen at the top:

.Ltmp0: 
        .cfi_def_cfa_offset 16
        callq   strlen
        testq   %rax, %rax
        movl    $42, %ecx
        cmovnel %eax, %ecx
        movl    %ecx, %eax
        popq    %rdx
        retq

Whereas GCC 4.6 will first avoid calling strlen until it really needs to:

        .cfi_def_cfa_offset 16
        cmpb    $0, (%rdi)
        movl    $42, %eax
        jne     .L5
        addq    $8, %rsp
        .cfi_remember_state
        .cfi_def_cfa_offset 8
        ret
        .p2align 4,,10
        .p2align 3
.L5:
        .cfi_restore_state
        call    strlen
        addq    $8, %rsp
        .cfi_def_cfa_offset 8
        .p2align 4,,1
        ret

I'm not sure how important this is. I only caught it trying to test something
else.</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>