Default to dwarf version 4 on hppa64-hpux
[official-gcc.git] / gcc / tree-ssa-threadupdate.c
blobdcabfdb30d2c0dfee702463713cb11fcdc3defa3
1 /* Thread edges through blocks and update the control flow and SSA graphs.
2 Copyright (C) 2004-2021 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"
37 #include "tree-vectorizer.h"
38 #include "tree-pass.h"
40 /* Given a block B, update the CFG and SSA graph to reflect redirecting
41 one or more in-edges to B to instead reach the destination of an
42 out-edge from B while preserving any side effects in B.
44 i.e., given A->B and B->C, change A->B to be A->C yet still preserve the
45 side effects of executing B.
47 1. Make a copy of B (including its outgoing edges and statements). Call
48 the copy B'. Note B' has no incoming edges or PHIs at this time.
50 2. Remove the control statement at the end of B' and all outgoing edges
51 except B'->C.
53 3. Add a new argument to each PHI in C with the same value as the existing
54 argument associated with edge B->C. Associate the new PHI arguments
55 with the edge B'->C.
57 4. For each PHI in B, find or create a PHI in B' with an identical
58 PHI_RESULT. Add an argument to the PHI in B' which has the same
59 value as the PHI in B associated with the edge A->B. Associate
60 the new argument in the PHI in B' with the edge A->B.
62 5. Change the edge A->B to A->B'.
64 5a. This automatically deletes any PHI arguments associated with the
65 edge A->B in B.
67 5b. This automatically associates each new argument added in step 4
68 with the edge A->B'.
70 6. Repeat for other incoming edges into B.
72 7. Put the duplicated resources in B and all the B' blocks into SSA form.
74 Note that block duplication can be minimized by first collecting the
75 set of unique destination blocks that the incoming edges should
76 be threaded to.
78 We reduce the number of edges and statements we create by not copying all
79 the outgoing edges and the control statement in step #1. We instead create
80 a template block without the outgoing edges and duplicate the template.
82 Another case this code handles is threading through a "joiner" block. In
83 this case, we do not know the destination of the joiner block, but one
84 of the outgoing edges from the joiner block leads to a threadable path. This
85 case largely works as outlined above, except the duplicate of the joiner
86 block still contains a full set of outgoing edges and its control statement.
87 We just redirect one of its outgoing edges to our jump threading path. */
90 /* Steps #5 and #6 of the above algorithm are best implemented by walking
91 all the incoming edges which thread to the same destination edge at
92 the same time. That avoids lots of table lookups to get information
93 for the destination edge.
95 To realize that implementation we create a list of incoming edges
96 which thread to the same outgoing edge. Thus to implement steps
97 #5 and #6 we traverse our hash table of outgoing edge information.
98 For each entry we walk the list of incoming edges which thread to
99 the current outgoing edge. */
101 struct el
103 edge e;
104 struct el *next;
107 /* Main data structure recording information regarding B's duplicate
108 blocks. */
110 /* We need to efficiently record the unique thread destinations of this
111 block and specific information associated with those destinations. We
112 may have many incoming edges threaded to the same outgoing edge. This
113 can be naturally implemented with a hash table. */
115 struct redirection_data : free_ptr_hash<redirection_data>
117 /* We support wiring up two block duplicates in a jump threading path.
119 One is a normal block copy where we remove the control statement
120 and wire up its single remaining outgoing edge to the thread path.
122 The other is a joiner block where we leave the control statement
123 in place, but wire one of the outgoing edges to a thread path.
125 In theory we could have multiple block duplicates in a jump
126 threading path, but I haven't tried that.
128 The duplicate blocks appear in this array in the same order in
129 which they appear in the jump thread path. */
130 basic_block dup_blocks[2];
132 vec<jump_thread_edge *> *path;
134 /* A list of incoming edges which we want to thread to the
135 same path. */
136 struct el *incoming_edges;
138 /* hash_table support. */
139 static inline hashval_t hash (const redirection_data *);
140 static inline int equal (const redirection_data *, const redirection_data *);
143 jump_thread_path_allocator::jump_thread_path_allocator ()
145 obstack_init (&m_obstack);
148 jump_thread_path_allocator::~jump_thread_path_allocator ()
150 obstack_free (&m_obstack, NULL);
153 jump_thread_edge *
154 jump_thread_path_allocator::allocate_thread_edge (edge e,
155 jump_thread_edge_type type)
157 void *r = obstack_alloc (&m_obstack, sizeof (jump_thread_edge));
158 return new (r) jump_thread_edge (e, type);
161 vec<jump_thread_edge *> *
162 jump_thread_path_allocator::allocate_thread_path ()
164 // ?? Since the paths live in an obstack, we should be able to remove all
165 // references to path->release() throughout the code.
166 void *r = obstack_alloc (&m_obstack, sizeof (vec <jump_thread_edge *>));
167 return new (r) vec<jump_thread_edge *> ();
170 jt_path_registry::jt_path_registry (bool backedge_threads)
172 m_paths.create (5);
173 m_num_threaded_edges = 0;
174 m_backedge_threads = backedge_threads;
177 jt_path_registry::~jt_path_registry ()
179 m_paths.release ();
182 fwd_jt_path_registry::fwd_jt_path_registry ()
183 : jt_path_registry (/*backedge_threads=*/false)
185 m_removed_edges = new hash_table<struct removed_edges> (17);
186 m_redirection_data = NULL;
189 fwd_jt_path_registry::~fwd_jt_path_registry ()
191 delete m_removed_edges;
194 back_jt_path_registry::back_jt_path_registry ()
195 : jt_path_registry (/*backedge_threads=*/true)
199 void
200 jt_path_registry::push_edge (vec<jump_thread_edge *> *path,
201 edge e, jump_thread_edge_type type)
203 jump_thread_edge *x = m_allocator.allocate_thread_edge (e, type);
204 path->safe_push (x);
207 vec<jump_thread_edge *> *
208 jt_path_registry::allocate_thread_path ()
210 return m_allocator.allocate_thread_path ();
213 /* Dump a jump threading path, including annotations about each
214 edge in the path. */
216 static void
217 dump_jump_thread_path (FILE *dump_file,
218 const vec<jump_thread_edge *> &path,
219 bool registering)
221 if (registering)
222 fprintf (dump_file,
223 " [%u] Registering jump thread: (%d, %d) incoming edge; ",
224 dbg_cnt_counter (registered_jump_thread),
225 path[0]->e->src->index, path[0]->e->dest->index);
226 else
227 fprintf (dump_file,
228 " Cancelling jump thread: (%d, %d) incoming edge; ",
229 path[0]->e->src->index, path[0]->e->dest->index);
231 for (unsigned int i = 1; i < path.length (); i++)
233 /* We can get paths with a NULL edge when the final destination
234 of a jump thread turns out to be a constant address. We dump
235 those paths when debugging, so we have to be prepared for that
236 possibility here. */
237 if (path[i]->e == NULL)
238 continue;
240 fprintf (dump_file, " (%d, %d) ",
241 path[i]->e->src->index, path[i]->e->dest->index);
242 switch (path[i]->type)
244 case EDGE_COPY_SRC_JOINER_BLOCK:
245 fprintf (dump_file, "joiner");
246 break;
247 case EDGE_COPY_SRC_BLOCK:
248 fprintf (dump_file, "normal");
249 break;
250 case EDGE_NO_COPY_SRC_BLOCK:
251 fprintf (dump_file, "nocopy");
252 break;
253 default:
254 gcc_unreachable ();
257 fprintf (dump_file, "; \n");
260 DEBUG_FUNCTION void
261 debug (const vec<jump_thread_edge *> &path)
263 dump_jump_thread_path (stderr, path, true);
266 DEBUG_FUNCTION void
267 debug (const vec<jump_thread_edge *> *path)
269 debug (*path);
272 /* Release the memory associated with PATH, and if dumping is enabled,
273 dump out the reason why the thread was canceled. */
275 static void
276 cancel_thread (vec<jump_thread_edge *> *path, const char *reason = NULL)
278 if (dump_file && (dump_flags & TDF_DETAILS))
280 if (reason)
281 fprintf (dump_file, "%s:\n", reason);
283 dump_jump_thread_path (dump_file, *path, false);
284 fprintf (dump_file, "\n");
286 path->release ();
289 /* Simple hashing function. For any given incoming edge E, we're going
290 to be most concerned with the final destination of its jump thread
291 path. So hash on the block index of the final edge in the path. */
293 inline hashval_t
294 redirection_data::hash (const redirection_data *p)
296 vec<jump_thread_edge *> *path = p->path;
297 return path->last ()->e->dest->index;
300 /* Given two hash table entries, return true if they have the same
301 jump threading path. */
302 inline int
303 redirection_data::equal (const redirection_data *p1, const redirection_data *p2)
305 vec<jump_thread_edge *> *path1 = p1->path;
306 vec<jump_thread_edge *> *path2 = p2->path;
308 if (path1->length () != path2->length ())
309 return false;
311 for (unsigned int i = 1; i < path1->length (); i++)
313 if ((*path1)[i]->type != (*path2)[i]->type
314 || (*path1)[i]->e != (*path2)[i]->e)
315 return false;
318 return true;
321 /* Data structure of information to pass to hash table traversal routines. */
322 struct ssa_local_info_t
324 /* The current block we are working on. */
325 basic_block bb;
327 /* We only create a template block for the first duplicated block in a
328 jump threading path as we may need many duplicates of that block.
330 The second duplicate block in a path is specific to that path. Creating
331 and sharing a template for that block is considerably more difficult. */
332 basic_block template_block;
334 /* If we append debug stmts to the template block after creating it,
335 this iterator won't be the last one in the block, and further
336 copies of the template block shouldn't get debug stmts after
337 it. */
338 gimple_stmt_iterator template_last_to_copy;
340 /* Blocks duplicated for the thread. */
341 bitmap duplicate_blocks;
343 /* TRUE if we thread one or more jumps, FALSE otherwise. */
344 bool jumps_threaded;
346 /* When we have multiple paths through a joiner which reach different
347 final destinations, then we may need to correct for potential
348 profile insanities. */
349 bool need_profile_correction;
351 // Jump threading statistics.
352 unsigned long num_threaded_edges;
355 /* When we start updating the CFG for threading, data necessary for jump
356 threading is attached to the AUX field for the incoming edge. Use these
357 macros to access the underlying structure attached to the AUX field. */
358 #define THREAD_PATH(E) ((vec<jump_thread_edge *> *)(E)->aux)
360 /* Remove the last statement in block BB if it is a control statement
361 Also remove all outgoing edges except the edge which reaches DEST_BB.
362 If DEST_BB is NULL, then remove all outgoing edges. */
364 static void
365 remove_ctrl_stmt_and_useless_edges (basic_block bb, basic_block dest_bb)
367 gimple_stmt_iterator gsi;
368 edge e;
369 edge_iterator ei;
371 gsi = gsi_last_bb (bb);
373 /* If the duplicate ends with a control statement, then remove it.
375 Note that if we are duplicating the template block rather than the
376 original basic block, then the duplicate might not have any real
377 statements in it. */
378 if (!gsi_end_p (gsi)
379 && gsi_stmt (gsi)
380 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
381 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
382 || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH))
383 gsi_remove (&gsi, true);
385 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
387 if (e->dest != dest_bb)
389 free_dom_edge_info (e);
390 remove_edge (e);
392 else
394 e->probability = profile_probability::always ();
395 ei_next (&ei);
399 /* If the remaining edge is a loop exit, there must have
400 a removed edge that was not a loop exit.
402 In that case BB and possibly other blocks were previously
403 in the loop, but are now outside the loop. Thus, we need
404 to update the loop structures. */
405 if (single_succ_p (bb)
406 && loop_outer (bb->loop_father)
407 && loop_exit_edge_p (bb->loop_father, single_succ_edge (bb)))
408 loops_state_set (LOOPS_NEED_FIXUP);
411 /* Create a duplicate of BB. Record the duplicate block in an array
412 indexed by COUNT stored in RD. */
414 static void
415 create_block_for_threading (basic_block bb,
416 struct redirection_data *rd,
417 unsigned int count,
418 bitmap *duplicate_blocks)
420 edge_iterator ei;
421 edge e;
423 /* We can use the generic block duplication code and simply remove
424 the stuff we do not need. */
425 rd->dup_blocks[count] = duplicate_block (bb, NULL, NULL);
427 FOR_EACH_EDGE (e, ei, rd->dup_blocks[count]->succs)
429 e->aux = NULL;
431 /* If we duplicate a block with an outgoing edge marked as
432 EDGE_IGNORE, we must clear EDGE_IGNORE so that it doesn't
433 leak out of the current pass.
435 It would be better to simplify switch statements and remove
436 the edges before we get here, but the sequencing is nontrivial. */
437 e->flags &= ~EDGE_IGNORE;
440 /* Zero out the profile, since the block is unreachable for now. */
441 rd->dup_blocks[count]->count = profile_count::uninitialized ();
442 if (duplicate_blocks)
443 bitmap_set_bit (*duplicate_blocks, rd->dup_blocks[count]->index);
446 /* Given an outgoing edge E lookup and return its entry in our hash table.
448 If INSERT is true, then we insert the entry into the hash table if
449 it is not already present. INCOMING_EDGE is added to the list of incoming
450 edges associated with E in the hash table. */
452 redirection_data *
453 fwd_jt_path_registry::lookup_redirection_data (edge e, insert_option insert)
455 struct redirection_data **slot;
456 struct redirection_data *elt;
457 vec<jump_thread_edge *> *path = THREAD_PATH (e);
459 /* Build a hash table element so we can see if E is already
460 in the table. */
461 elt = XNEW (struct redirection_data);
462 elt->path = path;
463 elt->dup_blocks[0] = NULL;
464 elt->dup_blocks[1] = NULL;
465 elt->incoming_edges = NULL;
467 slot = m_redirection_data->find_slot (elt, insert);
469 /* This will only happen if INSERT is false and the entry is not
470 in the hash table. */
471 if (slot == NULL)
473 free (elt);
474 return NULL;
477 /* This will only happen if E was not in the hash table and
478 INSERT is true. */
479 if (*slot == NULL)
481 *slot = elt;
482 elt->incoming_edges = XNEW (struct el);
483 elt->incoming_edges->e = e;
484 elt->incoming_edges->next = NULL;
485 return elt;
487 /* E was in the hash table. */
488 else
490 /* Free ELT as we do not need it anymore, we will extract the
491 relevant entry from the hash table itself. */
492 free (elt);
494 /* Get the entry stored in the hash table. */
495 elt = *slot;
497 /* If insertion was requested, then we need to add INCOMING_EDGE
498 to the list of incoming edges associated with E. */
499 if (insert)
501 struct el *el = XNEW (struct el);
502 el->next = elt->incoming_edges;
503 el->e = e;
504 elt->incoming_edges = el;
507 return elt;
511 /* Similar to copy_phi_args, except that the PHI arg exists, it just
512 does not have a value associated with it. */
514 static void
515 copy_phi_arg_into_existing_phi (edge src_e, edge tgt_e)
517 int src_idx = src_e->dest_idx;
518 int tgt_idx = tgt_e->dest_idx;
520 /* Iterate over each PHI in e->dest. */
521 for (gphi_iterator gsi = gsi_start_phis (src_e->dest),
522 gsi2 = gsi_start_phis (tgt_e->dest);
523 !gsi_end_p (gsi);
524 gsi_next (&gsi), gsi_next (&gsi2))
526 gphi *src_phi = gsi.phi ();
527 gphi *dest_phi = gsi2.phi ();
528 tree val = gimple_phi_arg_def (src_phi, src_idx);
529 location_t locus = gimple_phi_arg_location (src_phi, src_idx);
531 SET_PHI_ARG_DEF (dest_phi, tgt_idx, val);
532 gimple_phi_arg_set_location (dest_phi, tgt_idx, locus);
536 /* Given ssa_name DEF, backtrack jump threading PATH from node IDX
537 to see if it has constant value in a flow sensitive manner. Set
538 LOCUS to location of the constant phi arg and return the value.
539 Return DEF directly if either PATH or idx is ZERO. */
541 static tree
542 get_value_locus_in_path (tree def, vec<jump_thread_edge *> *path,
543 basic_block bb, int idx, location_t *locus)
545 tree arg;
546 gphi *def_phi;
547 basic_block def_bb;
549 if (path == NULL || idx == 0)
550 return def;
552 def_phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (def));
553 if (!def_phi)
554 return def;
556 def_bb = gimple_bb (def_phi);
557 /* Don't propagate loop invariants into deeper loops. */
558 if (!def_bb || bb_loop_depth (def_bb) < bb_loop_depth (bb))
559 return def;
561 /* Backtrack jump threading path from IDX to see if def has constant
562 value. */
563 for (int j = idx - 1; j >= 0; j--)
565 edge e = (*path)[j]->e;
566 if (e->dest == def_bb)
568 arg = gimple_phi_arg_def (def_phi, e->dest_idx);
569 if (is_gimple_min_invariant (arg))
571 *locus = gimple_phi_arg_location (def_phi, e->dest_idx);
572 return arg;
574 break;
578 return def;
581 /* For each PHI in BB, copy the argument associated with SRC_E to TGT_E.
582 Try to backtrack jump threading PATH from node IDX to see if the arg
583 has constant value, copy constant value instead of argument itself
584 if yes. */
586 static void
587 copy_phi_args (basic_block bb, edge src_e, edge tgt_e,
588 vec<jump_thread_edge *> *path, int idx)
590 gphi_iterator gsi;
591 int src_indx = src_e->dest_idx;
593 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
595 gphi *phi = gsi.phi ();
596 tree def = gimple_phi_arg_def (phi, src_indx);
597 location_t locus = gimple_phi_arg_location (phi, src_indx);
599 if (TREE_CODE (def) == SSA_NAME
600 && !virtual_operand_p (gimple_phi_result (phi)))
601 def = get_value_locus_in_path (def, path, bb, idx, &locus);
603 add_phi_arg (phi, def, tgt_e, locus);
607 /* We have recently made a copy of ORIG_BB, including its outgoing
608 edges. The copy is NEW_BB. Every PHI node in every direct successor of
609 ORIG_BB has a new argument associated with edge from NEW_BB to the
610 successor. Initialize the PHI argument so that it is equal to the PHI
611 argument associated with the edge from ORIG_BB to the successor.
612 PATH and IDX are used to check if the new PHI argument has constant
613 value in a flow sensitive manner. */
615 static void
616 update_destination_phis (basic_block orig_bb, basic_block new_bb,
617 vec<jump_thread_edge *> *path, int idx)
619 edge_iterator ei;
620 edge e;
622 FOR_EACH_EDGE (e, ei, orig_bb->succs)
624 edge e2 = find_edge (new_bb, e->dest);
625 copy_phi_args (e->dest, e, e2, path, idx);
629 /* Given a duplicate block and its single destination (both stored
630 in RD). Create an edge between the duplicate and its single
631 destination.
633 Add an additional argument to any PHI nodes at the single
634 destination. IDX is the start node in jump threading path
635 we start to check to see if the new PHI argument has constant
636 value along the jump threading path. */
638 static void
639 create_edge_and_update_destination_phis (struct redirection_data *rd,
640 basic_block bb, int idx)
642 edge e = make_single_succ_edge (bb, rd->path->last ()->e->dest, EDGE_FALLTHRU);
644 rescan_loop_exit (e, true, false);
646 /* We used to copy the thread path here. That was added in 2007
647 and dutifully updated through the representation changes in 2013.
649 In 2013 we added code to thread from an interior node through
650 the backedge to another interior node. That runs after the code
651 to thread through loop headers from outside the loop.
653 The latter may delete edges in the CFG, including those
654 which appeared in the jump threading path we copied here. Thus
655 we'd end up using a dangling pointer.
657 After reviewing the 2007/2011 code, I can't see how anything
658 depended on copying the AUX field and clearly copying the jump
659 threading path is problematical due to embedded edge pointers.
660 It has been removed. */
661 e->aux = NULL;
663 /* If there are any PHI nodes at the destination of the outgoing edge
664 from the duplicate block, then we will need to add a new argument
665 to them. The argument should have the same value as the argument
666 associated with the outgoing edge stored in RD. */
667 copy_phi_args (e->dest, rd->path->last ()->e, e, rd->path, idx);
670 /* Look through PATH beginning at START and return TRUE if there are
671 any additional blocks that need to be duplicated. Otherwise,
672 return FALSE. */
673 static bool
674 any_remaining_duplicated_blocks (vec<jump_thread_edge *> *path,
675 unsigned int start)
677 for (unsigned int i = start + 1; i < path->length (); i++)
679 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK
680 || (*path)[i]->type == EDGE_COPY_SRC_BLOCK)
681 return true;
683 return false;
687 /* Compute the amount of profile count coming into the jump threading
688 path stored in RD that we are duplicating, returned in PATH_IN_COUNT_PTR and
689 PATH_IN_FREQ_PTR, as well as the amount of counts flowing out of the
690 duplicated path, returned in PATH_OUT_COUNT_PTR. LOCAL_INFO is used to
691 identify blocks duplicated for jump threading, which have duplicated
692 edges that need to be ignored in the analysis. Return true if path contains
693 a joiner, false otherwise.
695 In the non-joiner case, this is straightforward - all the counts
696 flowing into the jump threading path should flow through the duplicated
697 block and out of the duplicated path.
699 In the joiner case, it is very tricky. Some of the counts flowing into
700 the original path go offpath at the joiner. The problem is that while
701 we know how much total count goes off-path in the original control flow,
702 we don't know how many of the counts corresponding to just the jump
703 threading path go offpath at the joiner.
705 For example, assume we have the following control flow and identified
706 jump threading paths:
708 A B C
709 \ | /
710 Ea \ |Eb / Ec
711 \ | /
712 v v v
713 J <-- Joiner
715 Eoff/ \Eon
718 Soff Son <--- Normal
720 Ed/ \ Ee
725 Jump threading paths: A -> J -> Son -> D (path 1)
726 C -> J -> Son -> E (path 2)
728 Note that the control flow could be more complicated:
729 - Each jump threading path may have more than one incoming edge. I.e. A and
730 Ea could represent multiple incoming blocks/edges that are included in
731 path 1.
732 - There could be EDGE_NO_COPY_SRC_BLOCK edges after the joiner (either
733 before or after the "normal" copy block). These are not duplicated onto
734 the jump threading path, as they are single-successor.
735 - Any of the blocks along the path may have other incoming edges that
736 are not part of any jump threading path, but add profile counts along
737 the path.
739 In the above example, after all jump threading is complete, we will
740 end up with the following control flow:
742 A B C
743 | | |
744 Ea| |Eb |Ec
745 | | |
746 v v v
747 Ja J Jc
748 / \ / \Eon' / \
749 Eona/ \ ---/---\-------- \Eonc
750 / \ / / \ \
751 v v v v v
752 Sona Soff Son Sonc
753 \ /\ /
754 \___________ / \ _____/
755 \ / \/
756 vv v
759 The main issue to notice here is that when we are processing path 1
760 (A->J->Son->D) we need to figure out the outgoing edge weights to
761 the duplicated edges Ja->Sona and Ja->Soff, while ensuring that the
762 sum of the incoming weights to D remain Ed. The problem with simply
763 assuming that Ja (and Jc when processing path 2) has the same outgoing
764 probabilities to its successors as the original block J, is that after
765 all paths are processed and other edges/counts removed (e.g. none
766 of Ec will reach D after processing path 2), we may end up with not
767 enough count flowing along duplicated edge Sona->D.
769 Therefore, in the case of a joiner, we keep track of all counts
770 coming in along the current path, as well as from predecessors not
771 on any jump threading path (Eb in the above example). While we
772 first assume that the duplicated Eona for Ja->Sona has the same
773 probability as the original, we later compensate for other jump
774 threading paths that may eliminate edges. We do that by keep track
775 of all counts coming into the original path that are not in a jump
776 thread (Eb in the above example, but as noted earlier, there could
777 be other predecessors incoming to the path at various points, such
778 as at Son). Call this cumulative non-path count coming into the path
779 before D as Enonpath. We then ensure that the count from Sona->D is as at
780 least as big as (Ed - Enonpath), but no bigger than the minimum
781 weight along the jump threading path. The probabilities of both the
782 original and duplicated joiner block J and Ja will be adjusted
783 accordingly after the updates. */
785 static bool
786 compute_path_counts (struct redirection_data *rd,
787 ssa_local_info_t *local_info,
788 profile_count *path_in_count_ptr,
789 profile_count *path_out_count_ptr)
791 edge e = rd->incoming_edges->e;
792 vec<jump_thread_edge *> *path = THREAD_PATH (e);
793 edge elast = path->last ()->e;
794 profile_count nonpath_count = profile_count::zero ();
795 bool has_joiner = false;
796 profile_count path_in_count = profile_count::zero ();
798 /* Start by accumulating incoming edge counts to the path's first bb
799 into a couple buckets:
800 path_in_count: total count of incoming edges that flow into the
801 current path.
802 nonpath_count: total count of incoming edges that are not
803 flowing along *any* path. These are the counts
804 that will still flow along the original path after
805 all path duplication is done by potentially multiple
806 calls to this routine.
807 (any other incoming edge counts are for a different jump threading
808 path that will be handled by a later call to this routine.)
809 To make this easier, start by recording all incoming edges that flow into
810 the current path in a bitmap. We could add up the path's incoming edge
811 counts here, but we still need to walk all the first bb's incoming edges
812 below to add up the counts of the other edges not included in this jump
813 threading path. */
814 struct el *next, *el;
815 auto_bitmap in_edge_srcs;
816 for (el = rd->incoming_edges; el; el = next)
818 next = el->next;
819 bitmap_set_bit (in_edge_srcs, el->e->src->index);
821 edge ein;
822 edge_iterator ei;
823 FOR_EACH_EDGE (ein, ei, e->dest->preds)
825 vec<jump_thread_edge *> *ein_path = THREAD_PATH (ein);
826 /* Simply check the incoming edge src against the set captured above. */
827 if (ein_path
828 && bitmap_bit_p (in_edge_srcs, (*ein_path)[0]->e->src->index))
830 /* It is necessary but not sufficient that the last path edges
831 are identical. There may be different paths that share the
832 same last path edge in the case where the last edge has a nocopy
833 source block. */
834 gcc_assert (ein_path->last ()->e == elast);
835 path_in_count += ein->count ();
837 else if (!ein_path)
839 /* Keep track of the incoming edges that are not on any jump-threading
840 path. These counts will still flow out of original path after all
841 jump threading is complete. */
842 nonpath_count += ein->count ();
846 /* Now compute the fraction of the total count coming into the first
847 path bb that is from the current threading path. */
848 profile_count total_count = e->dest->count;
849 /* Handle incoming profile insanities. */
850 if (total_count < path_in_count)
851 path_in_count = total_count;
852 profile_probability onpath_scale = path_in_count.probability_in (total_count);
854 /* Walk the entire path to do some more computation in order to estimate
855 how much of the path_in_count will flow out of the duplicated threading
856 path. In the non-joiner case this is straightforward (it should be
857 the same as path_in_count, although we will handle incoming profile
858 insanities by setting it equal to the minimum count along the path).
860 In the joiner case, we need to estimate how much of the path_in_count
861 will stay on the threading path after the joiner's conditional branch.
862 We don't really know for sure how much of the counts
863 associated with this path go to each successor of the joiner, but we'll
864 estimate based on the fraction of the total count coming into the path
865 bb was from the threading paths (computed above in onpath_scale).
866 Afterwards, we will need to do some fixup to account for other threading
867 paths and possible profile insanities.
869 In order to estimate the joiner case's counts we also need to update
870 nonpath_count with any additional counts coming into the path. Other
871 blocks along the path may have additional predecessors from outside
872 the path. */
873 profile_count path_out_count = path_in_count;
874 profile_count min_path_count = path_in_count;
875 for (unsigned int i = 1; i < path->length (); i++)
877 edge epath = (*path)[i]->e;
878 profile_count cur_count = epath->count ();
879 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
881 has_joiner = true;
882 cur_count = cur_count.apply_probability (onpath_scale);
884 /* In the joiner case we need to update nonpath_count for any edges
885 coming into the path that will contribute to the count flowing
886 into the path successor. */
887 if (has_joiner && epath != elast)
889 /* Look for other incoming edges after joiner. */
890 FOR_EACH_EDGE (ein, ei, epath->dest->preds)
892 if (ein != epath
893 /* Ignore in edges from blocks we have duplicated for a
894 threading path, which have duplicated edge counts until
895 they are redirected by an invocation of this routine. */
896 && !bitmap_bit_p (local_info->duplicate_blocks,
897 ein->src->index))
898 nonpath_count += ein->count ();
901 if (cur_count < path_out_count)
902 path_out_count = cur_count;
903 if (epath->count () < min_path_count)
904 min_path_count = epath->count ();
907 /* We computed path_out_count above assuming that this path targeted
908 the joiner's on-path successor with the same likelihood as it
909 reached the joiner. However, other thread paths through the joiner
910 may take a different path through the normal copy source block
911 (i.e. they have a different elast), meaning that they do not
912 contribute any counts to this path's elast. As a result, it may
913 turn out that this path must have more count flowing to the on-path
914 successor of the joiner. Essentially, all of this path's elast
915 count must be contributed by this path and any nonpath counts
916 (since any path through the joiner with a different elast will not
917 include a copy of this elast in its duplicated path).
918 So ensure that this path's path_out_count is at least the
919 difference between elast->count () and nonpath_count. Otherwise the edge
920 counts after threading will not be sane. */
921 if (local_info->need_profile_correction
922 && has_joiner && path_out_count < elast->count () - nonpath_count)
924 path_out_count = elast->count () - nonpath_count;
925 /* But neither can we go above the minimum count along the path
926 we are duplicating. This can be an issue due to profile
927 insanities coming in to this pass. */
928 if (path_out_count > min_path_count)
929 path_out_count = min_path_count;
932 *path_in_count_ptr = path_in_count;
933 *path_out_count_ptr = path_out_count;
934 return has_joiner;
938 /* Update the counts and frequencies for both an original path
939 edge EPATH and its duplicate EDUP. The duplicate source block
940 will get a count of PATH_IN_COUNT and PATH_IN_FREQ,
941 and the duplicate edge EDUP will have a count of PATH_OUT_COUNT. */
942 static void
943 update_profile (edge epath, edge edup, profile_count path_in_count,
944 profile_count path_out_count)
947 /* First update the duplicated block's count. */
948 if (edup)
950 basic_block dup_block = edup->src;
952 /* Edup's count is reduced by path_out_count. We need to redistribute
953 probabilities to the remaining edges. */
955 edge esucc;
956 edge_iterator ei;
957 profile_probability edup_prob
958 = path_out_count.probability_in (path_in_count);
960 /* Either scale up or down the remaining edges.
961 probabilities are always in range <0,1> and thus we can't do
962 both by same loop. */
963 if (edup->probability > edup_prob)
965 profile_probability rev_scale
966 = (profile_probability::always () - edup->probability)
967 / (profile_probability::always () - edup_prob);
968 FOR_EACH_EDGE (esucc, ei, dup_block->succs)
969 if (esucc != edup)
970 esucc->probability /= rev_scale;
972 else if (edup->probability < edup_prob)
974 profile_probability scale
975 = (profile_probability::always () - edup_prob)
976 / (profile_probability::always () - edup->probability);
977 FOR_EACH_EDGE (esucc, ei, dup_block->succs)
978 if (esucc != edup)
979 esucc->probability *= scale;
981 if (edup_prob.initialized_p ())
982 edup->probability = edup_prob;
984 gcc_assert (!dup_block->count.initialized_p ());
985 dup_block->count = path_in_count;
988 if (path_in_count == profile_count::zero ())
989 return;
991 profile_count final_count = epath->count () - path_out_count;
993 /* Now update the original block's count in the
994 opposite manner - remove the counts/freq that will flow
995 into the duplicated block. Handle underflow due to precision/
996 rounding issues. */
997 epath->src->count -= path_in_count;
999 /* Next update this path edge's original and duplicated counts. We know
1000 that the duplicated path will have path_out_count flowing
1001 out of it (in the joiner case this is the count along the duplicated path
1002 out of the duplicated joiner). This count can then be removed from the
1003 original path edge. */
1005 edge esucc;
1006 edge_iterator ei;
1007 profile_probability epath_prob = final_count.probability_in (epath->src->count);
1009 if (epath->probability > epath_prob)
1011 profile_probability rev_scale
1012 = (profile_probability::always () - epath->probability)
1013 / (profile_probability::always () - epath_prob);
1014 FOR_EACH_EDGE (esucc, ei, epath->src->succs)
1015 if (esucc != epath)
1016 esucc->probability /= rev_scale;
1018 else if (epath->probability < epath_prob)
1020 profile_probability scale
1021 = (profile_probability::always () - epath_prob)
1022 / (profile_probability::always () - epath->probability);
1023 FOR_EACH_EDGE (esucc, ei, epath->src->succs)
1024 if (esucc != epath)
1025 esucc->probability *= scale;
1027 if (epath_prob.initialized_p ())
1028 epath->probability = epath_prob;
1031 /* Wire up the outgoing edges from the duplicate blocks and
1032 update any PHIs as needed. Also update the profile counts
1033 on the original and duplicate blocks and edges. */
1034 void
1035 ssa_fix_duplicate_block_edges (struct redirection_data *rd,
1036 ssa_local_info_t *local_info)
1038 bool multi_incomings = (rd->incoming_edges->next != NULL);
1039 edge e = rd->incoming_edges->e;
1040 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1041 edge elast = path->last ()->e;
1042 profile_count path_in_count = profile_count::zero ();
1043 profile_count path_out_count = profile_count::zero ();
1045 /* First determine how much profile count to move from original
1046 path to the duplicate path. This is tricky in the presence of
1047 a joiner (see comments for compute_path_counts), where some portion
1048 of the path's counts will flow off-path from the joiner. In the
1049 non-joiner case the path_in_count and path_out_count should be the
1050 same. */
1051 bool has_joiner = compute_path_counts (rd, local_info,
1052 &path_in_count, &path_out_count);
1054 for (unsigned int count = 0, i = 1; i < path->length (); i++)
1056 edge epath = (*path)[i]->e;
1058 /* If we were threading through an joiner block, then we want
1059 to keep its control statement and redirect an outgoing edge.
1060 Else we want to remove the control statement & edges, then create
1061 a new outgoing edge. In both cases we may need to update PHIs. */
1062 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1064 edge victim;
1065 edge e2;
1067 gcc_assert (has_joiner);
1069 /* This updates the PHIs at the destination of the duplicate
1070 block. Pass 0 instead of i if we are threading a path which
1071 has multiple incoming edges. */
1072 update_destination_phis (local_info->bb, rd->dup_blocks[count],
1073 path, multi_incomings ? 0 : i);
1075 /* Find the edge from the duplicate block to the block we're
1076 threading through. That's the edge we want to redirect. */
1077 victim = find_edge (rd->dup_blocks[count], (*path)[i]->e->dest);
1079 /* If there are no remaining blocks on the path to duplicate,
1080 then redirect VICTIM to the final destination of the jump
1081 threading path. */
1082 if (!any_remaining_duplicated_blocks (path, i))
1084 e2 = redirect_edge_and_branch (victim, elast->dest);
1085 /* If we redirected the edge, then we need to copy PHI arguments
1086 at the target. If the edge already existed (e2 != victim
1087 case), then the PHIs in the target already have the correct
1088 arguments. */
1089 if (e2 == victim)
1090 copy_phi_args (e2->dest, elast, e2,
1091 path, multi_incomings ? 0 : i);
1093 else
1095 /* Redirect VICTIM to the next duplicated block in the path. */
1096 e2 = redirect_edge_and_branch (victim, rd->dup_blocks[count + 1]);
1098 /* We need to update the PHIs in the next duplicated block. We
1099 want the new PHI args to have the same value as they had
1100 in the source of the next duplicate block.
1102 Thus, we need to know which edge we traversed into the
1103 source of the duplicate. Furthermore, we may have
1104 traversed many edges to reach the source of the duplicate.
1106 Walk through the path starting at element I until we
1107 hit an edge marked with EDGE_COPY_SRC_BLOCK. We want
1108 the edge from the prior element. */
1109 for (unsigned int j = i + 1; j < path->length (); j++)
1111 if ((*path)[j]->type == EDGE_COPY_SRC_BLOCK)
1113 copy_phi_arg_into_existing_phi ((*path)[j - 1]->e, e2);
1114 break;
1119 /* Update the counts of both the original block
1120 and path edge, and the duplicates. The path duplicate's
1121 incoming count are the totals for all edges
1122 incoming to this jump threading path computed earlier.
1123 And we know that the duplicated path will have path_out_count
1124 flowing out of it (i.e. along the duplicated path out of the
1125 duplicated joiner). */
1126 update_profile (epath, e2, path_in_count, path_out_count);
1128 else if ((*path)[i]->type == EDGE_COPY_SRC_BLOCK)
1130 remove_ctrl_stmt_and_useless_edges (rd->dup_blocks[count], NULL);
1131 create_edge_and_update_destination_phis (rd, rd->dup_blocks[count],
1132 multi_incomings ? 0 : i);
1133 if (count == 1)
1134 single_succ_edge (rd->dup_blocks[1])->aux = NULL;
1136 /* Update the counts of both the original block
1137 and path edge, and the duplicates. Since we are now after
1138 any joiner that may have existed on the path, the count
1139 flowing along the duplicated threaded path is path_out_count.
1140 If we didn't have a joiner, then cur_path_freq was the sum
1141 of the total frequencies along all incoming edges to the
1142 thread path (path_in_freq). If we had a joiner, it would have
1143 been updated at the end of that handling to the edge frequency
1144 along the duplicated joiner path edge. */
1145 update_profile (epath, EDGE_SUCC (rd->dup_blocks[count], 0),
1146 path_out_count, path_out_count);
1148 else
1150 /* No copy case. In this case we don't have an equivalent block
1151 on the duplicated thread path to update, but we do need
1152 to remove the portion of the counts/freqs that were moved
1153 to the duplicated path from the counts/freqs flowing through
1154 this block on the original path. Since all the no-copy edges
1155 are after any joiner, the removed count is the same as
1156 path_out_count.
1158 If we didn't have a joiner, then cur_path_freq was the sum
1159 of the total frequencies along all incoming edges to the
1160 thread path (path_in_freq). If we had a joiner, it would have
1161 been updated at the end of that handling to the edge frequency
1162 along the duplicated joiner path edge. */
1163 update_profile (epath, NULL, path_out_count, path_out_count);
1166 /* Increment the index into the duplicated path when we processed
1167 a duplicated block. */
1168 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK
1169 || (*path)[i]->type == EDGE_COPY_SRC_BLOCK)
1171 count++;
1176 /* Hash table traversal callback routine to create duplicate blocks. */
1179 ssa_create_duplicates (struct redirection_data **slot,
1180 ssa_local_info_t *local_info)
1182 struct redirection_data *rd = *slot;
1184 /* The second duplicated block in a jump threading path is specific
1185 to the path. So it gets stored in RD rather than in LOCAL_DATA.
1187 Each time we're called, we have to look through the path and see
1188 if a second block needs to be duplicated.
1190 Note the search starts with the third edge on the path. The first
1191 edge is the incoming edge, the second edge always has its source
1192 duplicated. Thus we start our search with the third edge. */
1193 vec<jump_thread_edge *> *path = rd->path;
1194 for (unsigned int i = 2; i < path->length (); i++)
1196 if ((*path)[i]->type == EDGE_COPY_SRC_BLOCK
1197 || (*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1199 create_block_for_threading ((*path)[i]->e->src, rd, 1,
1200 &local_info->duplicate_blocks);
1201 break;
1205 /* Create a template block if we have not done so already. Otherwise
1206 use the template to create a new block. */
1207 if (local_info->template_block == NULL)
1209 create_block_for_threading ((*path)[1]->e->src, rd, 0,
1210 &local_info->duplicate_blocks);
1211 local_info->template_block = rd->dup_blocks[0];
1212 local_info->template_last_to_copy
1213 = gsi_last_bb (local_info->template_block);
1215 /* We do not create any outgoing edges for the template. We will
1216 take care of that in a later traversal. That way we do not
1217 create edges that are going to just be deleted. */
1219 else
1221 gimple_seq seq = NULL;
1222 if (gsi_stmt (local_info->template_last_to_copy)
1223 != gsi_stmt (gsi_last_bb (local_info->template_block)))
1225 if (gsi_end_p (local_info->template_last_to_copy))
1227 seq = bb_seq (local_info->template_block);
1228 set_bb_seq (local_info->template_block, NULL);
1230 else
1231 seq = gsi_split_seq_after (local_info->template_last_to_copy);
1233 create_block_for_threading (local_info->template_block, rd, 0,
1234 &local_info->duplicate_blocks);
1235 if (seq)
1237 if (gsi_end_p (local_info->template_last_to_copy))
1238 set_bb_seq (local_info->template_block, seq);
1239 else
1240 gsi_insert_seq_after (&local_info->template_last_to_copy,
1241 seq, GSI_SAME_STMT);
1244 /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
1245 block. */
1246 ssa_fix_duplicate_block_edges (rd, local_info);
1249 if (MAY_HAVE_DEBUG_STMTS)
1251 /* Copy debug stmts from each NO_COPY src block to the block
1252 that would have been its predecessor, if we can append to it
1253 (we can't add stmts after a block-ending stmt), or prepending
1254 to the duplicate of the successor, if there is one. If
1255 there's no duplicate successor, we'll mostly drop the blocks
1256 on the floor; propagate_threaded_block_debug_into, called
1257 elsewhere, will consolidate and preserve the effects of the
1258 binds, but none of the markers. */
1259 gimple_stmt_iterator copy_to = gsi_last_bb (rd->dup_blocks[0]);
1260 if (!gsi_end_p (copy_to))
1262 if (stmt_ends_bb_p (gsi_stmt (copy_to)))
1264 if (rd->dup_blocks[1])
1265 copy_to = gsi_after_labels (rd->dup_blocks[1]);
1266 else
1267 copy_to = gsi_none ();
1269 else
1270 gsi_next (&copy_to);
1272 for (unsigned int i = 2, j = 0; i < path->length (); i++)
1273 if ((*path)[i]->type == EDGE_NO_COPY_SRC_BLOCK
1274 && gsi_bb (copy_to))
1276 for (gimple_stmt_iterator gsi = gsi_start_bb ((*path)[i]->e->src);
1277 !gsi_end_p (gsi); gsi_next (&gsi))
1279 if (!is_gimple_debug (gsi_stmt (gsi)))
1280 continue;
1281 gimple *stmt = gsi_stmt (gsi);
1282 gimple *copy = gimple_copy (stmt);
1283 gsi_insert_before (&copy_to, copy, GSI_SAME_STMT);
1286 else if ((*path)[i]->type == EDGE_COPY_SRC_BLOCK
1287 || (*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1289 j++;
1290 gcc_assert (j < 2);
1291 copy_to = gsi_last_bb (rd->dup_blocks[j]);
1292 if (!gsi_end_p (copy_to))
1294 if (stmt_ends_bb_p (gsi_stmt (copy_to)))
1295 copy_to = gsi_none ();
1296 else
1297 gsi_next (&copy_to);
1302 /* Keep walking the hash table. */
1303 return 1;
1306 /* We did not create any outgoing edges for the template block during
1307 block creation. This hash table traversal callback creates the
1308 outgoing edge for the template block. */
1310 inline int
1311 ssa_fixup_template_block (struct redirection_data **slot,
1312 ssa_local_info_t *local_info)
1314 struct redirection_data *rd = *slot;
1316 /* If this is the template block halt the traversal after updating
1317 it appropriately.
1319 If we were threading through an joiner block, then we want
1320 to keep its control statement and redirect an outgoing edge.
1321 Else we want to remove the control statement & edges, then create
1322 a new outgoing edge. In both cases we may need to update PHIs. */
1323 if (rd->dup_blocks[0] && rd->dup_blocks[0] == local_info->template_block)
1325 ssa_fix_duplicate_block_edges (rd, local_info);
1326 return 0;
1329 return 1;
1332 /* Hash table traversal callback to redirect each incoming edge
1333 associated with this hash table element to its new destination. */
1335 static int
1336 ssa_redirect_edges (struct redirection_data **slot,
1337 ssa_local_info_t *local_info)
1339 struct redirection_data *rd = *slot;
1340 struct el *next, *el;
1342 /* Walk over all the incoming edges associated with this hash table
1343 entry. */
1344 for (el = rd->incoming_edges; el; el = next)
1346 edge e = el->e;
1347 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1349 /* Go ahead and free this element from the list. Doing this now
1350 avoids the need for another list walk when we destroy the hash
1351 table. */
1352 next = el->next;
1353 free (el);
1355 local_info->num_threaded_edges++;
1357 if (rd->dup_blocks[0])
1359 edge e2;
1361 if (dump_file && (dump_flags & TDF_DETAILS))
1362 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
1363 e->src->index, e->dest->index, rd->dup_blocks[0]->index);
1365 /* Redirect the incoming edge (possibly to the joiner block) to the
1366 appropriate duplicate block. */
1367 e2 = redirect_edge_and_branch (e, rd->dup_blocks[0]);
1368 gcc_assert (e == e2);
1369 flush_pending_stmts (e2);
1372 /* Go ahead and clear E->aux. It's not needed anymore and failure
1373 to clear it will cause all kinds of unpleasant problems later. */
1374 path->release ();
1375 e->aux = NULL;
1379 /* Indicate that we actually threaded one or more jumps. */
1380 if (rd->incoming_edges)
1381 local_info->jumps_threaded = true;
1383 return 1;
1386 /* Return true if this block has no executable statements other than
1387 a simple ctrl flow instruction. When the number of outgoing edges
1388 is one, this is equivalent to a "forwarder" block. */
1390 static bool
1391 redirection_block_p (basic_block bb)
1393 gimple_stmt_iterator gsi;
1395 /* Advance to the first executable statement. */
1396 gsi = gsi_start_bb (bb);
1397 while (!gsi_end_p (gsi)
1398 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
1399 || is_gimple_debug (gsi_stmt (gsi))
1400 || gimple_nop_p (gsi_stmt (gsi))
1401 || gimple_clobber_p (gsi_stmt (gsi))))
1402 gsi_next (&gsi);
1404 /* Check if this is an empty block. */
1405 if (gsi_end_p (gsi))
1406 return true;
1408 /* Test that we've reached the terminating control statement. */
1409 return gsi_stmt (gsi)
1410 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
1411 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
1412 || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH);
1415 /* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
1416 is reached via one or more specific incoming edges, we know which
1417 outgoing edge from BB will be traversed.
1419 We want to redirect those incoming edges to the target of the
1420 appropriate outgoing edge. Doing so avoids a conditional branch
1421 and may expose new optimization opportunities. Note that we have
1422 to update dominator tree and SSA graph after such changes.
1424 The key to keeping the SSA graph update manageable is to duplicate
1425 the side effects occurring in BB so that those side effects still
1426 occur on the paths which bypass BB after redirecting edges.
1428 We accomplish this by creating duplicates of BB and arranging for
1429 the duplicates to unconditionally pass control to one specific
1430 successor of BB. We then revector the incoming edges into BB to
1431 the appropriate duplicate of BB.
1433 If NOLOOP_ONLY is true, we only perform the threading as long as it
1434 does not affect the structure of the loops in a nontrivial way.
1436 If JOINERS is true, then thread through joiner blocks as well. */
1438 bool
1439 fwd_jt_path_registry::thread_block_1 (basic_block bb,
1440 bool noloop_only,
1441 bool joiners)
1443 /* E is an incoming edge into BB that we may or may not want to
1444 redirect to a duplicate of BB. */
1445 edge e, e2;
1446 edge_iterator ei;
1447 ssa_local_info_t local_info;
1449 local_info.duplicate_blocks = BITMAP_ALLOC (NULL);
1450 local_info.need_profile_correction = false;
1451 local_info.num_threaded_edges = 0;
1453 /* To avoid scanning a linear array for the element we need we instead
1454 use a hash table. For normal code there should be no noticeable
1455 difference. However, if we have a block with a large number of
1456 incoming and outgoing edges such linear searches can get expensive. */
1457 m_redirection_data
1458 = new hash_table<struct redirection_data> (EDGE_COUNT (bb->succs));
1460 /* Record each unique threaded destination into a hash table for
1461 efficient lookups. */
1462 edge last = NULL;
1463 FOR_EACH_EDGE (e, ei, bb->preds)
1465 if (e->aux == NULL)
1466 continue;
1468 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1470 if (((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK && !joiners)
1471 || ((*path)[1]->type == EDGE_COPY_SRC_BLOCK && joiners))
1472 continue;
1474 e2 = path->last ()->e;
1475 if (!e2 || noloop_only)
1477 /* If NOLOOP_ONLY is true, we only allow threading through the
1478 header of a loop to exit edges. */
1480 /* One case occurs when there was loop header buried in a jump
1481 threading path that crosses loop boundaries. We do not try
1482 and thread this elsewhere, so just cancel the jump threading
1483 request by clearing the AUX field now. */
1484 if (bb->loop_father != e2->src->loop_father
1485 && (!loop_exit_edge_p (e2->src->loop_father, e2)
1486 || flow_loop_nested_p (bb->loop_father,
1487 e2->dest->loop_father)))
1489 /* Since this case is not handled by our special code
1490 to thread through a loop header, we must explicitly
1491 cancel the threading request here. */
1492 cancel_thread (path, "Threading through unhandled loop header");
1493 e->aux = NULL;
1494 continue;
1497 /* Another case occurs when trying to thread through our
1498 own loop header, possibly from inside the loop. We will
1499 thread these later. */
1500 unsigned int i;
1501 for (i = 1; i < path->length (); i++)
1503 if ((*path)[i]->e->src == bb->loop_father->header
1504 && (!loop_exit_edge_p (bb->loop_father, e2)
1505 || (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK))
1506 break;
1509 if (i != path->length ())
1510 continue;
1512 /* Loop parallelization can be confused by the result of
1513 threading through the loop exit test back into the loop.
1514 However, theading those jumps seems to help other codes.
1516 I have been unable to find anything related to the shape of
1517 the CFG, the contents of the affected blocks, etc which would
1518 allow a more sensible test than what we're using below which
1519 merely avoids the optimization when parallelizing loops. */
1520 if (flag_tree_parallelize_loops > 1)
1522 for (i = 1; i < path->length (); i++)
1523 if (bb->loop_father == e2->src->loop_father
1524 && loop_exits_from_bb_p (bb->loop_father,
1525 (*path)[i]->e->src)
1526 && !loop_exit_edge_p (bb->loop_father, e2))
1527 break;
1529 if (i != path->length ())
1531 cancel_thread (path, "Threading through loop exit");
1532 e->aux = NULL;
1533 continue;
1538 /* Insert the outgoing edge into the hash table if it is not
1539 already in the hash table. */
1540 lookup_redirection_data (e, INSERT);
1542 /* When we have thread paths through a common joiner with different
1543 final destinations, then we may need corrections to deal with
1544 profile insanities. See the big comment before compute_path_counts. */
1545 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1547 if (!last)
1548 last = e2;
1549 else if (e2 != last)
1550 local_info.need_profile_correction = true;
1554 /* We do not update dominance info. */
1555 free_dominance_info (CDI_DOMINATORS);
1557 /* We know we only thread through the loop header to loop exits.
1558 Let the basic block duplication hook know we are not creating
1559 a multiple entry loop. */
1560 if (noloop_only
1561 && bb == bb->loop_father->header)
1562 set_loop_copy (bb->loop_father, loop_outer (bb->loop_father));
1564 /* Now create duplicates of BB.
1566 Note that for a block with a high outgoing degree we can waste
1567 a lot of time and memory creating and destroying useless edges.
1569 So we first duplicate BB and remove the control structure at the
1570 tail of the duplicate as well as all outgoing edges from the
1571 duplicate. We then use that duplicate block as a template for
1572 the rest of the duplicates. */
1573 local_info.template_block = NULL;
1574 local_info.bb = bb;
1575 local_info.jumps_threaded = false;
1576 m_redirection_data->traverse <ssa_local_info_t *, ssa_create_duplicates>
1577 (&local_info);
1579 /* The template does not have an outgoing edge. Create that outgoing
1580 edge and update PHI nodes as the edge's target as necessary.
1582 We do this after creating all the duplicates to avoid creating
1583 unnecessary edges. */
1584 m_redirection_data->traverse <ssa_local_info_t *, ssa_fixup_template_block>
1585 (&local_info);
1587 /* The hash table traversals above created the duplicate blocks (and the
1588 statements within the duplicate blocks). This loop creates PHI nodes for
1589 the duplicated blocks and redirects the incoming edges into BB to reach
1590 the duplicates of BB. */
1591 m_redirection_data->traverse <ssa_local_info_t *, ssa_redirect_edges>
1592 (&local_info);
1594 /* Done with this block. Clear REDIRECTION_DATA. */
1595 delete m_redirection_data;
1596 m_redirection_data = NULL;
1598 if (noloop_only
1599 && bb == bb->loop_father->header)
1600 set_loop_copy (bb->loop_father, NULL);
1602 BITMAP_FREE (local_info.duplicate_blocks);
1603 local_info.duplicate_blocks = NULL;
1605 m_num_threaded_edges += local_info.num_threaded_edges;
1607 /* Indicate to our caller whether or not any jumps were threaded. */
1608 return local_info.jumps_threaded;
1611 /* Wrapper for thread_block_1 so that we can first handle jump
1612 thread paths which do not involve copying joiner blocks, then
1613 handle jump thread paths which have joiner blocks.
1615 By doing things this way we can be as aggressive as possible and
1616 not worry that copying a joiner block will create a jump threading
1617 opportunity. */
1619 bool
1620 fwd_jt_path_registry::thread_block (basic_block bb, bool noloop_only)
1622 bool retval;
1623 retval = thread_block_1 (bb, noloop_only, false);
1624 retval |= thread_block_1 (bb, noloop_only, true);
1625 return retval;
1628 /* Callback for dfs_enumerate_from. Returns true if BB is different
1629 from STOP and DBDS_CE_STOP. */
1631 static basic_block dbds_ce_stop;
1632 static bool
1633 dbds_continue_enumeration_p (const_basic_block bb, const void *stop)
1635 return (bb != (const_basic_block) stop
1636 && bb != dbds_ce_stop);
1639 /* Evaluates the dominance relationship of latch of the LOOP and BB, and
1640 returns the state. */
1642 enum bb_dom_status
1643 determine_bb_domination_status (class loop *loop, basic_block bb)
1645 basic_block *bblocks;
1646 unsigned nblocks, i;
1647 bool bb_reachable = false;
1648 edge_iterator ei;
1649 edge e;
1651 /* This function assumes BB is a successor of LOOP->header.
1652 If that is not the case return DOMST_NONDOMINATING which
1653 is always safe. */
1655 bool ok = false;
1657 FOR_EACH_EDGE (e, ei, bb->preds)
1659 if (e->src == loop->header)
1661 ok = true;
1662 break;
1666 if (!ok)
1667 return DOMST_NONDOMINATING;
1670 if (bb == loop->latch)
1671 return DOMST_DOMINATING;
1673 /* Check that BB dominates LOOP->latch, and that it is back-reachable
1674 from it. */
1676 bblocks = XCNEWVEC (basic_block, loop->num_nodes);
1677 dbds_ce_stop = loop->header;
1678 nblocks = dfs_enumerate_from (loop->latch, 1, dbds_continue_enumeration_p,
1679 bblocks, loop->num_nodes, bb);
1680 for (i = 0; i < nblocks; i++)
1681 FOR_EACH_EDGE (e, ei, bblocks[i]->preds)
1683 if (e->src == loop->header)
1685 free (bblocks);
1686 return DOMST_NONDOMINATING;
1688 if (e->src == bb)
1689 bb_reachable = true;
1692 free (bblocks);
1693 return (bb_reachable ? DOMST_DOMINATING : DOMST_LOOP_BROKEN);
1696 /* Thread jumps through the header of LOOP. Returns true if cfg changes.
1697 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading from entry edges
1698 to the inside of the loop. */
1700 bool
1701 fwd_jt_path_registry::thread_through_loop_header (class loop *loop,
1702 bool may_peel_loop_headers)
1704 basic_block header = loop->header;
1705 edge e, tgt_edge, latch = loop_latch_edge (loop);
1706 edge_iterator ei;
1707 basic_block tgt_bb, atgt_bb;
1708 enum bb_dom_status domst;
1710 /* We have already threaded through headers to exits, so all the threading
1711 requests now are to the inside of the loop. We need to avoid creating
1712 irreducible regions (i.e., loops with more than one entry block), and
1713 also loop with several latch edges, or new subloops of the loop (although
1714 there are cases where it might be appropriate, it is difficult to decide,
1715 and doing it wrongly may confuse other optimizers).
1717 We could handle more general cases here. However, the intention is to
1718 preserve some information about the loop, which is impossible if its
1719 structure changes significantly, in a way that is not well understood.
1720 Thus we only handle few important special cases, in which also updating
1721 of the loop-carried information should be feasible:
1723 1) Propagation of latch edge to a block that dominates the latch block
1724 of a loop. This aims to handle the following idiom:
1726 first = 1;
1727 while (1)
1729 if (first)
1730 initialize;
1731 first = 0;
1732 body;
1735 After threading the latch edge, this becomes
1737 first = 1;
1738 if (first)
1739 initialize;
1740 while (1)
1742 first = 0;
1743 body;
1746 The original header of the loop is moved out of it, and we may thread
1747 the remaining edges through it without further constraints.
1749 2) All entry edges are propagated to a single basic block that dominates
1750 the latch block of the loop. This aims to handle the following idiom
1751 (normally created for "for" loops):
1753 i = 0;
1754 while (1)
1756 if (i >= 100)
1757 break;
1758 body;
1759 i++;
1762 This becomes
1764 i = 0;
1765 while (1)
1767 body;
1768 i++;
1769 if (i >= 100)
1770 break;
1774 /* Threading through the header won't improve the code if the header has just
1775 one successor. */
1776 if (single_succ_p (header))
1777 goto fail;
1779 if (!may_peel_loop_headers && !redirection_block_p (loop->header))
1780 goto fail;
1781 else
1783 tgt_bb = NULL;
1784 tgt_edge = NULL;
1785 FOR_EACH_EDGE (e, ei, header->preds)
1787 if (!e->aux)
1789 if (e == latch)
1790 continue;
1792 /* If latch is not threaded, and there is a header
1793 edge that is not threaded, we would create loop
1794 with multiple entries. */
1795 goto fail;
1798 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1800 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1801 goto fail;
1802 tgt_edge = (*path)[1]->e;
1803 atgt_bb = tgt_edge->dest;
1804 if (!tgt_bb)
1805 tgt_bb = atgt_bb;
1806 /* Two targets of threading would make us create loop
1807 with multiple entries. */
1808 else if (tgt_bb != atgt_bb)
1809 goto fail;
1812 if (!tgt_bb)
1814 /* There are no threading requests. */
1815 return false;
1818 /* Redirecting to empty loop latch is useless. */
1819 if (tgt_bb == loop->latch
1820 && empty_block_p (loop->latch))
1821 goto fail;
1824 /* The target block must dominate the loop latch, otherwise we would be
1825 creating a subloop. */
1826 domst = determine_bb_domination_status (loop, tgt_bb);
1827 if (domst == DOMST_NONDOMINATING)
1828 goto fail;
1829 if (domst == DOMST_LOOP_BROKEN)
1831 /* If the loop ceased to exist, mark it as such, and thread through its
1832 original header. */
1833 mark_loop_for_removal (loop);
1834 return thread_block (header, false);
1837 if (tgt_bb->loop_father->header == tgt_bb)
1839 /* If the target of the threading is a header of a subloop, we need
1840 to create a preheader for it, so that the headers of the two loops
1841 do not merge. */
1842 if (EDGE_COUNT (tgt_bb->preds) > 2)
1844 tgt_bb = create_preheader (tgt_bb->loop_father, 0);
1845 gcc_assert (tgt_bb != NULL);
1847 else
1848 tgt_bb = split_edge (tgt_edge);
1851 basic_block new_preheader;
1853 /* Now consider the case entry edges are redirected to the new entry
1854 block. Remember one entry edge, so that we can find the new
1855 preheader (its destination after threading). */
1856 FOR_EACH_EDGE (e, ei, header->preds)
1858 if (e->aux)
1859 break;
1862 /* The duplicate of the header is the new preheader of the loop. Ensure
1863 that it is placed correctly in the loop hierarchy. */
1864 set_loop_copy (loop, loop_outer (loop));
1866 thread_block (header, false);
1867 set_loop_copy (loop, NULL);
1868 new_preheader = e->dest;
1870 /* Create the new latch block. This is always necessary, as the latch
1871 must have only a single successor, but the original header had at
1872 least two successors. */
1873 loop->latch = NULL;
1874 mfb_kj_edge = single_succ_edge (new_preheader);
1875 loop->header = mfb_kj_edge->dest;
1876 latch = make_forwarder_block (tgt_bb, mfb_keep_just, NULL);
1877 loop->header = latch->dest;
1878 loop->latch = latch->src;
1879 return true;
1881 fail:
1882 /* We failed to thread anything. Cancel the requests. */
1883 FOR_EACH_EDGE (e, ei, header->preds)
1885 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1887 if (path)
1889 cancel_thread (path, "Failure in thread_through_loop_header");
1890 e->aux = NULL;
1893 return false;
1896 /* E1 and E2 are edges into the same basic block. Return TRUE if the
1897 PHI arguments associated with those edges are equal or there are no
1898 PHI arguments, otherwise return FALSE. */
1900 static bool
1901 phi_args_equal_on_edges (edge e1, edge e2)
1903 gphi_iterator gsi;
1904 int indx1 = e1->dest_idx;
1905 int indx2 = e2->dest_idx;
1907 for (gsi = gsi_start_phis (e1->dest); !gsi_end_p (gsi); gsi_next (&gsi))
1909 gphi *phi = gsi.phi ();
1911 if (!operand_equal_p (gimple_phi_arg_def (phi, indx1),
1912 gimple_phi_arg_def (phi, indx2), 0))
1913 return false;
1915 return true;
1918 /* Return the number of non-debug statements and non-virtual PHIs in a
1919 block. */
1921 static unsigned int
1922 count_stmts_and_phis_in_block (basic_block bb)
1924 unsigned int num_stmts = 0;
1926 gphi_iterator gpi;
1927 for (gpi = gsi_start_phis (bb); !gsi_end_p (gpi); gsi_next (&gpi))
1928 if (!virtual_operand_p (PHI_RESULT (gpi.phi ())))
1929 num_stmts++;
1931 gimple_stmt_iterator gsi;
1932 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1934 gimple *stmt = gsi_stmt (gsi);
1935 if (!is_gimple_debug (stmt))
1936 num_stmts++;
1939 return num_stmts;
1943 /* Walk through the registered jump threads and convert them into a
1944 form convenient for this pass.
1946 Any block which has incoming edges threaded to outgoing edges
1947 will have its entry in THREADED_BLOCK set.
1949 Any threaded edge will have its new outgoing edge stored in the
1950 original edge's AUX field.
1952 This form avoids the need to walk all the edges in the CFG to
1953 discover blocks which need processing and avoids unnecessary
1954 hash table lookups to map from threaded edge to new target. */
1956 void
1957 fwd_jt_path_registry::mark_threaded_blocks (bitmap threaded_blocks)
1959 unsigned int i;
1960 bitmap_iterator bi;
1961 auto_bitmap tmp;
1962 basic_block bb;
1963 edge e;
1964 edge_iterator ei;
1966 /* It is possible to have jump threads in which one is a subpath
1967 of the other. ie, (A, B), (B, C), (C, D) where B is a joiner
1968 block and (B, C), (C, D) where no joiner block exists.
1970 When this occurs ignore the jump thread request with the joiner
1971 block. It's totally subsumed by the simpler jump thread request.
1973 This results in less block copying, simpler CFGs. More importantly,
1974 when we duplicate the joiner block, B, in this case we will create
1975 a new threading opportunity that we wouldn't be able to optimize
1976 until the next jump threading iteration.
1978 So first convert the jump thread requests which do not require a
1979 joiner block. */
1980 for (i = 0; i < m_paths.length (); i++)
1982 vec<jump_thread_edge *> *path = m_paths[i];
1984 if (path->length () > 1
1985 && (*path)[1]->type != EDGE_COPY_SRC_JOINER_BLOCK)
1987 edge e = (*path)[0]->e;
1988 e->aux = (void *)path;
1989 bitmap_set_bit (tmp, e->dest->index);
1993 /* Now iterate again, converting cases where we want to thread
1994 through a joiner block, but only if no other edge on the path
1995 already has a jump thread attached to it. We do this in two passes,
1996 to avoid situations where the order in the paths vec can hide overlapping
1997 threads (the path is recorded on the incoming edge, so we would miss
1998 cases where the second path starts at a downstream edge on the same
1999 path). First record all joiner paths, deleting any in the unexpected
2000 case where there is already a path for that incoming edge. */
2001 for (i = 0; i < m_paths.length ();)
2003 vec<jump_thread_edge *> *path = m_paths[i];
2005 if (path->length () > 1
2006 && (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
2008 /* Attach the path to the starting edge if none is yet recorded. */
2009 if ((*path)[0]->e->aux == NULL)
2011 (*path)[0]->e->aux = path;
2012 i++;
2014 else
2016 m_paths.unordered_remove (i);
2017 cancel_thread (path);
2020 else
2022 i++;
2026 /* Second, look for paths that have any other jump thread attached to
2027 them, and either finish converting them or cancel them. */
2028 for (i = 0; i < m_paths.length ();)
2030 vec<jump_thread_edge *> *path = m_paths[i];
2031 edge e = (*path)[0]->e;
2033 if (path->length () > 1
2034 && (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK && e->aux == path)
2036 unsigned int j;
2037 for (j = 1; j < path->length (); j++)
2038 if ((*path)[j]->e->aux != NULL)
2039 break;
2041 /* If we iterated through the entire path without exiting the loop,
2042 then we are good to go, record it. */
2043 if (j == path->length ())
2045 bitmap_set_bit (tmp, e->dest->index);
2046 i++;
2048 else
2050 e->aux = NULL;
2051 m_paths.unordered_remove (i);
2052 cancel_thread (path);
2055 else
2057 i++;
2061 /* When optimizing for size, prune all thread paths where statement
2062 duplication is necessary.
2064 We walk the jump thread path looking for copied blocks. There's
2065 two types of copied blocks.
2067 EDGE_COPY_SRC_JOINER_BLOCK is always copied and thus we will
2068 cancel the jump threading request when optimizing for size.
2070 EDGE_COPY_SRC_BLOCK which is copied, but some of its statements
2071 will be killed by threading. If threading does not kill all of
2072 its statements, then we should cancel the jump threading request
2073 when optimizing for size. */
2074 if (optimize_function_for_size_p (cfun))
2076 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
2078 FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (cfun, i)->preds)
2079 if (e->aux)
2081 vec<jump_thread_edge *> *path = THREAD_PATH (e);
2083 unsigned int j;
2084 for (j = 1; j < path->length (); j++)
2086 bb = (*path)[j]->e->src;
2087 if (redirection_block_p (bb))
2089 else if ((*path)[j]->type == EDGE_COPY_SRC_JOINER_BLOCK
2090 || ((*path)[j]->type == EDGE_COPY_SRC_BLOCK
2091 && (count_stmts_and_phis_in_block (bb)
2092 != estimate_threading_killed_stmts (bb))))
2093 break;
2096 if (j != path->length ())
2098 cancel_thread (path);
2099 e->aux = NULL;
2101 else
2102 bitmap_set_bit (threaded_blocks, i);
2106 else
2107 bitmap_copy (threaded_blocks, tmp);
2109 /* If we have a joiner block (J) which has two successors S1 and S2 and
2110 we are threading though S1 and the final destination of the thread
2111 is S2, then we must verify that any PHI nodes in S2 have the same
2112 PHI arguments for the edge J->S2 and J->S1->...->S2.
2114 We used to detect this prior to registering the jump thread, but
2115 that prohibits propagation of edge equivalences into non-dominated
2116 PHI nodes as the equivalency test might occur before propagation.
2118 This must also occur after we truncate any jump threading paths
2119 as this scenario may only show up after truncation.
2121 This works for now, but will need improvement as part of the FSA
2122 optimization.
2124 Note since we've moved the thread request data to the edges,
2125 we have to iterate on those rather than the threaded_edges vector. */
2126 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
2128 bb = BASIC_BLOCK_FOR_FN (cfun, i);
2129 FOR_EACH_EDGE (e, ei, bb->preds)
2131 if (e->aux)
2133 vec<jump_thread_edge *> *path = THREAD_PATH (e);
2134 bool have_joiner = ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK);
2136 if (have_joiner)
2138 basic_block joiner = e->dest;
2139 edge final_edge = path->last ()->e;
2140 basic_block final_dest = final_edge->dest;
2141 edge e2 = find_edge (joiner, final_dest);
2143 if (e2 && !phi_args_equal_on_edges (e2, final_edge))
2145 cancel_thread (path);
2146 e->aux = NULL;
2153 /* Look for jump threading paths which cross multiple loop headers.
2155 The code to thread through loop headers will change the CFG in ways
2156 that invalidate the cached loop iteration information. So we must
2157 detect that case and wipe the cached information. */
2158 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
2160 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
2161 FOR_EACH_EDGE (e, ei, bb->preds)
2163 if (e->aux)
2165 gcc_assert (loops_state_satisfies_p
2166 (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS));
2167 vec<jump_thread_edge *> *path = THREAD_PATH (e);
2169 for (unsigned int i = 0, crossed_headers = 0;
2170 i < path->length ();
2171 i++)
2173 basic_block dest = (*path)[i]->e->dest;
2174 basic_block src = (*path)[i]->e->src;
2175 /* If we enter a loop. */
2176 if (flow_loop_nested_p (src->loop_father, dest->loop_father))
2177 ++crossed_headers;
2178 /* If we step from a block outside an irreducible region
2179 to a block inside an irreducible region, then we have
2180 crossed into a loop. */
2181 else if (! (src->flags & BB_IRREDUCIBLE_LOOP)
2182 && (dest->flags & BB_IRREDUCIBLE_LOOP))
2183 ++crossed_headers;
2184 if (crossed_headers > 1)
2186 vect_free_loop_info_assumptions
2187 ((*path)[path->length () - 1]->e->dest->loop_father);
2188 break;
2197 /* Verify that the REGION is a valid jump thread. A jump thread is a special
2198 case of SEME Single Entry Multiple Exits region in which all nodes in the
2199 REGION have exactly one incoming edge. The only exception is the first block
2200 that may not have been connected to the rest of the cfg yet. */
2202 DEBUG_FUNCTION void
2203 verify_jump_thread (basic_block *region, unsigned n_region)
2205 for (unsigned i = 0; i < n_region; i++)
2206 gcc_assert (EDGE_COUNT (region[i]->preds) <= 1);
2209 /* Return true when BB is one of the first N items in BBS. */
2211 static inline bool
2212 bb_in_bbs (basic_block bb, basic_block *bbs, int n)
2214 for (int i = 0; i < n; i++)
2215 if (bb == bbs[i])
2216 return true;
2218 return false;
2221 void
2222 jt_path_registry::debug_path (FILE *dump_file, int pathno)
2224 vec<jump_thread_edge *> *p = m_paths[pathno];
2225 fprintf (dump_file, "path: ");
2226 for (unsigned i = 0; i < p->length (); ++i)
2227 fprintf (dump_file, "%d -> %d, ",
2228 (*p)[i]->e->src->index, (*p)[i]->e->dest->index);
2229 fprintf (dump_file, "\n");
2232 void
2233 jt_path_registry::debug ()
2235 for (unsigned i = 0; i < m_paths.length (); ++i)
2236 debug_path (stderr, i);
2239 /* Rewire a jump_thread_edge so that the source block is now a
2240 threaded source block.
2242 PATH_NUM is an index into the global path table PATHS.
2243 EDGE_NUM is the jump thread edge number into said path.
2245 Returns TRUE if we were able to successfully rewire the edge. */
2247 bool
2248 back_jt_path_registry::rewire_first_differing_edge (unsigned path_num,
2249 unsigned edge_num)
2251 vec<jump_thread_edge *> *path = m_paths[path_num];
2252 edge &e = (*path)[edge_num]->e;
2253 if (dump_file && (dump_flags & TDF_DETAILS))
2254 fprintf (dump_file, "rewiring edge candidate: %d -> %d\n",
2255 e->src->index, e->dest->index);
2256 basic_block src_copy = get_bb_copy (e->src);
2257 if (src_copy == NULL)
2259 if (dump_file && (dump_flags & TDF_DETAILS))
2260 fprintf (dump_file, "ignoring candidate: there is no src COPY\n");
2261 return false;
2263 edge new_edge = find_edge (src_copy, e->dest);
2264 /* If the previously threaded paths created a flow graph where we
2265 can no longer figure out where to go, give up. */
2266 if (new_edge == NULL)
2268 if (dump_file && (dump_flags & TDF_DETAILS))
2269 fprintf (dump_file, "ignoring candidate: we lost our way\n");
2270 return false;
2272 e = new_edge;
2273 return true;
2276 /* After a path has been jump threaded, adjust the remaining paths
2277 that are subsets of this path, so these paths can be safely
2278 threaded within the context of the new threaded path.
2280 For example, suppose we have just threaded:
2282 5 -> 6 -> 7 -> 8 -> 12 => 5 -> 6' -> 7' -> 8' -> 12'
2284 And we have an upcoming threading candidate:
2285 5 -> 6 -> 7 -> 8 -> 15 -> 20
2287 This function adjusts the upcoming path into:
2288 8' -> 15 -> 20
2290 CURR_PATH_NUM is an index into the global paths table. It
2291 specifies the path that was just threaded. */
2293 void
2294 back_jt_path_registry::adjust_paths_after_duplication (unsigned curr_path_num)
2296 vec<jump_thread_edge *> *curr_path = m_paths[curr_path_num];
2298 if (dump_file && (dump_flags & TDF_DETAILS))
2300 fprintf (dump_file, "just threaded: ");
2301 debug_path (dump_file, curr_path_num);
2304 /* Iterate through all the other paths and adjust them. */
2305 for (unsigned cand_path_num = 0; cand_path_num < m_paths.length (); )
2307 if (cand_path_num == curr_path_num)
2309 ++cand_path_num;
2310 continue;
2312 /* Make sure the candidate to adjust starts with the same path
2313 as the recently threaded path. */
2314 vec<jump_thread_edge *> *cand_path = m_paths[cand_path_num];
2315 if ((*cand_path)[0]->e != (*curr_path)[0]->e)
2317 ++cand_path_num;
2318 continue;
2320 if (dump_file && (dump_flags & TDF_DETAILS))
2322 fprintf (dump_file, "adjusting candidate: ");
2323 debug_path (dump_file, cand_path_num);
2326 /* Chop off from the candidate path any prefix it shares with
2327 the recently threaded path. */
2328 unsigned minlength = MIN (curr_path->length (), cand_path->length ());
2329 unsigned j;
2330 for (j = 0; j < minlength; ++j)
2332 edge cand_edge = (*cand_path)[j]->e;
2333 edge curr_edge = (*curr_path)[j]->e;
2335 /* Once the prefix no longer matches, adjust the first
2336 non-matching edge to point from an adjusted edge to
2337 wherever it was going. */
2338 if (cand_edge != curr_edge)
2340 gcc_assert (cand_edge->src == curr_edge->src);
2341 if (!rewire_first_differing_edge (cand_path_num, j))
2342 goto remove_candidate_from_list;
2343 break;
2346 if (j == minlength)
2348 /* If we consumed the max subgraph we could look at, and
2349 still didn't find any different edges, it's the
2350 last edge after MINLENGTH. */
2351 if (cand_path->length () > minlength)
2353 if (!rewire_first_differing_edge (cand_path_num, j))
2354 goto remove_candidate_from_list;
2356 else if (dump_file && (dump_flags & TDF_DETAILS))
2357 fprintf (dump_file, "adjusting first edge after MINLENGTH.\n");
2359 if (j > 0)
2361 /* If we are removing everything, delete the entire candidate. */
2362 if (j == cand_path->length ())
2364 remove_candidate_from_list:
2365 cancel_thread (cand_path, "Adjusted candidate is EMPTY");
2366 m_paths.unordered_remove (cand_path_num);
2367 continue;
2369 /* Otherwise, just remove the redundant sub-path. */
2370 if (cand_path->length () - j > 1)
2371 cand_path->block_remove (0, j);
2372 else if (dump_file && (dump_flags & TDF_DETAILS))
2373 fprintf (dump_file, "Dropping illformed candidate.\n");
2375 if (dump_file && (dump_flags & TDF_DETAILS))
2377 fprintf (dump_file, "adjusted candidate: ");
2378 debug_path (dump_file, cand_path_num);
2380 ++cand_path_num;
2384 /* Duplicates a jump-thread path of N_REGION basic blocks.
2385 The ENTRY edge is redirected to the duplicate of the region.
2387 Remove the last conditional statement in the last basic block in the REGION,
2388 and create a single fallthru edge pointing to the same destination as the
2389 EXIT edge.
2391 CURRENT_PATH_NO is an index into the global paths[] table
2392 specifying the jump-thread path.
2394 Returns false if it is unable to copy the region, true otherwise. */
2396 bool
2397 back_jt_path_registry::duplicate_thread_path (edge entry,
2398 edge exit,
2399 basic_block *region,
2400 unsigned n_region,
2401 unsigned current_path_no)
2403 unsigned i;
2404 class loop *loop = entry->dest->loop_father;
2405 edge exit_copy;
2406 edge redirected;
2407 profile_count curr_count;
2409 if (!can_copy_bbs_p (region, n_region))
2410 return false;
2412 if (dump_file && (dump_flags & TDF_DETAILS))
2414 fprintf (dump_file, "\nabout to thread: ");
2415 debug_path (dump_file, current_path_no);
2418 /* Some sanity checking. Note that we do not check for all possible
2419 missuses of the functions. I.e. if you ask to copy something weird,
2420 it will work, but the state of structures probably will not be
2421 correct. */
2422 for (i = 0; i < n_region; i++)
2424 /* We do not handle subloops, i.e. all the blocks must belong to the
2425 same loop. */
2426 if (region[i]->loop_father != loop)
2427 return false;
2430 initialize_original_copy_tables ();
2432 set_loop_copy (loop, loop);
2434 basic_block *region_copy = XNEWVEC (basic_block, n_region);
2435 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
2436 split_edge_bb_loc (entry), false);
2438 /* Fix up: copy_bbs redirects all edges pointing to copied blocks. The
2439 following code ensures that all the edges exiting the jump-thread path are
2440 redirected back to the original code: these edges are exceptions
2441 invalidating the property that is propagated by executing all the blocks of
2442 the jump-thread path in order. */
2444 curr_count = entry->count ();
2446 for (i = 0; i < n_region; i++)
2448 edge e;
2449 edge_iterator ei;
2450 basic_block bb = region_copy[i];
2452 /* Watch inconsistent profile. */
2453 if (curr_count > region[i]->count)
2454 curr_count = region[i]->count;
2455 /* Scale current BB. */
2456 if (region[i]->count.nonzero_p () && curr_count.initialized_p ())
2458 /* In the middle of the path we only scale the frequencies.
2459 In last BB we need to update probabilities of outgoing edges
2460 because we know which one is taken at the threaded path. */
2461 if (i + 1 != n_region)
2462 scale_bbs_frequencies_profile_count (region + i, 1,
2463 region[i]->count - curr_count,
2464 region[i]->count);
2465 else
2466 update_bb_profile_for_threading (region[i],
2467 curr_count,
2468 exit);
2469 scale_bbs_frequencies_profile_count (region_copy + i, 1, curr_count,
2470 region_copy[i]->count);
2473 if (single_succ_p (bb))
2475 /* Make sure the successor is the next node in the path. */
2476 gcc_assert (i + 1 == n_region
2477 || region_copy[i + 1] == single_succ_edge (bb)->dest);
2478 if (i + 1 != n_region)
2480 curr_count = single_succ_edge (bb)->count ();
2482 continue;
2485 /* Special case the last block on the path: make sure that it does not
2486 jump back on the copied path, including back to itself. */
2487 if (i + 1 == n_region)
2489 FOR_EACH_EDGE (e, ei, bb->succs)
2490 if (bb_in_bbs (e->dest, region_copy, n_region))
2492 basic_block orig = get_bb_original (e->dest);
2493 if (orig)
2494 redirect_edge_and_branch_force (e, orig);
2496 continue;
2499 /* Redirect all other edges jumping to non-adjacent blocks back to the
2500 original code. */
2501 FOR_EACH_EDGE (e, ei, bb->succs)
2502 if (region_copy[i + 1] != e->dest)
2504 basic_block orig = get_bb_original (e->dest);
2505 if (orig)
2506 redirect_edge_and_branch_force (e, orig);
2508 else
2510 curr_count = e->count ();
2515 if (flag_checking)
2516 verify_jump_thread (region_copy, n_region);
2518 /* Remove the last branch in the jump thread path. */
2519 remove_ctrl_stmt_and_useless_edges (region_copy[n_region - 1], exit->dest);
2521 /* And fixup the flags on the single remaining edge. */
2522 edge fix_e = find_edge (region_copy[n_region - 1], exit->dest);
2523 fix_e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
2524 fix_e->flags |= EDGE_FALLTHRU;
2526 edge e = make_edge (region_copy[n_region - 1], exit->dest, EDGE_FALLTHRU);
2528 if (e)
2530 rescan_loop_exit (e, true, false);
2531 e->probability = profile_probability::always ();
2534 /* Redirect the entry and add the phi node arguments. */
2535 if (entry->dest == loop->header)
2536 mark_loop_for_removal (loop);
2537 redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
2538 gcc_assert (redirected != NULL);
2539 flush_pending_stmts (entry);
2541 /* Add the other PHI node arguments. */
2542 add_phi_args_after_copy (region_copy, n_region, NULL);
2544 free (region_copy);
2546 adjust_paths_after_duplication (current_path_no);
2548 free_original_copy_tables ();
2549 return true;
2552 /* Return true when PATH is a valid jump-thread path. */
2554 static bool
2555 valid_jump_thread_path (vec<jump_thread_edge *> *path)
2557 unsigned len = path->length ();
2559 /* Check that the path is connected. */
2560 for (unsigned int j = 0; j < len - 1; j++)
2562 edge e = (*path)[j]->e;
2563 if (e->dest != (*path)[j+1]->e->src)
2564 return false;
2566 return true;
2569 /* Remove any queued jump threads that include edge E.
2571 We don't actually remove them here, just record the edges into ax
2572 hash table. That way we can do the search once per iteration of
2573 DOM/VRP rather than for every case where DOM optimizes away a COND_EXPR. */
2575 void
2576 fwd_jt_path_registry::remove_jump_threads_including (edge_def *e)
2578 if (!m_paths.exists () || !flag_thread_jumps)
2579 return;
2581 edge *slot = m_removed_edges->find_slot (e, INSERT);
2582 *slot = e;
2585 /* Thread all paths that have been queued for jump threading, and
2586 update the CFG accordingly.
2588 It is the caller's responsibility to fix the dominance information
2589 and rewrite duplicated SSA_NAMEs back into SSA form.
2591 If PEEL_LOOP_HEADERS is false, avoid threading edges through loop
2592 headers if it does not simplify the loop.
2594 Returns true if one or more edges were threaded. */
2596 bool
2597 jt_path_registry::thread_through_all_blocks (bool peel_loop_headers)
2599 if (m_paths.length () == 0)
2600 return false;
2602 m_num_threaded_edges = 0;
2604 bool retval = update_cfg (peel_loop_headers);
2606 statistics_counter_event (cfun, "Jumps threaded", m_num_threaded_edges);
2608 if (retval)
2610 loops_state_set (LOOPS_NEED_FIXUP);
2611 return true;
2613 return false;
2616 /* This is the backward threader version of thread_through_all_blocks
2617 using a generic BB copier. */
2619 bool
2620 back_jt_path_registry::update_cfg (bool /*peel_loop_headers*/)
2622 bool retval = false;
2623 hash_set<edge> visited_starting_edges;
2625 while (m_paths.length ())
2627 vec<jump_thread_edge *> *path = m_paths[0];
2628 edge entry = (*path)[0]->e;
2630 /* Do not jump-thread twice from the same starting edge.
2632 Previously we only checked that we weren't threading twice
2633 from the same BB, but that was too restrictive. Imagine a
2634 path that starts from GIMPLE_COND(x_123 == 0,...), where both
2635 edges out of this conditional yield paths that can be
2636 threaded (for example, both lead to an x_123==0 or x_123!=0
2637 conditional further down the line. */
2638 if (visited_starting_edges.contains (entry)
2639 /* We may not want to realize this jump thread path for
2640 various reasons. So check it first. */
2641 || !valid_jump_thread_path (path))
2643 /* Remove invalid jump-thread paths. */
2644 cancel_thread (path, "Avoiding threading twice from same edge");
2645 m_paths.unordered_remove (0);
2646 continue;
2649 unsigned len = path->length ();
2650 edge exit = (*path)[len - 1]->e;
2651 basic_block *region = XNEWVEC (basic_block, len - 1);
2653 for (unsigned int j = 0; j < len - 1; j++)
2654 region[j] = (*path)[j]->e->dest;
2656 if (duplicate_thread_path (entry, exit, region, len - 1, 0))
2658 /* We do not update dominance info. */
2659 free_dominance_info (CDI_DOMINATORS);
2660 visited_starting_edges.add (entry);
2661 retval = true;
2662 m_num_threaded_edges++;
2665 path->release ();
2666 m_paths.unordered_remove (0);
2667 free (region);
2669 return retval;
2672 /* This is the forward threader version of thread_through_all_blocks,
2673 using a custom BB copier. */
2675 bool
2676 fwd_jt_path_registry::update_cfg (bool may_peel_loop_headers)
2678 bool retval = false;
2680 /* Remove any paths that referenced removed edges. */
2681 if (m_removed_edges)
2682 for (unsigned i = 0; i < m_paths.length (); )
2684 unsigned int j;
2685 vec<jump_thread_edge *> *path = m_paths[i];
2687 for (j = 0; j < path->length (); j++)
2689 edge e = (*path)[j]->e;
2690 if (m_removed_edges->find_slot (e, NO_INSERT))
2691 break;
2694 if (j != path->length ())
2696 cancel_thread (path, "Thread references removed edge");
2697 m_paths.unordered_remove (i);
2698 continue;
2700 i++;
2703 auto_bitmap threaded_blocks;
2704 mark_threaded_blocks (threaded_blocks);
2706 initialize_original_copy_tables ();
2708 /* The order in which we process jump threads can be important.
2710 Consider if we have two jump threading paths A and B. If the
2711 target edge of A is the starting edge of B and we thread path A
2712 first, then we create an additional incoming edge into B->dest that
2713 we cannot discover as a jump threading path on this iteration.
2715 If we instead thread B first, then the edge into B->dest will have
2716 already been redirected before we process path A and path A will
2717 natually, with no further work, target the redirected path for B.
2719 An post-order is sufficient here. Compute the ordering first, then
2720 process the blocks. */
2721 if (!bitmap_empty_p (threaded_blocks))
2723 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2724 unsigned int postorder_num = post_order_compute (postorder, false, false);
2725 for (unsigned int i = 0; i < postorder_num; i++)
2727 unsigned int indx = postorder[i];
2728 if (bitmap_bit_p (threaded_blocks, indx))
2730 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, indx);
2731 retval |= thread_block (bb, true);
2734 free (postorder);
2737 /* Then perform the threading through loop headers. We start with the
2738 innermost loop, so that the changes in cfg we perform won't affect
2739 further threading. */
2740 for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
2742 if (!loop->header
2743 || !bitmap_bit_p (threaded_blocks, loop->header->index))
2744 continue;
2746 retval |= thread_through_loop_header (loop, may_peel_loop_headers);
2749 /* All jump threading paths should have been resolved at this
2750 point. Verify that is the case. */
2751 basic_block bb;
2752 FOR_EACH_BB_FN (bb, cfun)
2754 edge_iterator ei;
2755 edge e;
2756 FOR_EACH_EDGE (e, ei, bb->preds)
2757 gcc_assert (e->aux == NULL);
2760 free_original_copy_tables ();
2762 return retval;
2765 bool
2766 jt_path_registry::cancel_invalid_paths (vec<jump_thread_edge *> &path)
2768 gcc_checking_assert (!path.is_empty ());
2769 edge taken_edge = path[path.length () - 1]->e;
2770 loop_p loop = taken_edge->src->loop_father;
2771 bool seen_latch = false;
2772 bool path_crosses_loops = false;
2774 for (unsigned int i = 0; i < path.length (); i++)
2776 edge e = path[i]->e;
2778 if (e == NULL)
2780 // NULL outgoing edges on a path can happen for jumping to a
2781 // constant address.
2782 cancel_thread (&path, "Found NULL edge in jump threading path");
2783 return true;
2786 if (loop->latch == e->src || loop->latch == e->dest)
2787 seen_latch = true;
2789 // The first entry represents the block with an outgoing edge
2790 // that we will redirect to the jump threading path. Thus we
2791 // don't care about that block's loop father.
2792 if ((i > 0 && e->src->loop_father != loop)
2793 || e->dest->loop_father != loop)
2794 path_crosses_loops = true;
2796 if (flag_checking && !m_backedge_threads)
2797 gcc_assert ((path[i]->e->flags & EDGE_DFS_BACK) == 0);
2800 if (cfun->curr_properties & PROP_loop_opts_done)
2801 return false;
2803 if (seen_latch && empty_block_p (loop->latch))
2805 cancel_thread (&path, "Threading through latch before loop opts "
2806 "would create non-empty latch");
2807 return true;
2809 if (path_crosses_loops)
2811 cancel_thread (&path, "Path crosses loops");
2812 return true;
2814 return false;
2817 /* Register a jump threading opportunity. We queue up all the jump
2818 threading opportunities discovered by a pass and update the CFG
2819 and SSA form all at once.
2821 E is the edge we can thread, E2 is the new target edge, i.e., we
2822 are effectively recording that E->dest can be changed to E2->dest
2823 after fixing the SSA graph.
2825 Return TRUE if PATH was successfully threaded. */
2827 bool
2828 jt_path_registry::register_jump_thread (vec<jump_thread_edge *> *path)
2830 gcc_checking_assert (flag_thread_jumps);
2832 if (!dbg_cnt (registered_jump_thread))
2834 path->release ();
2835 return false;
2838 if (cancel_invalid_paths (*path))
2839 return false;
2841 if (dump_file && (dump_flags & TDF_DETAILS))
2842 dump_jump_thread_path (dump_file, *path, true);
2844 m_paths.safe_push (path);
2845 return true;
2848 /* Return how many uses of T there are within BB, as long as there
2849 aren't any uses outside BB. If there are any uses outside BB,
2850 return -1 if there's at most one use within BB, or -2 if there is
2851 more than one use within BB. */
2853 static int
2854 uses_in_bb (tree t, basic_block bb)
2856 int uses = 0;
2857 bool outside_bb = false;
2859 imm_use_iterator iter;
2860 use_operand_p use_p;
2861 FOR_EACH_IMM_USE_FAST (use_p, iter, t)
2863 if (is_gimple_debug (USE_STMT (use_p)))
2864 continue;
2866 if (gimple_bb (USE_STMT (use_p)) != bb)
2867 outside_bb = true;
2868 else
2869 uses++;
2871 if (outside_bb && uses > 1)
2872 return -2;
2875 if (outside_bb)
2876 return -1;
2878 return uses;
2881 /* Starting from the final control flow stmt in BB, assuming it will
2882 be removed, follow uses in to-be-removed stmts back to their defs
2883 and count how many defs are to become dead and be removed as
2884 well. */
2886 unsigned int
2887 estimate_threading_killed_stmts (basic_block bb)
2889 int killed_stmts = 0;
2890 hash_map<tree, int> ssa_remaining_uses;
2891 auto_vec<gimple *, 4> dead_worklist;
2893 /* If the block has only two predecessors, threading will turn phi
2894 dsts into either src, so count them as dead stmts. */
2895 bool drop_all_phis = EDGE_COUNT (bb->preds) == 2;
2897 if (drop_all_phis)
2898 for (gphi_iterator gsi = gsi_start_phis (bb);
2899 !gsi_end_p (gsi); gsi_next (&gsi))
2901 gphi *phi = gsi.phi ();
2902 tree dst = gimple_phi_result (phi);
2904 /* We don't count virtual PHIs as stmts in
2905 record_temporary_equivalences_from_phis. */
2906 if (virtual_operand_p (dst))
2907 continue;
2909 killed_stmts++;
2912 if (gsi_end_p (gsi_last_bb (bb)))
2913 return killed_stmts;
2915 gimple *stmt = gsi_stmt (gsi_last_bb (bb));
2916 if (gimple_code (stmt) != GIMPLE_COND
2917 && gimple_code (stmt) != GIMPLE_GOTO
2918 && gimple_code (stmt) != GIMPLE_SWITCH)
2919 return killed_stmts;
2921 /* The control statement is always dead. */
2922 killed_stmts++;
2923 dead_worklist.quick_push (stmt);
2924 while (!dead_worklist.is_empty ())
2926 stmt = dead_worklist.pop ();
2928 ssa_op_iter iter;
2929 use_operand_p use_p;
2930 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
2932 tree t = USE_FROM_PTR (use_p);
2933 gimple *def = SSA_NAME_DEF_STMT (t);
2935 if (gimple_bb (def) == bb
2936 && (gimple_code (def) != GIMPLE_PHI
2937 || !drop_all_phis)
2938 && !gimple_has_side_effects (def))
2940 int *usesp = ssa_remaining_uses.get (t);
2941 int uses;
2943 if (usesp)
2944 uses = *usesp;
2945 else
2946 uses = uses_in_bb (t, bb);
2948 gcc_assert (uses);
2950 /* Don't bother recording the expected use count if we
2951 won't find any further uses within BB. */
2952 if (!usesp && (uses < -1 || uses > 1))
2954 usesp = &ssa_remaining_uses.get_or_insert (t);
2955 *usesp = uses;
2958 if (uses < 0)
2959 continue;
2961 --uses;
2962 if (usesp)
2963 *usesp = uses;
2965 if (!uses)
2967 killed_stmts++;
2968 if (usesp)
2969 ssa_remaining_uses.remove (t);
2970 if (gimple_code (def) != GIMPLE_PHI)
2971 dead_worklist.safe_push (def);
2977 if (dump_file)
2978 fprintf (dump_file, "threading bb %i kills %i stmts\n",
2979 bb->index, killed_stmts);
2981 return killed_stmts;