]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-threadupdate.c
tree-ssa.h: Remove all #include's
[thirdparty/gcc.git] / gcc / tree-ssa-threadupdate.c
CommitLineData
56b043c8 1/* Thread edges through blocks and update the control flow and SSA graphs.
d1e082c2 2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
56b043c8
JL
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
9dcd6f09 8the Free Software Foundation; either version 3, or (at your option)
56b043c8
JL
9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
56b043c8
JL
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
56b043c8
JL
23#include "tree.h"
24#include "flags.h"
56b043c8 25#include "basic-block.h"
56b043c8 26#include "function.h"
442b4905
AM
27#include "gimple.h"
28#include "gimple-ssa.h"
29#include "tree-phinodes.h"
7a300452 30#include "tree-ssa.h"
5254eac4 31#include "tree-ssa-threadupdate.h"
7ee2468b 32#include "dumpfile.h"
d38ffc55 33#include "cfgloop.h"
0823efed 34#include "hash-table.h"
01e127b1 35#include "dbgcnt.h"
56b043c8
JL
36
37/* Given a block B, update the CFG and SSA graph to reflect redirecting
38 one or more in-edges to B to instead reach the destination of an
39 out-edge from B while preserving any side effects in B.
40
454ff5cb 41 i.e., given A->B and B->C, change A->B to be A->C yet still preserve the
56b043c8
JL
42 side effects of executing B.
43
44 1. Make a copy of B (including its outgoing edges and statements). Call
45 the copy B'. Note B' has no incoming edges or PHIs at this time.
46
47 2. Remove the control statement at the end of B' and all outgoing edges
48 except B'->C.
49
50 3. Add a new argument to each PHI in C with the same value as the existing
51 argument associated with edge B->C. Associate the new PHI arguments
52 with the edge B'->C.
53
54 4. For each PHI in B, find or create a PHI in B' with an identical
d4a9b3a3 55 PHI_RESULT. Add an argument to the PHI in B' which has the same
56b043c8
JL
56 value as the PHI in B associated with the edge A->B. Associate
57 the new argument in the PHI in B' with the edge A->B.
58
59 5. Change the edge A->B to A->B'.
60
61 5a. This automatically deletes any PHI arguments associated with the
62 edge A->B in B.
63
64 5b. This automatically associates each new argument added in step 4
65 with the edge A->B'.
66
67 6. Repeat for other incoming edges into B.
68
69 7. Put the duplicated resources in B and all the B' blocks into SSA form.
70
71 Note that block duplication can be minimized by first collecting the
fa10beec 72 set of unique destination blocks that the incoming edges should
633c9126
JL
73 be threaded to.
74
aee2d611 75 Block duplication can be further minimized by using B instead of
633c9126
JL
76 creating B' for one destination if all edges into B are going to be
77 threaded to a successor of B. We had code to do this at one time, but
78 I'm not convinced it is correct with the changes to avoid mucking up
79 the loop structure (which may cancel threading requests, thus a block
80 which we thought was going to become unreachable may still be reachable).
81 This code was also going to get ugly with the introduction of the ability
aee2d611 82 for a single jump thread request to bypass multiple blocks.
56b043c8 83
1983ac12
JL
84 We further reduce the number of edges and statements we create by
85 not copying all the outgoing edges and the control statement in
86 step #1. We instead create a template block without the outgoing
87 edges and duplicate the template. */
88
89
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.
94
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. */
100
101struct el
102{
103 edge e;
104 struct el *next;
105};
56b043c8
JL
106
107/* Main data structure recording information regarding B's duplicate
108 blocks. */
109
1983ac12
JL
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
e7a531ae 113 can be naturally implemented with a hash table. */
1983ac12 114
5deac340 115struct redirection_data : typed_free_remove<redirection_data>
56b043c8
JL
116{
117 /* A duplicate of B with the trailing control statement removed and which
118 targets a single successor of B. */
119 basic_block dup_block;
120
1465e5cf
JL
121 /* The jump threading path. */
122 vec<jump_thread_edge *> *path;
1983ac12 123
1465e5cf
JL
124 /* A list of incoming edges which we want to thread to the
125 same path. */
1983ac12 126 struct el *incoming_edges;
5deac340
RG
127
128 /* hash_table support. */
5831a5f0
LC
129 typedef redirection_data value_type;
130 typedef redirection_data compare_type;
131 static inline hashval_t hash (const value_type *);
132 static inline int equal (const value_type *, const compare_type *);
56b043c8
JL
133};
134
1465e5cf
JL
135/* Simple hashing function. For any given incoming edge E, we're going
136 to be most concerned with the final destination of its jump thread
137 path. So hash on the block index of the final edge in the path. */
138
5deac340 139inline hashval_t
5831a5f0 140redirection_data::hash (const value_type *p)
5deac340 141{
1465e5cf
JL
142 vec<jump_thread_edge *> *path = p->path;
143 return path->last ()->e->dest->index;
5deac340
RG
144}
145
1465e5cf
JL
146/* Given two hash table entries, return true if they have the same
147 jump threading path. */
5deac340 148inline int
5831a5f0 149redirection_data::equal (const value_type *p1, const compare_type *p2)
5deac340 150{
1465e5cf
JL
151 vec<jump_thread_edge *> *path1 = p1->path;
152 vec<jump_thread_edge *> *path2 = p2->path;
153
154 if (path1->length () != path2->length ())
155 return false;
156
157 for (unsigned int i = 1; i < path1->length (); i++)
158 {
159 if ((*path1)[i]->type != (*path2)[i]->type
160 || (*path1)[i]->e != (*path2)[i]->e)
161 return false;
162 }
163
164 return true;
5deac340
RG
165}
166
1983ac12 167/* Data structure of information to pass to hash table traversal routines. */
0823efed 168struct ssa_local_info_t
1983ac12
JL
169{
170 /* The current block we are working on. */
171 basic_block bb;
172
173 /* A template copy of BB with no outgoing edges or control statement that
174 we use for creating copies. */
175 basic_block template_block;
d38ffc55
JL
176
177 /* TRUE if we thread one or more jumps, FALSE otherwise. */
178 bool jumps_threaded;
1983ac12 179};
37840132 180
8702a557
JL
181/* Passes which use the jump threading code register jump threading
182 opportunities as they are discovered. We keep the registered
183 jump threading opportunities in this vector as edge pairs
184 (original_edge, target_edge). */
aee2d611 185static vec<vec<jump_thread_edge *> *> paths;
8702a557 186
7134c090
JL
187/* When we start updating the CFG for threading, data necessary for jump
188 threading is attached to the AUX field for the incoming edge. Use these
189 macros to access the underlying structure attached to the AUX field. */
aee2d611 190#define THREAD_PATH(E) ((vec<jump_thread_edge *> *)(E)->aux)
8702a557 191
a4233c29
DN
192/* Jump threading statistics. */
193
194struct thread_stats_d
195{
196 unsigned long num_threaded_edges;
197};
198
199struct thread_stats_d thread_stats;
200
201
e376fe58
JL
202/* Remove the last statement in block BB if it is a control statement
203 Also remove all outgoing edges except the edge which reaches DEST_BB.
204 If DEST_BB is NULL, then remove all outgoing edges. */
56b043c8
JL
205
206static void
e376fe58 207remove_ctrl_stmt_and_useless_edges (basic_block bb, basic_block dest_bb)
56b043c8 208{
726a989a 209 gimple_stmt_iterator gsi;
628f6a4e
BE
210 edge e;
211 edge_iterator ei;
56b043c8 212
726a989a 213 gsi = gsi_last_bb (bb);
56b043c8 214
e376fe58 215 /* If the duplicate ends with a control statement, then remove it.
56b043c8 216
e376fe58
JL
217 Note that if we are duplicating the template block rather than the
218 original basic block, then the duplicate might not have any real
219 statements in it. */
726a989a
RB
220 if (!gsi_end_p (gsi)
221 && gsi_stmt (gsi)
222 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
223 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
224 || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH))
225 gsi_remove (&gsi, true);
56b043c8 226
628f6a4e 227 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
56b043c8 228 {
56b043c8 229 if (e->dest != dest_bb)
d0d2cc21 230 remove_edge (e);
628f6a4e
BE
231 else
232 ei_next (&ei);
56b043c8 233 }
56b043c8
JL
234}
235
a91926b9 236/* Create a duplicate of BB. Record the duplicate block in RD. */
56b043c8
JL
237
238static void
239create_block_for_threading (basic_block bb, struct redirection_data *rd)
240{
7134c090
JL
241 edge_iterator ei;
242 edge e;
243
56b043c8
JL
244 /* We can use the generic block duplication code and simply remove
245 the stuff we do not need. */
b9a66240 246 rd->dup_block = duplicate_block (bb, NULL, NULL);
56b043c8 247
7134c090
JL
248 FOR_EACH_EDGE (e, ei, rd->dup_block->succs)
249 e->aux = NULL;
250
15db5571
JH
251 /* Zero out the profile, since the block is unreachable for now. */
252 rd->dup_block->frequency = 0;
253 rd->dup_block->count = 0;
56b043c8
JL
254}
255
0823efed
DN
256/* Main data structure to hold information for duplicates of BB. */
257
5deac340 258static hash_table <redirection_data> redirection_data;
0823efed 259
1983ac12
JL
260/* Given an outgoing edge E lookup and return its entry in our hash table.
261
262 If INSERT is true, then we insert the entry into the hash table if
263 it is not already present. INCOMING_EDGE is added to the list of incoming
264 edges associated with E in the hash table. */
265
266static struct redirection_data *
361b51c0 267lookup_redirection_data (edge e, enum insert_option insert)
1983ac12 268{
0823efed 269 struct redirection_data **slot;
1983ac12 270 struct redirection_data *elt;
aee2d611 271 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1983ac12
JL
272
273 /* Build a hash table element so we can see if E is already
274 in the table. */
5ed6ace5 275 elt = XNEW (struct redirection_data);
1465e5cf 276 elt->path = path;
1983ac12 277 elt->dup_block = NULL;
1983ac12
JL
278 elt->incoming_edges = NULL;
279
0823efed 280 slot = redirection_data.find_slot (elt, insert);
1983ac12
JL
281
282 /* This will only happen if INSERT is false and the entry is not
283 in the hash table. */
284 if (slot == NULL)
285 {
286 free (elt);
287 return NULL;
288 }
289
290 /* This will only happen if E was not in the hash table and
291 INSERT is true. */
292 if (*slot == NULL)
293 {
0823efed 294 *slot = elt;
5ed6ace5 295 elt->incoming_edges = XNEW (struct el);
361b51c0 296 elt->incoming_edges->e = e;
1983ac12
JL
297 elt->incoming_edges->next = NULL;
298 return elt;
299 }
300 /* E was in the hash table. */
301 else
302 {
303 /* Free ELT as we do not need it anymore, we will extract the
304 relevant entry from the hash table itself. */
305 free (elt);
306
307 /* Get the entry stored in the hash table. */
0823efed 308 elt = *slot;
1983ac12
JL
309
310 /* If insertion was requested, then we need to add INCOMING_EDGE
311 to the list of incoming edges associated with E. */
312 if (insert)
313 {
5ed6ace5 314 struct el *el = XNEW (struct el);
1983ac12 315 el->next = elt->incoming_edges;
361b51c0 316 el->e = e;
1983ac12
JL
317 elt->incoming_edges = el;
318 }
319
320 return elt;
321 }
322}
323
361b51c0
JL
324/* For each PHI in BB, copy the argument associated with SRC_E to TGT_E. */
325
326static void
327copy_phi_args (basic_block bb, edge src_e, edge tgt_e)
328{
329 gimple_stmt_iterator gsi;
330 int src_indx = src_e->dest_idx;
331
332 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
333 {
334 gimple phi = gsi_stmt (gsi);
335 source_location locus = gimple_phi_arg_location (phi, src_indx);
9e227d60 336 add_phi_arg (phi, gimple_phi_arg_def (phi, src_indx), tgt_e, locus);
361b51c0
JL
337 }
338}
339
340/* We have recently made a copy of ORIG_BB, including its outgoing
341 edges. The copy is NEW_BB. Every PHI node in every direct successor of
342 ORIG_BB has a new argument associated with edge from NEW_BB to the
343 successor. Initialize the PHI argument so that it is equal to the PHI
344 argument associated with the edge from ORIG_BB to the successor. */
345
346static void
347update_destination_phis (basic_block orig_bb, basic_block new_bb)
348{
349 edge_iterator ei;
350 edge e;
351
352 FOR_EACH_EDGE (e, ei, orig_bb->succs)
353 {
354 edge e2 = find_edge (new_bb, e->dest);
355 copy_phi_args (e->dest, e, e2);
356 }
357}
358
1983ac12
JL
359/* Given a duplicate block and its single destination (both stored
360 in RD). Create an edge between the duplicate and its single
361 destination.
362
363 Add an additional argument to any PHI nodes at the single
364 destination. */
365
366static void
520af9ec
JL
367create_edge_and_update_destination_phis (struct redirection_data *rd,
368 basic_block bb)
1983ac12 369{
1465e5cf 370 edge e = make_edge (bb, rd->path->last ()->e->dest, EDGE_FALLTHRU);
1983ac12 371
aa2645a0 372 rescan_loop_exit (e, true, false);
d416304e 373 e->probability = REG_BR_PROB_BASE;
520af9ec 374 e->count = bb->count;
7134c090 375
aee2d611
JL
376 /* We have to copy path -- which means creating a new vector as well
377 as all the jump_thread_edge entries. */
1465e5cf 378 if (rd->path->last ()->e->aux)
7134c090 379 {
1465e5cf 380 vec<jump_thread_edge *> *path = THREAD_PATH (rd->path->last ()->e);
aee2d611
JL
381 vec<jump_thread_edge *> *copy = new vec<jump_thread_edge *> ();
382
383 /* Sadly, the elements of the vector are pointers and need to
384 be copied as well. */
385 for (unsigned int i = 0; i < path->length (); i++)
386 {
387 jump_thread_edge *x
388 = new jump_thread_edge ((*path)[i]->e, (*path)[i]->type);
389 copy->safe_push (x);
390 }
391 e->aux = (void *)copy;
7134c090
JL
392 }
393 else
394 {
395 e->aux = NULL;
396 }
d416304e 397
1983ac12
JL
398 /* If there are any PHI nodes at the destination of the outgoing edge
399 from the duplicate block, then we will need to add a new argument
400 to them. The argument should have the same value as the argument
401 associated with the outgoing edge stored in RD. */
1465e5cf 402 copy_phi_args (e->dest, rd->path->last ()->e, e);
361b51c0
JL
403}
404
405/* Wire up the outgoing edges from the duplicate block and
406 update any PHIs as needed. */
0823efed
DN
407void
408ssa_fix_duplicate_block_edges (struct redirection_data *rd,
409 ssa_local_info_t *local_info)
361b51c0 410{
aee2d611
JL
411 edge e = rd->incoming_edges->e;
412 vec<jump_thread_edge *> *path = THREAD_PATH (e);
413
361b51c0
JL
414 /* If we were threading through an joiner block, then we want
415 to keep its control statement and redirect an outgoing edge.
416 Else we want to remove the control statement & edges, then create
417 a new outgoing edge. In both cases we may need to update PHIs. */
aee2d611 418 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1983ac12 419 {
361b51c0
JL
420 edge victim;
421 edge e2;
361b51c0
JL
422
423 /* This updates the PHIs at the destination of the duplicate
424 block. */
425 update_destination_phis (local_info->bb, rd->dup_block);
f5045c96 426
361b51c0
JL
427 /* Find the edge from the duplicate block to the block we're
428 threading through. That's the edge we want to redirect. */
aee2d611
JL
429 victim = find_edge (rd->dup_block, (*path)[1]->e->dest);
430 e2 = redirect_edge_and_branch (victim, path->last ()->e->dest);
431 e2->count = path->last ()->e->count;
361b51c0 432
ad3577df
JL
433 /* If we redirected the edge, then we need to copy PHI arguments
434 at the target. If the edge already existed (e2 != victim case),
435 then the PHIs in the target already have the correct arguments. */
436 if (e2 == victim)
aee2d611 437 copy_phi_args (e2->dest, path->last ()->e, e2);
361b51c0
JL
438 }
439 else
440 {
441 remove_ctrl_stmt_and_useless_edges (rd->dup_block, NULL);
442 create_edge_and_update_destination_phis (rd, rd->dup_block);
1983ac12
JL
443 }
444}
1983ac12
JL
445/* Hash table traversal callback routine to create duplicate blocks. */
446
0823efed
DN
447int
448ssa_create_duplicates (struct redirection_data **slot,
449 ssa_local_info_t *local_info)
1983ac12 450{
0823efed 451 struct redirection_data *rd = *slot;
1983ac12 452
1983ac12
JL
453 /* Create a template block if we have not done so already. Otherwise
454 use the template to create a new block. */
455 if (local_info->template_block == NULL)
456 {
457 create_block_for_threading (local_info->bb, rd);
458 local_info->template_block = rd->dup_block;
459
460 /* We do not create any outgoing edges for the template. We will
461 take care of that in a later traversal. That way we do not
462 create edges that are going to just be deleted. */
463 }
464 else
465 {
466 create_block_for_threading (local_info->template_block, rd);
467
468 /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
361b51c0 469 block. */
0823efed 470 ssa_fix_duplicate_block_edges (rd, local_info);
1983ac12
JL
471 }
472
473 /* Keep walking the hash table. */
474 return 1;
475}
476
477/* We did not create any outgoing edges for the template block during
478 block creation. This hash table traversal callback creates the
479 outgoing edge for the template block. */
480
0823efed
DN
481inline int
482ssa_fixup_template_block (struct redirection_data **slot,
483 ssa_local_info_t *local_info)
1983ac12 484{
0823efed 485 struct redirection_data *rd = *slot;
1983ac12 486
361b51c0
JL
487 /* If this is the template block halt the traversal after updating
488 it appropriately.
489
490 If we were threading through an joiner block, then we want
491 to keep its control statement and redirect an outgoing edge.
492 Else we want to remove the control statement & edges, then create
493 a new outgoing edge. In both cases we may need to update PHIs. */
1983ac12
JL
494 if (rd->dup_block && rd->dup_block == local_info->template_block)
495 {
0823efed 496 ssa_fix_duplicate_block_edges (rd, local_info);
1983ac12
JL
497 return 0;
498 }
499
500 return 1;
501}
502
503/* Hash table traversal callback to redirect each incoming edge
504 associated with this hash table element to its new destination. */
505
0823efed
DN
506int
507ssa_redirect_edges (struct redirection_data **slot,
508 ssa_local_info_t *local_info)
1983ac12 509{
0823efed 510 struct redirection_data *rd = *slot;
1983ac12
JL
511 struct el *next, *el;
512
513 /* Walk over all the incoming edges associated associated with this
514 hash table entry. */
515 for (el = rd->incoming_edges; el; el = next)
516 {
517 edge e = el->e;
aee2d611 518 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1983ac12
JL
519
520 /* Go ahead and free this element from the list. Doing this now
521 avoids the need for another list walk when we destroy the hash
522 table. */
523 next = el->next;
524 free (el);
525
a4233c29
DN
526 thread_stats.num_threaded_edges++;
527
05357ac3 528 if (rd->dup_block)
1983ac12
JL
529 {
530 edge e2;
531
532 if (dump_file && (dump_flags & TDF_DETAILS))
533 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
534 e->src->index, e->dest->index, rd->dup_block->index);
535
2b151cb2 536 rd->dup_block->count += e->count;
0bed228e
JH
537
538 /* Excessive jump threading may make frequencies large enough so
539 the computation overflows. */
540 if (rd->dup_block->frequency < BB_FREQ_MAX * 2)
541 rd->dup_block->frequency += EDGE_FREQUENCY (e);
05357ac3
TJ
542
543 /* In the case of threading through a joiner block, the outgoing
544 edges from the duplicate block were updated when they were
545 redirected during ssa_fix_duplicate_block_edges. */
aee2d611 546 if ((*path)[1]->type != EDGE_COPY_SRC_JOINER_BLOCK)
05357ac3
TJ
547 EDGE_SUCC (rd->dup_block, 0)->count += e->count;
548
549 /* Redirect the incoming edge (possibly to the joiner block) to the
550 appropriate duplicate block. */
1983ac12 551 e2 = redirect_edge_and_branch (e, rd->dup_block);
b02b9b53 552 gcc_assert (e == e2);
1983ac12 553 flush_pending_stmts (e2);
1983ac12 554 }
7134c090
JL
555
556 /* Go ahead and clear E->aux. It's not needed anymore and failure
557 to clear it will cause all kinds of unpleasant problems later. */
aee2d611
JL
558 for (unsigned int i = 0; i < path->length (); i++)
559 delete (*path)[i];
560 path->release ();
7134c090
JL
561 e->aux = NULL;
562
1983ac12 563 }
d38ffc55
JL
564
565 /* Indicate that we actually threaded one or more jumps. */
566 if (rd->incoming_edges)
567 local_info->jumps_threaded = true;
568
1983ac12
JL
569 return 1;
570}
571
31a9760a
RS
572/* Return true if this block has no executable statements other than
573 a simple ctrl flow instruction. When the number of outgoing edges
574 is one, this is equivalent to a "forwarder" block. */
575
576static bool
b48d0358 577redirection_block_p (basic_block bb)
31a9760a 578{
726a989a 579 gimple_stmt_iterator gsi;
31a9760a
RS
580
581 /* Advance to the first executable statement. */
726a989a
RB
582 gsi = gsi_start_bb (bb);
583 while (!gsi_end_p (gsi)
584 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
b5b8b0ac 585 || is_gimple_debug (gsi_stmt (gsi))
726a989a
RB
586 || gimple_nop_p (gsi_stmt (gsi))))
587 gsi_next (&gsi);
b8698a0f 588
31a9760a 589 /* Check if this is an empty block. */
726a989a 590 if (gsi_end_p (gsi))
31a9760a
RS
591 return true;
592
593 /* Test that we've reached the terminating control statement. */
726a989a
RB
594 return gsi_stmt (gsi)
595 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
596 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
597 || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH);
31a9760a
RS
598}
599
56b043c8
JL
600/* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
601 is reached via one or more specific incoming edges, we know which
602 outgoing edge from BB will be traversed.
603
1983ac12 604 We want to redirect those incoming edges to the target of the
56b043c8
JL
605 appropriate outgoing edge. Doing so avoids a conditional branch
606 and may expose new optimization opportunities. Note that we have
607 to update dominator tree and SSA graph after such changes.
608
6cb38cd4 609 The key to keeping the SSA graph update manageable is to duplicate
2a7e31df 610 the side effects occurring in BB so that those side effects still
56b043c8
JL
611 occur on the paths which bypass BB after redirecting edges.
612
613 We accomplish this by creating duplicates of BB and arranging for
614 the duplicates to unconditionally pass control to one specific
615 successor of BB. We then revector the incoming edges into BB to
616 the appropriate duplicate of BB.
617
b02b9b53
ZD
618 If NOLOOP_ONLY is true, we only perform the threading as long as it
619 does not affect the structure of the loops in a nontrivial way. */
56b043c8 620
d38ffc55 621static bool
b02b9b53 622thread_block (basic_block bb, bool noloop_only)
56b043c8
JL
623{
624 /* E is an incoming edge into BB that we may or may not want to
625 redirect to a duplicate of BB. */
b02b9b53 626 edge e, e2;
628f6a4e 627 edge_iterator ei;
0823efed 628 ssa_local_info_t local_info;
b02b9b53 629 struct loop *loop = bb->loop_father;
d38ffc55 630
1983ac12 631 /* To avoid scanning a linear array for the element we need we instead
e7a531ae 632 use a hash table. For normal code there should be no noticeable
1983ac12
JL
633 difference. However, if we have a block with a large number of
634 incoming and outgoing edges such linear searches can get expensive. */
0823efed 635 redirection_data.create (EDGE_COUNT (bb->succs));
1983ac12 636
b02b9b53
ZD
637 /* If we thread the latch of the loop to its exit, the loop ceases to
638 exist. Make sure we do not restrict ourselves in order to preserve
639 this loop. */
d51157de 640 if (loop->header == bb)
b02b9b53
ZD
641 {
642 e = loop_latch_edge (loop);
aee2d611 643 vec<jump_thread_edge *> *path = THREAD_PATH (e);
7134c090 644
aee2d611 645 if (path)
b02b9b53 646 {
aee2d611
JL
647 for (unsigned int i = 1; i < path->length (); i++)
648 {
649 edge e2 = (*path)[i]->e;
650
651 if (loop_exit_edge_p (loop, e2))
652 {
653 loop->header = NULL;
654 loop->latch = NULL;
655 loops_state_set (LOOPS_NEED_FIXUP);
656 }
657 }
b02b9b53
ZD
658 }
659 }
d38ffc55 660
1983ac12
JL
661 /* Record each unique threaded destination into a hash table for
662 efficient lookups. */
628f6a4e 663 FOR_EACH_EDGE (e, ei, bb->preds)
56b043c8 664 {
7134c090
JL
665 if (e->aux == NULL)
666 continue;
667
aee2d611
JL
668 vec<jump_thread_edge *> *path = THREAD_PATH (e);
669 e2 = path->last ()->e;
581aedec
JL
670 if (!e2 || noloop_only)
671 {
b02b9b53 672 /* If NOLOOP_ONLY is true, we only allow threading through the
aee2d611 673 header of a loop to exit edges.
581aedec
JL
674
675 There are two cases to consider. The first when BB is the
676 loop header. We will attempt to thread this elsewhere, so
677 we can just continue here. */
678
679 if (bb == bb->loop_father->header
361b51c0 680 && (!loop_exit_edge_p (bb->loop_father, e2)
aee2d611 681 || (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK))
581aedec
JL
682 continue;
683
684
685 /* The second occurs when there was loop header buried in a jump
686 threading path. We do not try and thread this elsewhere, so
687 just cancel the jump threading request by clearing the AUX
688 field now. */
9e1376e9
JL
689 if ((bb->loop_father != e2->src->loop_father
690 && !loop_exit_edge_p (e2->src->loop_father, e2))
691 || (e2->src->loop_father != e2->dest->loop_father
692 && !loop_exit_edge_p (e2->src->loop_father, e2)))
581aedec
JL
693 {
694 /* Since this case is not handled by our special code
695 to thread through a loop header, we must explicitly
696 cancel the threading request here. */
aee2d611
JL
697 for (unsigned int i = 0; i < path->length (); i++)
698 delete (*path)[i];
699 path->release ();
581aedec
JL
700 e->aux = NULL;
701 continue;
702 }
703 }
1983ac12 704
520af9ec
JL
705 if (e->dest == e2->src)
706 update_bb_profile_for_threading (e->dest, EDGE_FREQUENCY (e),
aee2d611 707 e->count, (*THREAD_PATH (e))[1]->e);
b02b9b53
ZD
708
709 /* Insert the outgoing edge into the hash table if it is not
710 already in the hash table. */
361b51c0 711 lookup_redirection_data (e, INSERT);
56b043c8
JL
712 }
713
66f97d31
ZD
714 /* We do not update dominance info. */
715 free_dominance_info (CDI_DOMINATORS);
716
12df9a2f
RG
717 /* We know we only thread through the loop header to loop exits.
718 Let the basic block duplication hook know we are not creating
719 a multiple entry loop. */
720 if (noloop_only
721 && bb == bb->loop_father->header)
722 set_loop_copy (bb->loop_father, loop_outer (bb->loop_father));
723
1983ac12 724 /* Now create duplicates of BB.
e376fe58
JL
725
726 Note that for a block with a high outgoing degree we can waste
727 a lot of time and memory creating and destroying useless edges.
728
729 So we first duplicate BB and remove the control structure at the
730 tail of the duplicate as well as all outgoing edges from the
731 duplicate. We then use that duplicate block as a template for
732 the rest of the duplicates. */
1983ac12
JL
733 local_info.template_block = NULL;
734 local_info.bb = bb;
d38ffc55 735 local_info.jumps_threaded = false;
0823efed
DN
736 redirection_data.traverse <ssa_local_info_t *, ssa_create_duplicates>
737 (&local_info);
e376fe58 738
1983ac12
JL
739 /* The template does not have an outgoing edge. Create that outgoing
740 edge and update PHI nodes as the edge's target as necessary.
e376fe58 741
1983ac12
JL
742 We do this after creating all the duplicates to avoid creating
743 unnecessary edges. */
0823efed
DN
744 redirection_data.traverse <ssa_local_info_t *, ssa_fixup_template_block>
745 (&local_info);
e376fe58 746
1983ac12
JL
747 /* The hash table traversals above created the duplicate blocks (and the
748 statements within the duplicate blocks). This loop creates PHI nodes for
749 the duplicated blocks and redirects the incoming edges into BB to reach
750 the duplicates of BB. */
0823efed
DN
751 redirection_data.traverse <ssa_local_info_t *, ssa_redirect_edges>
752 (&local_info);
56b043c8 753
37840132 754 /* Done with this block. Clear REDIRECTION_DATA. */
0823efed 755 redirection_data.dispose ();
d38ffc55 756
12df9a2f
RG
757 if (noloop_only
758 && bb == bb->loop_father->header)
759 set_loop_copy (bb->loop_father, NULL);
760
d38ffc55
JL
761 /* Indicate to our caller whether or not any jumps were threaded. */
762 return local_info.jumps_threaded;
56b043c8
JL
763}
764
7134c090
JL
765/* Threads edge E through E->dest to the edge THREAD_TARGET (E). Returns the
766 copy of E->dest created during threading, or E->dest if it was not necessary
b02b9b53
ZD
767 to copy it (E is its single predecessor). */
768
769static basic_block
770thread_single_edge (edge e)
771{
772 basic_block bb = e->dest;
b02b9b53 773 struct redirection_data rd;
aee2d611
JL
774 vec<jump_thread_edge *> *path = THREAD_PATH (e);
775 edge eto = (*path)[1]->e;
b02b9b53 776
aee2d611
JL
777 for (unsigned int i = 0; i < path->length (); i++)
778 delete (*path)[i];
779 delete path;
b02b9b53
ZD
780 e->aux = NULL;
781
782 thread_stats.num_threaded_edges++;
783
784 if (single_pred_p (bb))
785 {
786 /* If BB has just a single predecessor, we should only remove the
787 control statements at its end, and successors except for ETO. */
788 remove_ctrl_stmt_and_useless_edges (bb, eto->dest);
d9eb5318
RG
789
790 /* And fixup the flags on the single remaining edge. */
791 eto->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
792 eto->flags |= EDGE_FALLTHRU;
793
b02b9b53
ZD
794 return bb;
795 }
796
797 /* Otherwise, we need to create a copy. */
520af9ec
JL
798 if (e->dest == eto->src)
799 update_bb_profile_for_threading (bb, EDGE_FREQUENCY (e), e->count, eto);
b02b9b53 800
1465e5cf
JL
801 vec<jump_thread_edge *> *npath = new vec<jump_thread_edge *> ();
802 jump_thread_edge *x = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
803 npath->safe_push (x);
804
805 x = new jump_thread_edge (eto, EDGE_COPY_SRC_BLOCK);
806 npath->safe_push (x);
807 rd.path = npath;
b02b9b53
ZD
808
809 create_block_for_threading (bb, &rd);
a91926b9 810 remove_ctrl_stmt_and_useless_edges (rd.dup_block, NULL);
520af9ec 811 create_edge_and_update_destination_phis (&rd, rd.dup_block);
b02b9b53
ZD
812
813 if (dump_file && (dump_flags & TDF_DETAILS))
814 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
815 e->src->index, e->dest->index, rd.dup_block->index);
816
817 rd.dup_block->count = e->count;
818 rd.dup_block->frequency = EDGE_FREQUENCY (e);
819 single_succ_edge (rd.dup_block)->count = e->count;
820 redirect_edge_and_branch (e, rd.dup_block);
821 flush_pending_stmts (e);
822
823 return rd.dup_block;
824}
825
826/* Callback for dfs_enumerate_from. Returns true if BB is different
827 from STOP and DBDS_CE_STOP. */
828
829static basic_block dbds_ce_stop;
830static bool
ed7a4b4b 831dbds_continue_enumeration_p (const_basic_block bb, const void *stop)
b02b9b53 832{
ed7a4b4b 833 return (bb != (const_basic_block) stop
b02b9b53
ZD
834 && bb != dbds_ce_stop);
835}
836
837/* Evaluates the dominance relationship of latch of the LOOP and BB, and
838 returns the state. */
839
840enum bb_dom_status
841{
842 /* BB does not dominate latch of the LOOP. */
843 DOMST_NONDOMINATING,
844 /* The LOOP is broken (there is no path from the header to its latch. */
845 DOMST_LOOP_BROKEN,
846 /* BB dominates the latch of the LOOP. */
847 DOMST_DOMINATING
848};
849
850static enum bb_dom_status
851determine_bb_domination_status (struct loop *loop, basic_block bb)
852{
853 basic_block *bblocks;
854 unsigned nblocks, i;
855 bool bb_reachable = false;
856 edge_iterator ei;
857 edge e;
858
520af9ec
JL
859 /* This function assumes BB is a successor of LOOP->header.
860 If that is not the case return DOMST_NONDOMINATING which
861 is always safe. */
b02b9b53
ZD
862 {
863 bool ok = false;
864
865 FOR_EACH_EDGE (e, ei, bb->preds)
866 {
867 if (e->src == loop->header)
868 {
869 ok = true;
870 break;
871 }
872 }
873
520af9ec
JL
874 if (!ok)
875 return DOMST_NONDOMINATING;
b02b9b53 876 }
b02b9b53
ZD
877
878 if (bb == loop->latch)
879 return DOMST_DOMINATING;
880
881 /* Check that BB dominates LOOP->latch, and that it is back-reachable
882 from it. */
883
884 bblocks = XCNEWVEC (basic_block, loop->num_nodes);
885 dbds_ce_stop = loop->header;
886 nblocks = dfs_enumerate_from (loop->latch, 1, dbds_continue_enumeration_p,
887 bblocks, loop->num_nodes, bb);
888 for (i = 0; i < nblocks; i++)
889 FOR_EACH_EDGE (e, ei, bblocks[i]->preds)
890 {
891 if (e->src == loop->header)
892 {
893 free (bblocks);
894 return DOMST_NONDOMINATING;
895 }
896 if (e->src == bb)
897 bb_reachable = true;
898 }
899
900 free (bblocks);
901 return (bb_reachable ? DOMST_DOMINATING : DOMST_LOOP_BROKEN);
902}
903
18ce8171
RG
904/* Return true if BB is part of the new pre-header that is created
905 when threading the latch to DATA. */
906
907static bool
908def_split_header_continue_p (const_basic_block bb, const void *data)
909{
910 const_basic_block new_header = (const_basic_block) data;
535269f4
JH
911 const struct loop *l;
912
913 if (bb == new_header
914 || loop_depth (bb->loop_father) < loop_depth (new_header->loop_father))
915 return false;
916 for (l = bb->loop_father; l; l = loop_outer (l))
917 if (l == new_header->loop_father)
918 return true;
919 return false;
18ce8171
RG
920}
921
b02b9b53
ZD
922/* Thread jumps through the header of LOOP. Returns true if cfg changes.
923 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading from entry edges
924 to the inside of the loop. */
925
926static bool
927thread_through_loop_header (struct loop *loop, bool may_peel_loop_headers)
928{
929 basic_block header = loop->header;
930 edge e, tgt_edge, latch = loop_latch_edge (loop);
931 edge_iterator ei;
932 basic_block tgt_bb, atgt_bb;
933 enum bb_dom_status domst;
934
935 /* We have already threaded through headers to exits, so all the threading
936 requests now are to the inside of the loop. We need to avoid creating
937 irreducible regions (i.e., loops with more than one entry block), and
938 also loop with several latch edges, or new subloops of the loop (although
939 there are cases where it might be appropriate, it is difficult to decide,
940 and doing it wrongly may confuse other optimizers).
941
942 We could handle more general cases here. However, the intention is to
943 preserve some information about the loop, which is impossible if its
944 structure changes significantly, in a way that is not well understood.
945 Thus we only handle few important special cases, in which also updating
946 of the loop-carried information should be feasible:
947
948 1) Propagation of latch edge to a block that dominates the latch block
949 of a loop. This aims to handle the following idiom:
950
951 first = 1;
952 while (1)
953 {
954 if (first)
955 initialize;
956 first = 0;
957 body;
958 }
959
960 After threading the latch edge, this becomes
961
962 first = 1;
963 if (first)
964 initialize;
965 while (1)
966 {
967 first = 0;
968 body;
969 }
970
971 The original header of the loop is moved out of it, and we may thread
972 the remaining edges through it without further constraints.
973
974 2) All entry edges are propagated to a single basic block that dominates
975 the latch block of the loop. This aims to handle the following idiom
976 (normally created for "for" loops):
977
978 i = 0;
979 while (1)
980 {
981 if (i >= 100)
982 break;
983 body;
984 i++;
985 }
986
987 This becomes
988
989 i = 0;
990 while (1)
991 {
992 body;
993 i++;
994 if (i >= 100)
995 break;
996 }
997 */
998
999 /* Threading through the header won't improve the code if the header has just
1000 one successor. */
1001 if (single_succ_p (header))
1002 goto fail;
1003
1004 if (latch->aux)
1005 {
aee2d611
JL
1006 vec<jump_thread_edge *> *path = THREAD_PATH (latch);
1007 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
361b51c0 1008 goto fail;
aee2d611 1009 tgt_edge = (*path)[1]->e;
b02b9b53
ZD
1010 tgt_bb = tgt_edge->dest;
1011 }
1012 else if (!may_peel_loop_headers
1013 && !redirection_block_p (loop->header))
1014 goto fail;
1015 else
1016 {
1017 tgt_bb = NULL;
1018 tgt_edge = NULL;
1019 FOR_EACH_EDGE (e, ei, header->preds)
1020 {
1021 if (!e->aux)
1022 {
1023 if (e == latch)
1024 continue;
1025
1026 /* If latch is not threaded, and there is a header
1027 edge that is not threaded, we would create loop
1028 with multiple entries. */
1029 goto fail;
1030 }
1031
aee2d611
JL
1032 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1033
1034 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
361b51c0 1035 goto fail;
aee2d611 1036 tgt_edge = (*path)[1]->e;
b02b9b53
ZD
1037 atgt_bb = tgt_edge->dest;
1038 if (!tgt_bb)
1039 tgt_bb = atgt_bb;
1040 /* Two targets of threading would make us create loop
1041 with multiple entries. */
1042 else if (tgt_bb != atgt_bb)
1043 goto fail;
1044 }
1045
1046 if (!tgt_bb)
1047 {
1048 /* There are no threading requests. */
1049 return false;
1050 }
1051
1052 /* Redirecting to empty loop latch is useless. */
1053 if (tgt_bb == loop->latch
1054 && empty_block_p (loop->latch))
1055 goto fail;
1056 }
1057
1058 /* The target block must dominate the loop latch, otherwise we would be
1059 creating a subloop. */
1060 domst = determine_bb_domination_status (loop, tgt_bb);
1061 if (domst == DOMST_NONDOMINATING)
1062 goto fail;
1063 if (domst == DOMST_LOOP_BROKEN)
1064 {
1065 /* If the loop ceased to exist, mark it as such, and thread through its
1066 original header. */
1067 loop->header = NULL;
1068 loop->latch = NULL;
7d776ee2 1069 loops_state_set (LOOPS_NEED_FIXUP);
b02b9b53
ZD
1070 return thread_block (header, false);
1071 }
1072
1073 if (tgt_bb->loop_father->header == tgt_bb)
1074 {
1075 /* If the target of the threading is a header of a subloop, we need
1076 to create a preheader for it, so that the headers of the two loops
1077 do not merge. */
1078 if (EDGE_COUNT (tgt_bb->preds) > 2)
1079 {
1080 tgt_bb = create_preheader (tgt_bb->loop_father, 0);
1081 gcc_assert (tgt_bb != NULL);
1082 }
1083 else
1084 tgt_bb = split_edge (tgt_edge);
1085 }
b8698a0f 1086
b02b9b53
ZD
1087 if (latch->aux)
1088 {
18ce8171
RG
1089 basic_block *bblocks;
1090 unsigned nblocks, i;
1091
07b1bf20
RG
1092 /* First handle the case latch edge is redirected. We are copying
1093 the loop header but not creating a multiple entry loop. Make the
1094 cfg manipulation code aware of that fact. */
1095 set_loop_copy (loop, loop);
b02b9b53 1096 loop->latch = thread_single_edge (latch);
07b1bf20 1097 set_loop_copy (loop, NULL);
b02b9b53
ZD
1098 gcc_assert (single_succ (loop->latch) == tgt_bb);
1099 loop->header = tgt_bb;
1100
18ce8171
RG
1101 /* Remove the new pre-header blocks from our loop. */
1102 bblocks = XCNEWVEC (basic_block, loop->num_nodes);
1103 nblocks = dfs_enumerate_from (header, 0, def_split_header_continue_p,
1104 bblocks, loop->num_nodes, tgt_bb);
1105 for (i = 0; i < nblocks; i++)
1779dc34
RG
1106 if (bblocks[i]->loop_father == loop)
1107 {
1108 remove_bb_from_loops (bblocks[i]);
1109 add_bb_to_loop (bblocks[i], loop_outer (loop));
1110 }
18ce8171
RG
1111 free (bblocks);
1112
a8886f7d
RG
1113 /* If the new header has multiple latches mark it so. */
1114 FOR_EACH_EDGE (e, ei, loop->header->preds)
1115 if (e->src->loop_father == loop
1116 && e->src != loop->latch)
1117 {
1118 loop->latch = NULL;
1119 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
1120 }
1121
18ce8171
RG
1122 /* Cancel remaining threading requests that would make the
1123 loop a multiple entry loop. */
1124 FOR_EACH_EDGE (e, ei, header->preds)
1125 {
1126 edge e2;
a8886f7d 1127
18ce8171
RG
1128 if (e->aux == NULL)
1129 continue;
1130
aee2d611
JL
1131 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1132 e2 = path->last ()->e;
18ce8171
RG
1133
1134 if (e->src->loop_father != e2->dest->loop_father
1135 && e2->dest != loop->header)
1136 {
aee2d611
JL
1137 for (unsigned int i = 0; i < path->length (); i++)
1138 delete (*path)[i];
1139 path->release ();
18ce8171
RG
1140 e->aux = NULL;
1141 }
1142 }
1143
b02b9b53
ZD
1144 /* Thread the remaining edges through the former header. */
1145 thread_block (header, false);
1146 }
1147 else
1148 {
1149 basic_block new_preheader;
1150
1151 /* Now consider the case entry edges are redirected to the new entry
1152 block. Remember one entry edge, so that we can find the new
7134c090 1153 preheader (its destination after threading). */
b02b9b53
ZD
1154 FOR_EACH_EDGE (e, ei, header->preds)
1155 {
1156 if (e->aux)
1157 break;
1158 }
1159
1160 /* The duplicate of the header is the new preheader of the loop. Ensure
1161 that it is placed correctly in the loop hierarchy. */
561e8a90 1162 set_loop_copy (loop, loop_outer (loop));
b02b9b53
ZD
1163
1164 thread_block (header, false);
561e8a90 1165 set_loop_copy (loop, NULL);
b02b9b53
ZD
1166 new_preheader = e->dest;
1167
1168 /* Create the new latch block. This is always necessary, as the latch
1169 must have only a single successor, but the original header had at
1170 least two successors. */
1171 loop->latch = NULL;
1172 mfb_kj_edge = single_succ_edge (new_preheader);
1173 loop->header = mfb_kj_edge->dest;
1174 latch = make_forwarder_block (tgt_bb, mfb_keep_just, NULL);
1175 loop->header = latch->dest;
1176 loop->latch = latch->src;
1177 }
b8698a0f 1178
b02b9b53
ZD
1179 return true;
1180
1181fail:
1182 /* We failed to thread anything. Cancel the requests. */
1183 FOR_EACH_EDGE (e, ei, header->preds)
1184 {
aee2d611
JL
1185 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1186
1187 if (path)
1188 {
1189 for (unsigned int i = 0; i < path->length (); i++)
1190 delete (*path)[i];
1191 path->release ();
1192 e->aux = NULL;
1193 }
b02b9b53
ZD
1194 }
1195 return false;
1196}
1197
8d34e421
JL
1198/* E1 and E2 are edges into the same basic block. Return TRUE if the
1199 PHI arguments associated with those edges are equal or there are no
1200 PHI arguments, otherwise return FALSE. */
1201
1202static bool
1203phi_args_equal_on_edges (edge e1, edge e2)
1204{
1205 gimple_stmt_iterator gsi;
1206 int indx1 = e1->dest_idx;
1207 int indx2 = e2->dest_idx;
1208
1209 for (gsi = gsi_start_phis (e1->dest); !gsi_end_p (gsi); gsi_next (&gsi))
1210 {
1211 gimple phi = gsi_stmt (gsi);
1212
1213 if (!operand_equal_p (gimple_phi_arg_def (phi, indx1),
1214 gimple_phi_arg_def (phi, indx2), 0))
1215 return false;
1216 }
1217 return true;
1218}
1219
8702a557 1220/* Walk through the registered jump threads and convert them into a
c0220ea4 1221 form convenient for this pass.
8702a557
JL
1222
1223 Any block which has incoming edges threaded to outgoing edges
1224 will have its entry in THREADED_BLOCK set.
56b043c8 1225
8702a557
JL
1226 Any threaded edge will have its new outgoing edge stored in the
1227 original edge's AUX field.
56b043c8 1228
8702a557
JL
1229 This form avoids the need to walk all the edges in the CFG to
1230 discover blocks which need processing and avoids unnecessary
1231 hash table lookups to map from threaded edge to new target. */
56b043c8 1232
8702a557
JL
1233static void
1234mark_threaded_blocks (bitmap threaded_blocks)
1235{
1236 unsigned int i;
b02b9b53
ZD
1237 bitmap_iterator bi;
1238 bitmap tmp = BITMAP_ALLOC (NULL);
1239 basic_block bb;
1240 edge e;
1241 edge_iterator ei;
8702a557 1242
34554d1a
JL
1243 /* It is possible to have jump threads in which one is a subpath
1244 of the other. ie, (A, B), (B, C), (C, D) where B is a joiner
1245 block and (B, C), (C, D) where no joiner block exists.
1246
1247 When this occurs ignore the jump thread request with the joiner
1248 block. It's totally subsumed by the simpler jump thread request.
1249
b5c4ff78 1250 This results in less block copying, simpler CFGs. More importantly,
34554d1a
JL
1251 when we duplicate the joiner block, B, in this case we will create
1252 a new threading opportunity that we wouldn't be able to optimize
aee2d611 1253 until the next jump threading iteration.
34554d1a
JL
1254
1255 So first convert the jump thread requests which do not require a
1256 joiner block. */
aee2d611 1257 for (i = 0; i < paths.length (); i++)
8702a557 1258 {
aee2d611 1259 vec<jump_thread_edge *> *path = paths[i];
8702a557 1260
aee2d611 1261 if ((*path)[1]->type != EDGE_COPY_SRC_JOINER_BLOCK)
34554d1a 1262 {
aee2d611
JL
1263 edge e = (*path)[0]->e;
1264 e->aux = (void *)path;
34554d1a
JL
1265 bitmap_set_bit (tmp, e->dest->index);
1266 }
b02b9b53
ZD
1267 }
1268
b5c4ff78
JL
1269 /* Now iterate again, converting cases where we want to thread
1270 through a joiner block, but only if no other edge on the path
1271 already has a jump thread attached to it. */
aee2d611 1272 for (i = 0; i < paths.length (); i++)
34554d1a 1273 {
aee2d611 1274 vec<jump_thread_edge *> *path = paths[i];
34554d1a 1275
b5c4ff78
JL
1276
1277 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
34554d1a 1278 {
b5c4ff78
JL
1279 unsigned int j;
1280
1281 for (j = 0; j < path->length (); j++)
1282 if ((*path)[j]->e->aux != NULL)
1283 break;
1284
1285 /* If we iterated through the entire path without exiting the loop,
1286 then we are good to go, attach the path to the starting edge. */
1287 if (j == path->length ())
1288 {
1289 edge e = (*path)[0]->e;
1290 e->aux = path;
1291 bitmap_set_bit (tmp, e->dest->index);
1292 }
34554d1a
JL
1293 }
1294 }
1295
8d34e421
JL
1296 /* If we have a joiner block (J) which has two successors S1 and S2 and
1297 we are threading though S1 and the final destination of the thread
1298 is S2, then we must verify that any PHI nodes in S2 have the same
1299 PHI arguments for the edge J->S2 and J->S1->...->S2.
1300
1301 We used to detect this prior to registering the jump thread, but
1302 that prohibits propagation of edge equivalences into non-dominated
aee2d611 1303 PHI nodes as the equivalency test might occur before propagation.
8d34e421
JL
1304
1305 This works for now, but will need improvement as part of the FSA
aee2d611 1306 optimization.
8d34e421
JL
1307
1308 Note since we've moved the thread request data to the edges,
1309 we have to iterate on those rather than the threaded_edges vector. */
1310 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
1311 {
1312 bb = BASIC_BLOCK (i);
1313 FOR_EACH_EDGE (e, ei, bb->preds)
1314 {
1315 if (e->aux)
1316 {
aee2d611
JL
1317 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1318 bool have_joiner = ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK);
8d34e421
JL
1319
1320 if (have_joiner)
1321 {
1322 basic_block joiner = e->dest;
aee2d611 1323 edge final_edge = path->last ()->e;
8d34e421
JL
1324 basic_block final_dest = final_edge->dest;
1325 edge e2 = find_edge (joiner, final_dest);
1326
1327 if (e2 && !phi_args_equal_on_edges (e2, final_edge))
1328 {
aee2d611
JL
1329 for (unsigned int i = 0; i < path->length (); i++)
1330 delete (*path)[i];
1331 path->release ();
8d34e421
JL
1332 e->aux = NULL;
1333 }
1334 }
1335 }
1336 }
1337 }
1338
34554d1a 1339
b02b9b53
ZD
1340 /* If optimizing for size, only thread through block if we don't have
1341 to duplicate it or it's an otherwise empty redirection block. */
efd8f750 1342 if (optimize_function_for_size_p (cfun))
b02b9b53
ZD
1343 {
1344 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
1345 {
1346 bb = BASIC_BLOCK (i);
1347 if (EDGE_COUNT (bb->preds) > 1
1348 && !redirection_block_p (bb))
1349 {
1350 FOR_EACH_EDGE (e, ei, bb->preds)
7134c090 1351 {
aee2d611
JL
1352 if (e->aux)
1353 {
1354 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1355 for (unsigned int i = 0; i < path->length (); i++)
1356 delete (*path)[i];
1357 path->release ();
1358 e->aux = NULL;
1359 }
7134c090 1360 }
b02b9b53
ZD
1361 }
1362 else
1363 bitmap_set_bit (threaded_blocks, i);
1364 }
8702a557 1365 }
b02b9b53
ZD
1366 else
1367 bitmap_copy (threaded_blocks, tmp);
1368
ef3cfba2
JL
1369 /* Look for jump threading paths which cross multiple loop headers.
1370
1371 The code to thread through loop headers will change the CFG in ways
1372 that break assumptions made by the loop optimization code.
1373
1374 We don't want to blindly cancel the requests. We can instead do better
1375 by trimming off the end of the jump thread path. */
1376 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
1377 {
1378 basic_block bb = BASIC_BLOCK (i);
1379 FOR_EACH_EDGE (e, ei, bb->preds)
1380 {
1381 if (e->aux)
1382 {
1383 vec<jump_thread_edge *> *path = THREAD_PATH (e);
1384
1385 /* Basically we're looking for a situation where we can see
1386 3 or more loop structures on a jump threading path. */
1387
1388 struct loop *first_father = (*path)[0]->e->src->loop_father;
1389 struct loop *second_father = NULL;
1390 for (unsigned int i = 0; i < path->length (); i++)
1391 {
1392 /* See if this is a loop father we have not seen before. */
1393 if ((*path)[i]->e->dest->loop_father != first_father
1394 && (*path)[i]->e->dest->loop_father != second_father)
1395 {
1396 /* We've already seen two loop fathers, so we
1397 need to trim this jump threading path. */
1398 if (second_father != NULL)
1399 {
1400 /* Trim from entry I onwards. */
1401 for (unsigned int j = i; j < path->length (); j++)
1402 delete (*path)[j];
1403 path->truncate (i);
1404
1405 /* Now that we've truncated the path, make sure
1406 what's left is still valid. We need at least
1407 two edges on the path and the last edge can not
1408 be a joiner. This should never happen, but let's
1409 be safe. */
1410 if (path->length () < 2
1411 || (path->last ()->type
1412 == EDGE_COPY_SRC_JOINER_BLOCK))
1413 {
1414 for (unsigned int i = 0; i < path->length (); i++)
1415 delete (*path)[i];
1416 path->release ();
1417 e->aux = NULL;
1418 }
1419 break;
1420 }
1421 else
1422 {
1423 second_father = (*path)[i]->e->dest->loop_father;
1424 }
1425 }
1426 }
1427 }
1428 }
1429 }
1430
c3284718 1431 BITMAP_FREE (tmp);
8702a557
JL
1432}
1433
1434
1435/* Walk through all blocks and thread incoming edges to the appropriate
1436 outgoing edge for each edge pair recorded in THREADED_EDGES.
56b043c8
JL
1437
1438 It is the caller's responsibility to fix the dominance information
1439 and rewrite duplicated SSA_NAMEs back into SSA form.
1440
b02b9b53
ZD
1441 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading edges through
1442 loop headers if it does not simplify the loop.
1443
471854f8 1444 Returns true if one or more edges were threaded, false otherwise. */
56b043c8
JL
1445
1446bool
b02b9b53 1447thread_through_all_blocks (bool may_peel_loop_headers)
56b043c8 1448{
56b043c8 1449 bool retval = false;
4aab792d
KH
1450 unsigned int i;
1451 bitmap_iterator bi;
8702a557 1452 bitmap threaded_blocks;
b02b9b53
ZD
1453 struct loop *loop;
1454 loop_iterator li;
8702a557 1455
d51157de
ZD
1456 /* We must know about loops in order to preserve them. */
1457 gcc_assert (current_loops != NULL);
1458
aee2d611 1459 if (!paths.exists ())
8702a557 1460 return false;
56b043c8 1461
8702a557 1462 threaded_blocks = BITMAP_ALLOC (NULL);
a4233c29 1463 memset (&thread_stats, 0, sizeof (thread_stats));
d38ffc55 1464
8702a557
JL
1465 mark_threaded_blocks (threaded_blocks);
1466
561e8a90 1467 initialize_original_copy_tables ();
b02b9b53
ZD
1468
1469 /* First perform the threading requests that do not affect
1470 loop structure. */
4aab792d 1471 EXECUTE_IF_SET_IN_BITMAP (threaded_blocks, 0, i, bi)
56b043c8 1472 {
4aab792d
KH
1473 basic_block bb = BASIC_BLOCK (i);
1474
1475 if (EDGE_COUNT (bb->preds) > 0)
b02b9b53
ZD
1476 retval |= thread_block (bb, true);
1477 }
1478
1479 /* Then perform the threading through loop headers. We start with the
1480 innermost loop, so that the changes in cfg we perform won't affect
1481 further threading. */
d51157de 1482 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
b02b9b53 1483 {
d51157de
ZD
1484 if (!loop->header
1485 || !bitmap_bit_p (threaded_blocks, loop->header->index))
1486 continue;
b02b9b53 1487
d51157de 1488 retval |= thread_through_loop_header (loop, may_peel_loop_headers);
56b043c8 1489 }
d38ffc55 1490
01902653
RG
1491 statistics_counter_event (cfun, "Jumps threaded",
1492 thread_stats.num_threaded_edges);
a4233c29 1493
561e8a90
ZD
1494 free_original_copy_tables ();
1495
8702a557
JL
1496 BITMAP_FREE (threaded_blocks);
1497 threaded_blocks = NULL;
aee2d611 1498 paths.release ();
b02b9b53 1499
592c303d 1500 if (retval)
f87000d0 1501 loops_state_set (LOOPS_NEED_FIXUP);
592c303d 1502
56b043c8
JL
1503 return retval;
1504}
8702a557 1505
5254eac4
JL
1506/* Dump a jump threading path, including annotations about each
1507 edge in the path. */
1508
1509static void
1510dump_jump_thread_path (FILE *dump_file, vec<jump_thread_edge *> path)
1511{
1512 fprintf (dump_file,
1513 " Registering jump thread: (%d, %d) incoming edge; ",
1514 path[0]->e->src->index, path[0]->e->dest->index);
1515
1516 for (unsigned int i = 1; i < path.length (); i++)
1517 {
1518 /* We can get paths with a NULL edge when the final destination
1519 of a jump thread turns out to be a constant address. We dump
1520 those paths when debugging, so we have to be prepared for that
1521 possibility here. */
1522 if (path[i]->e == NULL)
1523 continue;
1524
1525 if (path[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
1526 fprintf (dump_file, " (%d, %d) joiner; ",
1527 path[i]->e->src->index, path[i]->e->dest->index);
1528 if (path[i]->type == EDGE_COPY_SRC_BLOCK)
1529 fprintf (dump_file, " (%d, %d) normal;",
1530 path[i]->e->src->index, path[i]->e->dest->index);
1531 if (path[i]->type == EDGE_NO_COPY_SRC_BLOCK)
1532 fprintf (dump_file, " (%d, %d) nocopy;",
1533 path[i]->e->src->index, path[i]->e->dest->index);
1534 }
1535 fputc ('\n', dump_file);
1536}
1537
8702a557
JL
1538/* Register a jump threading opportunity. We queue up all the jump
1539 threading opportunities discovered by a pass and update the CFG
1540 and SSA form all at once.
1541
fa10beec 1542 E is the edge we can thread, E2 is the new target edge, i.e., we
8702a557
JL
1543 are effectively recording that E->dest can be changed to E2->dest
1544 after fixing the SSA graph. */
1545
1546void
aee2d611 1547register_jump_thread (vec<jump_thread_edge *> *path)
8702a557 1548{
01e127b1
JL
1549 if (!dbg_cnt (registered_jump_thread))
1550 {
1551 for (unsigned int i = 0; i < path->length (); i++)
1552 delete (*path)[i];
1553 path->release ();
1554 return;
1555 }
1556
5254eac4
JL
1557 /* First make sure there are no NULL outgoing edges on the jump threading
1558 path. That can happen for jumping to a constant address. */
aee2d611
JL
1559 for (unsigned int i = 0; i < path->length (); i++)
1560 if ((*path)[i]->e == NULL)
5254eac4
JL
1561 {
1562 if (dump_file && (dump_flags & TDF_DETAILS))
1563 {
1564 fprintf (dump_file,
1565 "Found NULL edge in jump threading path. Cancelling jump thread:\n");
aee2d611 1566 dump_jump_thread_path (dump_file, *path);
5254eac4 1567 }
aee2d611
JL
1568
1569 for (unsigned int i = 0; i < path->length (); i++)
1570 delete (*path)[i];
1571 path->release ();
5254eac4
JL
1572 return;
1573 }
b9ebee5d 1574
3b18bc42 1575 if (dump_file && (dump_flags & TDF_DETAILS))
aee2d611 1576 dump_jump_thread_path (dump_file, *path);
3b18bc42 1577
aee2d611
JL
1578 if (!paths.exists ())
1579 paths.create (5);
3b18bc42 1580
aee2d611 1581 paths.safe_push (path);
8702a557 1582}