<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/66546>66546</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            UndefinedBehaviorSanitizer incorrectly flags __builtin_clz(0) as undefined behavior
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            compiler-rt:ubsan
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          aaronpuchert
      </td>
    </tr>
</table>

<pre>
    This is easy to reproduce:
```c
int main(int argc, char** argv) {
  int value = __builtin_clz(0);
  return 0;
}
```
Now compile `-fsanitize=undefined` and run:
```
clz.c:2:29: runtime error: passing zero to clz(), which is not a valid argument
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior clz.c:2:29 in
```
But I don't think this is correct. The [GCC documentation says](https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fclz):

> If _x_ is 0, the result is undefined.

That's not undefined behavior, just an undefined result. The [LLVM documentation for the IR intrinsic](https://llvm.org/docs/LangRef.html#llvm-ctlz-intrinsic) states it more clearly:

> If `src == 0` then the result is the size in bits of the type of `src` if `is_zero_poison == 0` and `poison` otherwise.

Which is saying the same thing. So `__builtin_clz(0)` is not by itself undefined behavior.

I don't think it's necessarily wrong that UBSan warns about this, but it shouldn't be under `undefined-behavior`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VF2P6jYQ_TXmZUTk60AgD3mApVuttNtK-9GqT8hxJsS3jo38sXvh11eTAHuXSyVw7LE9PnPOzMgQ9M4iVmy-ZvPNRKbYOV9J6Z3dJ9Whj5PaNYfqtdMBdACU4QDRgce9d01SyPIV4xvGV6zg40-Na20j9FJbJpY0lX6nmLgD1UnPxIqJFZnemSiBLdbjFQA6-S5NQmD5BrbbOmkTtd0qc2RiyZkoWX457DEmb4FfTGyxucIyLv9wH6Bcv9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