This is an archive of the discontinued LLVM Phabricator instance.

[gvn] Handle simply phi equivalence cases
ClosedPublic

Authored by reames on Mar 5 2021, 2:43 PM.

Details

Summary

GVN basically doesn't handle phi nodes at all. This is for a reason - we can't value number their inputs since the predecessor blocks have probably not been visited yet.

However, it also creates a significant pass ordering problem. As it stands, instcombine ends up implementing CSE of phi nodes. This means that for any series of CSE opportunities intermixed with phi nodes, we end up having to alternate instcombine and gvn to make progress.

This patch handles the simplest case by simply preprocessing the phi instructions in a block, and CSEing them if they are syntactically identical. This turns out to be powerful enough to handle many cases in a single invocation of GVN since blocks which use the cse'd phi results are visited after the block containing the phi. If there's a CSE opportunity in one the phi predecessors required to recognize the phi CSE opportunity, that will require a second iteration on the function. (Still within a single run of gvn though.)

Compile time wise, this could go either way. On one hand, we're potentially causing GVN to iterate over the function more. On the other, we're cutting down on iterations between two passes and potentially shrinking the IR aggressively. So, a bit unclear what to expect.

Note that this does still rely on instcombine to canonicalize block order of the phis, but that's a one time transformation independent of the values incoming to the phi.

Diff Detail

Event Timeline

reames created this revision.Mar 5 2021, 2:43 PM
reames requested review of this revision.Mar 5 2021, 2:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 5 2021, 2:43 PM

Note that SimplifyCFG also does EliminateDuplicatePHINodes().

nikic accepted this revision.Mar 6 2021, 9:06 AM

LGTM. I think the motivation here is sound, in that CSEing phis may open up further CSE opportunities, so it only makes sense to performs this as part of the same transform.

This revision is now accepted and ready to land.Mar 6 2021, 9:06 AM
This revision was landed with ongoing or failed builds.Mar 6 2021, 9:31 AM
This revision was automatically updated to reflect the committed changes.