Given a range check on an induction variable, we can convert that range check to a loop invariant check against the starting value of the induction variable if the check would fail on the first iteration or not at all. This creates a check which is easily unswitched and thus effectively removes the range check from within the loop entirely.
Given C code:
for(int i = M; i < N; i++) // i is known not to overflow
if (i < 0) break; a[i] = 0;
}
This transformation produces:
for(int i = M; i < N; i++)
if (M < 0) break; a[i] = 0;
}
Which can be unswitched into:
if (M < 0) break;
for(int i = M; i < N; i++)
a[i] = 0;
}
I'm not entirely sure this is done in the best place. I couldn't really find a more natural fit for it, but the bit of code walking the use of each candidate induction variable didn't really seem elegant. Suggestions on a better way to structure this are very welcome.
Might want to just assert(BI->isConditional()) here, since it uses an icmp.