1 /* Thread edges through blocks and update the control flow and SSA graphs.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "coretypes.h"
30 #include "basic-block.h"
35 #include "diagnostic.h"
36 #include "tree-flow.h"
37 #include "tree-dump.h"
38 #include "tree-pass.h"
41 /* Given a block B, update the CFG and SSA graph to reflect redirecting
42 one or more in-edges to B to instead reach the destination of an
43 out-edge from B while preserving any side effects in B.
45 i.e., given A->B and B->C, change A->B to be A->C yet still preserve the
46 side effects of executing B.
48 1. Make a copy of B (including its outgoing edges and statements). Call
49 the copy B'. Note B' has no incoming edges or PHIs at this time.
51 2. Remove the control statement at the end of B' and all outgoing edges
54 3. Add a new argument to each PHI in C with the same value as the existing
55 argument associated with edge B->C. Associate the new PHI arguments
58 4. For each PHI in B, find or create a PHI in B' with an identical
59 PHI_RESULT. Add an argument to the PHI in B' which has the same
60 value as the PHI in B associated with the edge A->B. Associate
61 the new argument in the PHI in B' with the edge A->B.
63 5. Change the edge A->B to A->B'.
65 5a. This automatically deletes any PHI arguments associated with the
68 5b. This automatically associates each new argument added in step 4
71 6. Repeat for other incoming edges into B.
73 7. Put the duplicated resources in B and all the B' blocks into SSA form.
75 Note that block duplication can be minimized by first collecting the
76 the set of unique destination blocks that the incoming edges should
77 be threaded to. Block duplication can be further minimized by using
78 B instead of creating B' for one destination if all edges into B are
79 going to be threaded to a successor of B.
81 We further reduce the number of edges and statements we create by
82 not copying all the outgoing edges and the control statement in
83 step #1. We instead create a template block without the outgoing
84 edges and duplicate the template. */
87 /* Steps #5 and #6 of the above algorithm are best implemented by walking
88 all the incoming edges which thread to the same destination edge at
89 the same time. That avoids lots of table lookups to get information
90 for the destination edge.
92 To realize that implementation we create a list of incoming edges
93 which thread to the same outgoing edge. Thus to implement steps
94 #5 and #6 we traverse our hash table of outgoing edge information.
95 For each entry we walk the list of incoming edges which thread to
96 the current outgoing edge. */
104 /* Main data structure recording information regarding B's duplicate
107 /* We need to efficiently record the unique thread destinations of this
108 block and specific information associated with those destinations. We
109 may have many incoming edges threaded to the same outgoing edge. This
110 can be naturally implemented with a hash table. */
112 struct redirection_data
114 /* A duplicate of B with the trailing control statement removed and which
115 targets a single successor of B. */
116 basic_block dup_block;
118 /* An outgoing edge from B. DUP_BLOCK will have OUTGOING_EDGE->dest as
119 its single successor. */
122 /* A list of incoming edges which we want to thread to
123 OUTGOING_EDGE->dest. */
124 struct el *incoming_edges;
126 /* Flag indicating whether or not we should create a duplicate block
127 for this thread destination. This is only true if we are threading
128 all incoming edges and thus are using BB itself as a duplicate block. */
129 bool do_not_duplicate;
132 /* Main data structure to hold information for duplicates of BB. */
133 static htab_t redirection_data;
135 bool rediscover_loops_after_threading;
137 /* Data structure of information to pass to hash table traversal routines. */
140 /* The current block we are working on. */
143 /* A template copy of BB with no outgoing edges or control statement that
144 we use for creating copies. */
145 basic_block template_block;
147 /* TRUE if we thread one or more jumps, FALSE otherwise. */
151 /* Remove the last statement in block BB if it is a control statement
152 Also remove all outgoing edges except the edge which reaches DEST_BB.
153 If DEST_BB is NULL, then remove all outgoing edges. */
156 remove_ctrl_stmt_and_useless_edges (basic_block bb, basic_block dest_bb)
158 block_stmt_iterator bsi;
164 /* If the duplicate ends with a control statement, then remove it.
166 Note that if we are duplicating the template block rather than the
167 original basic block, then the duplicate might not have any real
171 && (TREE_CODE (bsi_stmt (bsi)) == COND_EXPR
172 || TREE_CODE (bsi_stmt (bsi)) == SWITCH_EXPR))
175 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
177 if (e->dest != dest_bb)
184 /* Create a duplicate of BB which only reaches the destination of the edge
185 stored in RD. Record the duplicate block in RD. */
188 create_block_for_threading (basic_block bb, struct redirection_data *rd)
190 /* We can use the generic block duplication code and simply remove
191 the stuff we do not need. */
192 rd->dup_block = duplicate_block (bb, NULL);
194 /* Zero out the profile, since the block is unreachable for now. */
195 rd->dup_block->frequency = 0;
196 rd->dup_block->count = 0;
198 /* The call to duplicate_block will copy everything, including the
199 useless COND_EXPR or SWITCH_EXPR at the end of BB. We just remove
200 the useless COND_EXPR or SWITCH_EXPR here rather than having a
201 specialized block copier. We also remove all outgoing edges
202 from the duplicate block. The appropriate edge will be created
204 remove_ctrl_stmt_and_useless_edges (rd->dup_block, NULL);
207 /* Hashing and equality routines for our hash table. */
209 redirection_data_hash (const void *p)
211 edge e = ((struct redirection_data *)p)->outgoing_edge;
212 return e->dest->index;
216 redirection_data_eq (const void *p1, const void *p2)
218 edge e1 = ((struct redirection_data *)p1)->outgoing_edge;
219 edge e2 = ((struct redirection_data *)p2)->outgoing_edge;
224 /* Given an outgoing edge E lookup and return its entry in our hash table.
226 If INSERT is true, then we insert the entry into the hash table if
227 it is not already present. INCOMING_EDGE is added to the list of incoming
228 edges associated with E in the hash table. */
230 static struct redirection_data *
231 lookup_redirection_data (edge e, edge incoming_edge, bool insert)
234 struct redirection_data *elt;
236 /* Build a hash table element so we can see if E is already
238 elt = xmalloc (sizeof (struct redirection_data));
239 elt->outgoing_edge = e;
240 elt->dup_block = NULL;
241 elt->do_not_duplicate = false;
242 elt->incoming_edges = NULL;
244 slot = htab_find_slot (redirection_data, elt, insert);
246 /* This will only happen if INSERT is false and the entry is not
247 in the hash table. */
254 /* This will only happen if E was not in the hash table and
259 elt->incoming_edges = xmalloc (sizeof (struct el));
260 elt->incoming_edges->e = incoming_edge;
261 elt->incoming_edges->next = NULL;
264 /* E was in the hash table. */
267 /* Free ELT as we do not need it anymore, we will extract the
268 relevant entry from the hash table itself. */
271 /* Get the entry stored in the hash table. */
272 elt = (struct redirection_data *) *slot;
274 /* If insertion was requested, then we need to add INCOMING_EDGE
275 to the list of incoming edges associated with E. */
278 struct el *el = xmalloc (sizeof (struct el));
279 el->next = elt->incoming_edges;
280 el->e = incoming_edge;
281 elt->incoming_edges = el;
288 /* Given a duplicate block and its single destination (both stored
289 in RD). Create an edge between the duplicate and its single
292 Add an additional argument to any PHI nodes at the single
296 create_edge_and_update_destination_phis (struct redirection_data *rd)
298 edge e = make_edge (rd->dup_block, rd->outgoing_edge->dest, EDGE_FALLTHRU);
301 /* If there are any PHI nodes at the destination of the outgoing edge
302 from the duplicate block, then we will need to add a new argument
303 to them. The argument should have the same value as the argument
304 associated with the outgoing edge stored in RD. */
305 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
307 int indx = rd->outgoing_edge->dest_idx;
308 add_phi_arg (phi, PHI_ARG_DEF_TREE (phi, indx), e);
312 /* Hash table traversal callback routine to create duplicate blocks. */
315 create_duplicates (void **slot, void *data)
317 struct redirection_data *rd = (struct redirection_data *) *slot;
318 struct local_info *local_info = (struct local_info *)data;
320 /* If this entry should not have a duplicate created, then there's
322 if (rd->do_not_duplicate)
325 /* Create a template block if we have not done so already. Otherwise
326 use the template to create a new block. */
327 if (local_info->template_block == NULL)
329 create_block_for_threading (local_info->bb, rd);
330 local_info->template_block = rd->dup_block;
332 /* We do not create any outgoing edges for the template. We will
333 take care of that in a later traversal. That way we do not
334 create edges that are going to just be deleted. */
338 create_block_for_threading (local_info->template_block, rd);
340 /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
342 create_edge_and_update_destination_phis (rd);
345 /* Keep walking the hash table. */
349 /* We did not create any outgoing edges for the template block during
350 block creation. This hash table traversal callback creates the
351 outgoing edge for the template block. */
354 fixup_template_block (void **slot, void *data)
356 struct redirection_data *rd = (struct redirection_data *) *slot;
357 struct local_info *local_info = (struct local_info *)data;
359 /* If this is the template block, then create its outgoing edges
360 and halt the hash table traversal. */
361 if (rd->dup_block && rd->dup_block == local_info->template_block)
363 create_edge_and_update_destination_phis (rd);
370 /* Not all jump threading requests are useful. In particular some
371 jump threading requests can create irreducible regions which are
374 This routine will examine the BB's incoming edges for jump threading
375 requests which, if acted upon, would create irreducible regions. Any
376 such jump threading requests found will be pruned away. */
379 prune_undesirable_thread_requests (basic_block bb)
383 bool may_create_irreducible_region = false;
384 unsigned int num_outgoing_edges_into_loop = 0;
386 /* For the heuristics below, we need to know if BB has more than
387 one outgoing edge into a loop. */
388 FOR_EACH_EDGE (e, ei, bb->succs)
389 num_outgoing_edges_into_loop += ((e->flags & EDGE_LOOP_EXIT) == 0);
391 if (num_outgoing_edges_into_loop > 1)
393 edge backedge = NULL;
395 /* Consider the effect of threading the edge (0, 1) to 2 on the left
396 CFG to produce the right CFG:
410 Threading the (0, 1) edge to 2 effectively creates two loops
411 (2, 4, 1) and (4, 1, 3) which are neither disjoint nor nested.
414 However, we do need to be able to thread (0, 1) to 2 or 3
415 in the left CFG below (which creates the middle and right
416 CFGs with nested loops).
420 1<--+ 2<----+ 3<-+<-+
424 3---+ 1--+--+ 2-----+
427 A safe heuristic appears to be to only allow threading if BB
428 has a single incoming backedge from one of its direct successors. */
430 FOR_EACH_EDGE (e, ei, bb->preds)
432 if (e->flags & EDGE_DFS_BACK)
446 if (backedge && find_edge (bb, backedge->src))
449 may_create_irreducible_region = true;
455 /* If we thread across the loop entry block (BB) into the
456 loop and BB is still reached from outside the loop, then
457 we would create an irreducible CFG. Consider the effect
458 of threading the edge (1, 4) to 5 on the left CFG to produce
470 Threading the (1, 4) edge to 5 creates two entry points
471 into the loop (4, 5) (one from block 1, the other from
472 block 2). A classic irreducible region.
474 So look at all of BB's incoming edges which are not
475 backedges and which are not threaded to the loop exit.
476 If that subset of incoming edges do not all thread
477 to the same block, then threading any of them will create
478 an irreducible region. */
480 FOR_EACH_EDGE (e, ei, bb->preds)
484 /* We ignore back edges for now. This may need refinement
485 as threading a backedge creates an inner loop which
486 we would need to verify has a single entry point.
488 If all backedges thread to new locations, then this
489 block will no longer have incoming backedges and we
490 need not worry about creating irreducible regions
491 by threading through BB. I don't think this happens
492 enough in practice to worry about it. */
493 if (e->flags & EDGE_DFS_BACK)
496 /* If the incoming edge threads to the loop exit, then it
499 if (e2 && (e2->flags & EDGE_LOOP_EXIT))
502 /* E enters the loop header and is not threaded. We can
503 not allow any other incoming edges to thread into
504 the loop as that would create an irreducible region. */
507 may_create_irreducible_region = true;
511 /* We know that this incoming edge threads to a block inside
512 the loop. This edge must thread to the same target in
513 the loop as any previously seen threaded edges. Otherwise
514 we will create an irreducible region. */
519 may_create_irreducible_region = true;
525 /* If we might create an irreducible region, then cancel any of
526 the jump threading requests for incoming edges which are
527 not backedges and which do not thread to the exit block. */
528 if (may_create_irreducible_region)
530 FOR_EACH_EDGE (e, ei, bb->preds)
534 /* Ignore back edges. */
535 if (e->flags & EDGE_DFS_BACK)
540 /* If this incoming edge was not threaded, then there is
545 /* If this incoming edge threaded to the loop exit,
546 then it can be ignored as it is safe. */
547 if (e2->flags & EDGE_LOOP_EXIT)
552 /* This edge threaded into the loop and the jump thread
553 request must be cancelled. */
554 if (dump_file && (dump_flags & TDF_DETAILS))
555 fprintf (dump_file, " Not threading jump %d --> %d to %d\n",
556 e->src->index, e->dest->index, e2->dest->index);
563 /* Hash table traversal callback to redirect each incoming edge
564 associated with this hash table element to its new destination. */
567 redirect_edges (void **slot, void *data)
569 struct redirection_data *rd = (struct redirection_data *) *slot;
570 struct local_info *local_info = (struct local_info *)data;
571 struct el *next, *el;
573 /* Walk over all the incoming edges associated associated with this
575 for (el = rd->incoming_edges; el; el = next)
579 /* Go ahead and free this element from the list. Doing this now
580 avoids the need for another list walk when we destroy the hash
585 /* Go ahead and clear E->aux. It's not needed anymore and failure
586 to clear it will cause all kinds of unpleasant problems later. */
593 if (dump_file && (dump_flags & TDF_DETAILS))
594 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
595 e->src->index, e->dest->index, rd->dup_block->index);
597 /* Redirect the incoming edge to the appropriate duplicate
599 e2 = redirect_edge_and_branch (e, rd->dup_block);
600 flush_pending_stmts (e2);
602 if ((dump_file && (dump_flags & TDF_DETAILS))
603 && e->src != e2->src)
604 fprintf (dump_file, " basic block %d created\n", e2->src->index);
608 if (dump_file && (dump_flags & TDF_DETAILS))
609 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
610 e->src->index, e->dest->index, local_info->bb->index);
612 /* We are using BB as the duplicate. Remove the unnecessary
613 outgoing edges and statements from BB. */
614 remove_ctrl_stmt_and_useless_edges (local_info->bb,
615 rd->outgoing_edge->dest);
617 /* And fixup the flags on the single remaining edge. */
618 EDGE_SUCC (local_info->bb, 0)->flags
619 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
620 EDGE_SUCC (local_info->bb, 0)->flags |= EDGE_FALLTHRU;
624 /* Indicate that we actually threaded one or more jumps. */
625 if (rd->incoming_edges)
626 local_info->jumps_threaded = true;
631 /* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
632 is reached via one or more specific incoming edges, we know which
633 outgoing edge from BB will be traversed.
635 We want to redirect those incoming edges to the target of the
636 appropriate outgoing edge. Doing so avoids a conditional branch
637 and may expose new optimization opportunities. Note that we have
638 to update dominator tree and SSA graph after such changes.
640 The key to keeping the SSA graph update manageable is to duplicate
641 the side effects occurring in BB so that those side effects still
642 occur on the paths which bypass BB after redirecting edges.
644 We accomplish this by creating duplicates of BB and arranging for
645 the duplicates to unconditionally pass control to one specific
646 successor of BB. We then revector the incoming edges into BB to
647 the appropriate duplicate of BB.
649 BB and its duplicates will have assignments to the same set of
650 SSA_NAMEs. Right now, we just call into rewrite_ssa_into_ssa
651 to update the SSA graph for those names.
653 We are also going to experiment with a true incremental update
654 scheme for the duplicated resources. One of the interesting
655 properties we can exploit here is that all the resources set
656 in BB will have the same IDFS, so we have one IDFS computation
657 per block with incoming threaded edges, which can lower the
658 cost of the true incremental update algorithm. */
661 thread_block (basic_block bb)
663 /* E is an incoming edge into BB that we may or may not want to
664 redirect to a duplicate of BB. */
667 struct local_info local_info;
669 /* FOUND_BACKEDGE indicates that we found an incoming backedge
670 into BB, in which case we may ignore certain jump threads
671 to avoid creating irreducible regions. */
672 bool found_backedge = false;
674 /* ALL indicates whether or not all incoming edges into BB should
675 be threaded to a duplicate of BB. */
678 /* To avoid scanning a linear array for the element we need we instead
679 use a hash table. For normal code there should be no noticeable
680 difference. However, if we have a block with a large number of
681 incoming and outgoing edges such linear searches can get expensive. */
682 redirection_data = htab_create (EDGE_COUNT (bb->succs),
683 redirection_data_hash,
687 FOR_EACH_EDGE (e, ei, bb->preds)
688 found_backedge |= ((e->flags & EDGE_DFS_BACK) != 0);
690 /* If BB has incoming backedges, then threading across BB might
691 introduce an irreducible region, which would be undesirable
692 as that inhibits various optimizations later. Prune away
693 any jump threading requests which we know will result in
694 an irreducible region. */
696 prune_undesirable_thread_requests (bb);
698 /* Record each unique threaded destination into a hash table for
699 efficient lookups. */
700 FOR_EACH_EDGE (e, ei, bb->preds)
710 /* If we thread to a loop exit edge, then we will need to
711 rediscover the loop exit edges. While it may seem that
712 the new edge is a loop exit edge, that is not the case.
713 Consider threading the edge (5,6) to E in the CFG on the
714 left which creates the CFG on the right:
729 After threading, the edge (0, 1) is the loop exit edge and
730 the nodes 0, 2, 6 are the only nodes in the loop. */
731 if (e2->flags & EDGE_LOOP_EXIT)
732 rediscover_loops_after_threading = true;
734 /* Insert the outgoing edge into the hash table if it is not
735 already in the hash table. */
736 lookup_redirection_data (e2, e, true);
740 /* If we are going to thread all incoming edges to an outgoing edge, then
741 BB will become unreachable. Rather than just throwing it away, use
742 it for one of the duplicates. Mark the first incoming edge with the
743 DO_NOT_DUPLICATE attribute. */
746 edge e = EDGE_PRED (bb, 0)->aux;
747 lookup_redirection_data (e, NULL, false)->do_not_duplicate = true;
750 /* Now create duplicates of BB.
752 Note that for a block with a high outgoing degree we can waste
753 a lot of time and memory creating and destroying useless edges.
755 So we first duplicate BB and remove the control structure at the
756 tail of the duplicate as well as all outgoing edges from the
757 duplicate. We then use that duplicate block as a template for
758 the rest of the duplicates. */
759 local_info.template_block = NULL;
761 local_info.jumps_threaded = false;
762 htab_traverse (redirection_data, create_duplicates, &local_info);
764 /* The template does not have an outgoing edge. Create that outgoing
765 edge and update PHI nodes as the edge's target as necessary.
767 We do this after creating all the duplicates to avoid creating
768 unnecessary edges. */
769 htab_traverse (redirection_data, fixup_template_block, &local_info);
771 /* The hash table traversals above created the duplicate blocks (and the
772 statements within the duplicate blocks). This loop creates PHI nodes for
773 the duplicated blocks and redirects the incoming edges into BB to reach
774 the duplicates of BB. */
775 htab_traverse (redirection_data, redirect_edges, &local_info);
777 /* Done with this block. Clear REDIRECTION_DATA. */
778 htab_delete (redirection_data);
779 redirection_data = NULL;
781 /* Indicate to our caller whether or not any jumps were threaded. */
782 return local_info.jumps_threaded;
785 /* Walk through all blocks and thread incoming edges to the block's
786 destinations as requested. This is the only entry point into this
789 Blocks which have one or more incoming edges have INCOMING_EDGE_THREADED
790 set in the block's annotation.
792 Each edge that should be threaded has the new destination edge stored in
793 the original edge's AUX field.
795 This routine (or one of its callees) will clear INCOMING_EDGE_THREADED
796 in the block annotations and the AUX field in the edges.
798 It is the caller's responsibility to fix the dominance information
799 and rewrite duplicated SSA_NAMEs back into SSA form.
801 Returns true if one or more edges were threaded, false otherwise. */
804 thread_through_all_blocks (void)
809 rediscover_loops_after_threading = false;
813 if (bb_ann (bb)->incoming_edge_threaded)
815 retval |= thread_block (bb);
816 bb_ann (bb)->incoming_edge_threaded = false;