OSDN Git Service

303463850d0c8bfc56bad1e14df7a26403cd3dae
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-threadupdate.c
1 /* Thread edges through blocks and update the control flow and SSA graphs.
2    Copyright (C) 2004, 2005 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 #include "cfgloop.h"
40
41 /* Given a block B, update the CFG and SSA graph to reflect redirecting
42    one or more in-edges to B to instead reach the destination of an
43    out-edge from B while preserving any side effects in B.
44
45    i.e., given A->B and B->C, change A->B to be A->C yet still preserve the
46    side effects of executing B.
47
48      1. Make a copy of B (including its outgoing edges and statements).  Call
49         the copy B'.  Note B' has no incoming edges or PHIs at this time.
50
51      2. Remove the control statement at the end of B' and all outgoing edges
52         except B'->C.
53
54      3. Add a new argument to each PHI in C with the same value as the existing
55         argument associated with edge B->C.  Associate the new PHI arguments
56         with the edge B'->C.
57
58      4. For each PHI in B, find or create a PHI in B' with an identical
59         PHI_RESULT.  Add an argument to the PHI in B' which has the same
60         value as the PHI in B associated with the edge A->B.  Associate
61         the new argument in the PHI in B' with the edge A->B.
62
63      5. Change the edge A->B to A->B'.
64
65         5a. This automatically deletes any PHI arguments associated with the
66             edge A->B in B.
67
68         5b. This automatically associates each new argument added in step 4
69             with the edge A->B'.
70
71      6. Repeat for other incoming edges into B.
72
73      7. Put the duplicated resources in B and all the B' blocks into SSA form.
74
75    Note that block duplication can be minimized by first collecting the
76    the set of unique destination blocks that the incoming edges should
77    be threaded to.  Block duplication can be further minimized by using
78    B instead of creating B' for one destination if all edges into B are
79    going to be threaded to a successor of B.
80
81    We further reduce the number of edges and statements we create by
82    not copying all the outgoing edges and the control statement in
83    step #1.  We instead create a template block without the outgoing
84    edges and duplicate the template.  */
85
86
87 /* Steps #5 and #6 of the above algorithm are best implemented by walking
88    all the incoming edges which thread to the same destination edge at
89    the same time.  That avoids lots of table lookups to get information
90    for the destination edge.
91
92    To realize that implementation we create a list of incoming edges
93    which thread to the same outgoing edge.  Thus to implement steps
94    #5 and #6 we traverse our hash table of outgoing edge information.
95    For each entry we walk the list of incoming edges which thread to
96    the current outgoing edge.  */
97
98 struct el
99 {
100   edge e;
101   struct el *next;
102 };
103
104 /* Main data structure recording information regarding B's duplicate
105    blocks.  */
106
107 /* We need to efficiently record the unique thread destinations of this
108    block and specific information associated with those destinations.  We
109    may have many incoming edges threaded to the same outgoing edge.  This
110    can be naturally implemented with a hash table.  */
111
112 struct redirection_data
113 {
114   /* A duplicate of B with the trailing control statement removed and which
115      targets a single successor of B.  */
116   basic_block dup_block;
117
118   /* An outgoing edge from B.  DUP_BLOCK will have OUTGOING_EDGE->dest as
119      its single successor.  */
120   edge outgoing_edge;
121
122   /* A list of incoming edges which we want to thread to
123      OUTGOING_EDGE->dest.  */
124   struct el *incoming_edges;
125
126   /* Flag indicating whether or not we should create a duplicate block
127      for this thread destination.  This is only true if we are threading
128      all incoming edges and thus are using BB itself as a duplicate block.  */
129   bool do_not_duplicate;
130 };
131
132 /* Main data structure to hold information for duplicates of BB.  */
133 static htab_t redirection_data;
134
135 bool rediscover_loops_after_threading;
136
137 /* Data structure of information to pass to hash table traversal routines.  */
138 struct local_info
139 {
140   /* The current block we are working on.  */
141   basic_block bb;
142
143   /* A template copy of BB with no outgoing edges or control statement that
144      we use for creating copies.  */
145   basic_block template_block;
146
147   /* TRUE if we thread one or more jumps, FALSE otherwise.  */
148   bool jumps_threaded;
149 };
150
151 /* Remove the last statement in block BB if it is a control statement
152    Also remove all outgoing edges except the edge which reaches DEST_BB.
153    If DEST_BB is NULL, then remove all outgoing edges.  */
154
155 static void
156 remove_ctrl_stmt_and_useless_edges (basic_block bb, basic_block dest_bb)
157 {
158   block_stmt_iterator bsi;
159   edge e;
160   edge_iterator ei;
161
162   bsi = bsi_last (bb);
163
164   /* If the duplicate ends with a control statement, then remove it.
165
166      Note that if we are duplicating the template block rather than the
167      original basic block, then the duplicate might not have any real
168      statements in it.  */
169   if (!bsi_end_p (bsi)
170       && bsi_stmt (bsi)
171       && (TREE_CODE (bsi_stmt (bsi)) == COND_EXPR
172           || TREE_CODE (bsi_stmt (bsi)) == GOTO_EXPR
173           || TREE_CODE (bsi_stmt (bsi)) == SWITCH_EXPR))
174     bsi_remove (&bsi);
175
176   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
177     {
178       if (e->dest != dest_bb)
179         remove_edge (e);
180       else
181         ei_next (&ei);
182     }
183 }
184
185 /* Create a duplicate of BB which only reaches the destination of the edge
186    stored in RD.  Record the duplicate block in RD.  */
187
188 static void
189 create_block_for_threading (basic_block bb, struct redirection_data *rd)
190 {
191   /* We can use the generic block duplication code and simply remove
192      the stuff we do not need.  */
193   rd->dup_block = duplicate_block (bb, NULL);
194
195   /* Zero out the profile, since the block is unreachable for now.  */
196   rd->dup_block->frequency = 0;
197   rd->dup_block->count = 0;
198
199   /* The call to duplicate_block will copy everything, including the
200      useless COND_EXPR or SWITCH_EXPR at the end of BB.  We just remove
201      the useless COND_EXPR or SWITCH_EXPR here rather than having a
202      specialized block copier.  We also remove all outgoing edges
203      from the duplicate block.  The appropriate edge will be created
204      later.  */
205   remove_ctrl_stmt_and_useless_edges (rd->dup_block, NULL);
206 }
207
208 /* Hashing and equality routines for our hash table.  */
209 static hashval_t
210 redirection_data_hash (const void *p)
211 {
212   edge e = ((struct redirection_data *)p)->outgoing_edge;
213   return e->dest->index;
214 }
215
216 static int
217 redirection_data_eq (const void *p1, const void *p2)
218 {
219   edge e1 = ((struct redirection_data *)p1)->outgoing_edge;
220   edge e2 = ((struct redirection_data *)p2)->outgoing_edge;
221
222   return e1 == e2;
223 }
224
225 /* Given an outgoing edge E lookup and return its entry in our hash table.
226
227    If INSERT is true, then we insert the entry into the hash table if
228    it is not already present.  INCOMING_EDGE is added to the list of incoming
229    edges associated with E in the hash table.  */
230
231 static struct redirection_data *
232 lookup_redirection_data (edge e, edge incoming_edge, enum insert_option insert)
233 {
234   void **slot;
235   struct redirection_data *elt;
236
237  /* Build a hash table element so we can see if E is already
238      in the table.  */
239   elt = xmalloc (sizeof (struct redirection_data));
240   elt->outgoing_edge = e;
241   elt->dup_block = NULL;
242   elt->do_not_duplicate = false;
243   elt->incoming_edges = NULL;
244
245   slot = htab_find_slot (redirection_data, elt, insert);
246
247   /* This will only happen if INSERT is false and the entry is not
248      in the hash table.  */
249   if (slot == NULL)
250     {
251       free (elt);
252       return NULL;
253     }
254
255   /* This will only happen if E was not in the hash table and
256      INSERT is true.  */
257   if (*slot == NULL)
258     {
259       *slot = (void *)elt;
260       elt->incoming_edges = xmalloc (sizeof (struct el));
261       elt->incoming_edges->e = incoming_edge;
262       elt->incoming_edges->next = NULL;
263       return elt;
264     }
265   /* E was in the hash table.  */
266   else
267     {
268       /* Free ELT as we do not need it anymore, we will extract the
269          relevant entry from the hash table itself.  */
270       free (elt);
271
272       /* Get the entry stored in the hash table.  */
273       elt = (struct redirection_data *) *slot;
274
275       /* If insertion was requested, then we need to add INCOMING_EDGE
276          to the list of incoming edges associated with E.  */
277       if (insert)
278         {
279           struct el *el = xmalloc (sizeof (struct el));
280           el->next = elt->incoming_edges;
281           el->e = incoming_edge;
282           elt->incoming_edges = el;
283         }
284
285       return elt;
286     }
287 }
288
289 /* Given a duplicate block and its single destination (both stored
290    in RD).  Create an edge between the duplicate and its single
291    destination.
292
293    Add an additional argument to any PHI nodes at the single
294    destination.  */
295
296 static void
297 create_edge_and_update_destination_phis (struct redirection_data *rd)
298 {
299   edge e = make_edge (rd->dup_block, rd->outgoing_edge->dest, EDGE_FALLTHRU);
300   tree phi;
301
302   e->probability = REG_BR_PROB_BASE;
303   e->count = rd->dup_block->count;
304
305   /* If there are any PHI nodes at the destination of the outgoing edge
306      from the duplicate block, then we will need to add a new argument
307      to them.  The argument should have the same value as the argument
308      associated with the outgoing edge stored in RD.  */
309   for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
310     {
311       int indx = rd->outgoing_edge->dest_idx;
312       add_phi_arg (phi, PHI_ARG_DEF (phi, indx), e);
313     }
314 }
315
316 /* Hash table traversal callback routine to create duplicate blocks.  */
317
318 static int
319 create_duplicates (void **slot, void *data)
320 {
321   struct redirection_data *rd = (struct redirection_data *) *slot;
322   struct local_info *local_info = (struct local_info *)data;
323
324   /* If this entry should not have a duplicate created, then there's
325      nothing to do.  */
326   if (rd->do_not_duplicate)
327     return 1;
328
329   /* Create a template block if we have not done so already.  Otherwise
330      use the template to create a new block.  */
331   if (local_info->template_block == NULL)
332     {
333       create_block_for_threading (local_info->bb, rd);
334       local_info->template_block = rd->dup_block;
335
336       /* We do not create any outgoing edges for the template.  We will
337          take care of that in a later traversal.  That way we do not
338          create edges that are going to just be deleted.  */
339     }
340   else
341     {
342       create_block_for_threading (local_info->template_block, rd);
343
344       /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
345          block.  */
346       create_edge_and_update_destination_phis (rd);
347     }
348
349   /* Keep walking the hash table.  */
350   return 1;
351 }
352
353 /* We did not create any outgoing edges for the template block during
354    block creation.  This hash table traversal callback creates the
355    outgoing edge for the template block.  */
356
357 static int
358 fixup_template_block (void **slot, void *data)
359 {
360   struct redirection_data *rd = (struct redirection_data *) *slot;
361   struct local_info *local_info = (struct local_info *)data;
362
363   /* If this is the template block, then create its outgoing edges
364      and halt the hash table traversal.  */
365   if (rd->dup_block && rd->dup_block == local_info->template_block)
366     {
367       create_edge_and_update_destination_phis (rd);
368       return 0;
369     }
370
371   return 1;
372 }
373
374 /* Not all jump threading requests are useful.  In particular some
375    jump threading requests can create irreducible regions which are
376    undesirable.
377
378    This routine will examine the BB's incoming edges for jump threading
379    requests which, if acted upon, would create irreducible regions.  Any
380    such jump threading requests found will be pruned away.  */
381
382 static void
383 prune_undesirable_thread_requests (basic_block bb)
384 {
385   edge e;
386   edge_iterator ei;
387   bool may_create_irreducible_region = false;
388   unsigned int num_outgoing_edges_into_loop = 0;
389
390   /* For the heuristics below, we need to know if BB has more than
391      one outgoing edge into a loop.  */
392   FOR_EACH_EDGE (e, ei, bb->succs)
393     num_outgoing_edges_into_loop += ((e->flags & EDGE_LOOP_EXIT) == 0);
394
395   if (num_outgoing_edges_into_loop > 1)
396     {
397       edge backedge = NULL;
398
399       /* Consider the effect of threading the edge (0, 1) to 2 on the left
400          CFG to produce the right CFG:
401     
402
403              0            0
404              |            |
405              1<--+        2<--------+
406             / \  |        |         |
407            2   3 |        4<----+   |
408             \ /  |       / \    |   |
409              4---+      E   1-- | --+
410              |              |   |
411              E              3---+
412
413
414         Threading the (0, 1) edge to 2 effectively creates two loops
415         (2, 4, 1) and (4, 1, 3) which are neither disjoint nor nested.
416         This is not good.
417
418         However, we do need to be able to thread  (0, 1) to 2 or 3
419         in the left CFG below (which creates the middle and right
420         CFGs with nested loops).
421
422              0          0             0
423              |          |             |
424              1<--+      2<----+       3<-+<-+
425             /|   |      |     |       |  |  |
426            2 |   |      3<-+  |       1--+  |
427             \|   |      |  |  |       |     |
428              3---+      1--+--+       2-----+
429
430          
431          A safe heuristic appears to be to only allow threading if BB
432          has a single incoming backedge from one of its direct successors.  */
433
434       FOR_EACH_EDGE (e, ei, bb->preds)
435         {
436           if (e->flags & EDGE_DFS_BACK)
437             {
438               if (backedge)
439                 {
440                   backedge = NULL;
441                   break;
442                 }
443               else
444                 {
445                   backedge = e;
446                 }
447             }
448         }
449
450       if (backedge && find_edge (bb, backedge->src))
451         ;
452       else
453         may_create_irreducible_region = true;
454     }
455   else
456     {
457       edge dest = NULL;
458
459       /* If we thread across the loop entry block (BB) into the
460          loop and BB is still reached from outside the loop, then
461          we would create an irreducible CFG.  Consider the effect
462          of threading the edge (1, 4) to 5 on the left CFG to produce
463          the right CFG
464
465              0               0
466             / \             / \
467            1   2           1   2
468             \ /            |   |
469              4<----+       5<->4
470             / \    |           |
471            E   5---+           E
472
473
474          Threading the (1, 4) edge to 5 creates two entry points
475          into the loop (4, 5) (one from block 1, the other from
476          block 2).  A classic irreducible region. 
477
478          So look at all of BB's incoming edges which are not
479          backedges and which are not threaded to the loop exit.
480          If that subset of incoming edges do not all thread
481          to the same block, then threading any of them will create
482          an irreducible region.  */
483
484       FOR_EACH_EDGE (e, ei, bb->preds)
485         {
486           edge e2;
487
488           /* We ignore back edges for now.  This may need refinement
489              as threading a backedge creates an inner loop which
490              we would need to verify has a single entry point. 
491
492              If all backedges thread to new locations, then this
493              block will no longer have incoming backedges and we
494              need not worry about creating irreducible regions
495              by threading through BB.  I don't think this happens
496              enough in practice to worry about it.  */
497           if (e->flags & EDGE_DFS_BACK)
498             continue;
499
500           /* If the incoming edge threads to the loop exit, then it
501              is clearly safe.  */
502           e2 = e->aux;
503           if (e2 && (e2->flags & EDGE_LOOP_EXIT))
504             continue;
505
506           /* E enters the loop header and is not threaded.  We can
507              not allow any other incoming edges to thread into
508              the loop as that would create an irreducible region.  */
509           if (!e2)
510             {
511               may_create_irreducible_region = true;
512               break;
513             }
514
515           /* We know that this incoming edge threads to a block inside
516              the loop.  This edge must thread to the same target in
517              the loop as any previously seen threaded edges.  Otherwise
518              we will create an irreducible region.  */
519           if (!dest)
520             dest = e2;
521           else if (e2 != dest)
522             {
523               may_create_irreducible_region = true;
524               break;
525             }
526         }
527     }
528
529   /* If we might create an irreducible region, then cancel any of
530      the jump threading requests for incoming edges which are
531      not backedges and which do not thread to the exit block.  */
532   if (may_create_irreducible_region)
533     {
534       FOR_EACH_EDGE (e, ei, bb->preds)
535         {
536           edge e2;
537
538           /* Ignore back edges.  */
539           if (e->flags & EDGE_DFS_BACK)
540             continue;
541
542           e2 = e->aux;
543
544           /* If this incoming edge was not threaded, then there is
545              nothing to do.  */
546           if (!e2)
547             continue;
548
549           /* If this incoming edge threaded to the loop exit,
550              then it can be ignored as it is safe.  */
551           if (e2->flags & EDGE_LOOP_EXIT)
552             continue;
553
554           if (e2)
555             {
556               /* This edge threaded into the loop and the jump thread
557                  request must be cancelled.  */
558               if (dump_file && (dump_flags & TDF_DETAILS))
559                 fprintf (dump_file, "  Not threading jump %d --> %d to %d\n",
560                          e->src->index, e->dest->index, e2->dest->index);
561               e->aux = NULL;
562             }
563         }
564     }
565 }
566
567 /* Hash table traversal callback to redirect each incoming edge
568    associated with this hash table element to its new destination.  */
569
570 static int
571 redirect_edges (void **slot, void *data)
572 {
573   struct redirection_data *rd = (struct redirection_data *) *slot;
574   struct local_info *local_info = (struct local_info *)data;
575   struct el *next, *el;
576
577   /* Walk over all the incoming edges associated associated with this
578      hash table entry.  */
579   for (el = rd->incoming_edges; el; el = next)
580     {
581       edge e = el->e;
582
583       /* Go ahead and free this element from the list.  Doing this now
584          avoids the need for another list walk when we destroy the hash
585          table.  */
586       next = el->next;
587       free (el);
588
589       /* Go ahead and clear E->aux.  It's not needed anymore and failure
590          to clear it will cause all kinds of unpleasant problems later.  */
591       e->aux = NULL;
592
593       if (rd->dup_block)
594         {
595           edge e2;
596
597           if (dump_file && (dump_flags & TDF_DETAILS))
598             fprintf (dump_file, "  Threaded jump %d --> %d to %d\n",
599                      e->src->index, e->dest->index, rd->dup_block->index);
600
601           /* Redirect the incoming edge to the appropriate duplicate
602              block.  */
603           e2 = redirect_edge_and_branch (e, rd->dup_block);
604           flush_pending_stmts (e2);
605
606           if ((dump_file && (dump_flags & TDF_DETAILS))
607               && e->src != e2->src)
608             fprintf (dump_file, "    basic block %d created\n", e2->src->index);
609         }
610       else
611         {
612           if (dump_file && (dump_flags & TDF_DETAILS))
613             fprintf (dump_file, "  Threaded jump %d --> %d to %d\n",
614                      e->src->index, e->dest->index, local_info->bb->index);
615
616           /* We are using BB as the duplicate.  Remove the unnecessary
617              outgoing edges and statements from BB.  */
618           remove_ctrl_stmt_and_useless_edges (local_info->bb,
619                                               rd->outgoing_edge->dest);
620
621           /* And fixup the flags on the single remaining edge.  */
622           single_succ_edge (local_info->bb)->flags
623             &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
624           single_succ_edge (local_info->bb)->flags |= EDGE_FALLTHRU;
625         }
626     }
627
628   /* Indicate that we actually threaded one or more jumps.  */
629   if (rd->incoming_edges)
630     local_info->jumps_threaded = true;
631
632   return 1;
633 }
634
635 /* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
636    is reached via one or more specific incoming edges, we know which
637    outgoing edge from BB will be traversed.
638
639    We want to redirect those incoming edges to the target of the
640    appropriate outgoing edge.  Doing so avoids a conditional branch
641    and may expose new optimization opportunities.  Note that we have
642    to update dominator tree and SSA graph after such changes.
643
644    The key to keeping the SSA graph update manageable is to duplicate
645    the side effects occurring in BB so that those side effects still
646    occur on the paths which bypass BB after redirecting edges.
647
648    We accomplish this by creating duplicates of BB and arranging for
649    the duplicates to unconditionally pass control to one specific
650    successor of BB.  We then revector the incoming edges into BB to
651    the appropriate duplicate of BB.
652
653    BB and its duplicates will have assignments to the same set of
654    SSA_NAMEs.  Right now, we just call into update_ssa to update the
655    SSA graph for those names.
656
657    We are also going to experiment with a true incremental update
658    scheme for the duplicated resources.  One of the interesting
659    properties we can exploit here is that all the resources set
660    in BB will have the same IDFS, so we have one IDFS computation
661    per block with incoming threaded edges, which can lower the
662    cost of the true incremental update algorithm.  */
663
664 static bool
665 thread_block (basic_block bb)
666 {
667   /* E is an incoming edge into BB that we may or may not want to
668      redirect to a duplicate of BB.  */
669   edge e;
670   edge_iterator ei;
671   struct local_info local_info;
672
673   /* FOUND_BACKEDGE indicates that we found an incoming backedge
674      into BB, in which case we may ignore certain jump threads
675      to avoid creating irreducible regions.  */
676   bool found_backedge = false;
677
678   /* ALL indicates whether or not all incoming edges into BB should
679      be threaded to a duplicate of BB.  */
680   bool all = true;
681
682   /* To avoid scanning a linear array for the element we need we instead
683      use a hash table.  For normal code there should be no noticeable
684      difference.  However, if we have a block with a large number of
685      incoming and outgoing edges such linear searches can get expensive.  */
686   redirection_data = htab_create (EDGE_COUNT (bb->succs),
687                                   redirection_data_hash,
688                                   redirection_data_eq,
689                                   free);
690
691   FOR_EACH_EDGE (e, ei, bb->preds)
692     found_backedge |= ((e->flags & EDGE_DFS_BACK) != 0);
693
694   /* If BB has incoming backedges, then threading across BB might
695      introduce an irreducible region, which would be undesirable
696      as that inhibits various optimizations later.  Prune away
697      any jump threading requests which we know will result in
698      an irreducible region.  */
699   if (found_backedge)
700     prune_undesirable_thread_requests (bb);
701
702   /* Record each unique threaded destination into a hash table for
703      efficient lookups.  */
704   FOR_EACH_EDGE (e, ei, bb->preds)
705     {
706       if (!e->aux)
707         {
708           all = false;
709         }
710       else
711         {
712           edge e2 = e->aux;
713
714           /* If we thread to a loop exit edge, then we will need to 
715              rediscover the loop exit edges.  While it may seem that
716              the new edge is a loop exit edge, that is not the case.
717              Consider threading the edge (5,6) to E in the CFG on the
718              left which creates the CFG on the right:
719
720
721                       0<--+            0<---+
722                      / \  |           / \   |
723                     1   2 |          1   2  |
724                    / \  | |         / \  |  |
725                   3   4 | |        3   4 6--+
726                    \ /  | |         \ /
727                     5   | |          5
728                      \ /  |          |
729                       6---+          E
730                       |
731                       E
732
733              After threading, the edge (0, 1)  is the loop exit edge and
734              the nodes 0, 2, 6 are the only nodes in the loop.  */
735           if (e2->flags & EDGE_LOOP_EXIT)
736             rediscover_loops_after_threading = true;
737
738           /* Insert the outgoing edge into the hash table if it is not
739              already in the hash table.  */
740           lookup_redirection_data (e2, e, INSERT);
741         }
742     }
743
744   /* If we are going to thread all incoming edges to an outgoing edge, then
745      BB will become unreachable.  Rather than just throwing it away, use
746      it for one of the duplicates.  Mark the first incoming edge with the
747      DO_NOT_DUPLICATE attribute.  */
748   if (all)
749     {
750       edge e = EDGE_PRED (bb, 0)->aux;
751       lookup_redirection_data (e, NULL, NO_INSERT)->do_not_duplicate = true;
752     }
753
754   /* Now create duplicates of BB.
755
756      Note that for a block with a high outgoing degree we can waste
757      a lot of time and memory creating and destroying useless edges.
758
759      So we first duplicate BB and remove the control structure at the
760      tail of the duplicate as well as all outgoing edges from the
761      duplicate.  We then use that duplicate block as a template for
762      the rest of the duplicates.  */
763   local_info.template_block = NULL;
764   local_info.bb = bb;
765   local_info.jumps_threaded = false;
766   htab_traverse (redirection_data, create_duplicates, &local_info);
767
768   /* The template does not have an outgoing edge.  Create that outgoing
769      edge and update PHI nodes as the edge's target as necessary.
770
771      We do this after creating all the duplicates to avoid creating
772      unnecessary edges.  */
773   htab_traverse (redirection_data, fixup_template_block, &local_info);
774
775   /* The hash table traversals above created the duplicate blocks (and the
776      statements within the duplicate blocks).  This loop creates PHI nodes for
777      the duplicated blocks and redirects the incoming edges into BB to reach
778      the duplicates of BB.  */
779   htab_traverse (redirection_data, redirect_edges, &local_info);
780
781   /* Done with this block.  Clear REDIRECTION_DATA.  */
782   htab_delete (redirection_data);
783   redirection_data = NULL;
784
785   /* Indicate to our caller whether or not any jumps were threaded.  */
786   return local_info.jumps_threaded;
787 }
788
789 /* Walk through all blocks and thread incoming edges to the block's
790    destinations as requested.  This is the only entry point into this
791    file.
792
793    Blocks which have one or more incoming edges have INCOMING_EDGE_THREADED
794    set in the block's annotation.
795
796    Each edge that should be threaded has the new destination edge stored in
797    the original edge's AUX field.
798
799    This routine (or one of its callees) will clear INCOMING_EDGE_THREADED
800    in the block annotations and the AUX field in the edges.
801
802    It is the caller's responsibility to fix the dominance information
803    and rewrite duplicated SSA_NAMEs back into SSA form.
804
805    Returns true if one or more edges were threaded, false otherwise.  */
806
807 bool
808 thread_through_all_blocks (bitmap threaded_blocks)
809 {
810   bool retval = false;
811   unsigned int i;
812   bitmap_iterator bi;
813
814   rediscover_loops_after_threading = false;
815
816   EXECUTE_IF_SET_IN_BITMAP (threaded_blocks, 0, i, bi)
817     {
818       basic_block bb = BASIC_BLOCK (i);
819
820       if (EDGE_COUNT (bb->preds) > 0)
821         retval |= thread_block (bb);
822     }
823
824   return retval;
825 }