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"
42 #include "fold-const.h"
43 #include "gimple-iterator.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
51 #include "tree-data-ref.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-pass.h"
54 #include "graphite-poly.h"
55 #include "tree-ssa-propagate.h"
56 #include "graphite-scop-detection.h"
57 #include "gimple-pretty-print.h"
66 set_dump_file (FILE *f
)
72 friend debug_printer
&
73 operator<< (debug_printer
&output
, int i
)
75 fprintf (output
.dump_file
, "%d", i
);
78 friend debug_printer
&
79 operator<< (debug_printer
&output
, const char *s
)
81 fprintf (output
.dump_file
, "%s", s
);
86 #define DEBUG_PRINT(args) do \
88 if (dump_file && (dump_flags & TDF_DETAILS)) { args; } \
91 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
92 different colors. If there are not enough colors, paint the
93 remaining SCoPs in gray.
96 - "*" after the node number denotes the entry of a SCoP,
97 - "#" after the node number denotes the exit of a SCoP,
98 - "()" around the node number denotes the entry or the
99 exit nodes of the SCOP. These are not part of SCoP. */
102 dot_all_scops_1 (FILE *file
, vec
<scop_p
> scops
)
111 /* Disable debugging while printing graph. */
112 int tmp_dump_flags
= dump_flags
;
115 fprintf (file
, "digraph all {\n");
117 FOR_ALL_BB_FN (bb
, cfun
)
119 int part_of_scop
= false;
121 /* Use HTML for every bb label. So we are able to print bbs
122 which are part of two different SCoPs, with two different
123 background colors. */
124 fprintf (file
, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
126 fprintf (file
, "CELLSPACING=\"0\">\n");
128 /* Select color for SCoP. */
129 FOR_EACH_VEC_ELT (scops
, i
, scop
)
131 sese_l region
= scop
->scop_info
->region
;
132 if (bb_in_sese_p (bb
, region
) || (region
.exit
->dest
== bb
)
133 || (region
.entry
->dest
== bb
))
192 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">",
195 if (!bb_in_sese_p (bb
, region
))
196 fprintf (file
, " (");
198 if (bb
== region
.entry
->dest
&& bb
== region
.exit
->dest
)
199 fprintf (file
, " %d*# ", bb
->index
);
200 else if (bb
== region
.entry
->dest
)
201 fprintf (file
, " %d* ", bb
->index
);
202 else if (bb
== region
.exit
->dest
)
203 fprintf (file
, " %d# ", bb
->index
);
205 fprintf (file
, " %d ", bb
->index
);
207 fprintf (file
, "{lp_%d}", bb
->loop_father
->num
);
209 if (!bb_in_sese_p (bb
, region
))
212 fprintf (file
, "</TD></TR>\n");
219 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
220 fprintf (file
, " %d {lp_%d} </TD></TR>\n", bb
->index
,
221 bb
->loop_father
->num
);
223 fprintf (file
, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
226 FOR_ALL_BB_FN (bb
, cfun
)
228 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
229 fprintf (file
, "%d -> %d;\n", bb
->index
, e
->dest
->index
);
232 fputs ("}\n\n", file
);
234 /* Enable debugging again. */
235 dump_flags
= tmp_dump_flags
;
238 /* Display all SCoPs using dotty. */
241 dot_all_scops (vec
<scop_p
> scops
)
243 /* When debugging, enable the following code. This cannot be used
244 in production compilers because it calls "system". */
247 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
250 dot_all_scops_1 (stream
, scops
);
253 x
= system ("dotty /tmp/allscops.dot &");
255 dot_all_scops_1 (stderr
, scops
);
259 /* Display all SCoPs using dotty. */
262 dot_scop (scop_p scop
)
264 auto_vec
<scop_p
, 1> scops
;
267 scops
.safe_push (scop
);
269 /* When debugging, enable the following code. This cannot be used
270 in production compilers because it calls "system". */
274 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
277 dot_all_scops_1 (stream
, scops
);
279 x
= system ("dotty /tmp/allscops.dot &");
282 dot_all_scops_1 (stderr
, scops
);
286 /* Return true if BB is empty, contains only DEBUG_INSNs. */
289 trivially_empty_bb_p (basic_block bb
)
291 gimple_stmt_iterator gsi
;
293 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
294 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_DEBUG
)
300 /* Returns true when P1 and P2 are close phis with the same
304 same_close_phi_node (gphi
*p1
, gphi
*p2
)
306 return operand_equal_p (gimple_phi_arg_def (p1
, 0),
307 gimple_phi_arg_def (p2
, 0), 0);
310 static void make_close_phi_nodes_unique (basic_block bb
);
312 /* Remove the close phi node at GSI and replace its rhs with the rhs
316 remove_duplicate_close_phi (gphi
*phi
, gphi_iterator
*gsi
)
320 imm_use_iterator imm_iter
;
321 tree res
= gimple_phi_result (phi
);
322 tree def
= gimple_phi_result (gsi
->phi ());
324 gcc_assert (same_close_phi_node (phi
, gsi
->phi ()));
326 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
328 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
329 SET_USE (use_p
, res
);
331 update_stmt (use_stmt
);
333 /* It is possible that we just created a duplicate close-phi
334 for an already-processed containing loop. Check for this
335 case and clean it up. */
336 if (gimple_code (use_stmt
) == GIMPLE_PHI
337 && gimple_phi_num_args (use_stmt
) == 1)
338 make_close_phi_nodes_unique (gimple_bb (use_stmt
));
341 remove_phi_node (gsi
, true);
344 /* Removes all the close phi duplicates from BB. */
347 make_close_phi_nodes_unique (basic_block bb
)
351 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
353 gphi_iterator gsi
= psi
;
354 gphi
*phi
= psi
.phi ();
356 /* At this point, PHI should be a close phi in normal form. */
357 gcc_assert (gimple_phi_num_args (phi
) == 1);
359 /* Iterate over the next phis and remove duplicates. */
361 while (!gsi_end_p (gsi
))
362 if (same_close_phi_node (phi
, gsi
.phi ()))
363 remove_duplicate_close_phi (phi
, &gsi
);
369 /* Transforms LOOP to the canonical loop closed SSA form. */
372 canonicalize_loop_closed_ssa (loop_p loop
)
374 edge e
= single_exit (loop
);
377 if (!e
|| e
->flags
& EDGE_ABNORMAL
)
382 if (single_pred_p (bb
))
384 e
= split_block_after_labels (bb
);
385 DEBUG_PRINT (dp
<< "\nSplitting bb_" << bb
->index
);
386 make_close_phi_nodes_unique (e
->src
);
391 basic_block close
= split_edge (e
);
393 e
= single_succ_edge (close
);
394 DEBUG_PRINT (dp
<< "\nSplitting edge (" << e
->src
->index
<< ","
395 << e
->dest
->index
<< ")\n");
397 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
399 gphi
*phi
= psi
.phi ();
402 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
403 if (gimple_phi_arg_edge (phi
, i
) == e
)
405 tree res
, arg
= gimple_phi_arg_def (phi
, i
);
409 if (TREE_CODE (arg
) != SSA_NAME
)
412 close_phi
= create_phi_node (NULL_TREE
, close
);
413 res
= create_new_def_for (arg
, close_phi
,
414 gimple_phi_result_ptr (close_phi
));
415 add_phi_arg (close_phi
, arg
,
416 gimple_phi_arg_edge (close_phi
, 0),
418 use_p
= gimple_phi_arg_imm_use_ptr (phi
, i
);
419 replace_exp (use_p
, res
);
424 make_close_phi_nodes_unique (close
);
427 /* The code above does not properly handle changes in the post dominance
428 information (yet). */
429 recompute_all_dominators ();
432 /* Converts the current loop closed SSA form to a canonical form
433 expected by the Graphite code generation.
435 The loop closed SSA form has the following invariant: a variable
436 defined in a loop that is used outside the loop appears only in the
437 phi nodes in the destination of the loop exit. These phi nodes are
438 called close phi nodes.
440 The canonical loop closed SSA form contains the extra invariants:
442 - when the loop contains only one exit, the close phi nodes contain
443 only one argument. That implies that the basic block that contains
444 the close phi nodes has only one predecessor, that is a basic block
447 - the basic block containing the close phi nodes does not contain
450 - there exist only one phi node per definition in the loop.
454 canonicalize_loop_closed_ssa_form (void)
456 checking_verify_loop_closed_ssa (true);
459 FOR_EACH_LOOP (loop
, 0)
460 canonicalize_loop_closed_ssa (loop
);
462 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
463 update_ssa (TODO_update_ssa
);
465 checking_verify_loop_closed_ssa (true);
468 /* Can all ivs be represented by a signed integer?
469 As ISL might generate negative values in its expressions, signed loop ivs
470 are required in the backend. */
473 loop_ivs_can_be_represented (loop_p loop
)
475 unsigned type_long_long
= TYPE_PRECISION (long_long_integer_type_node
);
476 for (gphi_iterator psi
= gsi_start_phis (loop
->header
); !gsi_end_p (psi
);
479 gphi
*phi
= psi
.phi ();
480 tree res
= PHI_RESULT (phi
);
481 tree type
= TREE_TYPE (res
);
483 if (TYPE_UNSIGNED (type
) && TYPE_PRECISION (type
) >= type_long_long
)
490 /* Returns a COND_EXPR statement when BB has a single predecessor, the
491 edge between BB and its predecessor is not a loop exit edge, and
492 the last statement of the single predecessor is a COND_EXPR. */
495 single_pred_cond_non_loop_exit (basic_block bb
)
497 if (single_pred_p (bb
))
499 edge e
= single_pred_edge (bb
);
500 basic_block pred
= e
->src
;
503 if (loop_depth (pred
->loop_father
) > loop_depth (bb
->loop_father
))
506 stmt
= last_stmt (pred
);
508 if (stmt
&& gimple_code (stmt
) == GIMPLE_COND
)
509 return as_a
<gcond
*> (stmt
);
518 /* Build the maximal scop containing LOOPs and add it to SCOPS. */
523 scop_detection () : scops (vNULL
) {}
530 /* A marker for invalid sese_l. */
531 static sese_l invalid_sese
;
533 /* Return the SCOPS in this SCOP_DETECTION. */
541 /* Return an sese_l around the LOOP. */
543 sese_l
get_sese (loop_p loop
);
545 /* Return the closest dominator with a single entry edge. In case of a
546 back-loop the back-edge is not counted. */
548 static edge
get_nearest_dom_with_single_entry (basic_block dom
);
550 /* Return the closest post-dominator with a single exit edge. In case of a
551 back-loop the back-edge is not counted. */
553 static edge
get_nearest_pdom_with_single_exit (basic_block dom
);
555 /* Print S to FILE. */
557 static void print_sese (FILE *file
, sese_l s
);
559 /* Merge scops at same loop depth and returns the new sese.
560 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
562 sese_l
merge_sese (sese_l first
, sese_l second
) const;
564 /* Build scop outer->inner if possible. */
566 sese_l
build_scop_depth (sese_l s
, loop_p loop
);
568 /* If loop and loop->next are valid scops, try to merge them. */
570 sese_l
build_scop_breadth (sese_l s1
, loop_p loop
);
572 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
573 region of code that can be represented in the polyhedral model. SCOP
574 defines the region we analyse. */
576 bool loop_is_valid_scop (loop_p loop
, sese_l scop
) const;
578 /* Return true when BEGIN is the preheader edge of a loop with a single exit
581 static bool region_has_one_loop (sese_l s
);
583 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
585 void add_scop (sese_l s
);
587 /* Returns true if S1 subsumes/surrounds S2. */
588 static bool subsumes (sese_l s1
, sese_l s2
);
590 /* Remove a SCoP which is subsumed by S1. */
591 void remove_subscops (sese_l s1
);
593 /* Returns true if S1 intersects with S2. Since we already know that S1 does
594 not subsume S2 or vice-versa, we only check for entry bbs. */
596 static bool intersects (sese_l s1
, sese_l s2
);
598 /* Remove one of the scops when it intersects with any other. */
600 void remove_intersecting_scops (sese_l s1
);
602 /* Return true when the body of LOOP has statements that can be represented
605 bool loop_body_is_valid_scop (loop_p loop
, sese_l scop
) const;
607 /* Return true when BB contains a harmful operation for a scop: that
608 can be a function call with side effects, the induction variables
609 are not linear with respect to SCOP, etc. The current open
610 scop should end before this statement. */
612 bool harmful_stmt_in_bb (sese_l scop
, basic_block bb
) const;
614 /* Return true when a statement in SCOP cannot be represented by Graphite.
615 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
616 Limit the number of bbs between adjacent loops to
617 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
619 bool harmful_stmt_in_region (sese_l scop
) const;
621 /* Return true only when STMT is simple enough for being handled by Graphite.
622 This depends on SCOP, as the parameters are initialized relatively to
623 this basic block, the linear functions are initialized based on the
624 outermost loop containing STMT inside the SCOP. BB is the place where we
625 try to evaluate the STMT. */
627 bool stmt_simple_for_scop_p (sese_l scop
, gimple
*stmt
,
628 basic_block bb
) const;
630 /* Something like "n * m" is not allowed. */
632 static bool graphite_can_represent_init (tree e
);
634 /* Return true when SCEV can be represented in the polyhedral model.
636 An expression can be represented, if it can be expressed as an
637 affine expression. For loops (i, j) and parameters (m, n) all
638 affine expressions are of the form:
640 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
642 1 i + 20 j + (-2) m + 25
644 Something like "i * n" or "n * m" is not allowed. */
646 static bool graphite_can_represent_scev (tree scev
);
648 /* Return true when EXPR can be represented in the polyhedral model.
650 This means an expression can be represented, if it is linear with respect
651 to the loops and the strides are non parametric. LOOP is the place where
652 the expr will be evaluated. SCOP defines the region we analyse. */
654 static bool graphite_can_represent_expr (sese_l scop
, loop_p loop
,
657 /* Return true if the data references of STMT can be represented by Graphite.
658 We try to analyze the data references in a loop contained in the SCOP. */
660 static bool stmt_has_simple_data_refs_p (sese_l scop
, gimple
*stmt
);
662 /* Remove the close phi node at GSI and replace its rhs with the rhs
665 static void remove_duplicate_close_phi (gphi
*phi
, gphi_iterator
*gsi
);
667 /* Returns true when Graphite can represent LOOP in SCOP.
668 FIXME: For the moment, graphite cannot be used on loops that iterate using
669 induction variables that wrap. */
671 static bool can_represent_loop_1 (loop_p loop
, sese_l scop
);
673 /* Return true when all the loops within LOOP can be represented by
676 static bool can_represent_loop (loop_p loop
, sese_l scop
);
678 /* Returns the number of pbbs that are in loops contained in SCOP. */
680 static int nb_pbbs_in_loops (scop_p scop
);
682 static bool graphite_can_represent_stmt (sese_l
, gimple
*, basic_block
);
688 sese_l
scop_detection::invalid_sese (NULL
, NULL
);
690 /* Return an sese_l around the LOOP. */
693 scop_detection::get_sese (loop_p loop
)
698 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS
))
700 edge scop_end
= single_exit (loop
);
703 edge scop_begin
= loop_preheader_edge (loop
);
704 sese_l
s (scop_begin
, scop_end
);
708 /* Return the closest dominator with a single entry edge. */
711 scop_detection::get_nearest_dom_with_single_entry (basic_block dom
)
715 /* If e1->src dominates e2->src then e1->src will also dominate dom. */
716 if (dom
->preds
->length () == 2)
718 edge e1
= (*dom
->preds
)[0];
719 edge e2
= (*dom
->preds
)[1];
720 if (dominated_by_p (CDI_DOMINATORS
, e2
->src
, e1
->src
))
722 if (dominated_by_p (CDI_DOMINATORS
, e1
->src
, e2
->src
))
726 while (dom
->preds
->length () != 1)
728 if (dom
->preds
->length () < 1)
730 dom
= get_immediate_dominator (CDI_DOMINATORS
, dom
);
734 return (*dom
->preds
)[0];
737 /* Return the closest post-dominator with a single exit edge. In case of a
738 back-loop the back-edge is not counted. */
741 scop_detection::get_nearest_pdom_with_single_exit (basic_block dom
)
745 if (dom
->succs
->length () == 2)
747 edge e1
= (*dom
->succs
)[0];
748 edge e2
= (*dom
->succs
)[1];
749 if (dominated_by_p (CDI_POST_DOMINATORS
, e2
->dest
, e1
->dest
))
751 if (dominated_by_p (CDI_POST_DOMINATORS
, e1
->dest
, e2
->dest
))
755 while (dom
->succs
->length () != 1)
757 if (dom
->succs
->length () < 1)
759 dom
= get_immediate_dominator (CDI_POST_DOMINATORS
, dom
);
763 return (*dom
->succs
)[0];
766 /* Print S to FILE. */
769 scop_detection::print_sese (FILE *file
, sese_l s
)
771 fprintf (file
, "(entry_edge (bb_%d, bb_%d), exit_edge (bb_%d, bb_%d))\n",
772 s
.entry
->src
->index
, s
.entry
->dest
->index
,
773 s
.exit
->src
->index
, s
.exit
->dest
->index
);
776 /* Merge scops at same loop depth and returns the new sese.
777 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
780 scop_detection::merge_sese (sese_l first
, sese_l second
) const
782 /* In the trivial case first/second may be NULL. */
788 DEBUG_PRINT (dp
<< "[try-merging-sese] s1: "; print_sese (dump_file
, first
);
789 dp
<< "[try-merging-sese] s2: ";
790 print_sese (dump_file
, second
));
792 /* Assumption: Both the sese's should be at the same loop depth or one scop
793 should subsume the other like in case of nested loops. */
795 /* Find the common dominators for entry,
796 and common post-dominators for the exit. */
797 basic_block dom
= nearest_common_dominator (CDI_DOMINATORS
,
798 get_entry_bb (first
),
799 get_entry_bb (second
));
801 edge entry
= get_nearest_dom_with_single_entry (dom
);
803 if (!entry
|| (entry
->flags
& EDGE_IRREDUCIBLE_LOOP
))
806 basic_block pdom
= nearest_common_dominator (CDI_POST_DOMINATORS
,
808 get_exit_bb (second
));
809 pdom
= nearest_common_dominator (CDI_POST_DOMINATORS
, dom
, pdom
);
811 edge exit
= get_nearest_pdom_with_single_exit (pdom
);
813 if (!exit
|| (exit
->flags
& EDGE_IRREDUCIBLE_LOOP
))
816 sese_l
combined (entry
, exit
);
818 /* FIXME: We could iterate to find the dom which dominates pdom, and pdom
819 which post-dominates dom, until it stabilizes. Also, ENTRY->SRC and
820 EXIT->DEST should be in the same loop nest. */
821 if (!dominated_by_p (CDI_DOMINATORS
, pdom
, dom
)
822 || loop_depth (entry
->src
->loop_father
)
823 != loop_depth (exit
->dest
->loop_father
))
826 /* For now we just want to bail out when exit does not post-dominate entry.
827 TODO: We might just add a basic_block at the exit to make exit
828 post-dominate entry (the entire region). */
829 if (!dominated_by_p (CDI_POST_DOMINATORS
, get_entry_bb (combined
),
830 get_exit_bb (combined
))
831 || !dominated_by_p (CDI_DOMINATORS
, get_exit_bb (combined
),
832 get_entry_bb (combined
)))
834 DEBUG_PRINT (dp
<< "[scop-detection-fail] cannot merge seses.\n");
838 /* FIXME: We should remove this piece of code once
839 canonicalize_loop_closed_ssa has been removed, because that function
840 adds a BB with single exit. */
841 if (!trivially_empty_bb_p (get_exit_bb (combined
)))
843 /* Find the first empty succ (with single exit) of combined.exit. */
844 basic_block imm_succ
= combined
.exit
->dest
;
845 if (single_succ_p (imm_succ
) && trivially_empty_bb_p (imm_succ
))
846 combined
.exit
= single_succ_edge (imm_succ
);
849 DEBUG_PRINT (dp
<< "\n[scop-detection-fail] Discarding SCoP because "
850 << "no single exit (empty succ) for sese exit";
851 print_sese (dump_file
, combined
));
856 /* Analyze all the BBs in new sese. */
857 if (harmful_stmt_in_region (combined
))
860 DEBUG_PRINT (dp
<< "[merged-sese] s1: "; print_sese (dump_file
, combined
));
865 /* Build scop outer->inner if possible. */
868 scop_detection::build_scop_depth (sese_l s
, loop_p loop
)
873 DEBUG_PRINT (dp
<< "\n[Depth loop_" << loop
->num
<< "]");
874 s
= build_scop_depth (s
, loop
->inner
);
876 sese_l s2
= merge_sese (s
, get_sese (loop
));
879 /* s might be a valid scop, so return it and start analyzing from the
881 build_scop_depth (invalid_sese
, loop
->next
);
885 if (!loop_is_valid_scop (loop
, s2
))
886 return build_scop_depth (invalid_sese
, loop
->next
);
888 return build_scop_breadth (s2
, loop
);
891 /* If loop and loop->next are valid scops, try to merge them. */
894 scop_detection::build_scop_breadth (sese_l s1
, loop_p loop
)
898 DEBUG_PRINT (dp
<< "\n[Breadth loop_" << loop
->num
<< "]");
902 sese_l s2
= build_scop_depth (invalid_sese
, l
->next
);
910 sese_l combined
= merge_sese (s1
, s2
);
922 /* Returns true when Graphite can represent LOOP in SCOP.
923 FIXME: For the moment, graphite cannot be used on loops that iterate using
924 induction variables that wrap. */
927 scop_detection::can_represent_loop_1 (loop_p loop
, sese_l scop
)
930 struct tree_niter_desc niter_desc
;
932 return single_exit (loop
)
933 && !(loop_preheader_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
)
934 && number_of_iterations_exit (loop
, single_exit (loop
), &niter_desc
, false)
935 && niter_desc
.control
.no_overflow
936 && (niter
= number_of_latch_executions (loop
))
937 && !chrec_contains_undetermined (niter
)
938 && graphite_can_represent_expr (scop
, loop
, niter
);
941 /* Return true when all the loops within LOOP can be represented by
945 scop_detection::can_represent_loop (loop_p loop
, sese_l scop
)
947 if (!can_represent_loop_1 (loop
, scop
))
949 if (loop
->inner
&& !can_represent_loop (loop
->inner
, scop
))
951 if (loop
->next
&& !can_represent_loop (loop
->next
, scop
))
957 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
958 region of code that can be represented in the polyhedral model. SCOP
959 defines the region we analyse. */
962 scop_detection::loop_is_valid_scop (loop_p loop
, sese_l scop
) const
967 if (!optimize_loop_nest_for_speed_p (loop
))
969 DEBUG_PRINT (dp
<< "[scop-detection-fail] loop_"
970 << loop
->num
<< " is not on a hot path.\n");
974 if (!can_represent_loop (loop
, scop
))
976 DEBUG_PRINT (dp
<< "[scop-detection-fail] cannot represent loop_"
977 << loop
->num
<< "\n");
981 if (loop_body_is_valid_scop (loop
, scop
))
983 DEBUG_PRINT (dp
<< "[valid-scop] loop_" << loop
->num
984 << "is a valid scop.\n");
990 /* Return true when BEGIN is the preheader edge of a loop with a single exit
994 scop_detection::region_has_one_loop (sese_l s
)
996 edge begin
= s
.entry
;
998 /* Check for a single perfectly nested loop. */
999 if (begin
->dest
->loop_father
->inner
)
1002 /* Otherwise, check whether we have adjacent loops. */
1003 return begin
->dest
->loop_father
== end
->src
->loop_father
;
1006 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
1009 scop_detection::add_scop (sese_l s
)
1013 /* Do not add scops with only one loop. */
1014 if (region_has_one_loop (s
))
1016 DEBUG_PRINT (dp
<< "\n[scop-detection-fail] Discarding one loop SCoP";
1017 print_sese (dump_file
, s
));
1021 if (get_exit_bb (s
) == EXIT_BLOCK_PTR_FOR_FN (cfun
))
1023 DEBUG_PRINT (dp
<< "\n[scop-detection-fail] "
1024 << "Discarding SCoP exiting to return";
1025 print_sese (dump_file
, s
));
1029 /* Remove all the scops which are subsumed by s. */
1030 remove_subscops (s
);
1032 /* Replace this with split-intersecting scops. */
1033 remove_intersecting_scops (s
);
1035 scops
.safe_push (s
);
1036 DEBUG_PRINT (dp
<< "\nAdding SCoP "; print_sese (dump_file
, s
));
1039 /* Return true when a statement in SCOP cannot be represented by Graphite.
1040 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
1041 Limit the number of bbs between adjacent loops to
1042 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
1045 scop_detection::harmful_stmt_in_region (sese_l scop
) const
1047 basic_block exit_bb
= get_exit_bb (scop
);
1048 basic_block entry_bb
= get_entry_bb (scop
);
1050 DEBUG_PRINT (dp
<< "\n[checking-harmful-bbs] ";
1051 print_sese (dump_file
, scop
));
1052 gcc_assert (dominated_by_p (CDI_DOMINATORS
, exit_bb
, entry_bb
));
1054 int depth
= bb_dom_dfs_in (CDI_DOMINATORS
, exit_bb
)
1055 - bb_dom_dfs_in (CDI_DOMINATORS
, entry_bb
);
1057 gcc_assert (depth
> 0);
1059 vec
<basic_block
> dom
1060 = get_dominated_to_depth (CDI_DOMINATORS
, entry_bb
, depth
);
1063 FOR_EACH_VEC_ELT (dom
, i
, bb
)
1065 DEBUG_PRINT (dp
<< "\nVisiting bb_" << bb
->index
);
1067 /* We don't want to analyze any bb outside sese. */
1068 if (!dominated_by_p (CDI_POST_DOMINATORS
, bb
, exit_bb
))
1071 /* The basic block should not be part of an irreducible loop. */
1072 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1078 if (harmful_stmt_in_bb (scop
, bb
))
1089 /* Returns true if S1 subsumes/surrounds S2. */
1091 scop_detection::subsumes (sese_l s1
, sese_l s2
)
1093 if (dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
),
1095 && dominated_by_p (CDI_POST_DOMINATORS
, s2
.exit
->dest
,
1101 /* Remove a SCoP which is subsumed by S1. */
1103 scop_detection::remove_subscops (sese_l s1
)
1107 FOR_EACH_VEC_ELT_REVERSE (scops
, j
, s2
)
1109 if (subsumes (s1
, *s2
))
1111 DEBUG_PRINT (dp
<< "\nRemoving sub-SCoP";
1112 print_sese (dump_file
, *s2
));
1113 scops
.unordered_remove (j
);
1118 /* Returns true if S1 intersects with S2. Since we already know that S1 does
1119 not subsume S2 or vice-versa, we only check for entry bbs. */
1122 scop_detection::intersects (sese_l s1
, sese_l s2
)
1124 if (dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
),
1126 && !dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
),
1129 if ((s1
.exit
== s2
.entry
) || (s2
.exit
== s1
.entry
))
1135 /* Remove one of the scops when it intersects with any other. */
1138 scop_detection::remove_intersecting_scops (sese_l s1
)
1142 FOR_EACH_VEC_ELT_REVERSE (scops
, j
, s2
)
1144 if (intersects (s1
, *s2
))
1146 DEBUG_PRINT (dp
<< "\nRemoving intersecting SCoP";
1147 print_sese (dump_file
, *s2
); dp
<< "Intersects with:";
1148 print_sese (dump_file
, s1
));
1149 scops
.unordered_remove (j
);
1154 /* Something like "n * m" is not allowed. */
1157 scop_detection::graphite_can_represent_init (tree e
)
1159 switch (TREE_CODE (e
))
1161 case POLYNOMIAL_CHREC
:
1162 return graphite_can_represent_init (CHREC_LEFT (e
))
1163 && graphite_can_represent_init (CHREC_RIGHT (e
));
1166 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
1167 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
1168 && tree_fits_shwi_p (TREE_OPERAND (e
, 1));
1170 return graphite_can_represent_init (TREE_OPERAND (e
, 1))
1171 && tree_fits_shwi_p (TREE_OPERAND (e
, 0));
1174 case POINTER_PLUS_EXPR
:
1176 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
1177 && graphite_can_represent_init (TREE_OPERAND (e
, 1));
1182 case NON_LVALUE_EXPR
:
1183 return graphite_can_represent_init (TREE_OPERAND (e
, 0));
1192 /* Return true when SCEV can be represented in the polyhedral model.
1194 An expression can be represented, if it can be expressed as an
1195 affine expression. For loops (i, j) and parameters (m, n) all
1196 affine expressions are of the form:
1198 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
1200 1 i + 20 j + (-2) m + 25
1202 Something like "i * n" or "n * m" is not allowed. */
1205 scop_detection::graphite_can_represent_scev (tree scev
)
1207 if (chrec_contains_undetermined (scev
))
1210 /* We disable the handling of pointer types, because it’s currently not
1211 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
1212 the only nodes, which are disabled in case they are pointers to object
1213 types, but this can be changed. */
1215 if (POINTER_TYPE_P (TREE_TYPE (scev
)) && TREE_CODE (scev
) == SSA_NAME
)
1218 switch (TREE_CODE (scev
))
1223 case NON_LVALUE_EXPR
:
1224 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0));
1227 case POINTER_PLUS_EXPR
:
1229 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
1230 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
1233 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 0)))
1234 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 1)))
1235 && !(chrec_contains_symbols (TREE_OPERAND (scev
, 0))
1236 && chrec_contains_symbols (TREE_OPERAND (scev
, 1)))
1237 && graphite_can_represent_init (scev
)
1238 && graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
1239 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
1241 case POLYNOMIAL_CHREC
:
1242 /* Check for constant strides. With a non constant stride of
1243 'n' we would have a value of 'iv * n'. Also check that the
1244 initial value can represented: for example 'n * m' cannot be
1246 if (!evolution_function_right_is_integer_cst (scev
)
1247 || !graphite_can_represent_init (scev
))
1249 return graphite_can_represent_scev (CHREC_LEFT (scev
));
1255 /* Only affine functions can be represented. */
1256 if (tree_contains_chrecs (scev
, NULL
) || !scev_is_linear_expression (scev
))
1262 /* Return true when EXPR can be represented in the polyhedral model.
1264 This means an expression can be represented, if it is linear with respect to
1265 the loops and the strides are non parametric. LOOP is the place where the
1266 expr will be evaluated. SCOP defines the region we analyse. */
1269 scop_detection::graphite_can_represent_expr (sese_l scop
, loop_p loop
,
1272 tree scev
= scalar_evolution_in_region (scop
, loop
, expr
);
1273 return graphite_can_represent_scev (scev
);
1276 /* Return true if the data references of STMT can be represented by Graphite.
1277 We try to analyze the data references in a loop contained in the SCOP. */
1280 scop_detection::stmt_has_simple_data_refs_p (sese_l scop
, gimple
*stmt
)
1282 loop_p nest
= outermost_loop_in_sese (scop
, gimple_bb (stmt
));
1283 loop_p loop
= loop_containing_stmt (stmt
);
1284 vec
<data_reference_p
> drs
= vNULL
;
1286 graphite_find_data_references_in_stmt (nest
, loop
, stmt
, &drs
);
1289 data_reference_p dr
;
1290 FOR_EACH_VEC_ELT (drs
, j
, dr
)
1292 int nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1294 if (nb_subscripts
< 1)
1296 free_data_refs (drs
);
1300 tree ref
= DR_REF (dr
);
1302 for (int i
= nb_subscripts
- 1; i
>= 0; i
--)
1304 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr
, i
))
1305 || (TREE_CODE (ref
) != ARRAY_REF
&& TREE_CODE (ref
) != MEM_REF
1306 && TREE_CODE (ref
) != COMPONENT_REF
))
1308 free_data_refs (drs
);
1312 ref
= TREE_OPERAND (ref
, 0);
1316 free_data_refs (drs
);
1320 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
1321 Calls have side-effects, except those to const or pure
1325 stmt_has_side_effects (gimple
*stmt
)
1327 if (gimple_has_volatile_ops (stmt
)
1328 || (gimple_code (stmt
) == GIMPLE_CALL
1329 && !(gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
)))
1330 || (gimple_code (stmt
) == GIMPLE_ASM
))
1332 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1333 << "Statement has side-effects:\n";
1334 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
| TDF_MEMSYMS
));
1340 /* Returns true if STMT can be represented in polyhedral model. LABEL,
1341 simple COND stmts, pure calls, and assignments can be repesented. */
1344 scop_detection::graphite_can_represent_stmt (sese_l scop
, gimple
*stmt
,
1347 loop_p loop
= bb
->loop_father
;
1348 switch (gimple_code (stmt
))
1355 /* We can handle all binary comparisons. Inequalities are
1356 also supported as they can be represented with union of
1358 enum tree_code code
= gimple_cond_code (stmt
);
1359 if (!(code
== LT_EXPR
1364 || code
== NE_EXPR
))
1366 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1367 << "Graphite cannot handle cond stmt:\n";
1368 print_gimple_stmt (dump_file
, stmt
, 0,
1369 TDF_VOPS
| TDF_MEMSYMS
));
1373 for (unsigned i
= 0; i
< 2; ++i
)
1375 tree op
= gimple_op (stmt
, i
);
1376 if (!graphite_can_represent_expr (scop
, loop
, op
)
1377 /* We can only constrain on integer type. */
1378 || (TREE_CODE (TREE_TYPE (op
)) != INTEGER_TYPE
))
1380 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1381 << "Graphite cannot represent stmt:\n";
1382 print_gimple_stmt (dump_file
, stmt
, 0,
1383 TDF_VOPS
| TDF_MEMSYMS
));
1396 /* These nodes cut a new scope. */
1398 dp
<< "[scop-detection-fail] "
1399 << "Gimple stmt not handled in Graphite:\n";
1400 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
| TDF_MEMSYMS
));
1405 /* Return true only when STMT is simple enough for being handled by Graphite.
1406 This depends on SCOP, as the parameters are initialized relatively to
1407 this basic block, the linear functions are initialized based on the outermost
1408 loop containing STMT inside the SCOP. BB is the place where we try to
1409 evaluate the STMT. */
1412 scop_detection::stmt_simple_for_scop_p (sese_l scop
, gimple
*stmt
,
1413 basic_block bb
) const
1417 if (is_gimple_debug (stmt
))
1420 if (stmt_has_side_effects (stmt
))
1423 if (!stmt_has_simple_data_refs_p (scop
, stmt
))
1425 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1426 << "Graphite cannot handle data-refs in stmt:\n";
1427 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
););
1431 return graphite_can_represent_stmt (scop
, stmt
, bb
);
1434 /* Return true when BB contains a harmful operation for a scop: that
1435 can be a function call with side effects, the induction variables
1436 are not linear with respect to SCOP, etc. The current open
1437 scop should end before this statement. */
1440 scop_detection::harmful_stmt_in_bb (sese_l scop
, basic_block bb
) const
1442 gimple_stmt_iterator gsi
;
1444 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1445 if (!stmt_simple_for_scop_p (scop
, gsi_stmt (gsi
), bb
))
1451 /* Return true when the body of LOOP has statements that can be represented as a
1455 scop_detection::loop_body_is_valid_scop (loop_p loop
, sese_l scop
) const
1457 if (!loop_ivs_can_be_represented (loop
))
1459 DEBUG_PRINT (dp
<< "[scop-detection-fail] loop_" << loop
->num
1460 << "IV cannot be represented.\n");
1464 if (!loop_nest_has_data_refs (loop
))
1466 DEBUG_PRINT (dp
<< "[scop-detection-fail] loop_" << loop
->num
1467 << "does not have any data reference.\n");
1471 basic_block
*bbs
= get_loop_body (loop
);
1472 for (unsigned i
= 0; i
< loop
->num_nodes
; i
++)
1474 basic_block bb
= bbs
[i
];
1476 if (harmful_stmt_in_bb (scop
, bb
))
1486 if (!loop_body_is_valid_scop (loop
, scop
))
1495 /* Returns the number of pbbs that are in loops contained in SCOP. */
1498 scop_detection::nb_pbbs_in_loops (scop_p scop
)
1504 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
1505 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb
)), scop
->scop_info
->region
))
1511 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
1512 Otherwise returns -1. */
1515 parameter_index_in_region_1 (tree name
, sese_info_p region
)
1520 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
1522 FOR_EACH_VEC_ELT (region
->params
, i
, p
)
1529 /* When the parameter NAME is in REGION, returns its index in
1530 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
1531 and returns the index of NAME. */
1534 parameter_index_in_region (tree name
, sese_info_p region
)
1538 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
1540 /* Cannot constrain on anything else than INTEGER_TYPE parameters. */
1541 if (TREE_CODE (TREE_TYPE (name
)) != INTEGER_TYPE
)
1544 if (!invariant_in_sese_p_rec (name
, region
->region
, NULL
))
1547 i
= parameter_index_in_region_1 (name
, region
);
1551 i
= region
->params
.length ();
1552 region
->params
.safe_push (name
);
1556 /* In the context of sese S, scan the expression E and translate it to
1557 a linear expression C. When parsing a symbolic multiplication, K
1558 represents the constant multiplier of an expression containing
1562 scan_tree_for_params (sese_info_p s
, tree e
)
1564 if (e
== chrec_dont_know
)
1567 switch (TREE_CODE (e
))
1569 case POLYNOMIAL_CHREC
:
1570 scan_tree_for_params (s
, CHREC_LEFT (e
));
1574 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
1575 scan_tree_for_params (s
, TREE_OPERAND (e
, 0));
1577 scan_tree_for_params (s
, TREE_OPERAND (e
, 1));
1581 case POINTER_PLUS_EXPR
:
1583 scan_tree_for_params (s
, TREE_OPERAND (e
, 0));
1584 scan_tree_for_params (s
, TREE_OPERAND (e
, 1));
1590 case NON_LVALUE_EXPR
:
1591 scan_tree_for_params (s
, TREE_OPERAND (e
, 0));
1595 parameter_index_in_region (e
, s
);
1611 /* Find parameters with respect to REGION in BB. We are looking in memory
1612 access functions, conditions and loop bounds. */
1615 find_params_in_bb (sese_info_p region
, gimple_poly_bb_p gbb
)
1617 /* Find parameters in the access functions of data references. */
1619 data_reference_p dr
;
1620 FOR_EACH_VEC_ELT (GBB_DATA_REFS (gbb
), i
, dr
)
1621 for (unsigned j
= 0; j
< DR_NUM_DIMENSIONS (dr
); j
++)
1622 scan_tree_for_params (region
, DR_ACCESS_FN (dr
, j
));
1624 /* Find parameters in conditional statements. */
1626 loop_p loop
= GBB_BB (gbb
)->loop_father
;
1627 FOR_EACH_VEC_ELT (GBB_CONDITIONS (gbb
), i
, stmt
)
1629 tree lhs
= scalar_evolution_in_region (region
->region
, loop
,
1630 gimple_cond_lhs (stmt
));
1631 tree rhs
= scalar_evolution_in_region (region
->region
, loop
,
1632 gimple_cond_rhs (stmt
));
1634 scan_tree_for_params (region
, lhs
);
1635 scan_tree_for_params (region
, rhs
);
1639 /* Record the parameters used in the SCOP. A variable is a parameter
1640 in a scop if it does not vary during the execution of that scop. */
1643 find_scop_parameters (scop_p scop
)
1646 sese_info_p region
= scop
->scop_info
;
1649 /* Find the parameters used in the loop bounds. */
1650 FOR_EACH_VEC_ELT (region
->loop_nest
, i
, loop
)
1652 tree nb_iters
= number_of_latch_executions (loop
);
1654 if (!chrec_contains_symbols (nb_iters
))
1657 nb_iters
= scalar_evolution_in_region (region
->region
, loop
, nb_iters
);
1658 scan_tree_for_params (region
, nb_iters
);
1661 /* Find the parameters used in data accesses. */
1663 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
1664 find_params_in_bb (region
, PBB_BLACK_BOX (pbb
));
1666 int nbp
= sese_nb_params (region
);
1667 scop_set_nb_params (scop
, nbp
);
1670 /* Record DEF if it is used in other bbs different than DEF_BB in the SCOP. */
1673 build_cross_bb_scalars_def (scop_p scop
, tree def
, basic_block def_bb
,
1677 if (!is_gimple_reg (def
))
1680 /* Do not gather scalar variables that can be analyzed by SCEV as they can be
1681 generated out of the induction variables. */
1682 if (scev_analyzable_p (def
, scop
->scop_info
->region
))
1686 imm_use_iterator imm_iter
;
1687 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
1688 if (def_bb
!= gimple_bb (use_stmt
) && !is_gimple_debug (use_stmt
))
1690 writes
->safe_push (def
);
1691 DEBUG_PRINT (dp
<< "Adding scalar write:\n";
1692 print_generic_expr (dump_file
, def
, 0);
1693 dp
<< "From stmt:\n";
1694 print_gimple_stmt (dump_file
,
1695 SSA_NAME_DEF_STMT (def
), 0, 0));
1696 /* This is required by the FOR_EACH_IMM_USE_STMT when we want to break
1697 before all the uses have been visited. */
1698 BREAK_FROM_IMM_USE_STMT (imm_iter
);
1702 /* Record DEF if it is used in other bbs different than DEF_BB in the SCOP. */
1705 build_cross_bb_scalars_use (scop_p scop
, tree use
, gimple
*use_stmt
,
1706 vec
<scalar_use
> *reads
)
1709 if (!is_gimple_reg (use
))
1712 /* Do not gather scalar variables that can be analyzed by SCEV as they can be
1713 generated out of the induction variables. */
1714 if (scev_analyzable_p (use
, scop
->scop_info
->region
))
1717 gimple
*def_stmt
= SSA_NAME_DEF_STMT (use
);
1718 if (gimple_bb (def_stmt
) != gimple_bb (use_stmt
))
1720 DEBUG_PRINT (dp
<< "\nAdding scalar read:";
1721 print_generic_expr (dump_file
, use
, 0);
1722 dp
<< "\nFrom stmt:";
1723 print_gimple_stmt (dump_file
, use_stmt
, 0, 0));
1724 reads
->safe_push (std::make_pair (use_stmt
, use
));
1728 /* Record all scalar variables that are defined and used in different BBs of the
1732 graphite_find_cross_bb_scalar_vars (scop_p scop
, gimple
*stmt
,
1733 vec
<scalar_use
> *reads
, vec
<tree
> *writes
)
1737 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
1738 def
= gimple_assign_lhs (stmt
);
1739 else if (gimple_code (stmt
) == GIMPLE_CALL
)
1740 def
= gimple_call_lhs (stmt
);
1741 else if (gimple_code (stmt
) == GIMPLE_PHI
)
1742 def
= gimple_phi_result (stmt
);
1747 build_cross_bb_scalars_def (scop
, def
, gimple_bb (stmt
), writes
);
1750 use_operand_p use_p
;
1751 FOR_EACH_PHI_OR_STMT_USE (use_p
, stmt
, iter
, SSA_OP_USE
)
1753 tree use
= USE_FROM_PTR (use_p
);
1754 build_cross_bb_scalars_use (scop
, use
, stmt
, reads
);
1758 /* Generates a polyhedral black box only if the bb contains interesting
1761 static gimple_poly_bb_p
1762 try_generate_gimple_bb (scop_p scop
, basic_block bb
)
1764 vec
<data_reference_p
> drs
= vNULL
;
1765 vec
<tree
> writes
= vNULL
;
1766 vec
<scalar_use
> reads
= vNULL
;
1768 sese_l region
= scop
->scop_info
->region
;
1769 loop_p nest
= outermost_loop_in_sese (region
, bb
);
1771 loop_p loop
= bb
->loop_father
;
1772 if (!loop_in_sese_p (loop
, region
))
1775 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
1778 gimple
*stmt
= gsi_stmt (gsi
);
1779 if (is_gimple_debug (stmt
))
1782 graphite_find_data_references_in_stmt (nest
, loop
, stmt
, &drs
);
1783 graphite_find_cross_bb_scalar_vars (scop
, stmt
, &reads
, &writes
);
1786 for (gphi_iterator psi
= gsi_start_phis (bb
); !gsi_end_p (psi
);
1788 if (!virtual_operand_p (gimple_phi_result (psi
.phi ())))
1789 graphite_find_cross_bb_scalar_vars (scop
, psi
.phi (), &reads
, &writes
);
1791 if (drs
.is_empty () && writes
.is_empty () && reads
.is_empty ())
1794 return new_gimple_poly_bb (bb
, drs
, reads
, writes
);
1797 /* Compute alias-sets for all data references in DRS. */
1800 build_alias_set (scop_p scop
)
1802 int num_vertices
= scop
->drs
.length ();
1803 struct graph
*g
= new_graph (num_vertices
);
1808 FOR_EACH_VEC_ELT (scop
->drs
, i
, dr1
)
1809 for (j
= i
+1; scop
->drs
.iterate (j
, &dr2
); j
++)
1810 if (dr_may_alias_p (dr1
->dr
, dr2
->dr
, true))
1816 all_vertices
= XNEWVEC (int, num_vertices
);
1817 for (i
= 0; i
< num_vertices
; i
++)
1818 all_vertices
[i
] = i
;
1820 graphds_dfs (g
, all_vertices
, num_vertices
, NULL
, true, NULL
);
1821 free (all_vertices
);
1823 for (i
= 0; i
< g
->n_vertices
; i
++)
1824 scop
->drs
[i
].alias_set
= g
->vertices
[i
].component
+ 1;
1829 /* Gather BBs and conditions for a SCOP. */
1830 class gather_bbs
: public dom_walker
1833 gather_bbs (cdi_direction
, scop_p
);
1835 virtual void before_dom_children (basic_block
);
1836 virtual void after_dom_children (basic_block
);
1839 auto_vec
<gimple
*, 3> conditions
, cases
;
1843 gather_bbs::gather_bbs (cdi_direction direction
, scop_p scop
)
1844 : dom_walker (direction
), scop (scop
)
1848 /* Call-back for dom_walk executed before visiting the dominated
1852 gather_bbs::before_dom_children (basic_block bb
)
1854 if (!bb_in_sese_p (bb
, scop
->scop_info
->region
))
1857 gcond
*stmt
= single_pred_cond_non_loop_exit (bb
);
1861 edge e
= single_pred_edge (bb
);
1863 conditions
.safe_push (stmt
);
1865 if (e
->flags
& EDGE_TRUE_VALUE
)
1866 cases
.safe_push (stmt
);
1868 cases
.safe_push (NULL
);
1871 scop
->scop_info
->bbs
.safe_push (bb
);
1873 gimple_poly_bb_p gbb
= try_generate_gimple_bb (scop
, bb
);
1877 GBB_CONDITIONS (gbb
) = conditions
.copy ();
1878 GBB_CONDITION_CASES (gbb
) = cases
.copy ();
1880 poly_bb_p pbb
= new_poly_bb (scop
, gbb
);
1881 scop
->pbbs
.safe_push (pbb
);
1884 data_reference_p dr
;
1885 FOR_EACH_VEC_ELT (gbb
->data_refs
, i
, dr
)
1886 scop
->drs
.safe_push (dr_info (dr
, pbb
));
1889 /* Call-back for dom_walk executed after visiting the dominated
1893 gather_bbs::after_dom_children (basic_block bb
)
1895 if (!bb_in_sese_p (bb
, scop
->scop_info
->region
))
1898 if (single_pred_cond_non_loop_exit (bb
))
1905 /* Find Static Control Parts (SCoP) in the current function and pushes
1909 build_scops (vec
<scop_p
> *scops
)
1912 dp
.set_dump_file (dump_file
);
1914 canonicalize_loop_closed_ssa_form ();
1917 sb
.build_scop_depth (scop_detection::invalid_sese
, current_loops
->tree_root
);
1919 /* Now create scops from the lightweight SESEs. */
1920 vec
<sese_l
> scops_l
= sb
.get_scops ();
1923 FOR_EACH_VEC_ELT (scops_l
, i
, s
)
1925 scop_p scop
= new_scop (s
->entry
, s
->exit
);
1927 /* Record all basic blocks and their conditions in REGION. */
1928 gather_bbs (CDI_DOMINATORS
, scop
).walk (cfun
->cfg
->x_entry_block_ptr
);
1930 build_alias_set (scop
);
1932 /* Do not optimize a scop containing only PBBs that do not belong
1934 if (sb
.nb_pbbs_in_loops (scop
) == 0)
1936 DEBUG_PRINT (dp
<< "[scop-detection-fail] no data references.\n");
1941 unsigned max_arrays
= PARAM_VALUE (PARAM_GRAPHITE_MAX_ARRAYS_PER_SCOP
);
1942 if (scop
->drs
.length () >= max_arrays
)
1944 DEBUG_PRINT (dp
<< "[scop-detection-fail] too many data references: "
1945 << scop
->drs
.length ()
1946 << " is larger than --param graphite-max-arrays-per-scop="
1947 << max_arrays
<< ".\n");
1952 build_sese_loop_nests (scop
->scop_info
);
1954 find_scop_parameters (scop
);
1955 graphite_dim_t max_dim
= PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS
);
1957 if (scop_nb_params (scop
) > max_dim
)
1959 DEBUG_PRINT (dp
<< "[scop-detection-fail] too many parameters: "
1960 << scop_nb_params (scop
)
1961 << " larger than --param graphite-max-nb-scop-params="
1962 << max_dim
<< ".\n");
1967 scops
->safe_push (scop
);
1970 DEBUG_PRINT (dp
<< "number of SCoPs: " << (scops
? scops
->length () : 0););
1973 #endif /* HAVE_isl */