<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 - Missed optimization opportunity due to select"
href="https://bugs.llvm.org/show_bug.cgi?id=37985">37985</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Missed optimization opportunity due to select
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Windows NT
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>LLVM Codegen
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>max.kazantsev@azul.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>Given example:
const long long N = 100;
long long z = 2325;
long long foo(long long a, long long b) {
return (a + b) * (a + b);
}
long long test() {
long long sum = 0;
for (long long i = 0; i < N; i++) {
for (long long j = 0; j < N; j++) {
sum += (i > 0); // [1]
sum += j;
long long a = z;
long b = i * j;
sum += (-a * b) + foo(a, b) - b * a;
}
}
return sum;
}
Compile with clang -O3 -emit-llvm -g0
define dso_local i64 @_Z4testv() local_unnamed_addr #1 {
%1 = load i64, i64* @z, align 8, !tbaa !2
%2 = mul i64 %1, %1
%3 = mul i64 %2, 100
%4 = add i64 %3, 4950
br label %5
%6 = phi i64 [ %4, %0 ], [ %20, %5 ]
%7 = phi i64 [ 328350, %0 ], [ %19, %5 ]
%8 = phi i64 [ 0, %0 ], [ %17, %5 ]
%9 = phi i64 [ 0, %0 ], [ %18, %5 ]
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i64 0, i64 100
%12 = add i64 %8, %6
%13 = add i64 %12, %11
%14 = add nuw nsw i64 %7, 656700
%15 = add i64 %6, %7
%16 = add i64 %13, %15
%17 = add i64 %16, 100
%18 = add nuw nsw i64 %9, 2
%19 = add nuw nsw i64 %7, 1313400
%20 = add i64 %15, %14
%21 = icmp eq i64 %18, 100
br i1 %21, label %22, label %5
ret i64 %17
}
Clang in unable to constant-fold calculations in this case: the code contains a
loop. However if we comment away line [1], the code becomes folded:
If we comment away [1]:
define dso_local i64 @_Z4testv() local_unnamed_addr #1 {
%1 = load i64, i64* @z, align 8, !tbaa !2
%2 = mul i64 %1, %1
%3 = mul i64 %2, 10000
%4 = add i64 %3, 107814217500
ret i64 %4
}
To me it looks like a failure of peeling heuristic: if we could peel away 1st
iteration of the inner loop, we could optimize the entire thing away.</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>