0
|
1 /* Thread edges through blocks and update the control flow and SSA graphs.
|
|
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation,
|
|
3 Inc.
|
|
4
|
|
5 This file is part of GCC.
|
|
6
|
|
7 GCC is free software; you can redistribute it and/or modify
|
|
8 it under the terms of the GNU General Public License as published by
|
|
9 the Free Software Foundation; either version 3, or (at your option)
|
|
10 any later version.
|
|
11
|
|
12 GCC is distributed in the hope that it will be useful,
|
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 GNU General Public License for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with GCC; see the file COPYING3. If not see
|
|
19 <http://www.gnu.org/licenses/>. */
|
|
20
|
|
21 #include "config.h"
|
|
22 #include "system.h"
|
|
23 #include "coretypes.h"
|
|
24 #include "tm.h"
|
|
25 #include "tree.h"
|
|
26 #include "flags.h"
|
|
27 #include "rtl.h"
|
|
28 #include "tm_p.h"
|
|
29 #include "ggc.h"
|
|
30 #include "basic-block.h"
|
|
31 #include "output.h"
|
|
32 #include "expr.h"
|
|
33 #include "function.h"
|
|
34 #include "diagnostic.h"
|
|
35 #include "tree-flow.h"
|
|
36 #include "tree-dump.h"
|
|
37 #include "tree-pass.h"
|
|
38 #include "cfgloop.h"
|
|
39
|
|
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.
|
|
43
|
|
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.
|
|
46
|
|
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.
|
|
49
|
|
50 2. Remove the control statement at the end of B' and all outgoing edges
|
|
51 except B'->C.
|
|
52
|
|
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.
|
|
56
|
|
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.
|
|
61
|
|
62 5. Change the edge A->B to A->B'.
|
|
63
|
|
64 5a. This automatically deletes any PHI arguments associated with the
|
|
65 edge A->B in B.
|
|
66
|
|
67 5b. This automatically associates each new argument added in step 4
|
|
68 with the edge A->B'.
|
|
69
|
|
70 6. Repeat for other incoming edges into B.
|
|
71
|
|
72 7. Put the duplicated resources in B and all the B' blocks into SSA form.
|
|
73
|
|
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. Block duplication can be further minimized by using
|
|
77 B instead of creating B' for one destination if all edges into B are
|
|
78 going to be threaded to a successor of B.
|
|
79
|
|
80 We further reduce the number of edges and statements we create by
|
|
81 not copying all the outgoing edges and the control statement in
|
|
82 step #1. We instead create a template block without the outgoing
|
|
83 edges and duplicate the template. */
|
|
84
|
|
85
|
|
86 /* Steps #5 and #6 of the above algorithm are best implemented by walking
|
|
87 all the incoming edges which thread to the same destination edge at
|
|
88 the same time. That avoids lots of table lookups to get information
|
|
89 for the destination edge.
|
|
90
|
|
91 To realize that implementation we create a list of incoming edges
|
|
92 which thread to the same outgoing edge. Thus to implement steps
|
|
93 #5 and #6 we traverse our hash table of outgoing edge information.
|
|
94 For each entry we walk the list of incoming edges which thread to
|
|
95 the current outgoing edge. */
|
|
96
|
|
97 struct el
|
|
98 {
|
|
99 edge e;
|
|
100 struct el *next;
|
|
101 };
|
|
102
|
|
103 /* Main data structure recording information regarding B's duplicate
|
|
104 blocks. */
|
|
105
|
|
106 /* We need to efficiently record the unique thread destinations of this
|
|
107 block and specific information associated with those destinations. We
|
|
108 may have many incoming edges threaded to the same outgoing edge. This
|
|
109 can be naturally implemented with a hash table. */
|
|
110
|
|
111 struct redirection_data
|
|
112 {
|
|
113 /* A duplicate of B with the trailing control statement removed and which
|
|
114 targets a single successor of B. */
|
|
115 basic_block dup_block;
|
|
116
|
|
117 /* An outgoing edge from B. DUP_BLOCK will have OUTGOING_EDGE->dest as
|
|
118 its single successor. */
|
|
119 edge outgoing_edge;
|
|
120
|
|
121 /* A list of incoming edges which we want to thread to
|
|
122 OUTGOING_EDGE->dest. */
|
|
123 struct el *incoming_edges;
|
|
124
|
|
125 /* Flag indicating whether or not we should create a duplicate block
|
|
126 for this thread destination. This is only true if we are threading
|
|
127 all incoming edges and thus are using BB itself as a duplicate block. */
|
|
128 bool do_not_duplicate;
|
|
129 };
|
|
130
|
|
131 /* Main data structure to hold information for duplicates of BB. */
|
|
132 static htab_t redirection_data;
|
|
133
|
|
134 /* Data structure of information to pass to hash table traversal routines. */
|
|
135 struct local_info
|
|
136 {
|
|
137 /* The current block we are working on. */
|
|
138 basic_block bb;
|
|
139
|
|
140 /* A template copy of BB with no outgoing edges or control statement that
|
|
141 we use for creating copies. */
|
|
142 basic_block template_block;
|
|
143
|
|
144 /* TRUE if we thread one or more jumps, FALSE otherwise. */
|
|
145 bool jumps_threaded;
|
|
146 };
|
|
147
|
|
148 /* Passes which use the jump threading code register jump threading
|
|
149 opportunities as they are discovered. We keep the registered
|
|
150 jump threading opportunities in this vector as edge pairs
|
|
151 (original_edge, target_edge). */
|
|
152 static VEC(edge,heap) *threaded_edges;
|
|
153
|
|
154
|
|
155 /* Jump threading statistics. */
|
|
156
|
|
157 struct thread_stats_d
|
|
158 {
|
|
159 unsigned long num_threaded_edges;
|
|
160 };
|
|
161
|
|
162 struct thread_stats_d thread_stats;
|
|
163
|
|
164
|
|
165 /* Remove the last statement in block BB if it is a control statement
|
|
166 Also remove all outgoing edges except the edge which reaches DEST_BB.
|
|
167 If DEST_BB is NULL, then remove all outgoing edges. */
|
|
168
|
|
169 static void
|
|
170 remove_ctrl_stmt_and_useless_edges (basic_block bb, basic_block dest_bb)
|
|
171 {
|
|
172 gimple_stmt_iterator gsi;
|
|
173 edge e;
|
|
174 edge_iterator ei;
|
|
175
|
|
176 gsi = gsi_last_bb (bb);
|
|
177
|
|
178 /* If the duplicate ends with a control statement, then remove it.
|
|
179
|
|
180 Note that if we are duplicating the template block rather than the
|
|
181 original basic block, then the duplicate might not have any real
|
|
182 statements in it. */
|
|
183 if (!gsi_end_p (gsi)
|
|
184 && gsi_stmt (gsi)
|
|
185 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
|
|
186 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
|
|
187 || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH))
|
|
188 gsi_remove (&gsi, true);
|
|
189
|
|
190 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
|
|
191 {
|
|
192 if (e->dest != dest_bb)
|
|
193 remove_edge (e);
|
|
194 else
|
|
195 ei_next (&ei);
|
|
196 }
|
|
197 }
|
|
198
|
|
199 /* Create a duplicate of BB which only reaches the destination of the edge
|
|
200 stored in RD. Record the duplicate block in RD. */
|
|
201
|
|
202 static void
|
|
203 create_block_for_threading (basic_block bb, struct redirection_data *rd)
|
|
204 {
|
|
205 /* We can use the generic block duplication code and simply remove
|
|
206 the stuff we do not need. */
|
|
207 rd->dup_block = duplicate_block (bb, NULL, NULL);
|
|
208
|
|
209 /* Zero out the profile, since the block is unreachable for now. */
|
|
210 rd->dup_block->frequency = 0;
|
|
211 rd->dup_block->count = 0;
|
|
212
|
|
213 /* The call to duplicate_block will copy everything, including the
|
|
214 useless COND_EXPR or SWITCH_EXPR at the end of BB. We just remove
|
|
215 the useless COND_EXPR or SWITCH_EXPR here rather than having a
|
|
216 specialized block copier. We also remove all outgoing edges
|
|
217 from the duplicate block. The appropriate edge will be created
|
|
218 later. */
|
|
219 remove_ctrl_stmt_and_useless_edges (rd->dup_block, NULL);
|
|
220 }
|
|
221
|
|
222 /* Hashing and equality routines for our hash table. */
|
|
223 static hashval_t
|
|
224 redirection_data_hash (const void *p)
|
|
225 {
|
|
226 edge e = ((const struct redirection_data *)p)->outgoing_edge;
|
|
227 return e->dest->index;
|
|
228 }
|
|
229
|
|
230 static int
|
|
231 redirection_data_eq (const void *p1, const void *p2)
|
|
232 {
|
|
233 edge e1 = ((const struct redirection_data *)p1)->outgoing_edge;
|
|
234 edge e2 = ((const struct redirection_data *)p2)->outgoing_edge;
|
|
235
|
|
236 return e1 == e2;
|
|
237 }
|
|
238
|
|
239 /* Given an outgoing edge E lookup and return its entry in our hash table.
|
|
240
|
|
241 If INSERT is true, then we insert the entry into the hash table if
|
|
242 it is not already present. INCOMING_EDGE is added to the list of incoming
|
|
243 edges associated with E in the hash table. */
|
|
244
|
|
245 static struct redirection_data *
|
|
246 lookup_redirection_data (edge e, edge incoming_edge, enum insert_option insert)
|
|
247 {
|
|
248 void **slot;
|
|
249 struct redirection_data *elt;
|
|
250
|
|
251 /* Build a hash table element so we can see if E is already
|
|
252 in the table. */
|
|
253 elt = XNEW (struct redirection_data);
|
|
254 elt->outgoing_edge = e;
|
|
255 elt->dup_block = NULL;
|
|
256 elt->do_not_duplicate = false;
|
|
257 elt->incoming_edges = NULL;
|
|
258
|
|
259 slot = htab_find_slot (redirection_data, elt, insert);
|
|
260
|
|
261 /* This will only happen if INSERT is false and the entry is not
|
|
262 in the hash table. */
|
|
263 if (slot == NULL)
|
|
264 {
|
|
265 free (elt);
|
|
266 return NULL;
|
|
267 }
|
|
268
|
|
269 /* This will only happen if E was not in the hash table and
|
|
270 INSERT is true. */
|
|
271 if (*slot == NULL)
|
|
272 {
|
|
273 *slot = (void *)elt;
|
|
274 elt->incoming_edges = XNEW (struct el);
|
|
275 elt->incoming_edges->e = incoming_edge;
|
|
276 elt->incoming_edges->next = NULL;
|
|
277 return elt;
|
|
278 }
|
|
279 /* E was in the hash table. */
|
|
280 else
|
|
281 {
|
|
282 /* Free ELT as we do not need it anymore, we will extract the
|
|
283 relevant entry from the hash table itself. */
|
|
284 free (elt);
|
|
285
|
|
286 /* Get the entry stored in the hash table. */
|
|
287 elt = (struct redirection_data *) *slot;
|
|
288
|
|
289 /* If insertion was requested, then we need to add INCOMING_EDGE
|
|
290 to the list of incoming edges associated with E. */
|
|
291 if (insert)
|
|
292 {
|
|
293 struct el *el = XNEW (struct el);
|
|
294 el->next = elt->incoming_edges;
|
|
295 el->e = incoming_edge;
|
|
296 elt->incoming_edges = el;
|
|
297 }
|
|
298
|
|
299 return elt;
|
|
300 }
|
|
301 }
|
|
302
|
|
303 /* Given a duplicate block and its single destination (both stored
|
|
304 in RD). Create an edge between the duplicate and its single
|
|
305 destination.
|
|
306
|
|
307 Add an additional argument to any PHI nodes at the single
|
|
308 destination. */
|
|
309
|
|
310 static void
|
|
311 create_edge_and_update_destination_phis (struct redirection_data *rd)
|
|
312 {
|
|
313 edge e = make_edge (rd->dup_block, rd->outgoing_edge->dest, EDGE_FALLTHRU);
|
|
314 gimple_stmt_iterator gsi;
|
|
315
|
|
316 rescan_loop_exit (e, true, false);
|
|
317 e->probability = REG_BR_PROB_BASE;
|
|
318 e->count = rd->dup_block->count;
|
|
319 e->aux = rd->outgoing_edge->aux;
|
|
320
|
|
321 /* If there are any PHI nodes at the destination of the outgoing edge
|
|
322 from the duplicate block, then we will need to add a new argument
|
|
323 to them. The argument should have the same value as the argument
|
|
324 associated with the outgoing edge stored in RD. */
|
|
325 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
|
|
326 {
|
|
327 gimple phi = gsi_stmt (gsi);
|
|
328
|
|
329 int indx = rd->outgoing_edge->dest_idx;
|
|
330 add_phi_arg (phi, gimple_phi_arg_def (phi, indx), e);
|
|
331 }
|
|
332 }
|
|
333
|
|
334 /* Hash table traversal callback routine to create duplicate blocks. */
|
|
335
|
|
336 static int
|
|
337 create_duplicates (void **slot, void *data)
|
|
338 {
|
|
339 struct redirection_data *rd = (struct redirection_data *) *slot;
|
|
340 struct local_info *local_info = (struct local_info *)data;
|
|
341
|
|
342 /* If this entry should not have a duplicate created, then there's
|
|
343 nothing to do. */
|
|
344 if (rd->do_not_duplicate)
|
|
345 return 1;
|
|
346
|
|
347 /* Create a template block if we have not done so already. Otherwise
|
|
348 use the template to create a new block. */
|
|
349 if (local_info->template_block == NULL)
|
|
350 {
|
|
351 create_block_for_threading (local_info->bb, rd);
|
|
352 local_info->template_block = rd->dup_block;
|
|
353
|
|
354 /* We do not create any outgoing edges for the template. We will
|
|
355 take care of that in a later traversal. That way we do not
|
|
356 create edges that are going to just be deleted. */
|
|
357 }
|
|
358 else
|
|
359 {
|
|
360 create_block_for_threading (local_info->template_block, rd);
|
|
361
|
|
362 /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
|
|
363 block. */
|
|
364 create_edge_and_update_destination_phis (rd);
|
|
365 }
|
|
366
|
|
367 /* Keep walking the hash table. */
|
|
368 return 1;
|
|
369 }
|
|
370
|
|
371 /* We did not create any outgoing edges for the template block during
|
|
372 block creation. This hash table traversal callback creates the
|
|
373 outgoing edge for the template block. */
|
|
374
|
|
375 static int
|
|
376 fixup_template_block (void **slot, void *data)
|
|
377 {
|
|
378 struct redirection_data *rd = (struct redirection_data *) *slot;
|
|
379 struct local_info *local_info = (struct local_info *)data;
|
|
380
|
|
381 /* If this is the template block, then create its outgoing edges
|
|
382 and halt the hash table traversal. */
|
|
383 if (rd->dup_block && rd->dup_block == local_info->template_block)
|
|
384 {
|
|
385 create_edge_and_update_destination_phis (rd);
|
|
386 return 0;
|
|
387 }
|
|
388
|
|
389 return 1;
|
|
390 }
|
|
391
|
|
392 /* Hash table traversal callback to redirect each incoming edge
|
|
393 associated with this hash table element to its new destination. */
|
|
394
|
|
395 static int
|
|
396 redirect_edges (void **slot, void *data)
|
|
397 {
|
|
398 struct redirection_data *rd = (struct redirection_data *) *slot;
|
|
399 struct local_info *local_info = (struct local_info *)data;
|
|
400 struct el *next, *el;
|
|
401
|
|
402 /* Walk over all the incoming edges associated associated with this
|
|
403 hash table entry. */
|
|
404 for (el = rd->incoming_edges; el; el = next)
|
|
405 {
|
|
406 edge e = el->e;
|
|
407
|
|
408 /* Go ahead and free this element from the list. Doing this now
|
|
409 avoids the need for another list walk when we destroy the hash
|
|
410 table. */
|
|
411 next = el->next;
|
|
412 free (el);
|
|
413
|
|
414 /* Go ahead and clear E->aux. It's not needed anymore and failure
|
|
415 to clear it will cause all kinds of unpleasant problems later. */
|
|
416 e->aux = NULL;
|
|
417
|
|
418 thread_stats.num_threaded_edges++;
|
|
419
|
|
420 if (rd->dup_block)
|
|
421 {
|
|
422 edge e2;
|
|
423
|
|
424 if (dump_file && (dump_flags & TDF_DETAILS))
|
|
425 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
|
|
426 e->src->index, e->dest->index, rd->dup_block->index);
|
|
427
|
|
428 rd->dup_block->count += e->count;
|
|
429 rd->dup_block->frequency += EDGE_FREQUENCY (e);
|
|
430 EDGE_SUCC (rd->dup_block, 0)->count += e->count;
|
|
431 /* Redirect the incoming edge to the appropriate duplicate
|
|
432 block. */
|
|
433 e2 = redirect_edge_and_branch (e, rd->dup_block);
|
|
434 gcc_assert (e == e2);
|
|
435 flush_pending_stmts (e2);
|
|
436 }
|
|
437 else
|
|
438 {
|
|
439 if (dump_file && (dump_flags & TDF_DETAILS))
|
|
440 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
|
|
441 e->src->index, e->dest->index, local_info->bb->index);
|
|
442
|
|
443 /* We are using BB as the duplicate. Remove the unnecessary
|
|
444 outgoing edges and statements from BB. */
|
|
445 remove_ctrl_stmt_and_useless_edges (local_info->bb,
|
|
446 rd->outgoing_edge->dest);
|
|
447
|
|
448 /* Fixup the flags on the single remaining edge. */
|
|
449 single_succ_edge (local_info->bb)->flags
|
|
450 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
|
|
451 single_succ_edge (local_info->bb)->flags |= EDGE_FALLTHRU;
|
|
452
|
|
453 /* And adjust count and frequency on BB. */
|
|
454 local_info->bb->count = e->count;
|
|
455 local_info->bb->frequency = EDGE_FREQUENCY (e);
|
|
456 }
|
|
457 }
|
|
458
|
|
459 /* Indicate that we actually threaded one or more jumps. */
|
|
460 if (rd->incoming_edges)
|
|
461 local_info->jumps_threaded = true;
|
|
462
|
|
463 return 1;
|
|
464 }
|
|
465
|
|
466 /* Return true if this block has no executable statements other than
|
|
467 a simple ctrl flow instruction. When the number of outgoing edges
|
|
468 is one, this is equivalent to a "forwarder" block. */
|
|
469
|
|
470 static bool
|
|
471 redirection_block_p (basic_block bb)
|
|
472 {
|
|
473 gimple_stmt_iterator gsi;
|
|
474
|
|
475 /* Advance to the first executable statement. */
|
|
476 gsi = gsi_start_bb (bb);
|
|
477 while (!gsi_end_p (gsi)
|
|
478 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
|
|
479 || gimple_nop_p (gsi_stmt (gsi))))
|
|
480 gsi_next (&gsi);
|
|
481
|
|
482 /* Check if this is an empty block. */
|
|
483 if (gsi_end_p (gsi))
|
|
484 return true;
|
|
485
|
|
486 /* Test that we've reached the terminating control statement. */
|
|
487 return gsi_stmt (gsi)
|
|
488 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
|
|
489 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
|
|
490 || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH);
|
|
491 }
|
|
492
|
|
493 /* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
|
|
494 is reached via one or more specific incoming edges, we know which
|
|
495 outgoing edge from BB will be traversed.
|
|
496
|
|
497 We want to redirect those incoming edges to the target of the
|
|
498 appropriate outgoing edge. Doing so avoids a conditional branch
|
|
499 and may expose new optimization opportunities. Note that we have
|
|
500 to update dominator tree and SSA graph after such changes.
|
|
501
|
|
502 The key to keeping the SSA graph update manageable is to duplicate
|
|
503 the side effects occurring in BB so that those side effects still
|
|
504 occur on the paths which bypass BB after redirecting edges.
|
|
505
|
|
506 We accomplish this by creating duplicates of BB and arranging for
|
|
507 the duplicates to unconditionally pass control to one specific
|
|
508 successor of BB. We then revector the incoming edges into BB to
|
|
509 the appropriate duplicate of BB.
|
|
510
|
|
511 If NOLOOP_ONLY is true, we only perform the threading as long as it
|
|
512 does not affect the structure of the loops in a nontrivial way. */
|
|
513
|
|
514 static bool
|
|
515 thread_block (basic_block bb, bool noloop_only)
|
|
516 {
|
|
517 /* E is an incoming edge into BB that we may or may not want to
|
|
518 redirect to a duplicate of BB. */
|
|
519 edge e, e2;
|
|
520 edge_iterator ei;
|
|
521 struct local_info local_info;
|
|
522 struct loop *loop = bb->loop_father;
|
|
523
|
|
524 /* ALL indicates whether or not all incoming edges into BB should
|
|
525 be threaded to a duplicate of BB. */
|
|
526 bool all = true;
|
|
527
|
|
528 /* To avoid scanning a linear array for the element we need we instead
|
|
529 use a hash table. For normal code there should be no noticeable
|
|
530 difference. However, if we have a block with a large number of
|
|
531 incoming and outgoing edges such linear searches can get expensive. */
|
|
532 redirection_data = htab_create (EDGE_COUNT (bb->succs),
|
|
533 redirection_data_hash,
|
|
534 redirection_data_eq,
|
|
535 free);
|
|
536
|
|
537 /* If we thread the latch of the loop to its exit, the loop ceases to
|
|
538 exist. Make sure we do not restrict ourselves in order to preserve
|
|
539 this loop. */
|
|
540 if (loop->header == bb)
|
|
541 {
|
|
542 e = loop_latch_edge (loop);
|
|
543 e2 = (edge) e->aux;
|
|
544
|
|
545 if (e2 && loop_exit_edge_p (loop, e2))
|
|
546 {
|
|
547 loop->header = NULL;
|
|
548 loop->latch = NULL;
|
|
549 }
|
|
550 }
|
|
551
|
|
552 /* Record each unique threaded destination into a hash table for
|
|
553 efficient lookups. */
|
|
554 FOR_EACH_EDGE (e, ei, bb->preds)
|
|
555 {
|
|
556 e2 = (edge) e->aux;
|
|
557
|
|
558 if (!e2
|
|
559 /* If NOLOOP_ONLY is true, we only allow threading through the
|
|
560 header of a loop to exit edges. */
|
|
561 || (noloop_only
|
|
562 && bb == bb->loop_father->header
|
|
563 && !loop_exit_edge_p (bb->loop_father, e2)))
|
|
564 {
|
|
565 all = false;
|
|
566 continue;
|
|
567 }
|
|
568
|
|
569 update_bb_profile_for_threading (e->dest, EDGE_FREQUENCY (e),
|
|
570 e->count, (edge) e->aux);
|
|
571
|
|
572 /* Insert the outgoing edge into the hash table if it is not
|
|
573 already in the hash table. */
|
|
574 lookup_redirection_data (e2, e, INSERT);
|
|
575 }
|
|
576
|
|
577 /* If we are going to thread all incoming edges to an outgoing edge, then
|
|
578 BB will become unreachable. Rather than just throwing it away, use
|
|
579 it for one of the duplicates. Mark the first incoming edge with the
|
|
580 DO_NOT_DUPLICATE attribute. */
|
|
581 if (all)
|
|
582 {
|
|
583 edge e = (edge) EDGE_PRED (bb, 0)->aux;
|
|
584 lookup_redirection_data (e, NULL, NO_INSERT)->do_not_duplicate = true;
|
|
585 }
|
|
586
|
|
587 /* We do not update dominance info. */
|
|
588 free_dominance_info (CDI_DOMINATORS);
|
|
589
|
|
590 /* Now create duplicates of BB.
|
|
591
|
|
592 Note that for a block with a high outgoing degree we can waste
|
|
593 a lot of time and memory creating and destroying useless edges.
|
|
594
|
|
595 So we first duplicate BB and remove the control structure at the
|
|
596 tail of the duplicate as well as all outgoing edges from the
|
|
597 duplicate. We then use that duplicate block as a template for
|
|
598 the rest of the duplicates. */
|
|
599 local_info.template_block = NULL;
|
|
600 local_info.bb = bb;
|
|
601 local_info.jumps_threaded = false;
|
|
602 htab_traverse (redirection_data, create_duplicates, &local_info);
|
|
603
|
|
604 /* The template does not have an outgoing edge. Create that outgoing
|
|
605 edge and update PHI nodes as the edge's target as necessary.
|
|
606
|
|
607 We do this after creating all the duplicates to avoid creating
|
|
608 unnecessary edges. */
|
|
609 htab_traverse (redirection_data, fixup_template_block, &local_info);
|
|
610
|
|
611 /* The hash table traversals above created the duplicate blocks (and the
|
|
612 statements within the duplicate blocks). This loop creates PHI nodes for
|
|
613 the duplicated blocks and redirects the incoming edges into BB to reach
|
|
614 the duplicates of BB. */
|
|
615 htab_traverse (redirection_data, redirect_edges, &local_info);
|
|
616
|
|
617 /* Done with this block. Clear REDIRECTION_DATA. */
|
|
618 htab_delete (redirection_data);
|
|
619 redirection_data = NULL;
|
|
620
|
|
621 /* Indicate to our caller whether or not any jumps were threaded. */
|
|
622 return local_info.jumps_threaded;
|
|
623 }
|
|
624
|
|
625 /* Threads edge E through E->dest to the edge E->aux. Returns the copy
|
|
626 of E->dest created during threading, or E->dest if it was not necessary
|
|
627 to copy it (E is its single predecessor). */
|
|
628
|
|
629 static basic_block
|
|
630 thread_single_edge (edge e)
|
|
631 {
|
|
632 basic_block bb = e->dest;
|
|
633 edge eto = (edge) e->aux;
|
|
634 struct redirection_data rd;
|
|
635 struct local_info local_info;
|
|
636
|
|
637 e->aux = NULL;
|
|
638
|
|
639 thread_stats.num_threaded_edges++;
|
|
640
|
|
641 if (single_pred_p (bb))
|
|
642 {
|
|
643 /* If BB has just a single predecessor, we should only remove the
|
|
644 control statements at its end, and successors except for ETO. */
|
|
645 remove_ctrl_stmt_and_useless_edges (bb, eto->dest);
|
|
646
|
|
647 /* And fixup the flags on the single remaining edge. */
|
|
648 eto->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
|
|
649 eto->flags |= EDGE_FALLTHRU;
|
|
650
|
|
651 return bb;
|
|
652 }
|
|
653
|
|
654 /* Otherwise, we need to create a copy. */
|
|
655 update_bb_profile_for_threading (bb, EDGE_FREQUENCY (e), e->count, eto);
|
|
656
|
|
657 local_info.bb = bb;
|
|
658 rd.outgoing_edge = eto;
|
|
659
|
|
660 create_block_for_threading (bb, &rd);
|
|
661 create_edge_and_update_destination_phis (&rd);
|
|
662
|
|
663 if (dump_file && (dump_flags & TDF_DETAILS))
|
|
664 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
|
|
665 e->src->index, e->dest->index, rd.dup_block->index);
|
|
666
|
|
667 rd.dup_block->count = e->count;
|
|
668 rd.dup_block->frequency = EDGE_FREQUENCY (e);
|
|
669 single_succ_edge (rd.dup_block)->count = e->count;
|
|
670 redirect_edge_and_branch (e, rd.dup_block);
|
|
671 flush_pending_stmts (e);
|
|
672
|
|
673 return rd.dup_block;
|
|
674 }
|
|
675
|
|
676 /* Callback for dfs_enumerate_from. Returns true if BB is different
|
|
677 from STOP and DBDS_CE_STOP. */
|
|
678
|
|
679 static basic_block dbds_ce_stop;
|
|
680 static bool
|
|
681 dbds_continue_enumeration_p (const_basic_block bb, const void *stop)
|
|
682 {
|
|
683 return (bb != (const_basic_block) stop
|
|
684 && bb != dbds_ce_stop);
|
|
685 }
|
|
686
|
|
687 /* Evaluates the dominance relationship of latch of the LOOP and BB, and
|
|
688 returns the state. */
|
|
689
|
|
690 enum bb_dom_status
|
|
691 {
|
|
692 /* BB does not dominate latch of the LOOP. */
|
|
693 DOMST_NONDOMINATING,
|
|
694 /* The LOOP is broken (there is no path from the header to its latch. */
|
|
695 DOMST_LOOP_BROKEN,
|
|
696 /* BB dominates the latch of the LOOP. */
|
|
697 DOMST_DOMINATING
|
|
698 };
|
|
699
|
|
700 static enum bb_dom_status
|
|
701 determine_bb_domination_status (struct loop *loop, basic_block bb)
|
|
702 {
|
|
703 basic_block *bblocks;
|
|
704 unsigned nblocks, i;
|
|
705 bool bb_reachable = false;
|
|
706 edge_iterator ei;
|
|
707 edge e;
|
|
708
|
|
709 #ifdef ENABLE_CHECKING
|
|
710 /* This function assumes BB is a successor of LOOP->header. */
|
|
711 {
|
|
712 bool ok = false;
|
|
713
|
|
714 FOR_EACH_EDGE (e, ei, bb->preds)
|
|
715 {
|
|
716 if (e->src == loop->header)
|
|
717 {
|
|
718 ok = true;
|
|
719 break;
|
|
720 }
|
|
721 }
|
|
722
|
|
723 gcc_assert (ok);
|
|
724 }
|
|
725 #endif
|
|
726
|
|
727 if (bb == loop->latch)
|
|
728 return DOMST_DOMINATING;
|
|
729
|
|
730 /* Check that BB dominates LOOP->latch, and that it is back-reachable
|
|
731 from it. */
|
|
732
|
|
733 bblocks = XCNEWVEC (basic_block, loop->num_nodes);
|
|
734 dbds_ce_stop = loop->header;
|
|
735 nblocks = dfs_enumerate_from (loop->latch, 1, dbds_continue_enumeration_p,
|
|
736 bblocks, loop->num_nodes, bb);
|
|
737 for (i = 0; i < nblocks; i++)
|
|
738 FOR_EACH_EDGE (e, ei, bblocks[i]->preds)
|
|
739 {
|
|
740 if (e->src == loop->header)
|
|
741 {
|
|
742 free (bblocks);
|
|
743 return DOMST_NONDOMINATING;
|
|
744 }
|
|
745 if (e->src == bb)
|
|
746 bb_reachable = true;
|
|
747 }
|
|
748
|
|
749 free (bblocks);
|
|
750 return (bb_reachable ? DOMST_DOMINATING : DOMST_LOOP_BROKEN);
|
|
751 }
|
|
752
|
|
753 /* Thread jumps through the header of LOOP. Returns true if cfg changes.
|
|
754 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading from entry edges
|
|
755 to the inside of the loop. */
|
|
756
|
|
757 static bool
|
|
758 thread_through_loop_header (struct loop *loop, bool may_peel_loop_headers)
|
|
759 {
|
|
760 basic_block header = loop->header;
|
|
761 edge e, tgt_edge, latch = loop_latch_edge (loop);
|
|
762 edge_iterator ei;
|
|
763 basic_block tgt_bb, atgt_bb;
|
|
764 enum bb_dom_status domst;
|
|
765
|
|
766 /* We have already threaded through headers to exits, so all the threading
|
|
767 requests now are to the inside of the loop. We need to avoid creating
|
|
768 irreducible regions (i.e., loops with more than one entry block), and
|
|
769 also loop with several latch edges, or new subloops of the loop (although
|
|
770 there are cases where it might be appropriate, it is difficult to decide,
|
|
771 and doing it wrongly may confuse other optimizers).
|
|
772
|
|
773 We could handle more general cases here. However, the intention is to
|
|
774 preserve some information about the loop, which is impossible if its
|
|
775 structure changes significantly, in a way that is not well understood.
|
|
776 Thus we only handle few important special cases, in which also updating
|
|
777 of the loop-carried information should be feasible:
|
|
778
|
|
779 1) Propagation of latch edge to a block that dominates the latch block
|
|
780 of a loop. This aims to handle the following idiom:
|
|
781
|
|
782 first = 1;
|
|
783 while (1)
|
|
784 {
|
|
785 if (first)
|
|
786 initialize;
|
|
787 first = 0;
|
|
788 body;
|
|
789 }
|
|
790
|
|
791 After threading the latch edge, this becomes
|
|
792
|
|
793 first = 1;
|
|
794 if (first)
|
|
795 initialize;
|
|
796 while (1)
|
|
797 {
|
|
798 first = 0;
|
|
799 body;
|
|
800 }
|
|
801
|
|
802 The original header of the loop is moved out of it, and we may thread
|
|
803 the remaining edges through it without further constraints.
|
|
804
|
|
805 2) All entry edges are propagated to a single basic block that dominates
|
|
806 the latch block of the loop. This aims to handle the following idiom
|
|
807 (normally created for "for" loops):
|
|
808
|
|
809 i = 0;
|
|
810 while (1)
|
|
811 {
|
|
812 if (i >= 100)
|
|
813 break;
|
|
814 body;
|
|
815 i++;
|
|
816 }
|
|
817
|
|
818 This becomes
|
|
819
|
|
820 i = 0;
|
|
821 while (1)
|
|
822 {
|
|
823 body;
|
|
824 i++;
|
|
825 if (i >= 100)
|
|
826 break;
|
|
827 }
|
|
828 */
|
|
829
|
|
830 /* Threading through the header won't improve the code if the header has just
|
|
831 one successor. */
|
|
832 if (single_succ_p (header))
|
|
833 goto fail;
|
|
834
|
|
835 if (latch->aux)
|
|
836 {
|
|
837 tgt_edge = (edge) latch->aux;
|
|
838 tgt_bb = tgt_edge->dest;
|
|
839 }
|
|
840 else if (!may_peel_loop_headers
|
|
841 && !redirection_block_p (loop->header))
|
|
842 goto fail;
|
|
843 else
|
|
844 {
|
|
845 tgt_bb = NULL;
|
|
846 tgt_edge = NULL;
|
|
847 FOR_EACH_EDGE (e, ei, header->preds)
|
|
848 {
|
|
849 if (!e->aux)
|
|
850 {
|
|
851 if (e == latch)
|
|
852 continue;
|
|
853
|
|
854 /* If latch is not threaded, and there is a header
|
|
855 edge that is not threaded, we would create loop
|
|
856 with multiple entries. */
|
|
857 goto fail;
|
|
858 }
|
|
859
|
|
860 tgt_edge = (edge) e->aux;
|
|
861 atgt_bb = tgt_edge->dest;
|
|
862 if (!tgt_bb)
|
|
863 tgt_bb = atgt_bb;
|
|
864 /* Two targets of threading would make us create loop
|
|
865 with multiple entries. */
|
|
866 else if (tgt_bb != atgt_bb)
|
|
867 goto fail;
|
|
868 }
|
|
869
|
|
870 if (!tgt_bb)
|
|
871 {
|
|
872 /* There are no threading requests. */
|
|
873 return false;
|
|
874 }
|
|
875
|
|
876 /* Redirecting to empty loop latch is useless. */
|
|
877 if (tgt_bb == loop->latch
|
|
878 && empty_block_p (loop->latch))
|
|
879 goto fail;
|
|
880 }
|
|
881
|
|
882 /* The target block must dominate the loop latch, otherwise we would be
|
|
883 creating a subloop. */
|
|
884 domst = determine_bb_domination_status (loop, tgt_bb);
|
|
885 if (domst == DOMST_NONDOMINATING)
|
|
886 goto fail;
|
|
887 if (domst == DOMST_LOOP_BROKEN)
|
|
888 {
|
|
889 /* If the loop ceased to exist, mark it as such, and thread through its
|
|
890 original header. */
|
|
891 loop->header = NULL;
|
|
892 loop->latch = NULL;
|
|
893 return thread_block (header, false);
|
|
894 }
|
|
895
|
|
896 if (tgt_bb->loop_father->header == tgt_bb)
|
|
897 {
|
|
898 /* If the target of the threading is a header of a subloop, we need
|
|
899 to create a preheader for it, so that the headers of the two loops
|
|
900 do not merge. */
|
|
901 if (EDGE_COUNT (tgt_bb->preds) > 2)
|
|
902 {
|
|
903 tgt_bb = create_preheader (tgt_bb->loop_father, 0);
|
|
904 gcc_assert (tgt_bb != NULL);
|
|
905 }
|
|
906 else
|
|
907 tgt_bb = split_edge (tgt_edge);
|
|
908 }
|
|
909
|
|
910 if (latch->aux)
|
|
911 {
|
|
912 /* First handle the case latch edge is redirected. */
|
|
913 loop->latch = thread_single_edge (latch);
|
|
914 gcc_assert (single_succ (loop->latch) == tgt_bb);
|
|
915 loop->header = tgt_bb;
|
|
916
|
|
917 /* Thread the remaining edges through the former header. */
|
|
918 thread_block (header, false);
|
|
919 }
|
|
920 else
|
|
921 {
|
|
922 basic_block new_preheader;
|
|
923
|
|
924 /* Now consider the case entry edges are redirected to the new entry
|
|
925 block. Remember one entry edge, so that we can find the new
|
|
926 preheader (its destination after threading). */
|
|
927 FOR_EACH_EDGE (e, ei, header->preds)
|
|
928 {
|
|
929 if (e->aux)
|
|
930 break;
|
|
931 }
|
|
932
|
|
933 /* The duplicate of the header is the new preheader of the loop. Ensure
|
|
934 that it is placed correctly in the loop hierarchy. */
|
|
935 set_loop_copy (loop, loop_outer (loop));
|
|
936
|
|
937 thread_block (header, false);
|
|
938 set_loop_copy (loop, NULL);
|
|
939 new_preheader = e->dest;
|
|
940
|
|
941 /* Create the new latch block. This is always necessary, as the latch
|
|
942 must have only a single successor, but the original header had at
|
|
943 least two successors. */
|
|
944 loop->latch = NULL;
|
|
945 mfb_kj_edge = single_succ_edge (new_preheader);
|
|
946 loop->header = mfb_kj_edge->dest;
|
|
947 latch = make_forwarder_block (tgt_bb, mfb_keep_just, NULL);
|
|
948 loop->header = latch->dest;
|
|
949 loop->latch = latch->src;
|
|
950 }
|
|
951
|
|
952 return true;
|
|
953
|
|
954 fail:
|
|
955 /* We failed to thread anything. Cancel the requests. */
|
|
956 FOR_EACH_EDGE (e, ei, header->preds)
|
|
957 {
|
|
958 e->aux = NULL;
|
|
959 }
|
|
960 return false;
|
|
961 }
|
|
962
|
|
963 /* Walk through the registered jump threads and convert them into a
|
|
964 form convenient for this pass.
|
|
965
|
|
966 Any block which has incoming edges threaded to outgoing edges
|
|
967 will have its entry in THREADED_BLOCK set.
|
|
968
|
|
969 Any threaded edge will have its new outgoing edge stored in the
|
|
970 original edge's AUX field.
|
|
971
|
|
972 This form avoids the need to walk all the edges in the CFG to
|
|
973 discover blocks which need processing and avoids unnecessary
|
|
974 hash table lookups to map from threaded edge to new target. */
|
|
975
|
|
976 static void
|
|
977 mark_threaded_blocks (bitmap threaded_blocks)
|
|
978 {
|
|
979 unsigned int i;
|
|
980 bitmap_iterator bi;
|
|
981 bitmap tmp = BITMAP_ALLOC (NULL);
|
|
982 basic_block bb;
|
|
983 edge e;
|
|
984 edge_iterator ei;
|
|
985
|
|
986 for (i = 0; i < VEC_length (edge, threaded_edges); i += 2)
|
|
987 {
|
|
988 edge e = VEC_index (edge, threaded_edges, i);
|
|
989 edge e2 = VEC_index (edge, threaded_edges, i + 1);
|
|
990
|
|
991 e->aux = e2;
|
|
992 bitmap_set_bit (tmp, e->dest->index);
|
|
993 }
|
|
994
|
|
995 /* If optimizing for size, only thread through block if we don't have
|
|
996 to duplicate it or it's an otherwise empty redirection block. */
|
|
997 if (optimize_function_for_size_p (cfun))
|
|
998 {
|
|
999 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
|
|
1000 {
|
|
1001 bb = BASIC_BLOCK (i);
|
|
1002 if (EDGE_COUNT (bb->preds) > 1
|
|
1003 && !redirection_block_p (bb))
|
|
1004 {
|
|
1005 FOR_EACH_EDGE (e, ei, bb->preds)
|
|
1006 e->aux = NULL;
|
|
1007 }
|
|
1008 else
|
|
1009 bitmap_set_bit (threaded_blocks, i);
|
|
1010 }
|
|
1011 }
|
|
1012 else
|
|
1013 bitmap_copy (threaded_blocks, tmp);
|
|
1014
|
|
1015 BITMAP_FREE(tmp);
|
|
1016 }
|
|
1017
|
|
1018
|
|
1019 /* Walk through all blocks and thread incoming edges to the appropriate
|
|
1020 outgoing edge for each edge pair recorded in THREADED_EDGES.
|
|
1021
|
|
1022 It is the caller's responsibility to fix the dominance information
|
|
1023 and rewrite duplicated SSA_NAMEs back into SSA form.
|
|
1024
|
|
1025 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading edges through
|
|
1026 loop headers if it does not simplify the loop.
|
|
1027
|
|
1028 Returns true if one or more edges were threaded, false otherwise. */
|
|
1029
|
|
1030 bool
|
|
1031 thread_through_all_blocks (bool may_peel_loop_headers)
|
|
1032 {
|
|
1033 bool retval = false;
|
|
1034 unsigned int i;
|
|
1035 bitmap_iterator bi;
|
|
1036 bitmap threaded_blocks;
|
|
1037 struct loop *loop;
|
|
1038 loop_iterator li;
|
|
1039
|
|
1040 /* We must know about loops in order to preserve them. */
|
|
1041 gcc_assert (current_loops != NULL);
|
|
1042
|
|
1043 if (threaded_edges == NULL)
|
|
1044 return false;
|
|
1045
|
|
1046 threaded_blocks = BITMAP_ALLOC (NULL);
|
|
1047 memset (&thread_stats, 0, sizeof (thread_stats));
|
|
1048
|
|
1049 mark_threaded_blocks (threaded_blocks);
|
|
1050
|
|
1051 initialize_original_copy_tables ();
|
|
1052
|
|
1053 /* First perform the threading requests that do not affect
|
|
1054 loop structure. */
|
|
1055 EXECUTE_IF_SET_IN_BITMAP (threaded_blocks, 0, i, bi)
|
|
1056 {
|
|
1057 basic_block bb = BASIC_BLOCK (i);
|
|
1058
|
|
1059 if (EDGE_COUNT (bb->preds) > 0)
|
|
1060 retval |= thread_block (bb, true);
|
|
1061 }
|
|
1062
|
|
1063 /* Then perform the threading through loop headers. We start with the
|
|
1064 innermost loop, so that the changes in cfg we perform won't affect
|
|
1065 further threading. */
|
|
1066 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
|
|
1067 {
|
|
1068 if (!loop->header
|
|
1069 || !bitmap_bit_p (threaded_blocks, loop->header->index))
|
|
1070 continue;
|
|
1071
|
|
1072 retval |= thread_through_loop_header (loop, may_peel_loop_headers);
|
|
1073 }
|
|
1074
|
|
1075 statistics_counter_event (cfun, "Jumps threaded",
|
|
1076 thread_stats.num_threaded_edges);
|
|
1077
|
|
1078 free_original_copy_tables ();
|
|
1079
|
|
1080 BITMAP_FREE (threaded_blocks);
|
|
1081 threaded_blocks = NULL;
|
|
1082 VEC_free (edge, heap, threaded_edges);
|
|
1083 threaded_edges = NULL;
|
|
1084
|
|
1085 if (retval)
|
|
1086 loops_state_set (LOOPS_NEED_FIXUP);
|
|
1087
|
|
1088 return retval;
|
|
1089 }
|
|
1090
|
|
1091 /* Register a jump threading opportunity. We queue up all the jump
|
|
1092 threading opportunities discovered by a pass and update the CFG
|
|
1093 and SSA form all at once.
|
|
1094
|
|
1095 E is the edge we can thread, E2 is the new target edge, i.e., we
|
|
1096 are effectively recording that E->dest can be changed to E2->dest
|
|
1097 after fixing the SSA graph. */
|
|
1098
|
|
1099 void
|
|
1100 register_jump_thread (edge e, edge e2)
|
|
1101 {
|
|
1102 if (threaded_edges == NULL)
|
|
1103 threaded_edges = VEC_alloc (edge, heap, 10);
|
|
1104
|
|
1105 VEC_safe_push (edge, heap, threaded_edges, e);
|
|
1106 VEC_safe_push (edge, heap, threaded_edges, e2);
|
|
1107 }
|