This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] Add G_SBFX/G_UBFX to computeKnownBits
ClosedPublic

Authored by bcahoon on May 22 2021, 10:39 AM.

Diff Detail

Event Timeline

bcahoon created this revision.May 22 2021, 10:39 AM
bcahoon requested review of this revision.May 22 2021, 10:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2021, 10:39 AM

Looks reasonable to me. My only slight concern is that the implementation could be more precise when offset and width are not exactly known - but I don't know if there's any point in trying to optimize that case.

foad added inline comments.May 24 2021, 5:09 AM
llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
524–525

For example, instead of expanding the calculation "(1 << Width) - 1" literally here, I think you might get better results with something like:

KnownBits Mask;
Mask.Zero = APInt::getBitsSetFrom(BitWidth, WidthKnown.getMaxValue());
Mask.One = APInt::getLowBitsSet(BitWidth, WidthKnown.getMinValue());
arsenm added inline comments.May 24 2021, 5:16 PM
llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
1794

Can you also test computeNumSignBits? It may just work as is

bcahoon updated this revision to Diff 347718.May 25 2021, 10:18 AM

Addressed review comments.
Use getBitsSetFrom and getLowBitsSet to compute mask for the width operand
of G_SBFX/G_UBFX as suggested.
Added tests for computeNumSignBits for G_SBFX/G_UBFX.
Fix clang-tidy formatting issues.

bcahoon marked 2 inline comments as done.May 25 2021, 10:22 AM
bcahoon added inline comments.
llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
524–525

Thanks for the suggestion. I use getBitsSetFrom/getLowBitsSet when the Width value is known. I think generating the mask explicitly or creating the literal calculation should generate the same results. But, when the width is known, it makes sense to just create the mask explicitly.

foad added inline comments.May 26 2021, 3:18 AM
llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
524–525

I don't understand why WidthKnown.isUnknown() should be a special case. But you might need some strategic std::mins to avoid passing a value larger than BitWidth into getBitsSetFrom/getLowBitsSet.

bcahoon updated this revision to Diff 348051.May 26 2021, 12:11 PM
bcahoon marked an inline comment as done.

Removed check for isUnknown, and limit value to BitWidth, as suggested in the review.

bcahoon marked an inline comment as done.May 26 2021, 12:11 PM

What about G_SBFX handling in GISelKnownBits::computeNumSignBits?

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
528

Are you going to have any issues here when Mask.hasConflict() is true?

foad added inline comments.May 27 2021, 4:22 AM
llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
528

That can only happen if WidthKnown.getMinValue() > WidthKnown.getMaxValue(), which I guess can only happen if WidthKnown.hasConflict() was already true. What are the rules about introducing or propagating conflicts?

RKSimon added inline comments.May 28 2021, 3:45 AM
llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
528

Well, we do assert there is no conflict at the end of GISelKnownBits::computeKnownBitsImpl so we should be ensuring this doesn't happen.

foad added inline comments.May 28 2021, 3:50 AM
llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
528

Right, but I'm saying that this code can't produce a conflict out of thin air, only if one of the inputs already had a conflict, so I *think* it's OK. Would you agree?

bcahoon updated this revision to Diff 349162.Jun 1 2021, 7:06 PM

Fix formatting

bcahoon added inline comments.Jun 1 2021, 7:07 PM
llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
528

I'm not sure what, if anything, needs to be done to handle a conflict here? Appreciate any suggestions.

RKSimon added inline comments.Jun 2 2021, 2:12 AM
llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
528

Yes. I think you're right.

536

Would it be clearer to handle G_SBFX/G_UBFX separately?

bcahoon updated this revision to Diff 349290.Jun 2 2021, 8:52 AM

Separate the G_UBFX and G_SBFX cases.

bcahoon marked an inline comment as done.Jun 2 2021, 8:52 AM
RKSimon accepted this revision.Jun 3 2021, 4:21 AM

LGTM with one minor - cheers.

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
119

Please can you add a TODO suggesting we move this to KnownBits.h - it looks like this might be usable in other places (e.g. x86 BEXTR) in the future but until then its fine to keep it here.

This revision is now accepted and ready to land.Jun 3 2021, 4:21 AM
This revision was automatically updated to reflect the committed changes.