This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Fold (add (select lhs, rhs, cc, 0, y), x) -> (select lhs, rhs, cc, x, (add x, y))
ClosedPublic

Authored by craig.topper on Aug 5 2021, 2:50 PM.

Details

Summary

Similar for sub except sub isn't commutative.

Modify the existing and/or/xor folds to also work on ISD::SELECT
and not just RISCVISD::SELECT_CC. This is needed to make sure
we do this transform before type legalization turns i32 add/sub
into add/sub+sign_extend_inreg on RV64. If we don't do this before
that, the sign_extend_inreg will still be after the select.

Diff Detail

Event Timeline

craig.topper created this revision.Aug 5 2021, 2:50 PM
craig.topper requested review of this revision.Aug 5 2021, 2:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 5 2021, 2:50 PM
Herald added a subscriber: MaskRay. · View Herald Transcript

A general observation is that expanding this to ISD::SELECT now opens this up to vector types. I think the only thing that's now preventing it is isZeroOrAllOnes which "silently" (for want of a better word) only accepts scalar constants.

My first thoughts are that we could either be clearer about not accepting vector types, or - without wanting to expand the scope of this patch - we do open it up to vectors. From a quick play around on top of your patch it doesn't look too useful in that case, though. A vector add_select_all_zeros_v4i32 just introduces an extra vsetvli.

llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5818

"the" condition, perhaps

5846

I know you didn't introduce this but /*AllOnes*/ might be a good change to make while you're here.

craig.topper retitled this revision from Fold (add (select lhs, rhs, cc, 0, y), x) -> (select lhs, rhs, cc, x, (add x, y)) to [RISCV] Fold (add (select lhs, rhs, cc, 0, y), x) -> (select lhs, rhs, cc, x, (add x, y)).Aug 6 2021, 5:01 PM

Address review comments

A general observation is that expanding this to ISD::SELECT now opens this up to vector types. I think the only thing that's now preventing it is isZeroOrAllOnes which "silently" (for want of a better word) only accepts scalar constants.

My first thoughts are that we could either be clearer about not accepting vector types, or - without wanting to expand the scope of this patch - we do open it up to vectors. From a quick play around on top of your patch it doesn't look too useful in that case, though. A vector add_select_all_zeros_v4i32 just introduces an extra vsetvli.

I disabled it for vectors.

frasercrmck accepted this revision.Aug 10 2021, 2:54 AM

LGTM, bearing in mind there are others with more knowledge about the "scalar" side of this target

This revision is now accepted and ready to land.Aug 10 2021, 2:54 AM
This revision was landed with ongoing or failed builds.Aug 10 2021, 9:03 AM
This revision was automatically updated to reflect the committed changes.