This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] Combine shr(shl x, c1), c2 to G_SBFX/G_UBFX
ClosedPublic

Authored by gargaroff on Aug 3 2021, 2:06 AM.

Diff Detail

Event Timeline

gargaroff created this revision.Aug 3 2021, 2:06 AM
gargaroff requested review of this revision.Aug 3 2021, 2:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 3 2021, 2:06 AM
foad added a reviewer: foad.Aug 3 2021, 5:10 AM
foad added a subscriber: foad.
foad added inline comments.
llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
544

This comment suggests that "n" is the same value on the LHS and the RHS, which is not true.

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
4155

You don't want a "one use" check for a constant.

4161

I think it would be better to combine this and the previous mi_match into one large match expression.

4163–4164

Since ShlAmt is signed don't you also need to check it's >= 0? (TBH I'm not sure how C++ compares int64_t vs unsigned.)

Anyway you might be able to simplify some of the bounds checking. I think all you need to check is: 0 <= ShlAmt <= ShrAmt < Size. Then the checks on Width and Pos should be unnecessary by construction.

4169

This is just Size - ShrAmt!

gargaroff updated this revision to Diff 363712.Aug 3 2021, 6:02 AM
gargaroff marked 5 inline comments as done.

Address comments

llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
544

Good point. Fixed!

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
4169

Good catch, thanks for spotting!

foad added a comment.Aug 3 2021, 6:35 AM

How does this new combine interact with matchAshrShlToSextInreg? If n==k, aren't they both trying to combine the same thing with different results?

How does this new combine interact with matchAshrShlToSextInreg? If n==k, aren't they both trying to combine the same thing with different results?

In that case they would indeed try to combine the same thing. Looks like SextInreg combine has a higher precedence, as no other tests were affected. Should we skip this new combine if n==k?

foad added a comment.Aug 3 2021, 6:43 AM

Looks like SextInreg combine has a higher precedence, as no other tests were affected. Should we skip this new combine if n==k?

I don't know! Is there a well defined way of ensuring that SextInreg has higher precedence? Or is it just luck?

(Actually from an engineering perspective I would prefer that we just have a single combine, that generates sext_inreg if signed&&n==k and sbfx/ubfx otherwise. But I'm not sure that other globalisel folks would agree.)

gargaroff updated this revision to Diff 363724.Aug 3 2021, 6:49 AM

Skip combine if ShlAmt == ShrAmt

AFAIK the order of the combines depends on the order in Combine.td. But I'm not sure if that's something we should rely on here and I would argue that G_SEXT_INREG should be preferred over G_SBFX. I have updated the patch to skip the combine if ShlAmt == ShrAmt.
But just in case, let's wait until tomorrow to give the others a chance to voice their opinion.

foad added inline comments.Aug 3 2021, 6:53 AM
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
4161

This will also skip the unsigned n==k case, which you probably do want to turn into ubfx?

gargaroff updated this revision to Diff 363726.Aug 3 2021, 6:59 AM
gargaroff marked an inline comment as done.

do not skip ubfx combine if shift sizes match

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
4161

Yep, was a bit too trigger happy :) Fixed now.

Ping. I'm leaving for vacation this weekend. Could we get this landed before that?

foad added a comment.Aug 5 2021, 1:01 AM

LGTM apart from the question inline.

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
4145

Lots of combines use isLegalOrBeforeLegalizer, i.e. they do apply before legalization. Is there a reason you have made this one different, so it does not apply before legalization?

gargaroff marked an inline comment as done.Aug 5 2021, 4:40 AM
gargaroff added inline comments.
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
4145

For that I followed the other bitfield extract combine. I'm guessing because there is only widenScalar implemented for G_SBFX and G_UBFX, so you wouldn't even be able to lower it if it would be illegal for your target.

Once at least the lower action is implemented, this check can be changed to make this combine also run before the legalizer.

foad accepted this revision.Aug 5 2021, 4:43 AM
This revision is now accepted and ready to land.Aug 5 2021, 4:43 AM
This revision was automatically updated to reflect the committed changes.
gargaroff marked an inline comment as done.