* testsuite/26_numerics/headers/cmath/hypot.cc: XFAIL on AIX.
[official-gcc.git] / gcc / tree-ssa-threadupdate.c
blob325cb0be8fc77cae780f0fb61c34534315357b60
1 /* Thread edges through blocks and update the control flow and SSA graphs.
2 Copyright (C) 2004-2016 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 "backend.h"
24 #include "tree.h"
25 #include "gimple.h"
26 #include "cfghooks.h"
27 #include "tree-pass.h"
28 #include "ssa.h"
29 #include "fold-const.h"
30 #include "cfganal.h"
31 #include "gimple-iterator.h"
32 #include "tree-ssa.h"
33 #include "tree-ssa-threadupdate.h"
34 #include "cfgloop.h"
35 #include "dbgcnt.h"
36 #include "tree-cfg.h"
38 /* Given a block B, update the CFG and SSA graph to reflect redirecting
39 one or more in-edges to B to instead reach the destination of an
40 out-edge from B while preserving any side effects in B.
42 i.e., given A->B and B->C, change A->B to be A->C yet still preserve the
43 side effects of executing B.
45 1. Make a copy of B (including its outgoing edges and statements). Call
46 the copy B'. Note B' has no incoming edges or PHIs at this time.
48 2. Remove the control statement at the end of B' and all outgoing edges
49 except B'->C.
51 3. Add a new argument to each PHI in C with the same value as the existing
52 argument associated with edge B->C. Associate the new PHI arguments
53 with the edge B'->C.
55 4. For each PHI in B, find or create a PHI in B' with an identical
56 PHI_RESULT. Add an argument to the PHI in B' which has the same
57 value as the PHI in B associated with the edge A->B. Associate
58 the new argument in the PHI in B' with the edge A->B.
60 5. Change the edge A->B to A->B'.
62 5a. This automatically deletes any PHI arguments associated with the
63 edge A->B in B.
65 5b. This automatically associates each new argument added in step 4
66 with the edge A->B'.
68 6. Repeat for other incoming edges into B.
70 7. Put the duplicated resources in B and all the B' blocks into SSA form.
72 Note that block duplication can be minimized by first collecting the
73 set of unique destination blocks that the incoming edges should
74 be threaded to.
76 We reduce the number of edges and statements we create by not copying all
77 the outgoing edges and the control statement in step #1. We instead create
78 a template block without the outgoing edges and duplicate the template.
80 Another case this code handles is threading through a "joiner" block. In
81 this case, we do not know the destination of the joiner block, but one
82 of the outgoing edges from the joiner block leads to a threadable path. This
83 case largely works as outlined above, except the duplicate of the joiner
84 block still contains a full set of outgoing edges and its control statement.
85 We just redirect one of its outgoing edges to our jump threading path. */
88 /* Steps #5 and #6 of the above algorithm are best implemented by walking
89 all the incoming edges which thread to the same destination edge at
90 the same time. That avoids lots of table lookups to get information
91 for the destination edge.
93 To realize that implementation we create a list of incoming edges
94 which thread to the same outgoing edge. Thus to implement steps
95 #5 and #6 we traverse our hash table of outgoing edge information.
96 For each entry we walk the list of incoming edges which thread to
97 the current outgoing edge. */
99 struct el
101 edge e;
102 struct el *next;
105 /* Main data structure recording information regarding B's duplicate
106 blocks. */
108 /* We need to efficiently record the unique thread destinations of this
109 block and specific information associated with those destinations. We
110 may have many incoming edges threaded to the same outgoing edge. This
111 can be naturally implemented with a hash table. */
113 struct redirection_data : free_ptr_hash<redirection_data>
115 /* We support wiring up two block duplicates in a jump threading path.
117 One is a normal block copy where we remove the control statement
118 and wire up its single remaining outgoing edge to the thread path.
120 The other is a joiner block where we leave the control statement
121 in place, but wire one of the outgoing edges to a thread path.
123 In theory we could have multiple block duplicates in a jump
124 threading path, but I haven't tried that.
126 The duplicate blocks appear in this array in the same order in
127 which they appear in the jump thread path. */
128 basic_block dup_blocks[2];
130 /* The jump threading path. */
131 vec<jump_thread_edge *> *path;
133 /* A list of incoming edges which we want to thread to the
134 same path. */
135 struct el *incoming_edges;
137 /* hash_table support. */
138 static inline hashval_t hash (const redirection_data *);
139 static inline int equal (const redirection_data *, const redirection_data *);
142 /* Dump a jump threading path, including annotations about each
143 edge in the path. */
145 static void
146 dump_jump_thread_path (FILE *dump_file, vec<jump_thread_edge *> path,
147 bool registering)
149 fprintf (dump_file,
150 " %s%s jump thread: (%d, %d) incoming edge; ",
151 (registering ? "Registering" : "Cancelling"),
152 (path[0]->type == EDGE_FSM_THREAD ? " FSM": ""),
153 path[0]->e->src->index, path[0]->e->dest->index);
155 for (unsigned int i = 1; i < path.length (); i++)
157 /* We can get paths with a NULL edge when the final destination
158 of a jump thread turns out to be a constant address. We dump
159 those paths when debugging, so we have to be prepared for that
160 possibility here. */
161 if (path[i]->e == NULL)
162 continue;
164 if (path[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
165 fprintf (dump_file, " (%d, %d) joiner; ",
166 path[i]->e->src->index, path[i]->e->dest->index);
167 if (path[i]->type == EDGE_COPY_SRC_BLOCK)
168 fprintf (dump_file, " (%d, %d) normal;",
169 path[i]->e->src->index, path[i]->e->dest->index);
170 if (path[i]->type == EDGE_NO_COPY_SRC_BLOCK)
171 fprintf (dump_file, " (%d, %d) nocopy;",
172 path[i]->e->src->index, path[i]->e->dest->index);
173 if (path[0]->type == EDGE_FSM_THREAD)
174 fprintf (dump_file, " (%d, %d) ",
175 path[i]->e->src->index, path[i]->e->dest->index);
177 fputc ('\n', dump_file);
180 /* Simple hashing function. For any given incoming edge E, we're going
181 to be most concerned with the final destination of its jump thread
182 path. So hash on the block index of the final edge in the path. */
184 inline hashval_t
185 redirection_data::hash (const redirection_data *p)
187 vec<jump_thread_edge *> *path = p->path;
188 return path->last ()->e->dest->index;
191 /* Given two hash table entries, return true if they have the same
192 jump threading path. */
193 inline int
194 redirection_data::equal (const redirection_data *p1, const redirection_data *p2)
196 vec<jump_thread_edge *> *path1 = p1->path;
197 vec<jump_thread_edge *> *path2 = p2->path;
199 if (path1->length () != path2->length ())
200 return false;
202 for (unsigned int i = 1; i < path1->length (); i++)
204 if ((*path1)[i]->type != (*path2)[i]->type
205 || (*path1)[i]->e != (*path2)[i]->e)
206 return false;
209 return true;
212 /* Rather than search all the edges in jump thread paths each time
213 DOM is able to simply if control statement, we build a hash table
214 with the deleted edges. We only care about the address of the edge,
215 not its contents. */
216 struct removed_edges : nofree_ptr_hash<edge_def>
218 static hashval_t hash (edge e) { return htab_hash_pointer (e); }
219 static bool equal (edge e1, edge e2) { return e1 == e2; }
222 static hash_table<removed_edges> *removed_edges;
224 /* Data structure of information to pass to hash table traversal routines. */
225 struct ssa_local_info_t
227 /* The current block we are working on. */
228 basic_block bb;
230 /* We only create a template block for the first duplicated block in a
231 jump threading path as we may need many duplicates of that block.
233 The second duplicate block in a path is specific to that path. Creating
234 and sharing a template for that block is considerably more difficult. */
235 basic_block template_block;
237 /* TRUE if we thread one or more jumps, FALSE otherwise. */
238 bool jumps_threaded;
240 /* Blocks duplicated for the thread. */
241 bitmap duplicate_blocks;
243 /* When we have multiple paths through a joiner which reach different
244 final destinations, then we may need to correct for potential
245 profile insanities. */
246 bool need_profile_correction;
249 /* Passes which use the jump threading code register jump threading
250 opportunities as they are discovered. We keep the registered
251 jump threading opportunities in this vector as edge pairs
252 (original_edge, target_edge). */
253 static vec<vec<jump_thread_edge *> *> paths;
255 /* When we start updating the CFG for threading, data necessary for jump
256 threading is attached to the AUX field for the incoming edge. Use these
257 macros to access the underlying structure attached to the AUX field. */
258 #define THREAD_PATH(E) ((vec<jump_thread_edge *> *)(E)->aux)
260 /* Jump threading statistics. */
262 struct thread_stats_d
264 unsigned long num_threaded_edges;
267 struct thread_stats_d thread_stats;
270 /* Remove the last statement in block BB if it is a control statement
271 Also remove all outgoing edges except the edge which reaches DEST_BB.
272 If DEST_BB is NULL, then remove all outgoing edges. */
274 void
275 remove_ctrl_stmt_and_useless_edges (basic_block bb, basic_block dest_bb)
277 gimple_stmt_iterator gsi;
278 edge e;
279 edge_iterator ei;
281 gsi = gsi_last_bb (bb);
283 /* If the duplicate ends with a control statement, then remove it.
285 Note that if we are duplicating the template block rather than the
286 original basic block, then the duplicate might not have any real
287 statements in it. */
288 if (!gsi_end_p (gsi)
289 && gsi_stmt (gsi)
290 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
291 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
292 || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH))
293 gsi_remove (&gsi, true);
295 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
297 if (e->dest != dest_bb)
299 free_dom_edge_info (e);
300 remove_edge (e);
302 else
303 ei_next (&ei);
306 /* If the remaining edge is a loop exit, there must have
307 a removed edge that was not a loop exit.
309 In that case BB and possibly other blocks were previously
310 in the loop, but are now outside the loop. Thus, we need
311 to update the loop structures. */
312 if (single_succ_p (bb)
313 && loop_outer (bb->loop_father)
314 && loop_exit_edge_p (bb->loop_father, single_succ_edge (bb)))
315 loops_state_set (LOOPS_NEED_FIXUP);
318 /* Create a duplicate of BB. Record the duplicate block in an array
319 indexed by COUNT stored in RD. */
321 static void
322 create_block_for_threading (basic_block bb,
323 struct redirection_data *rd,
324 unsigned int count,
325 bitmap *duplicate_blocks)
327 edge_iterator ei;
328 edge e;
330 /* We can use the generic block duplication code and simply remove
331 the stuff we do not need. */
332 rd->dup_blocks[count] = duplicate_block (bb, NULL, NULL);
334 FOR_EACH_EDGE (e, ei, rd->dup_blocks[count]->succs)
335 e->aux = NULL;
337 /* Zero out the profile, since the block is unreachable for now. */
338 rd->dup_blocks[count]->frequency = 0;
339 rd->dup_blocks[count]->count = 0;
340 if (duplicate_blocks)
341 bitmap_set_bit (*duplicate_blocks, rd->dup_blocks[count]->index);
344 /* Main data structure to hold information for duplicates of BB. */
346 static hash_table<redirection_data> *redirection_data;
348 /* Given an outgoing edge E lookup and return its entry in our hash table.
350 If INSERT is true, then we insert the entry into the hash table if
351 it is not already present. INCOMING_EDGE is added to the list of incoming
352 edges associated with E in the hash table. */
354 static struct redirection_data *
355 lookup_redirection_data (edge e, enum insert_option insert)
357 struct redirection_data **slot;
358 struct redirection_data *elt;
359 vec<jump_thread_edge *> *path = THREAD_PATH (e);
361 /* Build a hash table element so we can see if E is already
362 in the table. */
363 elt = XNEW (struct redirection_data);
364 elt->path = path;
365 elt->dup_blocks[0] = NULL;
366 elt->dup_blocks[1] = NULL;
367 elt->incoming_edges = NULL;
369 slot = redirection_data->find_slot (elt, insert);
371 /* This will only happen if INSERT is false and the entry is not
372 in the hash table. */
373 if (slot == NULL)
375 free (elt);
376 return NULL;
379 /* This will only happen if E was not in the hash table and
380 INSERT is true. */
381 if (*slot == NULL)
383 *slot = elt;
384 elt->incoming_edges = XNEW (struct el);
385 elt->incoming_edges->e = e;
386 elt->incoming_edges->next = NULL;
387 return elt;
389 /* E was in the hash table. */
390 else
392 /* Free ELT as we do not need it anymore, we will extract the
393 relevant entry from the hash table itself. */
394 free (elt);
396 /* Get the entry stored in the hash table. */
397 elt = *slot;
399 /* If insertion was requested, then we need to add INCOMING_EDGE
400 to the list of incoming edges associated with E. */
401 if (insert)
403 struct el *el = XNEW (struct el);
404 el->next = elt->incoming_edges;
405 el->e = e;
406 elt->incoming_edges = el;
409 return elt;
413 /* Similar to copy_phi_args, except that the PHI arg exists, it just
414 does not have a value associated with it. */
416 static void
417 copy_phi_arg_into_existing_phi (edge src_e, edge tgt_e)
419 int src_idx = src_e->dest_idx;
420 int tgt_idx = tgt_e->dest_idx;
422 /* Iterate over each PHI in e->dest. */
423 for (gphi_iterator gsi = gsi_start_phis (src_e->dest),
424 gsi2 = gsi_start_phis (tgt_e->dest);
425 !gsi_end_p (gsi);
426 gsi_next (&gsi), gsi_next (&gsi2))
428 gphi *src_phi = gsi.phi ();
429 gphi *dest_phi = gsi2.phi ();
430 tree val = gimple_phi_arg_def (src_phi, src_idx);
431 source_location locus = gimple_phi_arg_location (src_phi, src_idx);
433 SET_PHI_ARG_DEF (dest_phi, tgt_idx, val);
434 gimple_phi_arg_set_location (dest_phi, tgt_idx, locus);
438 /* Given ssa_name DEF, backtrack jump threading PATH from node IDX
439 to see if it has constant value in a flow sensitive manner. Set
440 LOCUS to location of the constant phi arg and return the value.
441 Return DEF directly if either PATH or idx is ZERO. */
443 static tree
444 get_value_locus_in_path (tree def, vec<jump_thread_edge *> *path,
445 basic_block bb, int idx, source_location *locus)
447 tree arg;
448 gphi *def_phi;
449 basic_block def_bb;
451 if (path == NULL || idx == 0)
452 return def;
454 def_phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (def));
455 if (!def_phi)
456 return def;
458 def_bb = gimple_bb (def_phi);
459 /* Don't propagate loop invariants into deeper loops. */
460 if (!def_bb || bb_loop_depth (def_bb) < bb_loop_depth (bb))
461 return def;
463 /* Backtrack jump threading path from IDX to see if def has constant
464 value. */
465 for (int j = idx - 1; j >= 0; j--)
467 edge e = (*path)[j]->e;
468 if (e->dest == def_bb)
470 arg = gimple_phi_arg_def (def_phi, e->dest_idx);
471 if (is_gimple_min_invariant (arg))
473 *locus = gimple_phi_arg_location (def_phi, e->dest_idx);
474 return arg;
476 break;
480 return def;
483 /* For each PHI in BB, copy the argument associated with SRC_E to TGT_E.
484 Try to backtrack jump threading PATH from node IDX to see if the arg
485 has constant value, copy constant value instead of argument itself
486 if yes. */
488 static void
489 copy_phi_args (basic_block bb, edge src_e, edge tgt_e,
490 vec<jump_thread_edge *> *path, int idx)
492 gphi_iterator gsi;
493 int src_indx = src_e->dest_idx;
495 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
497 gphi *phi = gsi.phi ();
498 tree def = gimple_phi_arg_def (phi, src_indx);
499 source_location locus = gimple_phi_arg_location (phi, src_indx);
501 if (TREE_CODE (def) == SSA_NAME
502 && !virtual_operand_p (gimple_phi_result (phi)))
503 def = get_value_locus_in_path (def, path, bb, idx, &locus);
505 add_phi_arg (phi, def, tgt_e, locus);
509 /* We have recently made a copy of ORIG_BB, including its outgoing
510 edges. The copy is NEW_BB. Every PHI node in every direct successor of
511 ORIG_BB has a new argument associated with edge from NEW_BB to the
512 successor. Initialize the PHI argument so that it is equal to the PHI
513 argument associated with the edge from ORIG_BB to the successor.
514 PATH and IDX are used to check if the new PHI argument has constant
515 value in a flow sensitive manner. */
517 static void
518 update_destination_phis (basic_block orig_bb, basic_block new_bb,
519 vec<jump_thread_edge *> *path, int idx)
521 edge_iterator ei;
522 edge e;
524 FOR_EACH_EDGE (e, ei, orig_bb->succs)
526 edge e2 = find_edge (new_bb, e->dest);
527 copy_phi_args (e->dest, e, e2, path, idx);
531 /* Given a duplicate block and its single destination (both stored
532 in RD). Create an edge between the duplicate and its single
533 destination.
535 Add an additional argument to any PHI nodes at the single
536 destination. IDX is the start node in jump threading path
537 we start to check to see if the new PHI argument has constant
538 value along the jump threading path. */
540 static void
541 create_edge_and_update_destination_phis (struct redirection_data *rd,
542 basic_block bb, int idx)
544 edge e = make_edge (bb, rd->path->last ()->e->dest, EDGE_FALLTHRU);
546 rescan_loop_exit (e, true, false);
547 e->probability = REG_BR_PROB_BASE;
548 e->count = bb->count;
550 /* We used to copy the thread path here. That was added in 2007
551 and dutifully updated through the representation changes in 2013.
553 In 2013 we added code to thread from an interior node through
554 the backedge to another interior node. That runs after the code
555 to thread through loop headers from outside the loop.
557 The latter may delete edges in the CFG, including those
558 which appeared in the jump threading path we copied here. Thus
559 we'd end up using a dangling pointer.
561 After reviewing the 2007/2011 code, I can't see how anything
562 depended on copying the AUX field and clearly copying the jump
563 threading path is problematical due to embedded edge pointers.
564 It has been removed. */
565 e->aux = NULL;
567 /* If there are any PHI nodes at the destination of the outgoing edge
568 from the duplicate block, then we will need to add a new argument
569 to them. The argument should have the same value as the argument
570 associated with the outgoing edge stored in RD. */
571 copy_phi_args (e->dest, rd->path->last ()->e, e, rd->path, idx);
574 /* Look through PATH beginning at START and return TRUE if there are
575 any additional blocks that need to be duplicated. Otherwise,
576 return FALSE. */
577 static bool
578 any_remaining_duplicated_blocks (vec<jump_thread_edge *> *path,
579 unsigned int start)
581 for (unsigned int i = start + 1; i < path->length (); i++)
583 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK
584 || (*path)[i]->type == EDGE_COPY_SRC_BLOCK)
585 return true;
587 return false;
591 /* Compute the amount of profile count/frequency coming into the jump threading
592 path stored in RD that we are duplicating, returned in PATH_IN_COUNT_PTR and
593 PATH_IN_FREQ_PTR, as well as the amount of counts flowing out of the
594 duplicated path, returned in PATH_OUT_COUNT_PTR. LOCAL_INFO is used to
595 identify blocks duplicated for jump threading, which have duplicated
596 edges that need to be ignored in the analysis. Return true if path contains
597 a joiner, false otherwise.
599 In the non-joiner case, this is straightforward - all the counts/frequency
600 flowing into the jump threading path should flow through the duplicated
601 block and out of the duplicated path.
603 In the joiner case, it is very tricky. Some of the counts flowing into
604 the original path go offpath at the joiner. The problem is that while
605 we know how much total count goes off-path in the original control flow,
606 we don't know how many of the counts corresponding to just the jump
607 threading path go offpath at the joiner.
609 For example, assume we have the following control flow and identified
610 jump threading paths:
612 A B C
613 \ | /
614 Ea \ |Eb / Ec
615 \ | /
616 v v v
617 J <-- Joiner
619 Eoff/ \Eon
622 Soff Son <--- Normal
624 Ed/ \ Ee
629 Jump threading paths: A -> J -> Son -> D (path 1)
630 C -> J -> Son -> E (path 2)
632 Note that the control flow could be more complicated:
633 - Each jump threading path may have more than one incoming edge. I.e. A and
634 Ea could represent multiple incoming blocks/edges that are included in
635 path 1.
636 - There could be EDGE_NO_COPY_SRC_BLOCK edges after the joiner (either
637 before or after the "normal" copy block). These are not duplicated onto
638 the jump threading path, as they are single-successor.
639 - Any of the blocks along the path may have other incoming edges that
640 are not part of any jump threading path, but add profile counts along
641 the path.
643 In the above example, after all jump threading is complete, we will
644 end up with the following control flow:
646 A B C
647 | | |
648 Ea| |Eb |Ec
649 | | |
650 v v v
651 Ja J Jc
652 / \ / \Eon' / \
653 Eona/ \ ---/---\-------- \Eonc
654 / \ / / \ \
655 v v v v v
656 Sona Soff Son Sonc
657 \ /\ /
658 \___________ / \ _____/
659 \ / \/
660 vv v
663 The main issue to notice here is that when we are processing path 1
664 (A->J->Son->D) we need to figure out the outgoing edge weights to
665 the duplicated edges Ja->Sona and Ja->Soff, while ensuring that the
666 sum of the incoming weights to D remain Ed. The problem with simply
667 assuming that Ja (and Jc when processing path 2) has the same outgoing
668 probabilities to its successors as the original block J, is that after
669 all paths are processed and other edges/counts removed (e.g. none
670 of Ec will reach D after processing path 2), we may end up with not
671 enough count flowing along duplicated edge Sona->D.
673 Therefore, in the case of a joiner, we keep track of all counts
674 coming in along the current path, as well as from predecessors not
675 on any jump threading path (Eb in the above example). While we
676 first assume that the duplicated Eona for Ja->Sona has the same
677 probability as the original, we later compensate for other jump
678 threading paths that may eliminate edges. We do that by keep track
679 of all counts coming into the original path that are not in a jump
680 thread (Eb in the above example, but as noted earlier, there could
681 be other predecessors incoming to the path at various points, such
682 as at Son). Call this cumulative non-path count coming into the path
683 before D as Enonpath. We then ensure that the count from Sona->D is as at
684 least as big as (Ed - Enonpath), but no bigger than the minimum
685 weight along the jump threading path. The probabilities of both the
686 original and duplicated joiner block J and Ja will be adjusted
687 accordingly after the updates. */
689 static bool
690 compute_path_counts (struct redirection_data *rd,
691 ssa_local_info_t *local_info,
692 gcov_type *path_in_count_ptr,
693 gcov_type *path_out_count_ptr,
694 int *path_in_freq_ptr)
696 edge e = rd->incoming_edges->e;
697 vec<jump_thread_edge *> *path = THREAD_PATH (e);
698 edge elast = path->last ()->e;
699 gcov_type nonpath_count = 0;
700 bool has_joiner = false;
701 gcov_type path_in_count = 0;
702 int path_in_freq = 0;
704 /* Start by accumulating incoming edge counts to the path's first bb
705 into a couple buckets:
706 path_in_count: total count of incoming edges that flow into the
707 current path.
708 nonpath_count: total count of incoming edges that are not
709 flowing along *any* path. These are the counts
710 that will still flow along the original path after
711 all path duplication is done by potentially multiple
712 calls to this routine.
713 (any other incoming edge counts are for a different jump threading
714 path that will be handled by a later call to this routine.)
715 To make this easier, start by recording all incoming edges that flow into
716 the current path in a bitmap. We could add up the path's incoming edge
717 counts here, but we still need to walk all the first bb's incoming edges
718 below to add up the counts of the other edges not included in this jump
719 threading path. */
720 struct el *next, *el;
721 bitmap in_edge_srcs = BITMAP_ALLOC (NULL);
722 for (el = rd->incoming_edges; el; el = next)
724 next = el->next;
725 bitmap_set_bit (in_edge_srcs, el->e->src->index);
727 edge ein;
728 edge_iterator ei;
729 FOR_EACH_EDGE (ein, ei, e->dest->preds)
731 vec<jump_thread_edge *> *ein_path = THREAD_PATH (ein);
732 /* Simply check the incoming edge src against the set captured above. */
733 if (ein_path
734 && bitmap_bit_p (in_edge_srcs, (*ein_path)[0]->e->src->index))
736 /* It is necessary but not sufficient that the last path edges
737 are identical. There may be different paths that share the
738 same last path edge in the case where the last edge has a nocopy
739 source block. */
740 gcc_assert (ein_path->last ()->e == elast);
741 path_in_count += ein->count;
742 path_in_freq += EDGE_FREQUENCY (ein);
744 else if (!ein_path)
746 /* Keep track of the incoming edges that are not on any jump-threading
747 path. These counts will still flow out of original path after all
748 jump threading is complete. */
749 nonpath_count += ein->count;
753 /* This is needed due to insane incoming frequencies. */
754 if (path_in_freq > BB_FREQ_MAX)
755 path_in_freq = BB_FREQ_MAX;
757 BITMAP_FREE (in_edge_srcs);
759 /* Now compute the fraction of the total count coming into the first
760 path bb that is from the current threading path. */
761 gcov_type total_count = e->dest->count;
762 /* Handle incoming profile insanities. */
763 if (total_count < path_in_count)
764 path_in_count = total_count;
765 int onpath_scale = GCOV_COMPUTE_SCALE (path_in_count, total_count);
767 /* Walk the entire path to do some more computation in order to estimate
768 how much of the path_in_count will flow out of the duplicated threading
769 path. In the non-joiner case this is straightforward (it should be
770 the same as path_in_count, although we will handle incoming profile
771 insanities by setting it equal to the minimum count along the path).
773 In the joiner case, we need to estimate how much of the path_in_count
774 will stay on the threading path after the joiner's conditional branch.
775 We don't really know for sure how much of the counts
776 associated with this path go to each successor of the joiner, but we'll
777 estimate based on the fraction of the total count coming into the path
778 bb was from the threading paths (computed above in onpath_scale).
779 Afterwards, we will need to do some fixup to account for other threading
780 paths and possible profile insanities.
782 In order to estimate the joiner case's counts we also need to update
783 nonpath_count with any additional counts coming into the path. Other
784 blocks along the path may have additional predecessors from outside
785 the path. */
786 gcov_type path_out_count = path_in_count;
787 gcov_type min_path_count = path_in_count;
788 for (unsigned int i = 1; i < path->length (); i++)
790 edge epath = (*path)[i]->e;
791 gcov_type cur_count = epath->count;
792 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
794 has_joiner = true;
795 cur_count = apply_probability (cur_count, onpath_scale);
797 /* In the joiner case we need to update nonpath_count for any edges
798 coming into the path that will contribute to the count flowing
799 into the path successor. */
800 if (has_joiner && epath != elast)
802 /* Look for other incoming edges after joiner. */
803 FOR_EACH_EDGE (ein, ei, epath->dest->preds)
805 if (ein != epath
806 /* Ignore in edges from blocks we have duplicated for a
807 threading path, which have duplicated edge counts until
808 they are redirected by an invocation of this routine. */
809 && !bitmap_bit_p (local_info->duplicate_blocks,
810 ein->src->index))
811 nonpath_count += ein->count;
814 if (cur_count < path_out_count)
815 path_out_count = cur_count;
816 if (epath->count < min_path_count)
817 min_path_count = epath->count;
820 /* We computed path_out_count above assuming that this path targeted
821 the joiner's on-path successor with the same likelihood as it
822 reached the joiner. However, other thread paths through the joiner
823 may take a different path through the normal copy source block
824 (i.e. they have a different elast), meaning that they do not
825 contribute any counts to this path's elast. As a result, it may
826 turn out that this path must have more count flowing to the on-path
827 successor of the joiner. Essentially, all of this path's elast
828 count must be contributed by this path and any nonpath counts
829 (since any path through the joiner with a different elast will not
830 include a copy of this elast in its duplicated path).
831 So ensure that this path's path_out_count is at least the
832 difference between elast->count and nonpath_count. Otherwise the edge
833 counts after threading will not be sane. */
834 if (local_info->need_profile_correction
835 && has_joiner && path_out_count < elast->count - nonpath_count)
837 path_out_count = elast->count - nonpath_count;
838 /* But neither can we go above the minimum count along the path
839 we are duplicating. This can be an issue due to profile
840 insanities coming in to this pass. */
841 if (path_out_count > min_path_count)
842 path_out_count = min_path_count;
845 *path_in_count_ptr = path_in_count;
846 *path_out_count_ptr = path_out_count;
847 *path_in_freq_ptr = path_in_freq;
848 return has_joiner;
852 /* Update the counts and frequencies for both an original path
853 edge EPATH and its duplicate EDUP. The duplicate source block
854 will get a count/frequency of PATH_IN_COUNT and PATH_IN_FREQ,
855 and the duplicate edge EDUP will have a count of PATH_OUT_COUNT. */
856 static void
857 update_profile (edge epath, edge edup, gcov_type path_in_count,
858 gcov_type path_out_count, int path_in_freq)
861 /* First update the duplicated block's count / frequency. */
862 if (edup)
864 basic_block dup_block = edup->src;
865 gcc_assert (dup_block->count == 0);
866 gcc_assert (dup_block->frequency == 0);
867 dup_block->count = path_in_count;
868 dup_block->frequency = path_in_freq;
871 /* Now update the original block's count and frequency in the
872 opposite manner - remove the counts/freq that will flow
873 into the duplicated block. Handle underflow due to precision/
874 rounding issues. */
875 epath->src->count -= path_in_count;
876 if (epath->src->count < 0)
877 epath->src->count = 0;
878 epath->src->frequency -= path_in_freq;
879 if (epath->src->frequency < 0)
880 epath->src->frequency = 0;
882 /* Next update this path edge's original and duplicated counts. We know
883 that the duplicated path will have path_out_count flowing
884 out of it (in the joiner case this is the count along the duplicated path
885 out of the duplicated joiner). This count can then be removed from the
886 original path edge. */
887 if (edup)
888 edup->count = path_out_count;
889 epath->count -= path_out_count;
890 gcc_assert (epath->count >= 0);
894 /* The duplicate and original joiner blocks may end up with different
895 probabilities (different from both the original and from each other).
896 Recompute the probabilities here once we have updated the edge
897 counts and frequencies. */
899 static void
900 recompute_probabilities (basic_block bb)
902 edge esucc;
903 edge_iterator ei;
904 FOR_EACH_EDGE (esucc, ei, bb->succs)
906 if (!bb->count)
907 continue;
909 /* Prevent overflow computation due to insane profiles. */
910 if (esucc->count < bb->count)
911 esucc->probability = GCOV_COMPUTE_SCALE (esucc->count,
912 bb->count);
913 else
914 /* Can happen with missing/guessed probabilities, since we
915 may determine that more is flowing along duplicated
916 path than joiner succ probabilities allowed.
917 Counts and freqs will be insane after jump threading,
918 at least make sure probability is sane or we will
919 get a flow verification error.
920 Not much we can do to make counts/freqs sane without
921 redoing the profile estimation. */
922 esucc->probability = REG_BR_PROB_BASE;
927 /* Update the counts of the original and duplicated edges from a joiner
928 that go off path, given that we have already determined that the
929 duplicate joiner DUP_BB has incoming count PATH_IN_COUNT and
930 outgoing count along the path PATH_OUT_COUNT. The original (on-)path
931 edge from joiner is EPATH. */
933 static void
934 update_joiner_offpath_counts (edge epath, basic_block dup_bb,
935 gcov_type path_in_count,
936 gcov_type path_out_count)
938 /* Compute the count that currently flows off path from the joiner.
939 In other words, the total count of joiner's out edges other than
940 epath. Compute this by walking the successors instead of
941 subtracting epath's count from the joiner bb count, since there
942 are sometimes slight insanities where the total out edge count is
943 larger than the bb count (possibly due to rounding/truncation
944 errors). */
945 gcov_type total_orig_off_path_count = 0;
946 edge enonpath;
947 edge_iterator ei;
948 FOR_EACH_EDGE (enonpath, ei, epath->src->succs)
950 if (enonpath == epath)
951 continue;
952 total_orig_off_path_count += enonpath->count;
955 /* For the path that we are duplicating, the amount that will flow
956 off path from the duplicated joiner is the delta between the
957 path's cumulative in count and the portion of that count we
958 estimated above as flowing from the joiner along the duplicated
959 path. */
960 gcov_type total_dup_off_path_count = path_in_count - path_out_count;
962 /* Now do the actual updates of the off-path edges. */
963 FOR_EACH_EDGE (enonpath, ei, epath->src->succs)
965 /* Look for edges going off of the threading path. */
966 if (enonpath == epath)
967 continue;
969 /* Find the corresponding edge out of the duplicated joiner. */
970 edge enonpathdup = find_edge (dup_bb, enonpath->dest);
971 gcc_assert (enonpathdup);
973 /* We can't use the original probability of the joiner's out
974 edges, since the probabilities of the original branch
975 and the duplicated branches may vary after all threading is
976 complete. But apportion the duplicated joiner's off-path
977 total edge count computed earlier (total_dup_off_path_count)
978 among the duplicated off-path edges based on their original
979 ratio to the full off-path count (total_orig_off_path_count).
981 int scale = GCOV_COMPUTE_SCALE (enonpath->count,
982 total_orig_off_path_count);
983 /* Give the duplicated offpath edge a portion of the duplicated
984 total. */
985 enonpathdup->count = apply_scale (scale,
986 total_dup_off_path_count);
987 /* Now update the original offpath edge count, handling underflow
988 due to rounding errors. */
989 enonpath->count -= enonpathdup->count;
990 if (enonpath->count < 0)
991 enonpath->count = 0;
996 /* Check if the paths through RD all have estimated frequencies but zero
997 profile counts. This is more accurate than checking the entry block
998 for a zero profile count, since profile insanities sometimes creep in. */
1000 static bool
1001 estimated_freqs_path (struct redirection_data *rd)
1003 edge e = rd->incoming_edges->e;
1004 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1005 edge ein;
1006 edge_iterator ei;
1007 bool non_zero_freq = false;
1008 FOR_EACH_EDGE (ein, ei, e->dest->preds)
1010 if (ein->count)
1011 return false;
1012 non_zero_freq |= ein->src->frequency != 0;
1015 for (unsigned int i = 1; i < path->length (); i++)
1017 edge epath = (*path)[i]->e;
1018 if (epath->src->count)
1019 return false;
1020 non_zero_freq |= epath->src->frequency != 0;
1021 edge esucc;
1022 FOR_EACH_EDGE (esucc, ei, epath->src->succs)
1024 if (esucc->count)
1025 return false;
1026 non_zero_freq |= esucc->src->frequency != 0;
1029 return non_zero_freq;
1033 /* Invoked for routines that have guessed frequencies and no profile
1034 counts to record the block and edge frequencies for paths through RD
1035 in the profile count fields of those blocks and edges. This is because
1036 ssa_fix_duplicate_block_edges incrementally updates the block and
1037 edge counts as edges are redirected, and it is difficult to do that
1038 for edge frequencies which are computed on the fly from the source
1039 block frequency and probability. When a block frequency is updated
1040 its outgoing edge frequencies are affected and become difficult to
1041 adjust. */
1043 static void
1044 freqs_to_counts_path (struct redirection_data *rd)
1046 edge e = rd->incoming_edges->e;
1047 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1048 edge ein;
1049 edge_iterator ei;
1050 FOR_EACH_EDGE (ein, ei, e->dest->preds)
1052 /* Scale up the frequency by REG_BR_PROB_BASE, to avoid rounding
1053 errors applying the probability when the frequencies are very
1054 small. */
1055 ein->count = apply_probability (ein->src->frequency * REG_BR_PROB_BASE,
1056 ein->probability);
1059 for (unsigned int i = 1; i < path->length (); i++)
1061 edge epath = (*path)[i]->e;
1062 edge esucc;
1063 /* Scale up the frequency by REG_BR_PROB_BASE, to avoid rounding
1064 errors applying the edge probability when the frequencies are very
1065 small. */
1066 epath->src->count = epath->src->frequency * REG_BR_PROB_BASE;
1067 FOR_EACH_EDGE (esucc, ei, epath->src->succs)
1068 esucc->count = apply_probability (esucc->src->count,
1069 esucc->probability);
1074 /* For routines that have guessed frequencies and no profile counts, where we
1075 used freqs_to_counts_path to record block and edge frequencies for paths
1076 through RD, we clear the counts after completing all updates for RD.
1077 The updates in ssa_fix_duplicate_block_edges are based off the count fields,
1078 but the block frequencies and edge probabilities were updated as well,
1079 so we can simply clear the count fields. */
1081 static void
1082 clear_counts_path (struct redirection_data *rd)
1084 edge e = rd->incoming_edges->e;
1085 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1086 edge ein, esucc;
1087 edge_iterator ei;
1088 FOR_EACH_EDGE (ein, ei, e->dest->preds)
1089 ein->count = 0;
1091 /* First clear counts along original path. */
1092 for (unsigned int i = 1; i < path->length (); i++)
1094 edge epath = (*path)[i]->e;
1095 FOR_EACH_EDGE (esucc, ei, epath->src->succs)
1096 esucc->count = 0;
1097 epath->src->count = 0;
1099 /* Also need to clear the counts along duplicated path. */
1100 for (unsigned int i = 0; i < 2; i++)
1102 basic_block dup = rd->dup_blocks[i];
1103 if (!dup)
1104 continue;
1105 FOR_EACH_EDGE (esucc, ei, dup->succs)
1106 esucc->count = 0;
1107 dup->count = 0;
1111 /* Wire up the outgoing edges from the duplicate blocks and
1112 update any PHIs as needed. Also update the profile counts
1113 on the original and duplicate blocks and edges. */
1114 void
1115 ssa_fix_duplicate_block_edges (struct redirection_data *rd,
1116 ssa_local_info_t *local_info)
1118 bool multi_incomings = (rd->incoming_edges->next != NULL);
1119 edge e = rd->incoming_edges->e;
1120 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1121 edge elast = path->last ()->e;
1122 gcov_type path_in_count = 0;
1123 gcov_type path_out_count = 0;
1124 int path_in_freq = 0;
1126 /* This routine updates profile counts, frequencies, and probabilities
1127 incrementally. Since it is difficult to do the incremental updates
1128 using frequencies/probabilities alone, for routines without profile
1129 data we first take a snapshot of the existing block and edge frequencies
1130 by copying them into the empty profile count fields. These counts are
1131 then used to do the incremental updates, and cleared at the end of this
1132 routine. If the function is marked as having a profile, we still check
1133 to see if the paths through RD are using estimated frequencies because
1134 the routine had zero profile counts. */
1135 bool do_freqs_to_counts = (profile_status_for_fn (cfun) != PROFILE_READ
1136 || estimated_freqs_path (rd));
1137 if (do_freqs_to_counts)
1138 freqs_to_counts_path (rd);
1140 /* First determine how much profile count to move from original
1141 path to the duplicate path. This is tricky in the presence of
1142 a joiner (see comments for compute_path_counts), where some portion
1143 of the path's counts will flow off-path from the joiner. In the
1144 non-joiner case the path_in_count and path_out_count should be the
1145 same. */
1146 bool has_joiner = compute_path_counts (rd, local_info,
1147 &path_in_count, &path_out_count,
1148 &path_in_freq);
1150 int cur_path_freq = path_in_freq;
1151 for (unsigned int count = 0, i = 1; i < path->length (); i++)
1153 edge epath = (*path)[i]->e;
1155 /* If we were threading through an joiner block, then we want
1156 to keep its control statement and redirect an outgoing edge.
1157 Else we want to remove the control statement & edges, then create
1158 a new outgoing edge. In both cases we may need to update PHIs. */
1159 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1161 edge victim;
1162 edge e2;
1164 gcc_assert (has_joiner);
1166 /* This updates the PHIs at the destination of the duplicate
1167 block. Pass 0 instead of i if we are threading a path which
1168 has multiple incoming edges. */
1169 update_destination_phis (local_info->bb, rd->dup_blocks[count],
1170 path, multi_incomings ? 0 : i);
1172 /* Find the edge from the duplicate block to the block we're
1173 threading through. That's the edge we want to redirect. */
1174 victim = find_edge (rd->dup_blocks[count], (*path)[i]->e->dest);
1176 /* If there are no remaining blocks on the path to duplicate,
1177 then redirect VICTIM to the final destination of the jump
1178 threading path. */
1179 if (!any_remaining_duplicated_blocks (path, i))
1181 e2 = redirect_edge_and_branch (victim, elast->dest);
1182 /* If we redirected the edge, then we need to copy PHI arguments
1183 at the target. If the edge already existed (e2 != victim
1184 case), then the PHIs in the target already have the correct
1185 arguments. */
1186 if (e2 == victim)
1187 copy_phi_args (e2->dest, elast, e2,
1188 path, multi_incomings ? 0 : i);
1190 else
1192 /* Redirect VICTIM to the next duplicated block in the path. */
1193 e2 = redirect_edge_and_branch (victim, rd->dup_blocks[count + 1]);
1195 /* We need to update the PHIs in the next duplicated block. We
1196 want the new PHI args to have the same value as they had
1197 in the source of the next duplicate block.
1199 Thus, we need to know which edge we traversed into the
1200 source of the duplicate. Furthermore, we may have
1201 traversed many edges to reach the source of the duplicate.
1203 Walk through the path starting at element I until we
1204 hit an edge marked with EDGE_COPY_SRC_BLOCK. We want
1205 the edge from the prior element. */
1206 for (unsigned int j = i + 1; j < path->length (); j++)
1208 if ((*path)[j]->type == EDGE_COPY_SRC_BLOCK)
1210 copy_phi_arg_into_existing_phi ((*path)[j - 1]->e, e2);
1211 break;
1216 /* Update the counts and frequency of both the original block
1217 and path edge, and the duplicates. The path duplicate's
1218 incoming count and frequency are the totals for all edges
1219 incoming to this jump threading path computed earlier.
1220 And we know that the duplicated path will have path_out_count
1221 flowing out of it (i.e. along the duplicated path out of the
1222 duplicated joiner). */
1223 update_profile (epath, e2, path_in_count, path_out_count,
1224 path_in_freq);
1226 /* Next we need to update the counts of the original and duplicated
1227 edges from the joiner that go off path. */
1228 update_joiner_offpath_counts (epath, e2->src, path_in_count,
1229 path_out_count);
1231 /* Finally, we need to set the probabilities on the duplicated
1232 edges out of the duplicated joiner (e2->src). The probabilities
1233 along the original path will all be updated below after we finish
1234 processing the whole path. */
1235 recompute_probabilities (e2->src);
1237 /* Record the frequency flowing to the downstream duplicated
1238 path blocks. */
1239 cur_path_freq = EDGE_FREQUENCY (e2);
1241 else if ((*path)[i]->type == EDGE_COPY_SRC_BLOCK)
1243 remove_ctrl_stmt_and_useless_edges (rd->dup_blocks[count], NULL);
1244 create_edge_and_update_destination_phis (rd, rd->dup_blocks[count],
1245 multi_incomings ? 0 : i);
1246 if (count == 1)
1247 single_succ_edge (rd->dup_blocks[1])->aux = NULL;
1249 /* Update the counts and frequency of both the original block
1250 and path edge, and the duplicates. Since we are now after
1251 any joiner that may have existed on the path, the count
1252 flowing along the duplicated threaded path is path_out_count.
1253 If we didn't have a joiner, then cur_path_freq was the sum
1254 of the total frequencies along all incoming edges to the
1255 thread path (path_in_freq). If we had a joiner, it would have
1256 been updated at the end of that handling to the edge frequency
1257 along the duplicated joiner path edge. */
1258 update_profile (epath, EDGE_SUCC (rd->dup_blocks[count], 0),
1259 path_out_count, path_out_count,
1260 cur_path_freq);
1262 else
1264 /* No copy case. In this case we don't have an equivalent block
1265 on the duplicated thread path to update, but we do need
1266 to remove the portion of the counts/freqs that were moved
1267 to the duplicated path from the counts/freqs flowing through
1268 this block on the original path. Since all the no-copy edges
1269 are after any joiner, the removed count is the same as
1270 path_out_count.
1272 If we didn't have a joiner, then cur_path_freq was the sum
1273 of the total frequencies along all incoming edges to the
1274 thread path (path_in_freq). If we had a joiner, it would have
1275 been updated at the end of that handling to the edge frequency
1276 along the duplicated joiner path edge. */
1277 update_profile (epath, NULL, path_out_count, path_out_count,
1278 cur_path_freq);
1281 /* Increment the index into the duplicated path when we processed
1282 a duplicated block. */
1283 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK
1284 || (*path)[i]->type == EDGE_COPY_SRC_BLOCK)
1286 count++;
1290 /* Now walk orig blocks and update their probabilities, since the
1291 counts and freqs should be updated properly by above loop. */
1292 for (unsigned int i = 1; i < path->length (); i++)
1294 edge epath = (*path)[i]->e;
1295 recompute_probabilities (epath->src);
1298 /* Done with all profile and frequency updates, clear counts if they
1299 were copied. */
1300 if (do_freqs_to_counts)
1301 clear_counts_path (rd);
1304 /* Hash table traversal callback routine to create duplicate blocks. */
1307 ssa_create_duplicates (struct redirection_data **slot,
1308 ssa_local_info_t *local_info)
1310 struct redirection_data *rd = *slot;
1312 /* The second duplicated block in a jump threading path is specific
1313 to the path. So it gets stored in RD rather than in LOCAL_DATA.
1315 Each time we're called, we have to look through the path and see
1316 if a second block needs to be duplicated.
1318 Note the search starts with the third edge on the path. The first
1319 edge is the incoming edge, the second edge always has its source
1320 duplicated. Thus we start our search with the third edge. */
1321 vec<jump_thread_edge *> *path = rd->path;
1322 for (unsigned int i = 2; i < path->length (); i++)
1324 if ((*path)[i]->type == EDGE_COPY_SRC_BLOCK
1325 || (*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1327 create_block_for_threading ((*path)[i]->e->src, rd, 1,
1328 &local_info->duplicate_blocks);
1329 break;
1333 /* Create a template block if we have not done so already. Otherwise
1334 use the template to create a new block. */
1335 if (local_info->template_block == NULL)
1337 create_block_for_threading ((*path)[1]->e->src, rd, 0,
1338 &local_info->duplicate_blocks);
1339 local_info->template_block = rd->dup_blocks[0];
1341 /* We do not create any outgoing edges for the template. We will
1342 take care of that in a later traversal. That way we do not
1343 create edges that are going to just be deleted. */
1345 else
1347 create_block_for_threading (local_info->template_block, rd, 0,
1348 &local_info->duplicate_blocks);
1350 /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
1351 block. */
1352 ssa_fix_duplicate_block_edges (rd, local_info);
1355 /* Keep walking the hash table. */
1356 return 1;
1359 /* We did not create any outgoing edges for the template block during
1360 block creation. This hash table traversal callback creates the
1361 outgoing edge for the template block. */
1363 inline int
1364 ssa_fixup_template_block (struct redirection_data **slot,
1365 ssa_local_info_t *local_info)
1367 struct redirection_data *rd = *slot;
1369 /* If this is the template block halt the traversal after updating
1370 it appropriately.
1372 If we were threading through an joiner block, then we want
1373 to keep its control statement and redirect an outgoing edge.
1374 Else we want to remove the control statement & edges, then create
1375 a new outgoing edge. In both cases we may need to update PHIs. */
1376 if (rd->dup_blocks[0] && rd->dup_blocks[0] == local_info->template_block)
1378 ssa_fix_duplicate_block_edges (rd, local_info);
1379 return 0;
1382 return 1;
1385 /* Hash table traversal callback to redirect each incoming edge
1386 associated with this hash table element to its new destination. */
1389 ssa_redirect_edges (struct redirection_data **slot,
1390 ssa_local_info_t *local_info)
1392 struct redirection_data *rd = *slot;
1393 struct el *next, *el;
1395 /* Walk over all the incoming edges associated with this hash table
1396 entry. */
1397 for (el = rd->incoming_edges; el; el = next)
1399 edge e = el->e;
1400 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1402 /* Go ahead and free this element from the list. Doing this now
1403 avoids the need for another list walk when we destroy the hash
1404 table. */
1405 next = el->next;
1406 free (el);
1408 thread_stats.num_threaded_edges++;
1410 if (rd->dup_blocks[0])
1412 edge e2;
1414 if (dump_file && (dump_flags & TDF_DETAILS))
1415 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
1416 e->src->index, e->dest->index, rd->dup_blocks[0]->index);
1418 /* Redirect the incoming edge (possibly to the joiner block) to the
1419 appropriate duplicate block. */
1420 e2 = redirect_edge_and_branch (e, rd->dup_blocks[0]);
1421 gcc_assert (e == e2);
1422 flush_pending_stmts (e2);
1425 /* Go ahead and clear E->aux. It's not needed anymore and failure
1426 to clear it will cause all kinds of unpleasant problems later. */
1427 delete_jump_thread_path (path);
1428 e->aux = NULL;
1432 /* Indicate that we actually threaded one or more jumps. */
1433 if (rd->incoming_edges)
1434 local_info->jumps_threaded = true;
1436 return 1;
1439 /* Return true if this block has no executable statements other than
1440 a simple ctrl flow instruction. When the number of outgoing edges
1441 is one, this is equivalent to a "forwarder" block. */
1443 static bool
1444 redirection_block_p (basic_block bb)
1446 gimple_stmt_iterator gsi;
1448 /* Advance to the first executable statement. */
1449 gsi = gsi_start_bb (bb);
1450 while (!gsi_end_p (gsi)
1451 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
1452 || is_gimple_debug (gsi_stmt (gsi))
1453 || gimple_nop_p (gsi_stmt (gsi))
1454 || gimple_clobber_p (gsi_stmt (gsi))))
1455 gsi_next (&gsi);
1457 /* Check if this is an empty block. */
1458 if (gsi_end_p (gsi))
1459 return true;
1461 /* Test that we've reached the terminating control statement. */
1462 return gsi_stmt (gsi)
1463 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
1464 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
1465 || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH);
1468 /* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
1469 is reached via one or more specific incoming edges, we know which
1470 outgoing edge from BB will be traversed.
1472 We want to redirect those incoming edges to the target of the
1473 appropriate outgoing edge. Doing so avoids a conditional branch
1474 and may expose new optimization opportunities. Note that we have
1475 to update dominator tree and SSA graph after such changes.
1477 The key to keeping the SSA graph update manageable is to duplicate
1478 the side effects occurring in BB so that those side effects still
1479 occur on the paths which bypass BB after redirecting edges.
1481 We accomplish this by creating duplicates of BB and arranging for
1482 the duplicates to unconditionally pass control to one specific
1483 successor of BB. We then revector the incoming edges into BB to
1484 the appropriate duplicate of BB.
1486 If NOLOOP_ONLY is true, we only perform the threading as long as it
1487 does not affect the structure of the loops in a nontrivial way.
1489 If JOINERS is true, then thread through joiner blocks as well. */
1491 static bool
1492 thread_block_1 (basic_block bb, bool noloop_only, bool joiners)
1494 /* E is an incoming edge into BB that we may or may not want to
1495 redirect to a duplicate of BB. */
1496 edge e, e2;
1497 edge_iterator ei;
1498 ssa_local_info_t local_info;
1500 local_info.duplicate_blocks = BITMAP_ALLOC (NULL);
1501 local_info.need_profile_correction = false;
1503 /* To avoid scanning a linear array for the element we need we instead
1504 use a hash table. For normal code there should be no noticeable
1505 difference. However, if we have a block with a large number of
1506 incoming and outgoing edges such linear searches can get expensive. */
1507 redirection_data
1508 = new hash_table<struct redirection_data> (EDGE_COUNT (bb->succs));
1510 /* Record each unique threaded destination into a hash table for
1511 efficient lookups. */
1512 edge last = NULL;
1513 FOR_EACH_EDGE (e, ei, bb->preds)
1515 if (e->aux == NULL)
1516 continue;
1518 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1520 if (((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK && !joiners)
1521 || ((*path)[1]->type == EDGE_COPY_SRC_BLOCK && joiners))
1522 continue;
1524 e2 = path->last ()->e;
1525 if (!e2 || noloop_only)
1527 /* If NOLOOP_ONLY is true, we only allow threading through the
1528 header of a loop to exit edges. */
1530 /* One case occurs when there was loop header buried in a jump
1531 threading path that crosses loop boundaries. We do not try
1532 and thread this elsewhere, so just cancel the jump threading
1533 request by clearing the AUX field now. */
1534 if (bb->loop_father != e2->src->loop_father
1535 && !loop_exit_edge_p (e2->src->loop_father, e2))
1537 /* Since this case is not handled by our special code
1538 to thread through a loop header, we must explicitly
1539 cancel the threading request here. */
1540 delete_jump_thread_path (path);
1541 e->aux = NULL;
1542 continue;
1545 /* Another case occurs when trying to thread through our
1546 own loop header, possibly from inside the loop. We will
1547 thread these later. */
1548 unsigned int i;
1549 for (i = 1; i < path->length (); i++)
1551 if ((*path)[i]->e->src == bb->loop_father->header
1552 && (!loop_exit_edge_p (bb->loop_father, e2)
1553 || (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK))
1554 break;
1557 if (i != path->length ())
1558 continue;
1561 /* Insert the outgoing edge into the hash table if it is not
1562 already in the hash table. */
1563 lookup_redirection_data (e, INSERT);
1565 /* When we have thread paths through a common joiner with different
1566 final destinations, then we may need corrections to deal with
1567 profile insanities. See the big comment before compute_path_counts. */
1568 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1570 if (!last)
1571 last = e2;
1572 else if (e2 != last)
1573 local_info.need_profile_correction = true;
1577 /* We do not update dominance info. */
1578 free_dominance_info (CDI_DOMINATORS);
1580 /* We know we only thread through the loop header to loop exits.
1581 Let the basic block duplication hook know we are not creating
1582 a multiple entry loop. */
1583 if (noloop_only
1584 && bb == bb->loop_father->header)
1585 set_loop_copy (bb->loop_father, loop_outer (bb->loop_father));
1587 /* Now create duplicates of BB.
1589 Note that for a block with a high outgoing degree we can waste
1590 a lot of time and memory creating and destroying useless edges.
1592 So we first duplicate BB and remove the control structure at the
1593 tail of the duplicate as well as all outgoing edges from the
1594 duplicate. We then use that duplicate block as a template for
1595 the rest of the duplicates. */
1596 local_info.template_block = NULL;
1597 local_info.bb = bb;
1598 local_info.jumps_threaded = false;
1599 redirection_data->traverse <ssa_local_info_t *, ssa_create_duplicates>
1600 (&local_info);
1602 /* The template does not have an outgoing edge. Create that outgoing
1603 edge and update PHI nodes as the edge's target as necessary.
1605 We do this after creating all the duplicates to avoid creating
1606 unnecessary edges. */
1607 redirection_data->traverse <ssa_local_info_t *, ssa_fixup_template_block>
1608 (&local_info);
1610 /* The hash table traversals above created the duplicate blocks (and the
1611 statements within the duplicate blocks). This loop creates PHI nodes for
1612 the duplicated blocks and redirects the incoming edges into BB to reach
1613 the duplicates of BB. */
1614 redirection_data->traverse <ssa_local_info_t *, ssa_redirect_edges>
1615 (&local_info);
1617 /* Done with this block. Clear REDIRECTION_DATA. */
1618 delete redirection_data;
1619 redirection_data = NULL;
1621 if (noloop_only
1622 && bb == bb->loop_father->header)
1623 set_loop_copy (bb->loop_father, NULL);
1625 BITMAP_FREE (local_info.duplicate_blocks);
1626 local_info.duplicate_blocks = NULL;
1628 /* Indicate to our caller whether or not any jumps were threaded. */
1629 return local_info.jumps_threaded;
1632 /* Wrapper for thread_block_1 so that we can first handle jump
1633 thread paths which do not involve copying joiner blocks, then
1634 handle jump thread paths which have joiner blocks.
1636 By doing things this way we can be as aggressive as possible and
1637 not worry that copying a joiner block will create a jump threading
1638 opportunity. */
1640 static bool
1641 thread_block (basic_block bb, bool noloop_only)
1643 bool retval;
1644 retval = thread_block_1 (bb, noloop_only, false);
1645 retval |= thread_block_1 (bb, noloop_only, true);
1646 return retval;
1649 /* Callback for dfs_enumerate_from. Returns true if BB is different
1650 from STOP and DBDS_CE_STOP. */
1652 static basic_block dbds_ce_stop;
1653 static bool
1654 dbds_continue_enumeration_p (const_basic_block bb, const void *stop)
1656 return (bb != (const_basic_block) stop
1657 && bb != dbds_ce_stop);
1660 /* Evaluates the dominance relationship of latch of the LOOP and BB, and
1661 returns the state. */
1663 enum bb_dom_status
1664 determine_bb_domination_status (struct loop *loop, basic_block bb)
1666 basic_block *bblocks;
1667 unsigned nblocks, i;
1668 bool bb_reachable = false;
1669 edge_iterator ei;
1670 edge e;
1672 /* This function assumes BB is a successor of LOOP->header.
1673 If that is not the case return DOMST_NONDOMINATING which
1674 is always safe. */
1676 bool ok = false;
1678 FOR_EACH_EDGE (e, ei, bb->preds)
1680 if (e->src == loop->header)
1682 ok = true;
1683 break;
1687 if (!ok)
1688 return DOMST_NONDOMINATING;
1691 if (bb == loop->latch)
1692 return DOMST_DOMINATING;
1694 /* Check that BB dominates LOOP->latch, and that it is back-reachable
1695 from it. */
1697 bblocks = XCNEWVEC (basic_block, loop->num_nodes);
1698 dbds_ce_stop = loop->header;
1699 nblocks = dfs_enumerate_from (loop->latch, 1, dbds_continue_enumeration_p,
1700 bblocks, loop->num_nodes, bb);
1701 for (i = 0; i < nblocks; i++)
1702 FOR_EACH_EDGE (e, ei, bblocks[i]->preds)
1704 if (e->src == loop->header)
1706 free (bblocks);
1707 return DOMST_NONDOMINATING;
1709 if (e->src == bb)
1710 bb_reachable = true;
1713 free (bblocks);
1714 return (bb_reachable ? DOMST_DOMINATING : DOMST_LOOP_BROKEN);
1717 /* Thread jumps through the header of LOOP. Returns true if cfg changes.
1718 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading from entry edges
1719 to the inside of the loop. */
1721 static bool
1722 thread_through_loop_header (struct loop *loop, bool may_peel_loop_headers)
1724 basic_block header = loop->header;
1725 edge e, tgt_edge, latch = loop_latch_edge (loop);
1726 edge_iterator ei;
1727 basic_block tgt_bb, atgt_bb;
1728 enum bb_dom_status domst;
1730 /* We have already threaded through headers to exits, so all the threading
1731 requests now are to the inside of the loop. We need to avoid creating
1732 irreducible regions (i.e., loops with more than one entry block), and
1733 also loop with several latch edges, or new subloops of the loop (although
1734 there are cases where it might be appropriate, it is difficult to decide,
1735 and doing it wrongly may confuse other optimizers).
1737 We could handle more general cases here. However, the intention is to
1738 preserve some information about the loop, which is impossible if its
1739 structure changes significantly, in a way that is not well understood.
1740 Thus we only handle few important special cases, in which also updating
1741 of the loop-carried information should be feasible:
1743 1) Propagation of latch edge to a block that dominates the latch block
1744 of a loop. This aims to handle the following idiom:
1746 first = 1;
1747 while (1)
1749 if (first)
1750 initialize;
1751 first = 0;
1752 body;
1755 After threading the latch edge, this becomes
1757 first = 1;
1758 if (first)
1759 initialize;
1760 while (1)
1762 first = 0;
1763 body;
1766 The original header of the loop is moved out of it, and we may thread
1767 the remaining edges through it without further constraints.
1769 2) All entry edges are propagated to a single basic block that dominates
1770 the latch block of the loop. This aims to handle the following idiom
1771 (normally created for "for" loops):
1773 i = 0;
1774 while (1)
1776 if (i >= 100)
1777 break;
1778 body;
1779 i++;
1782 This becomes
1784 i = 0;
1785 while (1)
1787 body;
1788 i++;
1789 if (i >= 100)
1790 break;
1794 /* Threading through the header won't improve the code if the header has just
1795 one successor. */
1796 if (single_succ_p (header))
1797 goto fail;
1799 if (!may_peel_loop_headers && !redirection_block_p (loop->header))
1800 goto fail;
1801 else
1803 tgt_bb = NULL;
1804 tgt_edge = NULL;
1805 FOR_EACH_EDGE (e, ei, header->preds)
1807 if (!e->aux)
1809 if (e == latch)
1810 continue;
1812 /* If latch is not threaded, and there is a header
1813 edge that is not threaded, we would create loop
1814 with multiple entries. */
1815 goto fail;
1818 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1820 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1821 goto fail;
1822 tgt_edge = (*path)[1]->e;
1823 atgt_bb = tgt_edge->dest;
1824 if (!tgt_bb)
1825 tgt_bb = atgt_bb;
1826 /* Two targets of threading would make us create loop
1827 with multiple entries. */
1828 else if (tgt_bb != atgt_bb)
1829 goto fail;
1832 if (!tgt_bb)
1834 /* There are no threading requests. */
1835 return false;
1838 /* Redirecting to empty loop latch is useless. */
1839 if (tgt_bb == loop->latch
1840 && empty_block_p (loop->latch))
1841 goto fail;
1844 /* The target block must dominate the loop latch, otherwise we would be
1845 creating a subloop. */
1846 domst = determine_bb_domination_status (loop, tgt_bb);
1847 if (domst == DOMST_NONDOMINATING)
1848 goto fail;
1849 if (domst == DOMST_LOOP_BROKEN)
1851 /* If the loop ceased to exist, mark it as such, and thread through its
1852 original header. */
1853 mark_loop_for_removal (loop);
1854 return thread_block (header, false);
1857 if (tgt_bb->loop_father->header == tgt_bb)
1859 /* If the target of the threading is a header of a subloop, we need
1860 to create a preheader for it, so that the headers of the two loops
1861 do not merge. */
1862 if (EDGE_COUNT (tgt_bb->preds) > 2)
1864 tgt_bb = create_preheader (tgt_bb->loop_father, 0);
1865 gcc_assert (tgt_bb != NULL);
1867 else
1868 tgt_bb = split_edge (tgt_edge);
1871 basic_block new_preheader;
1873 /* Now consider the case entry edges are redirected to the new entry
1874 block. Remember one entry edge, so that we can find the new
1875 preheader (its destination after threading). */
1876 FOR_EACH_EDGE (e, ei, header->preds)
1878 if (e->aux)
1879 break;
1882 /* The duplicate of the header is the new preheader of the loop. Ensure
1883 that it is placed correctly in the loop hierarchy. */
1884 set_loop_copy (loop, loop_outer (loop));
1886 thread_block (header, false);
1887 set_loop_copy (loop, NULL);
1888 new_preheader = e->dest;
1890 /* Create the new latch block. This is always necessary, as the latch
1891 must have only a single successor, but the original header had at
1892 least two successors. */
1893 loop->latch = NULL;
1894 mfb_kj_edge = single_succ_edge (new_preheader);
1895 loop->header = mfb_kj_edge->dest;
1896 latch = make_forwarder_block (tgt_bb, mfb_keep_just, NULL);
1897 loop->header = latch->dest;
1898 loop->latch = latch->src;
1899 return true;
1901 fail:
1902 /* We failed to thread anything. Cancel the requests. */
1903 FOR_EACH_EDGE (e, ei, header->preds)
1905 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1907 if (path)
1909 delete_jump_thread_path (path);
1910 e->aux = NULL;
1913 return false;
1916 /* E1 and E2 are edges into the same basic block. Return TRUE if the
1917 PHI arguments associated with those edges are equal or there are no
1918 PHI arguments, otherwise return FALSE. */
1920 static bool
1921 phi_args_equal_on_edges (edge e1, edge e2)
1923 gphi_iterator gsi;
1924 int indx1 = e1->dest_idx;
1925 int indx2 = e2->dest_idx;
1927 for (gsi = gsi_start_phis (e1->dest); !gsi_end_p (gsi); gsi_next (&gsi))
1929 gphi *phi = gsi.phi ();
1931 if (!operand_equal_p (gimple_phi_arg_def (phi, indx1),
1932 gimple_phi_arg_def (phi, indx2), 0))
1933 return false;
1935 return true;
1938 /* Walk through the registered jump threads and convert them into a
1939 form convenient for this pass.
1941 Any block which has incoming edges threaded to outgoing edges
1942 will have its entry in THREADED_BLOCK set.
1944 Any threaded edge will have its new outgoing edge stored in the
1945 original edge's AUX field.
1947 This form avoids the need to walk all the edges in the CFG to
1948 discover blocks which need processing and avoids unnecessary
1949 hash table lookups to map from threaded edge to new target. */
1951 static void
1952 mark_threaded_blocks (bitmap threaded_blocks)
1954 unsigned int i;
1955 bitmap_iterator bi;
1956 bitmap tmp = BITMAP_ALLOC (NULL);
1957 basic_block bb;
1958 edge e;
1959 edge_iterator ei;
1961 /* It is possible to have jump threads in which one is a subpath
1962 of the other. ie, (A, B), (B, C), (C, D) where B is a joiner
1963 block and (B, C), (C, D) where no joiner block exists.
1965 When this occurs ignore the jump thread request with the joiner
1966 block. It's totally subsumed by the simpler jump thread request.
1968 This results in less block copying, simpler CFGs. More importantly,
1969 when we duplicate the joiner block, B, in this case we will create
1970 a new threading opportunity that we wouldn't be able to optimize
1971 until the next jump threading iteration.
1973 So first convert the jump thread requests which do not require a
1974 joiner block. */
1975 for (i = 0; i < paths.length (); i++)
1977 vec<jump_thread_edge *> *path = paths[i];
1979 if ((*path)[1]->type != EDGE_COPY_SRC_JOINER_BLOCK)
1981 edge e = (*path)[0]->e;
1982 e->aux = (void *)path;
1983 bitmap_set_bit (tmp, e->dest->index);
1987 /* Now iterate again, converting cases where we want to thread
1988 through a joiner block, but only if no other edge on the path
1989 already has a jump thread attached to it. We do this in two passes,
1990 to avoid situations where the order in the paths vec can hide overlapping
1991 threads (the path is recorded on the incoming edge, so we would miss
1992 cases where the second path starts at a downstream edge on the same
1993 path). First record all joiner paths, deleting any in the unexpected
1994 case where there is already a path for that incoming edge. */
1995 for (i = 0; i < paths.length ();)
1997 vec<jump_thread_edge *> *path = paths[i];
1999 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
2001 /* Attach the path to the starting edge if none is yet recorded. */
2002 if ((*path)[0]->e->aux == NULL)
2004 (*path)[0]->e->aux = path;
2005 i++;
2007 else
2009 paths.unordered_remove (i);
2010 if (dump_file && (dump_flags & TDF_DETAILS))
2011 dump_jump_thread_path (dump_file, *path, false);
2012 delete_jump_thread_path (path);
2015 else
2017 i++;
2021 /* Second, look for paths that have any other jump thread attached to
2022 them, and either finish converting them or cancel them. */
2023 for (i = 0; i < paths.length ();)
2025 vec<jump_thread_edge *> *path = paths[i];
2026 edge e = (*path)[0]->e;
2028 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK && e->aux == path)
2030 unsigned int j;
2031 for (j = 1; j < path->length (); j++)
2032 if ((*path)[j]->e->aux != NULL)
2033 break;
2035 /* If we iterated through the entire path without exiting the loop,
2036 then we are good to go, record it. */
2037 if (j == path->length ())
2039 bitmap_set_bit (tmp, e->dest->index);
2040 i++;
2042 else
2044 e->aux = NULL;
2045 paths.unordered_remove (i);
2046 if (dump_file && (dump_flags & TDF_DETAILS))
2047 dump_jump_thread_path (dump_file, *path, false);
2048 delete_jump_thread_path (path);
2051 else
2053 i++;
2057 /* If optimizing for size, only thread through block if we don't have
2058 to duplicate it or it's an otherwise empty redirection block. */
2059 if (optimize_function_for_size_p (cfun))
2061 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
2063 bb = BASIC_BLOCK_FOR_FN (cfun, i);
2064 if (EDGE_COUNT (bb->preds) > 1
2065 && !redirection_block_p (bb))
2067 FOR_EACH_EDGE (e, ei, bb->preds)
2069 if (e->aux)
2071 vec<jump_thread_edge *> *path = THREAD_PATH (e);
2072 delete_jump_thread_path (path);
2073 e->aux = NULL;
2077 else
2078 bitmap_set_bit (threaded_blocks, i);
2081 else
2082 bitmap_copy (threaded_blocks, tmp);
2084 /* Look for jump threading paths which cross multiple loop headers.
2086 The code to thread through loop headers will change the CFG in ways
2087 that break assumptions made by the loop optimization code.
2089 We don't want to blindly cancel the requests. We can instead do better
2090 by trimming off the end of the jump thread path. */
2091 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
2093 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
2094 FOR_EACH_EDGE (e, ei, bb->preds)
2096 if (e->aux)
2098 vec<jump_thread_edge *> *path = THREAD_PATH (e);
2100 for (unsigned int i = 0, crossed_headers = 0;
2101 i < path->length ();
2102 i++)
2104 basic_block dest = (*path)[i]->e->dest;
2105 crossed_headers += (dest == dest->loop_father->header);
2106 if (crossed_headers > 1)
2108 /* Trim from entry I onwards. */
2109 for (unsigned int j = i; j < path->length (); j++)
2110 delete (*path)[j];
2111 path->truncate (i);
2113 /* Now that we've truncated the path, make sure
2114 what's left is still valid. We need at least
2115 two edges on the path and the last edge can not
2116 be a joiner. This should never happen, but let's
2117 be safe. */
2118 if (path->length () < 2
2119 || (path->last ()->type
2120 == EDGE_COPY_SRC_JOINER_BLOCK))
2122 delete_jump_thread_path (path);
2123 e->aux = NULL;
2125 break;
2132 /* If we have a joiner block (J) which has two successors S1 and S2 and
2133 we are threading though S1 and the final destination of the thread
2134 is S2, then we must verify that any PHI nodes in S2 have the same
2135 PHI arguments for the edge J->S2 and J->S1->...->S2.
2137 We used to detect this prior to registering the jump thread, but
2138 that prohibits propagation of edge equivalences into non-dominated
2139 PHI nodes as the equivalency test might occur before propagation.
2141 This must also occur after we truncate any jump threading paths
2142 as this scenario may only show up after truncation.
2144 This works for now, but will need improvement as part of the FSA
2145 optimization.
2147 Note since we've moved the thread request data to the edges,
2148 we have to iterate on those rather than the threaded_edges vector. */
2149 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
2151 bb = BASIC_BLOCK_FOR_FN (cfun, i);
2152 FOR_EACH_EDGE (e, ei, bb->preds)
2154 if (e->aux)
2156 vec<jump_thread_edge *> *path = THREAD_PATH (e);
2157 bool have_joiner = ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK);
2159 if (have_joiner)
2161 basic_block joiner = e->dest;
2162 edge final_edge = path->last ()->e;
2163 basic_block final_dest = final_edge->dest;
2164 edge e2 = find_edge (joiner, final_dest);
2166 if (e2 && !phi_args_equal_on_edges (e2, final_edge))
2168 delete_jump_thread_path (path);
2169 e->aux = NULL;
2176 BITMAP_FREE (tmp);
2180 /* Verify that the REGION is a valid jump thread. A jump thread is a special
2181 case of SEME Single Entry Multiple Exits region in which all nodes in the
2182 REGION have exactly one incoming edge. The only exception is the first block
2183 that may not have been connected to the rest of the cfg yet. */
2185 DEBUG_FUNCTION void
2186 verify_jump_thread (basic_block *region, unsigned n_region)
2188 for (unsigned i = 0; i < n_region; i++)
2189 gcc_assert (EDGE_COUNT (region[i]->preds) <= 1);
2192 /* Return true when BB is one of the first N items in BBS. */
2194 static inline bool
2195 bb_in_bbs (basic_block bb, basic_block *bbs, int n)
2197 for (int i = 0; i < n; i++)
2198 if (bb == bbs[i])
2199 return true;
2201 return false;
2204 /* Duplicates a jump-thread path of N_REGION basic blocks.
2205 The ENTRY edge is redirected to the duplicate of the region.
2207 Remove the last conditional statement in the last basic block in the REGION,
2208 and create a single fallthru edge pointing to the same destination as the
2209 EXIT edge.
2211 The new basic blocks are stored to REGION_COPY in the same order as they had
2212 in REGION, provided that REGION_COPY is not NULL.
2214 Returns false if it is unable to copy the region, true otherwise. */
2216 static bool
2217 duplicate_thread_path (edge entry, edge exit,
2218 basic_block *region, unsigned n_region,
2219 basic_block *region_copy)
2221 unsigned i;
2222 bool free_region_copy = false;
2223 struct loop *loop = entry->dest->loop_father;
2224 edge exit_copy;
2225 edge redirected;
2226 int total_freq = 0, entry_freq = 0;
2227 gcov_type total_count = 0, entry_count = 0;
2229 if (!can_copy_bbs_p (region, n_region))
2230 return false;
2232 /* Some sanity checking. Note that we do not check for all possible
2233 missuses of the functions. I.e. if you ask to copy something weird,
2234 it will work, but the state of structures probably will not be
2235 correct. */
2236 for (i = 0; i < n_region; i++)
2238 /* We do not handle subloops, i.e. all the blocks must belong to the
2239 same loop. */
2240 if (region[i]->loop_father != loop)
2241 return false;
2244 initialize_original_copy_tables ();
2246 set_loop_copy (loop, loop);
2248 if (!region_copy)
2250 region_copy = XNEWVEC (basic_block, n_region);
2251 free_region_copy = true;
2254 if (entry->dest->count)
2256 total_count = entry->dest->count;
2257 entry_count = entry->count;
2258 /* Fix up corner cases, to avoid division by zero or creation of negative
2259 frequencies. */
2260 if (entry_count > total_count)
2261 entry_count = total_count;
2263 else
2265 total_freq = entry->dest->frequency;
2266 entry_freq = EDGE_FREQUENCY (entry);
2267 /* Fix up corner cases, to avoid division by zero or creation of negative
2268 frequencies. */
2269 if (total_freq == 0)
2270 total_freq = 1;
2271 else if (entry_freq > total_freq)
2272 entry_freq = total_freq;
2275 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
2276 split_edge_bb_loc (entry), false);
2278 /* Fix up: copy_bbs redirects all edges pointing to copied blocks. The
2279 following code ensures that all the edges exiting the jump-thread path are
2280 redirected back to the original code: these edges are exceptions
2281 invalidating the property that is propagated by executing all the blocks of
2282 the jump-thread path in order. */
2284 for (i = 0; i < n_region; i++)
2286 edge e;
2287 edge_iterator ei;
2288 basic_block bb = region_copy[i];
2290 if (single_succ_p (bb))
2292 /* Make sure the successor is the next node in the path. */
2293 gcc_assert (i + 1 == n_region
2294 || region_copy[i + 1] == single_succ_edge (bb)->dest);
2295 continue;
2298 /* Special case the last block on the path: make sure that it does not
2299 jump back on the copied path, including back to itself. */
2300 if (i + 1 == n_region)
2302 FOR_EACH_EDGE (e, ei, bb->succs)
2303 if (bb_in_bbs (e->dest, region_copy, n_region))
2305 basic_block orig = get_bb_original (e->dest);
2306 if (orig)
2307 redirect_edge_and_branch_force (e, orig);
2309 continue;
2312 /* Redirect all other edges jumping to non-adjacent blocks back to the
2313 original code. */
2314 FOR_EACH_EDGE (e, ei, bb->succs)
2315 if (region_copy[i + 1] != e->dest)
2317 basic_block orig = get_bb_original (e->dest);
2318 if (orig)
2319 redirect_edge_and_branch_force (e, orig);
2323 if (total_count)
2325 scale_bbs_frequencies_gcov_type (region, n_region,
2326 total_count - entry_count,
2327 total_count);
2328 scale_bbs_frequencies_gcov_type (region_copy, n_region, entry_count,
2329 total_count);
2331 else
2333 scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
2334 total_freq);
2335 scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
2338 if (flag_checking)
2339 verify_jump_thread (region_copy, n_region);
2341 /* Remove the last branch in the jump thread path. */
2342 remove_ctrl_stmt_and_useless_edges (region_copy[n_region - 1], exit->dest);
2344 /* And fixup the flags on the single remaining edge. */
2345 edge fix_e = find_edge (region_copy[n_region - 1], exit->dest);
2346 fix_e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
2347 fix_e->flags |= EDGE_FALLTHRU;
2349 edge e = make_edge (region_copy[n_region - 1], exit->dest, EDGE_FALLTHRU);
2351 if (e) {
2352 rescan_loop_exit (e, true, false);
2353 e->probability = REG_BR_PROB_BASE;
2354 e->count = region_copy[n_region - 1]->count;
2357 /* Redirect the entry and add the phi node arguments. */
2358 if (entry->dest == loop->header)
2359 mark_loop_for_removal (loop);
2360 redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
2361 gcc_assert (redirected != NULL);
2362 flush_pending_stmts (entry);
2364 /* Add the other PHI node arguments. */
2365 add_phi_args_after_copy (region_copy, n_region, NULL);
2367 if (free_region_copy)
2368 free (region_copy);
2370 free_original_copy_tables ();
2371 return true;
2374 /* Return true when PATH is a valid jump-thread path. */
2376 static bool
2377 valid_jump_thread_path (vec<jump_thread_edge *> *path)
2379 unsigned len = path->length ();
2381 /* Check that the path is connected. */
2382 for (unsigned int j = 0; j < len - 1; j++)
2384 edge e = (*path)[j]->e;
2385 if (e->dest != (*path)[j+1]->e->src)
2386 return false;
2388 return true;
2391 /* Remove any queued jump threads that include edge E.
2393 We don't actually remove them here, just record the edges into ax
2394 hash table. That way we can do the search once per iteration of
2395 DOM/VRP rather than for every case where DOM optimizes away a COND_EXPR. */
2397 void
2398 remove_jump_threads_including (edge_def *e)
2400 if (!paths.exists ())
2401 return;
2403 if (!removed_edges)
2404 removed_edges = new hash_table<struct removed_edges> (17);
2406 edge *slot = removed_edges->find_slot (e, INSERT);
2407 *slot = e;
2410 /* Walk through all blocks and thread incoming edges to the appropriate
2411 outgoing edge for each edge pair recorded in THREADED_EDGES.
2413 It is the caller's responsibility to fix the dominance information
2414 and rewrite duplicated SSA_NAMEs back into SSA form.
2416 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading edges through
2417 loop headers if it does not simplify the loop.
2419 Returns true if one or more edges were threaded, false otherwise. */
2421 bool
2422 thread_through_all_blocks (bool may_peel_loop_headers)
2424 bool retval = false;
2425 unsigned int i;
2426 bitmap_iterator bi;
2427 bitmap threaded_blocks;
2428 struct loop *loop;
2430 if (!paths.exists ())
2432 retval = false;
2433 goto out;
2436 threaded_blocks = BITMAP_ALLOC (NULL);
2437 memset (&thread_stats, 0, sizeof (thread_stats));
2439 /* Remove any paths that referenced removed edges. */
2440 if (removed_edges)
2441 for (i = 0; i < paths.length (); )
2443 unsigned int j;
2444 vec<jump_thread_edge *> *path = paths[i];
2446 for (j = 0; j < path->length (); j++)
2448 edge e = (*path)[j]->e;
2449 if (removed_edges->find_slot (e, NO_INSERT))
2450 break;
2453 if (j != path->length ())
2455 delete_jump_thread_path (path);
2456 paths.unordered_remove (i);
2457 continue;
2459 i++;
2462 /* Jump-thread all FSM threads before other jump-threads. */
2463 for (i = 0; i < paths.length ();)
2465 vec<jump_thread_edge *> *path = paths[i];
2466 edge entry = (*path)[0]->e;
2468 /* Only code-generate FSM jump-threads in this loop. */
2469 if ((*path)[0]->type != EDGE_FSM_THREAD)
2471 i++;
2472 continue;
2475 /* Do not jump-thread twice from the same block. */
2476 if (bitmap_bit_p (threaded_blocks, entry->src->index)
2477 /* We may not want to realize this jump thread path
2478 for various reasons. So check it first. */
2479 || !valid_jump_thread_path (path))
2481 /* Remove invalid FSM jump-thread paths. */
2482 delete_jump_thread_path (path);
2483 paths.unordered_remove (i);
2484 continue;
2487 unsigned len = path->length ();
2488 edge exit = (*path)[len - 1]->e;
2489 basic_block *region = XNEWVEC (basic_block, len - 1);
2491 for (unsigned int j = 0; j < len - 1; j++)
2492 region[j] = (*path)[j]->e->dest;
2494 if (duplicate_thread_path (entry, exit, region, len - 1, NULL))
2496 /* We do not update dominance info. */
2497 free_dominance_info (CDI_DOMINATORS);
2498 bitmap_set_bit (threaded_blocks, entry->src->index);
2499 retval = true;
2500 thread_stats.num_threaded_edges++;
2503 delete_jump_thread_path (path);
2504 paths.unordered_remove (i);
2505 free (region);
2508 /* Remove from PATHS all the jump-threads starting with an edge already
2509 jump-threaded. */
2510 for (i = 0; i < paths.length ();)
2512 vec<jump_thread_edge *> *path = paths[i];
2513 edge entry = (*path)[0]->e;
2515 /* Do not jump-thread twice from the same block. */
2516 if (bitmap_bit_p (threaded_blocks, entry->src->index))
2518 delete_jump_thread_path (path);
2519 paths.unordered_remove (i);
2521 else
2522 i++;
2525 bitmap_clear (threaded_blocks);
2527 mark_threaded_blocks (threaded_blocks);
2529 initialize_original_copy_tables ();
2531 /* First perform the threading requests that do not affect
2532 loop structure. */
2533 EXECUTE_IF_SET_IN_BITMAP (threaded_blocks, 0, i, bi)
2535 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
2537 if (EDGE_COUNT (bb->preds) > 0)
2538 retval |= thread_block (bb, true);
2541 /* Then perform the threading through loop headers. We start with the
2542 innermost loop, so that the changes in cfg we perform won't affect
2543 further threading. */
2544 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
2546 if (!loop->header
2547 || !bitmap_bit_p (threaded_blocks, loop->header->index))
2548 continue;
2550 retval |= thread_through_loop_header (loop, may_peel_loop_headers);
2553 /* All jump threading paths should have been resolved at this
2554 point. Verify that is the case. */
2555 basic_block bb;
2556 FOR_EACH_BB_FN (bb, cfun)
2558 edge_iterator ei;
2559 edge e;
2560 FOR_EACH_EDGE (e, ei, bb->preds)
2561 gcc_assert (e->aux == NULL);
2564 statistics_counter_event (cfun, "Jumps threaded",
2565 thread_stats.num_threaded_edges);
2567 free_original_copy_tables ();
2569 BITMAP_FREE (threaded_blocks);
2570 threaded_blocks = NULL;
2571 paths.release ();
2573 if (retval)
2574 loops_state_set (LOOPS_NEED_FIXUP);
2576 out:
2577 delete removed_edges;
2578 removed_edges = NULL;
2579 return retval;
2582 /* Delete the jump threading path PATH. We have to explcitly delete
2583 each entry in the vector, then the container. */
2585 void
2586 delete_jump_thread_path (vec<jump_thread_edge *> *path)
2588 for (unsigned int i = 0; i < path->length (); i++)
2589 delete (*path)[i];
2590 path->release();
2591 delete path;
2594 /* Register a jump threading opportunity. We queue up all the jump
2595 threading opportunities discovered by a pass and update the CFG
2596 and SSA form all at once.
2598 E is the edge we can thread, E2 is the new target edge, i.e., we
2599 are effectively recording that E->dest can be changed to E2->dest
2600 after fixing the SSA graph. */
2602 void
2603 register_jump_thread (vec<jump_thread_edge *> *path)
2605 if (!dbg_cnt (registered_jump_thread))
2607 delete_jump_thread_path (path);
2608 return;
2611 /* First make sure there are no NULL outgoing edges on the jump threading
2612 path. That can happen for jumping to a constant address. */
2613 for (unsigned int i = 0; i < path->length (); i++)
2615 if ((*path)[i]->e == NULL)
2617 if (dump_file && (dump_flags & TDF_DETAILS))
2619 fprintf (dump_file,
2620 "Found NULL edge in jump threading path. Cancelling jump thread:\n");
2621 dump_jump_thread_path (dump_file, *path, false);
2624 delete_jump_thread_path (path);
2625 return;
2628 /* Only the FSM threader is allowed to thread across
2629 backedges in the CFG. */
2630 if (flag_checking
2631 && (*path)[0]->type != EDGE_FSM_THREAD)
2632 gcc_assert (((*path)[i]->e->flags & EDGE_DFS_BACK) == 0);
2635 if (dump_file && (dump_flags & TDF_DETAILS))
2636 dump_jump_thread_path (dump_file, *path, true);
2638 if (!paths.exists ())
2639 paths.create (5);
2641 paths.safe_push (path);