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)
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/>. */
25 /* Workaround for GMP 5.1.3 bug, see PR56019. */
28 #include <isl/constraint.h>
31 #include <isl/union_map.h>
34 #include "coretypes.h"
41 #include "fold-const.h"
42 #include "gimple-iterator.h"
43 #include "tree-ssa-loop-manip.h"
44 #include "tree-ssa-loop-niter.h"
45 #include "tree-ssa-loop.h"
46 #include "tree-into-ssa.h"
49 #include "tree-data-ref.h"
50 #include "tree-scalar-evolution.h"
51 #include "tree-pass.h"
52 #include "graphite-poly.h"
53 #include "tree-ssa-propagate.h"
54 #include "graphite-scop-detection.h"
55 #include "gimple-pretty-print.h"
57 /* Lightweight representation of sese for scop detection.
58 TODO: Make all this as a constant_edge. */
61 sese_l (edge e
, edge x
)
65 /* This is to push objects of sese_l in a vec. */
67 : entry (NULL
), exit (NULL
)
72 operator bool () const
78 operator= (const sese_l
&s
)
89 /* APIs for getting entry/exit of an sese. */
107 void set_dump_file (FILE *f
)
113 friend debug_printer
&operator<<(debug_printer
&output
, int i
)
115 fprintf (output
.dump_file
, "%d", i
);
118 friend debug_printer
&operator<<(debug_printer
&output
, const char *s
)
120 fprintf (output
.dump_file
, "%s", s
);
125 #define DEBUG_PRINT(args) do \
127 if (dump_file && (dump_flags & TDF_DETAILS)) { args; } \
131 /* Return true if BB is empty, contains only DEBUG_INSNs. */
134 trivially_empty_bb_p (basic_block bb
)
136 gimple_stmt_iterator gsi
;
138 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
139 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_DEBUG
)
146 /* Forward declarations. */
147 static void make_close_phi_nodes_unique (basic_block
);
149 /* Something like "n * m" is not allowed. */
152 graphite_can_represent_init (tree e
)
154 switch (TREE_CODE (e
))
156 case POLYNOMIAL_CHREC
:
157 return graphite_can_represent_init (CHREC_LEFT (e
))
158 && graphite_can_represent_init (CHREC_RIGHT (e
));
161 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
162 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
163 && tree_fits_shwi_p (TREE_OPERAND (e
, 1));
165 return graphite_can_represent_init (TREE_OPERAND (e
, 1))
166 && tree_fits_shwi_p (TREE_OPERAND (e
, 0));
169 case POINTER_PLUS_EXPR
:
171 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
172 && graphite_can_represent_init (TREE_OPERAND (e
, 1));
177 case NON_LVALUE_EXPR
:
178 return graphite_can_represent_init (TREE_OPERAND (e
, 0));
187 /* Return true when SCEV can be represented in the polyhedral model.
189 An expression can be represented, if it can be expressed as an
190 affine expression. For loops (i, j) and parameters (m, n) all
191 affine expressions are of the form:
193 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
195 1 i + 20 j + (-2) m + 25
197 Something like "i * n" or "n * m" is not allowed. */
200 graphite_can_represent_scev (tree scev
)
202 if (chrec_contains_undetermined (scev
))
205 /* We disable the handling of pointer types, because it’s currently not
206 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
207 the only nodes, which are disabled in case they are pointers to object
208 types, but this can be changed. */
210 if (POINTER_TYPE_P (TREE_TYPE (scev
)) && TREE_CODE (scev
) == SSA_NAME
)
213 switch (TREE_CODE (scev
))
218 case NON_LVALUE_EXPR
:
219 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0));
222 case POINTER_PLUS_EXPR
:
224 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
225 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
228 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 0)))
229 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 1)))
230 && !(chrec_contains_symbols (TREE_OPERAND (scev
, 0))
231 && chrec_contains_symbols (TREE_OPERAND (scev
, 1)))
232 && graphite_can_represent_init (scev
)
233 && graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
234 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
236 case POLYNOMIAL_CHREC
:
237 /* Check for constant strides. With a non constant stride of
238 'n' we would have a value of 'iv * n'. Also check that the
239 initial value can represented: for example 'n * m' cannot be
241 if (!evolution_function_right_is_integer_cst (scev
)
242 || !graphite_can_represent_init (scev
))
244 return graphite_can_represent_scev (CHREC_LEFT (scev
));
250 /* Only affine functions can be represented. */
251 if (tree_contains_chrecs (scev
, NULL
)
252 || !scev_is_linear_expression (scev
))
259 /* Return true when EXPR can be represented in the polyhedral model.
261 This means an expression can be represented, if it is linear with respect to
262 the loops and the strides are non parametric. LOOP is the place where the
263 expr will be evaluated. SCOP defines the region we analyse. */
266 graphite_can_represent_expr (sese_l scop
, loop_p loop
, tree expr
)
268 sese region
= new_sese (scop
.entry
, scop
.exit
);
269 tree scev
= scalar_evolution_in_region (region
, loop
, expr
);
271 return graphite_can_represent_scev (scev
);
274 /* Return true if the data references of STMT can be represented by Graphite.
275 We try to analyze the data references in a loop contained in the SCOP. */
278 stmt_has_simple_data_refs_p (sese_l scop
, gimple
*stmt
)
280 sese region
= new_sese (scop
.entry
, scop
.exit
);
281 loop_p nest
= outermost_loop_in_sese (region
, gimple_bb (stmt
));
282 loop_p loop
= loop_containing_stmt (stmt
);
283 vec
<data_reference_p
> drs
= vNULL
;
285 graphite_find_data_references_in_stmt (nest
, loop
, stmt
, &drs
);
289 FOR_EACH_VEC_ELT (drs
, j
, dr
)
291 int nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
293 if (nb_subscripts
< 1)
295 free_data_refs (drs
);
299 tree ref
= DR_REF (dr
);
301 for (int i
= nb_subscripts
- 1; i
>= 0; i
--)
303 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr
, i
))
304 || (TREE_CODE (ref
) != ARRAY_REF
305 && TREE_CODE (ref
) != MEM_REF
306 && TREE_CODE (ref
) != COMPONENT_REF
))
308 free_data_refs (drs
);
312 ref
= TREE_OPERAND (ref
, 0);
316 free_data_refs (drs
);
320 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
321 Calls have side-effects, except those to const or pure
325 stmt_has_side_effects (gimple
*stmt
)
327 if (gimple_has_volatile_ops (stmt
)
328 || (gimple_code (stmt
) == GIMPLE_CALL
329 && !(gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
)))
330 || (gimple_code (stmt
) == GIMPLE_ASM
))
332 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
333 << "Statement has side-effects:\n";
334 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
));
340 /* Returns true if STMT can be represented in polyhedral model. LABEL,
341 simple COND stmts, pure calls, and assignments can be repesented. */
344 graphite_can_represent_stmt (sese_l scop
, gimple
*stmt
, basic_block bb
)
346 loop_p loop
= bb
->loop_father
;
347 switch (gimple_code (stmt
))
354 /* We can handle all binary comparisons. Inequalities are
355 also supported as they can be represented with union of
357 enum tree_code code
= gimple_cond_code (stmt
);
358 if (!(code
== LT_EXPR
365 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
366 << "Graphite cannot handle cond stmt:\n";
367 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
));
371 for (unsigned i
= 0; i
< 2; ++i
)
373 tree op
= gimple_op (stmt
, i
);
374 if (!graphite_can_represent_expr (scop
, loop
, op
)
375 /* We can only constrain on integer type. */
376 || (TREE_CODE (TREE_TYPE (op
)) != INTEGER_TYPE
))
378 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
379 << "Graphite cannot represent stmt:\n";
380 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
));
393 /* These nodes cut a new scope. */
394 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
395 << "Gimple stmt not handled in Graphite:\n";
396 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
));
401 /* Return true only when STMT is simple enough for being handled by Graphite.
402 This depends on SCOP, as the parameters are initialized relatively to
403 this basic block, the linear functions are initialized based on the outermost
404 loop containing STMT inside the SCOP. BB is the place where we try to
405 evaluate the STMT. */
408 stmt_simple_for_scop_p (sese_l scop
, gimple
*stmt
, basic_block bb
)
412 if (is_gimple_debug (stmt
))
415 if (stmt_has_side_effects (stmt
))
418 if (!stmt_has_simple_data_refs_p (scop
, stmt
))
420 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
421 << "Graphite cannot handle data-refs in stmt:\n";
422 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
););
426 return graphite_can_represent_stmt (scop
, stmt
, bb
);
429 /* Return true when BB contains a harmful operation for a scop: that
430 can be a function call with side effects, the induction variables
431 are not linear with respect to SCOP, etc. The current open
432 scop should end before this statement. */
435 harmful_stmt_in_bb (sese_l scop
, basic_block bb
)
437 gimple_stmt_iterator gsi
;
439 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
440 if (!stmt_simple_for_scop_p (scop
, gsi_stmt (gsi
), bb
))
446 /* Returns true when P1 and P2 are close phis with the same
450 same_close_phi_node (gphi
*p1
, gphi
*p2
)
452 return operand_equal_p (gimple_phi_arg_def (p1
, 0),
453 gimple_phi_arg_def (p2
, 0), 0);
456 /* Remove the close phi node at GSI and replace its rhs with the rhs
460 remove_duplicate_close_phi (gphi
*phi
, gphi_iterator
*gsi
)
464 imm_use_iterator imm_iter
;
465 tree res
= gimple_phi_result (phi
);
466 tree def
= gimple_phi_result (gsi
->phi ());
468 gcc_assert (same_close_phi_node (phi
, gsi
->phi ()));
470 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
472 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
473 SET_USE (use_p
, res
);
475 update_stmt (use_stmt
);
477 /* It is possible that we just created a duplicate close-phi
478 for an already-processed containing loop. Check for this
479 case and clean it up. */
480 if (gimple_code (use_stmt
) == GIMPLE_PHI
481 && gimple_phi_num_args (use_stmt
) == 1)
482 make_close_phi_nodes_unique (gimple_bb (use_stmt
));
485 remove_phi_node (gsi
, true);
488 /* Removes all the close phi duplicates from BB. */
491 make_close_phi_nodes_unique (basic_block bb
)
495 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
497 gphi_iterator gsi
= psi
;
498 gphi
*phi
= psi
.phi ();
500 /* At this point, PHI should be a close phi in normal form. */
501 gcc_assert (gimple_phi_num_args (phi
) == 1);
503 /* Iterate over the next phis and remove duplicates. */
505 while (!gsi_end_p (gsi
))
506 if (same_close_phi_node (phi
, gsi
.phi ()))
507 remove_duplicate_close_phi (phi
, &gsi
);
513 /* Transforms LOOP to the canonical loop closed SSA form. */
516 canonicalize_loop_closed_ssa (loop_p loop
)
518 edge e
= single_exit (loop
);
521 if (!e
|| e
->flags
& EDGE_ABNORMAL
)
526 if (single_pred_p (bb
))
528 e
= split_block_after_labels (bb
);
529 DEBUG_PRINT (dp
<< "\nSplitting bb_" << bb
->index
);
530 make_close_phi_nodes_unique (e
->src
);
535 basic_block close
= split_edge (e
);
537 e
= single_succ_edge (close
);
538 DEBUG_PRINT (dp
<< "\nSplitting edge ("
539 << e
->src
->index
<< "," << e
->dest
->index
542 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
544 gphi
*phi
= psi
.phi ();
547 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
548 if (gimple_phi_arg_edge (phi
, i
) == e
)
550 tree res
, arg
= gimple_phi_arg_def (phi
, i
);
554 if (TREE_CODE (arg
) != SSA_NAME
)
557 close_phi
= create_phi_node (NULL_TREE
, close
);
558 res
= create_new_def_for (arg
, close_phi
,
559 gimple_phi_result_ptr (close_phi
));
560 add_phi_arg (close_phi
, arg
,
561 gimple_phi_arg_edge (close_phi
, 0),
563 use_p
= gimple_phi_arg_imm_use_ptr (phi
, i
);
564 replace_exp (use_p
, res
);
569 make_close_phi_nodes_unique (close
);
572 /* The code above does not properly handle changes in the post dominance
573 information (yet). */
574 recompute_all_dominators ();
577 /* Converts the current loop closed SSA form to a canonical form
578 expected by the Graphite code generation.
580 The loop closed SSA form has the following invariant: a variable
581 defined in a loop that is used outside the loop appears only in the
582 phi nodes in the destination of the loop exit. These phi nodes are
583 called close phi nodes.
585 The canonical loop closed SSA form contains the extra invariants:
587 - when the loop contains only one exit, the close phi nodes contain
588 only one argument. That implies that the basic block that contains
589 the close phi nodes has only one predecessor, that is a basic block
592 - the basic block containing the close phi nodes does not contain
595 - there exist only one phi node per definition in the loop.
599 canonicalize_loop_closed_ssa_form (void)
603 #ifdef ENABLE_CHECKING
604 verify_loop_closed_ssa (true);
607 FOR_EACH_LOOP (loop
, 0)
608 canonicalize_loop_closed_ssa (loop
);
610 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
611 update_ssa (TODO_update_ssa
);
613 #ifdef ENABLE_CHECKING
614 verify_loop_closed_ssa (true);
618 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
619 different colors. If there are not enough colors, paint the
620 remaining SCoPs in gray.
623 - "*" after the node number denotes the entry of a SCoP,
624 - "#" after the node number denotes the exit of a SCoP,
625 - "()" around the node number denotes the entry or the
626 exit nodes of the SCOP. These are not part of SCoP. */
629 dot_all_scops_1 (FILE *file
, vec
<scop_p
> scops
)
638 /* Disable debugging while printing graph. */
639 int tmp_dump_flags
= dump_flags
;
642 fprintf (file
, "digraph all {\n");
644 FOR_ALL_BB_FN (bb
, cfun
)
646 int part_of_scop
= false;
648 /* Use HTML for every bb label. So we are able to print bbs
649 which are part of two different SCoPs, with two different
650 background colors. */
651 fprintf (file
, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
653 fprintf (file
, "CELLSPACING=\"0\">\n");
655 /* Select color for SCoP. */
656 FOR_EACH_VEC_ELT (scops
, i
, scop
)
658 sese region
= SCOP_REGION (scop
);
659 if (bb_in_sese_p (bb
, region
)
660 || (SESE_EXIT_BB (region
) == bb
)
661 || (SESE_ENTRY_BB (region
) == bb
))
720 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color
);
722 if (!bb_in_sese_p (bb
, region
))
723 fprintf (file
, " (");
725 if (bb
== SESE_ENTRY_BB (region
)
726 && bb
== SESE_EXIT_BB (region
))
727 fprintf (file
, " %d*# ", bb
->index
);
728 else if (bb
== SESE_ENTRY_BB (region
))
729 fprintf (file
, " %d* ", bb
->index
);
730 else if (bb
== SESE_EXIT_BB (region
))
731 fprintf (file
, " %d# ", bb
->index
);
733 fprintf (file
, " %d ", bb
->index
);
735 fprintf (file
, "{lp_%d}", bb
->loop_father
->num
);
737 if (!bb_in_sese_p (bb
,region
))
740 fprintf (file
, "</TD></TR>\n");
747 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
748 fprintf (file
, " %d {lp_%d} </TD></TR>\n",
749 bb
->index
, bb
->loop_father
->num
);
751 fprintf (file
, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
754 FOR_ALL_BB_FN (bb
, cfun
)
756 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
757 fprintf (file
, "%d -> %d;\n", bb
->index
, e
->dest
->index
);
760 fputs ("}\n\n", file
);
762 /* Enable debugging again. */
763 dump_flags
= tmp_dump_flags
;
766 /* Display all SCoPs using dotty. */
769 dot_all_scops (vec
<scop_p
> scops
)
771 /* When debugging, enable the following code. This cannot be used
772 in production compilers because it calls "system". */
775 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
778 dot_all_scops_1 (stream
, scops
);
781 x
= system ("dotty /tmp/allscops.dot &");
783 dot_all_scops_1 (stderr
, scops
);
787 /* Display all SCoPs using dotty. */
790 dot_scop (scop_p scop
)
792 auto_vec
<scop_p
, 1> scops
;
795 scops
.safe_push (scop
);
797 /* When debugging, enable the following code. This cannot be used
798 in production compilers because it calls "system". */
802 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
805 dot_all_scops_1 (stream
, scops
);
807 x
= system ("dotty /tmp/allscops.dot &");
810 dot_all_scops_1 (stderr
, scops
);
814 /* Return true when the body of LOOP has statements that can be represented as a
818 loop_body_is_valid_scop (loop_p loop
, sese_l scop
)
820 if (!loop_nest_has_data_refs (loop
))
822 DEBUG_PRINT (dp
<< "[scop-detection-fail] loop_"
823 << loop
->num
<< "does not have any data reference.\n");
827 basic_block
*bbs
= get_loop_body (loop
);
828 for (unsigned i
= 0; i
< loop
->num_nodes
; i
++)
830 basic_block bb
= bbs
[i
];
832 if (harmful_stmt_in_bb (scop
, bb
))
842 if (!loop_body_is_valid_scop (loop
, scop
))
851 /* Build the maximal scop containing LOOP(s) and add it to SCOPS. */
860 static sese_l invalid_sese
;
869 get_sese (loop_p loop
)
874 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS
))
876 edge scop_end
= single_exit (loop
);
879 edge scop_begin
= loop_preheader_edge (loop
);
880 sese_l
s (scop_begin
, scop_end
);
885 get_nearest_dom_with_single_entry (basic_block dom
)
889 /* If e1->src dominates e2->src then e1->src will also dominate dom. */
890 if (dom
->preds
->length () == 2)
892 edge e1
= (*dom
->preds
)[0];
893 edge e2
= (*dom
->preds
)[1];
894 if (dominated_by_p (CDI_DOMINATORS
, e2
->src
, e1
->src
))
896 if (dominated_by_p (CDI_DOMINATORS
, e1
->src
, e2
->src
))
900 while (dom
->preds
->length () != 1)
902 if (dom
->preds
->length () < 1)
904 dom
= get_immediate_dominator (CDI_DOMINATORS
, dom
);
908 return (*dom
->preds
)[0];
912 get_nearest_pdom_with_single_exit (basic_block dom
)
916 if (dom
->succs
->length () == 2)
918 edge e1
= (*dom
->succs
)[0];
919 edge e2
= (*dom
->succs
)[1];
920 if (dominated_by_p (CDI_POST_DOMINATORS
, e2
->dest
, e1
->dest
))
922 if (dominated_by_p (CDI_POST_DOMINATORS
, e1
->dest
, e2
->dest
))
926 while (dom
->succs
->length () != 1)
928 if (dom
->succs
->length () < 1)
930 dom
= get_immediate_dominator (CDI_POST_DOMINATORS
, dom
);
934 return (*dom
->succs
)[0];
937 /* Print S to FILE. */
940 print_sese (FILE *file
, sese_l s
)
942 fprintf (file
, "(entry_edge (bb_%d, bb_%d), exit_edge (bb_%d, bb_%d))\n",
943 s
.entry
->src
->index
, s
.entry
->dest
->index
,
944 s
.exit
->src
->index
, s
.exit
->dest
->index
);
947 /* Merge scops at same loop depth and returns the new sese.
948 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
951 merge_sese (sese_l first
, sese_l second
)
953 /* In the trivial case first/second may be NULL. */
959 DEBUG_PRINT (dp
<< "[try-merging-sese] s1: ";
960 print_sese (dump_file
, first
);
961 dp
<< "[try-merging-sese] s2: ";
962 print_sese (dump_file
, second
));
964 /* Assumption: Both the sese's should be at the same loop depth or one scop
965 should subsume the other like in case of nested loops. */
967 /* Find the common dominators for entry,
968 and common post-dominators for the exit. */
969 basic_block dom
= nearest_common_dominator (CDI_DOMINATORS
,
970 get_entry_bb (first
.entry
),
971 get_entry_bb (second
.entry
));
974 edge entry
= get_nearest_dom_with_single_entry (dom
);
978 basic_block pdom
= nearest_common_dominator (CDI_POST_DOMINATORS
,
979 get_exit_bb (first
.exit
),
980 get_exit_bb (second
.exit
));
981 pdom
= nearest_common_dominator (CDI_POST_DOMINATORS
, dom
, pdom
);
983 edge exit
= get_nearest_pdom_with_single_exit (pdom
);
987 sese_l
combined (entry
, exit
);
989 /* FIXME: We could iterate to find the dom which dominates pdom, and pdom
990 which post-dominates dom, until it stabilizes. Also, ENTRY->SRC and
991 EXIT->DEST should be in the same loop nest. */
992 if (!dominated_by_p (CDI_DOMINATORS
, pdom
, dom
)
993 || loop_depth (entry
->src
->loop_father
)
994 != loop_depth (exit
->dest
->loop_father
))
997 /* For now we just want to bail out when exit does not post-dominate entry.
998 TODO: We might just add a basic_block at the exit to make exit
999 post-dominate entry (the entire region). */
1000 if (!dominated_by_p (CDI_POST_DOMINATORS
, get_entry_bb (entry
),
1002 || !dominated_by_p (CDI_DOMINATORS
, get_exit_bb (exit
),
1003 get_entry_bb (entry
)))
1005 DEBUG_PRINT (dp
<< "[scop-detection-fail] cannot merge seses.\n");
1006 return invalid_sese
;
1009 /* FIXME: We should remove this piece of code once
1010 canonicalize_loop_closed_ssa has been removed, because that function
1011 adds a BB with single exit. */
1012 if (!trivially_empty_bb_p (get_exit_bb (combined
.exit
)))
1014 /* Find the first empty succ (with single exit) of combined.exit. */
1015 basic_block imm_succ
= combined
.exit
->dest
;
1016 if (single_succ_p (imm_succ
) && trivially_empty_bb_p (imm_succ
))
1017 combined
.exit
= single_succ_edge (imm_succ
);
1020 DEBUG_PRINT (dp
<< "\n[scop-detection-fail] Discarding SCoP because "
1021 << "no single exit (empty succ) for sese exit";
1022 print_sese (dump_file
, combined
));
1023 return invalid_sese
;
1027 /* Analyze all the BBs in new sese. */
1028 if (harmful_stmt_in_region (combined
))
1029 return invalid_sese
;
1031 DEBUG_PRINT (dp
<< "[merged-sese] s1: ";
1032 print_sese (dump_file
, combined
));
1037 /* Build scop outer->inner if possible. */
1039 build_scop_depth (sese_l s
, loop_p loop
)
1044 DEBUG_PRINT (dp
<< "\n[Depth loop_" << loop
->num
<< "]");
1045 s
= build_scop_depth (s
, loop
->inner
);
1047 sese_l s2
= merge_sese (s
, get_sese (loop
));
1050 /* s might be a valid scop, so return it and start analyzing from the
1052 build_scop_depth (invalid_sese
, loop
->next
);
1056 if (!loop_is_valid_scop (loop
, s2
))
1057 return build_scop_depth (invalid_sese
, loop
->next
);
1059 return build_scop_breadth (s2
, loop
);
1062 /* If loop and loop->next are valid scops, try to merge them. */
1065 build_scop_breadth (sese_l s1
, loop_p loop
)
1069 DEBUG_PRINT (dp
<< "\n[Breadth loop_" << loop
->num
<< "]");
1073 sese_l s2
= build_scop_depth (invalid_sese
, l
->next
);
1081 sese_l combined
= merge_sese (s1
, s2
);
1093 /* Returns true when Graphite can represent LOOP in SCOP.
1094 FIXME: For the moment, graphite cannot be used on loops that iterate using
1095 induction variables that wrap. */
1097 can_represent_loop_1 (loop_p loop
, sese_l scop
)
1100 struct tree_niter_desc niter_desc
;
1102 return single_exit (loop
)
1103 && number_of_iterations_exit (loop
, single_exit (loop
), &niter_desc
, false)
1104 && niter_desc
.control
.no_overflow
1105 && (niter
= number_of_latch_executions (loop
))
1106 && !chrec_contains_undetermined (niter
)
1107 && graphite_can_represent_expr (scop
, loop
, niter
);
1110 /* Return true when all the loops within LOOP can be represented by
1114 can_represent_loop (loop_p loop
, sese_l scop
)
1116 if (!can_represent_loop_1 (loop
, scop
))
1118 if (loop
->inner
&& !can_represent_loop (loop
->inner
, scop
))
1120 if (loop
->next
&& !can_represent_loop (loop
->next
, scop
))
1126 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
1127 region of code that can be represented in the polyhedral model. SCOP
1128 defines the region we analyse. */
1131 loop_is_valid_scop (loop_p loop
, sese_l scop
)
1136 if (!can_represent_loop (loop
, scop
))
1138 DEBUG_PRINT (dp
<< "[scop-detection-fail] cannot represent loop_"
1139 << loop
->num
<< "\n");
1143 if (loop_body_is_valid_scop (loop
, scop
))
1145 DEBUG_PRINT (dp
<< "[valid-scop] loop_"
1146 << loop
->num
<< "is a valid scop.\n");
1152 /* Return true when BEGIN is the preheader edge of a loop with a single exit
1156 region_has_one_loop (sese_l s
)
1158 edge begin
= s
.entry
;
1160 /* Check for a single perfectly nested loop. */
1161 if (begin
->dest
->loop_father
->inner
)
1164 /* Otherwise, check whether we have adjacent loops. */
1165 return begin
->dest
->loop_father
== end
->src
->loop_father
;
1168 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
1175 /* Do not add scops with only one loop. */
1176 if (region_has_one_loop (s
))
1178 DEBUG_PRINT (dp
<< "\n[scop-detection-fail] Discarding one loop SCoP";
1179 print_sese (dump_file
, s
));
1183 if (get_exit_bb (s
.exit
) == EXIT_BLOCK_PTR_FOR_FN (cfun
))
1185 DEBUG_PRINT (dp
<< "\n[scop-detection-fail] "
1186 << "Discarding SCoP exiting to return";
1187 print_sese (dump_file
, s
));
1191 /* Remove all the scops which are subsumed by s. */
1192 remove_subscops (s
);
1194 /* Replace this with split-intersecting scops. */
1195 remove_intersecting_scops (s
);
1197 scops
.safe_push (s
);
1198 DEBUG_PRINT (dp
<< "\nAdding SCoP "; print_sese (dump_file
, s
));
1201 /* Return true when a statement in SCOP cannot be represented by Graphite.
1202 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
1203 Limit the number of bbs between adjacent loops to
1204 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
1207 harmful_stmt_in_region (sese_l scop
)
1209 basic_block exit_bb
= get_exit_bb (scop
.exit
);
1210 basic_block entry_bb
= get_entry_bb (scop
.entry
);
1212 DEBUG_PRINT (dp
<< "\n[checking-harmful-bbs] ";
1213 print_sese (dump_file
, scop
));
1214 gcc_assert (dominated_by_p (CDI_DOMINATORS
, exit_bb
, entry_bb
));
1216 int depth
= bb_dom_dfs_in (CDI_DOMINATORS
, exit_bb
)
1217 - bb_dom_dfs_in (CDI_DOMINATORS
, entry_bb
);
1219 gcc_assert (depth
>0);
1221 vec
<basic_block
> dom
= get_dominated_to_depth (CDI_DOMINATORS
,
1225 FOR_EACH_VEC_ELT (dom
, i
, bb
)
1227 DEBUG_PRINT (dp
<< "\nVisiting bb_" << bb
->index
);
1229 /* We don't want to analyze any bb outside sese. */
1230 if (!dominated_by_p (CDI_POST_DOMINATORS
, bb
, exit_bb
))
1233 if (harmful_stmt_in_bb (scop
, bb
))
1240 /* Returns true if S1 subsumes/surrounds S2. */
1242 subsumes (sese_l s1
, sese_l s2
)
1244 if (dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
.entry
),
1245 get_entry_bb (s1
.entry
))
1246 && dominated_by_p (CDI_POST_DOMINATORS
, get_entry_bb (s2
.exit
),
1247 get_entry_bb (s1
.exit
)))
1252 /* Remove a SCoP which is subsumed by S1. */
1254 remove_subscops (sese_l s1
)
1258 FOR_EACH_VEC_ELT_REVERSE (scops
, j
, s2
)
1260 if (subsumes (s1
, s2
))
1262 DEBUG_PRINT (dp
<< "\nRemoving sub-SCoP";
1263 print_sese (dump_file
, s2
));
1264 scops
.unordered_remove (j
);
1269 /* Returns true if S1 intersects with S2. Since we already know that S1 does
1270 not subsume S2 or vice-versa, we only check for entry bbs. */
1273 intersects (sese_l s1
, sese_l s2
)
1275 if (dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
.entry
),
1276 get_entry_bb (s1
.entry
))
1277 && !dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
.entry
),
1278 get_exit_bb (s1
.exit
)))
1280 if ((s1
.exit
== s2
.entry
)
1281 || (s2
.exit
== s1
.entry
))
1287 /* Remove one of the scops when it intersects with any other. */
1290 remove_intersecting_scops (sese_l s1
)
1294 FOR_EACH_VEC_ELT_REVERSE (scops
, j
, s2
)
1296 if (intersects (s1
, s2
))
1298 DEBUG_PRINT (dp
<< "\nRemoving intersecting SCoP";
1299 print_sese (dump_file
, s2
);
1300 dp
<< "Intersects with:";
1301 print_sese (dump_file
, s1
));
1302 scops
.unordered_remove (j
);
1311 sese_l
scop_builder::invalid_sese (0);
1313 /* Find Static Control Parts (SCoP) in the current function and pushes
1317 build_scops (vec
<scop_p
> *scops
)
1320 dp
.set_dump_file (dump_file
);
1322 canonicalize_loop_closed_ssa_form ();
1325 sb
.build_scop_depth (scop_builder::invalid_sese
, current_loops
->tree_root
);
1327 /* Now create scops from the lightweight SESEs. */
1328 vec
<sese_l
> scops_l
= sb
.get_scops ();
1331 FOR_EACH_VEC_ELT (scops_l
, i
, s
)
1333 sese sese_reg
= new_sese (s
.entry
, s
.exit
);
1334 scops
->safe_push (new_scop (sese_reg
));
1337 DEBUG_PRINT (dp
<< "number of SCoPs: " << (scops
? scops
->length () : 0););
1340 #endif /* HAVE_isl */