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

    <tr>
        <th>Summary</th>
        <td>
            [X86][CodeGen] Custom loop alignment not working if loop header has multiple predecessors
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:X86,
            llvm:codegen
      </td>
    </tr>

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

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

<pre>
    Manually aligning loops via the `llvm.loop.align` metadata currently only works if the loop header has a single predecessor.

See this CE example: https://gcc.godbolt.org/z/PPj4dG8a1

The issue seems to be related to this algorithm:
https://github.com/llvm/llvm-project/blob/52126dc72c3f6f4d27e3835b0ad53e162af25e53/llvm/lib/CodeGen/MachineLoopInfo.cpp#L184-L222

For a single predecessor, the first code path is used, otherwise the second path. The second path does not work, because `blocks()` also includes the header, which typically does not branch to itself, so the function returns `nullptr`.
The second path is also not covered by tests.

I will prepare a fix for this.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVE1v3DYQ_TXUZRBBGn1Ye9Bhs66CAA4QoDn0yo-RxJgiBZKyvf31BWnXXbe95CJBnJk3mnnvkYegF0s0su4zQ9y4l_wQ5MkzRNbdF_yIq_PjTaAQTl3Hb9we3JgrcKMXq-0Cxrk9wJPmEFcC1lfGPG1lOi1zDusr2ChyxSMHeXhPNporOGuu8Oz8YwA959JUAitxRR5WHoBD0HYxBLsnRZJCcL5k1T2rzq_P34kgrjrA5TegF77thlhzhjXGPbDmzHBiOC1SlotTwplYOr8wnP5kOH3__rNVXwZe3-L9WAl0CAdBINoCRAeCwJPhkVT6ys24WZzXcd1Si1z3r4Y6rocopdsYTmkXb69Pu3c_SUaGkzBOMJw6rLFX8g5lM_dzq_COmqHpRMVV11DdI5-xo665wdGp7uIUfSHLcPrG5aotPTi3f7WzK-W-M2we6qH99ICIt8NNzv_vRhle8vZn7UME6RTBzuMKOsARSKWwiyv5Zx0oJwaSzqqcVMKPjwegHAWwLmZmU60gyY-QdSGMk4-B4cDwlETBTXCgrTSHopChX8lPZc-rlivE665lVts7rvDcpogDHQOZOSUH9zrBYWXUzoKneHgbUk97GLNHz_qq_Ifj2x_OhAaXsaV7Ik8KxBUihRg-iO0rPGtj0uZ27gk4zPoFZuezKMpCjY06NSde0FjfYYP10Dd1sY5KqVrNiDS3XTdLxXG460Rb9111mmXdFHrECttqqE5111VNXQ6K960gwq4dqk40rK1o49qU2VfOL0XW6FhX2DdYGC7IhDcfCy4fySrWnP8Y-uRkvDDELJ7mnLhdkmyywf2YRSmOJbC2MjrN-94h6mjy3ZBgunvWff5bc909XI4Q3fZq1-zwjWx8Jz1dCXr-j5m3w0S9f5ReKA5vxl92Tx4_MJzeNvA04l8BAAD__5xmlGc">