[llvm-bugs] [Bug 31302] New: We don't get rid of a loop calling strlen()

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Dec 6 19:07:29 PST 2016


            Bug ID: 31302
           Summary: We don't get rid of a loop calling strlen()
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: sanjoy at playingwithpointers.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

Given this program:

unsigned long strlen(const char *str);

int baz(const char *s) {
  int acc = 0;
  for (int i = 0; i < 100; ++i)
    acc += strlen(s);
  return acc;

clang as of 288619 produces this output on -O3:

define i32 @baz(i8* nocapture readonly %s) local_unnamed_addr #0 {
  %call = tail call i64 @strlen(i8* %s)
  br label %for.body

for.cond.cleanup:                                 ; preds = %for.body
  %conv1 = trunc i64 %add.4 to i32
  ret i32 %conv1

for.body:                                         ; preds = %for.body, %entry
  %i.08 = phi i32 [ 0, %entry ], [ %inc.4, %for.body ]
  %acc.07 = phi i64 [ 0, %entry ], [ %add.4, %for.body ]
  %add = add i64 %call, %acc.07
  %add.1 = add i64 %call, %add
  %add.2 = add i64 %call, %add.1
  %add.3 = add i64 %call, %add.2
  %conv6.4 = and i64 %add.3, 4294967295
  %add.4 = add i64 %call, %conv6.4
  %inc.4 = add nsw i32 %i.08, 5
  %exitcond.4 = icmp eq i32 %inc.4, 100
  br i1 %exitcond.4, label %for.cond.cleanup, label %for.body

which is pretty bad.  Passing the above IR through opt -O3 replaces the loop
with a multiplication instruction, so this may be a pass ordering issue.

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/20161207/fd5e1836/attachment-0001.html>

More information about the llvm-bugs mailing list