Thread edges through blocks and update the control flow and SSA graphs.
Copyright (C) 2004-2024 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>.
Given a block B, update the CFG and SSA graph to reflect redirecting
one or more in-edges to B to instead reach the destination of an
out-edge from B while preserving any side effects in B.
i.e., given A->B and B->C, change A->B to be A->C yet still preserve the
side effects of executing B.
1. Make a copy of B (including its outgoing edges and statements). Call
the copy B'. Note B' has no incoming edges or PHIs at this time.
2. Remove the control statement at the end of B' and all outgoing edges
except B'->C.
3. Add a new argument to each PHI in C with the same value as the existing
argument associated with edge B->C. Associate the new PHI arguments
with the edge B'->C.
4. For each PHI in B, find or create a PHI in B' with an identical
PHI_RESULT. Add an argument to the PHI in B' which has the same
value as the PHI in B associated with the edge A->B. Associate
the new argument in the PHI in B' with the edge A->B.
5. Change the edge A->B to A->B'.
5a. This automatically deletes any PHI arguments associated with the
edge A->B in B.
5b. This automatically associates each new argument added in step 4
with the edge A->B'.
6. Repeat for other incoming edges into B.
7. Put the duplicated resources in B and all the B' blocks into SSA form.
Note that block duplication can be minimized by first collecting the
set of unique destination blocks that the incoming edges should
be threaded to.
We reduce the number of edges and statements we create by not copying all
the outgoing edges and the control statement in step #1. We instead create
a template block without the outgoing edges and duplicate the template.
Another case this code handles is threading through a "joiner" block. In
this case, we do not know the destination of the joiner block, but one
of the outgoing edges from the joiner block leads to a threadable path. This
case largely works as outlined above, except the duplicate of the joiner
block still contains a full set of outgoing edges and its control statement.
We just redirect one of its outgoing edges to our jump threading path.
Steps #5 and #6 of the above algorithm are best implemented by walking
all the incoming edges which thread to the same destination edge at
the same time. That avoids lots of table lookups to get information
for the destination edge.
To realize that implementation we create a list of incoming edges
which thread to the same outgoing edge. Thus to implement steps
#5 and #6 we traverse our hash table of outgoing edge information.
For each entry we walk the list of incoming edges which thread to
the current outgoing edge.