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