This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] PHI-of-insertvalues -> insertvalue-of-PHI's
ClosedPublic

Authored by lebedev.ri on Aug 20 2020, 10:15 AM.

Details

Summary

As per statistic, this happens pretty exceedingly rare,
but i have seen it in exactly the situations the
Phi-aware aggregate reconstruction would have handled,
eventually, and allowed invoke -> call fold later on.

So while this might be something that other fold
will have to learn about, i wonder if we should be
doing this transform in general?

Here, we are okay with adding two PHI's to get both the base aggregate,
and the inserted value. I'm not sure it makes much sense to restrict
it to a single phi (to just the inserted value?), because originally
we'd be receiving the final aggregate already..

llvm test-suite + RawSpeed:

| statistic name                             | baseline  | proposed  |    Δ |      % | \|%\| |
|--------------------------------------------|-----------|-----------|-----:|-------:|------:|
| instcombine.InsertValueOfPHIs              | 0         | 12        |  12  |  0.00% | 0.00% |
| asm-printer.EmittedInsts                   | 8926643   | 8926595   | -48  |  0.00% | 0.00% |
| instcombine.NumCombined                    | 3846614   | 3846640   |  26  |  0.00% | 0.00% |
| instcombine.NumConstProp                   | 24302     | 24293     |  -9  | -0.04% | 0.04% |
| instcombine.NumDeadInst                    | 1620140   | 1620112   | -28  |  0.00% | 0.00% |
| instcount.NumBrInst                        | 898466    | 898464    |  -2  |  0.00% | 0.00% |
| instcount.NumCallInst                      | 1760819   | 1760875   |  56  |  0.00% | 0.00% |
| instcount.NumExtractValueInst              | 45659     | 45649     | -10  | -0.02% | 0.02% |
| instcount.NumInsertValueInst               | 4991      | 4981      | -10  | -0.20% | 0.20% |
| instcount.NumIntToPtrInst                  | 27084     | 27087     |   3  |  0.01% | 0.01% |
| instcount.NumPHIInst                       | 371435    | 371429    |  -6  |  0.00% | 0.00% |
| instcount.NumStoreInst                     | 906011    | 906019    |   8  |  0.00% | 0.00% |
| instcount.TotalBlocks                      | 1105520   | 1105518   |  -2  |  0.00% | 0.00% |
| instcount.TotalInsts                       | 9795737   | 9795776   |  39  |  0.00% | 0.00% |
| simplifycfg.NumInvokes                     | 2784      | 2786      |   2  |  0.07% | 0.07% |
| simplifycfg.NumSimpl                       | 1001840   | 1001850   |  10  |  0.00% | 0.00% |
| simplifycfg.NumSinkCommonInstrs            | 15174     | 15170     |  -4  | -0.03% | 0.03% |

Diff Detail

Event Timeline

lebedev.ri created this revision.Aug 20 2020, 10:15 AM
lebedev.ri requested review of this revision.Aug 20 2020, 10:15 AM

This seems ok to me, but I haven't looked at insertvalue/extractvalue patterns very much, so you might want to get a 2nd opinion.

llvm/lib/Transforms/InstCombine/InstCombineInternal.h
618

I view it is an opportunity to update formatting of related code if we are adding similar but new functionality.
Otherwise, we are going to be stuck with those capital letters forever. :)

llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
309

We are allowing multi-index aggregates, right? If so, we should have at least 1 test to verify that pattern is working as expected.

315

Testing my limits of C++... is seq() better/different than just listing the values as {0, 1}?

317

typo: receive

lebedev.ri added inline comments.Aug 24 2020, 11:57 AM
llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
315

Right, in this case it's not, will change.

lebedev.ri marked 4 inline comments as done.

@spatel thank you for taking a look!
Addressing review comments.
I don't believe there are any sharp edges under the water in this case, it really should be that simple.

Hmm, i will follow-up with the same for extractvalue, i have an inkling suspicion
that it might just be enough to catch (some?) remaining interesting cases, and maybe spare me
from having to make InstCombinerImpl::foldAggregateConstructionIntoAggregateReuse() more recursive.

spatel accepted this revision.Aug 24 2020, 1:32 PM

LGTM

This revision is now accepted and ready to land.Aug 24 2020, 1:32 PM

LGTM

Thank you for review!

This revision was landed with ongoing or failed builds.Aug 25 2020, 12:38 AM
This revision was automatically updated to reflect the committed changes.