[llvm-bugs] [Bug 27871] New: Problematic loop-carried variable widening by InstCombine

via llvm-bugs llvm-bugs at lists.llvm.org
Tue May 24 18:19:20 PDT 2016


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

            Bug ID: 27871
           Summary: Problematic loop-carried variable widening by
                    InstCombine
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: me at manueljacob.de
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

Given the following C code:

int test(size_t n, char *a) {
  char sum = 0;
  for (size_t i = 0; i < n; ++i)
    sum += a[i];
  return sum;
}

After running a few simplifications (SROA, SimplifyCFG, LoopRotate) the IR
looks like this:

define i32 @test(i64 %n, i8* %a) #0 {
entry:
  %cmp4 = icmp ult i64 0, %n
  br i1 %cmp4, label %for.body, label %for.end

for.body:                                         ; preds = %entry, %for.body
  %i.06 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
  %sum.05 = phi i8 [ %conv2, %for.body ], [ 0, %entry ]
  %arrayidx = getelementptr inbounds i8, i8* %a, i64 %i.06
  %0 = load i8, i8* %arrayidx, align 1
  %conv = sext i8 %0 to i32
  %conv1 = sext i8 %sum.05 to i32
  %add = add nsw i32 %conv1, %conv
  %conv2 = trunc i32 %add to i8
  %inc = add i64 %i.06, 1
  %cmp = icmp ult i64 %inc, %n
  br i1 %cmp, label %for.body, label %for.end

for.end:                                          ; preds = %for.body, %entry
  %sum.0.lcssa = phi i8 [ 0, %entry ], [ %conv2, %for.body ]
  %conv3 = sext i8 %sum.0.lcssa to i32
  ret i32 %conv3
}

I expected that running InstCombine produces code similar to this (transforming
the sext, sext, add, trunc chain to a single add instruction):

define i32 @test(i64 %n, i8* %a) #0 {
entry:
  %cmp4 = icmp ult i64 0, %n
  br i1 %cmp4, label %for.body, label %for.end

for.body:                                         ; preds = %entry, %for.body
  %i.06 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
  %sum.05 = phi i8 [ %add, %for.body ], [ 0, %entry ]
  %arrayidx = getelementptr inbounds i8, i8* %a, i64 %i.06
  %0 = load i8, i8* %arrayidx, align 1
  %add = add nsw i8 %sum.05, %0
  %inc = add i64 %i.06, 1
  %cmp = icmp ult i64 %inc, %n
  br i1 %cmp, label %for.body, label %for.end

for.end:                                          ; preds = %for.body, %entry
  %sum.0.lcssa = phi i8 [ 0, %entry ], [ %add, %for.body ]
  %conv3 = sext i8 %sum.0.lcssa to i32
  ret i32 %conv3
}

However instead InstCombine widened %sum.05 to i32 type, adding several shifts
to fix up the width:

define i32 @test(i64 %n, i8* %a) #0 {
entry:
  %cmp4 = icmp eq i64 %n, 0
  br i1 %cmp4, label %for.end, label %for.body

for.body:                                         ; preds = %entry, %for.body
  %i.06 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
  %sum.05 = phi i32 [ %add, %for.body ], [ 0, %entry ]
  %arrayidx = getelementptr inbounds i8, i8* %a, i64 %i.06
  %0 = load i8, i8* %arrayidx, align 1
  %conv = sext i8 %0 to i32
  %sext7 = shl i32 %sum.05, 24
  %conv1 = ashr exact i32 %sext7, 24
  %add = add nsw i32 %conv1, %conv
  %inc = add i64 %i.06, 1
  %cmp = icmp ult i64 %inc, %n
  br i1 %cmp, label %for.body, label %for.end

for.end:                                          ; preds = %entry, %for.body
  %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ]
  %sext = shl i32 %sum.0.lcssa, 24
  %conv3 = ashr exact i32 %sext, 24
  ret i32 %conv3
}

Running opt -O3 again on the IR doesn't improve the code.  Instead it generates
very awful code.  I've attached it for reference, but there seems to be an
unrelated problem involved.

-- 
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/20160525/c53ae95b/attachment.html>


More information about the llvm-bugs mailing list