<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 - Improve IR for code which finds position of highest bit"
href="https://bugs.llvm.org/show_bug.cgi?id=44126">44126</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Improve IR for code which finds position of highest bit
</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>enhancement
</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>david.bolvansky@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre><a href="https://github.com/facebook/zstd/blob/47034cd6c31125fdba3155abe9a618f580b4f3eb/programs/fileio.c#L1789">https://github.com/facebook/zstd/blob/47034cd6c31125fdba3155abe9a618f580b4f3eb/programs/fileio.c#L1789</a>
unsigned long long FIO_highbit64(unsigned long long v)
{
unsigned count = 0;
v >>= 1;
while (v) { v >>= 1; count++; }
return count;
}
should be same as:
unsigned long long FIO_highbit64a(unsigned long long v)
{
return 63 - __builtin_clzll(v);
}
But first version has worse IR and codegen:
define dso_local i64 @_Z13FIO_highbit64y(i64 %0) local_unnamed_addr #0 {
%2 = lshr i64 %0, 1
%3 = call i64 @llvm.ctlz.i64(i64 %2, i1 false), !range !2
%4 = sub nuw nsw i64 64, %3
ret i64 %4
}
=>
define dso_local i64 @_Z14FIO_highbit64ay(i64 %0) local_unnamed_addr #1 {
%2 = tail call i64 @llvm.ctlz.i64(i64 %0, i1 true), !range !3
%3 = xor i64 %2, 63
ret i64 %3
}
It would be good to not forget on trunc variant:
unsigned FIO_highbit64(unsigned long long v)
{
unsigned count = 0;
v >>= 1;
while (v) { v >>= 1; count++; }
return count;
}
define dso_local i32 @_Z13FIO_highbit64y(i64 %0) local_unnamed_addr #0 {
%2 = lshr i64 %0, 1
%3 = call i64 @llvm.ctlz.i64(i64 %2, i1 false), !range !2
%4 = trunc i64 %3 to i32
%5 = sub nsw i32 64, %4
ret i32 %5
}
=>
define dso_local i32 @_Z13FIO_highbit64y(i64 %0) local_unnamed_addr #0 {
%2 = tail call i64 @llvm.cttz.i64(i64 %0, i1 true), !range !2
%3 = trunc i64 %2 to i32
%4 = xor i32 %3, 63
ret i32 %4
}
FIO_highbit64(unsigned long long):
shr rdi
je .LBB0_1
bsr rcx, rdi
xor rcx, 63
mov eax, 64
sub eax, ecx
ret
.LBB0_1:
mov ecx, 64
mov eax, 64
sub eax, ecx
ret
vs:
FIO_highbit64(unsigned long long):
bsf rax, rdi
xor eax, 63
ret</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>