[llvm-bugs] [Bug 43962] New: Unnecessary memory read in a loop

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Nov 11 05:51:08 PST 2019


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

            Bug ID: 43962
           Summary: Unnecessary memory read in a loop
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: antoshkka at gmail.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

Consider the example:

int* f2(int** x) {
    int** max = x;
    for (int i =0 ; i < 5; ++ i) {
        ++ x;
        if (**max < **x) {
            max = x;
        }
    }
    return *max;
}

Clang with '-O2 -fno-unroll-loops' generates the following assembly:

f2(int**): # @f2(int**)
  mov eax, 8
  mov rcx, rdi
  jmp .LBB0_1
.LBB0_3: # in Loop: Header=BB0_1 Depth=1
  add rax, 8
  cmp eax, 48
  je .LBB0_4
.LBB0_1: # =>This Inner Loop Header: Depth=1
  mov rdx, qword ptr [rcx]
  mov edx, dword ptr [rdx]
  mov rsi, qword ptr [rdi + rax]
  cmp edx, dword ptr [rsi]
  jge .LBB0_3
  lea rcx, [rdi + rax]
  jmp .LBB0_3
.LBB0_4:
  mov rax, qword ptr [rcx]
  ret

That assembly has 4 memory reads in a loop.

However other compilers generate loops with only 3 reads, for example:
f2(int**):
  mov rax, QWORD PTR [rdi]
  lea rcx, [rdi+40]
.L3:
  mov rdx, QWORD PTR [rdi+8]
  add rdi, 8
  mov esi, DWORD PTR [rdx]
  cmp DWORD PTR [rax], esi
  cmovl rax, rdx
  cmp rcx, rdi
  jne .L3
  ret

Please improve the memory accesses.
Godbolt playground: https://godbolt.org/z/F-dJSX

-- 
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/20191111/27c9cec5/attachment-0001.html>


More information about the llvm-bugs mailing list