<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJylVdtu2zgQ_Rr5ZWBDFn190EPjpIu8tMDCQLH7ElDUSGJKkQIv8eXrd0jZjdOmuwEWoGVzOJo5PHNmXJn6VH7rUMPJBBCmH6RC8J10tKkRDtJ3IBTXLczZuMtW-fQrg2nPregydn_crKarBUynzte0FVlxR6vg5Jfl91n-6fKk7bjGbcGkFipQjoztKukz9vDeiaCoUt-cpidZCJUcBqyzYpMSf6IVyL5aPHngWbGFbH03egNY9MFqeHqqglRe6ifhz0rRq6NjsYL8uG4ydnkhW9-_Dzo9I1Mt-pGlmB4J6fKuNXVllM-W92TrvB9cBFV8pnU5mhnb0u5Mn81f--eHpf12pPxx_Z6oyjVgOfntwNZytNF1_gXfPuLqkWtHELmPBXvlKo_ZVjkIrq-scH2CF64CxhzaeNAo0DlupTqB1BQEIZ_OizXh0C3OICWgW_eUwECFwKEKbXybu-TtTLACRwUJhdxSIOflEBT3eEEV_S4AUnJwnQmqjuHeyXl7vz92O6iNCD1qz700OmY-dFJ08JgV655QuNBLkuwo3MYoZQ4uejl-GhGOtujDKxNGNPFD1LwRSaSq4T0xEYt5WyX2AH8m-GNAHfoKLZgGvOVSxcj5lGTt4m1S9Zzn1qeMYzrixXlwstWykVQOD-QOg3Ey3mkGjw0cgYjO48sjWy4oH01B19hIjfXsl7r_KEsUwMg0UaPwNgKB_PmeqRmu2qAwUQZuQEHQomx2IP3V_CM5larjL9LYa8F2Y-sTBu2SmLiu08FlrthrjUkLSkEbOBXXI94K4gbgr6IlBFwdYg3_QyJ7OpM1cgWNPFKx7dispFI4JATRHAO0qNGSKOtRrNEzzre3sy2mJkad6ZHCUAWV_I4fbtZIwsWWhswHOrgiRRpqTAIhPQpSGXVNGr77v3df9uDCMBjrY0hs3wB230-KEzhCfLih2gxe9vKMcNU6ORCuxKl23gaR-oi9P4L8WZA64cNjaFKXrN6yLZ_w4Dtjy7NExZ8nwaryp8FIdwrVjPRBG6Verl_TwZpnujhtJTUzUvN-XjLG8klXrjbbqtiwZj1foNhylrPNajuvmViw5QqbxUTxCpUraSRnRaHxACkE_abRPPn_CGRZ5EWRz4t5Pl8uWDGbrxa82RbIc9EIsRTZIseeZsAsxokzf2LLFJLk5-hQSefd6yFNK5oBiAkwIfTSKywftTDWEoCbP61Uh9dqptk3SejKBO0fZ6xysg>53330</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Incorrect __builtin_tzcntll optimization
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    When you compile this code with clang 13 with `-O3 -march=x86-64 --std=c++2a`

```
#include <bit>
#include <cstdint>

int clipped(std::uint64_t a) {
  return __builtin_ctzll(a) & 0x7f;
}
```

you get this (see [godbolt](https://godbolt.org/z/8YTjE5rWx))

```
bsf rax, rdi
ret
```

This means that `clipped(0)` can return any value, not necessarily in the 0-127 range. This seems to be a bug, as the source code clearly stipulates that the return value should be in the 0-127 range.

GCC documentation, which I'm assuming clang follows, says the following about the the `__builtin_ctz` family:

> Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.

This seems to mean that while the result of `__builtin_ctzll(0)` is not specified, it is not undefined behavior in the C++ sense, and the compiler should still guarantee that the result of `clipped(0)` is always in the 0-127 range.

The ideal fix for this bug would fix the generated code for `-march=x86-64` to something like
```
bsf rax, rdi
and rax, 0x7f
ret
```

but on architectures with TZCNT support, eg `-march=skylake` would still optimize out the `and` instruction:
```
tzcnt   rax, rdi
ret
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNVE1v4zYQ_TXyZWBDlmzZPuiwcbJFLi1QBFi0l4CiRhKzFCnwI_749R2S9sbZTdsAlGwOR_MeZ95Mo9tT_W1ABSftgetx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