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

    <tr>
        <th>Summary</th>
        <td>
            splitBasicBlockBefore() modifies the list of predecessors while iterating on them
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          davidebaroffio-polimi
      </td>
    </tr>
</table>

<pre>
    The issue
---
I noticed that the function `splitBasicBlockBefore()` implemented in the file `llvm/lib/IR/BasicBlock.cpp` updated the successors only of the first predecessor of 'this' BasicBlock. My take is that the following for loop 
```
for (BasicBlock *Pred : predecessors(this))
```
together with this line 
```
Instruction *TI = Pred->getTerminator();
``` 
in the loop modifies the iterator causing the program to exit the for loop.

My solution
---
I managed to fix this modifying the `for` with the following two lines:
```
while (std::distance(predecessors(this).begin(), predecessors(this).end()) != 0) {
   BasicBlock *Pred = *predecessors(this).begin();
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNVMGO2yAQ_Rr7gmJ5sbOODz4kTSPlUKmq9gcwjG26GCzAm83fd8CONu2mVSWEgWHem3kzuDXi2rwMQKRzMyT5Mcn3m81mWZyJNl5yEMQPzOMEpJs199JokjznblLSH5iT_KAMfz1AZywkdJfQGq1EjpOCEbRHf6kXb6kgeCr1Nib0pGSL8_kHTh8wGZ-m4D5PgvlIDcTNnINzxjpitLoS061w1nkyWRCwmIMhoZUfpMMPuQMl367Es9eQ510yRilzkbrHlSXKmIksiSP_OuI2WDGvDzjc7b8jLUmK_T0_su4W8jqMR1je9IDcllykH0i4TJTU8Jj4rJ238yo43b-ckfBIAvMmKb4i0AvYUWrmjV11Lw5_AK3AawFijqMRspPg4on0YIM_4Wx2QYpwOFnTWzYSbwi8y5tYi0TZShBnVNUZNYcAP_XOyDTrQwENFup9STVSX280GB6ihiBXMe5L4i8mKuNQ44fiXIbYTXTnvAh3ir2QzjPNQw8-LkrWQi_1KhX98pfSZaDF7U6NBE9B9Dyuq1VeQsjDbjiG9X9wfypTKppC1EXNUi-9guYfb-v3-inMObT9PSlZpFlKG7Q0sfpjOlvVDN5PUVN6wtGj8HObcRPf4_os8bPBFvgJ3OM2_hkwg9P2uci36dBUINiuLHjelkLQvC55WW1bqLuuynclq1PFWlCuSbaHZHtMZUNzSnN0fqLFtqQZrypOy-dtKxgTFcuTMoeRSZUF4szYPrVNjKGde4fGkKL7MDLnZK8Bbvhs9oOxjWBvUkDLrOk6aTaTUXKUaQy-iZH_AoHOjhE">