Tail-folding does not support cases where an IV is used outside of the
loop. Before this change, the tail-folding legality check did not check
for this. In PR52335, this led to generating code with an incorrrect
index computation. Fix this by adding the missing check.
This change also adds assertions that there are no externally-used IVs
when performing tail folding.
Fixes PR52335.
While this indeed fixes the issue for the test case, I think this is not really the right fix.
IIUC AllowedExit is supposed to contain the set of instructions that are *allowed* to have outside users. So we need to check *all* instructions that have outsides users are also in this set.
But that's not what is happening at the moment. We won't check reduction or induction phis against this set in general.
For tail folding, we need to make sure that there are no users outside the loop for any induction, independent of whether they are allowed to have outside users or not (i.e. no IV is allowed to have outside users at the moment for tail folding).
A direct way to fix this would be to use the following
Note that it might be possible to compute the final induction value even when tail-folding similar to how we compute the final reduction value, but that's only a potential follow up.
Independent of that, we may be able to allow users of the phi itself even if there is a SCEV predicate in general.
Note that the current logic does not prevent vectorization if the induction phi is used outside in the predicated case. Without tail folding, we never check if an induction phi has users outside the loop. A possible justification is the following: the phi value is guaranteed to be valid for each iteration of the loop as per the SCEV predicate, so even if it is used outside the loop, the value is only used if the predicate holds. For the incremented value this is not true. The value computed in the last iteration may be poison and should not be used outside the scope of the predicate.