<html>
<head>
<base href="https://llvm.org/bugs/" />
</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 --- - Problematic loop-carried variable widening by InstCombine"
href="https://llvm.org/bugs/show_bug.cgi?id=27871">27871</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Problematic loop-carried variable widening by InstCombine
</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>Linux
</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>me@manueljacob.de
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>Given the following C code:
int test(size_t n, char *a) {
char sum = 0;
for (size_t i = 0; i < n; ++i)
sum += a[i];
return sum;
}
After running a few simplifications (SROA, SimplifyCFG, LoopRotate) the IR
looks like this:
define i32 @test(i64 %n, i8* %a) #0 {
entry:
%cmp4 = icmp ult i64 0, %n
br i1 %cmp4, label %for.body, label %for.end
for.body: ; preds = %entry, %for.body
%i.06 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
%sum.05 = phi i8 [ %conv2, %for.body ], [ 0, %entry ]
%arrayidx = getelementptr inbounds i8, i8* %a, i64 %i.06
%0 = load i8, i8* %arrayidx, align 1
%conv = sext i8 %0 to i32
%conv1 = sext i8 %sum.05 to i32
%add = add nsw i32 %conv1, %conv
%conv2 = trunc i32 %add to i8
%inc = add i64 %i.06, 1
%cmp = icmp ult i64 %inc, %n
br i1 %cmp, label %for.body, label %for.end
for.end: ; preds = %for.body, %entry
%sum.0.lcssa = phi i8 [ 0, %entry ], [ %conv2, %for.body ]
%conv3 = sext i8 %sum.0.lcssa to i32
ret i32 %conv3
}
I expected that running InstCombine produces code similar to this (transforming
the sext, sext, add, trunc chain to a single add instruction):
define i32 @test(i64 %n, i8* %a) #0 {
entry:
%cmp4 = icmp ult i64 0, %n
br i1 %cmp4, label %for.body, label %for.end
for.body: ; preds = %entry, %for.body
%i.06 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
%sum.05 = phi i8 [ %add, %for.body ], [ 0, %entry ]
%arrayidx = getelementptr inbounds i8, i8* %a, i64 %i.06
%0 = load i8, i8* %arrayidx, align 1
%add = add nsw i8 %sum.05, %0
%inc = add i64 %i.06, 1
%cmp = icmp ult i64 %inc, %n
br i1 %cmp, label %for.body, label %for.end
for.end: ; preds = %for.body, %entry
%sum.0.lcssa = phi i8 [ 0, %entry ], [ %add, %for.body ]
%conv3 = sext i8 %sum.0.lcssa to i32
ret i32 %conv3
}
However instead InstCombine widened %sum.05 to i32 type, adding several shifts
to fix up the width:
define i32 @test(i64 %n, i8* %a) #0 {
entry:
%cmp4 = icmp eq i64 %n, 0
br i1 %cmp4, label %for.end, label %for.body
for.body: ; preds = %entry, %for.body
%i.06 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
%sum.05 = phi i32 [ %add, %for.body ], [ 0, %entry ]
%arrayidx = getelementptr inbounds i8, i8* %a, i64 %i.06
%0 = load i8, i8* %arrayidx, align 1
%conv = sext i8 %0 to i32
%sext7 = shl i32 %sum.05, 24
%conv1 = ashr exact i32 %sext7, 24
%add = add nsw i32 %conv1, %conv
%inc = add i64 %i.06, 1
%cmp = icmp ult i64 %inc, %n
br i1 %cmp, label %for.body, label %for.end
for.end: ; preds = %entry, %for.body
%sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ]
%sext = shl i32 %sum.0.lcssa, 24
%conv3 = ashr exact i32 %sext, 24
ret i32 %conv3
}
Running opt -O3 again on the IR doesn't improve the code. Instead it generates
very awful code. I've attached it for reference, but there seems to be an
unrelated problem involved.</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>