1 /* Thread edges through blocks and update the control flow and SSA graphs.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Free Software Foundation,
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "basic-block.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
33 #include "tree-pass.h"
36 /* Given a block B, update the CFG and SSA graph to reflect redirecting
37 one or more in-edges to B to instead reach the destination of an
38 out-edge from B while preserving any side effects in B.
40 i.e., given A->B and B->C, change A->B to be A->C yet still preserve the
41 side effects of executing B.
43 1. Make a copy of B (including its outgoing edges and statements). Call
44 the copy B'. Note B' has no incoming edges or PHIs at this time.
46 2. Remove the control statement at the end of B' and all outgoing edges
49 3. Add a new argument to each PHI in C with the same value as the existing
50 argument associated with edge B->C. Associate the new PHI arguments
53 4. For each PHI in B, find or create a PHI in B' with an identical
54 PHI_RESULT. Add an argument to the PHI in B' which has the same
55 value as the PHI in B associated with the edge A->B. Associate
56 the new argument in the PHI in B' with the edge A->B.
58 5. Change the edge A->B to A->B'.
60 5a. This automatically deletes any PHI arguments associated with the
63 5b. This automatically associates each new argument added in step 4
66 6. Repeat for other incoming edges into B.
68 7. Put the duplicated resources in B and all the B' blocks into SSA form.
70 Note that block duplication can be minimized by first collecting the
71 set of unique destination blocks that the incoming edges should
74 Block duplication can be further minimized by using B instead of
75 creating B' for one destination if all edges into B are going to be
76 threaded to a successor of B. We had code to do this at one time, but
77 I'm not convinced it is correct with the changes to avoid mucking up
78 the loop structure (which may cancel threading requests, thus a block
79 which we thought was going to become unreachable may still be reachable).
80 This code was also going to get ugly with the introduction of the ability
81 for a single jump thread request to bypass multiple blocks.
83 We further reduce the number of edges and statements we create by
84 not copying all the outgoing edges and the control statement in
85 step #1. We instead create a template block without the outgoing
86 edges and duplicate the template. */
89 /* Steps #5 and #6 of the above algorithm are best implemented by walking
90 all the incoming edges which thread to the same destination edge at
91 the same time. That avoids lots of table lookups to get information
92 for the destination edge.
94 To realize that implementation we create a list of incoming edges
95 which thread to the same outgoing edge. Thus to implement steps
96 #5 and #6 we traverse our hash table of outgoing edge information.
97 For each entry we walk the list of incoming edges which thread to
98 the current outgoing edge. */
106 /* Main data structure recording information regarding B's duplicate
109 /* We need to efficiently record the unique thread destinations of this
110 block and specific information associated with those destinations. We
111 may have many incoming edges threaded to the same outgoing edge. This
112 can be naturally implemented with a hash table. */
114 struct redirection_data
116 /* A duplicate of B with the trailing control statement removed and which
117 targets a single successor of B. */
118 basic_block dup_block
;
120 /* An outgoing edge from B. DUP_BLOCK will have OUTGOING_EDGE->dest as
121 its single successor. */
124 /* A list of incoming edges which we want to thread to
125 OUTGOING_EDGE->dest. */
126 struct el
*incoming_edges
;
129 /* Main data structure to hold information for duplicates of BB. */
130 static htab_t redirection_data
;
132 /* Data structure of information to pass to hash table traversal routines. */
135 /* The current block we are working on. */
138 /* A template copy of BB with no outgoing edges or control statement that
139 we use for creating copies. */
140 basic_block template_block
;
142 /* TRUE if we thread one or more jumps, FALSE otherwise. */
146 /* Passes which use the jump threading code register jump threading
147 opportunities as they are discovered. We keep the registered
148 jump threading opportunities in this vector as edge pairs
149 (original_edge, target_edge). */
150 static VEC(edge
,heap
) *threaded_edges
;
152 /* When we start updating the CFG for threading, data necessary for jump
153 threading is attached to the AUX field for the incoming edge. Use these
154 macros to access the underlying structure attached to the AUX field. */
155 #define THREAD_TARGET(E) ((edge *)(E)->aux)[0]
157 /* Jump threading statistics. */
159 struct thread_stats_d
161 unsigned long num_threaded_edges
;
164 struct thread_stats_d thread_stats
;
167 /* Remove the last statement in block BB if it is a control statement
168 Also remove all outgoing edges except the edge which reaches DEST_BB.
169 If DEST_BB is NULL, then remove all outgoing edges. */
172 remove_ctrl_stmt_and_useless_edges (basic_block bb
, basic_block dest_bb
)
174 gimple_stmt_iterator gsi
;
178 gsi
= gsi_last_bb (bb
);
180 /* If the duplicate ends with a control statement, then remove it.
182 Note that if we are duplicating the template block rather than the
183 original basic block, then the duplicate might not have any real
187 && (gimple_code (gsi_stmt (gsi
)) == GIMPLE_COND
188 || gimple_code (gsi_stmt (gsi
)) == GIMPLE_GOTO
189 || gimple_code (gsi_stmt (gsi
)) == GIMPLE_SWITCH
))
190 gsi_remove (&gsi
, true);
192 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
194 if (e
->dest
!= dest_bb
)
201 /* Create a duplicate of BB which only reaches the destination of the edge
202 stored in RD. Record the duplicate block in RD. */
205 create_block_for_threading (basic_block bb
, struct redirection_data
*rd
)
210 /* We can use the generic block duplication code and simply remove
211 the stuff we do not need. */
212 rd
->dup_block
= duplicate_block (bb
, NULL
, NULL
);
214 FOR_EACH_EDGE (e
, ei
, rd
->dup_block
->succs
)
217 /* Zero out the profile, since the block is unreachable for now. */
218 rd
->dup_block
->frequency
= 0;
219 rd
->dup_block
->count
= 0;
221 /* The call to duplicate_block will copy everything, including the
222 useless COND_EXPR or SWITCH_EXPR at the end of BB. We just remove
223 the useless COND_EXPR or SWITCH_EXPR here rather than having a
224 specialized block copier. We also remove all outgoing edges
225 from the duplicate block. The appropriate edge will be created
227 remove_ctrl_stmt_and_useless_edges (rd
->dup_block
, NULL
);
230 /* Hashing and equality routines for our hash table. */
232 redirection_data_hash (const void *p
)
234 edge e
= ((const struct redirection_data
*)p
)->outgoing_edge
;
235 return e
->dest
->index
;
239 redirection_data_eq (const void *p1
, const void *p2
)
241 edge e1
= ((const struct redirection_data
*)p1
)->outgoing_edge
;
242 edge e2
= ((const struct redirection_data
*)p2
)->outgoing_edge
;
247 /* Given an outgoing edge E lookup and return its entry in our hash table.
249 If INSERT is true, then we insert the entry into the hash table if
250 it is not already present. INCOMING_EDGE is added to the list of incoming
251 edges associated with E in the hash table. */
253 static struct redirection_data
*
254 lookup_redirection_data (edge e
, edge incoming_edge
, enum insert_option insert
)
257 struct redirection_data
*elt
;
259 /* Build a hash table element so we can see if E is already
261 elt
= XNEW (struct redirection_data
);
262 elt
->outgoing_edge
= e
;
263 elt
->dup_block
= NULL
;
264 elt
->incoming_edges
= NULL
;
266 slot
= htab_find_slot (redirection_data
, elt
, insert
);
268 /* This will only happen if INSERT is false and the entry is not
269 in the hash table. */
276 /* This will only happen if E was not in the hash table and
281 elt
->incoming_edges
= XNEW (struct el
);
282 elt
->incoming_edges
->e
= incoming_edge
;
283 elt
->incoming_edges
->next
= NULL
;
286 /* E was in the hash table. */
289 /* Free ELT as we do not need it anymore, we will extract the
290 relevant entry from the hash table itself. */
293 /* Get the entry stored in the hash table. */
294 elt
= (struct redirection_data
*) *slot
;
296 /* If insertion was requested, then we need to add INCOMING_EDGE
297 to the list of incoming edges associated with E. */
300 struct el
*el
= XNEW (struct el
);
301 el
->next
= elt
->incoming_edges
;
302 el
->e
= incoming_edge
;
303 elt
->incoming_edges
= el
;
310 /* Given a duplicate block and its single destination (both stored
311 in RD). Create an edge between the duplicate and its single
314 Add an additional argument to any PHI nodes at the single
318 create_edge_and_update_destination_phis (struct redirection_data
*rd
,
321 edge e
= make_edge (bb
, rd
->outgoing_edge
->dest
, EDGE_FALLTHRU
);
322 gimple_stmt_iterator gsi
;
324 rescan_loop_exit (e
, true, false);
325 e
->probability
= REG_BR_PROB_BASE
;
326 e
->count
= bb
->count
;
328 if (rd
->outgoing_edge
->aux
)
330 e
->aux
= (edge
*) XNEWVEC (edge
, 1);
331 THREAD_TARGET(e
) = THREAD_TARGET (rd
->outgoing_edge
);
338 /* If there are any PHI nodes at the destination of the outgoing edge
339 from the duplicate block, then we will need to add a new argument
340 to them. The argument should have the same value as the argument
341 associated with the outgoing edge stored in RD. */
342 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
344 gimple phi
= gsi_stmt (gsi
);
345 source_location locus
;
346 int indx
= rd
->outgoing_edge
->dest_idx
;
348 locus
= gimple_phi_arg_location (phi
, indx
);
349 add_phi_arg (phi
, gimple_phi_arg_def (phi
, indx
), e
, locus
);
353 /* Hash table traversal callback routine to create duplicate blocks. */
356 create_duplicates (void **slot
, void *data
)
358 struct redirection_data
*rd
= (struct redirection_data
*) *slot
;
359 struct local_info
*local_info
= (struct local_info
*)data
;
361 /* Create a template block if we have not done so already. Otherwise
362 use the template to create a new block. */
363 if (local_info
->template_block
== NULL
)
365 create_block_for_threading (local_info
->bb
, rd
);
366 local_info
->template_block
= rd
->dup_block
;
368 /* We do not create any outgoing edges for the template. We will
369 take care of that in a later traversal. That way we do not
370 create edges that are going to just be deleted. */
374 create_block_for_threading (local_info
->template_block
, rd
);
376 /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
378 create_edge_and_update_destination_phis (rd
, rd
->dup_block
);
381 /* Keep walking the hash table. */
385 /* We did not create any outgoing edges for the template block during
386 block creation. This hash table traversal callback creates the
387 outgoing edge for the template block. */
390 fixup_template_block (void **slot
, void *data
)
392 struct redirection_data
*rd
= (struct redirection_data
*) *slot
;
393 struct local_info
*local_info
= (struct local_info
*)data
;
395 /* If this is the template block, then create its outgoing edges
396 and halt the hash table traversal. */
397 if (rd
->dup_block
&& rd
->dup_block
== local_info
->template_block
)
399 create_edge_and_update_destination_phis (rd
, rd
->dup_block
);
406 /* Hash table traversal callback to redirect each incoming edge
407 associated with this hash table element to its new destination. */
410 redirect_edges (void **slot
, void *data
)
412 struct redirection_data
*rd
= (struct redirection_data
*) *slot
;
413 struct local_info
*local_info
= (struct local_info
*)data
;
414 struct el
*next
, *el
;
416 /* Walk over all the incoming edges associated associated with this
418 for (el
= rd
->incoming_edges
; el
; el
= next
)
422 /* Go ahead and free this element from the list. Doing this now
423 avoids the need for another list walk when we destroy the hash
428 thread_stats
.num_threaded_edges
++;
434 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
435 fprintf (dump_file
, " Threaded jump %d --> %d to %d\n",
436 e
->src
->index
, e
->dest
->index
, rd
->dup_block
->index
);
438 rd
->dup_block
->count
+= e
->count
;
439 rd
->dup_block
->frequency
+= EDGE_FREQUENCY (e
);
440 EDGE_SUCC (rd
->dup_block
, 0)->count
+= e
->count
;
441 /* Redirect the incoming edge to the appropriate duplicate
443 e2
= redirect_edge_and_branch (e
, rd
->dup_block
);
444 gcc_assert (e
== e2
);
445 flush_pending_stmts (e2
);
448 /* Go ahead and clear E->aux. It's not needed anymore and failure
449 to clear it will cause all kinds of unpleasant problems later. */
455 /* Indicate that we actually threaded one or more jumps. */
456 if (rd
->incoming_edges
)
457 local_info
->jumps_threaded
= true;
462 /* Return true if this block has no executable statements other than
463 a simple ctrl flow instruction. When the number of outgoing edges
464 is one, this is equivalent to a "forwarder" block. */
467 redirection_block_p (basic_block bb
)
469 gimple_stmt_iterator gsi
;
471 /* Advance to the first executable statement. */
472 gsi
= gsi_start_bb (bb
);
473 while (!gsi_end_p (gsi
)
474 && (gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
475 || is_gimple_debug (gsi_stmt (gsi
))
476 || gimple_nop_p (gsi_stmt (gsi
))))
479 /* Check if this is an empty block. */
483 /* Test that we've reached the terminating control statement. */
484 return gsi_stmt (gsi
)
485 && (gimple_code (gsi_stmt (gsi
)) == GIMPLE_COND
486 || gimple_code (gsi_stmt (gsi
)) == GIMPLE_GOTO
487 || gimple_code (gsi_stmt (gsi
)) == GIMPLE_SWITCH
);
490 /* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
491 is reached via one or more specific incoming edges, we know which
492 outgoing edge from BB will be traversed.
494 We want to redirect those incoming edges to the target of the
495 appropriate outgoing edge. Doing so avoids a conditional branch
496 and may expose new optimization opportunities. Note that we have
497 to update dominator tree and SSA graph after such changes.
499 The key to keeping the SSA graph update manageable is to duplicate
500 the side effects occurring in BB so that those side effects still
501 occur on the paths which bypass BB after redirecting edges.
503 We accomplish this by creating duplicates of BB and arranging for
504 the duplicates to unconditionally pass control to one specific
505 successor of BB. We then revector the incoming edges into BB to
506 the appropriate duplicate of BB.
508 If NOLOOP_ONLY is true, we only perform the threading as long as it
509 does not affect the structure of the loops in a nontrivial way. */
512 thread_block (basic_block bb
, bool noloop_only
)
514 /* E is an incoming edge into BB that we may or may not want to
515 redirect to a duplicate of BB. */
518 struct local_info local_info
;
519 struct loop
*loop
= bb
->loop_father
;
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
,
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
533 if (loop
->header
== bb
)
535 e
= loop_latch_edge (loop
);
538 e2
= THREAD_TARGET (e
);
542 if (e2
&& loop_exit_edge_p (loop
, e2
))
549 /* Record each unique threaded destination into a hash table for
550 efficient lookups. */
551 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
556 e2
= THREAD_TARGET (e
);
559 /* If NOLOOP_ONLY is true, we only allow threading through the
560 header of a loop to exit edges. */
562 && bb
== bb
->loop_father
->header
563 && (!loop_exit_edge_p (bb
->loop_father
, e2
))))
566 if (e
->dest
== e2
->src
)
567 update_bb_profile_for_threading (e
->dest
, EDGE_FREQUENCY (e
),
568 e
->count
, THREAD_TARGET (e
));
570 /* Insert the outgoing edge into the hash table if it is not
571 already in the hash table. */
572 lookup_redirection_data (e2
, e
, INSERT
);
575 /* We do not update dominance info. */
576 free_dominance_info (CDI_DOMINATORS
);
578 /* Now create duplicates of BB.
580 Note that for a block with a high outgoing degree we can waste
581 a lot of time and memory creating and destroying useless edges.
583 So we first duplicate BB and remove the control structure at the
584 tail of the duplicate as well as all outgoing edges from the
585 duplicate. We then use that duplicate block as a template for
586 the rest of the duplicates. */
587 local_info
.template_block
= NULL
;
589 local_info
.jumps_threaded
= false;
590 htab_traverse (redirection_data
, create_duplicates
, &local_info
);
592 /* The template does not have an outgoing edge. Create that outgoing
593 edge and update PHI nodes as the edge's target as necessary.
595 We do this after creating all the duplicates to avoid creating
596 unnecessary edges. */
597 htab_traverse (redirection_data
, fixup_template_block
, &local_info
);
599 /* The hash table traversals above created the duplicate blocks (and the
600 statements within the duplicate blocks). This loop creates PHI nodes for
601 the duplicated blocks and redirects the incoming edges into BB to reach
602 the duplicates of BB. */
603 htab_traverse (redirection_data
, redirect_edges
, &local_info
);
605 /* Done with this block. Clear REDIRECTION_DATA. */
606 htab_delete (redirection_data
);
607 redirection_data
= NULL
;
609 /* Indicate to our caller whether or not any jumps were threaded. */
610 return local_info
.jumps_threaded
;
613 /* Threads edge E through E->dest to the edge THREAD_TARGET (E). Returns the
614 copy of E->dest created during threading, or E->dest if it was not necessary
615 to copy it (E is its single predecessor). */
618 thread_single_edge (edge e
)
620 basic_block bb
= e
->dest
;
621 edge eto
= THREAD_TARGET (e
);
622 struct redirection_data rd
;
627 thread_stats
.num_threaded_edges
++;
629 if (single_pred_p (bb
))
631 /* If BB has just a single predecessor, we should only remove the
632 control statements at its end, and successors except for ETO. */
633 remove_ctrl_stmt_and_useless_edges (bb
, eto
->dest
);
635 /* And fixup the flags on the single remaining edge. */
636 eto
->flags
&= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
| EDGE_ABNORMAL
);
637 eto
->flags
|= EDGE_FALLTHRU
;
642 /* Otherwise, we need to create a copy. */
643 if (e
->dest
== eto
->src
)
644 update_bb_profile_for_threading (bb
, EDGE_FREQUENCY (e
), e
->count
, eto
);
646 rd
.outgoing_edge
= eto
;
648 create_block_for_threading (bb
, &rd
);
649 create_edge_and_update_destination_phis (&rd
, rd
.dup_block
);
651 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
652 fprintf (dump_file
, " Threaded jump %d --> %d to %d\n",
653 e
->src
->index
, e
->dest
->index
, rd
.dup_block
->index
);
655 rd
.dup_block
->count
= e
->count
;
656 rd
.dup_block
->frequency
= EDGE_FREQUENCY (e
);
657 single_succ_edge (rd
.dup_block
)->count
= e
->count
;
658 redirect_edge_and_branch (e
, rd
.dup_block
);
659 flush_pending_stmts (e
);
664 /* Callback for dfs_enumerate_from. Returns true if BB is different
665 from STOP and DBDS_CE_STOP. */
667 static basic_block dbds_ce_stop
;
669 dbds_continue_enumeration_p (const_basic_block bb
, const void *stop
)
671 return (bb
!= (const_basic_block
) stop
672 && bb
!= dbds_ce_stop
);
675 /* Evaluates the dominance relationship of latch of the LOOP and BB, and
676 returns the state. */
680 /* BB does not dominate latch of the LOOP. */
682 /* The LOOP is broken (there is no path from the header to its latch. */
684 /* BB dominates the latch of the LOOP. */
688 static enum bb_dom_status
689 determine_bb_domination_status (struct loop
*loop
, basic_block bb
)
691 basic_block
*bblocks
;
693 bool bb_reachable
= false;
697 /* This function assumes BB is a successor of LOOP->header.
698 If that is not the case return DOMST_NONDOMINATING which
703 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
705 if (e
->src
== loop
->header
)
713 return DOMST_NONDOMINATING
;
716 if (bb
== loop
->latch
)
717 return DOMST_DOMINATING
;
719 /* Check that BB dominates LOOP->latch, and that it is back-reachable
722 bblocks
= XCNEWVEC (basic_block
, loop
->num_nodes
);
723 dbds_ce_stop
= loop
->header
;
724 nblocks
= dfs_enumerate_from (loop
->latch
, 1, dbds_continue_enumeration_p
,
725 bblocks
, loop
->num_nodes
, bb
);
726 for (i
= 0; i
< nblocks
; i
++)
727 FOR_EACH_EDGE (e
, ei
, bblocks
[i
]->preds
)
729 if (e
->src
== loop
->header
)
732 return DOMST_NONDOMINATING
;
739 return (bb_reachable
? DOMST_DOMINATING
: DOMST_LOOP_BROKEN
);
742 /* Thread jumps through the header of LOOP. Returns true if cfg changes.
743 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading from entry edges
744 to the inside of the loop. */
747 thread_through_loop_header (struct loop
*loop
, bool may_peel_loop_headers
)
749 basic_block header
= loop
->header
;
750 edge e
, tgt_edge
, latch
= loop_latch_edge (loop
);
752 basic_block tgt_bb
, atgt_bb
;
753 enum bb_dom_status domst
;
755 /* We have already threaded through headers to exits, so all the threading
756 requests now are to the inside of the loop. We need to avoid creating
757 irreducible regions (i.e., loops with more than one entry block), and
758 also loop with several latch edges, or new subloops of the loop (although
759 there are cases where it might be appropriate, it is difficult to decide,
760 and doing it wrongly may confuse other optimizers).
762 We could handle more general cases here. However, the intention is to
763 preserve some information about the loop, which is impossible if its
764 structure changes significantly, in a way that is not well understood.
765 Thus we only handle few important special cases, in which also updating
766 of the loop-carried information should be feasible:
768 1) Propagation of latch edge to a block that dominates the latch block
769 of a loop. This aims to handle the following idiom:
780 After threading the latch edge, this becomes
791 The original header of the loop is moved out of it, and we may thread
792 the remaining edges through it without further constraints.
794 2) All entry edges are propagated to a single basic block that dominates
795 the latch block of the loop. This aims to handle the following idiom
796 (normally created for "for" loops):
819 /* Threading through the header won't improve the code if the header has just
821 if (single_succ_p (header
))
826 tgt_edge
= THREAD_TARGET (latch
);
827 tgt_bb
= tgt_edge
->dest
;
829 else if (!may_peel_loop_headers
830 && !redirection_block_p (loop
->header
))
836 FOR_EACH_EDGE (e
, ei
, header
->preds
)
843 /* If latch is not threaded, and there is a header
844 edge that is not threaded, we would create loop
845 with multiple entries. */
849 tgt_edge
= THREAD_TARGET (e
);
850 atgt_bb
= tgt_edge
->dest
;
853 /* Two targets of threading would make us create loop
854 with multiple entries. */
855 else if (tgt_bb
!= atgt_bb
)
861 /* There are no threading requests. */
865 /* Redirecting to empty loop latch is useless. */
866 if (tgt_bb
== loop
->latch
867 && empty_block_p (loop
->latch
))
871 /* The target block must dominate the loop latch, otherwise we would be
872 creating a subloop. */
873 domst
= determine_bb_domination_status (loop
, tgt_bb
);
874 if (domst
== DOMST_NONDOMINATING
)
876 if (domst
== DOMST_LOOP_BROKEN
)
878 /* If the loop ceased to exist, mark it as such, and thread through its
882 return thread_block (header
, false);
885 if (tgt_bb
->loop_father
->header
== tgt_bb
)
887 /* If the target of the threading is a header of a subloop, we need
888 to create a preheader for it, so that the headers of the two loops
890 if (EDGE_COUNT (tgt_bb
->preds
) > 2)
892 tgt_bb
= create_preheader (tgt_bb
->loop_father
, 0);
893 gcc_assert (tgt_bb
!= NULL
);
896 tgt_bb
= split_edge (tgt_edge
);
901 /* First handle the case latch edge is redirected. */
902 loop
->latch
= thread_single_edge (latch
);
903 gcc_assert (single_succ (loop
->latch
) == tgt_bb
);
904 loop
->header
= tgt_bb
;
906 /* Thread the remaining edges through the former header. */
907 thread_block (header
, false);
911 basic_block new_preheader
;
913 /* Now consider the case entry edges are redirected to the new entry
914 block. Remember one entry edge, so that we can find the new
915 preheader (its destination after threading). */
916 FOR_EACH_EDGE (e
, ei
, header
->preds
)
922 /* The duplicate of the header is the new preheader of the loop. Ensure
923 that it is placed correctly in the loop hierarchy. */
924 set_loop_copy (loop
, loop_outer (loop
));
926 thread_block (header
, false);
927 set_loop_copy (loop
, NULL
);
928 new_preheader
= e
->dest
;
930 /* Create the new latch block. This is always necessary, as the latch
931 must have only a single successor, but the original header had at
932 least two successors. */
934 mfb_kj_edge
= single_succ_edge (new_preheader
);
935 loop
->header
= mfb_kj_edge
->dest
;
936 latch
= make_forwarder_block (tgt_bb
, mfb_keep_just
, NULL
);
937 loop
->header
= latch
->dest
;
938 loop
->latch
= latch
->src
;
944 /* We failed to thread anything. Cancel the requests. */
945 FOR_EACH_EDGE (e
, ei
, header
->preds
)
953 /* Walk through the registered jump threads and convert them into a
954 form convenient for this pass.
956 Any block which has incoming edges threaded to outgoing edges
957 will have its entry in THREADED_BLOCK set.
959 Any threaded edge will have its new outgoing edge stored in the
960 original edge's AUX field.
962 This form avoids the need to walk all the edges in the CFG to
963 discover blocks which need processing and avoids unnecessary
964 hash table lookups to map from threaded edge to new target. */
967 mark_threaded_blocks (bitmap threaded_blocks
)
971 bitmap tmp
= BITMAP_ALLOC (NULL
);
976 for (i
= 0; i
< VEC_length (edge
, threaded_edges
); i
+= 2)
978 edge e
= VEC_index (edge
, threaded_edges
, i
);
979 edge
*x
= (edge
*) XNEWVEC (edge
, 1);
981 x
[0] = VEC_index (edge
, threaded_edges
, i
+ 1);
983 bitmap_set_bit (tmp
, e
->dest
->index
);
986 /* If optimizing for size, only thread through block if we don't have
987 to duplicate it or it's an otherwise empty redirection block. */
988 if (optimize_function_for_size_p (cfun
))
990 EXECUTE_IF_SET_IN_BITMAP (tmp
, 0, i
, bi
)
992 bb
= BASIC_BLOCK (i
);
993 if (EDGE_COUNT (bb
->preds
) > 1
994 && !redirection_block_p (bb
))
996 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1003 bitmap_set_bit (threaded_blocks
, i
);
1007 bitmap_copy (threaded_blocks
, tmp
);
1013 /* Walk through all blocks and thread incoming edges to the appropriate
1014 outgoing edge for each edge pair recorded in THREADED_EDGES.
1016 It is the caller's responsibility to fix the dominance information
1017 and rewrite duplicated SSA_NAMEs back into SSA form.
1019 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading edges through
1020 loop headers if it does not simplify the loop.
1022 Returns true if one or more edges were threaded, false otherwise. */
1025 thread_through_all_blocks (bool may_peel_loop_headers
)
1027 bool retval
= false;
1030 bitmap threaded_blocks
;
1034 /* We must know about loops in order to preserve them. */
1035 gcc_assert (current_loops
!= NULL
);
1037 if (threaded_edges
== NULL
)
1040 threaded_blocks
= BITMAP_ALLOC (NULL
);
1041 memset (&thread_stats
, 0, sizeof (thread_stats
));
1043 mark_threaded_blocks (threaded_blocks
);
1045 initialize_original_copy_tables ();
1047 /* First perform the threading requests that do not affect
1049 EXECUTE_IF_SET_IN_BITMAP (threaded_blocks
, 0, i
, bi
)
1051 basic_block bb
= BASIC_BLOCK (i
);
1053 if (EDGE_COUNT (bb
->preds
) > 0)
1054 retval
|= thread_block (bb
, true);
1057 /* Then perform the threading through loop headers. We start with the
1058 innermost loop, so that the changes in cfg we perform won't affect
1059 further threading. */
1060 FOR_EACH_LOOP (li
, loop
, LI_FROM_INNERMOST
)
1063 || !bitmap_bit_p (threaded_blocks
, loop
->header
->index
))
1066 retval
|= thread_through_loop_header (loop
, may_peel_loop_headers
);
1069 statistics_counter_event (cfun
, "Jumps threaded",
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
;
1080 loops_state_set (LOOPS_NEED_FIXUP
);
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, i.e., we
1090 are effectively recording that E->dest can be changed to E2->dest
1091 after fixing the SSA graph. */
1094 register_jump_thread (edge e
, edge e2
)
1096 /* This can occur if we're jumping to a constant address or
1097 or something similar. Just get out now. */
1101 if (threaded_edges
== NULL
)
1102 threaded_edges
= VEC_alloc (edge
, heap
, 15);
1104 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
1105 && e
->dest
!= e2
->src
)
1107 " Registering jump thread around one or more intermediate blocks\n");
1109 VEC_safe_push (edge
, heap
, threaded_edges
, e
);
1110 VEC_safe_push (edge
, heap
, threaded_edges
, e2
);