This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Optimize away the unnecessary multi-use sign-extend
ClosedPublic

Authored by Bhramar.vatsa on Nov 12 2020, 6:06 AM.

Details

Summary

C.f. https://bugs.llvm.org/show_bug.cgi?id=47765

Added a case for handling the sign-extend (Shl+AShr) for multiple uses, to optimize it away for an individual use, when the demanded bits aren't affected by sign-extend.

https://rise4fun.com/Alive/lgf

Diff Detail

Event Timeline

Bhramar.vatsa created this revision.Nov 12 2020, 6:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 12 2020, 6:06 AM
Bhramar.vatsa requested review of this revision.Nov 12 2020, 6:06 AM
lebedev.ri edited the summary of this revision. (Show Details)Nov 12 2020, 6:34 AM
lebedev.ri added a reviewer: spatel.
lebedev.ri set the repository for this revision to rG LLVM Github Monorepo.

vector tests?

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
912

Is it worth merging the Shl matching and LeftShiftAmt == RightShiftAmt here as well?

915

Safer just to do a SRA->getValue()->ult(BitWidth) bounds check?

921

Use APInt::setLowBits ?

llvm/test/Transforms/InstCombine/simplify-multiuse-demanded-bits-ashr.ll
8 ↗(On Diff #304805)

strip the metadata/attaributes?

Can the test cases be simplified? I'm not sure you need the multiple blocks to just check for multiuse?

Can the test cases be simplified? I'm not sure you need the multiple blocks to just check for multiuse?

Note that I posted an alternate version at D91364 and added tests for it. Unless I missed some corner case covered by these tests, they should not be needed now.

Bhramar.vatsa marked 4 inline comments as done.Nov 16 2020, 7:31 AM

I have adapted the code as per @spatel's suggestion. This also solves the comments given on earlier version by @RKSimon.

@spatel, yes, I did see the requirement for same diff in the test. So, added the test to patch with same changes as shown in D91364.

lebedev.ri accepted this revision.Nov 16 2020, 8:00 AM

LGTM

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
904–906

s/if the demanded bits are already within original operand/if we do not demand any of the new sign bits/

This revision is now accepted and ready to land.Nov 16 2020, 8:00 AM
Bhramar.vatsa marked an inline comment as done.Nov 16 2020, 11:38 PM

Modified the comment as suggested by @lebedev.ri

Bhramar.vatsa added inline comments.Nov 17 2020, 12:35 AM
llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
931

For this patch, this call will no more happen, even when the 'case AShr' doesn't match completely. So, should a call to computeKnownBits(), be added in that case?
The call isn't there for other cases, but there, perhaps it isn't needed.

Please do say if you need help landing this (state the name e@ma.il to be used for the git commit's Author field).

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
931

Yes, this doesn't look immediately correct to me, but it's a preexisting problem.
I think the contents of default: switch should be located after the switch.
I think it would be an interesting experiment to make that change,
but not in this review.

craig.topper added inline comments.Nov 17 2020, 12:51 PM
llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
931

Don't the other handlers already do the equivalent of the default?

lebedev.ri added inline comments.Nov 17 2020, 1:45 PM
llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
931

The equivalent being computing the Known - err, right, they do.

Bhramar.vatsa added inline comments.Nov 17 2020, 10:42 PM
llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
931

So, then, perhaps in the favor of not changing the functionality when optimization doesn't take place, should the code for the default case be added for 'AShr' case too?
If agreed, I will provide another patch with this change.

Added the missing computation of Known bits for the case Instruction::AShr. This should result in no change of functionality except when intended.

Thanks @lebedev.ri , please help in landing this. Please use Name: Bhramar Vatsa, e-mail : Bhramar.Vatsa@synopsys.com

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
922–925

Maybe this should be moved between line 904 and 906 ? (After the computKnownBits, but before checking the signextend pattern). This brings it more in line how the other cases are handled, preferring to return a constant when that is possible.

Bhramar.vatsa marked an inline comment as done.Nov 18 2020, 6:51 AM
Bhramar.vatsa added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
922–925

Yes, I agree.

Bhramar.vatsa marked an inline comment as done.

Restructured the code as suggested by @jeroen.dobbelaere

Apologies for wrong patch (repeat of previous). I have now put up the latest patch. Please review and accept.

Looks good to me.

fhahn added a subscriber: fhahn.Apr 1 2021, 7:28 AM

This appears to cause the following crash: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=32759

It would be great if you could take a look. Reproducer test case linked on the page is https://oss-fuzz.com/download?testcase_id=5658887807172608 , crashes when running opt -instcombine

RKSimon added inline comments.Apr 1 2021, 10:18 AM
llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
921

Its possible that the shift amount is out of bounds - add a check for ShiftLC->ult(BitWidth) ?

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
921

Yes, that seems to be the case. I am looking for a fix.