This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] add support for multi-use Y of (X op Y) op Z --> (Y op Z) op X
ClosedPublic

Authored by Chenbing.Zheng on Aug 27 2022, 12:41 AM.

Details

Summary

For (X op Y) op Z --> (Y op Z) op X
we can still do transform when Y is multi-use. In D131356
limit it to one-use, this patch remove this limit.

This is still not a complete solution, I add a todo test to show it.
In this case, X and Y are both multi use, we can't differentiate how to convert based on this.
But, At least the branch we added won't make the code worse,and it solves half the scenarios

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptAug 27 2022, 12:41 AM
Chenbing.Zheng requested review of this revision.Aug 27 2022, 12:41 AM

I agree this change help to get more opportune to combine. But I still feel a little strange for the non-deterministic transform here.

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
1754–1755

m_BinOp is enough?

Chenbing.Zheng added a reviewer: bcl5980.

address comment

I agree this change help to get more opportune to combine. But I still feel a little strange for the non-deterministic transform here.

Yer, this change will not directly optimize,and the information provided here is limited. When X, Y are both multi-use, We don't know how to transform so that the code is optimized.
But at least this provides more opportunities here.

spatel accepted this revision.Aug 30 2022, 5:11 AM

I agree that this patch is taking the transform further into "-reassociate" territory, but I don't see a specific problem with the minor enhancement, so LGTM.

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
1758

Move this comment line down, so it is above the corresponding code.
Add another comment for:

// (X op Y) opZ --> (X op Z) op Y
This revision is now accepted and ready to land.Aug 30 2022, 5:11 AM

I commit this patch https://reviews.llvm.org/rG35a3048c2501, but forget associate this patch and I will close this.

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
1758

OK, I will modify it when I commit.