<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 - UBSan safe leading/trailing zero counts can't simplify to zero-input correct variants"
href="https://bugs.llvm.org/show_bug.cgi?id=50140">50140</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>UBSan safe leading/trailing zero counts can't simplify to zero-input correct variants
</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>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>Scalar Optimizations
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>llvm-dev@redking.me.uk
</td>
</tr>
<tr>
<th>CC</th>
<td>david.bolvansky@gmail.com, lebedev.ri@gmail.com, llvm-bugs@lists.llvm.org, nikita.ppv@gmail.com, spatel+llvm@rotateright.com
</td>
</tr></table>
<p>
<div>
<pre><a href="https://simd.godbolt.org/z/KrahKsdhe">https://simd.godbolt.org/z/KrahKsdhe</a>
clang -g0 -O3 -march=haswell
#include <cstdint>
#include <climits>
auto lz(unsigned x) {
int c = __builtin_clz( x );
return x ? c : sizeof(x) * CHAR_BIT;
}
auto tz(unsigned x) {
int c = __builtin_ctz( x );
return x ? c : sizeof(x) * CHAR_BIT;
}
lz:
lzcntl %edi, %eax
retq
tz:
tzcntl %edi, %eax
retq
auto lz_ubsan(unsigned x) {
int c = __builtin_clz( x ? x : ~0U );
return x ? c : sizeof(x) * CHAR_BIT;
}
auto tz_ubsan(unsigned x) {
int c = __builtin_ctz( x ? x : ~0U );
return x ? c : sizeof(x) * CHAR_BIT;
}
lz_ubsan:
xorl %eax, %eax
cmpl $1, %edi
sbbl %eax, %eax
orl %edi, %eax
lzcntl %eax, %ecx
testl %edi, %edi
movl $32, %eax
cmovnel %ecx, %eax
retq
tz_ubsan:
xorl %eax, %eax
cmpl $1, %edi
sbbl %eax, %eax
orl %edi, %eax
tzcntl %eax, %ecx
testl %edi, %edi
movl $32, %eax
cmovnel %ecx, %eax
retq
To prevent ubsan warnings, we mustn't call the ctz/clz builtins with a
zero-value. So we typically insert an all-bits value to make it shutup
Unfortunately this conflicts with the post-combine that recognise cases where
the zero-value input should be correctly handled (e.g tzcnt/lzcnt x86
instructions).
Funnily enough, gcc has better code from the ubsan-safe cases.....</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>