This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/86923] New: Missed optimization performing consecutive integer sum, loop not removed
- From: "nightstrike at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sun, 12 Aug 2018 18:12:52 +0000
- Subject: [Bug c/86923] New: Missed optimization performing consecutive integer sum, loop not removed
- Auto-submitted: auto-generated
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86923
Bug ID: 86923
Summary: Missed optimization performing consecutive integer
sum, loop not removed
Product: gcc
Version: 9.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: nightstrike at gmail dot com
Target Milestone: ---
unsigned long f(unsigned int n) {
unsigned long total = 0;
for (unsigned int i = 0; i < n; ++i)
total += i;
return total;
}
It may not be terribly useful to report this, as I can (and arguably should)
write n(n-1)/2 manually, but clang does successfully transform this into:
lea eax, [rdi - 1]
add edi, -2
imul rdi, rax
shr rdi
add rdi, rax
whereas GCC tries instead to vectorize and process the loop as written.
Obviously, O(1) is preferable to O(n) here.
This applies to both C and C++.