Import pre-release gcc-5.0 to new vendor branch
[dragonfly.git] / contrib / gcc-5.0 / gcc / graphite-scop-detection.c
1 /* Detection of Static Control Parts (SCoP) for Graphite.
2    Copyright (C) 2009-2015 Free Software Foundation, Inc.
3    Contributed by Sebastian Pop <sebastian.pop@amd.com> and
4    Tobias Grosser <grosser@fim.uni-passau.de>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23
24 #ifdef HAVE_isl
25 #include <isl/set.h>
26 #include <isl/map.h>
27 #include <isl/union_map.h>
28 #endif
29
30 #include "system.h"
31 #include "coretypes.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "vec.h"
35 #include "double-int.h"
36 #include "input.h"
37 #include "alias.h"
38 #include "symtab.h"
39 #include "options.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "fold-const.h"
44 #include "predict.h"
45 #include "tm.h"
46 #include "hard-reg-set.h"
47 #include "input.h"
48 #include "function.h"
49 #include "dominance.h"
50 #include "cfg.h"
51 #include "basic-block.h"
52 #include "tree-ssa-alias.h"
53 #include "internal-fn.h"
54 #include "gimple-expr.h"
55 #include "is-a.h"
56 #include "gimple.h"
57 #include "gimple-iterator.h"
58 #include "gimple-ssa.h"
59 #include "tree-phinodes.h"
60 #include "ssa-iterators.h"
61 #include "tree-ssa-loop-manip.h"
62 #include "tree-ssa-loop-niter.h"
63 #include "tree-ssa-loop.h"
64 #include "tree-into-ssa.h"
65 #include "tree-ssa.h"
66 #include "cfgloop.h"
67 #include "tree-chrec.h"
68 #include "tree-data-ref.h"
69 #include "tree-scalar-evolution.h"
70 #include "tree-pass.h"
71 #include "sese.h"
72 #include "tree-ssa-propagate.h"
73 #include "cp/cp-tree.h"
74
75 #ifdef HAVE_isl
76 #include "graphite-poly.h"
77 #include "graphite-scop-detection.h"
78
79 /* Forward declarations.  */
80 static void make_close_phi_nodes_unique (basic_block);
81
82 /* The type of the analyzed basic block.  */
83
84 typedef enum gbb_type {
85   GBB_UNKNOWN,
86   GBB_LOOP_SING_EXIT_HEADER,
87   GBB_LOOP_MULT_EXIT_HEADER,
88   GBB_LOOP_EXIT,
89   GBB_COND_HEADER,
90   GBB_SIMPLE,
91   GBB_LAST
92 } gbb_type;
93
94 /* Detect the type of BB.  Loop headers are only marked, if they are
95    new.  This means their loop_father is different to LAST_LOOP.
96    Otherwise they are treated like any other bb and their type can be
97    any other type.  */
98
99 static gbb_type
100 get_bb_type (basic_block bb, struct loop *last_loop)
101 {
102   vec<basic_block> dom;
103   int nb_dom;
104   struct loop *loop = bb->loop_father;
105
106   /* Check, if we entry into a new loop. */
107   if (loop != last_loop)
108     {
109       if (single_exit (loop) != NULL)
110         return GBB_LOOP_SING_EXIT_HEADER;
111       else if (loop->num != 0)
112         return GBB_LOOP_MULT_EXIT_HEADER;
113       else
114         return GBB_COND_HEADER;
115     }
116
117   dom = get_dominated_by (CDI_DOMINATORS, bb);
118   nb_dom = dom.length ();
119   dom.release ();
120
121   if (nb_dom == 0)
122     return GBB_LAST;
123
124   if (nb_dom == 1 && single_succ_p (bb))
125     return GBB_SIMPLE;
126
127   return GBB_COND_HEADER;
128 }
129
130 /* A SCoP detection region, defined using bbs as borders.
131
132    All control flow touching this region, comes in passing basic_block
133    ENTRY and leaves passing basic_block EXIT.  By using bbs instead of
134    edges for the borders we are able to represent also regions that do
135    not have a single entry or exit edge.
136
137    But as they have a single entry basic_block and a single exit
138    basic_block, we are able to generate for every sd_region a single
139    entry and exit edge.
140
141    1   2
142     \ /
143      3  <- entry
144      |
145      4
146     / \                 This region contains: {3, 4, 5, 6, 7, 8}
147    5   6
148    |   |
149    7   8
150     \ /
151      9  <- exit  */
152
153
154 typedef struct sd_region_p
155 {
156   /* The entry bb dominates all bbs in the sd_region.  It is part of
157      the region.  */
158   basic_block entry;
159
160   /* The exit bb postdominates all bbs in the sd_region, but is not
161      part of the region.  */
162   basic_block exit;
163 } sd_region;
164
165
166
167 /* Moves the scops from SOURCE to TARGET and clean up SOURCE.  */
168
169 static void
170 move_sd_regions (vec<sd_region> *source, vec<sd_region> *target)
171 {
172   sd_region *s;
173   int i;
174
175   FOR_EACH_VEC_ELT (*source, i, s)
176     target->safe_push (*s);
177
178   source->release ();
179 }
180
181 /* Something like "n * m" is not allowed.  */
182
183 static bool
184 graphite_can_represent_init (tree e)
185 {
186   switch (TREE_CODE (e))
187     {
188     case POLYNOMIAL_CHREC:
189       return graphite_can_represent_init (CHREC_LEFT (e))
190         && graphite_can_represent_init (CHREC_RIGHT (e));
191
192     case MULT_EXPR:
193       if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
194         return graphite_can_represent_init (TREE_OPERAND (e, 0))
195           && tree_fits_shwi_p (TREE_OPERAND (e, 1));
196       else
197         return graphite_can_represent_init (TREE_OPERAND (e, 1))
198           && tree_fits_shwi_p (TREE_OPERAND (e, 0));
199
200     case PLUS_EXPR:
201     case POINTER_PLUS_EXPR:
202     case MINUS_EXPR:
203       return graphite_can_represent_init (TREE_OPERAND (e, 0))
204         && graphite_can_represent_init (TREE_OPERAND (e, 1));
205
206     case NEGATE_EXPR:
207     case BIT_NOT_EXPR:
208     CASE_CONVERT:
209     case NON_LVALUE_EXPR:
210       return graphite_can_represent_init (TREE_OPERAND (e, 0));
211
212    default:
213      break;
214     }
215
216   return true;
217 }
218
219 /* Return true when SCEV can be represented in the polyhedral model.
220
221    An expression can be represented, if it can be expressed as an
222    affine expression.  For loops (i, j) and parameters (m, n) all
223    affine expressions are of the form:
224
225    x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
226
227    1 i + 20 j + (-2) m + 25
228
229    Something like "i * n" or "n * m" is not allowed.  */
230
231 static bool
232 graphite_can_represent_scev (tree scev)
233 {
234   if (chrec_contains_undetermined (scev))
235     return false;
236
237   /* We disable the handling of pointer types, because it’s currently not
238      supported by Graphite with the ISL AST generator. SSA_NAME nodes are
239      the only nodes, which are disabled in case they are pointers to object
240      types, but this can be changed.  */
241
242   if (TYPE_PTROB_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
243     return false;
244
245   switch (TREE_CODE (scev))
246     {
247     case NEGATE_EXPR:
248     case BIT_NOT_EXPR:
249     CASE_CONVERT:
250     case NON_LVALUE_EXPR:
251       return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
252
253     case PLUS_EXPR:
254     case POINTER_PLUS_EXPR:
255     case MINUS_EXPR:
256       return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
257         && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
258
259     case MULT_EXPR:
260       return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
261         && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
262         && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
263              && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
264         && graphite_can_represent_init (scev)
265         && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
266         && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
267
268     case POLYNOMIAL_CHREC:
269       /* Check for constant strides.  With a non constant stride of
270          'n' we would have a value of 'iv * n'.  Also check that the
271          initial value can represented: for example 'n * m' cannot be
272          represented.  */
273       if (!evolution_function_right_is_integer_cst (scev)
274           || !graphite_can_represent_init (scev))
275         return false;
276       return graphite_can_represent_scev (CHREC_LEFT (scev));
277
278     default:
279       break;
280     }
281
282   /* Only affine functions can be represented.  */
283   if (tree_contains_chrecs (scev, NULL)
284       || !scev_is_linear_expression (scev))
285     return false;
286
287   return true;
288 }
289
290
291 /* Return true when EXPR can be represented in the polyhedral model.
292
293    This means an expression can be represented, if it is linear with
294    respect to the loops and the strides are non parametric.
295    LOOP is the place where the expr will be evaluated.  SCOP_ENTRY defines the
296    entry of the region we analyse.  */
297
298 static bool
299 graphite_can_represent_expr (basic_block scop_entry, loop_p loop,
300                              tree expr)
301 {
302   tree scev = analyze_scalar_evolution (loop, expr);
303
304   scev = instantiate_scev (scop_entry, loop, scev);
305
306   return graphite_can_represent_scev (scev);
307 }
308
309 /* Return true if the data references of STMT can be represented by
310    Graphite.  */
311
312 static bool
313 stmt_has_simple_data_refs_p (loop_p outermost_loop ATTRIBUTE_UNUSED,
314                              gimple stmt)
315 {
316   data_reference_p dr;
317   unsigned i;
318   int j;
319   bool res = true;
320   vec<data_reference_p> drs = vNULL;
321   loop_p outer;
322
323   for (outer = loop_containing_stmt (stmt); outer; outer = loop_outer (outer))
324     {
325       graphite_find_data_references_in_stmt (outer,
326                                              loop_containing_stmt (stmt),
327                                              stmt, &drs);
328
329       FOR_EACH_VEC_ELT (drs, j, dr)
330         for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
331           if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i)))
332             {
333               res = false;
334               goto done;
335             }
336
337       free_data_refs (drs);
338       drs.create (0);
339     }
340
341  done:
342   free_data_refs (drs);
343   return res;
344 }
345
346 /* Return true only when STMT is simple enough for being handled by
347    Graphite.  This depends on SCOP_ENTRY, as the parameters are
348    initialized relatively to this basic block, the linear functions
349    are initialized to OUTERMOST_LOOP and BB is the place where we try
350    to evaluate the STMT.  */
351
352 static bool
353 stmt_simple_for_scop_p (basic_block scop_entry, loop_p outermost_loop,
354                         gimple stmt, basic_block bb)
355 {
356   loop_p loop = bb->loop_father;
357
358   gcc_assert (scop_entry);
359
360   /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
361      Calls have side-effects, except those to const or pure
362      functions.  */
363   if (gimple_has_volatile_ops (stmt)
364       || (gimple_code (stmt) == GIMPLE_CALL
365           && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
366       || (gimple_code (stmt) == GIMPLE_ASM))
367     return false;
368
369   if (is_gimple_debug (stmt))
370     return true;
371
372   if (!stmt_has_simple_data_refs_p (outermost_loop, stmt))
373     return false;
374
375   switch (gimple_code (stmt))
376     {
377     case GIMPLE_RETURN:
378     case GIMPLE_LABEL:
379       return true;
380
381     case GIMPLE_COND:
382       {
383         /* We can handle all binary comparisons.  Inequalities are
384            also supported as they can be represented with union of
385            polyhedra.  */
386         enum tree_code code = gimple_cond_code (stmt);
387         if (!(code == LT_EXPR
388               || code == GT_EXPR
389               || code == LE_EXPR
390               || code == GE_EXPR
391               || code == EQ_EXPR
392               || code == NE_EXPR))
393           return false;
394
395         for (unsigned i = 0; i < 2; ++i)
396           {
397             tree op = gimple_op (stmt, i);
398             if (!graphite_can_represent_expr (scop_entry, loop, op)
399                 /* We can not handle REAL_TYPE. Failed for pr39260.  */
400                 || TREE_CODE (TREE_TYPE (op)) == REAL_TYPE)
401               return false;
402           }
403
404         return true;
405       }
406
407     case GIMPLE_ASSIGN:
408     case GIMPLE_CALL:
409       return true;
410
411     default:
412       /* These nodes cut a new scope.  */
413       return false;
414     }
415
416   return false;
417 }
418
419 /* Returns the statement of BB that contains a harmful operation: that
420    can be a function call with side effects, the induction variables
421    are not linear with respect to SCOP_ENTRY, etc.  The current open
422    scop should end before this statement.  The evaluation is limited using
423    OUTERMOST_LOOP as outermost loop that may change.  */
424
425 static gimple
426 harmful_stmt_in_bb (basic_block scop_entry, loop_p outer_loop, basic_block bb)
427 {
428   gimple_stmt_iterator gsi;
429
430   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
431     if (!stmt_simple_for_scop_p (scop_entry, outer_loop, gsi_stmt (gsi), bb))
432       return gsi_stmt (gsi);
433
434   return NULL;
435 }
436
437 /* Return true if LOOP can be represented in the polyhedral
438    representation.  This is evaluated taking SCOP_ENTRY and
439    OUTERMOST_LOOP in mind.  */
440
441 static bool
442 graphite_can_represent_loop (basic_block scop_entry, loop_p loop)
443 {
444   tree niter;
445   struct tree_niter_desc niter_desc;
446
447   /* FIXME: For the moment, graphite cannot be used on loops that
448      iterate using induction variables that wrap.  */
449
450   return number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
451     && niter_desc.control.no_overflow
452     && (niter = number_of_latch_executions (loop))
453     && !chrec_contains_undetermined (niter)
454     && graphite_can_represent_expr (scop_entry, loop, niter);
455 }
456
457 /* Store information needed by scopdet_* functions.  */
458
459 struct scopdet_info
460 {
461   /* Exit of the open scop would stop if the current BB is harmful.  */
462   basic_block exit;
463
464   /* Where the next scop would start if the current BB is harmful.  */
465   basic_block next;
466
467   /* The bb or one of its children contains open loop exits.  That means
468      loop exit nodes that are not surrounded by a loop dominated by bb.  */
469   bool exits;
470
471   /* The bb or one of its children contains only structures we can handle.  */
472   bool difficult;
473 };
474
475 static struct scopdet_info build_scops_1 (basic_block, loop_p,
476                                           vec<sd_region> *, loop_p);
477
478 /* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
479    to SCOPS.  TYPE is the gbb_type of BB.  */
480
481 static struct scopdet_info
482 scopdet_basic_block_info (basic_block bb, loop_p outermost_loop,
483                           vec<sd_region> *scops, gbb_type type)
484 {
485   loop_p loop = bb->loop_father;
486   struct scopdet_info result;
487   gimple stmt;
488
489   /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps.  */
490   basic_block entry_block = ENTRY_BLOCK_PTR_FOR_FN (cfun);
491   stmt = harmful_stmt_in_bb (entry_block, outermost_loop, bb);
492   result.difficult = (stmt != NULL);
493   result.exit = NULL;
494
495   switch (type)
496     {
497     case GBB_LAST:
498       result.next = NULL;
499       result.exits = false;
500
501       /* Mark bbs terminating a SESE region difficult, if they start
502          a condition or if the block it exits to cannot be split
503          with make_forwarder_block.  */
504       if (!single_succ_p (bb)
505           || bb_has_abnormal_pred (single_succ (bb)))
506         result.difficult = true;
507       else
508         result.exit = single_succ (bb);
509
510       break;
511
512     case GBB_SIMPLE:
513       result.next = single_succ (bb);
514       result.exits = false;
515       result.exit = single_succ (bb);
516       break;
517
518     case GBB_LOOP_SING_EXIT_HEADER:
519       {
520         auto_vec<sd_region, 3> regions;
521         struct scopdet_info sinfo;
522         edge exit_e = single_exit (loop);
523
524         sinfo = build_scops_1 (bb, outermost_loop, &regions, loop);
525
526         if (!graphite_can_represent_loop (entry_block, loop))
527           result.difficult = true;
528
529         result.difficult |= sinfo.difficult;
530
531         /* Try again with another loop level.  */
532         if (result.difficult
533             && loop_depth (outermost_loop) + 1 == loop_depth (loop))
534           {
535             outermost_loop = loop;
536
537             regions.release ();
538             regions.create (3);
539
540             sinfo = scopdet_basic_block_info (bb, outermost_loop, scops, type);
541
542             result = sinfo;
543             result.difficult = true;
544
545             if (sinfo.difficult)
546               move_sd_regions (&regions, scops);
547             else
548               {
549                 sd_region open_scop;
550                 open_scop.entry = bb;
551                 open_scop.exit = exit_e->dest;
552                 scops->safe_push (open_scop);
553                 regions.release ();
554               }
555           }
556         else
557           {
558             result.exit = exit_e->dest;
559             result.next = exit_e->dest;
560
561             /* If we do not dominate result.next, remove it.  It's either
562                the exit block, or another bb dominates it and will
563                call the scop detection for this bb.  */
564             if (!dominated_by_p (CDI_DOMINATORS, result.next, bb))
565               result.next = NULL;
566
567             if (exit_e->src->loop_father != loop)
568               result.next = NULL;
569
570             result.exits = false;
571
572             if (result.difficult)
573               move_sd_regions (&regions, scops);
574             else
575               regions.release ();
576           }
577
578         break;
579       }
580
581     case GBB_LOOP_MULT_EXIT_HEADER:
582       {
583         /* XXX: For now we just do not join loops with multiple exits.  If the
584            exits lead to the same bb it may be possible to join the loop.  */
585         auto_vec<sd_region, 3> regions;
586         vec<edge> exits = get_loop_exit_edges (loop);
587         edge e;
588         int i;
589         build_scops_1 (bb, loop, &regions, loop);
590
591         /* Scan the code dominated by this loop.  This means all bbs, that are
592            are dominated by a bb in this loop, but are not part of this loop.
593
594            The easiest case:
595              - The loop exit destination is dominated by the exit sources.
596
597            TODO: We miss here the more complex cases:
598                   - The exit destinations are dominated by another bb inside
599                     the loop.
600                   - The loop dominates bbs, that are not exit destinations.  */
601         FOR_EACH_VEC_ELT (exits, i, e)
602           if (e->src->loop_father == loop
603               && dominated_by_p (CDI_DOMINATORS, e->dest, e->src))
604             {
605               if (loop_outer (outermost_loop))
606                 outermost_loop = loop_outer (outermost_loop);
607
608               /* Pass loop_outer to recognize e->dest as loop header in
609                  build_scops_1.  */
610               if (e->dest->loop_father->header == e->dest)
611                 build_scops_1 (e->dest, outermost_loop, &regions,
612                                loop_outer (e->dest->loop_father));
613               else
614                 build_scops_1 (e->dest, outermost_loop, &regions,
615                                e->dest->loop_father);
616             }
617
618         result.next = NULL;
619         result.exit = NULL;
620         result.difficult = true;
621         result.exits = false;
622         move_sd_regions (&regions, scops);
623         exits.release ();
624         break;
625       }
626     case GBB_COND_HEADER:
627       {
628         auto_vec<sd_region, 3> regions;
629         struct scopdet_info sinfo;
630         vec<basic_block> dominated;
631         int i;
632         basic_block dom_bb;
633         basic_block last_exit = NULL;
634         edge e;
635         result.exits = false;
636
637         /* First check the successors of BB, and check if it is
638            possible to join the different branches.  */
639         FOR_EACH_VEC_SAFE_ELT (bb->succs, i, e)
640           {
641             /* Ignore loop exits.  They will be handled after the loop
642                body.  */
643             if (loop_exits_to_bb_p (loop, e->dest))
644               {
645                 result.exits = true;
646                 continue;
647               }
648
649             /* Do not follow edges that lead to the end of the
650                conditions block.  For example, in
651
652                |   0
653                |  /|\
654                | 1 2 |
655                | | | |
656                | 3 4 |
657                |  \|/
658                |   6
659
660                the edge from 0 => 6.  Only check if all paths lead to
661                the same node 6.  */
662
663             if (!single_pred_p (e->dest))
664               {
665                 /* Check, if edge leads directly to the end of this
666                    condition.  */
667                 if (!last_exit)
668                   last_exit = e->dest;
669
670                 if (e->dest != last_exit)
671                   result.difficult = true;
672
673                 continue;
674               }
675
676             if (!dominated_by_p (CDI_DOMINATORS, e->dest, bb))
677               {
678                 result.difficult = true;
679                 continue;
680               }
681
682             sinfo = build_scops_1 (e->dest, outermost_loop, &regions, loop);
683
684             result.exits |= sinfo.exits;
685             result.difficult |= sinfo.difficult;
686
687             /* Checks, if all branches end at the same point.
688                If that is true, the condition stays joinable.
689                Have a look at the example above.  */
690             if (sinfo.exit)
691               {
692                 if (!last_exit)
693                   last_exit = sinfo.exit;
694
695                 if (sinfo.exit != last_exit)
696                   result.difficult = true;
697               }
698             else
699               result.difficult = true;
700           }
701
702         if (!last_exit)
703           result.difficult = true;
704
705         /* Join the branches of the condition if possible.  */
706         if (!result.exits && !result.difficult)
707           {
708             /* Only return a next pointer if we dominate this pointer.
709                Otherwise it will be handled by the bb dominating it.  */
710             if (dominated_by_p (CDI_DOMINATORS, last_exit, bb)
711                 && last_exit != bb)
712               result.next = last_exit;
713             else
714               result.next = NULL;
715
716             result.exit = last_exit;
717
718             regions.release ();
719             break;
720           }
721
722         /* Scan remaining bbs dominated by BB.  */
723         dominated = get_dominated_by (CDI_DOMINATORS, bb);
724
725         FOR_EACH_VEC_ELT (dominated, i, dom_bb)
726           {
727             /* Ignore loop exits: they will be handled after the loop body.  */
728             if (loop_depth (find_common_loop (loop, dom_bb->loop_father))
729                 < loop_depth (loop))
730               {
731                 result.exits = true;
732                 continue;
733               }
734
735             /* Ignore the bbs processed above.  */
736             if (single_pred_p (dom_bb) && single_pred (dom_bb) == bb)
737               continue;
738
739             if (loop_depth (loop) > loop_depth (dom_bb->loop_father))
740               sinfo = build_scops_1 (dom_bb, outermost_loop, &regions,
741                                      loop_outer (loop));
742             else
743               sinfo = build_scops_1 (dom_bb, outermost_loop, &regions, loop);
744
745             result.exits |= sinfo.exits;
746             result.difficult = true;
747             result.exit = NULL;
748           }
749
750         dominated.release ();
751
752         result.next = NULL;
753         move_sd_regions (&regions, scops);
754
755         break;
756       }
757
758     default:
759       gcc_unreachable ();
760     }
761
762   return result;
763 }
764
765 /* Starting from CURRENT we walk the dominance tree and add new sd_regions to
766    SCOPS. The analyse if a sd_region can be handled is based on the value
767    of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change.  LOOP
768    is the loop in which CURRENT is handled.
769
770    TODO: These functions got a little bit big. They definitely should be cleaned
771          up.  */
772
773 static struct scopdet_info
774 build_scops_1 (basic_block current, loop_p outermost_loop,
775                vec<sd_region> *scops, loop_p loop)
776 {
777   bool in_scop = false;
778   sd_region open_scop;
779   struct scopdet_info sinfo;
780
781   /* Initialize result.  */
782   struct scopdet_info result;
783   result.exits = false;
784   result.difficult = false;
785   result.next = NULL;
786   result.exit = NULL;
787   open_scop.entry = NULL;
788   open_scop.exit = NULL;
789   sinfo.exit = NULL;
790
791   /* Loop over the dominance tree.  If we meet a difficult bb, close
792      the current SCoP.  Loop and condition header start a new layer,
793      and can only be added if all bbs in deeper layers are simple.  */
794   while (current != NULL)
795     {
796       sinfo = scopdet_basic_block_info (current, outermost_loop, scops,
797                                         get_bb_type (current, loop));
798
799       if (!in_scop && !(sinfo.exits || sinfo.difficult))
800         {
801           open_scop.entry = current;
802           open_scop.exit = NULL;
803           in_scop = true;
804         }
805       else if (in_scop && (sinfo.exits || sinfo.difficult))
806         {
807           open_scop.exit = current;
808           scops->safe_push (open_scop);
809           in_scop = false;
810         }
811
812       result.difficult |= sinfo.difficult;
813       result.exits |= sinfo.exits;
814
815       current = sinfo.next;
816     }
817
818   /* Try to close open_scop, if we are still in an open SCoP.  */
819   if (in_scop)
820     {
821       open_scop.exit = sinfo.exit;
822       gcc_assert (open_scop.exit);
823       scops->safe_push (open_scop);
824     }
825
826   result.exit = sinfo.exit;
827   return result;
828 }
829
830 /* Checks if a bb is contained in REGION.  */
831
832 static bool
833 bb_in_sd_region (basic_block bb, sd_region *region)
834 {
835   return bb_in_region (bb, region->entry, region->exit);
836 }
837
838 /* Returns the single entry edge of REGION, if it does not exits NULL.  */
839
840 static edge
841 find_single_entry_edge (sd_region *region)
842 {
843   edge e;
844   edge_iterator ei;
845   edge entry = NULL;
846
847   FOR_EACH_EDGE (e, ei, region->entry->preds)
848     if (!bb_in_sd_region (e->src, region))
849       {
850         if (entry)
851           {
852             entry = NULL;
853             break;
854           }
855
856         else
857           entry = e;
858       }
859
860   return entry;
861 }
862
863 /* Returns the single exit edge of REGION, if it does not exits NULL.  */
864
865 static edge
866 find_single_exit_edge (sd_region *region)
867 {
868   edge e;
869   edge_iterator ei;
870   edge exit = NULL;
871
872   FOR_EACH_EDGE (e, ei, region->exit->preds)
873     if (bb_in_sd_region (e->src, region))
874       {
875         if (exit)
876           {
877             exit = NULL;
878             break;
879           }
880
881         else
882           exit = e;
883       }
884
885   return exit;
886 }
887
888 /* Create a single entry edge for REGION.  */
889
890 static void
891 create_single_entry_edge (sd_region *region)
892 {
893   if (find_single_entry_edge (region))
894     return;
895
896   /* There are multiple predecessors for bb_3
897
898   |  1  2
899   |  | /
900   |  |/
901   |  3  <- entry
902   |  |\
903   |  | |
904   |  4 ^
905   |  | |
906   |  |/
907   |  5
908
909   There are two edges (1->3, 2->3), that point from outside into the region,
910   and another one (5->3), a loop latch, lead to bb_3.
911
912   We split bb_3.
913
914   |  1  2
915   |  | /
916   |  |/
917   |3.0
918   |  |\     (3.0 -> 3.1) = single entry edge
919   |3.1 |        <- entry
920   |  | |
921   |  | |
922   |  4 ^
923   |  | |
924   |  |/
925   |  5
926
927   If the loop is part of the SCoP, we have to redirect the loop latches.
928
929   |  1  2
930   |  | /
931   |  |/
932   |3.0
933   |  |      (3.0 -> 3.1) = entry edge
934   |3.1          <- entry
935   |  |\
936   |  | |
937   |  4 ^
938   |  | |
939   |  |/
940   |  5  */
941
942   if (region->entry->loop_father->header != region->entry
943       || dominated_by_p (CDI_DOMINATORS,
944                          loop_latch_edge (region->entry->loop_father)->src,
945                          region->exit))
946     {
947       edge forwarder = split_block_after_labels (region->entry);
948       region->entry = forwarder->dest;
949     }
950   else
951     /* This case is never executed, as the loop headers seem always to have a
952        single edge pointing from outside into the loop.  */
953     gcc_unreachable ();
954
955   gcc_checking_assert (find_single_entry_edge (region));
956 }
957
958 /* Check if the sd_region, mentioned in EDGE, has no exit bb.  */
959
960 static bool
961 sd_region_without_exit (edge e)
962 {
963   sd_region *r = (sd_region *) e->aux;
964
965   if (r)
966     return r->exit == NULL;
967   else
968     return false;
969 }
970
971 /* Create a single exit edge for REGION.  */
972
973 static void
974 create_single_exit_edge (sd_region *region)
975 {
976   edge e;
977   edge_iterator ei;
978   edge forwarder = NULL;
979   basic_block exit;
980
981   /* We create a forwarder bb (5) for all edges leaving this region
982      (3->5, 4->5).  All other edges leading to the same bb, are moved
983      to a new bb (6).  If these edges where part of another region (2->5)
984      we update the region->exit pointer, of this region.
985
986      To identify which edge belongs to which region we depend on the e->aux
987      pointer in every edge.  It points to the region of the edge or to NULL,
988      if the edge is not part of any region.
989
990      1 2 3 4    1->5 no region,                 2->5 region->exit = 5,
991       \| |/     3->5 region->exit = NULL,       4->5 region->exit = NULL
992         5       <- exit
993
994      changes to
995
996      1 2 3 4    1->6 no region,                         2->6 region->exit = 6,
997      | | \/     3->5 no region,                         4->5 no region,
998      | |  5
999       \| /      5->6 region->exit = 6
1000         6
1001
1002      Now there is only a single exit edge (5->6).  */
1003   exit = region->exit;
1004   region->exit = NULL;
1005   forwarder = make_forwarder_block (exit, &sd_region_without_exit, NULL);
1006
1007   /* Unmark the edges, that are no longer exit edges.  */
1008   FOR_EACH_EDGE (e, ei, forwarder->src->preds)
1009     if (e->aux)
1010       e->aux = NULL;
1011
1012   /* Mark the new exit edge.  */
1013   single_succ_edge (forwarder->src)->aux = region;
1014
1015   /* Update the exit bb of all regions, where exit edges lead to
1016      forwarder->dest.  */
1017   FOR_EACH_EDGE (e, ei, forwarder->dest->preds)
1018     if (e->aux)
1019       ((sd_region *) e->aux)->exit = forwarder->dest;
1020
1021   gcc_checking_assert (find_single_exit_edge (region));
1022 }
1023
1024 /* Unmark the exit edges of all REGIONS.
1025    See comment in "create_single_exit_edge". */
1026
1027 static void
1028 unmark_exit_edges (vec<sd_region> regions)
1029 {
1030   int i;
1031   sd_region *s;
1032   edge e;
1033   edge_iterator ei;
1034
1035   FOR_EACH_VEC_ELT (regions, i, s)
1036     FOR_EACH_EDGE (e, ei, s->exit->preds)
1037       e->aux = NULL;
1038 }
1039
1040
1041 /* Mark the exit edges of all REGIONS.
1042    See comment in "create_single_exit_edge". */
1043
1044 static void
1045 mark_exit_edges (vec<sd_region> regions)
1046 {
1047   int i;
1048   sd_region *s;
1049   edge e;
1050   edge_iterator ei;
1051
1052   FOR_EACH_VEC_ELT (regions, i, s)
1053     FOR_EACH_EDGE (e, ei, s->exit->preds)
1054       if (bb_in_sd_region (e->src, s))
1055         e->aux = s;
1056 }
1057
1058 /* Create for all scop regions a single entry and a single exit edge.  */
1059
1060 static void
1061 create_sese_edges (vec<sd_region> regions)
1062 {
1063   int i;
1064   sd_region *s;
1065
1066   FOR_EACH_VEC_ELT (regions, i, s)
1067     create_single_entry_edge (s);
1068
1069   mark_exit_edges (regions);
1070
1071   FOR_EACH_VEC_ELT (regions, i, s)
1072     /* Don't handle multiple edges exiting the function.  */
1073     if (!find_single_exit_edge (s)
1074         && s->exit != EXIT_BLOCK_PTR_FOR_FN (cfun))
1075       create_single_exit_edge (s);
1076
1077   unmark_exit_edges (regions);
1078
1079   calculate_dominance_info (CDI_DOMINATORS);
1080   fix_loop_structure (NULL);
1081
1082 #ifdef ENABLE_CHECKING
1083   verify_loop_structure ();
1084   verify_ssa (false, true);
1085 #endif
1086 }
1087
1088 /* Create graphite SCoPs from an array of scop detection REGIONS.  */
1089
1090 static void
1091 build_graphite_scops (vec<sd_region> regions,
1092                       vec<scop_p> *scops)
1093 {
1094   int i;
1095   sd_region *s;
1096
1097   FOR_EACH_VEC_ELT (regions, i, s)
1098     {
1099       edge entry = find_single_entry_edge (s);
1100       edge exit = find_single_exit_edge (s);
1101       scop_p scop;
1102
1103       if (!exit)
1104         continue;
1105
1106       scop = new_scop (new_sese (entry, exit));
1107       scops->safe_push (scop);
1108
1109       /* Are there overlapping SCoPs?  */
1110 #ifdef ENABLE_CHECKING
1111         {
1112           int j;
1113           sd_region *s2;
1114
1115           FOR_EACH_VEC_ELT (regions, j, s2)
1116             if (s != s2)
1117               gcc_assert (!bb_in_sd_region (s->entry, s2));
1118         }
1119 #endif
1120     }
1121 }
1122
1123 /* Returns true when BB contains only close phi nodes.  */
1124
1125 static bool
1126 contains_only_close_phi_nodes (basic_block bb)
1127 {
1128   gimple_stmt_iterator gsi;
1129
1130   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1131     if (gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
1132       return false;
1133
1134   return true;
1135 }
1136
1137 /* Print statistics for SCOP to FILE.  */
1138
1139 static void
1140 print_graphite_scop_statistics (FILE* file, scop_p scop)
1141 {
1142   long n_bbs = 0;
1143   long n_loops = 0;
1144   long n_stmts = 0;
1145   long n_conditions = 0;
1146   long n_p_bbs = 0;
1147   long n_p_loops = 0;
1148   long n_p_stmts = 0;
1149   long n_p_conditions = 0;
1150
1151   basic_block bb;
1152
1153   FOR_ALL_BB_FN (bb, cfun)
1154     {
1155       gimple_stmt_iterator psi;
1156       loop_p loop = bb->loop_father;
1157
1158       if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
1159         continue;
1160
1161       n_bbs++;
1162       n_p_bbs += bb->count;
1163
1164       if (EDGE_COUNT (bb->succs) > 1)
1165         {
1166           n_conditions++;
1167           n_p_conditions += bb->count;
1168         }
1169
1170       for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
1171         {
1172           n_stmts++;
1173           n_p_stmts += bb->count;
1174         }
1175
1176       if (loop->header == bb && loop_in_sese_p (loop, SCOP_REGION (scop)))
1177         {
1178           n_loops++;
1179           n_p_loops += bb->count;
1180         }
1181
1182     }
1183
1184   fprintf (file, "\nBefore limit_scops SCoP statistics (");
1185   fprintf (file, "BBS:%ld, ", n_bbs);
1186   fprintf (file, "LOOPS:%ld, ", n_loops);
1187   fprintf (file, "CONDITIONS:%ld, ", n_conditions);
1188   fprintf (file, "STMTS:%ld)\n", n_stmts);
1189   fprintf (file, "\nBefore limit_scops SCoP profiling statistics (");
1190   fprintf (file, "BBS:%ld, ", n_p_bbs);
1191   fprintf (file, "LOOPS:%ld, ", n_p_loops);
1192   fprintf (file, "CONDITIONS:%ld, ", n_p_conditions);
1193   fprintf (file, "STMTS:%ld)\n", n_p_stmts);
1194 }
1195
1196 /* Print statistics for SCOPS to FILE.  */
1197
1198 static void
1199 print_graphite_statistics (FILE* file, vec<scop_p> scops)
1200 {
1201   int i;
1202   scop_p scop;
1203
1204   FOR_EACH_VEC_ELT (scops, i, scop)
1205     print_graphite_scop_statistics (file, scop);
1206 }
1207
1208 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
1209
1210    Example:
1211
1212    for (i      |
1213      {         |
1214        for (j  |  SCoP 1
1215        for (k  |
1216      }         |
1217
1218    * SCoP frontier, as this line is not surrounded by any loop. *
1219
1220    for (l      |  SCoP 2
1221
1222    This is necessary as scalar evolution and parameter detection need a
1223    outermost loop to initialize parameters correctly.
1224
1225    TODO: FIX scalar evolution and parameter detection to allow more flexible
1226          SCoP frontiers.  */
1227
1228 static void
1229 limit_scops (vec<scop_p> *scops)
1230 {
1231   auto_vec<sd_region, 3> regions;
1232
1233   int i;
1234   scop_p scop;
1235
1236   FOR_EACH_VEC_ELT (*scops, i, scop)
1237     {
1238       int j;
1239       loop_p loop;
1240       sese region = SCOP_REGION (scop);
1241       build_sese_loop_nests (region);
1242
1243       FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), j, loop)
1244         if (!loop_in_sese_p (loop_outer (loop), region)
1245             && single_exit (loop))
1246           {
1247             sd_region open_scop;
1248             open_scop.entry = loop->header;
1249             open_scop.exit = single_exit (loop)->dest;
1250
1251             /* This is a hack on top of the limit_scops hack.  The
1252                limit_scops hack should disappear all together.  */
1253             if (single_succ_p (open_scop.exit)
1254                 && contains_only_close_phi_nodes (open_scop.exit))
1255               open_scop.exit = single_succ_edge (open_scop.exit)->dest;
1256
1257             regions.safe_push (open_scop);
1258           }
1259     }
1260
1261   free_scops (*scops);
1262   scops->create (3);
1263
1264   create_sese_edges (regions);
1265   build_graphite_scops (regions, scops);
1266 }
1267
1268 /* Returns true when P1 and P2 are close phis with the same
1269    argument.  */
1270
1271 static inline bool
1272 same_close_phi_node (gphi *p1, gphi *p2)
1273 {
1274   return operand_equal_p (gimple_phi_arg_def (p1, 0),
1275                           gimple_phi_arg_def (p2, 0), 0);
1276 }
1277
1278 /* Remove the close phi node at GSI and replace its rhs with the rhs
1279    of PHI.  */
1280
1281 static void
1282 remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
1283 {
1284   gimple use_stmt;
1285   use_operand_p use_p;
1286   imm_use_iterator imm_iter;
1287   tree res = gimple_phi_result (phi);
1288   tree def = gimple_phi_result (gsi->phi ());
1289
1290   gcc_assert (same_close_phi_node (phi, gsi->phi ()));
1291
1292   FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
1293     {
1294       FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1295         SET_USE (use_p, res);
1296
1297       update_stmt (use_stmt);
1298       
1299       /* It is possible that we just created a duplicate close-phi
1300          for an already-processed containing loop.  Check for this
1301          case and clean it up.  */
1302       if (gimple_code (use_stmt) == GIMPLE_PHI
1303           && gimple_phi_num_args (use_stmt) == 1)
1304         make_close_phi_nodes_unique (gimple_bb (use_stmt));
1305     }
1306
1307   remove_phi_node (gsi, true);
1308 }
1309
1310 /* Removes all the close phi duplicates from BB.  */
1311
1312 static void
1313 make_close_phi_nodes_unique (basic_block bb)
1314 {
1315   gphi_iterator psi;
1316
1317   for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1318     {
1319       gphi_iterator gsi = psi;
1320       gphi *phi = psi.phi ();
1321
1322       /* At this point, PHI should be a close phi in normal form.  */
1323       gcc_assert (gimple_phi_num_args (phi) == 1);
1324
1325       /* Iterate over the next phis and remove duplicates.  */
1326       gsi_next (&gsi);
1327       while (!gsi_end_p (gsi))
1328         if (same_close_phi_node (phi, gsi.phi ()))
1329           remove_duplicate_close_phi (phi, &gsi);
1330         else
1331           gsi_next (&gsi);
1332     }
1333 }
1334
1335 /* Transforms LOOP to the canonical loop closed SSA form.  */
1336
1337 static void
1338 canonicalize_loop_closed_ssa (loop_p loop)
1339 {
1340   edge e = single_exit (loop);
1341   basic_block bb;
1342
1343   if (!e || e->flags & EDGE_ABNORMAL)
1344     return;
1345
1346   bb = e->dest;
1347
1348   if (single_pred_p (bb))
1349     {
1350       e = split_block_after_labels (bb);
1351       make_close_phi_nodes_unique (e->src);
1352     }
1353   else
1354     {
1355       gphi_iterator psi;
1356       basic_block close = split_edge (e);
1357
1358       e = single_succ_edge (close);
1359
1360       for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1361         {
1362           gphi *phi = psi.phi ();
1363           unsigned i;
1364
1365           for (i = 0; i < gimple_phi_num_args (phi); i++)
1366             if (gimple_phi_arg_edge (phi, i) == e)
1367               {
1368                 tree res, arg = gimple_phi_arg_def (phi, i);
1369                 use_operand_p use_p;
1370                 gphi *close_phi;
1371
1372                 if (TREE_CODE (arg) != SSA_NAME)
1373                   continue;
1374
1375                 close_phi = create_phi_node (NULL_TREE, close);
1376                 res = create_new_def_for (arg, close_phi,
1377                                           gimple_phi_result_ptr (close_phi));
1378                 add_phi_arg (close_phi, arg,
1379                              gimple_phi_arg_edge (close_phi, 0),
1380                              UNKNOWN_LOCATION);
1381                 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
1382                 replace_exp (use_p, res);
1383                 update_stmt (phi);
1384               }
1385         }
1386
1387       make_close_phi_nodes_unique (close);
1388     }
1389
1390   /* The code above does not properly handle changes in the post dominance
1391      information (yet).  */
1392   free_dominance_info (CDI_POST_DOMINATORS);
1393 }
1394
1395 /* Converts the current loop closed SSA form to a canonical form
1396    expected by the Graphite code generation.
1397
1398    The loop closed SSA form has the following invariant: a variable
1399    defined in a loop that is used outside the loop appears only in the
1400    phi nodes in the destination of the loop exit.  These phi nodes are
1401    called close phi nodes.
1402
1403    The canonical loop closed SSA form contains the extra invariants:
1404
1405    - when the loop contains only one exit, the close phi nodes contain
1406    only one argument.  That implies that the basic block that contains
1407    the close phi nodes has only one predecessor, that is a basic block
1408    in the loop.
1409
1410    - the basic block containing the close phi nodes does not contain
1411    other statements.
1412
1413    - there exist only one phi node per definition in the loop.
1414 */
1415
1416 static void
1417 canonicalize_loop_closed_ssa_form (void)
1418 {
1419   loop_p loop;
1420
1421 #ifdef ENABLE_CHECKING
1422   verify_loop_closed_ssa (true);
1423 #endif
1424
1425   FOR_EACH_LOOP (loop, 0)
1426     canonicalize_loop_closed_ssa (loop);
1427
1428   rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1429   update_ssa (TODO_update_ssa);
1430
1431 #ifdef ENABLE_CHECKING
1432   verify_loop_closed_ssa (true);
1433 #endif
1434 }
1435
1436 /* Find Static Control Parts (SCoP) in the current function and pushes
1437    them to SCOPS.  */
1438
1439 void
1440 build_scops (vec<scop_p> *scops)
1441 {
1442   struct loop *loop = current_loops->tree_root;
1443   auto_vec<sd_region, 3> regions;
1444
1445   canonicalize_loop_closed_ssa_form ();
1446   build_scops_1 (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1447                  ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father,
1448                  &regions, loop);
1449   create_sese_edges (regions);
1450   build_graphite_scops (regions, scops);
1451
1452   if (dump_file && (dump_flags & TDF_DETAILS))
1453     print_graphite_statistics (dump_file, *scops);
1454
1455   limit_scops (scops);
1456   regions.release ();
1457
1458   if (dump_file && (dump_flags & TDF_DETAILS))
1459     fprintf (dump_file, "\nnumber of SCoPs: %d\n",
1460              scops ? scops->length () : 0);
1461 }
1462
1463 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1464    different colors.  If there are not enough colors, paint the
1465    remaining SCoPs in gray.
1466
1467    Special nodes:
1468    - "*" after the node number denotes the entry of a SCoP,
1469    - "#" after the node number denotes the exit of a SCoP,
1470    - "()" around the node number denotes the entry or the
1471      exit nodes of the SCOP.  These are not part of SCoP.  */
1472
1473 static void
1474 dot_all_scops_1 (FILE *file, vec<scop_p> scops)
1475 {
1476   basic_block bb;
1477   edge e;
1478   edge_iterator ei;
1479   scop_p scop;
1480   const char* color;
1481   int i;
1482
1483   /* Disable debugging while printing graph.  */
1484   int tmp_dump_flags = dump_flags;
1485   dump_flags = 0;
1486
1487   fprintf (file, "digraph all {\n");
1488
1489   FOR_ALL_BB_FN (bb, cfun)
1490     {
1491       int part_of_scop = false;
1492
1493       /* Use HTML for every bb label.  So we are able to print bbs
1494          which are part of two different SCoPs, with two different
1495          background colors.  */
1496       fprintf (file, "%d [label=<\n  <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1497                      bb->index);
1498       fprintf (file, "CELLSPACING=\"0\">\n");
1499
1500       /* Select color for SCoP.  */
1501       FOR_EACH_VEC_ELT (scops, i, scop)
1502         {
1503           sese region = SCOP_REGION (scop);
1504           if (bb_in_sese_p (bb, region)
1505               || (SESE_EXIT_BB (region) == bb)
1506               || (SESE_ENTRY_BB (region) == bb))
1507             {
1508               switch (i % 17)
1509                 {
1510                 case 0: /* red */
1511                   color = "#e41a1c";
1512                   break;
1513                 case 1: /* blue */
1514                   color = "#377eb8";
1515                   break;
1516                 case 2: /* green */
1517                   color = "#4daf4a";
1518                   break;
1519                 case 3: /* purple */
1520                   color = "#984ea3";
1521                   break;
1522                 case 4: /* orange */
1523                   color = "#ff7f00";
1524                   break;
1525                 case 5: /* yellow */
1526                   color = "#ffff33";
1527                   break;
1528                 case 6: /* brown */
1529                   color = "#a65628";
1530                   break;
1531                 case 7: /* rose */
1532                   color = "#f781bf";
1533                   break;
1534                 case 8:
1535                   color = "#8dd3c7";
1536                   break;
1537                 case 9:
1538                   color = "#ffffb3";
1539                   break;
1540                 case 10:
1541                   color = "#bebada";
1542                   break;
1543                 case 11:
1544                   color = "#fb8072";
1545                   break;
1546                 case 12:
1547                   color = "#80b1d3";
1548                   break;
1549                 case 13:
1550                   color = "#fdb462";
1551                   break;
1552                 case 14:
1553                   color = "#b3de69";
1554                   break;
1555                 case 15:
1556                   color = "#fccde5";
1557                   break;
1558                 case 16:
1559                   color = "#bc80bd";
1560                   break;
1561                 default: /* gray */
1562                   color = "#999999";
1563                 }
1564
1565               fprintf (file, "    <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
1566
1567               if (!bb_in_sese_p (bb, region))
1568                 fprintf (file, " (");
1569
1570               if (bb == SESE_ENTRY_BB (region)
1571                   && bb == SESE_EXIT_BB (region))
1572                 fprintf (file, " %d*# ", bb->index);
1573               else if (bb == SESE_ENTRY_BB (region))
1574                 fprintf (file, " %d* ", bb->index);
1575               else if (bb == SESE_EXIT_BB (region))
1576                 fprintf (file, " %d# ", bb->index);
1577               else
1578                 fprintf (file, " %d ", bb->index);
1579
1580               if (!bb_in_sese_p (bb,region))
1581                 fprintf (file, ")");
1582
1583               fprintf (file, "</TD></TR>\n");
1584               part_of_scop  = true;
1585             }
1586         }
1587
1588       if (!part_of_scop)
1589         {
1590           fprintf (file, "    <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1591           fprintf (file, " %d </TD></TR>\n", bb->index);
1592         }
1593       fprintf (file, "  </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1594     }
1595
1596   FOR_ALL_BB_FN (bb, cfun)
1597     {
1598       FOR_EACH_EDGE (e, ei, bb->succs)
1599               fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
1600     }
1601
1602   fputs ("}\n\n", file);
1603
1604   /* Enable debugging again.  */
1605   dump_flags = tmp_dump_flags;
1606 }
1607
1608 /* Display all SCoPs using dotty.  */
1609
1610 DEBUG_FUNCTION void
1611 dot_all_scops (vec<scop_p> scops)
1612 {
1613   /* When debugging, enable the following code.  This cannot be used
1614      in production compilers because it calls "system".  */
1615 #if 0
1616   int x;
1617   FILE *stream = fopen ("/tmp/allscops.dot", "w");
1618   gcc_assert (stream);
1619
1620   dot_all_scops_1 (stream, scops);
1621   fclose (stream);
1622
1623   x = system ("dotty /tmp/allscops.dot &");
1624 #else
1625   dot_all_scops_1 (stderr, scops);
1626 #endif
1627 }
1628
1629 /* Display all SCoPs using dotty.  */
1630
1631 DEBUG_FUNCTION void
1632 dot_scop (scop_p scop)
1633 {
1634   auto_vec<scop_p, 1> scops;
1635
1636   if (scop)
1637     scops.safe_push (scop);
1638
1639   /* When debugging, enable the following code.  This cannot be used
1640      in production compilers because it calls "system".  */
1641 #if 0
1642   {
1643     int x;
1644     FILE *stream = fopen ("/tmp/allscops.dot", "w");
1645     gcc_assert (stream);
1646
1647     dot_all_scops_1 (stream, scops);
1648     fclose (stream);
1649     x = system ("dotty /tmp/allscops.dot &");
1650   }
1651 #else
1652   dot_all_scops_1 (stderr, scops);
1653 #endif
1654 }
1655
1656 #endif