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/>. */
29 #include "coretypes.h"
37 #include "fold-const.h"
38 #include "gimple-iterator.h"
40 #include "tree-ssa-loop-manip.h"
41 #include "tree-ssa-loop-niter.h"
42 #include "tree-ssa-loop.h"
43 #include "tree-into-ssa.h"
46 #include "tree-data-ref.h"
47 #include "tree-scalar-evolution.h"
48 #include "tree-pass.h"
49 #include "tree-ssa-propagate.h"
50 #include "gimple-pretty-print.h"
52 #include <isl/constraint.h>
55 #include <isl/union_map.h>
57 #include "graphite-poly.h"
58 #include "graphite-scop-detection.h"
67 set_dump_file (FILE *f
)
73 friend debug_printer
&
74 operator<< (debug_printer
&output
, int i
)
76 fprintf (output
.dump_file
, "%d", i
);
79 friend debug_printer
&
80 operator<< (debug_printer
&output
, const char *s
)
82 fprintf (output
.dump_file
, "%s", s
);
87 #define DEBUG_PRINT(args) do \
89 if (dump_file && (dump_flags & TDF_DETAILS)) { args; } \
92 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
93 different colors. If there are not enough colors, paint the
94 remaining SCoPs in gray.
97 - "*" after the node number denotes the entry of a SCoP,
98 - "#" after the node number denotes the exit of a SCoP,
99 - "()" around the node number denotes the entry or the
100 exit nodes of the SCOP. These are not part of SCoP. */
103 dot_all_scops_1 (FILE *file
, vec
<scop_p
> scops
)
112 /* Disable debugging while printing graph. */
113 int tmp_dump_flags
= dump_flags
;
116 fprintf (file
, "digraph all {\n");
118 FOR_ALL_BB_FN (bb
, cfun
)
120 int part_of_scop
= false;
122 /* Use HTML for every bb label. So we are able to print bbs
123 which are part of two different SCoPs, with two different
124 background colors. */
125 fprintf (file
, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
127 fprintf (file
, "CELLSPACING=\"0\">\n");
129 /* Select color for SCoP. */
130 FOR_EACH_VEC_ELT (scops
, i
, scop
)
132 sese_l region
= scop
->scop_info
->region
;
133 if (bb_in_sese_p (bb
, region
) || (region
.exit
->dest
== bb
)
134 || (region
.entry
->dest
== bb
))
193 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">",
196 if (!bb_in_sese_p (bb
, region
))
197 fprintf (file
, " (");
199 if (bb
== region
.entry
->dest
&& bb
== region
.exit
->dest
)
200 fprintf (file
, " %d*# ", bb
->index
);
201 else if (bb
== region
.entry
->dest
)
202 fprintf (file
, " %d* ", bb
->index
);
203 else if (bb
== region
.exit
->dest
)
204 fprintf (file
, " %d# ", bb
->index
);
206 fprintf (file
, " %d ", bb
->index
);
208 fprintf (file
, "{lp_%d}", bb
->loop_father
->num
);
210 if (!bb_in_sese_p (bb
, region
))
213 fprintf (file
, "</TD></TR>\n");
220 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
221 fprintf (file
, " %d {lp_%d} </TD></TR>\n", bb
->index
,
222 bb
->loop_father
->num
);
224 fprintf (file
, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
227 FOR_ALL_BB_FN (bb
, cfun
)
229 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
230 fprintf (file
, "%d -> %d;\n", bb
->index
, e
->dest
->index
);
233 fputs ("}\n\n", file
);
235 /* Enable debugging again. */
236 dump_flags
= tmp_dump_flags
;
239 /* Display all SCoPs using dotty. */
242 dot_all_scops (vec
<scop_p
> scops
)
244 /* When debugging, enable the following code. This cannot be used
245 in production compilers because it calls "system". */
248 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
251 dot_all_scops_1 (stream
, scops
);
254 x
= system ("dotty /tmp/allscops.dot &");
256 dot_all_scops_1 (stderr
, scops
);
260 /* Display all SCoPs using dotty. */
263 dot_scop (scop_p scop
)
265 auto_vec
<scop_p
, 1> scops
;
268 scops
.safe_push (scop
);
270 /* When debugging, enable the following code. This cannot be used
271 in production compilers because it calls "system". */
275 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
278 dot_all_scops_1 (stream
, scops
);
280 x
= system ("dotty /tmp/allscops.dot &");
283 dot_all_scops_1 (stderr
, scops
);
287 /* Return true if BB is empty, contains only DEBUG_INSNs. */
290 trivially_empty_bb_p (basic_block bb
)
292 gimple_stmt_iterator gsi
;
294 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
295 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_DEBUG
)
301 /* Returns true when P1 and P2 are close phis with the same
305 same_close_phi_node (gphi
*p1
, gphi
*p2
)
307 return operand_equal_p (gimple_phi_arg_def (p1
, 0),
308 gimple_phi_arg_def (p2
, 0), 0);
311 static void make_close_phi_nodes_unique (basic_block bb
);
313 /* Remove the close phi node at GSI and replace its rhs with the rhs
317 remove_duplicate_close_phi (gphi
*phi
, gphi_iterator
*gsi
)
321 imm_use_iterator imm_iter
;
322 tree res
= gimple_phi_result (phi
);
323 tree def
= gimple_phi_result (gsi
->phi ());
325 gcc_assert (same_close_phi_node (phi
, gsi
->phi ()));
327 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
329 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
330 SET_USE (use_p
, res
);
332 update_stmt (use_stmt
);
334 /* It is possible that we just created a duplicate close-phi
335 for an already-processed containing loop. Check for this
336 case and clean it up. */
337 if (gimple_code (use_stmt
) == GIMPLE_PHI
338 && gimple_phi_num_args (use_stmt
) == 1)
339 make_close_phi_nodes_unique (gimple_bb (use_stmt
));
342 remove_phi_node (gsi
, true);
345 /* Removes all the close phi duplicates from BB. */
348 make_close_phi_nodes_unique (basic_block bb
)
352 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
354 gphi_iterator gsi
= psi
;
355 gphi
*phi
= psi
.phi ();
357 /* At this point, PHI should be a close phi in normal form. */
358 gcc_assert (gimple_phi_num_args (phi
) == 1);
360 /* Iterate over the next phis and remove duplicates. */
362 while (!gsi_end_p (gsi
))
363 if (same_close_phi_node (phi
, gsi
.phi ()))
364 remove_duplicate_close_phi (phi
, &gsi
);
370 /* Transforms LOOP to the canonical loop closed SSA form. */
373 canonicalize_loop_closed_ssa (loop_p loop
)
375 edge e
= single_exit (loop
);
378 if (!e
|| e
->flags
& EDGE_ABNORMAL
)
383 if (single_pred_p (bb
))
385 e
= split_block_after_labels (bb
);
386 DEBUG_PRINT (dp
<< "\nSplitting bb_" << bb
->index
);
387 make_close_phi_nodes_unique (e
->src
);
392 basic_block close
= split_edge (e
);
394 e
= single_succ_edge (close
);
395 DEBUG_PRINT (dp
<< "\nSplitting edge (" << e
->src
->index
<< ","
396 << e
->dest
->index
<< ")\n");
398 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
400 gphi
*phi
= psi
.phi ();
403 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
404 if (gimple_phi_arg_edge (phi
, i
) == e
)
406 tree res
, arg
= gimple_phi_arg_def (phi
, i
);
410 if (TREE_CODE (arg
) != SSA_NAME
)
413 close_phi
= create_phi_node (NULL_TREE
, close
);
414 res
= create_new_def_for (arg
, close_phi
,
415 gimple_phi_result_ptr (close_phi
));
416 add_phi_arg (close_phi
, arg
,
417 gimple_phi_arg_edge (close_phi
, 0),
419 use_p
= gimple_phi_arg_imm_use_ptr (phi
, i
);
420 replace_exp (use_p
, res
);
425 make_close_phi_nodes_unique (close
);
428 /* The code above does not properly handle changes in the post dominance
429 information (yet). */
430 recompute_all_dominators ();
433 /* Converts the current loop closed SSA form to a canonical form
434 expected by the Graphite code generation.
436 The loop closed SSA form has the following invariant: a variable
437 defined in a loop that is used outside the loop appears only in the
438 phi nodes in the destination of the loop exit. These phi nodes are
439 called close phi nodes.
441 The canonical loop closed SSA form contains the extra invariants:
443 - when the loop contains only one exit, the close phi nodes contain
444 only one argument. That implies that the basic block that contains
445 the close phi nodes has only one predecessor, that is a basic block
448 - the basic block containing the close phi nodes does not contain
451 - there exist only one phi node per definition in the loop.
455 canonicalize_loop_closed_ssa_form (void)
457 checking_verify_loop_closed_ssa (true);
460 FOR_EACH_LOOP (loop
, 0)
461 canonicalize_loop_closed_ssa (loop
);
463 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
464 update_ssa (TODO_update_ssa
);
466 checking_verify_loop_closed_ssa (true);
469 /* Can all ivs be represented by a signed integer?
470 As ISL might generate negative values in its expressions, signed loop ivs
471 are required in the backend. */
474 loop_ivs_can_be_represented (loop_p loop
)
476 unsigned type_long_long
= TYPE_PRECISION (long_long_integer_type_node
);
477 for (gphi_iterator psi
= gsi_start_phis (loop
->header
); !gsi_end_p (psi
);
480 gphi
*phi
= psi
.phi ();
481 tree res
= PHI_RESULT (phi
);
482 tree type
= TREE_TYPE (res
);
484 if (TYPE_UNSIGNED (type
) && TYPE_PRECISION (type
) >= type_long_long
)
491 /* Returns a COND_EXPR statement when BB has a single predecessor, the
492 edge between BB and its predecessor is not a loop exit edge, and
493 the last statement of the single predecessor is a COND_EXPR. */
496 single_pred_cond_non_loop_exit (basic_block bb
)
498 if (single_pred_p (bb
))
500 edge e
= single_pred_edge (bb
);
501 basic_block pred
= e
->src
;
504 if (loop_depth (pred
->loop_father
) > loop_depth (bb
->loop_father
))
507 stmt
= last_stmt (pred
);
509 if (stmt
&& gimple_code (stmt
) == GIMPLE_COND
)
510 return as_a
<gcond
*> (stmt
);
519 /* Build the maximal scop containing LOOPs and add it to SCOPS. */
524 scop_detection () : scops (vNULL
) {}
531 /* A marker for invalid sese_l. */
532 static sese_l invalid_sese
;
534 /* Return the SCOPS in this SCOP_DETECTION. */
542 /* Return an sese_l around the LOOP. */
544 sese_l
get_sese (loop_p loop
);
546 /* Return the closest dominator with a single entry edge. In case of a
547 back-loop the back-edge is not counted. */
549 static edge
get_nearest_dom_with_single_entry (basic_block dom
);
551 /* Return the closest post-dominator with a single exit edge. In case of a
552 back-loop the back-edge is not counted. */
554 static edge
get_nearest_pdom_with_single_exit (basic_block dom
);
556 /* Print S to FILE. */
558 static void print_sese (FILE *file
, sese_l s
);
560 /* Merge scops at same loop depth and returns the new sese.
561 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
563 sese_l
merge_sese (sese_l first
, sese_l second
) const;
565 /* Build scop outer->inner if possible. */
567 sese_l
build_scop_depth (sese_l s
, loop_p loop
);
569 /* If loop and loop->next are valid scops, try to merge them. */
571 sese_l
build_scop_breadth (sese_l s1
, loop_p loop
);
573 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
574 region of code that can be represented in the polyhedral model. SCOP
575 defines the region we analyse. */
577 bool loop_is_valid_scop (loop_p loop
, sese_l scop
) const;
579 /* Return true when BEGIN is the preheader edge of a loop with a single exit
582 static bool region_has_one_loop (sese_l s
);
584 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
586 void add_scop (sese_l s
);
588 /* Returns true if S1 subsumes/surrounds S2. */
589 static bool subsumes (sese_l s1
, sese_l s2
);
591 /* Remove a SCoP which is subsumed by S1. */
592 void remove_subscops (sese_l s1
);
594 /* Returns true if S1 intersects with S2. Since we already know that S1 does
595 not subsume S2 or vice-versa, we only check for entry bbs. */
597 static bool intersects (sese_l s1
, sese_l s2
);
599 /* Remove one of the scops when it intersects with any other. */
601 void remove_intersecting_scops (sese_l s1
);
603 /* Return true when the body of LOOP has statements that can be represented
606 bool loop_body_is_valid_scop (loop_p loop
, sese_l scop
) const;
608 /* Return true when BB contains a harmful operation for a scop: that
609 can be a function call with side effects, the induction variables
610 are not linear with respect to SCOP, etc. The current open
611 scop should end before this statement. */
613 bool harmful_stmt_in_bb (sese_l scop
, basic_block bb
) const;
615 /* Return true when a statement in SCOP cannot be represented by Graphite.
616 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
617 Limit the number of bbs between adjacent loops to
618 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
620 bool harmful_stmt_in_region (sese_l scop
) const;
622 /* Return true only when STMT is simple enough for being handled by Graphite.
623 This depends on SCOP, as the parameters are initialized relatively to
624 this basic block, the linear functions are initialized based on the
625 outermost loop containing STMT inside the SCOP. BB is the place where we
626 try to evaluate the STMT. */
628 bool stmt_simple_for_scop_p (sese_l scop
, gimple
*stmt
,
629 basic_block bb
) const;
631 /* Something like "n * m" is not allowed. */
633 static bool graphite_can_represent_init (tree e
);
635 /* Return true when SCEV can be represented in the polyhedral model.
637 An expression can be represented, if it can be expressed as an
638 affine expression. For loops (i, j) and parameters (m, n) all
639 affine expressions are of the form:
641 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
643 1 i + 20 j + (-2) m + 25
645 Something like "i * n" or "n * m" is not allowed. */
647 static bool graphite_can_represent_scev (tree scev
);
649 /* Return true when EXPR can be represented in the polyhedral model.
651 This means an expression can be represented, if it is linear with respect
652 to the loops and the strides are non parametric. LOOP is the place where
653 the expr will be evaluated. SCOP defines the region we analyse. */
655 static bool graphite_can_represent_expr (sese_l scop
, loop_p loop
,
658 /* Return true if the data references of STMT can be represented by Graphite.
659 We try to analyze the data references in a loop contained in the SCOP. */
661 static bool stmt_has_simple_data_refs_p (sese_l scop
, gimple
*stmt
);
663 /* Remove the close phi node at GSI and replace its rhs with the rhs
666 static void remove_duplicate_close_phi (gphi
*phi
, gphi_iterator
*gsi
);
668 /* Returns true when Graphite can represent LOOP in SCOP.
669 FIXME: For the moment, graphite cannot be used on loops that iterate using
670 induction variables that wrap. */
672 static bool can_represent_loop_1 (loop_p loop
, sese_l scop
);
674 /* Return true when all the loops within LOOP can be represented by
677 static bool can_represent_loop (loop_p loop
, sese_l scop
);
679 /* Returns the number of pbbs that are in loops contained in SCOP. */
681 static int nb_pbbs_in_loops (scop_p scop
);
683 static bool graphite_can_represent_stmt (sese_l
, gimple
*, basic_block
);
689 sese_l
scop_detection::invalid_sese (NULL
, NULL
);
691 /* Return an sese_l around the LOOP. */
694 scop_detection::get_sese (loop_p loop
)
699 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS
))
701 edge scop_end
= single_exit (loop
);
704 edge scop_begin
= loop_preheader_edge (loop
);
705 sese_l
s (scop_begin
, scop_end
);
709 /* Return the closest dominator with a single entry edge. */
712 scop_detection::get_nearest_dom_with_single_entry (basic_block dom
)
716 /* If e1->src dominates e2->src then e1->src will also dominate dom. */
717 if (dom
->preds
->length () == 2)
719 edge e1
= (*dom
->preds
)[0];
720 edge e2
= (*dom
->preds
)[1];
721 if (dominated_by_p (CDI_DOMINATORS
, e2
->src
, e1
->src
))
723 if (dominated_by_p (CDI_DOMINATORS
, e1
->src
, e2
->src
))
727 while (dom
->preds
->length () != 1)
729 if (dom
->preds
->length () < 1)
731 dom
= get_immediate_dominator (CDI_DOMINATORS
, dom
);
735 return (*dom
->preds
)[0];
738 /* Return the closest post-dominator with a single exit edge. In case of a
739 back-loop the back-edge is not counted. */
742 scop_detection::get_nearest_pdom_with_single_exit (basic_block dom
)
746 if (dom
->succs
->length () == 2)
748 edge e1
= (*dom
->succs
)[0];
749 edge e2
= (*dom
->succs
)[1];
750 if (dominated_by_p (CDI_POST_DOMINATORS
, e2
->dest
, e1
->dest
))
752 if (dominated_by_p (CDI_POST_DOMINATORS
, e1
->dest
, e2
->dest
))
756 while (dom
->succs
->length () != 1)
758 if (dom
->succs
->length () < 1)
760 dom
= get_immediate_dominator (CDI_POST_DOMINATORS
, dom
);
764 return (*dom
->succs
)[0];
767 /* Print S to FILE. */
770 scop_detection::print_sese (FILE *file
, sese_l s
)
772 fprintf (file
, "(entry_edge (bb_%d, bb_%d), exit_edge (bb_%d, bb_%d))\n",
773 s
.entry
->src
->index
, s
.entry
->dest
->index
,
774 s
.exit
->src
->index
, s
.exit
->dest
->index
);
777 /* Merge scops at same loop depth and returns the new sese.
778 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
781 scop_detection::merge_sese (sese_l first
, sese_l second
) const
783 /* In the trivial case first/second may be NULL. */
789 DEBUG_PRINT (dp
<< "[try-merging-sese] s1: "; print_sese (dump_file
, first
);
790 dp
<< "[try-merging-sese] s2: ";
791 print_sese (dump_file
, second
));
793 /* Assumption: Both the sese's should be at the same loop depth or one scop
794 should subsume the other like in case of nested loops. */
796 /* Find the common dominators for entry,
797 and common post-dominators for the exit. */
798 basic_block dom
= nearest_common_dominator (CDI_DOMINATORS
,
799 get_entry_bb (first
),
800 get_entry_bb (second
));
802 edge entry
= get_nearest_dom_with_single_entry (dom
);
804 if (!entry
|| (entry
->flags
& EDGE_IRREDUCIBLE_LOOP
))
807 basic_block pdom
= nearest_common_dominator (CDI_POST_DOMINATORS
,
809 get_exit_bb (second
));
810 pdom
= nearest_common_dominator (CDI_POST_DOMINATORS
, dom
, pdom
);
812 edge exit
= get_nearest_pdom_with_single_exit (pdom
);
814 if (!exit
|| (exit
->flags
& EDGE_IRREDUCIBLE_LOOP
))
817 sese_l
combined (entry
, exit
);
819 /* FIXME: We could iterate to find the dom which dominates pdom, and pdom
820 which post-dominates dom, until it stabilizes. Also, ENTRY->SRC and
821 EXIT->DEST should be in the same loop nest. */
822 if (!dominated_by_p (CDI_DOMINATORS
, pdom
, dom
)
823 || loop_depth (entry
->src
->loop_father
)
824 != loop_depth (exit
->dest
->loop_father
))
827 /* For now we just want to bail out when exit does not post-dominate entry.
828 TODO: We might just add a basic_block at the exit to make exit
829 post-dominate entry (the entire region). */
830 if (!dominated_by_p (CDI_POST_DOMINATORS
, get_entry_bb (combined
),
831 get_exit_bb (combined
))
832 || !dominated_by_p (CDI_DOMINATORS
, get_exit_bb (combined
),
833 get_entry_bb (combined
)))
835 DEBUG_PRINT (dp
<< "[scop-detection-fail] cannot merge seses.\n");
839 /* FIXME: We should remove this piece of code once
840 canonicalize_loop_closed_ssa has been removed, because that function
841 adds a BB with single exit. */
842 if (!trivially_empty_bb_p (get_exit_bb (combined
)))
844 /* Find the first empty succ (with single exit) of combined.exit. */
845 basic_block imm_succ
= combined
.exit
->dest
;
846 if (single_succ_p (imm_succ
) && trivially_empty_bb_p (imm_succ
))
847 combined
.exit
= single_succ_edge (imm_succ
);
850 DEBUG_PRINT (dp
<< "\n[scop-detection-fail] Discarding SCoP because "
851 << "no single exit (empty succ) for sese exit";
852 print_sese (dump_file
, combined
));
857 /* Analyze all the BBs in new sese. */
858 if (harmful_stmt_in_region (combined
))
861 DEBUG_PRINT (dp
<< "[merged-sese] s1: "; print_sese (dump_file
, combined
));
866 /* Build scop outer->inner if possible. */
869 scop_detection::build_scop_depth (sese_l s
, loop_p loop
)
874 DEBUG_PRINT (dp
<< "\n[Depth loop_" << loop
->num
<< "]");
875 s
= build_scop_depth (s
, loop
->inner
);
877 sese_l s2
= merge_sese (s
, get_sese (loop
));
880 /* s might be a valid scop, so return it and start analyzing from the
882 build_scop_depth (invalid_sese
, loop
->next
);
886 if (!loop_is_valid_scop (loop
, s2
))
887 return build_scop_depth (invalid_sese
, loop
->next
);
889 return build_scop_breadth (s2
, loop
);
892 /* If loop and loop->next are valid scops, try to merge them. */
895 scop_detection::build_scop_breadth (sese_l s1
, loop_p loop
)
899 DEBUG_PRINT (dp
<< "\n[Breadth loop_" << loop
->num
<< "]");
903 sese_l s2
= build_scop_depth (invalid_sese
, l
->next
);
911 sese_l combined
= merge_sese (s1
, s2
);
923 /* Returns true when Graphite can represent LOOP in SCOP.
924 FIXME: For the moment, graphite cannot be used on loops that iterate using
925 induction variables that wrap. */
928 scop_detection::can_represent_loop_1 (loop_p loop
, sese_l scop
)
931 struct tree_niter_desc niter_desc
;
933 return single_exit (loop
)
934 && !(loop_preheader_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
)
935 && number_of_iterations_exit (loop
, single_exit (loop
), &niter_desc
, false)
936 && niter_desc
.control
.no_overflow
937 && (niter
= number_of_latch_executions (loop
))
938 && !chrec_contains_undetermined (niter
)
939 && graphite_can_represent_expr (scop
, loop
, niter
);
942 /* Return true when all the loops within LOOP can be represented by
946 scop_detection::can_represent_loop (loop_p loop
, sese_l scop
)
948 if (!can_represent_loop_1 (loop
, scop
))
950 if (loop
->inner
&& !can_represent_loop (loop
->inner
, scop
))
952 if (loop
->next
&& !can_represent_loop (loop
->next
, scop
))
958 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
959 region of code that can be represented in the polyhedral model. SCOP
960 defines the region we analyse. */
963 scop_detection::loop_is_valid_scop (loop_p loop
, sese_l scop
) const
968 if (!optimize_loop_nest_for_speed_p (loop
))
970 DEBUG_PRINT (dp
<< "[scop-detection-fail] loop_"
971 << loop
->num
<< " is not on a hot path.\n");
975 if (!can_represent_loop (loop
, scop
))
977 DEBUG_PRINT (dp
<< "[scop-detection-fail] cannot represent loop_"
978 << loop
->num
<< "\n");
982 if (loop_body_is_valid_scop (loop
, scop
))
984 DEBUG_PRINT (dp
<< "[valid-scop] loop_" << loop
->num
985 << "is a valid scop.\n");
991 /* Return true when BEGIN is the preheader edge of a loop with a single exit
995 scop_detection::region_has_one_loop (sese_l s
)
997 edge begin
= s
.entry
;
999 /* Check for a single perfectly nested loop. */
1000 if (begin
->dest
->loop_father
->inner
)
1003 /* Otherwise, check whether we have adjacent loops. */
1004 return begin
->dest
->loop_father
== end
->src
->loop_father
;
1007 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
1010 scop_detection::add_scop (sese_l s
)
1014 /* Do not add scops with only one loop. */
1015 if (region_has_one_loop (s
))
1017 DEBUG_PRINT (dp
<< "\n[scop-detection-fail] Discarding one loop SCoP";
1018 print_sese (dump_file
, s
));
1022 if (get_exit_bb (s
) == EXIT_BLOCK_PTR_FOR_FN (cfun
))
1024 DEBUG_PRINT (dp
<< "\n[scop-detection-fail] "
1025 << "Discarding SCoP exiting to return";
1026 print_sese (dump_file
, s
));
1030 /* Remove all the scops which are subsumed by s. */
1031 remove_subscops (s
);
1033 /* Replace this with split-intersecting scops. */
1034 remove_intersecting_scops (s
);
1036 scops
.safe_push (s
);
1037 DEBUG_PRINT (dp
<< "\nAdding SCoP "; print_sese (dump_file
, s
));
1040 /* Return true when a statement in SCOP cannot be represented by Graphite.
1041 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
1042 Limit the number of bbs between adjacent loops to
1043 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
1046 scop_detection::harmful_stmt_in_region (sese_l scop
) const
1048 basic_block exit_bb
= get_exit_bb (scop
);
1049 basic_block entry_bb
= get_entry_bb (scop
);
1051 DEBUG_PRINT (dp
<< "\n[checking-harmful-bbs] ";
1052 print_sese (dump_file
, scop
));
1053 gcc_assert (dominated_by_p (CDI_DOMINATORS
, exit_bb
, entry_bb
));
1055 int depth
= bb_dom_dfs_in (CDI_DOMINATORS
, exit_bb
)
1056 - bb_dom_dfs_in (CDI_DOMINATORS
, entry_bb
);
1058 gcc_assert (depth
> 0);
1060 vec
<basic_block
> dom
1061 = get_dominated_to_depth (CDI_DOMINATORS
, entry_bb
, depth
);
1064 FOR_EACH_VEC_ELT (dom
, i
, bb
)
1066 DEBUG_PRINT (dp
<< "Visiting bb_" << bb
->index
<< "\n");
1068 /* We don't want to analyze any bb outside sese. */
1069 if (!dominated_by_p (CDI_POST_DOMINATORS
, bb
, exit_bb
))
1072 /* Basic blocks dominated by the scop->exit are not in the scop. */
1073 if (bb
!= exit_bb
&& dominated_by_p (CDI_DOMINATORS
, bb
, exit_bb
))
1076 /* The basic block should not be part of an irreducible loop. */
1077 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1083 if (harmful_stmt_in_bb (scop
, bb
))
1094 /* Returns true if S1 subsumes/surrounds S2. */
1096 scop_detection::subsumes (sese_l s1
, sese_l s2
)
1098 if (dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
),
1100 && dominated_by_p (CDI_POST_DOMINATORS
, s2
.exit
->dest
,
1106 /* Remove a SCoP which is subsumed by S1. */
1108 scop_detection::remove_subscops (sese_l s1
)
1112 FOR_EACH_VEC_ELT_REVERSE (scops
, j
, s2
)
1114 if (subsumes (s1
, *s2
))
1116 DEBUG_PRINT (dp
<< "\nRemoving sub-SCoP";
1117 print_sese (dump_file
, *s2
));
1118 scops
.unordered_remove (j
);
1123 /* Returns true if S1 intersects with S2. Since we already know that S1 does
1124 not subsume S2 or vice-versa, we only check for entry bbs. */
1127 scop_detection::intersects (sese_l s1
, sese_l s2
)
1129 if (dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
),
1131 && !dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
),
1134 if ((s1
.exit
== s2
.entry
) || (s2
.exit
== s1
.entry
))
1140 /* Remove one of the scops when it intersects with any other. */
1143 scop_detection::remove_intersecting_scops (sese_l s1
)
1147 FOR_EACH_VEC_ELT_REVERSE (scops
, j
, s2
)
1149 if (intersects (s1
, *s2
))
1151 DEBUG_PRINT (dp
<< "\nRemoving intersecting SCoP";
1152 print_sese (dump_file
, *s2
); dp
<< "Intersects with:";
1153 print_sese (dump_file
, s1
));
1154 scops
.unordered_remove (j
);
1159 /* Something like "n * m" is not allowed. */
1162 scop_detection::graphite_can_represent_init (tree e
)
1164 switch (TREE_CODE (e
))
1166 case POLYNOMIAL_CHREC
:
1167 return graphite_can_represent_init (CHREC_LEFT (e
))
1168 && graphite_can_represent_init (CHREC_RIGHT (e
));
1171 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
1172 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
1173 && tree_fits_shwi_p (TREE_OPERAND (e
, 1));
1175 return graphite_can_represent_init (TREE_OPERAND (e
, 1))
1176 && tree_fits_shwi_p (TREE_OPERAND (e
, 0));
1179 case POINTER_PLUS_EXPR
:
1181 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
1182 && graphite_can_represent_init (TREE_OPERAND (e
, 1));
1187 case NON_LVALUE_EXPR
:
1188 return graphite_can_represent_init (TREE_OPERAND (e
, 0));
1197 /* Return true when SCEV can be represented in the polyhedral model.
1199 An expression can be represented, if it can be expressed as an
1200 affine expression. For loops (i, j) and parameters (m, n) all
1201 affine expressions are of the form:
1203 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
1205 1 i + 20 j + (-2) m + 25
1207 Something like "i * n" or "n * m" is not allowed. */
1210 scop_detection::graphite_can_represent_scev (tree scev
)
1212 if (chrec_contains_undetermined (scev
))
1215 /* We disable the handling of pointer types, because it’s currently not
1216 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
1217 the only nodes, which are disabled in case they are pointers to object
1218 types, but this can be changed. */
1220 if (POINTER_TYPE_P (TREE_TYPE (scev
)) && TREE_CODE (scev
) == SSA_NAME
)
1223 switch (TREE_CODE (scev
))
1228 case NON_LVALUE_EXPR
:
1229 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0));
1232 case POINTER_PLUS_EXPR
:
1234 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
1235 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
1238 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 0)))
1239 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 1)))
1240 && !(chrec_contains_symbols (TREE_OPERAND (scev
, 0))
1241 && chrec_contains_symbols (TREE_OPERAND (scev
, 1)))
1242 && graphite_can_represent_init (scev
)
1243 && graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
1244 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
1246 case POLYNOMIAL_CHREC
:
1247 /* Check for constant strides. With a non constant stride of
1248 'n' we would have a value of 'iv * n'. Also check that the
1249 initial value can represented: for example 'n * m' cannot be
1251 if (!evolution_function_right_is_integer_cst (scev
)
1252 || !graphite_can_represent_init (scev
))
1254 return graphite_can_represent_scev (CHREC_LEFT (scev
));
1260 /* Only affine functions can be represented. */
1261 if (tree_contains_chrecs (scev
, NULL
) || !scev_is_linear_expression (scev
))
1267 /* Return true when EXPR can be represented in the polyhedral model.
1269 This means an expression can be represented, if it is linear with respect to
1270 the loops and the strides are non parametric. LOOP is the place where the
1271 expr will be evaluated. SCOP defines the region we analyse. */
1274 scop_detection::graphite_can_represent_expr (sese_l scop
, loop_p loop
,
1277 tree scev
= scalar_evolution_in_region (scop
, loop
, expr
);
1278 return graphite_can_represent_scev (scev
);
1281 /* Return true if the data references of STMT can be represented by Graphite.
1282 We try to analyze the data references in a loop contained in the SCOP. */
1285 scop_detection::stmt_has_simple_data_refs_p (sese_l scop
, gimple
*stmt
)
1287 loop_p nest
= outermost_loop_in_sese (scop
, gimple_bb (stmt
));
1288 loop_p loop
= loop_containing_stmt (stmt
);
1289 vec
<data_reference_p
> drs
= vNULL
;
1291 graphite_find_data_references_in_stmt (nest
, loop
, stmt
, &drs
);
1294 data_reference_p dr
;
1295 FOR_EACH_VEC_ELT (drs
, j
, dr
)
1297 int nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1299 if (nb_subscripts
< 1)
1301 free_data_refs (drs
);
1305 tree ref
= DR_REF (dr
);
1307 for (int i
= nb_subscripts
- 1; i
>= 0; i
--)
1309 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr
, i
))
1310 || (TREE_CODE (ref
) != ARRAY_REF
&& TREE_CODE (ref
) != MEM_REF
1311 && TREE_CODE (ref
) != COMPONENT_REF
))
1313 free_data_refs (drs
);
1317 ref
= TREE_OPERAND (ref
, 0);
1321 free_data_refs (drs
);
1325 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
1326 Calls have side-effects, except those to const or pure
1330 stmt_has_side_effects (gimple
*stmt
)
1332 if (gimple_has_volatile_ops (stmt
)
1333 || (gimple_code (stmt
) == GIMPLE_CALL
1334 && !(gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
)))
1335 || (gimple_code (stmt
) == GIMPLE_ASM
))
1337 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1338 << "Statement has side-effects:\n";
1339 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
| TDF_MEMSYMS
));
1345 /* Returns true if STMT can be represented in polyhedral model. LABEL,
1346 simple COND stmts, pure calls, and assignments can be repesented. */
1349 scop_detection::graphite_can_represent_stmt (sese_l scop
, gimple
*stmt
,
1352 loop_p loop
= bb
->loop_father
;
1353 switch (gimple_code (stmt
))
1360 /* We can handle all binary comparisons. Inequalities are
1361 also supported as they can be represented with union of
1363 enum tree_code code
= gimple_cond_code (stmt
);
1364 if (!(code
== LT_EXPR
1369 || code
== NE_EXPR
))
1371 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1372 << "Graphite cannot handle cond stmt:\n";
1373 print_gimple_stmt (dump_file
, stmt
, 0,
1374 TDF_VOPS
| TDF_MEMSYMS
));
1378 for (unsigned i
= 0; i
< 2; ++i
)
1380 tree op
= gimple_op (stmt
, i
);
1381 if (!graphite_can_represent_expr (scop
, loop
, op
)
1382 /* We can only constrain on integer type. */
1383 || (TREE_CODE (TREE_TYPE (op
)) != INTEGER_TYPE
))
1385 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1386 << "Graphite cannot represent stmt:\n";
1387 print_gimple_stmt (dump_file
, stmt
, 0,
1388 TDF_VOPS
| TDF_MEMSYMS
));
1401 /* These nodes cut a new scope. */
1403 dp
<< "[scop-detection-fail] "
1404 << "Gimple stmt not handled in Graphite:\n";
1405 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
| TDF_MEMSYMS
));
1410 /* Return true only when STMT is simple enough for being handled by Graphite.
1411 This depends on SCOP, as the parameters are initialized relatively to
1412 this basic block, the linear functions are initialized based on the outermost
1413 loop containing STMT inside the SCOP. BB is the place where we try to
1414 evaluate the STMT. */
1417 scop_detection::stmt_simple_for_scop_p (sese_l scop
, gimple
*stmt
,
1418 basic_block bb
) const
1422 if (is_gimple_debug (stmt
))
1425 if (stmt_has_side_effects (stmt
))
1428 if (!stmt_has_simple_data_refs_p (scop
, stmt
))
1430 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1431 << "Graphite cannot handle data-refs in stmt:\n";
1432 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
););
1436 return graphite_can_represent_stmt (scop
, stmt
, bb
);
1439 /* Return true when BB contains a harmful operation for a scop: that
1440 can be a function call with side effects, the induction variables
1441 are not linear with respect to SCOP, etc. The current open
1442 scop should end before this statement. */
1445 scop_detection::harmful_stmt_in_bb (sese_l scop
, basic_block bb
) const
1447 gimple_stmt_iterator gsi
;
1449 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1450 if (!stmt_simple_for_scop_p (scop
, gsi_stmt (gsi
), bb
))
1456 /* Return true when the body of LOOP has statements that can be represented as a
1460 scop_detection::loop_body_is_valid_scop (loop_p loop
, sese_l scop
) const
1462 if (!loop_ivs_can_be_represented (loop
))
1464 DEBUG_PRINT (dp
<< "[scop-detection-fail] loop_" << loop
->num
1465 << "IV cannot be represented.\n");
1469 if (!loop_nest_has_data_refs (loop
))
1471 DEBUG_PRINT (dp
<< "[scop-detection-fail] loop_" << loop
->num
1472 << "does not have any data reference.\n");
1476 basic_block
*bbs
= get_loop_body (loop
);
1477 for (unsigned i
= 0; i
< loop
->num_nodes
; i
++)
1479 basic_block bb
= bbs
[i
];
1481 if (harmful_stmt_in_bb (scop
, bb
))
1491 if (!loop_body_is_valid_scop (loop
, scop
))
1500 /* Returns the number of pbbs that are in loops contained in SCOP. */
1503 scop_detection::nb_pbbs_in_loops (scop_p scop
)
1509 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
1510 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb
)), scop
->scop_info
->region
))
1516 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
1517 Otherwise returns -1. */
1520 parameter_index_in_region_1 (tree name
, sese_info_p region
)
1525 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
1527 FOR_EACH_VEC_ELT (region
->params
, i
, p
)
1534 /* When the parameter NAME is in REGION, returns its index in
1535 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
1536 and returns the index of NAME. */
1539 parameter_index_in_region (tree name
, sese_info_p region
)
1543 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
1545 /* Cannot constrain on anything else than INTEGER_TYPE parameters. */
1546 if (TREE_CODE (TREE_TYPE (name
)) != INTEGER_TYPE
)
1549 if (!invariant_in_sese_p_rec (name
, region
->region
, NULL
))
1552 i
= parameter_index_in_region_1 (name
, region
);
1556 i
= region
->params
.length ();
1557 region
->params
.safe_push (name
);
1561 /* In the context of sese S, scan the expression E and translate it to
1562 a linear expression C. When parsing a symbolic multiplication, K
1563 represents the constant multiplier of an expression containing
1567 scan_tree_for_params (sese_info_p s
, tree e
)
1569 if (e
== chrec_dont_know
)
1572 switch (TREE_CODE (e
))
1574 case POLYNOMIAL_CHREC
:
1575 scan_tree_for_params (s
, CHREC_LEFT (e
));
1579 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
1580 scan_tree_for_params (s
, TREE_OPERAND (e
, 0));
1582 scan_tree_for_params (s
, TREE_OPERAND (e
, 1));
1586 case POINTER_PLUS_EXPR
:
1588 scan_tree_for_params (s
, TREE_OPERAND (e
, 0));
1589 scan_tree_for_params (s
, TREE_OPERAND (e
, 1));
1595 case NON_LVALUE_EXPR
:
1596 scan_tree_for_params (s
, TREE_OPERAND (e
, 0));
1600 parameter_index_in_region (e
, s
);
1616 /* Find parameters with respect to REGION in BB. We are looking in memory
1617 access functions, conditions and loop bounds. */
1620 find_params_in_bb (sese_info_p region
, gimple_poly_bb_p gbb
)
1622 /* Find parameters in the access functions of data references. */
1624 data_reference_p dr
;
1625 FOR_EACH_VEC_ELT (GBB_DATA_REFS (gbb
), i
, dr
)
1626 for (unsigned j
= 0; j
< DR_NUM_DIMENSIONS (dr
); j
++)
1627 scan_tree_for_params (region
, DR_ACCESS_FN (dr
, j
));
1629 /* Find parameters in conditional statements. */
1631 loop_p loop
= GBB_BB (gbb
)->loop_father
;
1632 FOR_EACH_VEC_ELT (GBB_CONDITIONS (gbb
), i
, stmt
)
1634 tree lhs
= scalar_evolution_in_region (region
->region
, loop
,
1635 gimple_cond_lhs (stmt
));
1636 tree rhs
= scalar_evolution_in_region (region
->region
, loop
,
1637 gimple_cond_rhs (stmt
));
1639 scan_tree_for_params (region
, lhs
);
1640 scan_tree_for_params (region
, rhs
);
1644 /* Record the parameters used in the SCOP. A variable is a parameter
1645 in a scop if it does not vary during the execution of that scop. */
1648 find_scop_parameters (scop_p scop
)
1651 sese_info_p region
= scop
->scop_info
;
1654 /* Find the parameters used in the loop bounds. */
1655 FOR_EACH_VEC_ELT (region
->loop_nest
, i
, loop
)
1657 tree nb_iters
= number_of_latch_executions (loop
);
1659 if (!chrec_contains_symbols (nb_iters
))
1662 nb_iters
= scalar_evolution_in_region (region
->region
, loop
, nb_iters
);
1663 scan_tree_for_params (region
, nb_iters
);
1666 /* Find the parameters used in data accesses. */
1668 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
1669 find_params_in_bb (region
, PBB_BLACK_BOX (pbb
));
1671 int nbp
= sese_nb_params (region
);
1672 scop_set_nb_params (scop
, nbp
);
1675 /* Record DEF if it is used in other bbs different than DEF_BB in the SCOP. */
1678 build_cross_bb_scalars_def (scop_p scop
, tree def
, basic_block def_bb
,
1682 if (!is_gimple_reg (def
))
1685 /* Do not gather scalar variables that can be analyzed by SCEV as they can be
1686 generated out of the induction variables. */
1687 if (scev_analyzable_p (def
, scop
->scop_info
->region
))
1691 imm_use_iterator imm_iter
;
1692 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
1693 if (def_bb
!= gimple_bb (use_stmt
) && !is_gimple_debug (use_stmt
))
1695 writes
->safe_push (def
);
1696 DEBUG_PRINT (dp
<< "Adding scalar write:\n";
1697 print_generic_expr (dump_file
, def
, 0);
1698 dp
<< "From stmt:\n";
1699 print_gimple_stmt (dump_file
,
1700 SSA_NAME_DEF_STMT (def
), 0, 0));
1701 /* This is required by the FOR_EACH_IMM_USE_STMT when we want to break
1702 before all the uses have been visited. */
1703 BREAK_FROM_IMM_USE_STMT (imm_iter
);
1707 /* Record DEF if it is used in other bbs different than DEF_BB in the SCOP. */
1710 build_cross_bb_scalars_use (scop_p scop
, tree use
, gimple
*use_stmt
,
1711 vec
<scalar_use
> *reads
)
1714 if (!is_gimple_reg (use
))
1717 /* Do not gather scalar variables that can be analyzed by SCEV as they can be
1718 generated out of the induction variables. */
1719 if (scev_analyzable_p (use
, scop
->scop_info
->region
))
1722 gimple
*def_stmt
= SSA_NAME_DEF_STMT (use
);
1723 if (gimple_bb (def_stmt
) != gimple_bb (use_stmt
))
1725 DEBUG_PRINT (dp
<< "\nAdding scalar read:";
1726 print_generic_expr (dump_file
, use
, 0);
1727 dp
<< "\nFrom stmt:";
1728 print_gimple_stmt (dump_file
, use_stmt
, 0, 0));
1729 reads
->safe_push (std::make_pair (use_stmt
, use
));
1733 /* Record all scalar variables that are defined and used in different BBs of the
1737 graphite_find_cross_bb_scalar_vars (scop_p scop
, gimple
*stmt
,
1738 vec
<scalar_use
> *reads
, vec
<tree
> *writes
)
1742 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
1743 def
= gimple_assign_lhs (stmt
);
1744 else if (gimple_code (stmt
) == GIMPLE_CALL
)
1745 def
= gimple_call_lhs (stmt
);
1746 else if (gimple_code (stmt
) == GIMPLE_PHI
)
1747 def
= gimple_phi_result (stmt
);
1752 build_cross_bb_scalars_def (scop
, def
, gimple_bb (stmt
), writes
);
1755 use_operand_p use_p
;
1756 FOR_EACH_PHI_OR_STMT_USE (use_p
, stmt
, iter
, SSA_OP_USE
)
1758 tree use
= USE_FROM_PTR (use_p
);
1759 build_cross_bb_scalars_use (scop
, use
, stmt
, reads
);
1763 /* Generates a polyhedral black box only if the bb contains interesting
1766 static gimple_poly_bb_p
1767 try_generate_gimple_bb (scop_p scop
, basic_block bb
)
1769 vec
<data_reference_p
> drs
= vNULL
;
1770 vec
<tree
> writes
= vNULL
;
1771 vec
<scalar_use
> reads
= vNULL
;
1773 sese_l region
= scop
->scop_info
->region
;
1774 loop_p nest
= outermost_loop_in_sese (region
, bb
);
1776 loop_p loop
= bb
->loop_father
;
1777 if (!loop_in_sese_p (loop
, region
))
1780 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
1783 gimple
*stmt
= gsi_stmt (gsi
);
1784 if (is_gimple_debug (stmt
))
1787 graphite_find_data_references_in_stmt (nest
, loop
, stmt
, &drs
);
1788 graphite_find_cross_bb_scalar_vars (scop
, stmt
, &reads
, &writes
);
1791 for (gphi_iterator psi
= gsi_start_phis (bb
); !gsi_end_p (psi
);
1793 if (!virtual_operand_p (gimple_phi_result (psi
.phi ())))
1794 graphite_find_cross_bb_scalar_vars (scop
, psi
.phi (), &reads
, &writes
);
1796 if (drs
.is_empty () && writes
.is_empty () && reads
.is_empty ())
1799 return new_gimple_poly_bb (bb
, drs
, reads
, writes
);
1802 /* Compute alias-sets for all data references in DRS. */
1805 build_alias_set (scop_p scop
)
1807 int num_vertices
= scop
->drs
.length ();
1808 struct graph
*g
= new_graph (num_vertices
);
1813 FOR_EACH_VEC_ELT (scop
->drs
, i
, dr1
)
1814 for (j
= i
+1; scop
->drs
.iterate (j
, &dr2
); j
++)
1815 if (dr_may_alias_p (dr1
->dr
, dr2
->dr
, true))
1821 all_vertices
= XNEWVEC (int, num_vertices
);
1822 for (i
= 0; i
< num_vertices
; i
++)
1823 all_vertices
[i
] = i
;
1825 graphds_dfs (g
, all_vertices
, num_vertices
, NULL
, true, NULL
);
1826 free (all_vertices
);
1828 for (i
= 0; i
< g
->n_vertices
; i
++)
1829 scop
->drs
[i
].alias_set
= g
->vertices
[i
].component
+ 1;
1834 /* Gather BBs and conditions for a SCOP. */
1835 class gather_bbs
: public dom_walker
1838 gather_bbs (cdi_direction
, scop_p
);
1840 virtual void before_dom_children (basic_block
);
1841 virtual void after_dom_children (basic_block
);
1844 auto_vec
<gimple
*, 3> conditions
, cases
;
1848 gather_bbs::gather_bbs (cdi_direction direction
, scop_p scop
)
1849 : dom_walker (direction
), scop (scop
)
1853 /* Call-back for dom_walk executed before visiting the dominated
1857 gather_bbs::before_dom_children (basic_block bb
)
1859 if (!bb_in_sese_p (bb
, scop
->scop_info
->region
))
1862 gcond
*stmt
= single_pred_cond_non_loop_exit (bb
);
1866 edge e
= single_pred_edge (bb
);
1868 conditions
.safe_push (stmt
);
1870 if (e
->flags
& EDGE_TRUE_VALUE
)
1871 cases
.safe_push (stmt
);
1873 cases
.safe_push (NULL
);
1876 scop
->scop_info
->bbs
.safe_push (bb
);
1878 gimple_poly_bb_p gbb
= try_generate_gimple_bb (scop
, bb
);
1882 GBB_CONDITIONS (gbb
) = conditions
.copy ();
1883 GBB_CONDITION_CASES (gbb
) = cases
.copy ();
1885 poly_bb_p pbb
= new_poly_bb (scop
, gbb
);
1886 scop
->pbbs
.safe_push (pbb
);
1889 data_reference_p dr
;
1890 FOR_EACH_VEC_ELT (gbb
->data_refs
, i
, dr
)
1891 scop
->drs
.safe_push (dr_info (dr
, pbb
));
1894 /* Call-back for dom_walk executed after visiting the dominated
1898 gather_bbs::after_dom_children (basic_block bb
)
1900 if (!bb_in_sese_p (bb
, scop
->scop_info
->region
))
1903 if (single_pred_cond_non_loop_exit (bb
))
1910 /* Find Static Control Parts (SCoP) in the current function and pushes
1914 build_scops (vec
<scop_p
> *scops
)
1917 dp
.set_dump_file (dump_file
);
1919 canonicalize_loop_closed_ssa_form ();
1922 sb
.build_scop_depth (scop_detection::invalid_sese
, current_loops
->tree_root
);
1924 /* Now create scops from the lightweight SESEs. */
1925 vec
<sese_l
> scops_l
= sb
.get_scops ();
1928 FOR_EACH_VEC_ELT (scops_l
, i
, s
)
1930 scop_p scop
= new_scop (s
->entry
, s
->exit
);
1932 /* Record all basic blocks and their conditions in REGION. */
1933 gather_bbs (CDI_DOMINATORS
, scop
).walk (cfun
->cfg
->x_entry_block_ptr
);
1935 build_alias_set (scop
);
1937 /* Do not optimize a scop containing only PBBs that do not belong
1939 if (sb
.nb_pbbs_in_loops (scop
) == 0)
1941 DEBUG_PRINT (dp
<< "[scop-detection-fail] no data references.\n");
1946 unsigned max_arrays
= PARAM_VALUE (PARAM_GRAPHITE_MAX_ARRAYS_PER_SCOP
);
1947 if (scop
->drs
.length () >= max_arrays
)
1949 DEBUG_PRINT (dp
<< "[scop-detection-fail] too many data references: "
1950 << scop
->drs
.length ()
1951 << " is larger than --param graphite-max-arrays-per-scop="
1952 << max_arrays
<< ".\n");
1957 build_sese_loop_nests (scop
->scop_info
);
1959 find_scop_parameters (scop
);
1960 graphite_dim_t max_dim
= PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS
);
1962 if (scop_nb_params (scop
) > max_dim
)
1964 DEBUG_PRINT (dp
<< "[scop-detection-fail] too many parameters: "
1965 << scop_nb_params (scop
)
1966 << " larger than --param graphite-max-nb-scop-params="
1967 << max_dim
<< ".\n");
1972 scops
->safe_push (scop
);
1975 DEBUG_PRINT (dp
<< "number of SCoPs: " << (scops
? scops
->length () : 0););
1978 #endif /* HAVE_isl */