This is an archive of the discontinued LLVM Phabricator instance.

[ConstantRanges] Use APInt for constant case for urem/srem.
ClosedPublic

Authored by fhahn on Jun 29 2021, 6:24 AM.

Details

Summary

Currently UREM & SREM on constant ranges produces overly pessimistic
results for single element constant ranges.

Delegate to APInt's implementation if both operands are single element
constant ranges. We already do something similar for other binary
operators, like binary AND.o

Fixes PR49731.

Diff Detail

Event Timeline

fhahn created this revision.Jun 29 2021, 6:24 AM
fhahn requested review of this revision.Jun 29 2021, 6:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 29 2021, 6:24 AM
nikic added a comment.Jun 29 2021, 6:29 AM

What does this do if the single element on the RHS is zero?

fhahn updated this revision to Diff 355255.Jun 29 2021, 8:19 AM

What does this do if the single element on the RHS is zero?

Oh, APInt::urem/srem assert that RHS is not zero. I updated the code to directly handle the case by return an empty range. I think we could further simplify the compares against UREM/SREM by 0, but currently that is not happening as we force empty ranges to overdefined when resolving undefs.

This revision is now accepted and ready to land.Jun 29 2021, 8:59 AM
nikic added inline comments.Jun 29 2021, 9:22 AM
llvm/lib/IR/ConstantRange.cpp
1224

This should be if (const APInt *RHSInt = RHS.getSingleElement()). Same for the other isSingleElement + getSingleElement combinations.

fhahn updated this revision to Diff 355474.Jun 30 2021, 1:36 AM

Rebase & use getSingleElement in top-most if.

fhahn marked an inline comment as done.Jun 30 2021, 1:37 AM
fhahn added inline comments.
llvm/lib/IR/ConstantRange.cpp
1224

Updated, thanks!

lebedev.ri added inline comments.Jun 30 2021, 1:49 AM
llvm/lib/IR/ConstantRange.cpp
1229–1230

Technically these also can be if(const APInt *LHSInt = getSingleElement()) return {LHSInt->urem(*RHSInt)};

fhahn marked an inline comment as done.Jun 30 2021, 1:52 AM
fhahn added inline comments.
llvm/lib/IR/ConstantRange.cpp
1229–1230

yeah I 'll update that in the committed version

This revision was landed with ongoing or failed builds.Jun 30 2021, 3:21 AM
This revision was automatically updated to reflect the committed changes.