This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] visitTrunc - trunc (*shr (trunc A), C) --> trunc(*shr A, C)
ClosedPublic

Authored by RKSimon on Sep 28 2020, 10:12 AM.

Details

Summary

Attempt to fold trunc (*shr (trunc A), C) --> trunc(*shr A, C) iff the shift amount if small enough that all zero/sign bits created by the shift are removed by the last trunc.

Helps fix the regressions encountered in D88316.

Diff Detail

Event Timeline

RKSimon created this revision.Sep 28 2020, 10:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 28 2020, 10:12 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
lebedev.ri added inline comments.Sep 28 2020, 10:19 AM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
847

Do you want to deal with non-splats from the getgo?
See m_SpecificInt_ICMP()

852

I think you can preserve exactness of the shift.

RKSimon added inline comments.Sep 28 2020, 10:53 AM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
847

OMG - I never knew that existed - beautiful!!!!

852
RKSimon updated this revision to Diff 294953.Sep 29 2020, 6:23 AM

Use m_SpecificInt_ICMP to support non-uniform constants

Please can you post the alive proof you're basing this on?
I don't understand how you arrived at that precondition,
i think it should be much simpler: https://rise4fun.com/Alive/zpJn

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
844

I do not understand why this isn't just unsigned MaxShiftAmt = AWidth - DestWidth;

RKSimon added inline comments.Sep 29 2020, 8:28 AM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
844

I tried that and hit issues with cases like this: https://alive2.llvm.org/ce/z/qV29xP truncating i64 -> i9 -> i8 - so I think there's a typo and it should be std::min<unsigned>(DestWidth, SrcWidth - DestWidth)

lebedev.ri added inline comments.Sep 29 2020, 8:50 AM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
844

It is correct that https://alive2.llvm.org/ce/z/qV29x9 is a micompile, it should not be getting folded:
https://rise4fun.com/Alive/RTe7

However to me it still looks like that the precondition i proposed is correct:
https://rise4fun.com/Alive/saBR

lebedev.ri requested changes to this revision.Sep 29 2020, 8:55 AM
lebedev.ri added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
844

Err, i guess i'm just not reading what i'm writing.
Of course unsigned MaxShiftAmt = AWidth - DestWidth; won't work, because i'm prooving that C1 u<= width(%b)-width(%R).
So it should be unsigned MaxShiftAmt = C0->getType()->getScalarSizeInBits() - DestWidth;
:/

This revision now requires changes to proceed.Sep 29 2020, 8:55 AM
RKSimon updated this revision to Diff 295021.Sep 29 2020, 9:43 AM

Fix MaxShiftAmt value

lebedev.ri accepted this revision.Sep 29 2020, 9:52 AM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
844

Please do mark done comments as such

This revision is now accepted and ready to land.Sep 29 2020, 9:52 AM