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

    <tr>
        <th>Summary</th>
        <td>
            CodeGenPrepare does not sink some address computations
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:codegen,
            missed-optimization
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            MatzeB,
            nikic,
            uweigand,
            efriedma-quic
      </td>
    </tr>

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

<pre>
    While working on improvements of FP addressing, I have seen a regression that seems to origin in CodeGenPrepare.

_Short background: SystemZ has 16 FP regs and 32 overlapping vector regs. The FP memory instructions support 20-bit displacements, while the vector instructions only 12-bit. Currently the SystemZ isLegalAddressingMode() returns true for a 20-bit displacement in these cases, but I think it would be good to do the opposite in order to do vector memory accesses by default, which I have now tried._

I find that when CGP cannot find computation to sink into the operands of a memory access it does not sink the addressing computation either. This causes increased register pressure if the address base is also live after the newly computed (unfoldable) address. For example:

```
define void @fun(ptr %Ptr, i1 %Cond) {
  %i79 = getelementptr inbounds i8, ptr %Ptr, i64 0
  %i82 = getelementptr inbounds i8, ptr %Ptr, i64 -16
  br i1 %Cond, label %bb1, label %bb2

bb1:
  %i80 = load volatile double, ptr %i79, align 8
  store volatile double %i80, ptr %i82, align 8
  br label %bb2

bb2:
  ret void
}
```
Since the negative offset cannot be folded into the store, %i82 remains in the first block although it would be better to still compute it right before the store:
```
*** IR Dump After CodeGen Prepare (codegenprepare) ***
define void @fun(ptr %Ptr, i1 %Cond) #0 {
  %i82 = getelementptr inbounds i8, ptr %Ptr, i64 -16
  br i1 %Cond, label %bb1, label %bb2

bb1:                                              ; preds = %0
  %1 = bitcast ptr %Ptr to ptr
  %i80 = load volatile double, ptr %1, align 8
  store volatile double %i80, ptr %i82, align 8
  br label %bb2

bb2:                                              ; preds = %bb1, %0
  ret void
}
```

This seems to have caused increased spilling during my experiments, and it seems it is something missing. My guess is that earlier optimizations rely on CodeGenPrepare for address sinking and folding. On the other hand, overlapping registers like this is not a problem specific to addressing only. 

What are your thoughts on this, and what could the solution be?

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFVtlu4zYU_Rr7hYghS7FjP_ghCzKYooMGnQID9GVASVcSG4pUucR1v77nUnKiBCnQmQJtYNjhcg8Pz91Y2vp0-NIpTeJo3aMyrbBGqH5w9ol6MsEL24j7ByHr2pH32LDIb8VH0cknEp7ICCkctWkNlqGTgad7L4IV1qlWAc6IW1vTBzIPjgbpaLXI7hbZ9fj99XNnXRClrB5bZ6OpF8W1-HzygfpfcYwX6y0TwCFeSFOLIhfg5rQcBqb7RFWwLi2vxC8d8d6eeutOONcHF6sAYl74OAx8Tp5dlCqIWvlBy2q8I1_pmEQIAJgQX1lbo09inbPpStxG52CGGd5-pqr8j9RKff0s1CfceZHvFvke7EJ0gAEgiQbg8j0eLBQQPYlKekqsyhggduiUeRTYfbRR16Ik0Vpbs8K1TRws7uZVIEawriY3rU1XmfSQVQVm5EV5EjU1Muow3bzqzi419giWiurV17mTPopGQfvk3mMHr99-eABLY2wYVyrbDzHIkILACp8Im3CmRw6uS7EkX7PhW9UWnBgpWbHBS7S9AiaFRcduVh6nR76LMpUjyFVzCCi4womBbaODGs0cDSEGaWEptbdCK9xWNryf9xg6wp_jYcCC36JprK5lqYk9OGGsxD30pD9kP2C-uJ5rtNhm0ycNobAyiCarAHeZNdEAdAgO2JuH4Fh5tebBrUXM44jF1c1oKXhWXWGmuBMtBdIpPNhWmZJTBNfeMcAbuO2lyOYQu_x7IC7W2zNI6V5xvBValqR5oizXb8b5XAtePqszcckSF21lDU00HIp0q21M-j7TwK15JLVqjdid7T2CmN5aTahz413-jjHu8Pcs8xlLJGny1rTj6u5dt35GwNEUMi34IIps03jYTulQcobrGkH0HP-JP1ObnOKolygvU74jgZyHnbbVI8iHzsa2e5XtJYUw5rSHAPocprwHFbbjMxtW6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