<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 opportunity to reduce number of loop iterations?"
href="https://bugs.llvm.org/show_bug.cgi?id=39702">39702</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Missed opportunity to reduce number of loop iterations?
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</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>Loop Optimizer
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>vsk@apple.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>In llvm, there's plenty of hot code that looks like this:
```
if (pred_size(BB) != 2)
return false; // From InstCombiner::mergeStoreIntoSuccessor.
```
Here, pred_size takes linear time, but there's never a need to visit more than
two predecessors. In a stage2 Release build of llc, we do *45 million*
unnecessary linked list iterations just at this one call site [1].
Is there a way for the optimizer to recognize that no more than two iterations
are needed?
Reduced example (<a href="https://godbolt.org/z/TOtQD6">https://godbolt.org/z/TOtQD6</a>):
```
struct linked_list {
int x = 0;
linked_list *next = nullptr;
};
int size(linked_list *ll) {
int s = 0;
while (ll) {
++s;
ll = ll->next;
}
return s;
}
int no_more_than_two_iterations(linked_list *ll) {
int s = size(ll);
if (s != 2)
return 0;
return 1;
}
```
Currently we produce:
```
define dso_local i32
@_Z27no_more_than_two_iterationsP11linked_list(%struct.linked_list* readonly)
local_unnamed_addr #0 {
%2 = icmp eq %struct.linked_list* %0, null
br i1 %2, label %13, label %3
; <label>:3: ; preds = %1, %3
%4 = phi i32 [ %6, %3 ], [ 0, %1 ]
%5 = phi %struct.linked_list* [ %8, %3 ], [ %0, %1 ]
%6 = add nuw nsw i32 %4, 1
%7 = getelementptr inbounds %struct.linked_list, %struct.linked_list* %5, i64
0, i32 1
%8 = load %struct.linked_list*, %struct.linked_list** %7, align 8, !tbaa !2
%9 = icmp eq %struct.linked_list* %8, null
br i1 %9, label %10, label %3
; <label>:10: ; preds = %3
%11 = icmp eq i32 %6, 2
%12 = zext i1 %11 to i32
ret i32 %12
; <label>:13: ; preds = %1
ret i32 0
}
```
Could we somehow make the loop exit early if the sum ever exceeds 2? Like:
```
%9 = icmp eq %struct.linked_list* %8, null
%enough-seen = icmp ugt i32 %6, 2
%early-exit = or i1 %9, %enough-seen
br i1 %early-exit, label %10, label %3
```
[1] I defined 'wasted iterations' as `pred_size(BB) - 2`, whenever that
difference would be positive. I did the stage2 build with r347179.</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>