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

    <tr>
        <th>Summary</th>
        <td>
            i386-on-i386 build is different from i386-on-amd64 build
        </td>
    </tr>

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

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

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

<pre>
    On FreeBSD we see a couple binaries which build differently for the i386 target depending on whether they are running on an amd64 host or on an "i386" host (in fact, i386 binaries running on an amd64 host).

I have narrowed it down to the Machine Instruction Scheduler step (running with `-mllvm -enable-misched=0` prevents the divergence between i386 and amd64 hosts); looking at the produced assembly code it looks like different choices are made (with very low probability -- a few times out of the entire OS build) in the register allocator.  In my reproducer:

* esi is used when building on i386 where edx is used when building on amd64.
* eax is used when building on i386 where esi is used when building on amd64.

Test case: `clang -fpic -O2 -S reduced.ll` with the [reduced.ll.txt](https://github.com/user-attachments/files/16275043/reduced.ll.txt) file produces [i386-on-i386.asm.txt](https://github.com/user-attachments/files/16275041/i386-on-i386.asm.txt) on an i386 host and [i386-on-amd64.asm.txt](https://github.com/user-attachments/files/16275042/i386-on-amd64.asm.txt) on an amd64 host.

Clang/LLVM versions:
```
amd64 host:
FreeBSD clang version 18.1.3 (https://github.com/llvm/llvm-project.git llvmorg-18.1.3-0-gc13b7485b879)
Target: x86_64-unknown-freebsd15.0
Thread model: posix

i386 host:
FreeBSD clang version 18.1.6 (https://github.com/llvm/llvm-project.git llvmorg-18.1.6-0-g1118c2e05e67)
Target: i386-unknown-freebsd15.0
Thread model: posix
```

The test case was reduced from `contrib/libarchive/libarchive/archive_write_set_format_7zip.c` (FreeBSD's included copy of libarchive).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVl-Pm7oT_TTOywgE5k_CAw-7XUWq1KoPW_1eV8YewL8aG9km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