[llvm-bugs] [Bug 38554] New: Loop optimization makes code bloat when moving conditionally called pure functions outside of loop
via llvm-bugs
llvm-bugs at lists.llvm.org
Tue Aug 14 01:40:32 PDT 2018
https://bugs.llvm.org/show_bug.cgi?id=38554
Bug ID: 38554
Summary: Loop optimization makes code bloat when moving
conditionally called pure functions outside of loop
Product: libraries
Version: 6.0
Hardware: PC
OS: All
Status: NEW
Severity: enhancement
Priority: P
Component: Loop Optimizer
Assignee: unassignedbugs at nondot.org
Reporter: o_kniemeyer at maxon.de
CC: llvm-bugs at lists.llvm.org
If you compile this code snippet at https://gcc.godbolt.org with -O3
int Func(const void*) __attribute__ ((pure));
int Test(int n, const void* ptr2)
{
const void* ptr = ptr2;
int result = 0;
for (int i = 0; i < n; ++i)
{
#if 1
result += Func(ptr);
#else
result += ptr ? Func(ptr) : 0;
#endif
}
return result;
}
you get the expected result:
Test(int, void const*): # @Test(int, void const*)
pushq %rbx
movl %edi, %ebx
testl %ebx, %ebx
jle .LBB0_1
movq %rsi, %rdi
callq Func(void const*)
imull %ebx, %eax
popq %rbx
retq
.LBB0_1:
xorl %eax, %eax
popq %rbx
retq
So the optimizer has moved the call to Func outside of the loop, and there's
just a multiplication with the loop count. If you use the code with nullptr
check (#if 0) the call is also moved to the outside, but the multiplication is
replaced by code bloat:
Test(int, void const*): # @Test(int, void const*)
pushq %rbx
movl %edi, %ebx
xorl %ecx, %ecx
testl %ebx, %ebx
jle .LBB0_13
testq %rsi, %rsi
je .LBB0_13
movq %rsi, %rid
callq Func(void const*)
xorl %edx, %edx
movl $0, %ecx
cmpl $7, %ebx
jbe .LBB0_3
movl %ebx, %edx
andl $-8, %edx
movd %eax, %xmm0
pshufd $0, %xmm0, %xmm0 # xmm0 = xmm0[0,0,0,0]
leal -8(%rdx), %esi
movl %esi, %edi
shrl $3, %edi
leal 1(%rdi), %ecx
andl $7, %ecx
cmpl $56, %esi
jae .LBB0_7
pxor %xmm1, %xmm1
pxor %xmm2, %xmm2
testl %ecx, %ecx
jne .LBB0_10
jmp .LBB0_12
.LBB0_7:
leal -1(%rcx), %esi
subl %edi, %esi
pxor %xmm1, %xmm1
pxor %xmm2, %xmm2
.LBB0_8: # =>This Inner Loop Header: Depth=1
paddd %xmm0, %xmm1
paddd %xmm0, %xmm2
paddd %xmm0, %xmm1
paddd %xmm0, %xmm2
paddd %xmm0, %xmm1
paddd %xmm0, %xmm2
paddd %xmm0, %xmm1
paddd %xmm0, %xmm2
paddd %xmm0, %xmm1
paddd %xmm0, %xmm2
paddd %xmm0, %xmm1
paddd %xmm0, %xmm2
paddd %xmm0, %xmm1
paddd %xmm0, %xmm2
paddd %xmm0, %xmm1
paddd %xmm0, %xmm2
addl $8, %esi
jne .LBB0_8
testl %ecx, %ecx
je .LBB0_12
.LBB0_10:
negl %ecx
.LBB0_11: # =>This Inner Loop Header: Depth=1
paddd %xmm0, %xmm1
paddd %xmm0, %xmm2
addl $1, %ecx
jne .LBB0_11
.LBB0_12:
paddd %xmm2, %xmm1
pshufd $78, %xmm1, %xmm0 # xmm0 = xmm1[2,3,0,1]
paddd %xmm1, %xmm0
pshufd $229, %xmm0, %xmm1 # xmm1 = xmm0[1,1,2,3]
paddd %xmm0, %xmm1
movd %xmm1, %ecx
cmpl %ebx, %edx
je .LBB0_13
.LBB0_3:
subl %edx, %ebx
.LBB0_4: # =>This Inner Loop Header: Depth=1
addl %eax, %ecx
addl $-1, %ebx
jne .LBB0_4
.LBB0_13:
movl %ecx, %eax
popq %rbx
retq
Adding -fno-vectorize helps a bit, but I doubt that the code is faster than
imull.
--
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/20180814/55f64aaf/attachment.html>
More information about the llvm-bugs
mailing list