EnumSet*.class: Regenerate
[official-gcc.git] / gcc / tree-ssa-threadupdate.c
blobd4d9ee53788ee8680967abed60f972d4dbdcf5fa
1 /* Thread edges through blocks and update the control flow and SSA graphs.
2 Copyright (C) 2004, 2005, 2006, 2007 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 3, or (at your option)
9 any later version.
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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "ggc.h"
29 #include "basic-block.h"
30 #include "output.h"
31 #include "expr.h"
32 #include "function.h"
33 #include "diagnostic.h"
34 #include "tree-flow.h"
35 #include "tree-dump.h"
36 #include "tree-pass.h"
37 #include "cfgloop.h"
39 /* Given a block B, update the CFG and SSA graph to reflect redirecting
40 one or more in-edges to B to instead reach the destination of an
41 out-edge from B while preserving any side effects in B.
43 i.e., given A->B and B->C, change A->B to be A->C yet still preserve the
44 side effects of executing B.
46 1. Make a copy of B (including its outgoing edges and statements). Call
47 the copy B'. Note B' has no incoming edges or PHIs at this time.
49 2. Remove the control statement at the end of B' and all outgoing edges
50 except B'->C.
52 3. Add a new argument to each PHI in C with the same value as the existing
53 argument associated with edge B->C. Associate the new PHI arguments
54 with the edge B'->C.
56 4. For each PHI in B, find or create a PHI in B' with an identical
57 PHI_RESULT. Add an argument to the PHI in B' which has the same
58 value as the PHI in B associated with the edge A->B. Associate
59 the new argument in the PHI in B' with the edge A->B.
61 5. Change the edge A->B to A->B'.
63 5a. This automatically deletes any PHI arguments associated with the
64 edge A->B in B.
66 5b. This automatically associates each new argument added in step 4
67 with the edge A->B'.
69 6. Repeat for other incoming edges into B.
71 7. Put the duplicated resources in B and all the B' blocks into SSA form.
73 Note that block duplication can be minimized by first collecting the
74 the set of unique destination blocks that the incoming edges should
75 be threaded to. Block duplication can be further minimized by using
76 B instead of creating B' for one destination if all edges into B are
77 going to be threaded to a successor of B.
79 We further reduce the number of edges and statements we create by
80 not copying all the outgoing edges and the control statement in
81 step #1. We instead create a template block without the outgoing
82 edges and duplicate the template. */
85 /* Steps #5 and #6 of the above algorithm are best implemented by walking
86 all the incoming edges which thread to the same destination edge at
87 the same time. That avoids lots of table lookups to get information
88 for the destination edge.
90 To realize that implementation we create a list of incoming edges
91 which thread to the same outgoing edge. Thus to implement steps
92 #5 and #6 we traverse our hash table of outgoing edge information.
93 For each entry we walk the list of incoming edges which thread to
94 the current outgoing edge. */
96 struct el
98 edge e;
99 struct el *next;
102 /* Main data structure recording information regarding B's duplicate
103 blocks. */
105 /* We need to efficiently record the unique thread destinations of this
106 block and specific information associated with those destinations. We
107 may have many incoming edges threaded to the same outgoing edge. This
108 can be naturally implemented with a hash table. */
110 struct redirection_data
112 /* A duplicate of B with the trailing control statement removed and which
113 targets a single successor of B. */
114 basic_block dup_block;
116 /* An outgoing edge from B. DUP_BLOCK will have OUTGOING_EDGE->dest as
117 its single successor. */
118 edge outgoing_edge;
120 /* A list of incoming edges which we want to thread to
121 OUTGOING_EDGE->dest. */
122 struct el *incoming_edges;
124 /* Flag indicating whether or not we should create a duplicate block
125 for this thread destination. This is only true if we are threading
126 all incoming edges and thus are using BB itself as a duplicate block. */
127 bool do_not_duplicate;
130 /* Main data structure to hold information for duplicates of BB. */
131 static htab_t redirection_data;
133 /* Data structure of information to pass to hash table traversal routines. */
134 struct local_info
136 /* The current block we are working on. */
137 basic_block bb;
139 /* A template copy of BB with no outgoing edges or control statement that
140 we use for creating copies. */
141 basic_block template_block;
143 /* TRUE if we thread one or more jumps, FALSE otherwise. */
144 bool jumps_threaded;
147 /* Passes which use the jump threading code register jump threading
148 opportunities as they are discovered. We keep the registered
149 jump threading opportunities in this vector as edge pairs
150 (original_edge, target_edge). */
151 static VEC(edge,heap) *threaded_edges;
154 /* Jump threading statistics. */
156 struct thread_stats_d
158 unsigned long num_threaded_edges;
161 struct thread_stats_d thread_stats;
164 /* Remove the last statement in block BB if it is a control statement
165 Also remove all outgoing edges except the edge which reaches DEST_BB.
166 If DEST_BB is NULL, then remove all outgoing edges. */
168 static void
169 remove_ctrl_stmt_and_useless_edges (basic_block bb, basic_block dest_bb)
171 block_stmt_iterator bsi;
172 edge e;
173 edge_iterator ei;
175 bsi = bsi_last (bb);
177 /* If the duplicate ends with a control statement, then remove it.
179 Note that if we are duplicating the template block rather than the
180 original basic block, then the duplicate might not have any real
181 statements in it. */
182 if (!bsi_end_p (bsi)
183 && bsi_stmt (bsi)
184 && (TREE_CODE (bsi_stmt (bsi)) == COND_EXPR
185 || TREE_CODE (bsi_stmt (bsi)) == GOTO_EXPR
186 || TREE_CODE (bsi_stmt (bsi)) == SWITCH_EXPR))
187 bsi_remove (&bsi, true);
189 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
191 if (e->dest != dest_bb)
192 remove_edge (e);
193 else
194 ei_next (&ei);
198 /* Create a duplicate of BB which only reaches the destination of the edge
199 stored in RD. Record the duplicate block in RD. */
201 static void
202 create_block_for_threading (basic_block bb, struct redirection_data *rd)
204 /* We can use the generic block duplication code and simply remove
205 the stuff we do not need. */
206 rd->dup_block = duplicate_block (bb, NULL, NULL);
208 /* Zero out the profile, since the block is unreachable for now. */
209 rd->dup_block->frequency = 0;
210 rd->dup_block->count = 0;
212 /* The call to duplicate_block will copy everything, including the
213 useless COND_EXPR or SWITCH_EXPR at the end of BB. We just remove
214 the useless COND_EXPR or SWITCH_EXPR here rather than having a
215 specialized block copier. We also remove all outgoing edges
216 from the duplicate block. The appropriate edge will be created
217 later. */
218 remove_ctrl_stmt_and_useless_edges (rd->dup_block, NULL);
221 /* Hashing and equality routines for our hash table. */
222 static hashval_t
223 redirection_data_hash (const void *p)
225 edge e = ((const struct redirection_data *)p)->outgoing_edge;
226 return e->dest->index;
229 static int
230 redirection_data_eq (const void *p1, const void *p2)
232 edge e1 = ((const struct redirection_data *)p1)->outgoing_edge;
233 edge e2 = ((const struct redirection_data *)p2)->outgoing_edge;
235 return e1 == e2;
238 /* Given an outgoing edge E lookup and return its entry in our hash table.
240 If INSERT is true, then we insert the entry into the hash table if
241 it is not already present. INCOMING_EDGE is added to the list of incoming
242 edges associated with E in the hash table. */
244 static struct redirection_data *
245 lookup_redirection_data (edge e, edge incoming_edge, enum insert_option insert)
247 void **slot;
248 struct redirection_data *elt;
250 /* Build a hash table element so we can see if E is already
251 in the table. */
252 elt = XNEW (struct redirection_data);
253 elt->outgoing_edge = e;
254 elt->dup_block = NULL;
255 elt->do_not_duplicate = false;
256 elt->incoming_edges = NULL;
258 slot = htab_find_slot (redirection_data, elt, insert);
260 /* This will only happen if INSERT is false and the entry is not
261 in the hash table. */
262 if (slot == NULL)
264 free (elt);
265 return NULL;
268 /* This will only happen if E was not in the hash table and
269 INSERT is true. */
270 if (*slot == NULL)
272 *slot = (void *)elt;
273 elt->incoming_edges = XNEW (struct el);
274 elt->incoming_edges->e = incoming_edge;
275 elt->incoming_edges->next = NULL;
276 return elt;
278 /* E was in the hash table. */
279 else
281 /* Free ELT as we do not need it anymore, we will extract the
282 relevant entry from the hash table itself. */
283 free (elt);
285 /* Get the entry stored in the hash table. */
286 elt = (struct redirection_data *) *slot;
288 /* If insertion was requested, then we need to add INCOMING_EDGE
289 to the list of incoming edges associated with E. */
290 if (insert)
292 struct el *el = XNEW (struct el);
293 el->next = elt->incoming_edges;
294 el->e = incoming_edge;
295 elt->incoming_edges = el;
298 return elt;
302 /* Given a duplicate block and its single destination (both stored
303 in RD). Create an edge between the duplicate and its single
304 destination.
306 Add an additional argument to any PHI nodes at the single
307 destination. */
309 static void
310 create_edge_and_update_destination_phis (struct redirection_data *rd)
312 edge e = make_edge (rd->dup_block, rd->outgoing_edge->dest, EDGE_FALLTHRU);
313 tree phi;
315 rescan_loop_exit (e, true, false);
316 e->probability = REG_BR_PROB_BASE;
317 e->count = rd->dup_block->count;
318 e->aux = rd->outgoing_edge->aux;
320 /* If there are any PHI nodes at the destination of the outgoing edge
321 from the duplicate block, then we will need to add a new argument
322 to them. The argument should have the same value as the argument
323 associated with the outgoing edge stored in RD. */
324 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
326 int indx = rd->outgoing_edge->dest_idx;
327 add_phi_arg (phi, PHI_ARG_DEF (phi, indx), e);
331 /* Hash table traversal callback routine to create duplicate blocks. */
333 static int
334 create_duplicates (void **slot, void *data)
336 struct redirection_data *rd = (struct redirection_data *) *slot;
337 struct local_info *local_info = (struct local_info *)data;
339 /* If this entry should not have a duplicate created, then there's
340 nothing to do. */
341 if (rd->do_not_duplicate)
342 return 1;
344 /* Create a template block if we have not done so already. Otherwise
345 use the template to create a new block. */
346 if (local_info->template_block == NULL)
348 create_block_for_threading (local_info->bb, rd);
349 local_info->template_block = rd->dup_block;
351 /* We do not create any outgoing edges for the template. We will
352 take care of that in a later traversal. That way we do not
353 create edges that are going to just be deleted. */
355 else
357 create_block_for_threading (local_info->template_block, rd);
359 /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
360 block. */
361 create_edge_and_update_destination_phis (rd);
364 /* Keep walking the hash table. */
365 return 1;
368 /* We did not create any outgoing edges for the template block during
369 block creation. This hash table traversal callback creates the
370 outgoing edge for the template block. */
372 static int
373 fixup_template_block (void **slot, void *data)
375 struct redirection_data *rd = (struct redirection_data *) *slot;
376 struct local_info *local_info = (struct local_info *)data;
378 /* If this is the template block, then create its outgoing edges
379 and halt the hash table traversal. */
380 if (rd->dup_block && rd->dup_block == local_info->template_block)
382 create_edge_and_update_destination_phis (rd);
383 return 0;
386 return 1;
389 /* Hash table traversal callback to redirect each incoming edge
390 associated with this hash table element to its new destination. */
392 static int
393 redirect_edges (void **slot, void *data)
395 struct redirection_data *rd = (struct redirection_data *) *slot;
396 struct local_info *local_info = (struct local_info *)data;
397 struct el *next, *el;
399 /* Walk over all the incoming edges associated associated with this
400 hash table entry. */
401 for (el = rd->incoming_edges; el; el = next)
403 edge e = el->e;
405 /* Go ahead and free this element from the list. Doing this now
406 avoids the need for another list walk when we destroy the hash
407 table. */
408 next = el->next;
409 free (el);
411 /* Go ahead and clear E->aux. It's not needed anymore and failure
412 to clear it will cause all kinds of unpleasant problems later. */
413 e->aux = NULL;
415 thread_stats.num_threaded_edges++;
417 if (rd->dup_block)
419 edge e2;
421 if (dump_file && (dump_flags & TDF_DETAILS))
422 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
423 e->src->index, e->dest->index, rd->dup_block->index);
425 rd->dup_block->count += e->count;
426 rd->dup_block->frequency += EDGE_FREQUENCY (e);
427 EDGE_SUCC (rd->dup_block, 0)->count += e->count;
428 /* Redirect the incoming edge to the appropriate duplicate
429 block. */
430 e2 = redirect_edge_and_branch (e, rd->dup_block);
431 gcc_assert (e == e2);
432 flush_pending_stmts (e2);
434 else
436 if (dump_file && (dump_flags & TDF_DETAILS))
437 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
438 e->src->index, e->dest->index, local_info->bb->index);
440 /* We are using BB as the duplicate. Remove the unnecessary
441 outgoing edges and statements from BB. */
442 remove_ctrl_stmt_and_useless_edges (local_info->bb,
443 rd->outgoing_edge->dest);
445 /* And fixup the flags on the single remaining edge. */
446 single_succ_edge (local_info->bb)->flags
447 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
448 single_succ_edge (local_info->bb)->flags |= EDGE_FALLTHRU;
452 /* Indicate that we actually threaded one or more jumps. */
453 if (rd->incoming_edges)
454 local_info->jumps_threaded = true;
456 return 1;
459 /* Return true if this block has no executable statements other than
460 a simple ctrl flow instruction. When the number of outgoing edges
461 is one, this is equivalent to a "forwarder" block. */
463 static bool
464 redirection_block_p (const_basic_block bb)
466 const_block_stmt_iterator bsi;
468 /* Advance to the first executable statement. */
469 bsi = cbsi_start (bb);
470 while (!cbsi_end_p (bsi)
471 && (TREE_CODE (cbsi_stmt (bsi)) == LABEL_EXPR
472 || IS_EMPTY_STMT (cbsi_stmt (bsi))))
473 cbsi_next (&bsi);
475 /* Check if this is an empty block. */
476 if (cbsi_end_p (bsi))
477 return true;
479 /* Test that we've reached the terminating control statement. */
480 return cbsi_stmt (bsi)
481 && (TREE_CODE (cbsi_stmt (bsi)) == COND_EXPR
482 || TREE_CODE (cbsi_stmt (bsi)) == GOTO_EXPR
483 || TREE_CODE (cbsi_stmt (bsi)) == SWITCH_EXPR);
486 /* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
487 is reached via one or more specific incoming edges, we know which
488 outgoing edge from BB will be traversed.
490 We want to redirect those incoming edges to the target of the
491 appropriate outgoing edge. Doing so avoids a conditional branch
492 and may expose new optimization opportunities. Note that we have
493 to update dominator tree and SSA graph after such changes.
495 The key to keeping the SSA graph update manageable is to duplicate
496 the side effects occurring in BB so that those side effects still
497 occur on the paths which bypass BB after redirecting edges.
499 We accomplish this by creating duplicates of BB and arranging for
500 the duplicates to unconditionally pass control to one specific
501 successor of BB. We then revector the incoming edges into BB to
502 the appropriate duplicate of BB.
504 If NOLOOP_ONLY is true, we only perform the threading as long as it
505 does not affect the structure of the loops in a nontrivial way. */
507 static bool
508 thread_block (basic_block bb, bool noloop_only)
510 /* E is an incoming edge into BB that we may or may not want to
511 redirect to a duplicate of BB. */
512 edge e, e2;
513 edge_iterator ei;
514 struct local_info local_info;
515 struct loop *loop = bb->loop_father;
517 /* ALL indicates whether or not all incoming edges into BB should
518 be threaded to a duplicate of BB. */
519 bool all = true;
521 /* To avoid scanning a linear array for the element we need we instead
522 use a hash table. For normal code there should be no noticeable
523 difference. However, if we have a block with a large number of
524 incoming and outgoing edges such linear searches can get expensive. */
525 redirection_data = htab_create (EDGE_COUNT (bb->succs),
526 redirection_data_hash,
527 redirection_data_eq,
528 free);
530 /* If we thread the latch of the loop to its exit, the loop ceases to
531 exist. Make sure we do not restrict ourselves in order to preserve
532 this loop. */
533 if (loop->header == bb)
535 e = loop_latch_edge (loop);
536 e2 = (edge) e->aux;
538 if (e2 && loop_exit_edge_p (loop, e2))
540 loop->header = NULL;
541 loop->latch = NULL;
545 /* Record each unique threaded destination into a hash table for
546 efficient lookups. */
547 FOR_EACH_EDGE (e, ei, bb->preds)
549 e2 = (edge) e->aux;
551 if (!e2
552 /* If NOLOOP_ONLY is true, we only allow threading through the
553 header of a loop to exit edges. */
554 || (noloop_only
555 && bb == bb->loop_father->header
556 && !loop_exit_edge_p (bb->loop_father, e2)))
558 all = false;
559 continue;
562 update_bb_profile_for_threading (e->dest, EDGE_FREQUENCY (e),
563 e->count, (edge) e->aux);
565 /* Insert the outgoing edge into the hash table if it is not
566 already in the hash table. */
567 lookup_redirection_data (e2, e, INSERT);
570 /* If we are going to thread all incoming edges to an outgoing edge, then
571 BB will become unreachable. Rather than just throwing it away, use
572 it for one of the duplicates. Mark the first incoming edge with the
573 DO_NOT_DUPLICATE attribute. */
574 if (all)
576 edge e = (edge) EDGE_PRED (bb, 0)->aux;
577 lookup_redirection_data (e, NULL, NO_INSERT)->do_not_duplicate = true;
580 /* We do not update dominance info. */
581 free_dominance_info (CDI_DOMINATORS);
583 /* Now create duplicates of BB.
585 Note that for a block with a high outgoing degree we can waste
586 a lot of time and memory creating and destroying useless edges.
588 So we first duplicate BB and remove the control structure at the
589 tail of the duplicate as well as all outgoing edges from the
590 duplicate. We then use that duplicate block as a template for
591 the rest of the duplicates. */
592 local_info.template_block = NULL;
593 local_info.bb = bb;
594 local_info.jumps_threaded = false;
595 htab_traverse (redirection_data, create_duplicates, &local_info);
597 /* The template does not have an outgoing edge. Create that outgoing
598 edge and update PHI nodes as the edge's target as necessary.
600 We do this after creating all the duplicates to avoid creating
601 unnecessary edges. */
602 htab_traverse (redirection_data, fixup_template_block, &local_info);
604 /* The hash table traversals above created the duplicate blocks (and the
605 statements within the duplicate blocks). This loop creates PHI nodes for
606 the duplicated blocks and redirects the incoming edges into BB to reach
607 the duplicates of BB. */
608 htab_traverse (redirection_data, redirect_edges, &local_info);
610 /* Done with this block. Clear REDIRECTION_DATA. */
611 htab_delete (redirection_data);
612 redirection_data = NULL;
614 /* Indicate to our caller whether or not any jumps were threaded. */
615 return local_info.jumps_threaded;
618 /* Threads edge E through E->dest to the edge E->aux. Returns the copy
619 of E->dest created during threading, or E->dest if it was not necessary
620 to copy it (E is its single predecessor). */
622 static basic_block
623 thread_single_edge (edge e)
625 basic_block bb = e->dest;
626 edge eto = (edge) e->aux;
627 struct redirection_data rd;
628 struct local_info local_info;
630 e->aux = NULL;
632 thread_stats.num_threaded_edges++;
634 if (single_pred_p (bb))
636 /* If BB has just a single predecessor, we should only remove the
637 control statements at its end, and successors except for ETO. */
638 remove_ctrl_stmt_and_useless_edges (bb, eto->dest);
640 /* And fixup the flags on the single remaining edge. */
641 eto->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
642 eto->flags |= EDGE_FALLTHRU;
644 return bb;
647 /* Otherwise, we need to create a copy. */
648 update_bb_profile_for_threading (bb, EDGE_FREQUENCY (e), e->count, eto);
650 local_info.bb = bb;
651 rd.outgoing_edge = eto;
653 create_block_for_threading (bb, &rd);
654 create_edge_and_update_destination_phis (&rd);
656 if (dump_file && (dump_flags & TDF_DETAILS))
657 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
658 e->src->index, e->dest->index, rd.dup_block->index);
660 rd.dup_block->count = e->count;
661 rd.dup_block->frequency = EDGE_FREQUENCY (e);
662 single_succ_edge (rd.dup_block)->count = e->count;
663 redirect_edge_and_branch (e, rd.dup_block);
664 flush_pending_stmts (e);
666 return rd.dup_block;
669 /* Callback for dfs_enumerate_from. Returns true if BB is different
670 from STOP and DBDS_CE_STOP. */
672 static basic_block dbds_ce_stop;
673 static bool
674 dbds_continue_enumeration_p (const_basic_block bb, const void *stop)
676 return (bb != (const_basic_block) stop
677 && bb != dbds_ce_stop);
680 /* Evaluates the dominance relationship of latch of the LOOP and BB, and
681 returns the state. */
683 enum bb_dom_status
685 /* BB does not dominate latch of the LOOP. */
686 DOMST_NONDOMINATING,
687 /* The LOOP is broken (there is no path from the header to its latch. */
688 DOMST_LOOP_BROKEN,
689 /* BB dominates the latch of the LOOP. */
690 DOMST_DOMINATING
693 static enum bb_dom_status
694 determine_bb_domination_status (struct loop *loop, basic_block bb)
696 basic_block *bblocks;
697 unsigned nblocks, i;
698 bool bb_reachable = false;
699 edge_iterator ei;
700 edge e;
702 #ifdef ENABLE_CHECKING
703 /* This function assumes BB is a successor of LOOP->header. */
705 bool ok = false;
707 FOR_EACH_EDGE (e, ei, bb->preds)
709 if (e->src == loop->header)
711 ok = true;
712 break;
716 gcc_assert (ok);
718 #endif
720 if (bb == loop->latch)
721 return DOMST_DOMINATING;
723 /* Check that BB dominates LOOP->latch, and that it is back-reachable
724 from it. */
726 bblocks = XCNEWVEC (basic_block, loop->num_nodes);
727 dbds_ce_stop = loop->header;
728 nblocks = dfs_enumerate_from (loop->latch, 1, dbds_continue_enumeration_p,
729 bblocks, loop->num_nodes, bb);
730 for (i = 0; i < nblocks; i++)
731 FOR_EACH_EDGE (e, ei, bblocks[i]->preds)
733 if (e->src == loop->header)
735 free (bblocks);
736 return DOMST_NONDOMINATING;
738 if (e->src == bb)
739 bb_reachable = true;
742 free (bblocks);
743 return (bb_reachable ? DOMST_DOMINATING : DOMST_LOOP_BROKEN);
746 /* Thread jumps through the header of LOOP. Returns true if cfg changes.
747 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading from entry edges
748 to the inside of the loop. */
750 static bool
751 thread_through_loop_header (struct loop *loop, bool may_peel_loop_headers)
753 basic_block header = loop->header;
754 edge e, tgt_edge, latch = loop_latch_edge (loop);
755 edge_iterator ei;
756 basic_block tgt_bb, atgt_bb;
757 enum bb_dom_status domst;
759 /* We have already threaded through headers to exits, so all the threading
760 requests now are to the inside of the loop. We need to avoid creating
761 irreducible regions (i.e., loops with more than one entry block), and
762 also loop with several latch edges, or new subloops of the loop (although
763 there are cases where it might be appropriate, it is difficult to decide,
764 and doing it wrongly may confuse other optimizers).
766 We could handle more general cases here. However, the intention is to
767 preserve some information about the loop, which is impossible if its
768 structure changes significantly, in a way that is not well understood.
769 Thus we only handle few important special cases, in which also updating
770 of the loop-carried information should be feasible:
772 1) Propagation of latch edge to a block that dominates the latch block
773 of a loop. This aims to handle the following idiom:
775 first = 1;
776 while (1)
778 if (first)
779 initialize;
780 first = 0;
781 body;
784 After threading the latch edge, this becomes
786 first = 1;
787 if (first)
788 initialize;
789 while (1)
791 first = 0;
792 body;
795 The original header of the loop is moved out of it, and we may thread
796 the remaining edges through it without further constraints.
798 2) All entry edges are propagated to a single basic block that dominates
799 the latch block of the loop. This aims to handle the following idiom
800 (normally created for "for" loops):
802 i = 0;
803 while (1)
805 if (i >= 100)
806 break;
807 body;
808 i++;
811 This becomes
813 i = 0;
814 while (1)
816 body;
817 i++;
818 if (i >= 100)
819 break;
823 /* Threading through the header won't improve the code if the header has just
824 one successor. */
825 if (single_succ_p (header))
826 goto fail;
828 if (latch->aux)
830 tgt_edge = (edge) latch->aux;
831 tgt_bb = tgt_edge->dest;
833 else if (!may_peel_loop_headers
834 && !redirection_block_p (loop->header))
835 goto fail;
836 else
838 tgt_bb = NULL;
839 tgt_edge = NULL;
840 FOR_EACH_EDGE (e, ei, header->preds)
842 if (!e->aux)
844 if (e == latch)
845 continue;
847 /* If latch is not threaded, and there is a header
848 edge that is not threaded, we would create loop
849 with multiple entries. */
850 goto fail;
853 tgt_edge = (edge) e->aux;
854 atgt_bb = tgt_edge->dest;
855 if (!tgt_bb)
856 tgt_bb = atgt_bb;
857 /* Two targets of threading would make us create loop
858 with multiple entries. */
859 else if (tgt_bb != atgt_bb)
860 goto fail;
863 if (!tgt_bb)
865 /* There are no threading requests. */
866 return false;
869 /* Redirecting to empty loop latch is useless. */
870 if (tgt_bb == loop->latch
871 && empty_block_p (loop->latch))
872 goto fail;
875 /* The target block must dominate the loop latch, otherwise we would be
876 creating a subloop. */
877 domst = determine_bb_domination_status (loop, tgt_bb);
878 if (domst == DOMST_NONDOMINATING)
879 goto fail;
880 if (domst == DOMST_LOOP_BROKEN)
882 /* If the loop ceased to exist, mark it as such, and thread through its
883 original header. */
884 loop->header = NULL;
885 loop->latch = NULL;
886 return thread_block (header, false);
889 if (tgt_bb->loop_father->header == tgt_bb)
891 /* If the target of the threading is a header of a subloop, we need
892 to create a preheader for it, so that the headers of the two loops
893 do not merge. */
894 if (EDGE_COUNT (tgt_bb->preds) > 2)
896 tgt_bb = create_preheader (tgt_bb->loop_father, 0);
897 gcc_assert (tgt_bb != NULL);
899 else
900 tgt_bb = split_edge (tgt_edge);
903 if (latch->aux)
905 /* First handle the case latch edge is redirected. */
906 loop->latch = thread_single_edge (latch);
907 gcc_assert (single_succ (loop->latch) == tgt_bb);
908 loop->header = tgt_bb;
910 /* Thread the remaining edges through the former header. */
911 thread_block (header, false);
913 else
915 basic_block new_preheader;
917 /* Now consider the case entry edges are redirected to the new entry
918 block. Remember one entry edge, so that we can find the new
919 preheader (its destination after threading). */
920 FOR_EACH_EDGE (e, ei, header->preds)
922 if (e->aux)
923 break;
926 /* The duplicate of the header is the new preheader of the loop. Ensure
927 that it is placed correctly in the loop hierarchy. */
928 set_loop_copy (loop, loop_outer (loop));
930 thread_block (header, false);
931 set_loop_copy (loop, NULL);
932 new_preheader = e->dest;
934 /* Create the new latch block. This is always necessary, as the latch
935 must have only a single successor, but the original header had at
936 least two successors. */
937 loop->latch = NULL;
938 mfb_kj_edge = single_succ_edge (new_preheader);
939 loop->header = mfb_kj_edge->dest;
940 latch = make_forwarder_block (tgt_bb, mfb_keep_just, NULL);
941 loop->header = latch->dest;
942 loop->latch = latch->src;
945 return true;
947 fail:
948 /* We failed to thread anything. Cancel the requests. */
949 FOR_EACH_EDGE (e, ei, header->preds)
951 e->aux = NULL;
953 return false;
956 /* Walk through the registered jump threads and convert them into a
957 form convenient for this pass.
959 Any block which has incoming edges threaded to outgoing edges
960 will have its entry in THREADED_BLOCK set.
962 Any threaded edge will have its new outgoing edge stored in the
963 original edge's AUX field.
965 This form avoids the need to walk all the edges in the CFG to
966 discover blocks which need processing and avoids unnecessary
967 hash table lookups to map from threaded edge to new target. */
969 static void
970 mark_threaded_blocks (bitmap threaded_blocks)
972 unsigned int i;
973 bitmap_iterator bi;
974 bitmap tmp = BITMAP_ALLOC (NULL);
975 basic_block bb;
976 edge e;
977 edge_iterator ei;
979 for (i = 0; i < VEC_length (edge, threaded_edges); i += 2)
981 edge e = VEC_index (edge, threaded_edges, i);
982 edge e2 = VEC_index (edge, threaded_edges, i + 1);
984 e->aux = e2;
985 bitmap_set_bit (tmp, e->dest->index);
988 /* If optimizing for size, only thread through block if we don't have
989 to duplicate it or it's an otherwise empty redirection block. */
990 if (optimize_size)
992 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
994 bb = BASIC_BLOCK (i);
995 if (EDGE_COUNT (bb->preds) > 1
996 && !redirection_block_p (bb))
998 FOR_EACH_EDGE (e, ei, bb->preds)
999 e->aux = NULL;
1001 else
1002 bitmap_set_bit (threaded_blocks, i);
1005 else
1006 bitmap_copy (threaded_blocks, tmp);
1008 BITMAP_FREE(tmp);
1012 /* Walk through all blocks and thread incoming edges to the appropriate
1013 outgoing edge for each edge pair recorded in THREADED_EDGES.
1015 It is the caller's responsibility to fix the dominance information
1016 and rewrite duplicated SSA_NAMEs back into SSA form.
1018 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading edges through
1019 loop headers if it does not simplify the loop.
1021 Returns true if one or more edges were threaded, false otherwise. */
1023 bool
1024 thread_through_all_blocks (bool may_peel_loop_headers)
1026 bool retval = false;
1027 unsigned int i;
1028 bitmap_iterator bi;
1029 bitmap threaded_blocks;
1030 struct loop *loop;
1031 loop_iterator li;
1033 /* We must know about loops in order to preserve them. */
1034 gcc_assert (current_loops != NULL);
1036 if (threaded_edges == NULL)
1037 return false;
1039 threaded_blocks = BITMAP_ALLOC (NULL);
1040 memset (&thread_stats, 0, sizeof (thread_stats));
1042 mark_threaded_blocks (threaded_blocks);
1044 initialize_original_copy_tables ();
1046 /* First perform the threading requests that do not affect
1047 loop structure. */
1048 EXECUTE_IF_SET_IN_BITMAP (threaded_blocks, 0, i, bi)
1050 basic_block bb = BASIC_BLOCK (i);
1052 if (EDGE_COUNT (bb->preds) > 0)
1053 retval |= thread_block (bb, true);
1056 /* Then perform the threading through loop headers. We start with the
1057 innermost loop, so that the changes in cfg we perform won't affect
1058 further threading. */
1059 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
1061 if (!loop->header
1062 || !bitmap_bit_p (threaded_blocks, loop->header->index))
1063 continue;
1065 retval |= thread_through_loop_header (loop, may_peel_loop_headers);
1068 if (dump_file && (dump_flags & TDF_STATS))
1069 fprintf (dump_file, "\nJumps threaded: %lu\n",
1070 thread_stats.num_threaded_edges);
1072 free_original_copy_tables ();
1074 BITMAP_FREE (threaded_blocks);
1075 threaded_blocks = NULL;
1076 VEC_free (edge, heap, threaded_edges);
1077 threaded_edges = NULL;
1079 if (retval)
1080 loops_state_set (LOOPS_NEED_FIXUP);
1082 return retval;
1085 /* Register a jump threading opportunity. We queue up all the jump
1086 threading opportunities discovered by a pass and update the CFG
1087 and SSA form all at once.
1089 E is the edge we can thread, E2 is the new target edge. ie, we
1090 are effectively recording that E->dest can be changed to E2->dest
1091 after fixing the SSA graph. */
1093 void
1094 register_jump_thread (edge e, edge e2)
1096 if (threaded_edges == NULL)
1097 threaded_edges = VEC_alloc (edge, heap, 10);
1099 VEC_safe_push (edge, heap, threaded_edges, e);
1100 VEC_safe_push (edge, heap, threaded_edges, e2);