This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Add new combine to add folding.
ClosedPublic

Authored by cdawson on May 3 2019, 8:46 AM.

Details

Summary
(X | C1) + C2 --> (X | C1) ^ C1 iff (C1 == -C2)

I verified the correctness using Alive:
https://rise4fun.com/Alive/YNV

This transform enables the following transform that already exists in instcombine:

(X | Y) ^ Y --> X & ~Y

As a result, the full expected transform is:

(X | C1) + C2 --> X & ~C1 iff (C1 == -C2)

There already exists the transform in the sub case:

(X | Y) - Y --> X & ~Y

however this does not trigger in the case where Y is constant due to an earlier transform:

X - (-C) --> X + C

With this new add fold, both the add and sub constant cases are handled.

This is a spin off from: D61307

Diff Detail

Repository
rL LLVM

Event Timeline

cdawson created this revision.May 3 2019, 8:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2019, 8:46 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
cdawson edited the summary of this revision. (Show Details)May 3 2019, 8:51 AM

Needs some negative tests (non-matching scalar constants; non-matching vector constants; vector with undef; matching vector constants, but non-splat (i.e. not all constants in vector are identical); ..)

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
899 ↗(On Diff #198023)

Constant is guaranteed to be on the rhs

llvm/test/Transforms/InstCombine/add.ll
998 ↗(On Diff #198023)

This will already be canonicalized to the @test44.

cdawson updated this revision to Diff 198034.May 3 2019, 9:25 AM
cdawson marked 2 inline comments as done.

Addressed review comments

lebedev.ri added inline comments.May 3 2019, 9:36 AM
llvm/test/Transforms/InstCombine/sub.ll
1281 ↗(On Diff #198034)

Hm, these are tests from D61307, right?
This comment seems wrong, i think it will trigger there.

spatel added a comment.May 3 2019, 9:49 AM

I think we would've avoided some confusion if the tests were committed with baseline checks as a preliminary step. That way, we can see exactly what is and isn't changing with the code patch. Please do that or let me know if I should do that on your behalf.

cdawson updated this revision to Diff 198468.May 7 2019, 7:52 AM

Updated patch after test pre-commit

lebedev.ri accepted this revision.May 7 2019, 8:28 AM

Looks reasonable.

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
896 ↗(On Diff #198468)

this will never be the case

This revision is now accepted and ready to land.May 7 2019, 8:28 AM
This revision was automatically updated to reflect the committed changes.