This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] visitTrunc - trunc (lshr (sext A), C) --> (ashr A, C) non-uniform support
ClosedPublic

Authored by RKSimon on Sep 29 2020, 3:22 AM.

Details

Summary

This came from @lebedev.ri's suggestion to use m_SpecificInt_ICMP for D88429 - since I was going to change the m_APInt to m_Constant for that patch I thought I would do it for the only other user of the APInt first.

I've added a ConstantExpr::getUMin helper - its trivial to add UMAX/SMIN/SMAX but thought I'd wait until we have use cases.

Diff Detail

Event Timeline

RKSimon created this revision.Sep 29 2020, 3:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 29 2020, 3:22 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
RKSimon requested review of this revision.Sep 29 2020, 3:22 AM

Hm, why do we need that clamping here?
If it's for undefs, see Constant::replaceUndefsWith().

Hm, why do we need that clamping here?
If it's for undefs, see Constant::replaceUndefsWith().

Isn't it matching the existing std::min calls ?

Hm, why do we need that clamping here?
If it's for undefs, see Constant::replaceUndefsWith().

Isn't it matching the existing std::min calls ?

Yes, obviously. I'm asking why it's there in the first place.

I think this should look closer to: (precommit that before this patch)

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
830–857

Your patch breaks the trunc_lshr_sext_wide_input test resulting in this fail: https://alive2.llvm.org/ce/z/VmcSVq

If you don't mind I'd prefer to use my existing version instead of getting waylaid on this

lebedev.ri accepted this revision.Sep 29 2020, 5:58 AM

LGTM

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
830–857

Err, that should of course be

// Note that since we will perform the shift in narrower type, we need to clamp the shift amount.
Constant *MaxAmt = ConstantInt::get(SrcTy, AWidth - 1, false);
Constant *ShAmt = ConstantExpr::getUMin(C, MaxAmt);
ShAmt = ConstantExpr::getTrunc(ShAmt, A->getType());
llvm/test/Transforms/InstCombine/cast.ll
1563

It would be kind of good to preserve undef, but there is no nice way to do that currently.

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

Note: here we also should be preserving exactness of the right-shift..

Note: here we also should be preserving exactness of the right-shift..

(i.e. unless you want to fix that in a followup, please can you add a FIXME comment there?)

This revision was landed with ongoing or failed builds.Sep 29 2020, 7:01 AM
This revision was automatically updated to reflect the committed changes.

Note: here we also should be preserving exactness of the right-shift..

(i.e. unless you want to fix that in a followup, please can you add a FIXME comment there?)

Sorry - missed your exactness comment - dealing with it now