This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold icmp ugt/ult (add nuw X, C2), C --> icmp ugt/ult X, (C - C2)
ClosedPublic

Authored by Nicola on Aug 20 2018, 7:41 AM.

Details

Summary

Support for sgt/slt was added in https://reviews.llvm.org/D29774, this adds the same cases also for unsigned compares.

Alive proof is at: https://rise4fun.com/Alive/nyY

Name: ult
Pre: WillNotOverflowUnsignedSub(C2, C1)
%a = add nuw i8 %x, C1
%b = icmp ult %a, C2

=>

%b = icmp ult %x, C2-C1

Name: ugt
Pre: WillNotOverflowUnsignedSub(C2, C1)
%a = add nuw i8 %x, C1
%b = icmp ugt %a, C2

=>

%b = icmp ugt %x, C2-C1

I updated the tests that changed and added new tests for the new combines.

Diff Detail

Repository
rL LLVM

Event Timeline

Nicola created this revision.Aug 20 2018, 7:41 AM
xbolva00 added inline comments.
test/Transforms/LoopVectorize/runtime-check.ll
20

TMP2 is unused?

Nicola added inline comments.Aug 20 2018, 8:28 AM
test/Transforms/LoopVectorize/runtime-check.ll
20

It is still used on line 36, unfortunately.

spatel added inline comments.Aug 20 2018, 8:43 AM
test/Transforms/InstCombine/icmp-add.ll
1

Please commit the tests first. Use this script to auto-generate the current results. Then, update with this patch and auto-generate the new results.

Nicola added inline comments.Aug 21 2018, 1:04 AM
test/Transforms/InstCombine/icmp-add.ll
1

Ok, thanks. I'll create a separate review for it.
Should I also regenerate all the other tests that I'm touching? LoopVectorize/runtime-check.ll is adding a lot on new checks, and some others are changing in InstCombine/icmp-add.ll

spatel added inline comments.Aug 21 2018, 4:55 AM
test/Transforms/InstCombine/icmp-add.ll
1

No, it's not necessary to regenerate everything. I suggest doing it for this file because these are the primary/minimal tests for the transform though. If the patch gets reverted for some reason, we want to keep the evidence that we're missing an optimization.

And yes, other tests in the file will be affected if we regenerate the whole thing. That's ok; an older version of the script was used previously, and it behaved slightly differently. Alternatively, you can auto-gen just the new functions by specifying "--function=foo" as a parameter.

Nicola updated this revision to Diff 161734.Aug 21 2018, 8:36 AM

Updated after I committed the new tests.

spatel accepted this revision.Aug 21 2018, 8:50 AM

The code change LGTM.

Does the signed version of this fold have negative tests (eg, when there's no 'nsw' on the add)? Either way, if they don't already exist, please add negative tests for this transform where:

  1. The add does not have 'nuw'.
  2. usub_ov() actually does overflow.

...so we verify that this transform is sufficiently guarded.

This revision is now accepted and ready to land.Aug 21 2018, 8:50 AM
This revision was automatically updated to reflect the committed changes.