FoldPHIArgOpIntoPHI and related routines move operations from PHI
arguments to the result of the PHI node. To avoid creating more
new operations than are removed by the transformation, this is
currently only done if the original operations each have a single
use only (i.e. the PHI node). This is overly pessimistic for
two reasons:
- A single operation can be used multiple times as input to the same PHI node. This is called out in a FIXME in the code: FIXME: The hasOneUse check will fail for PHIs that use the value more than themselves more than once.
- Sometimes there are two (or more) PHI nodes using the same set of arguments (e.g. if a loop variable is also used after the end of the loop). If *all* these PHIs are transformed, the original argument operations will still be dead.
This patch implements an IsFoldPHIArgProfitable routine that
performs a more precise check to determine whether after all
PHI nodes are transformed, there will be no more operations
in total than we had before. This check is then used instead
of the hasOneUse checks throughout FoldPHIArgOpIntoPHI and
related routines.
The existing Transforms/LoopUnroll/runtime-loop-multiple-exits.ll
test case now shows further improvement (it was using a single
operation multiple times in the same PHI). A new test added to
Transforms/InstCombine/phi.ll demonstrates the multiple-PHI case.