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

    <tr>
        <th>Summary</th>
        <td>
            -march=i386 should not generate bswap instruction
        </td>
    </tr>

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

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

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

<pre>
    While investigating a totally unrelated problem, I ran across Clang generating a `BSWAP` instruction with `-march=i386`. `BSWAP` is a 486 instruction (see <https://www.felixcloutier.com/x86/bswap>) and should thus not be available when targeting 386.

Clang generates `BSWAP` for both, a call to `__builtin_bswap32`, and as an optimization of a pure C implementation.

```
// -m32 -march=i386 -O3 -Wall -Wextra -Wpedantic

int foo(int x) {
    return __builtin_bswap32(x);
}

unsigned int foo2(unsigned int x) {
    return 0u
        | (x & 0x000000ffu) << 24
        | (x & 0x0000ff00u) << 8
        | (x & 0x00ff0000u) >> 8
        | (x & 0xff000000u) >> 24
        ;
}
```
(<https://godbolt.org/z/8zhK7GnsM>)

Now, I am not actually sure if Clang even wants to be able to target 386-class CPUs at all (I was not able to find any documentation indicating a positive or negative answer to that), however, it does accept `-march=i386` without any warning or error. IMHO, it should either generate proper instructions honoring the given target, or just fail accepting the target CPU in the first place.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyFVE1v4zgM_TXKhUjgyIljH3xo0-lusZidARaDHgvZpmMNFMnQR5z21y-lJJ1kuosaSixL5NMj9cjGdK_18yAVgtQHdF7uhJd6BwK88UKpVwjaohIeOxitaRTuGd_CE1ihQbTWOAdbJchjhxrtxZkV2f0_z3ff6U3AztvQemk0TNIPcXO-F7YdWP4g87Kg78WthyOIVVncuDJeOkRg-XbwfnQsv2P8kcY0TYselTy2ygQv0S5aQxwfjwTMHxs3iZHlXxivQOgO3GCC6sAPwYE2HhoEcRBSCYoM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