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

    <tr>
        <th>Summary</th>
        <td>
            LLVM Codegen producing extra move instructions or crashing with freeze
        </td>
    </tr>

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

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

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

<pre>
    # Synopsis
[selection-dag] [global-isel]

During Codegen for x86-64 the use of `freeze` is resulting in additional `mov` instructions that can be optimized away. If `-global-isel` is used with `freeze`, `llc` crashes.

These extra `mov` instructions have been observed during test-suite executions of [D128501](https://reviews.llvm.org/D128501) in `MultiSource/Applications/SIBsim4/SIBsim4.test`. The extra `mov` instructions are present in the function `link_msps` (simb4b1.c), which result in a 15% increase in execution time (2.11 sec to 2.43 sec). A reduced example is provided.

# Examples

| Arch | Flags | URL | Notes |
|---|---|---|---|
| x86-64 |    | https://godbolt.org/z/xKEasaM98 | Extra `mov`(s) with `freeze`|
| x86-64 | `-global-isel` | https://godbolt.org/z/caoe18zbz | Build failure with `freeze` and extra `mov` without `freeze`|
| aarch64 |    | https://godbolt.org/z/5nhYnKoqK | No issues |
| aarch64 | `-global-isel` | https://godbolt.org/z/vcWfcsbea | No issues |

## x86-64
The following example can be found here: https://godbolt.org/z/xKEasaM98.

For example the following non-freeze code:
```llvm
; Example 1 - without freeze
define void @link_msps(ptr %n2) {
  %bf.load = load i64, ptr %n2, align 4
  %bf.set = or i64 %bf.load, 4294967292
  store i64 %bf.set, ptr %n2, align 4
  ret void

```
results in the following x86-64 assembly:
```asm
link_msps:
        or      dword ptr [rdi], -4
        ret
```
If a `freeze` instruction is inserted in Example 1:
```llvm
; Example 2 - with freeze
define void @link_msps(ptr %n2) {
  %bf.load = load i64, ptr %n2, align 4
  %bf.freeze = freeze i64 %bf.load
  %bf.set = or i64 %bf.freeze, 4294967292
  store i64 %bf.set, ptr %n2, align 4
  ret void
}
```
additional `mov` instructions are present in the resulting assembly:
```asm
link_msps: 
        mov     eax, 4294967292
        or      rax, qword ptr [rdi]
        mov     qword ptr [rdi], rax
        ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFVktz4jgQ_jXmorLLFrYxBw4QkqqpJHvY7KP2tCVbbdCssBhJBpJfvy3ZPJyQSWaqtpYC3JL7pa8_tVQq_jwL6Jg8PTdqa4QJ4mUQz4NsYUBCZYVqQs5WQbYkOLeSqmQyFPgOZ3pV_79stWhW5EZxWEFDaqXJocjDPCV2DaQ1QFRNgjyuNcALoECEIRpMK62zEw1hnAsXjkmnt1E7r9QYq1ufhkFPzJKKNaREb1srNuIFOGF79hyRL957eJlgFwNDc7IXdj2IHtAbN5aycmqVZmYNJrpc0G84AQQOVrP38lmzHWAuuFxVGtA7DMQ7GCwYG5pWWOcBqrbTdwhki2VCiyxOHH60WFu7NcF4HtA7_GrYCdibSMrdJlJ6hVNHbTp1GGEKjw6xJ9XqCvD1fLuVomLePw6fviyM2KRnKXKZoFVEcD3fXw3TQLZYEWisC-XKVreNf-mhEs0_f28MZouGmDl6L9MyiSpMzaG5X4tq3VfUl5MkWUAzFCsNDKHEuRMWBIsHzguNkoQYqIhVhEbp2MnoMCJzdMXbCiGFA9tsJbhabrXaCQ58UChH3ttOxwzmJzdkrjEnJ9xJtjJe-v3XB__8RSE0Tjpph2F49f_srme0E_HjHsP6rRQvlbR96V7wd7i_ZYY9TguvfTvE36HoCvuWne8EvcLwzyRRMQVJ8VK-eO1FKyQnNROyxYq_iU1Yw98QxWmp1r6fJGOI9I9BkzXrv5p79e2-LwcW2LSvSjLw-5Or31V_1pUpgb0b50gjx6QO7VMLwEYmpdq7PX3kYd-AatUiUGvQgLE_z4MBde-wSx7d2kGwBttuhzSpsKU6z50d4u6_rkX0U-PFkf8kIeGpVn2hvA6HWjRAdkpwEqQXe5kWW6txI2YNdUw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