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

            Bug ID: 28090
           Summary: LLVM generates terrible x86 code for trivial, fully
                    unrolled loops
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Backend: X86
          Assignee: unassignedb...@nondot.org
          Reporter: chandl...@gmail.com
                CC: llvm-bugs@lists.llvm.org
    Classification: Unclassified

Consider this code:

----
struct V {
  static constexpr int length = 32;
  unsigned short data[32];
};

int reduce(V &v) {
  int sum = 0;
  for (int i = 0; i < v.length; ++i) {
    sum += static_cast<int>(v.data[i]);
  }
  return sum;
}
----

If the length weren't a constant, LLVM would do a delightful job of vectorizing
the reduction loop. But because it happens to be a constant trip count, we
fully unroll the loop and generate this mess:

----
% ./bin/clang++ -std=c++1z -c -S -o - -O2 -march=haswell x.cpp
        .text
        .file   "x.cpp"
        .globl  _Z6reduceR1V
        .p2align        4, 0x90
        .type   _Z6reduceR1V,@function
_Z6reduceR1V:                           # @_Z6reduceR1V
        .cfi_startproc
# BB#0:                                 # %entry
        movzwl  (%rdi), %eax
        movzwl  2(%rdi), %ecx
        addl    %eax, %ecx
        movzwl  4(%rdi), %eax
        addl    %ecx, %eax
        ....
        ; repeat OVER AND OVER AGAIN with minor variations in registers...
        ....
        movzwl  60(%rdi), %edx
        addl    %ecx, %edx
        movzwl  62(%rdi), %eax
        addl    %edx, %eax
        retq
----

Ow. This hurts code size as well. =/ I figure we need reduction support in the
SLP vectorizer or some such?

-- 
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