This is an archive of the discontinued LLVM Phabricator instance.

[LoopIdiomRecognize] Only convert loops to ctlz if we can prove that the input is non-negative.
ClosedPublic

Authored by craig.topper on May 24 2018, 4:00 PM.

Details

Summary

Loop idiom recognize tries to convert loops like

int foo(int x) {
  int cnt = 0;
  while (x) {
    x >>= 1;
    ++cnt;
  }
  return cnt;
}

into calls to ctlz, but if x is initially negative this loop should be infinite.

It happens that the cases that motivated this change have an absolute value of x before the loop. So this patch restricts the transform to cases where we know x is positive. Note: We are relying on the absolute value of INT_MIN to be undefined so we can assume that the result is always positive.

Fixes PR37479

Diff Detail

Repository
rL LLVM

Event Timeline

craig.topper created this revision.May 24 2018, 4:00 PM

Missing patch.

Add the code changes.

Out of curiosity, did the original test case come from C/C++? Because if it did, given that we get to assume infinite loops like this don't happen in C/C++, we may end up "breaking" this again in the future.

In any case, I recommend actually using the phrase "infinite loop" in the comment to make it clear what's going on.

The original motivating code is something like the loop in the htest_one_block function here https://github.com/mozilla/mozjpeg/blob/master/jchuff.c

efriedma added inline comments.May 25 2018, 10:40 AM
test/Transforms/LoopIdiom/ARM/ctlz.ll
16 ↗(On Diff #148498)

Please update this comment.

54 ↗(On Diff #148498)

Given that shr11 is known positive, it seems likely some other pass will convert it to an lshr. (If that doesn't happen now, it likely will some time in the future.)

Mention in infinite loop in comments. Update C code in comments in ARM test. Change ashr to lshr in tests when its directly after the abs pattern. InstCombine is able to convert those. The ashr inside the loop dont' get changed because value tracking doesn't understand the recurrence I guess.

I've left FIXME comments to handle lshr loops in the future. But I don't have a motivating example of those yet.

efriedma accepted this revision.May 31 2018, 2:24 PM

LGTM

I'm worried the motivating case will break once instcombine or CorrelatedValuePropagation gets a bit smarter... but I guess that's sort of orthogonal to this patch.

This revision is now accepted and ready to land.May 31 2018, 2:24 PM
This revision was automatically updated to reflect the committed changes.