[SimplifyCFG] Start redesigning `FoldTwoEntryPHINode()`.
authorRoman Lebedev <lebedev.ri@gmail.com>
Sun, 23 Jan 2022 14:05:22 +0000 (17:05 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Wed, 2 Feb 2022 14:53:56 +0000 (17:53 +0300)
commit1e353f092288309d74d380367aa50bbd383780ed
tree3363a75b775e0571dd36c17fb4538ecfc64fba8c
parent73cb542930bb28424aea4329a43de11b5b3a6761
[SimplifyCFG] Start redesigning `FoldTwoEntryPHINode()`.

The current `FoldTwoEntryPHINode()` is not quite designed correctly.
It starts from the merge point, and then tries to detect
the 'divergence' point.

Because of that, it is limited to the simple two-predecessor case,
where the PHI completely goes away. but that is rather pessimistic,
and it doesn't make much sense from the costmodel side of things.

For example if there is some other unrelated predecessor of
the merge point,  we could split the merge point so that
the then/else blocks first branch to an empty block
and then to the merge point, and then we'd be able to speculate
the then/else code.

But if we'd instead simply start at the divergence point,
and look for the merge point, then we'll just natively support this case.

There's also the fact that `SpeculativelyExecuteBB()` already does
just that, but only if there is a single block to speculate,
and with a much more restrictive cost model.
But that also means we have code duplication.

Now, sadly, while this is as much NFCI as possible,
there is just no way to cleanly migrate to
the proper implementation. The results *are* going to be different
somewhat because of various phase ordering effects and SimplifyCFG
block iteration strategy.
15 files changed:
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/CodeGen/AArch64/check-sign-bit-before-extension.ll
llvm/test/CodeGen/AArch64/combine-comparisons-by-cse.ll
llvm/test/CodeGen/AArch64/typepromotion-phisret.ll
llvm/test/CodeGen/ARM/ifcvt-callback.ll
llvm/test/CodeGen/ARM/ifcvt1.ll
llvm/test/Transforms/PGOProfile/chr.ll
llvm/test/Transforms/PhaseOrdering/X86/earlycse-after-simplifycfg-two-entry-phi-node-folding.ll
llvm/test/Transforms/PhaseOrdering/X86/merge-functions.ll
llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
llvm/test/Transforms/SimplifyCFG/X86/sink-common-code.ll
llvm/test/Transforms/SimplifyCFG/bbi-23595.ll
llvm/test/Transforms/SimplifyCFG/merge-cond-stores-2.ll
llvm/test/Transforms/SimplifyCFG/no-md-sink.ll
llvm/test/Transforms/SimplifyCFG/preserve-store-alignment.ll