https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98334
Bug ID: 98334
Summary: Failure to optimally optimize add loop to mul
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: gabravier at gmail dot com
Target Milestone: ---
int f(int i, unsigned int n) {
int result = 0;
while (n > 0) {
result += i;
n -= 1;
}
return result;
}
GCC optimizes this function to code that effectively does `return (n == 0) ? 0
: (i - 1) * n + n;`. It could instead emit the more optimal `return (n == 0) ?
0 : i * n;`, or just `return i * n;` on platforms where multiplication is fast.
This transformation is done by LLVM, but not by GCC.