Fix type in the changelog entry,
[official-gcc.git] / gcc / graphite-scop-detection.c
blob7c0dc21b01bf7a25fd8872eb35415185789fd8d6
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>.
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
24 #ifdef HAVE_isl
25 /* Workaround for GMP 5.1.3 bug, see PR56019. */
26 #include <stddef.h>
28 #include <isl/constraint.h>
29 #include <isl/set.h>
30 #include <isl/map.h>
31 #include <isl/union_map.h>
33 #include "system.h"
34 #include "coretypes.h"
35 #include "backend.h"
36 #include "cfghooks.h"
37 #include "tree.h"
38 #include "gimple.h"
39 #include "ssa.h"
40 #include "fold-const.h"
41 #include "gimple-iterator.h"
42 #include "tree-ssa-loop-manip.h"
43 #include "tree-ssa-loop-niter.h"
44 #include "tree-ssa-loop.h"
45 #include "tree-into-ssa.h"
46 #include "tree-ssa.h"
47 #include "cfgloop.h"
48 #include "tree-data-ref.h"
49 #include "tree-scalar-evolution.h"
50 #include "tree-pass.h"
51 #include "graphite-poly.h"
52 #include "tree-ssa-propagate.h"
53 #include "graphite-scop-detection.h"
54 #include "gimple-pretty-print.h"
56 /* Forward declarations. */
57 static void make_close_phi_nodes_unique (basic_block);
59 /* The type of the analyzed basic block. */
61 enum gbb_type {
62 GBB_UNKNOWN,
63 GBB_LOOP_SING_EXIT_HEADER,
64 GBB_LOOP_MULT_EXIT_HEADER,
65 GBB_LOOP_EXIT,
66 GBB_COND_HEADER,
67 GBB_SIMPLE,
68 GBB_LAST
71 /* Detect the type of BB. Loop headers are only marked, if they are
72 new. This means their loop_father is different to LAST_LOOP.
73 Otherwise they are treated like any other bb and their type can be
74 any other type. */
76 static gbb_type
77 get_bb_type (basic_block bb, struct loop *last_loop)
79 vec<basic_block> dom;
80 int nb_dom;
81 struct loop *loop = bb->loop_father;
83 /* Check, if we entry into a new loop. */
84 if (loop != last_loop)
86 if (single_exit (loop) != NULL)
87 return GBB_LOOP_SING_EXIT_HEADER;
88 else if (loop->num != 0)
89 return GBB_LOOP_MULT_EXIT_HEADER;
90 else
91 return GBB_COND_HEADER;
94 dom = get_dominated_by (CDI_DOMINATORS, bb);
95 nb_dom = dom.length ();
96 dom.release ();
98 if (nb_dom == 0)
99 return GBB_LAST;
101 if (nb_dom == 1 && single_succ_p (bb))
102 return GBB_SIMPLE;
104 return GBB_COND_HEADER;
107 /* A SCoP detection region, defined using bbs as borders.
109 All control flow touching this region, comes in passing basic_block
110 ENTRY and leaves passing basic_block EXIT. By using bbs instead of
111 edges for the borders we are able to represent also regions that do
112 not have a single entry or exit edge.
114 But as they have a single entry basic_block and a single exit
115 basic_block, we are able to generate for every sd_region a single
116 entry and exit edge.
120 3 <- entry
123 / \ This region contains: {3, 4, 5, 6, 7, 8}
128 9 <- exit */
131 struct sd_region
133 /* The entry bb dominates all bbs in the sd_region. It is part of
134 the region. */
135 basic_block entry;
137 /* The exit bb postdominates all bbs in the sd_region, but is not
138 part of the region. */
139 basic_block exit;
144 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
146 static void
147 move_sd_regions (vec<sd_region> *source, vec<sd_region> *target)
149 sd_region *s;
150 int i;
152 FOR_EACH_VEC_ELT (*source, i, s)
153 target->safe_push (*s);
155 source->release ();
158 /* Something like "n * m" is not allowed. */
160 static bool
161 graphite_can_represent_init (tree e)
163 switch (TREE_CODE (e))
165 case POLYNOMIAL_CHREC:
166 return graphite_can_represent_init (CHREC_LEFT (e))
167 && graphite_can_represent_init (CHREC_RIGHT (e));
169 case MULT_EXPR:
170 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
171 return graphite_can_represent_init (TREE_OPERAND (e, 0))
172 && tree_fits_shwi_p (TREE_OPERAND (e, 1));
173 else
174 return graphite_can_represent_init (TREE_OPERAND (e, 1))
175 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
177 case PLUS_EXPR:
178 case POINTER_PLUS_EXPR:
179 case MINUS_EXPR:
180 return graphite_can_represent_init (TREE_OPERAND (e, 0))
181 && graphite_can_represent_init (TREE_OPERAND (e, 1));
183 case NEGATE_EXPR:
184 case BIT_NOT_EXPR:
185 CASE_CONVERT:
186 case NON_LVALUE_EXPR:
187 return graphite_can_represent_init (TREE_OPERAND (e, 0));
189 default:
190 break;
193 return true;
196 /* Return true when SCEV can be represented in the polyhedral model.
198 An expression can be represented, if it can be expressed as an
199 affine expression. For loops (i, j) and parameters (m, n) all
200 affine expressions are of the form:
202 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
204 1 i + 20 j + (-2) m + 25
206 Something like "i * n" or "n * m" is not allowed. */
208 static bool
209 graphite_can_represent_scev (tree scev)
211 if (chrec_contains_undetermined (scev))
212 return false;
214 /* We disable the handling of pointer types, because it’s currently not
215 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
216 the only nodes, which are disabled in case they are pointers to object
217 types, but this can be changed. */
219 if (POINTER_TYPE_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
220 return false;
222 switch (TREE_CODE (scev))
224 case NEGATE_EXPR:
225 case BIT_NOT_EXPR:
226 CASE_CONVERT:
227 case NON_LVALUE_EXPR:
228 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
230 case PLUS_EXPR:
231 case POINTER_PLUS_EXPR:
232 case MINUS_EXPR:
233 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
234 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
236 case MULT_EXPR:
237 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
238 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
239 && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
240 && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
241 && graphite_can_represent_init (scev)
242 && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
243 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
245 case POLYNOMIAL_CHREC:
246 /* Check for constant strides. With a non constant stride of
247 'n' we would have a value of 'iv * n'. Also check that the
248 initial value can represented: for example 'n * m' cannot be
249 represented. */
250 if (!evolution_function_right_is_integer_cst (scev)
251 || !graphite_can_represent_init (scev))
252 return false;
253 return graphite_can_represent_scev (CHREC_LEFT (scev));
255 default:
256 break;
259 /* Only affine functions can be represented. */
260 if (tree_contains_chrecs (scev, NULL)
261 || !scev_is_linear_expression (scev))
262 return false;
264 return true;
268 /* Return true when EXPR can be represented in the polyhedral model.
270 This means an expression can be represented, if it is linear with
271 respect to the loops and the strides are non parametric.
272 LOOP is the place where the expr will be evaluated. SCOP_ENTRY defines the
273 entry of the region we analyse. */
275 static bool
276 graphite_can_represent_expr (basic_block scop_entry, loop_p loop,
277 tree expr)
279 tree scev = analyze_scalar_evolution (loop, expr);
281 scev = instantiate_scev (scop_entry, loop, scev);
283 return graphite_can_represent_scev (scev);
286 /* Return true if the data references of STMT can be represented by
287 Graphite. */
289 static bool
290 stmt_has_simple_data_refs_p (loop_p outermost_loop ATTRIBUTE_UNUSED,
291 gimple *stmt)
293 data_reference_p dr;
294 int j;
295 bool res = true;
296 vec<data_reference_p> drs = vNULL;
297 loop_p outer;
299 for (outer = loop_containing_stmt (stmt); outer; outer = loop_outer (outer))
301 graphite_find_data_references_in_stmt (outer,
302 loop_containing_stmt (stmt),
303 stmt, &drs);
305 FOR_EACH_VEC_ELT (drs, j, dr)
307 int nb_subscripts = DR_NUM_DIMENSIONS (dr);
308 tree ref = DR_REF (dr);
310 for (int i = nb_subscripts - 1; i >= 0; i--)
312 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i))
313 || (TREE_CODE (ref) != ARRAY_REF
314 && TREE_CODE (ref) != MEM_REF
315 && TREE_CODE (ref) != COMPONENT_REF))
317 free_data_refs (drs);
318 return false;
321 ref = TREE_OPERAND (ref, 0);
325 free_data_refs (drs);
326 drs.create (0);
329 free_data_refs (drs);
330 return res;
333 /* Return true only when STMT is simple enough for being handled by
334 Graphite. This depends on SCOP_ENTRY, as the parameters are
335 initialized relatively to this basic block, the linear functions
336 are initialized to OUTERMOST_LOOP and BB is the place where we try
337 to evaluate the STMT. */
339 static bool
340 stmt_simple_for_scop_p (basic_block scop_entry, loop_p outermost_loop,
341 gimple *stmt, basic_block bb)
343 loop_p loop = bb->loop_father;
345 gcc_assert (scop_entry);
347 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
348 Calls have side-effects, except those to const or pure
349 functions. */
350 if (gimple_has_volatile_ops (stmt)
351 || (gimple_code (stmt) == GIMPLE_CALL
352 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
353 || (gimple_code (stmt) == GIMPLE_ASM))
355 if (dump_file && (dump_flags & TDF_DETAILS))
357 fprintf (dump_file, "[scop-detection-fail] ");
358 fprintf (dump_file, "Graphite cannot handle this stmt:\n");
359 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS);
362 return false;
365 if (is_gimple_debug (stmt))
366 return true;
368 if (!stmt_has_simple_data_refs_p (outermost_loop, stmt))
370 if (dump_file && (dump_flags & TDF_DETAILS))
372 fprintf (dump_file, "[scop-detection-fail] ");
373 fprintf (dump_file, "Graphite cannot handle data-refs in stmt:\n");
374 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS);
377 return false;
380 switch (gimple_code (stmt))
382 case GIMPLE_LABEL:
383 return true;
385 case GIMPLE_COND:
387 /* We can handle all binary comparisons. Inequalities are
388 also supported as they can be represented with union of
389 polyhedra. */
390 enum tree_code code = gimple_cond_code (stmt);
391 if (!(code == LT_EXPR
392 || code == GT_EXPR
393 || code == LE_EXPR
394 || code == GE_EXPR
395 || code == EQ_EXPR
396 || code == NE_EXPR))
398 if (dump_file && (dump_flags & TDF_DETAILS))
400 fprintf (dump_file, "[scop-detection-fail] ");
401 fprintf (dump_file, "Graphite cannot handle cond stmt:\n");
402 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS);
405 return false;
408 for (unsigned i = 0; i < 2; ++i)
410 tree op = gimple_op (stmt, i);
411 if (!graphite_can_represent_expr (scop_entry, loop, op)
412 /* We can only constrain on integer type. */
413 || (TREE_CODE (TREE_TYPE (op)) != INTEGER_TYPE))
415 if (dump_file && (dump_flags & TDF_DETAILS))
417 fprintf (dump_file, "[scop-detection-fail] ");
418 fprintf (dump_file, "Graphite cannot represent stmt:\n");
419 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS);
422 return false;
426 return true;
429 case GIMPLE_ASSIGN:
430 case GIMPLE_CALL:
431 return true;
433 default:
434 /* These nodes cut a new scope. */
435 if (dump_file && (dump_flags & TDF_DETAILS))
437 fprintf (dump_file, "[scop-detection-fail] ");
438 fprintf (dump_file, "Gimple stmt not handled in Graphite:\n");
439 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS);
441 return false;
444 return false;
447 /* Returns the statement of BB that contains a harmful operation: that
448 can be a function call with side effects, the induction variables
449 are not linear with respect to SCOP_ENTRY, etc. The current open
450 scop should end before this statement. The evaluation is limited using
451 OUTERMOST_LOOP as outermost loop that may change. */
453 static gimple *
454 harmful_stmt_in_bb (basic_block scop_entry, loop_p outer_loop, basic_block bb)
456 gimple_stmt_iterator gsi;
458 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
459 if (!stmt_simple_for_scop_p (scop_entry, outer_loop, gsi_stmt (gsi), bb))
460 return gsi_stmt (gsi);
462 return NULL;
465 /* Return true if LOOP can be represented in the polyhedral
466 representation. This is evaluated taking SCOP_ENTRY and
467 OUTERMOST_LOOP in mind. */
469 static bool
470 graphite_can_represent_loop (basic_block scop_entry, loop_p loop)
472 tree niter;
473 struct tree_niter_desc niter_desc;
475 if (!loop_nest_has_data_refs (loop))
477 if (dump_file && (dump_flags & TDF_DETAILS))
479 fprintf (dump_file, "[scop-detection-fail] ");
480 fprintf (dump_file, "Loop %d does not have any data reference.\n",
481 loop->num);
483 return false;
486 /* FIXME: For the moment, graphite cannot be used on loops that
487 iterate using induction variables that wrap. */
489 return number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
490 && niter_desc.control.no_overflow
491 && (niter = number_of_latch_executions (loop))
492 && !chrec_contains_undetermined (niter)
493 && graphite_can_represent_expr (scop_entry, loop, niter);
496 /* Store information needed by scopdet_* functions. */
498 struct scopdet_info
500 /* Exit of the open scop would stop if the current BB is harmful. */
501 basic_block exit;
503 /* Where the next scop would start if the current BB is harmful. */
504 basic_block next;
506 /* The bb or one of its children contains open loop exits. That means
507 loop exit nodes that are not surrounded by a loop dominated by bb. */
508 bool exits;
510 /* The bb or one of its children contains only structures we can handle. */
511 bool difficult;
514 static struct scopdet_info build_scops_1 (basic_block, loop_p,
515 vec<sd_region> *, loop_p);
517 /* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
518 to SCOPS. TYPE is the gbb_type of BB. */
520 static struct scopdet_info
521 scopdet_basic_block_info (basic_block bb, loop_p outermost_loop,
522 vec<sd_region> *scops, gbb_type type)
524 loop_p loop = bb->loop_father;
525 struct scopdet_info result;
526 gimple *stmt;
528 /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps. */
529 basic_block entry_block = ENTRY_BLOCK_PTR_FOR_FN (cfun);
530 stmt = harmful_stmt_in_bb (entry_block, outermost_loop, bb);
531 result.difficult = (stmt != NULL);
532 result.exit = NULL;
534 switch (type)
536 case GBB_LAST:
537 result.next = NULL;
538 result.exits = false;
540 /* Mark bbs terminating a SESE region difficult, if they start
541 a condition or if the block it exits to cannot be split
542 with make_forwarder_block. */
543 if (!single_succ_p (bb)
544 || bb_has_abnormal_pred (single_succ (bb)))
546 if (dump_file && (dump_flags & TDF_DETAILS))
548 fprintf (dump_file, "[scop-detection-fail] ");
549 fprintf (dump_file, "BB %d cannot be part of a scop.\n",
550 bb->index);
553 result.difficult = true;
555 else
556 result.exit = single_succ (bb);
558 break;
560 case GBB_SIMPLE:
561 result.next = single_succ (bb);
562 result.exits = false;
563 result.exit = single_succ (bb);
564 break;
566 case GBB_LOOP_SING_EXIT_HEADER:
568 auto_vec<sd_region, 3> regions;
569 struct scopdet_info sinfo;
570 edge exit_e = single_exit (loop);
572 sinfo = build_scops_1 (bb, outermost_loop, &regions, loop);
574 if (!graphite_can_represent_loop (entry_block, loop))
576 if (dump_file && (dump_flags & TDF_DETAILS))
578 fprintf (dump_file, "[scop-detection-fail] ");
579 fprintf (dump_file, "Graphite cannot represent loop %d.\n",
580 loop->num);
582 result.difficult = true;
585 result.difficult |= sinfo.difficult;
587 /* Try again with another loop level. */
588 if (result.difficult
589 && loop_depth (outermost_loop) + 1 == loop_depth (loop))
591 outermost_loop = loop;
593 regions.release ();
594 regions.create (3);
596 sinfo = scopdet_basic_block_info (bb, outermost_loop, scops, type);
598 result = sinfo;
599 result.difficult = true;
601 if (sinfo.difficult)
602 move_sd_regions (&regions, scops);
603 else
605 sd_region open_scop;
606 open_scop.entry = bb;
607 open_scop.exit = exit_e->dest;
608 scops->safe_push (open_scop);
609 regions.release ();
612 else
614 result.exit = exit_e->dest;
615 result.next = exit_e->dest;
617 /* If we do not dominate result.next, remove it. It's either
618 the exit block, or another bb dominates it and will
619 call the scop detection for this bb. */
620 if (!dominated_by_p (CDI_DOMINATORS, result.next, bb))
621 result.next = NULL;
623 if (exit_e->src->loop_father != loop)
624 result.next = NULL;
626 result.exits = false;
628 if (result.difficult)
629 move_sd_regions (&regions, scops);
630 else
631 regions.release ();
634 break;
637 case GBB_LOOP_MULT_EXIT_HEADER:
639 /* XXX: For now we just do not join loops with multiple exits. If the
640 exits lead to the same bb it may be possible to join the loop. */
641 auto_vec<sd_region, 3> regions;
642 vec<edge> exits = get_loop_exit_edges (loop);
643 edge e;
644 int i;
645 build_scops_1 (bb, loop, &regions, loop);
647 /* Scan the code dominated by this loop. This means all bbs, that are
648 are dominated by a bb in this loop, but are not part of this loop.
650 The easiest case:
651 - The loop exit destination is dominated by the exit sources.
653 TODO: We miss here the more complex cases:
654 - The exit destinations are dominated by another bb inside
655 the loop.
656 - The loop dominates bbs, that are not exit destinations. */
657 FOR_EACH_VEC_ELT (exits, i, e)
658 if (e->src->loop_father == loop
659 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src))
661 if (loop_outer (outermost_loop))
662 outermost_loop = loop_outer (outermost_loop);
664 /* Pass loop_outer to recognize e->dest as loop header in
665 build_scops_1. */
666 if (e->dest->loop_father->header == e->dest)
667 build_scops_1 (e->dest, outermost_loop, &regions,
668 loop_outer (e->dest->loop_father));
669 else
670 build_scops_1 (e->dest, outermost_loop, &regions,
671 e->dest->loop_father);
674 result.next = NULL;
675 result.exit = NULL;
676 result.difficult = true;
677 result.exits = false;
678 move_sd_regions (&regions, scops);
679 exits.release ();
680 break;
682 case GBB_COND_HEADER:
684 auto_vec<sd_region, 3> regions;
685 struct scopdet_info sinfo;
686 vec<basic_block> dominated;
687 int i;
688 basic_block dom_bb;
689 basic_block last_exit = NULL;
690 edge e;
691 result.exits = false;
693 /* First check the successors of BB, and check if it is
694 possible to join the different branches. */
695 FOR_EACH_VEC_SAFE_ELT (bb->succs, i, e)
697 /* Ignore loop exits. They will be handled after the loop
698 body. */
699 if (loop_exits_to_bb_p (loop, e->dest))
701 result.exits = true;
702 continue;
705 /* Do not follow edges that lead to the end of the
706 conditions block. For example, in
709 | /|\
710 | 1 2 |
711 | | | |
712 | 3 4 |
713 | \|/
716 the edge from 0 => 6. Only check if all paths lead to
717 the same node 6. */
719 if (!single_pred_p (e->dest))
721 /* Check, if edge leads directly to the end of this
722 condition. */
723 if (!last_exit)
724 last_exit = e->dest;
726 if (e->dest != last_exit)
727 result.difficult = true;
729 continue;
732 if (!dominated_by_p (CDI_DOMINATORS, e->dest, bb))
734 result.difficult = true;
735 continue;
738 sinfo = build_scops_1 (e->dest, outermost_loop, &regions, loop);
740 result.exits |= sinfo.exits;
741 result.difficult |= sinfo.difficult;
743 /* Checks, if all branches end at the same point.
744 If that is true, the condition stays joinable.
745 Have a look at the example above. */
746 if (sinfo.exit)
748 if (!last_exit)
749 last_exit = sinfo.exit;
751 if (sinfo.exit != last_exit)
752 result.difficult = true;
754 else
755 result.difficult = true;
758 if (!last_exit)
759 result.difficult = true;
761 /* Join the branches of the condition if possible. */
762 if (!result.exits && !result.difficult)
764 /* Only return a next pointer if we dominate this pointer.
765 Otherwise it will be handled by the bb dominating it. */
766 if (dominated_by_p (CDI_DOMINATORS, last_exit, bb)
767 && last_exit != bb)
768 result.next = last_exit;
769 else
770 result.next = NULL;
772 result.exit = last_exit;
774 regions.release ();
775 break;
778 /* Scan remaining bbs dominated by BB. */
779 dominated = get_dominated_by (CDI_DOMINATORS, bb);
781 FOR_EACH_VEC_ELT (dominated, i, dom_bb)
783 /* Ignore loop exits: they will be handled after the loop body. */
784 if (loop_depth (find_common_loop (loop, dom_bb->loop_father))
785 < loop_depth (loop))
787 result.exits = true;
788 continue;
791 /* Ignore the bbs processed above. */
792 if (single_pred_p (dom_bb) && single_pred (dom_bb) == bb)
793 continue;
795 if (loop_depth (loop) > loop_depth (dom_bb->loop_father))
796 sinfo = build_scops_1 (dom_bb, outermost_loop, &regions,
797 loop_outer (loop));
798 else
799 sinfo = build_scops_1 (dom_bb, outermost_loop, &regions, loop);
801 result.exits |= sinfo.exits;
802 result.difficult = true;
803 result.exit = NULL;
806 dominated.release ();
808 result.next = NULL;
809 move_sd_regions (&regions, scops);
811 break;
814 default:
815 gcc_unreachable ();
818 return result;
821 /* Starting from CURRENT we walk the dominance tree and add new sd_regions to
822 SCOPS. The analyse if a sd_region can be handled is based on the value
823 of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change. LOOP
824 is the loop in which CURRENT is handled.
826 TODO: These functions got a little bit big. They definitely should be cleaned
827 up. */
829 static struct scopdet_info
830 build_scops_1 (basic_block current, loop_p outermost_loop,
831 vec<sd_region> *scops, loop_p loop)
833 bool in_scop = false;
834 sd_region open_scop;
835 struct scopdet_info sinfo;
837 /* Initialize result. */
838 struct scopdet_info result;
839 result.exits = false;
840 result.difficult = false;
841 result.next = NULL;
842 result.exit = NULL;
843 open_scop.entry = NULL;
844 open_scop.exit = NULL;
845 sinfo.exit = NULL;
847 /* Loop over the dominance tree. If we meet a difficult bb, close
848 the current SCoP. Loop and condition header start a new layer,
849 and can only be added if all bbs in deeper layers are simple. */
850 while (current != NULL)
852 sinfo = scopdet_basic_block_info (current, outermost_loop, scops,
853 get_bb_type (current, loop));
855 if (!in_scop && !(sinfo.exits || sinfo.difficult))
857 open_scop.entry = current;
858 open_scop.exit = NULL;
859 in_scop = true;
861 else if (in_scop && (sinfo.exits || sinfo.difficult))
863 open_scop.exit = current;
864 scops->safe_push (open_scop);
865 in_scop = false;
868 result.difficult |= sinfo.difficult;
869 result.exits |= sinfo.exits;
871 current = sinfo.next;
874 /* Try to close open_scop, if we are still in an open SCoP. */
875 if (in_scop)
877 open_scop.exit = sinfo.exit;
878 gcc_assert (open_scop.exit);
879 if (open_scop.entry != open_scop.exit)
880 scops->safe_push (open_scop);
881 else
883 sinfo.difficult = true;
884 sinfo.exits = false;
885 sinfo.exit = NULL;
889 result.exit = sinfo.exit;
890 return result;
893 /* Checks if a bb is contained in REGION. */
895 static bool
896 bb_in_sd_region (basic_block bb, sd_region *region)
898 return bb_in_region (bb, region->entry, region->exit);
901 /* Returns the single entry edge of REGION, if it does not exits NULL. */
903 static edge
904 find_single_entry_edge (sd_region *region)
906 edge e;
907 edge_iterator ei;
908 edge entry = NULL;
910 FOR_EACH_EDGE (e, ei, region->entry->preds)
911 if (!bb_in_sd_region (e->src, region))
913 if (entry)
915 entry = NULL;
916 break;
919 else
920 entry = e;
923 return entry;
926 /* Returns the single exit edge of REGION, if it does not exits NULL. */
928 static edge
929 find_single_exit_edge (sd_region *region)
931 edge e;
932 edge_iterator ei;
933 edge exit = NULL;
935 FOR_EACH_EDGE (e, ei, region->exit->preds)
936 if (bb_in_sd_region (e->src, region))
938 if (exit)
940 exit = NULL;
941 break;
944 else
945 exit = e;
948 return exit;
951 /* Create a single entry edge for REGION. */
953 static void
954 create_single_entry_edge (sd_region *region)
956 if (find_single_entry_edge (region))
957 return;
959 /* There are multiple predecessors for bb_3
961 | 1 2
962 | | /
963 | |/
964 | 3 <- entry
965 | |\
966 | | |
967 | 4 ^
968 | | |
969 | |/
972 There are two edges (1->3, 2->3), that point from outside into the region,
973 and another one (5->3), a loop latch, lead to bb_3.
975 We split bb_3.
977 | 1 2
978 | | /
979 | |/
980 |3.0
981 | |\ (3.0 -> 3.1) = single entry edge
982 |3.1 | <- entry
983 | | |
984 | | |
985 | 4 ^
986 | | |
987 | |/
990 If the loop is part of the SCoP, we have to redirect the loop latches.
992 | 1 2
993 | | /
994 | |/
995 |3.0
996 | | (3.0 -> 3.1) = entry edge
997 |3.1 <- entry
998 | |\
999 | | |
1000 | 4 ^
1001 | | |
1002 | |/
1003 | 5 */
1005 if (region->entry->loop_father->header != region->entry
1006 || dominated_by_p (CDI_DOMINATORS,
1007 loop_latch_edge (region->entry->loop_father)->src,
1008 region->exit))
1010 edge forwarder = split_block_after_labels (region->entry);
1011 region->entry = forwarder->dest;
1013 else
1014 /* This case is never executed, as the loop headers seem always to have a
1015 single edge pointing from outside into the loop. */
1016 gcc_unreachable ();
1018 gcc_checking_assert (find_single_entry_edge (region));
1021 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
1023 static bool
1024 sd_region_without_exit (edge e)
1026 sd_region *r = (sd_region *) e->aux;
1028 if (r)
1029 return r->exit == NULL;
1030 else
1031 return false;
1034 /* Create a single exit edge for REGION. */
1036 static void
1037 create_single_exit_edge (sd_region *region)
1039 edge e;
1040 edge_iterator ei;
1041 edge forwarder = NULL;
1042 basic_block exit;
1044 /* We create a forwarder bb (5) for all edges leaving this region
1045 (3->5, 4->5). All other edges leading to the same bb, are moved
1046 to a new bb (6). If these edges where part of another region (2->5)
1047 we update the region->exit pointer, of this region.
1049 To identify which edge belongs to which region we depend on the e->aux
1050 pointer in every edge. It points to the region of the edge or to NULL,
1051 if the edge is not part of any region.
1053 1 2 3 4 1->5 no region, 2->5 region->exit = 5,
1054 \| |/ 3->5 region->exit = NULL, 4->5 region->exit = NULL
1055 5 <- exit
1057 changes to
1059 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
1060 | | \/ 3->5 no region, 4->5 no region,
1061 | | 5
1062 \| / 5->6 region->exit = 6
1065 Now there is only a single exit edge (5->6). */
1066 exit = region->exit;
1067 region->exit = NULL;
1068 forwarder = make_forwarder_block (exit, &sd_region_without_exit, NULL);
1070 /* Unmark the edges, that are no longer exit edges. */
1071 FOR_EACH_EDGE (e, ei, forwarder->src->preds)
1072 if (e->aux)
1073 e->aux = NULL;
1075 /* Mark the new exit edge. */
1076 single_succ_edge (forwarder->src)->aux = region;
1078 /* Update the exit bb of all regions, where exit edges lead to
1079 forwarder->dest. */
1080 FOR_EACH_EDGE (e, ei, forwarder->dest->preds)
1081 if (e->aux)
1082 ((sd_region *) e->aux)->exit = forwarder->dest;
1084 gcc_checking_assert (find_single_exit_edge (region));
1087 /* Unmark the exit edges of all REGIONS.
1088 See comment in "create_single_exit_edge". */
1090 static void
1091 unmark_exit_edges (vec<sd_region> regions)
1093 int i;
1094 sd_region *s;
1095 edge e;
1096 edge_iterator ei;
1098 FOR_EACH_VEC_ELT (regions, i, s)
1099 FOR_EACH_EDGE (e, ei, s->exit->preds)
1100 e->aux = NULL;
1104 /* Mark the exit edges of all REGIONS.
1105 See comment in "create_single_exit_edge". */
1107 static void
1108 mark_exit_edges (vec<sd_region> regions)
1110 int i;
1111 sd_region *s;
1112 edge e;
1113 edge_iterator ei;
1115 FOR_EACH_VEC_ELT (regions, i, s)
1116 FOR_EACH_EDGE (e, ei, s->exit->preds)
1117 if (bb_in_sd_region (e->src, s))
1118 e->aux = s;
1121 /* Create for all scop regions a single entry and a single exit edge. */
1123 static void
1124 create_sese_edges (vec<sd_region> regions)
1126 int i;
1127 sd_region *s;
1129 FOR_EACH_VEC_ELT (regions, i, s)
1130 create_single_entry_edge (s);
1132 mark_exit_edges (regions);
1134 FOR_EACH_VEC_ELT (regions, i, s)
1135 /* Don't handle multiple edges exiting the function. */
1136 if (!find_single_exit_edge (s)
1137 && s->exit != EXIT_BLOCK_PTR_FOR_FN (cfun))
1138 create_single_exit_edge (s);
1140 unmark_exit_edges (regions);
1142 calculate_dominance_info (CDI_DOMINATORS);
1143 fix_loop_structure (NULL);
1145 #ifdef ENABLE_CHECKING
1146 verify_loop_structure ();
1147 verify_ssa (false, true);
1148 #endif
1151 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1153 static void
1154 build_graphite_scops (vec<sd_region> regions,
1155 vec<scop_p> *scops)
1157 int i;
1158 sd_region *s;
1160 FOR_EACH_VEC_ELT (regions, i, s)
1162 edge entry = find_single_entry_edge (s);
1163 edge exit = find_single_exit_edge (s);
1164 scop_p scop;
1166 if (!exit)
1167 continue;
1169 sese sese_reg = new_sese (entry, exit);
1170 scop = new_scop (sese_reg);
1172 build_sese_loop_nests (sese_reg);
1174 /* Scops with one or no loops are not interesting. */
1175 if (SESE_LOOP_NEST (sese_reg).length () > 1)
1176 scops->safe_push (scop);
1177 else if (dump_file && (dump_flags & TDF_DETAILS))
1178 fprintf (dump_file, "Discarded scop: %d loops\n",
1179 SESE_LOOP_NEST (sese_reg).length ());
1181 /* Are there overlapping SCoPs? */
1182 #ifdef ENABLE_CHECKING
1184 int j;
1185 sd_region *s2;
1187 FOR_EACH_VEC_ELT (regions, j, s2)
1188 if (s != s2)
1189 gcc_assert (!bb_in_sd_region (s->entry, s2));
1191 #endif
1195 /* Returns true when P1 and P2 are close phis with the same
1196 argument. */
1198 static inline bool
1199 same_close_phi_node (gphi *p1, gphi *p2)
1201 return operand_equal_p (gimple_phi_arg_def (p1, 0),
1202 gimple_phi_arg_def (p2, 0), 0);
1205 /* Remove the close phi node at GSI and replace its rhs with the rhs
1206 of PHI. */
1208 static void
1209 remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
1211 gimple *use_stmt;
1212 use_operand_p use_p;
1213 imm_use_iterator imm_iter;
1214 tree res = gimple_phi_result (phi);
1215 tree def = gimple_phi_result (gsi->phi ());
1217 gcc_assert (same_close_phi_node (phi, gsi->phi ()));
1219 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
1221 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1222 SET_USE (use_p, res);
1224 update_stmt (use_stmt);
1226 /* It is possible that we just created a duplicate close-phi
1227 for an already-processed containing loop. Check for this
1228 case and clean it up. */
1229 if (gimple_code (use_stmt) == GIMPLE_PHI
1230 && gimple_phi_num_args (use_stmt) == 1)
1231 make_close_phi_nodes_unique (gimple_bb (use_stmt));
1234 remove_phi_node (gsi, true);
1237 /* Removes all the close phi duplicates from BB. */
1239 static void
1240 make_close_phi_nodes_unique (basic_block bb)
1242 gphi_iterator psi;
1244 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1246 gphi_iterator gsi = psi;
1247 gphi *phi = psi.phi ();
1249 /* At this point, PHI should be a close phi in normal form. */
1250 gcc_assert (gimple_phi_num_args (phi) == 1);
1252 /* Iterate over the next phis and remove duplicates. */
1253 gsi_next (&gsi);
1254 while (!gsi_end_p (gsi))
1255 if (same_close_phi_node (phi, gsi.phi ()))
1256 remove_duplicate_close_phi (phi, &gsi);
1257 else
1258 gsi_next (&gsi);
1262 /* Transforms LOOP to the canonical loop closed SSA form. */
1264 static void
1265 canonicalize_loop_closed_ssa (loop_p loop)
1267 edge e = single_exit (loop);
1268 basic_block bb;
1270 if (!e || e->flags & EDGE_ABNORMAL)
1271 return;
1273 bb = e->dest;
1275 if (single_pred_p (bb))
1277 e = split_block_after_labels (bb);
1278 make_close_phi_nodes_unique (e->src);
1280 else
1282 gphi_iterator psi;
1283 basic_block close = split_edge (e);
1285 e = single_succ_edge (close);
1287 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1289 gphi *phi = psi.phi ();
1290 unsigned i;
1292 for (i = 0; i < gimple_phi_num_args (phi); i++)
1293 if (gimple_phi_arg_edge (phi, i) == e)
1295 tree res, arg = gimple_phi_arg_def (phi, i);
1296 use_operand_p use_p;
1297 gphi *close_phi;
1299 if (TREE_CODE (arg) != SSA_NAME)
1300 continue;
1302 close_phi = create_phi_node (NULL_TREE, close);
1303 res = create_new_def_for (arg, close_phi,
1304 gimple_phi_result_ptr (close_phi));
1305 add_phi_arg (close_phi, arg,
1306 gimple_phi_arg_edge (close_phi, 0),
1307 UNKNOWN_LOCATION);
1308 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
1309 replace_exp (use_p, res);
1310 update_stmt (phi);
1314 make_close_phi_nodes_unique (close);
1317 /* The code above does not properly handle changes in the post dominance
1318 information (yet). */
1319 free_dominance_info (CDI_POST_DOMINATORS);
1322 /* Converts the current loop closed SSA form to a canonical form
1323 expected by the Graphite code generation.
1325 The loop closed SSA form has the following invariant: a variable
1326 defined in a loop that is used outside the loop appears only in the
1327 phi nodes in the destination of the loop exit. These phi nodes are
1328 called close phi nodes.
1330 The canonical loop closed SSA form contains the extra invariants:
1332 - when the loop contains only one exit, the close phi nodes contain
1333 only one argument. That implies that the basic block that contains
1334 the close phi nodes has only one predecessor, that is a basic block
1335 in the loop.
1337 - the basic block containing the close phi nodes does not contain
1338 other statements.
1340 - there exist only one phi node per definition in the loop.
1343 static void
1344 canonicalize_loop_closed_ssa_form (void)
1346 loop_p loop;
1348 #ifdef ENABLE_CHECKING
1349 verify_loop_closed_ssa (true);
1350 #endif
1352 FOR_EACH_LOOP (loop, 0)
1353 canonicalize_loop_closed_ssa (loop);
1355 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1356 update_ssa (TODO_update_ssa);
1358 #ifdef ENABLE_CHECKING
1359 verify_loop_closed_ssa (true);
1360 #endif
1363 /* Find Static Control Parts (SCoP) in the current function and pushes
1364 them to SCOPS. */
1366 void
1367 build_scops (vec<scop_p> *scops)
1369 struct loop *loop = current_loops->tree_root;
1370 auto_vec<sd_region, 3> regions;
1372 canonicalize_loop_closed_ssa_form ();
1373 build_scops_1 (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1374 ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father,
1375 &regions, loop);
1376 create_sese_edges (regions);
1377 build_graphite_scops (regions, scops);
1379 regions.release ();
1381 if (dump_file && (dump_flags & TDF_DETAILS))
1382 fprintf (dump_file, "\nnumber of SCoPs: %d\n",
1383 scops ? scops->length () : 0);
1386 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1387 different colors. If there are not enough colors, paint the
1388 remaining SCoPs in gray.
1390 Special nodes:
1391 - "*" after the node number denotes the entry of a SCoP,
1392 - "#" after the node number denotes the exit of a SCoP,
1393 - "()" around the node number denotes the entry or the
1394 exit nodes of the SCOP. These are not part of SCoP. */
1396 static void
1397 dot_all_scops_1 (FILE *file, vec<scop_p> scops)
1399 basic_block bb;
1400 edge e;
1401 edge_iterator ei;
1402 scop_p scop;
1403 const char* color;
1404 int i;
1406 /* Disable debugging while printing graph. */
1407 int tmp_dump_flags = dump_flags;
1408 dump_flags = 0;
1410 fprintf (file, "digraph all {\n");
1412 FOR_ALL_BB_FN (bb, cfun)
1414 int part_of_scop = false;
1416 /* Use HTML for every bb label. So we are able to print bbs
1417 which are part of two different SCoPs, with two different
1418 background colors. */
1419 fprintf (file, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1420 bb->index);
1421 fprintf (file, "CELLSPACING=\"0\">\n");
1423 /* Select color for SCoP. */
1424 FOR_EACH_VEC_ELT (scops, i, scop)
1426 sese region = SCOP_REGION (scop);
1427 if (bb_in_sese_p (bb, region)
1428 || (SESE_EXIT_BB (region) == bb)
1429 || (SESE_ENTRY_BB (region) == bb))
1431 switch (i % 17)
1433 case 0: /* red */
1434 color = "#e41a1c";
1435 break;
1436 case 1: /* blue */
1437 color = "#377eb8";
1438 break;
1439 case 2: /* green */
1440 color = "#4daf4a";
1441 break;
1442 case 3: /* purple */
1443 color = "#984ea3";
1444 break;
1445 case 4: /* orange */
1446 color = "#ff7f00";
1447 break;
1448 case 5: /* yellow */
1449 color = "#ffff33";
1450 break;
1451 case 6: /* brown */
1452 color = "#a65628";
1453 break;
1454 case 7: /* rose */
1455 color = "#f781bf";
1456 break;
1457 case 8:
1458 color = "#8dd3c7";
1459 break;
1460 case 9:
1461 color = "#ffffb3";
1462 break;
1463 case 10:
1464 color = "#bebada";
1465 break;
1466 case 11:
1467 color = "#fb8072";
1468 break;
1469 case 12:
1470 color = "#80b1d3";
1471 break;
1472 case 13:
1473 color = "#fdb462";
1474 break;
1475 case 14:
1476 color = "#b3de69";
1477 break;
1478 case 15:
1479 color = "#fccde5";
1480 break;
1481 case 16:
1482 color = "#bc80bd";
1483 break;
1484 default: /* gray */
1485 color = "#999999";
1488 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
1490 if (!bb_in_sese_p (bb, region))
1491 fprintf (file, " (");
1493 if (bb == SESE_ENTRY_BB (region)
1494 && bb == SESE_EXIT_BB (region))
1495 fprintf (file, " %d*# ", bb->index);
1496 else if (bb == SESE_ENTRY_BB (region))
1497 fprintf (file, " %d* ", bb->index);
1498 else if (bb == SESE_EXIT_BB (region))
1499 fprintf (file, " %d# ", bb->index);
1500 else
1501 fprintf (file, " %d ", bb->index);
1503 if (!bb_in_sese_p (bb,region))
1504 fprintf (file, ")");
1506 fprintf (file, "</TD></TR>\n");
1507 part_of_scop = true;
1511 if (!part_of_scop)
1513 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1514 fprintf (file, " %d </TD></TR>\n", bb->index);
1516 fprintf (file, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1519 FOR_ALL_BB_FN (bb, cfun)
1521 FOR_EACH_EDGE (e, ei, bb->succs)
1522 fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
1525 fputs ("}\n\n", file);
1527 /* Enable debugging again. */
1528 dump_flags = tmp_dump_flags;
1531 /* Display all SCoPs using dotty. */
1533 DEBUG_FUNCTION void
1534 dot_all_scops (vec<scop_p> scops)
1536 /* When debugging, enable the following code. This cannot be used
1537 in production compilers because it calls "system". */
1538 #if 0
1539 int x;
1540 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1541 gcc_assert (stream);
1543 dot_all_scops_1 (stream, scops);
1544 fclose (stream);
1546 x = system ("dotty /tmp/allscops.dot &");
1547 #else
1548 dot_all_scops_1 (stderr, scops);
1549 #endif
1552 /* Display all SCoPs using dotty. */
1554 DEBUG_FUNCTION void
1555 dot_scop (scop_p scop)
1557 auto_vec<scop_p, 1> scops;
1559 if (scop)
1560 scops.safe_push (scop);
1562 /* When debugging, enable the following code. This cannot be used
1563 in production compilers because it calls "system". */
1564 #if 0
1566 int x;
1567 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1568 gcc_assert (stream);
1570 dot_all_scops_1 (stream, scops);
1571 fclose (stream);
1572 x = system ("dotty /tmp/allscops.dot &");
1574 #else
1575 dot_all_scops_1 (stderr, scops);
1576 #endif
1579 #endif /* HAVE_isl */