<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - InstCombine misses C0-(C1-X) optimization"
href="https://bugs.llvm.org/show_bug.cgi?id=47997">47997</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>InstCombine misses C0-(C1-X) optimization
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</td>
</tr>
<tr>
<th>Version</th>
<td>11.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>All
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Scalar Optimizations
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>bruno-llvm@defraine.net
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>We are seeing a performance regression in LLVM-11. The optimizer leaves
redundant C0-(C1-X) instructions, where previously this was combined to
X+(C0-C1).
Reduced C++ case:
extern int x;
extern int y;
bool test(int a) {
x = a - 1;
y = 1 - a;
return x == -y;
}
clang-10 output with -O2:
define dso_local zeroext i1 @_Z4testi(i32 %0) local_unnamed_addr #0 {
%2 = add nsw i32 %0, -1
store i32 %2, i32* @x, align 4, !tbaa !2
%3 = sub nsw i32 1, %0
store i32 %3, i32* @y, align 4, !tbaa !2
ret i1 true
}
clang-11 output with -O2:
define dso_local zeroext i1 @_Z4testi(i32 %0) local_unnamed_addr #0 {
%2 = add nsw i32 %0, -1
store i32 %2, i32* @x, align 4, !tbaa !2
%3 = sub nsw i32 1, %0
store i32 %3, i32* @y, align 4, !tbaa !2
%4 = sub nsw i32 0, %3
%5 = icmp eq i32 %2, %4
ret i1 %5
}
Godbolt link: <a href="https://godbolt.org/z/Y94rnE">https://godbolt.org/z/Y94rnE</a>
In the LLVM-11 outut, %4 should be optimized to (add i32 %0, -1) such that %5
can be optimized to true.
Analysis:
This regression was introduced by <a href="https://reviews.llvm.org/D68408">https://reviews.llvm.org/D68408</a>
The issue seems to be in InstCombiner::visitSub; an expression C0-(C1-X)
matches the IsNegation path, but is not handled by Negator::Negate.
Because this path ends with a return:
if (IsNegation)
return TryToNarrowDeduceFlags(); // Should have been handled in Negator!
Further optimizations from InstCombiner::visitSub are not attempted.
Either this should be fixed in Negator::Negate, or the code should leave
opportunity for other optimizations by doing:
if (IsNegation)
if (auto *I = TryToNarrowDeduceFlags())
return I; // Should have been handled in Negator!</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>