Loop Idiom recognition was generating memset in a case that would result generating a division operation to an unsafe location.
Details
Diff Detail
Event Timeline
test/Transforms/LoopIdiom/unsafe.ll | ||
---|---|---|
2–8 | I'm under the impression this test can be greatly reduced. |
lib/Transforms/Scalar/LoopIdiomRecognize.cpp | ||
---|---|---|
823 | Do we also need to check whether it's safe to expand NumBytesS? |
lib/Transforms/Scalar/LoopIdiomRecognize.cpp | ||
---|---|---|
823 | I don't think so (IIUC), because the NumBytes is based on the trip count of the loop*storesize, both of them should be safe to expand if the loop has a valid SCEV. |
lib/Transforms/Scalar/LoopIdiomRecognize.cpp | ||
---|---|---|
823 | The store size is a constant, so that's obviously fine, but I don't see how the loop's trip count is obviously safe to expand; . Consider, e.g.: #include <stdio.h> void f(unsigned a, unsigned b, char* x) { for (int j = 0; j < a; j++) if (b != 0) for (long long i = 0; i < a / b; ++i) x[i] = 0; } |
lib/Transforms/Scalar/LoopIdiomRecognize.cpp | ||
---|---|---|
823 | Thanks for the test case. You are right, this test does hoist udiv even with this patch. I'll update the patch and add this test case. |
LGTM, with some minor comments.
lib/Transforms/Scalar/LoopIdiomRecognize.cpp | ||
---|---|---|
825 | We should rearrange the checks so LIR doesn't leave around the dead expansion of BasePtr... but it's not a big deal, and it would make this patch a mess, so please fix that as a followup. | |
test/Transforms/LoopIdiom/unsafe.ll | ||
25 | Using "undef" here is kind of confusing; please use 0 instead. |
Do we also need to check whether it's safe to expand NumBytesS?