This is an archive of the discontinued LLVM Phabricator instance.

[ARM] Prefer lsls+lsrs over lsls+ands or lsrs+ands in Thumb1.
ClosedPublic

Authored by efriedma on Jul 19 2018, 7:37 PM.

Details

Summary

Saves materializing the immediate for the "ands".

Corresponding patterns exist for lsrs+lsls, but that seems less common in practice.

Now implemented as a DAGCombine.

Diff Detail

Repository
rL LLVM

Event Timeline

efriedma created this revision.Jul 19 2018, 7:37 PM

Nice optimisation, looks good to me.

About where this should go, I am also not so sure. Looks like this is not so different from what's already done in Select, but I would propably first have thought about a Combine, and am not sure if it matters.

Perhaps @samparker has an opinion on this.

test/CodeGen/Thumb/shift-and.ll
1 ↗(On Diff #156419)

Nit: remove the NOTE.

6 ↗(On Diff #156419)

Nit: do we need to match bb.0 and entry? Perhaps that's convenient for the check-next.

I did some work for Thumb-2 last year in a similar vain during the combine phase, in PerformSHLSimplify, but this (unsurprisingly) doesn't handle lshr. I didn't find any headaches from changing the canonical form in those cases, so probably would be worth having it there.

efriedma updated this revision to Diff 156611.Jul 20 2018, 2:56 PM
efriedma edited the summary of this revision. (Show Details)

Changed to DAGCombine, added some more tests for related patterns, added some FIXMEs for future improvements.

samparker added inline comments.Jul 23 2018, 2:46 AM
test/CodeGen/Thumb/shift-and.ll
97 ↗(On Diff #156611)

This is the same as the above test. Also, can't this be combined to (shl (lshr %x, 31), 2)?

efriedma added inline comments.Jul 23 2018, 10:33 AM
test/CodeGen/Thumb/shift-and.ll
97 ↗(On Diff #156611)

Oh, oops; I think I meant to change the shift amount between the two tests.

Yes, it can be combined; this is one of the "reversed" patterns I added a FIXME for (the patch currently only produces shl+lshr, not lshr+shl).

Note that DAGCombine already does such a transform, but not in the case of constants (D48768)
Maybe this should be moved there, and exposed as a [yet another] hook.

I don't think this makes sense for other targets... other targets generally have an and-with-immediate instruction, and in that case this transform saves zero instructions. Or do you have some specific target in mind?

efriedma updated this revision to Diff 156897.Jul 23 2018, 3:22 PM

Fix testcase.

I don't think this makes sense for other targets... other targets generally have an and-with-immediate instruction, and in that case this transform saves zero instructions. Or do you have some specific target in mind?

I don't have any specific target in mind, but this is pretty generic, thus i suggested it.

samparker accepted this revision.Jul 25 2018, 2:26 AM

This LGTM. I don't think there's a problem with solving this niche in the backend.

This revision is now accepted and ready to land.Jul 25 2018, 2:26 AM
This revision was automatically updated to reflect the committed changes.