This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fix InstCombinerImpl::foldICmpMulConstant for nsw and nuw mul with unsigned compare.
ClosedPublic

Authored by craig.topper on Feb 10 2023, 10:19 AM.

Details

Summary

If we have both an nsw and nuw flag, we would see the nsw flag
first and only handle signed comparisons.

This patch ignores the nsw flag if the comparison isn't signed.

Diff Detail

Event Timeline

craig.topper created this revision.Feb 10 2023, 10:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 10 2023, 10:19 AM
craig.topper requested review of this revision.Feb 10 2023, 10:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 10 2023, 10:19 AM
goldstein.w.n added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2083–2084

Does this need an ICmpInst::isUnsigned(Pred) check aswell?

llvm/test/Transforms/InstCombine/icmp-mul.ll
997

imo a few more tests (at least one with unsigned pred).

goldstein.w.n added inline comments.Feb 10 2023, 11:53 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2079–2081

I'm not sure this is correct w.o nuw:
nsw only: https://alive2.llvm.org/ce/z/xrrGmG
nsw nuw: https://alive2.llvm.org/ce/z/CCHqJ8

craig.topper added inline comments.Feb 10 2023, 11:56 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2083–2084

I didn't add isUnsigned because the body of else only checks unsigned predicates and leaves NewC null if it doesn't find an unsigned predicate. NewC being null is sufficient to make the function return null.

llvm/test/Transforms/InstCombine/icmp-mul.ll
997

Yes I'm going to find the tests for the unsigned code and see if I can copy them and add an nsw flag to expose the bug.

craig.topper added inline comments.Feb 10 2023, 12:06 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2079–2081

I think you need to correct the result of the sdiv to round down to match the RoundingSDiv call. sdiv rounds towards 0.

https://alive2.llvm.org/ce/z/c2BQVk

goldstein.w.n added inline comments.Feb 10 2023, 12:20 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2083–2084

Isn't that also true for the signed case? If so maybe drop that check aswell.

craig.topper added inline comments.Feb 10 2023, 12:27 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2083–2084

It is true, but I need it to still go to the else if the condition isn't signed.

Alternatively, I could remove the else and write.

if (NSW) {
  ....
}

if (!NewC && NUW) {

}

-Rebase on new pre-commit with tests.
-Convert else if to an assert since we already checked isSigned.
-Make unsigned code more similar to new signed code.

craig.topper added inline comments.Feb 10 2023, 12:41 PM
llvm/test/Transforms/InstCombine/icmp-mul.ll
137

Forgot to remove the FIXMEs. Will fix.

Update FIXMEs

This comment was removed by craig.topper.
goldstein.w.n added inline comments.Feb 10 2023, 1:53 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2071

is this needed? Seems redundant given the nsw flag.

craig.topper added inline comments.Feb 10 2023, 1:59 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2071

I think this check is protecting the APIntOps::RoundingSDiv call. You can't pass those values to it. I'm not sure the nsw flag matters.

I suspect a signed comparison with INT_MIN on the RHS side would have already been converted to eq/ne compare or true/false before we got here.

craig.topper retitled this revision from [InstCombine][WIP] Fix InstCombinerImpl::foldICmpMulConstant for nsw and nuw mul with unsigned compare. to [InstCombine] Fix InstCombinerImpl::foldICmpMulConstant for nsw and nuw mul with unsigned compare..Feb 10 2023, 3:41 PM
craig.topper edited the summary of this revision. (Show Details)

Ping. This seems like a pretty straightforward bug fix. Is there anything I need to do?

nikic accepted this revision.Feb 14 2023, 11:16 PM

LGTM

This revision is now accepted and ready to land.Feb 14 2023, 11:16 PM
This revision was landed with ongoing or failed builds.Feb 14 2023, 11:44 PM
This revision was automatically updated to reflect the committed changes.