[LLVMbugs] [Bug 19903] New: Missed strlen==0 optimization

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri May 30 15:04:25 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=19903

            Bug ID: 19903
           Summary: Missed strlen==0 optimization
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: dnovillo at google.com
                CC: dblaikie at gmail.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140530/ed30f91e/attachment.html>


More information about the llvm-bugs mailing list