[llvm-bugs] [Bug 36135] New: Missed optimization: loop-invariant strlen() not hoisted out of loop

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Jan 29 06:59:25 PST 2018


https://bugs.llvm.org/show_bug.cgi?id=36135

            Bug ID: 36135
           Summary: Missed optimization: loop-invariant strlen() not
                    hoisted out of loop
           Product: compiler-rt
           Version: 5.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: compiler-rt
          Assignee: unassignedbugs at nondot.org
          Reporter: eyalroz at technion.ac.il
                CC: llvm-bugs at lists.llvm.org

Consider the following code:

#include <string.h>

void bar(char c);

void foo(const char* __restrict__ ss) 
{
    for (int i = 0; i < strlen(ss); ++i) 
    {
        bar(*ss);
    }
}

To my understanding, the fact that ss is __restrict__ed, combined with the fact
that it isn't written through (or that it's a const pointer) should be
sufficient to allow the compiler to assume the memory accessible via ss remains
constant, and thus assume that strlen(ss) will return the same value for all
loop iterations.

But - that's not what happens (with clang 5.0):

.LBB0_2: # =>This Inner Loop Header: Depth=1
  mov edi, ebp
  call bar(char)
  inc rbx
  mov rdi, r14
  call strlen
  cmp rax, rbx
  ja .LBB0_2

(obtained with https://godbolt.org/g/vdGSBe )

Now, I'm no compiler expert, so maybe there are considerations I'm ignoring,
but it seems to me the compiler should be able to hoist the heavier code up
above the loop.

Cf. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84083 and
https://stackoverflow.com/q/48482003/1593077

-- 
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/20180129/ee2a52c0/attachment.html>


More information about the llvm-bugs mailing list