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

    <tr>
        <th>Summary</th>
        <td>
            Aggressive instruction re-ordering introduces register spilling 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    I have a complex function that runs a hot loop with a lot of variables. That means we have to spill and fill a lot of registers to and from the stack. 

I have some code like so:

```zig
// some usage of `next_is_escaped_on_demand`
// ...
// ... some stuff ...
// ...
self.next_is_escaped_on_demand = (escape_and_terminal_code & backslashes) >> 63;
```

In the assembly, I see this:

```asm
        ; some usage of r14, which is `next_is_escaped_on_demand`
...
        ; compute `escape_and_terminal_code` and `backslashes`, which are r13 and rdx
 mov     qword ptr [rsp + 152], r13
        mov     qword ptr [rsp + 96], rdx
...
...
...
        mov     rdx, qword ptr [rsp + 96]
 and     rdx, qword ptr [rsp + 152]
        mov     qword ptr [rsp + 96], rdx
...
        mov     rdi, qword ptr [rsp + 96]
        shr rdi, 63
...
        mov     r14, rdi; r14 is going to be used in the next iteration of the loop
```

Unfortunately, LLVM tried to be really tricky and spread out individual pieces of the line of code across the whole loop, meaning we spill and fill multiple times just to do individual parts of a single line of high-level code. We also are unnecessarily moving the data into `rdi` just so we can move it over to `r14`.

Could be:

```asm
        ; we could even fold the usage of `qword ptr [rsp + 96]` into some other instruction if possible!
        mov     r14, qword ptr [rsp + 96]
...
        ; compute `escape_and_terminal_code` and `backslashes`, which are `r13` and `rdx`
        and     rdx, r13
 shr     rdx, 63
        mov     qword ptr [rsp + 96], rdx
```

If you want to see my garbage, chicken scratch in-dev code, [here ya go](https://zig.godbolt.org/z/Mbbr1e9qx). Navigate to line 2710 in the Zig code and "reveal linked code" for that line. Make note of where on the stack the data is coming from and CTRL+F for that. In my case, I turned on regular expressions and searched for `rsp \+ (?:96|152)` in the assembly to find the lines of code presented above.


LLVM unoptimized dump [here](https://gist.github.com/Validark/f3146a0e9daa5f70bdeed9c2245c08eb).

LLVM optimized dump [here](https://gist.github.com/Validark/69f821778df7a1f20a222d9e89a08481).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VkFv2zoS_jX0ZRBBoizZOvgQJzVQoN3DotsF9hKMxJHEmiJVkrLj_voFKTtx0jbtw-szAsckZ-YbDr_5SHROdppow4otK-4XOPne2M1nVFKg3S9qI06b99DjgQChMcOo6BHaSTdeGg2-Rw920g4QeuNBGTPCUfoeEJTxYFo4oJVYK3IJfArWA6F2cKQ5pjfgRqkUoBbQxh8XR0uddJ6sC0Zx2ZoBfE_gPDb7BFh6z9Lb-fucojMDQWMEgZL7MGT57bUZK9P575vszjN8x_hudpwcdhSgWZlqevQP0j2Qa3Ak8WD0g6ABtQj-155Jkrwez9Gcn9r2R8vz2JFqk5-iAMvvgfH1vPCAWjx4soPUqB7i_hgvocZm7xS6nhzjFbD8HcvfQZmzfPtquy9KpWMR0TkaanVi_A7egyMC30v3s3qhG-YZOH9Yvn1VNJstQ6xjL5sepPu9Ij6V4zpuoNnkKUT42f5ZmUZOsDK9rkKZPqeAlsBmeTSz4vEMM5hDxPl6NFbA6C2wYmvdCIxvISs4K-5DCJvlL_N6068qL24XnKd9ff_jdcTgw-_eCjy7hX38yv68gT-S-fd5yt_K8_xxvb24lPmvQs_UCeb5NowCfzojdRd6vw4kIwFyJm4gFUhPFqMEmTbOBuV5g_T_0a2xftLoaab8hw-fP4K3ksQZwhIqdQpTzf4Ui-1GSyjATB6kFvIgxYQKRkkNuSdYqSP5Y09iY41zcf7YG3VOit9FzQubOdJrtRsm5eWoCLwcyMGXyfmQjzAvINH6iIjgpO7UM2wvu_5G0YFUzCCB_xKgciaSf9I6pOrQSnUKtY717AkEegSpvQntE4pepjOyMyHFBnWwJpAezIEsnA2zJSvT5Lqqd2ZSAm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