This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] enable more factorization in SimplifyUsingDistributiveLaws
AbandonedPublic

Authored by Allen on Oct 24 2022, 10:24 AM.

Details

Summary

This is one of the IR transforms suggested in issue #57255.
https://godbolt.org/z/P9rMGhTcE.
Now we already support to transform t*5 + t into t * (5+1), but not for t - 1 + 5 * t
so the patch try logic: (t-1)+(5*t) -> (t*5)+(t-1) -> (t*5+t)-1 = t*(5+1)-1

Depend D132412

Diff Detail

Event Timeline

Allen created this revision.Oct 24 2022, 10:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 24 2022, 10:24 AM
Allen requested review of this revision.Oct 24 2022, 10:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 24 2022, 10:24 AM

Please add more tests

Allen updated this revision to Diff 470990.Oct 26 2022, 7:17 PM

Add 4 more tests according comment

spatel added inline comments.Oct 27 2022, 8:19 AM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
759

Make formatting changes independently of this patch or just leave it unchanged.

775

It seems odd for this function to specify the exact opcode. Everything else is generalized based on association and distribution laws. Is mul really the only opcode where this transform works?

780

Use plain `cast' if we don't use the return value. Alternatively, give it a name and use it:

if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I)) {
  cast<Instruction>(New)->setHasNoSignedWrap(OBO.hasNoSignedWrap())

How did we guarantee that "New" is an OBO instruction?

If it is safe to propagate no-wrap like this, then we need tests and Alive2 proofs to show it.

787–788

Is this change independent of the one above? If so, we should have separate patches and tests, so it is clear what this does.

llvm/test/Transforms/InstCombine/add4.ll
131

(x + (-1)) + (x * 5) --> (x * 6) + (-1)

144

(x * 4) + (x - 1) --> (x * 5) + (-1)

Is this converted to shl first?

157

(x + y) + (x * 4) --> (x * 5) + y

Is this converted to shl first?

170

(x + 2) + (x * t) --> (t + 1) * x + 2

spatel edited the summary of this revision. (Show Details)Oct 27 2022, 8:22 AM
Allen added inline comments.Oct 27 2022, 9:05 AM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
775

Thanks, I restrict to mul as some performance regression of logic combination tests

787–788

Yes, for case (t-1)+(5*t) -> (t*5)+(t-1) -> (t*5+t)-1

  • here is the 1st step: (t-1)+(5*t) -> (t*5)+(t-1)
  • above transform is the 2ns step: (t*5)+(t-1) -> (t*5+t)-1
Allen marked 5 inline comments as done.Oct 27 2022, 8:33 PM
Allen added inline comments.
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
759

Thansk for your suggestion, addressed https://reviews.llvm.org/D136912

llvm/test/Transforms/InstCombine/add4.ll
157

Instcombine is executed many times, and we have a chance to match the mul version.

Allen updated this revision to Diff 471992.Oct 31 2022, 7:11 AM
Allen marked 2 inline comments as done.
Allen added reviewers: efriedma, dmgreen.

Address comment, I think it is better to separate the precommit test after accept the total changes.

Allen marked 2 inline comments as done and an inline comment as not done.Oct 31 2022, 7:19 AM
Allen added inline comments.
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
780

As we have condition TopLevelOpcode == RHSOpcode, so the operand of I and New must be same, this can guard the "New" is an OBO instruction

780

I don't try to update the no-wrap to start begin, and will check that if the basic changes is accepted.

787–788

Ok, I'll separate the function after this patch reviewed to make sure this is the direction.

Allen updated this revision to Diff 473395.Nov 4 2022, 9:00 PM
Allen marked an inline comment as done.

Rebase top to fix the failed case in file Transforms/PGOProfile/chr.ll

Allen planned changes to this revision.Nov 4 2022, 9:01 PM
Allen requested review of this revision.Nov 4 2022, 9:10 PM
spatel resigned from this revision.Nov 8 2022, 5:13 AM
spatel added a subscriber: spatel.
Allen abandoned this revision.Jul 2 2023, 8:32 PM