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

    <tr>
        <th>Summary</th>
        <td>
            [SCEV] [IndVars] Missed optimization for invariant condition after first iteration
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            loopoptim,
            missed-optimization
      </td>
    </tr>

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

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

<pre>
    Inspired by #70597, this issue focuses on its phi form.
For loops like:
```C
c = a;
for(int j = 0; j < xxxxx; j += b)
 if(cmp(c))
     c = i;  // i is invariant
```
`c` become invariant after the first iteration. And therefore `cmp(c)` become invariant after the first iteration.
When it become phi, `cmp(c)` is equvalent to `cmp(phi(a,i))`.
We could peel out such condition easier when there is no load/store, but *PHINode*.
A proof for SSA version(without memory operation):
https://alive2.llvm.org/ce/z/TP3WDq

In this way, the loop could be unrolled: 
https://godbolt.org/z/q64nndzde

Actually I have no idea which pass is responsible for such fold. So feel free to change the tags.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVE2P4zYM_TXMhZhAke18HHxwkhrNocUCU-z2Kst0rK0seSQ508yvLyR7JoNpLzUCx5REvsdHUsJ7dTVEJRRHKM4rMYXeuvLPU39fNba9lxfjR-WoxeaOwLMdKw474CcMvfKovJ8IOysnTx6tQRU8jr3CzrphDewMrKqtQ23t6FGrvwiyal6GLZt_p9mWCNkZBWTH2e6sA75XJuDPtMMgO6bPE_4dn8Xkx7jZAD_Mbqg64Hs5jPEdVz824jODqOiLwGvgNSqMeZibcEqY8IXbhylhy7AhaQd6HEbRBXIYesJOOR9QBXIiKGvWWJk2bjjqrCOMER6U_l-omcOPnqK6745jr2IR_hVXeaSX6SY0mYDBPg4kh70AflKLKlv2HppQ2km3OBJptFNAP8kepTWtigyQhFfk8DVSSDlFGGNRW9ECr32wjiKbZgoIvPr26-V32xLwagGocHTWdrEp8Pm5whs5r6wBvn9VoY-AAw3W3dGOS9KR33uj9CGMPlqpXkKrG_G11rdhbd0VeC0JeP0GvP7jW_bj_LKULL0vZm7TV3GfW5ZSJy7pNoSTcVZraiGr8L_QrrZtrA4LUgR52ebGtG8tfcapZJiE1ne8YC9uFLVRLQl87ZXscRQ-Tgo68qM1XjWakhRJ5c7qdo3PFrsofueIYtlkL8yVEuEgrn7RcdWWWXvIDmJF5WZ72O-yTbYpVn1ZsPyQZ9t9zuQ2540UMt8fWMckl0XWHdqVKjnj2YZlG1bk-yxfi0KIgyg6zjaCFUUOOaNBKP0h7CqNdrlju-1upUVD2qcrgvOooB2DGoBz4CfgfFDeU_uUFtXbUkAebxNXxnhPzXT1kDOtfPAPhKCCTvfO8-mX71CcEYrjxbTfhfPR-i1Fxc9Rk2yPmXl06Dw9XyZnNTldfqmnCv3UrKUdgNeRyPL3NDr7k2QAXqe8PfA6pf5PAAAA__8KK5nM">