[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


            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:

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

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_def_cfa_offset 8
        .p2align 4,,10
        .p2align 3
        call    strlen
        addq    $8, %rsp
        .cfi_def_cfa_offset 8
        .p2align 4,,1

I'm not sure how important this is. I only caught it trying to test something

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