tree-ssa-threadupdate.c (redirection_data_hash): Use the index of the destination...
[gcc.git] / gcc / tree-ssa-threadupdate.c
1 /* Thread edges through blocks and update the control flow and SSA graphs.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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 "errors.h"
33 #include "expr.h"
34 #include "function.h"
35 #include "diagnostic.h"
36 #include "tree-flow.h"
37 #include "tree-dump.h"
38 #include "tree-pass.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 the 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
145 /* Remove the last statement in block BB if it is a control statement
146 Also remove all outgoing edges except the edge which reaches DEST_BB.
147 If DEST_BB is NULL, then remove all outgoing edges. */
148
149 static void
150 remove_ctrl_stmt_and_useless_edges (basic_block bb, basic_block dest_bb)
151 {
152 block_stmt_iterator bsi;
153 edge e;
154 edge_iterator ei;
155
156 bsi = bsi_last (bb);
157
158 /* If the duplicate ends with a control statement, then remove it.
159
160 Note that if we are duplicating the template block rather than the
161 original basic block, then the duplicate might not have any real
162 statements in it. */
163 if (!bsi_end_p (bsi)
164 && bsi_stmt (bsi)
165 && (TREE_CODE (bsi_stmt (bsi)) == COND_EXPR
166 || TREE_CODE (bsi_stmt (bsi)) == SWITCH_EXPR))
167 bsi_remove (&bsi);
168
169 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
170 {
171 if (e->dest != dest_bb)
172 ssa_remove_edge (e);
173 else
174 ei_next (&ei);
175 }
176 }
177
178 /* Create a duplicate of BB which only reaches the destination of the edge
179 stored in RD. Record the duplicate block in RD. */
180
181 static void
182 create_block_for_threading (basic_block bb, struct redirection_data *rd)
183 {
184 /* We can use the generic block duplication code and simply remove
185 the stuff we do not need. */
186 rd->dup_block = duplicate_block (bb, NULL);
187
188 /* Zero out the profile, since the block is unreachable for now. */
189 rd->dup_block->frequency = 0;
190 rd->dup_block->count = 0;
191
192 /* The call to duplicate_block will copy everything, including the
193 useless COND_EXPR or SWITCH_EXPR at the end of BB. We just remove
194 the useless COND_EXPR or SWITCH_EXPR here rather than having a
195 specialized block copier. We also remove all outgoing edges
196 from the duplicate block. The appropriate edge will be created
197 later. */
198 remove_ctrl_stmt_and_useless_edges (rd->dup_block, NULL);
199 }
200
201 /* Hashing and equality routines for our hash table. */
202 static hashval_t
203 redirection_data_hash (const void *p)
204 {
205 edge e = ((struct redirection_data *)p)->outgoing_edge;
206 return e->dest->index;
207 }
208
209 static int
210 redirection_data_eq (const void *p1, const void *p2)
211 {
212 edge e1 = ((struct redirection_data *)p1)->outgoing_edge;
213 edge e2 = ((struct redirection_data *)p2)->outgoing_edge;
214
215 return e1 == e2;
216 }
217
218 /* Given an outgoing edge E lookup and return its entry in our hash table.
219
220 If INSERT is true, then we insert the entry into the hash table if
221 it is not already present. INCOMING_EDGE is added to the list of incoming
222 edges associated with E in the hash table. */
223
224 static struct redirection_data *
225 lookup_redirection_data (edge e, edge incoming_edge, bool insert)
226 {
227 void **slot;
228 struct redirection_data *elt;
229
230 /* Build a hash table element so we can see if E is already
231 in the table. */
232 elt = xmalloc (sizeof (struct redirection_data));
233 elt->outgoing_edge = e;
234 elt->dup_block = NULL;
235 elt->do_not_duplicate = false;
236 elt->incoming_edges = NULL;
237
238 slot = htab_find_slot (redirection_data, elt, insert);
239
240 /* This will only happen if INSERT is false and the entry is not
241 in the hash table. */
242 if (slot == NULL)
243 {
244 free (elt);
245 return NULL;
246 }
247
248 /* This will only happen if E was not in the hash table and
249 INSERT is true. */
250 if (*slot == NULL)
251 {
252 *slot = (void *)elt;
253 elt->incoming_edges = xmalloc (sizeof (struct el));
254 elt->incoming_edges->e = incoming_edge;
255 elt->incoming_edges->next = NULL;
256 return elt;
257 }
258 /* E was in the hash table. */
259 else
260 {
261 /* Free ELT as we do not need it anymore, we will extract the
262 relevant entry from the hash table itself. */
263 free (elt);
264
265 /* Get the entry stored in the hash table. */
266 elt = (struct redirection_data *) *slot;
267
268 /* If insertion was requested, then we need to add INCOMING_EDGE
269 to the list of incoming edges associated with E. */
270 if (insert)
271 {
272 struct el *el = xmalloc (sizeof (struct el));
273 el->next = elt->incoming_edges;
274 el->e = incoming_edge;
275 elt->incoming_edges = el;
276 }
277
278 return elt;
279 }
280 }
281
282 /* Given a duplicate block and its single destination (both stored
283 in RD). Create an edge between the duplicate and its single
284 destination.
285
286 Add an additional argument to any PHI nodes at the single
287 destination. */
288
289 static void
290 create_edge_and_update_destination_phis (struct redirection_data *rd)
291 {
292 edge e = make_edge (rd->dup_block, rd->outgoing_edge->dest, EDGE_FALLTHRU);
293 tree phi;
294
295 /* If there are any PHI nodes at the destination of the outgoing edge
296 from the duplicate block, then we will need to add a new argument
297 to them. The argument should have the same value as the argument
298 associated with the outgoing edge stored in RD. */
299 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
300 {
301 int indx = phi_arg_from_edge (phi, rd->outgoing_edge);
302 add_phi_arg (&phi, PHI_ARG_DEF_TREE (phi, indx), e);
303 }
304 }
305
306 /* Hash table traversal callback routine to create duplicate blocks. */
307
308 static int
309 create_duplicates (void **slot, void *data)
310 {
311 struct redirection_data *rd = (struct redirection_data *) *slot;
312 struct local_info *local_info = (struct local_info *)data;
313
314 /* If this entry should not have a duplicate created, then there's
315 nothing to do. */
316 if (rd->do_not_duplicate)
317 return 1;
318
319 /* Create a template block if we have not done so already. Otherwise
320 use the template to create a new block. */
321 if (local_info->template_block == NULL)
322 {
323 create_block_for_threading (local_info->bb, rd);
324 local_info->template_block = rd->dup_block;
325
326 /* We do not create any outgoing edges for the template. We will
327 take care of that in a later traversal. That way we do not
328 create edges that are going to just be deleted. */
329 }
330 else
331 {
332 create_block_for_threading (local_info->template_block, rd);
333
334 /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
335 block. */
336 create_edge_and_update_destination_phis (rd);
337 }
338
339 /* Keep walking the hash table. */
340 return 1;
341 }
342
343 /* We did not create any outgoing edges for the template block during
344 block creation. This hash table traversal callback creates the
345 outgoing edge for the template block. */
346
347 static int
348 fixup_template_block (void **slot, void *data)
349 {
350 struct redirection_data *rd = (struct redirection_data *) *slot;
351 struct local_info *local_info = (struct local_info *)data;
352
353 /* If this is the template block, then create its outgoing edges
354 and halt the hash table traversal. */
355 if (rd->dup_block && rd->dup_block == local_info->template_block)
356 {
357 create_edge_and_update_destination_phis (rd);
358 return 0;
359 }
360
361 return 1;
362 }
363
364 /* Hash table traversal callback to redirect each incoming edge
365 associated with this hash table element to its new destination. */
366
367 static int
368 redirect_edges (void **slot, void *data)
369 {
370 struct redirection_data *rd = (struct redirection_data *) *slot;
371 struct local_info *local_info = (struct local_info *)data;
372 struct el *next, *el;
373
374 /* Walk over all the incoming edges associated associated with this
375 hash table entry. */
376 for (el = rd->incoming_edges; el; el = next)
377 {
378 edge e = el->e;
379
380 /* Go ahead and free this element from the list. Doing this now
381 avoids the need for another list walk when we destroy the hash
382 table. */
383 next = el->next;
384 free (el);
385
386 /* Go ahead and clear E->aux. It's not needed anymore and failure
387 to clear it will cause all kinds of unpleasant problems later. */
388 e->aux = NULL;
389
390 if (rd->dup_block)
391 {
392 edge e2;
393
394 if (dump_file && (dump_flags & TDF_DETAILS))
395 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
396 e->src->index, e->dest->index, rd->dup_block->index);
397
398 /* Redirect the incoming edge to the appropriate duplicate
399 block. */
400 e2 = redirect_edge_and_branch (e, rd->dup_block);
401 flush_pending_stmts (e2);
402
403 if ((dump_file && (dump_flags & TDF_DETAILS))
404 && e->src != e2->src)
405 fprintf (dump_file, " basic block %d created\n", e2->src->index);
406 }
407 else
408 {
409 if (dump_file && (dump_flags & TDF_DETAILS))
410 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
411 e->src->index, e->dest->index, local_info->bb->index);
412
413 /* We are using BB as the duplicate. Remove the unnecessary
414 outgoing edges and statements from BB. */
415 remove_ctrl_stmt_and_useless_edges (local_info->bb,
416 rd->outgoing_edge->dest);
417
418 /* And fixup the flags on the single remaining edge. */
419 EDGE_SUCC (local_info->bb, 0)->flags
420 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
421 EDGE_SUCC (local_info->bb, 0)->flags |= EDGE_FALLTHRU;
422 }
423 }
424 return 1;
425 }
426
427 /* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
428 is reached via one or more specific incoming edges, we know which
429 outgoing edge from BB will be traversed.
430
431 We want to redirect those incoming edges to the target of the
432 appropriate outgoing edge. Doing so avoids a conditional branch
433 and may expose new optimization opportunities. Note that we have
434 to update dominator tree and SSA graph after such changes.
435
436 The key to keeping the SSA graph update manageable is to duplicate
437 the side effects occurring in BB so that those side effects still
438 occur on the paths which bypass BB after redirecting edges.
439
440 We accomplish this by creating duplicates of BB and arranging for
441 the duplicates to unconditionally pass control to one specific
442 successor of BB. We then revector the incoming edges into BB to
443 the appropriate duplicate of BB.
444
445 BB and its duplicates will have assignments to the same set of
446 SSA_NAMEs. Right now, we just call into rewrite_ssa_into_ssa
447 to update the SSA graph for those names.
448
449 We are also going to experiment with a true incremental update
450 scheme for the duplicated resources. One of the interesting
451 properties we can exploit here is that all the resources set
452 in BB will have the same IDFS, so we have one IDFS computation
453 per block with incoming threaded edges, which can lower the
454 cost of the true incremental update algorithm. */
455
456 static void
457 thread_block (basic_block bb)
458 {
459 /* E is an incoming edge into BB that we may or may not want to
460 redirect to a duplicate of BB. */
461 edge e;
462 edge_iterator ei;
463 struct local_info local_info;
464
465 /* ALL indicates whether or not all incoming edges into BB should
466 be threaded to a duplicate of BB. */
467 bool all = true;
468
469 /* To avoid scanning a linear array for the element we need we instead
470 use a hash table. For normal code there should be no noticeable
471 difference. However, if we have a block with a large number of
472 incoming and outgoing edges such linear searches can get expensive. */
473 redirection_data = htab_create (EDGE_COUNT (bb->succs),
474 redirection_data_hash,
475 redirection_data_eq,
476 free);
477
478 /* Record each unique threaded destination into a hash table for
479 efficient lookups. */
480 FOR_EACH_EDGE (e, ei, bb->preds)
481 {
482 if (!e->aux)
483 {
484 all = false;
485 }
486 else
487 {
488 edge e2 = e->aux;
489
490 /* Insert the outgoing edge into the hash table if it is not
491 already in the hash table. */
492 lookup_redirection_data (e2, e, true);
493 }
494 }
495
496 /* If we are going to thread all incoming edges to an outgoing edge, then
497 BB will become unreachable. Rather than just throwing it away, use
498 it for one of the duplicates. Mark the first incoming edge with the
499 DO_NOT_DUPLICATE attribute. */
500 if (all)
501 {
502 edge e = EDGE_PRED (bb, 0)->aux;
503 lookup_redirection_data (e, NULL, false)->do_not_duplicate = true;
504 }
505
506 /* Now create duplicates of BB.
507
508 Note that for a block with a high outgoing degree we can waste
509 a lot of time and memory creating and destroying useless edges.
510
511 So we first duplicate BB and remove the control structure at the
512 tail of the duplicate as well as all outgoing edges from the
513 duplicate. We then use that duplicate block as a template for
514 the rest of the duplicates. */
515 local_info.template_block = NULL;
516 local_info.bb = bb;
517 htab_traverse (redirection_data, create_duplicates, &local_info);
518
519 /* The template does not have an outgoing edge. Create that outgoing
520 edge and update PHI nodes as the edge's target as necessary.
521
522 We do this after creating all the duplicates to avoid creating
523 unnecessary edges. */
524 htab_traverse (redirection_data, fixup_template_block, &local_info);
525
526 /* The hash table traversals above created the duplicate blocks (and the
527 statements within the duplicate blocks). This loop creates PHI nodes for
528 the duplicated blocks and redirects the incoming edges into BB to reach
529 the duplicates of BB. */
530 htab_traverse (redirection_data, redirect_edges, &local_info);
531
532 /* Done with this block. Clear REDIRECTION_DATA. */
533 htab_delete (redirection_data);
534 redirection_data = NULL;
535 }
536
537 /* Walk through all blocks and thread incoming edges to the block's
538 destinations as requested. This is the only entry point into this
539 file.
540
541 Blocks which have one or more incoming edges have INCOMING_EDGE_THREADED
542 set in the block's annotation.
543
544 Each edge that should be threaded has the new destination edge stored in
545 the original edge's AUX field.
546
547 This routine (or one of its callees) will clear INCOMING_EDGE_THREADED
548 in the block annotations and the AUX field in the edges.
549
550 It is the caller's responsibility to fix the dominance information
551 and rewrite duplicated SSA_NAMEs back into SSA form.
552
553 Returns true if one or more edges were threaded, false otherwise. */
554
555 bool
556 thread_through_all_blocks (void)
557 {
558 basic_block bb;
559 bool retval = false;
560
561 FOR_EACH_BB (bb)
562 {
563 if (bb_ann (bb)->incoming_edge_threaded)
564 {
565 thread_block (bb);
566 retval = true;
567 bb_ann (bb)->incoming_edge_threaded = false;
568 }
569 }
570 return retval;
571 }