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: unassignedb...@nondot.org
          Reporter: eyal...@technion.ac.il
                CC: llvm-bugs@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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to