This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] fold add(add(A, ~B), 1) -> sub(A, B)
AbandonedPublic

Authored by deadalnix on Mar 2 2019, 12:15 PM.

Details

Summary

As per title.

Diff Detail

Event Timeline

deadalnix created this revision.Mar 2 2019, 12:15 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 2 2019, 12:15 PM

vector test?

Test for ((A + 1) + ~B) and ((~B + 1) + A) cases?

spatel added inline comments.Mar 3 2019, 6:00 AM
test/Transforms/InstCombine/add.ll
954

This is not testing what you expected. The operands are getting commuted independently of this patch, so it's effectively the same test as the next one. Grep for "thwart" in the instcombine regression test dir to see some options to work around that.

spatel added a comment.Mar 3 2019, 7:08 AM

Test for ((A + 1) + ~B) and ((~B + 1) + A) cases?

We already get that case in instcombine. So this brings up a question of division-of-labor. The -reassociate pass will take the pattern from this patch and form the above because it "ranks" the constant 1 above a variable operand. Then instcombine can fold it. Is that good enough, or do we need to handle a pattern that is not canonical based on the output of -reassociate. Unless we have evidence that this is slipping through a normal opt -O1 pipeline, we probably do not need this patch?

deadalnix updated this revision to Diff 189082.Mar 3 2019, 7:21 AM
deadalnix marked an inline comment as done.

Add extra ops to ensure proper ordering.

Interestingly, IntCombine chose to flip the sign of the constant of the mul and go with an add insteadof a sub, which is also perfectly fine.

Test for ((A + 1) + ~B) and ((~B + 1) + A) cases?

The first is already handled explicitly. The second is handled via ((~B + 1) + A) -> ((0-B) + A) -> A - B . There are tests for both patterns already.

lib/Transforms/InstCombine/InstCombineAddSub.cpp
1177

See here.

The logic/tests look ok, but the question of necessity/usefulness has not been answered. Is there evidence that this pattern will ever reach instcombine in a normal optimization pipeline that includes -reassociate?

@spatel I'm not sure, you tell me :) The history of this patch is that this pattern shows up in practice post legalization, so I submitted a patch for it for DAGCombiner. From there, @ lebedev.ri noticed that InstCombine wasn't doing it and suggested it to be added there as well.

As far as I can tell, -reassociate also fails to trigger this transform: https://godbolt.org/z/gweK6j

@spatel I'm not sure, you tell me :) The history of this patch is that this pattern shows up in practice post legalization, so I submitted a patch for it for DAGCombiner. From there, @ lebedev.ri noticed that InstCombine wasn't doing it and suggested it to be added there as well.

As far as I can tell, -reassociate also fails to trigger this transform: https://godbolt.org/z/gweK6j

You have to run reassociate before instcombine to get the expected ranking of operands:
https://godbolt.org/z/5XHhTh

So I agree that we needed the backend patch, but I don't see any evidence that this is needed within instcombine yet. The difference is that - in the backend - DAGCombiner is doing the job of both of the IR passes (reassociate is a canonicalization pass that works with instcombine). If we don't see this pattern after a normal IR optimization sequence, then I don't think we want to add it to instcombine (it would likely infinitesimally increase compile-time with no potential benefit). One way to see if there's any benefit to this patch would be to add a transform statistic, build the test-suite or some other substantial codebase, and see if this transform ever fires.

Is someone of a different opinion than @spatel ? If not, I'll abandon this patch soon.

Is someone of a different opinion than @spatel ? If not, I'll abandon this patch soon.

I haven't tried this on a whole llvm stage-2 build, but on small-ish codebase i have tried this does not seem to be reachable.
(You could, of course, try stage-2 build, that will have more coverage.)
Sorry for lost effort, i should have seen that -reassociate would have solved this :(

deadalnix abandoned this revision.Mar 30 2019, 6:42 AM

Ok, abandoning it as it doesn't seems to be necessary.