This is an archive of the discontinued LLVM Phabricator instance.

[LoopIdiom] PR32811 check for safety while expanding
ClosedPublic

Authored by hiraditya on Apr 29 2017, 5:39 PM.

Details

Summary

Loop Idiom recognition was generating memset in a case that would result generating a division operation to an unsafe location.

Diff Detail

Repository
rL LLVM

Event Timeline

hiraditya created this revision.Apr 29 2017, 5:39 PM
davide added inline comments.Apr 29 2017, 7:26 PM
test/Transforms/LoopIdiom/unsafe.ll
1–7 ↗(On Diff #97200)

I'm under the impression this test can be greatly reduced.

hiraditya updated this revision to Diff 97206.Apr 29 2017, 10:44 PM
hiraditya marked an inline comment as done.
efriedma added inline comments.
lib/Transforms/Scalar/LoopIdiomRecognize.cpp
823 ↗(On Diff #97206)

Do we also need to check whether it's safe to expand NumBytesS?

hiraditya added inline comments.May 1 2017, 1:30 PM
lib/Transforms/Scalar/LoopIdiomRecognize.cpp
823 ↗(On Diff #97206)

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.

efriedma added inline comments.May 1 2017, 2:11 PM
lib/Transforms/Scalar/LoopIdiomRecognize.cpp
823 ↗(On Diff #97206)

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;
}
hiraditya added inline comments.May 1 2017, 5:21 PM
lib/Transforms/Scalar/LoopIdiomRecognize.cpp
823 ↗(On Diff #97206)

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.

hiraditya updated this revision to Diff 97377.May 1 2017, 6:25 PM

Addressed Eli's comments.

efriedma accepted this revision.May 2 2017, 10:13 AM

LGTM, with some minor comments.

lib/Transforms/Scalar/LoopIdiomRecognize.cpp
825 ↗(On Diff #97377)

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
24 ↗(On Diff #97377)

Using "undef" here is kind of confusing; please use 0 instead.

This revision is now accepted and ready to land.May 2 2017, 10:13 AM
This revision was automatically updated to reflect the committed changes.