[llvm-bugs] [Bug 49024] New: Failure to optimize add loop in -O3 as well as in -Oz
via llvm-bugs
llvm-bugs at lists.llvm.org
Wed Feb 3 12:08:58 PST 2021
https://bugs.llvm.org/show_bug.cgi?id=49024
Bug ID: 49024
Summary: Failure to optimize add loop in -O3 as well as in -Oz
Product: libraries
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: Scalar Optimizations
Assignee: unassignedbugs at nondot.org
Reporter: gabravier at gmail.com
CC: llvm-bugs at lists.llvm.org
int f(uint8_t max_value) {
int sum = 0;
for (int i = 0; i < max_value; i++) {
sum += i;
}
return sum;
}
This can be optimized to `return (((unsigned _ExtInt(33))max_value) * (unsigned
_ExtInt(33))((uint32_t)max_value - 1)) >> 1;`. This transformation is done
under `-Oz`, but `-O3` seems to generate far inferior code, as the IR shows:
define dso_local i32 @_Z1fh(i8 zeroext %0) local_unnamed_addr #0 {
%2 = zext i8 %0 to i32
%3 = icmp eq i8 %0, 0
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = add nsw i32 %2, -1
%6 = zext i32 %5 to i33
%7 = add nsw i32 %2, -2
%8 = zext i32 %7 to i33
%9 = mul i33 %6, %8
%10 = lshr i33 %9, 1
%11 = trunc i33 %10 to i32
%12 = add i32 %2, %11
%13 = add i32 %12, -1
br label %14
14: ; preds = %4, %1
%15 = phi i32 [ 0, %1 ], [ %13, %4 ]
ret i32 %15
}
Compared to the far simpler and faster code generated on -Oz:
define dso_local i32 @_Z1fh(i8 zeroext %0) local_unnamed_addr #0 {
%2 = zext i8 %0 to i32
%3 = zext i8 %0 to i33
%4 = add nsw i32 %2, -1
%5 = zext i32 %4 to i33
%6 = mul i33 %3, %5
%7 = lshr i33 %6, 1
%8 = trunc i33 %7 to i32
ret i32 %8
}
Godbolt comparison: https://godbolt.org/z/vW5jKz
alive2 comparison: https://alive2.llvm.org/ce/z/NVhmdm
--
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/20210203/5da96e8d/attachment.html>
More information about the llvm-bugs
mailing list