<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 produces better code in general case than in particular case with constant"
href="https://bugs.llvm.org/show_bug.cgi?id=37147">37147</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>InstCombine produces better code in general case than in particular case with constant
</td>
</tr>
<tr>
<th>Product</th>
<td>new-bugs
</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>new bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@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>Run "opt -instcombine -S" on the following example with simple three-way
comparison:
declare void @foo(i32 %x)
define i32 @compare_against_arbitrary_value(i32 %x, i32 %c) {
entry:
%cmp1 = icmp eq i32 %x, %c
%cmp2 = icmp slt i32 %x, %c
%select1 = select i1 %cmp2, i32 -1, i32 1
%select2 = select i1 %cmp1, i32 0, i32 %select1
%cond = icmp sgt i32 %select2, 0
br i1 %cond, label %callfoo, label %exit
callfoo:
call void @foo(i32 %select2)
br label %exit
exit:
ret i32 42
}
define i32 @compare_against_zero(i32 %x) {
entry:
%cmp1 = icmp eq i32 %x, 0
%cmp2 = icmp slt i32 %x, 0
%select1 = select i1 %cmp2, i32 -1, i32 1
%select2 = select i1 %cmp1, i32 0, i32 %select1
%cond = icmp sgt i32 %select2, 0
br i1 %cond, label %callfoo, label %exit
callfoo:
call void @foo(i32 %select2)
br label %exit
exit:
ret i32 42
}
As we see, the second example if just a particular case of the first one with
%c known to be 0. For first test, the smart code is generated:
define i32 @compare_against_arbitrary_value(i32 %x, i32 %c) {
entry:
%0 = icmp sgt i32 %x, %c
br i1 %0, label %callfoo, label %exit
callfoo: ; preds = %entry
%cmp1 = icmp ne i32 %x, %c
%select2 = zext i1 %cmp1 to i32
call void @foo(i32 %select2)
br label %exit
exit: ; preds = %callfoo, %entry
ret i32 42
}
However for the second case, it generates something weird:
define i32 @compare_against_zero(i32 %x) {
entry:
%cmp1 = icmp eq i32 %x, 0
%0 = ashr i32 %x, 31
%1 = or i32 %0, 1
%select2 = select i1 %cmp1, i32 0, i32 %1
%cond = icmp sgt i32 %select2, 0
br i1 %cond, label %callfoo, label %exit
callfoo: ; preds = %entry
call void @foo(i32 %select2)
br label %exit
exit: ; preds = %callfoo, %entry
ret i32 42
}
This strange pattern is generated within the function foldSelectInstWithICmp.
If we forcefully disable this transform, the code is neat:
define i32 @compare_against_zero(i32 %x) {
entry:
%0 = icmp sgt i32 %x, 0
br i1 %0, label %callfoo, label %exit
callfoo: ; preds = %entry
call void @foo(i32 1)
br label %exit
exit: ; preds = %callfoo, %entry
ret i32 42
}
So it seems that some particular case handling discourages the further
transformations which ends up with worse code.</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>