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

    <tr>
        <th>Summary</th>
        <td>
            Vectorize loops with exit users with EVL tail folding
        </td>
    </tr>

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

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

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

<pre>
    https://godbolt.org/z/c7daf875K

If a loop has users in the exit block, we currently can't vectorize it with EVL tail folding and instead fall back to a scalar epilogue:

```llvm
define i32 @f(ptr %p, ptr %q, i32 %n) {
entry:
  br label %loop

loop:
  %iv = phi i32 [0, %entry], [%iv.next, %loop]
  %gep = getelementptr i32, ptr %p, i32 %iv
  %x = load i32, ptr %gep
  %y = add i32 %x, 1
  store i32 %y, ptr %gep
  %iv.next = add i32 %iv, 1
  %done = icmp eq i32 %iv.next, %n
  br i1 %done, label %exit, label %loop

exit:
  ret i32 %y
}

```

I don't think it's easy to do this with regular data/mask tail folding, but with EVL I think it should be possible to form a VPInstruction::ExtractLastElement which reads from the last active lane.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJxsVEGP4joT_DXOpbUocWISDjkwH4M0-vbwTnN34k7ih7GzdoeB_fVPdmCYHa2EhLGri3K7umQIerSILRMvTBwyudDkfGuWE5pdnXVO3dqJaA6s3DN-ZPw4OtU5QxvnR8aPvxk_9rWSQ1OL_7N8z_L92wASjHMzTDLAEtAH0BZoQsCrJuiM60-M_w8-EPrFe7RkbtBLy3hNcMGenNe_ETTBh6YJXt9_AkltYHBGaTuCtAq0DYRSwSCNgU72JyAHEkIvjfSAszZuXDCKTprYNl8_xlzOLN8rHLRF0CUHVuUD481MHhgXcxR2X_-K6wThwjK-A1a_sHyPlvxtZQboPBjZoYmYeOf179LqjmBc6Auw8gDzpFc68ZJHasbFyiUO6ad4SdiNxSvdzxORODyIRpwT04iEBs9oKUrVJf-iev6iWl8elddUZ5xU3-Ajzg_MLWGkUo_ya8QV6TiQ8_jYv_29_i79O4u-PGkYF8pZTBDdn2fAX0_Y14vbR3d18SiKJ5-9jk76Y-PZ_HR0b75HeorO96w-fPPD3bKg3Go_mrQ9QeSuA6AMt-gr5eJ-WO3ocVyix5QkyfjxLMPpD3tGVd3yxbtvn6QQJrcYBR3C7ELQncFIPzh_Bgnv_7zZQH7pSTsbL1DuX6_kZU8_ZaDX9b3hY9J9FCFVgMG7c5orIwOB7Elf4triJlNtqXblTmbYFrXgVdMU9Tab2qqU9a6RtSjznSpEiY0cyq2ohRCcbyvMdMtzLvK6qIq82BbbjZBNUfdVpbZCNl2hWJXjWWqziaMUUyDTISzYFlWzzcssPUhIccL55zD7wDiP-eLbWPajW8bAqtzoQOFJRJoMtu-fCRDf9N71lBxrlvw1FLLFm-9BpWlauk3vzowf09yvXz9m7_7Fnhg_JuWB8eNd_KXl_wUAAP__rpGL0w">