As per title.
Details
Diff Detail
- Repository
- rL LLVM
- Build Status
Buildable 28728 Build 28727: arc lint + arc unit
Event Timeline
test/Transforms/InstCombine/add.ll | ||
---|---|---|
955 | 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. |
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?
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.
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
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.
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 :(
See here.