1 /* Detection of Static Control Parts (SCoP) for Graphite.
2 Copyright (C) 2009-2016 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"
60 set_dump_file (FILE *f
)
66 friend debug_printer
&
67 operator<< (debug_printer
&output
, int i
)
69 fprintf (output
.dump_file
, "%d", i
);
72 friend debug_printer
&
73 operator<< (debug_printer
&output
, const char *s
)
75 fprintf (output
.dump_file
, "%s", s
);
80 #define DEBUG_PRINT(args) do \
82 if (dump_file && (dump_flags & TDF_DETAILS)) { args; } \
85 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
86 different colors. If there are not enough colors, paint the
87 remaining SCoPs in gray.
90 - "*" after the node number denotes the entry of a SCoP,
91 - "#" after the node number denotes the exit of a SCoP,
92 - "()" around the node number denotes the entry or the
93 exit nodes of the SCOP. These are not part of SCoP. */
96 dot_all_sese (FILE *file
, vec
<sese_l
>& scops
)
98 /* Disable debugging while printing graph. */
99 int tmp_dump_flags
= dump_flags
;
102 fprintf (file
, "digraph all {\n");
105 FOR_ALL_BB_FN (bb
, cfun
)
107 int part_of_scop
= false;
109 /* Use HTML for every bb label. So we are able to print bbs
110 which are part of two different SCoPs, with two different
111 background colors. */
112 fprintf (file
, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
114 fprintf (file
, "CELLSPACING=\"0\">\n");
116 /* Select color for SCoP. */
119 FOR_EACH_VEC_ELT (scops
, i
, region
)
121 bool sese_in_region
= bb_in_sese_p (bb
, *region
);
122 if (sese_in_region
|| (region
->exit
->dest
== bb
)
123 || (region
->entry
->dest
== bb
))
183 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">",
187 fprintf (file
, " (");
189 if (bb
== region
->entry
->dest
&& bb
== region
->exit
->dest
)
190 fprintf (file
, " %d*# ", bb
->index
);
191 else if (bb
== region
->entry
->dest
)
192 fprintf (file
, " %d* ", bb
->index
);
193 else if (bb
== region
->exit
->dest
)
194 fprintf (file
, " %d# ", bb
->index
);
196 fprintf (file
, " %d ", bb
->index
);
198 fprintf (file
, "{lp_%d}", bb
->loop_father
->num
);
203 fprintf (file
, "</TD></TR>\n");
210 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
211 fprintf (file
, " %d {lp_%d} </TD></TR>\n", bb
->index
,
212 bb
->loop_father
->num
);
214 fprintf (file
, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
217 FOR_ALL_BB_FN (bb
, cfun
)
221 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
222 fprintf (file
, "%d -> %d;\n", bb
->index
, e
->dest
->index
);
225 fputs ("}\n\n", file
);
227 /* Enable debugging again. */
228 dump_flags
= tmp_dump_flags
;
231 /* Display SCoP on stderr. */
234 dot_sese (sese_l
& scop
)
240 scops
.safe_push (scop
);
242 dot_all_sese (stderr
, scops
);
252 dot_all_sese (stderr
, scops
);
256 /* Return true if BB is empty, contains only DEBUG_INSNs. */
259 trivially_empty_bb_p (basic_block bb
)
261 gimple_stmt_iterator gsi
;
263 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
264 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_DEBUG
)
270 /* Returns true when P1 and P2 are close phis with the same
274 same_close_phi_node (gphi
*p1
, gphi
*p2
)
276 return operand_equal_p (gimple_phi_arg_def (p1
, 0),
277 gimple_phi_arg_def (p2
, 0), 0);
280 static void make_close_phi_nodes_unique (basic_block bb
);
282 /* Remove the close phi node at GSI and replace its rhs with the rhs
286 remove_duplicate_close_phi (gphi
*phi
, gphi_iterator
*gsi
)
290 imm_use_iterator imm_iter
;
291 tree res
= gimple_phi_result (phi
);
292 tree def
= gimple_phi_result (gsi
->phi ());
294 gcc_assert (same_close_phi_node (phi
, gsi
->phi ()));
296 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
298 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
299 SET_USE (use_p
, res
);
301 update_stmt (use_stmt
);
303 /* It is possible that we just created a duplicate close-phi
304 for an already-processed containing loop. Check for this
305 case and clean it up. */
306 if (gimple_code (use_stmt
) == GIMPLE_PHI
307 && gimple_phi_num_args (use_stmt
) == 1)
308 make_close_phi_nodes_unique (gimple_bb (use_stmt
));
311 remove_phi_node (gsi
, true);
314 /* Removes all the close phi duplicates from BB. */
317 make_close_phi_nodes_unique (basic_block bb
)
321 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
323 gphi_iterator gsi
= psi
;
324 gphi
*phi
= psi
.phi ();
326 /* At this point, PHI should be a close phi in normal form. */
327 gcc_assert (gimple_phi_num_args (phi
) == 1);
329 /* Iterate over the next phis and remove duplicates. */
331 while (!gsi_end_p (gsi
))
332 if (same_close_phi_node (phi
, gsi
.phi ()))
333 remove_duplicate_close_phi (phi
, &gsi
);
339 /* Return true when NAME is defined in LOOP. */
342 defined_in_loop_p (tree name
, loop_p loop
)
344 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
345 return loop
== loop_containing_stmt (SSA_NAME_DEF_STMT (name
));
348 /* Transforms LOOP to the canonical loop closed SSA form. */
351 canonicalize_loop_closed_ssa (loop_p loop
)
353 edge e
= single_exit (loop
);
356 if (!e
|| e
->flags
& EDGE_ABNORMAL
)
361 if (single_pred_p (bb
))
363 e
= split_block_after_labels (bb
);
364 DEBUG_PRINT (dp
<< "Splitting bb_" << bb
->index
<< ".\n");
365 make_close_phi_nodes_unique (e
->src
);
370 basic_block close
= split_edge (e
);
372 e
= single_succ_edge (close
);
373 DEBUG_PRINT (dp
<< "Splitting edge (" << e
->src
->index
<< ","
374 << e
->dest
->index
<< ")\n");
376 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
378 gphi
*phi
= psi
.phi ();
381 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
382 if (gimple_phi_arg_edge (phi
, i
) == e
)
384 tree res
, arg
= gimple_phi_arg_def (phi
, i
);
388 /* Only add close phi nodes for SSA_NAMEs defined in LOOP. */
389 if (TREE_CODE (arg
) != SSA_NAME
390 || !defined_in_loop_p (arg
, loop
))
393 close_phi
= create_phi_node (NULL_TREE
, close
);
394 res
= create_new_def_for (arg
, close_phi
,
395 gimple_phi_result_ptr (close_phi
));
396 add_phi_arg (close_phi
, arg
,
397 gimple_phi_arg_edge (close_phi
, 0),
399 use_p
= gimple_phi_arg_imm_use_ptr (phi
, i
);
400 replace_exp (use_p
, res
);
405 make_close_phi_nodes_unique (close
);
408 /* The code above does not properly handle changes in the post dominance
409 information (yet). */
410 recompute_all_dominators ();
413 /* Converts the current loop closed SSA form to a canonical form
414 expected by the Graphite code generation.
416 The loop closed SSA form has the following invariant: a variable
417 defined in a loop that is used outside the loop appears only in the
418 phi nodes in the destination of the loop exit. These phi nodes are
419 called close phi nodes.
421 The canonical loop closed SSA form contains the extra invariants:
423 - when the loop contains only one exit, the close phi nodes contain
424 only one argument. That implies that the basic block that contains
425 the close phi nodes has only one predecessor, that is a basic block
428 - the basic block containing the close phi nodes does not contain
431 - there exist only one phi node per definition in the loop.
435 canonicalize_loop_closed_ssa_form (void)
437 checking_verify_loop_closed_ssa (true);
440 FOR_EACH_LOOP (loop
, 0)
441 canonicalize_loop_closed_ssa (loop
);
443 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
444 update_ssa (TODO_update_ssa
);
446 checking_verify_loop_closed_ssa (true);
449 /* Can all ivs be represented by a signed integer?
450 As isl might generate negative values in its expressions, signed loop ivs
451 are required in the backend. */
454 loop_ivs_can_be_represented (loop_p loop
)
456 unsigned type_long_long
= TYPE_PRECISION (long_long_integer_type_node
);
457 for (gphi_iterator psi
= gsi_start_phis (loop
->header
); !gsi_end_p (psi
);
460 gphi
*phi
= psi
.phi ();
461 tree res
= PHI_RESULT (phi
);
462 tree type
= TREE_TYPE (res
);
464 if (TYPE_UNSIGNED (type
) && TYPE_PRECISION (type
) >= type_long_long
)
471 /* Returns a COND_EXPR statement when BB has a single predecessor, the
472 edge between BB and its predecessor is not a loop exit edge, and
473 the last statement of the single predecessor is a COND_EXPR. */
476 single_pred_cond_non_loop_exit (basic_block bb
)
478 if (single_pred_p (bb
))
480 edge e
= single_pred_edge (bb
);
481 basic_block pred
= e
->src
;
484 if (loop_depth (pred
->loop_father
) > loop_depth (bb
->loop_father
))
487 stmt
= last_stmt (pred
);
489 if (stmt
&& gimple_code (stmt
) == GIMPLE_COND
)
490 return as_a
<gcond
*> (stmt
);
499 /* Build the maximal scop containing LOOPs and add it to SCOPS. */
504 scop_detection () : scops (vNULL
) {}
511 /* A marker for invalid sese_l. */
512 static sese_l invalid_sese
;
514 /* Return the SCOPS in this SCOP_DETECTION. */
522 /* Return an sese_l around the LOOP. */
524 sese_l
get_sese (loop_p loop
);
526 /* Return the closest dominator with a single entry edge. In case of a
527 back-loop the back-edge is not counted. */
529 static edge
get_nearest_dom_with_single_entry (basic_block dom
);
531 /* Return the closest post-dominator with a single exit edge. In case of a
532 back-loop the back-edge is not counted. */
534 static edge
get_nearest_pdom_with_single_exit (basic_block dom
);
536 /* Merge scops at same loop depth and returns the new sese.
537 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
539 sese_l
merge_sese (sese_l first
, sese_l second
) const;
541 /* Build scop outer->inner if possible. */
543 sese_l
build_scop_depth (sese_l s
, loop_p loop
);
545 /* If loop and loop->next are valid scops, try to merge them. */
547 sese_l
build_scop_breadth (sese_l s1
, loop_p loop
);
549 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
550 region of code that can be represented in the polyhedral model. SCOP
551 defines the region we analyse. */
553 bool loop_is_valid_in_scop (loop_p loop
, sese_l scop
) const;
555 /* Return true when BEGIN is the preheader edge of a loop with a single exit
558 static bool region_has_one_loop (sese_l s
);
560 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
562 void add_scop (sese_l s
);
564 /* Returns true if S1 subsumes/surrounds S2. */
565 static bool subsumes (sese_l s1
, sese_l s2
);
567 /* Remove a SCoP which is subsumed by S1. */
568 void remove_subscops (sese_l s1
);
570 /* Returns true if S1 intersects with S2. Since we already know that S1 does
571 not subsume S2 or vice-versa, we only check for entry bbs. */
573 static bool intersects (sese_l s1
, sese_l s2
);
575 /* Remove one of the scops when it intersects with any other. */
577 void remove_intersecting_scops (sese_l s1
);
579 /* Return true when the body of LOOP has statements that can be represented
582 bool loop_body_is_valid_scop (loop_p loop
, sese_l scop
) const;
584 /* Return true when BB contains a harmful operation for a scop: that
585 can be a function call with side effects, the induction variables
586 are not linear with respect to SCOP, etc. The current open
587 scop should end before this statement. */
589 bool harmful_stmt_in_bb (sese_l scop
, basic_block bb
) const;
591 /* Return true when a statement in SCOP cannot be represented by Graphite.
592 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
593 Limit the number of bbs between adjacent loops to
594 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
596 bool harmful_loop_in_region (sese_l scop
) const;
598 /* Return true only when STMT is simple enough for being handled by Graphite.
599 This depends on SCOP, as the parameters are initialized relatively to
600 this basic block, the linear functions are initialized based on the
601 outermost loop containing STMT inside the SCOP. BB is the place where we
602 try to evaluate the STMT. */
604 bool stmt_simple_for_scop_p (sese_l scop
, gimple
*stmt
,
605 basic_block bb
) const;
607 /* Something like "n * m" is not allowed. */
609 static bool graphite_can_represent_init (tree e
);
611 /* Return true when SCEV can be represented in the polyhedral model.
613 An expression can be represented, if it can be expressed as an
614 affine expression. For loops (i, j) and parameters (m, n) all
615 affine expressions are of the form:
617 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
619 1 i + 20 j + (-2) m + 25
621 Something like "i * n" or "n * m" is not allowed. */
623 static bool graphite_can_represent_scev (tree scev
);
625 /* Return true when EXPR can be represented in the polyhedral model.
627 This means an expression can be represented, if it is linear with respect
628 to the loops and the strides are non parametric. LOOP is the place where
629 the expr will be evaluated. SCOP defines the region we analyse. */
631 static bool graphite_can_represent_expr (sese_l scop
, loop_p loop
,
634 /* Return true if the data references of STMT can be represented by Graphite.
635 We try to analyze the data references in a loop contained in the SCOP. */
637 static bool stmt_has_simple_data_refs_p (sese_l scop
, gimple
*stmt
);
639 /* Remove the close phi node at GSI and replace its rhs with the rhs
642 static void remove_duplicate_close_phi (gphi
*phi
, gphi_iterator
*gsi
);
644 /* Returns true when Graphite can represent LOOP in SCOP.
645 FIXME: For the moment, graphite cannot be used on loops that iterate using
646 induction variables that wrap. */
648 static bool can_represent_loop_1 (loop_p loop
, sese_l scop
);
650 /* Return true when all the loops within LOOP can be represented by
653 static bool can_represent_loop (loop_p loop
, sese_l scop
);
655 /* Returns the number of pbbs that are in loops contained in SCOP. */
657 static int nb_pbbs_in_loops (scop_p scop
);
659 static bool graphite_can_represent_stmt (sese_l
, gimple
*, basic_block
);
665 sese_l
scop_detection::invalid_sese (NULL
, NULL
);
667 /* Return an sese_l around the LOOP. */
670 scop_detection::get_sese (loop_p loop
)
675 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS
))
677 edge scop_end
= single_exit (loop
);
680 edge scop_begin
= loop_preheader_edge (loop
);
681 sese_l
s (scop_begin
, scop_end
);
685 /* Return the closest dominator with a single entry edge. */
688 scop_detection::get_nearest_dom_with_single_entry (basic_block dom
)
693 /* If any of the dominators has two predecessors but one of them is a back
694 edge, then that basic block also qualifies as a dominator with single
696 if (dom
->preds
->length () == 2)
698 /* If e1->src dominates e2->src then e1->src will also dominate dom. */
699 edge e1
= (*dom
->preds
)[0];
700 edge e2
= (*dom
->preds
)[1];
701 loop_p l
= dom
->loop_father
;
702 loop_p l1
= e1
->src
->loop_father
;
703 loop_p l2
= e2
->src
->loop_father
;
704 if (l
!= l1
&& l
== l2
705 && dominated_by_p (CDI_DOMINATORS
, e2
->src
, e1
->src
))
707 if (l
!= l2
&& l
== l1
708 && dominated_by_p (CDI_DOMINATORS
, e1
->src
, e2
->src
))
712 while (dom
->preds
->length () != 1)
714 if (dom
->preds
->length () < 1)
716 dom
= get_immediate_dominator (CDI_DOMINATORS
, dom
);
720 return (*dom
->preds
)[0];
723 /* Return the closest post-dominator with a single exit edge. In case of a
724 back-loop the back-edge is not counted. */
727 scop_detection::get_nearest_pdom_with_single_exit (basic_block pdom
)
732 /* If any of the post-dominators has two successors but one of them is a back
733 edge, then that basic block also qualifies as a post-dominator with single
735 if (pdom
->succs
->length () == 2)
737 /* If e1->dest post-dominates e2->dest then e1->dest will also
738 post-dominate pdom. */
739 edge e1
= (*pdom
->succs
)[0];
740 edge e2
= (*pdom
->succs
)[1];
741 loop_p l
= pdom
->loop_father
;
742 loop_p l1
= e1
->dest
->loop_father
;
743 loop_p l2
= e2
->dest
->loop_father
;
744 if (l
!= l1
&& l
== l2
745 && dominated_by_p (CDI_POST_DOMINATORS
, e2
->dest
, e1
->dest
))
747 if (l
!= l2
&& l
== l1
748 && dominated_by_p (CDI_POST_DOMINATORS
, e1
->dest
, e2
->dest
))
752 while (pdom
->succs
->length () != 1)
754 if (pdom
->succs
->length () < 1)
756 pdom
= get_immediate_dominator (CDI_POST_DOMINATORS
, pdom
);
761 return (*pdom
->succs
)[0];
764 /* Merge scops at same loop depth and returns the new sese.
765 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
768 scop_detection::merge_sese (sese_l first
, sese_l second
) const
770 /* In the trivial case first/second may be NULL. */
776 DEBUG_PRINT (dp
<< "[scop-detection] try merging sese s1: ";
777 print_sese (dump_file
, first
);
778 dp
<< "[scop-detection] try merging sese s2: ";
779 print_sese (dump_file
, second
));
781 /* Assumption: Both the sese's should be at the same loop depth or one scop
782 should subsume the other like in case of nested loops. */
784 /* Find the common dominators for entry,
785 and common post-dominators for the exit. */
786 basic_block dom
= nearest_common_dominator (CDI_DOMINATORS
,
787 get_entry_bb (first
),
788 get_entry_bb (second
));
790 edge entry
= get_nearest_dom_with_single_entry (dom
);
792 if (!entry
|| (entry
->flags
& EDGE_IRREDUCIBLE_LOOP
))
795 basic_block pdom
= nearest_common_dominator (CDI_POST_DOMINATORS
,
797 get_exit_bb (second
));
798 pdom
= nearest_common_dominator (CDI_POST_DOMINATORS
, dom
, pdom
);
800 edge exit
= get_nearest_pdom_with_single_exit (pdom
);
802 if (!exit
|| (exit
->flags
& EDGE_IRREDUCIBLE_LOOP
))
805 sese_l
combined (entry
, exit
);
807 DEBUG_PRINT (dp
<< "[scop-detection] checking combined sese: ";
808 print_sese (dump_file
, combined
));
810 /* FIXME: We could iterate to find the dom which dominates pdom, and pdom
811 which post-dominates dom, until it stabilizes. Also, ENTRY->SRC and
812 EXIT->DEST should be in the same loop nest. */
813 if (!dominated_by_p (CDI_DOMINATORS
, pdom
, dom
)
814 || loop_depth (entry
->src
->loop_father
)
815 != loop_depth (exit
->dest
->loop_father
))
818 /* For now we just want to bail out when exit does not post-dominate entry.
819 TODO: We might just add a basic_block at the exit to make exit
820 post-dominate entry (the entire region). */
821 if (!dominated_by_p (CDI_POST_DOMINATORS
, get_entry_bb (combined
),
822 get_exit_bb (combined
))
823 || !dominated_by_p (CDI_DOMINATORS
, get_exit_bb (combined
),
824 get_entry_bb (combined
)))
826 DEBUG_PRINT (dp
<< "[scop-detection-fail] cannot merge seses.\n");
830 /* FIXME: We should remove this piece of code once
831 canonicalize_loop_closed_ssa has been removed, because that function
832 adds a BB with single exit. */
833 if (!trivially_empty_bb_p (get_exit_bb (combined
)))
835 /* Find the first empty succ (with single exit) of combined.exit. */
836 basic_block imm_succ
= combined
.exit
->dest
;
837 if (single_succ_p (imm_succ
) && trivially_empty_bb_p (imm_succ
))
838 combined
.exit
= single_succ_edge (imm_succ
);
841 DEBUG_PRINT (dp
<< "[scop-detection-fail] Discarding SCoP because "
842 << "no single exit (empty succ) for sese exit";
843 print_sese (dump_file
, combined
));
848 /* Analyze all the BBs in new sese. */
849 if (harmful_loop_in_region (combined
))
852 DEBUG_PRINT (dp
<< "[merged-sese] s1: "; print_sese (dump_file
, combined
));
857 /* Build scop outer->inner if possible. */
860 scop_detection::build_scop_depth (sese_l s
, loop_p loop
)
865 DEBUG_PRINT (dp
<< "[Depth loop_" << loop
->num
<< "]\n");
866 s
= build_scop_depth (s
, loop
->inner
);
868 sese_l s2
= merge_sese (s
, get_sese (loop
));
871 /* s might be a valid scop, so return it and start analyzing from the
873 build_scop_depth (invalid_sese
, loop
->next
);
877 if (!loop_is_valid_in_scop (loop
, s2
))
878 return build_scop_depth (invalid_sese
, loop
->next
);
880 return build_scop_breadth (s2
, loop
);
883 /* If loop and loop->next are valid scops, try to merge them. */
886 scop_detection::build_scop_breadth (sese_l s1
, loop_p loop
)
890 DEBUG_PRINT (dp
<< "[Breadth loop_" << loop
->num
<< "]\n");
894 sese_l s2
= build_scop_depth (invalid_sese
, l
->next
);
902 sese_l combined
= merge_sese (s1
, s2
);
914 /* Returns true when Graphite can represent LOOP in SCOP.
915 FIXME: For the moment, graphite cannot be used on loops that iterate using
916 induction variables that wrap. */
919 scop_detection::can_represent_loop_1 (loop_p loop
, sese_l scop
)
922 struct tree_niter_desc niter_desc
;
924 return single_exit (loop
)
925 && !(loop_preheader_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
)
926 && number_of_iterations_exit (loop
, single_exit (loop
), &niter_desc
, false)
927 && niter_desc
.control
.no_overflow
928 && (niter
= number_of_latch_executions (loop
))
929 && !chrec_contains_undetermined (niter
)
930 && graphite_can_represent_expr (scop
, loop
, niter
);
933 /* Return true when all the loops within LOOP can be represented by
937 scop_detection::can_represent_loop (loop_p loop
, sese_l scop
)
939 if (!can_represent_loop_1 (loop
, scop
))
941 if (loop
->inner
&& !can_represent_loop (loop
->inner
, scop
))
943 if (loop
->next
&& !can_represent_loop (loop
->next
, scop
))
949 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
950 region of code that can be represented in the polyhedral model. SCOP
951 defines the region we analyse. */
954 scop_detection::loop_is_valid_in_scop (loop_p loop
, sese_l scop
) const
959 if (!optimize_loop_nest_for_speed_p (loop
))
961 DEBUG_PRINT (dp
<< "[scop-detection-fail] loop_"
962 << loop
->num
<< " is not on a hot path.\n");
966 if (!can_represent_loop (loop
, scop
))
968 DEBUG_PRINT (dp
<< "[scop-detection-fail] cannot represent loop_"
969 << loop
->num
<< "\n");
973 if (loop_body_is_valid_scop (loop
, scop
))
975 DEBUG_PRINT (dp
<< "[valid-scop] loop_" << loop
->num
976 << " is a valid scop.\n");
982 /* Return true when BEGIN is the preheader edge of a loop with a single exit
986 scop_detection::region_has_one_loop (sese_l s
)
988 edge begin
= s
.entry
;
990 /* Check for a single perfectly nested loop. */
991 if (begin
->dest
->loop_father
->inner
)
994 /* Otherwise, check whether we have adjacent loops. */
995 return begin
->dest
->loop_father
== end
->src
->loop_father
;
998 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
1001 scop_detection::add_scop (sese_l s
)
1005 /* Do not add scops with only one loop. */
1006 if (region_has_one_loop (s
))
1008 DEBUG_PRINT (dp
<< "[scop-detection-fail] Discarding one loop SCoP: ";
1009 print_sese (dump_file
, s
));
1013 if (get_exit_bb (s
) == EXIT_BLOCK_PTR_FOR_FN (cfun
))
1015 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1016 << "Discarding SCoP exiting to return: ";
1017 print_sese (dump_file
, s
));
1021 /* Remove all the scops which are subsumed by s. */
1022 remove_subscops (s
);
1024 /* Remove intersecting scops. FIXME: It will be a good idea to keep
1025 the non-intersecting part of the scop already in the list. */
1026 remove_intersecting_scops (s
);
1028 scops
.safe_push (s
);
1029 DEBUG_PRINT (dp
<< "[scop-detection] Adding SCoP: "; print_sese (dump_file
, s
));
1032 /* Return true when a statement in SCOP cannot be represented by Graphite.
1033 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
1034 Limit the number of bbs between adjacent loops to
1035 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
1038 scop_detection::harmful_loop_in_region (sese_l scop
) const
1040 basic_block exit_bb
= get_exit_bb (scop
);
1041 basic_block entry_bb
= get_entry_bb (scop
);
1043 DEBUG_PRINT (dp
<< "[checking-harmful-bbs] ";
1044 print_sese (dump_file
, scop
));
1045 gcc_assert (dominated_by_p (CDI_DOMINATORS
, exit_bb
, entry_bb
));
1047 int depth
= bb_dom_dfs_in (CDI_DOMINATORS
, exit_bb
)
1048 - bb_dom_dfs_in (CDI_DOMINATORS
, entry_bb
);
1050 gcc_assert (depth
> 0);
1052 vec
<basic_block
> dom
1053 = get_dominated_to_depth (CDI_DOMINATORS
, entry_bb
, depth
);
1056 bitmap loops
= BITMAP_ALLOC (NULL
);
1057 FOR_EACH_VEC_ELT (dom
, i
, bb
)
1059 DEBUG_PRINT (dp
<< "Visiting bb_" << bb
->index
<< "\n");
1061 /* We don't want to analyze any bb outside sese. */
1062 if (!dominated_by_p (CDI_POST_DOMINATORS
, bb
, exit_bb
))
1065 /* Basic blocks dominated by the scop->exit are not in the scop. */
1066 if (bb
!= exit_bb
&& dominated_by_p (CDI_DOMINATORS
, bb
, exit_bb
))
1069 /* The basic block should not be part of an irreducible loop. */
1070 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1073 BITMAP_FREE (loops
);
1077 /* Check for unstructured control flow: CFG not generated by structured
1079 if (bb
->succs
->length () > 1)
1083 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1084 if (!dominated_by_p (CDI_POST_DOMINATORS
, bb
, e
->dest
)
1085 && !dominated_by_p (CDI_DOMINATORS
, e
->dest
, bb
))
1089 /* Collect all loops in the current region. */
1090 loop_p loop
= bb
->loop_father
;
1091 if (loop_in_sese_p (loop
, scop
))
1092 bitmap_set_bit (loops
, loop
->num
);
1095 /* We only check for harmful statements in basic blocks not part of
1096 any loop fully contained in the scop: other bbs are checked below
1097 in loop_is_valid_in_scop. */
1098 if (harmful_stmt_in_bb (scop
, bb
))
1101 BITMAP_FREE (loops
);
1108 /* Go through all loops and check that they are still valid in the combined
1112 EXECUTE_IF_SET_IN_BITMAP (loops
, 0, j
, bi
)
1114 loop_p loop
= (*current_loops
->larray
)[j
];
1115 gcc_assert (loop
->num
== (int) j
);
1117 if (!loop_is_valid_in_scop (loop
, scop
))
1120 BITMAP_FREE (loops
);
1126 BITMAP_FREE (loops
);
1130 /* Returns true if S1 subsumes/surrounds S2. */
1132 scop_detection::subsumes (sese_l s1
, sese_l s2
)
1134 if (dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
),
1136 && dominated_by_p (CDI_POST_DOMINATORS
, s2
.exit
->dest
,
1142 /* Remove a SCoP which is subsumed by S1. */
1144 scop_detection::remove_subscops (sese_l s1
)
1148 FOR_EACH_VEC_ELT_REVERSE (scops
, j
, s2
)
1150 if (subsumes (s1
, *s2
))
1152 DEBUG_PRINT (dp
<< "Removing sub-SCoP";
1153 print_sese (dump_file
, *s2
));
1154 scops
.unordered_remove (j
);
1159 /* Returns true if S1 intersects with S2. Since we already know that S1 does
1160 not subsume S2 or vice-versa, we only check for entry bbs. */
1163 scop_detection::intersects (sese_l s1
, sese_l s2
)
1165 if (dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
),
1167 && !dominated_by_p (CDI_DOMINATORS
, get_entry_bb (s2
),
1170 if ((s1
.exit
== s2
.entry
) || (s2
.exit
== s1
.entry
))
1176 /* Remove one of the scops when it intersects with any other. */
1179 scop_detection::remove_intersecting_scops (sese_l s1
)
1183 FOR_EACH_VEC_ELT_REVERSE (scops
, j
, s2
)
1185 if (intersects (s1
, *s2
))
1187 DEBUG_PRINT (dp
<< "Removing intersecting SCoP";
1188 print_sese (dump_file
, *s2
);
1189 dp
<< "Intersects with:";
1190 print_sese (dump_file
, s1
));
1191 scops
.unordered_remove (j
);
1196 /* Something like "n * m" is not allowed. */
1199 scop_detection::graphite_can_represent_init (tree e
)
1201 switch (TREE_CODE (e
))
1203 case POLYNOMIAL_CHREC
:
1204 return graphite_can_represent_init (CHREC_LEFT (e
))
1205 && graphite_can_represent_init (CHREC_RIGHT (e
));
1208 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
1209 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
1210 && tree_fits_shwi_p (TREE_OPERAND (e
, 1));
1212 return graphite_can_represent_init (TREE_OPERAND (e
, 1))
1213 && tree_fits_shwi_p (TREE_OPERAND (e
, 0));
1216 case POINTER_PLUS_EXPR
:
1218 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
1219 && graphite_can_represent_init (TREE_OPERAND (e
, 1));
1224 case NON_LVALUE_EXPR
:
1225 return graphite_can_represent_init (TREE_OPERAND (e
, 0));
1234 /* Return true when SCEV can be represented in the polyhedral model.
1236 An expression can be represented, if it can be expressed as an
1237 affine expression. For loops (i, j) and parameters (m, n) all
1238 affine expressions are of the form:
1240 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
1242 1 i + 20 j + (-2) m + 25
1244 Something like "i * n" or "n * m" is not allowed. */
1247 scop_detection::graphite_can_represent_scev (tree scev
)
1249 if (chrec_contains_undetermined (scev
))
1252 /* We disable the handling of pointer types, because it’s currently not
1253 supported by Graphite with the isl AST generator. SSA_NAME nodes are
1254 the only nodes, which are disabled in case they are pointers to object
1255 types, but this can be changed. */
1257 if (POINTER_TYPE_P (TREE_TYPE (scev
)) && TREE_CODE (scev
) == SSA_NAME
)
1260 switch (TREE_CODE (scev
))
1265 case NON_LVALUE_EXPR
:
1266 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0));
1269 case POINTER_PLUS_EXPR
:
1271 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
1272 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
1275 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 0)))
1276 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 1)))
1277 && !(chrec_contains_symbols (TREE_OPERAND (scev
, 0))
1278 && chrec_contains_symbols (TREE_OPERAND (scev
, 1)))
1279 && graphite_can_represent_init (scev
)
1280 && graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
1281 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
1283 case POLYNOMIAL_CHREC
:
1284 /* Check for constant strides. With a non constant stride of
1285 'n' we would have a value of 'iv * n'. Also check that the
1286 initial value can represented: for example 'n * m' cannot be
1288 if (!evolution_function_right_is_integer_cst (scev
)
1289 || !graphite_can_represent_init (scev
))
1291 return graphite_can_represent_scev (CHREC_LEFT (scev
));
1297 /* Only affine functions can be represented. */
1298 if (tree_contains_chrecs (scev
, NULL
) || !scev_is_linear_expression (scev
))
1304 /* Return true when EXPR can be represented in the polyhedral model.
1306 This means an expression can be represented, if it is linear with respect to
1307 the loops and the strides are non parametric. LOOP is the place where the
1308 expr will be evaluated. SCOP defines the region we analyse. */
1311 scop_detection::graphite_can_represent_expr (sese_l scop
, loop_p loop
,
1314 tree scev
= scalar_evolution_in_region (scop
, loop
, expr
);
1315 return graphite_can_represent_scev (scev
);
1318 /* Return true if the data references of STMT can be represented by Graphite.
1319 We try to analyze the data references in a loop contained in the SCOP. */
1322 scop_detection::stmt_has_simple_data_refs_p (sese_l scop
, gimple
*stmt
)
1324 loop_p nest
= outermost_loop_in_sese (scop
, gimple_bb (stmt
));
1325 loop_p loop
= loop_containing_stmt (stmt
);
1326 vec
<data_reference_p
> drs
= vNULL
;
1328 graphite_find_data_references_in_stmt (nest
, loop
, stmt
, &drs
);
1331 data_reference_p dr
;
1332 FOR_EACH_VEC_ELT (drs
, j
, dr
)
1334 int nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
1336 if (nb_subscripts
< 1)
1338 free_data_refs (drs
);
1342 tree ref
= DR_REF (dr
);
1344 for (int i
= nb_subscripts
- 1; i
>= 0; i
--)
1346 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr
, i
))
1347 || (TREE_CODE (ref
) != ARRAY_REF
&& TREE_CODE (ref
) != MEM_REF
1348 && TREE_CODE (ref
) != COMPONENT_REF
))
1350 free_data_refs (drs
);
1354 ref
= TREE_OPERAND (ref
, 0);
1358 free_data_refs (drs
);
1362 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
1363 Calls have side-effects, except those to const or pure
1367 stmt_has_side_effects (gimple
*stmt
)
1369 if (gimple_has_volatile_ops (stmt
)
1370 || (gimple_code (stmt
) == GIMPLE_CALL
1371 && !(gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
)))
1372 || (gimple_code (stmt
) == GIMPLE_ASM
))
1374 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1375 << "Statement has side-effects:\n";
1376 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
| TDF_MEMSYMS
));
1382 /* Returns true if STMT can be represented in polyhedral model. LABEL,
1383 simple COND stmts, pure calls, and assignments can be repesented. */
1386 scop_detection::graphite_can_represent_stmt (sese_l scop
, gimple
*stmt
,
1389 loop_p loop
= bb
->loop_father
;
1390 switch (gimple_code (stmt
))
1397 /* We can handle all binary comparisons. Inequalities are
1398 also supported as they can be represented with union of
1400 enum tree_code code
= gimple_cond_code (stmt
);
1401 if (!(code
== LT_EXPR
1406 || code
== NE_EXPR
))
1408 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1409 << "Graphite cannot handle cond stmt:\n";
1410 print_gimple_stmt (dump_file
, stmt
, 0,
1411 TDF_VOPS
| TDF_MEMSYMS
));
1415 for (unsigned i
= 0; i
< 2; ++i
)
1417 tree op
= gimple_op (stmt
, i
);
1418 if (!graphite_can_represent_expr (scop
, loop
, op
)
1419 /* We can only constrain on integer type. */
1420 || (TREE_CODE (TREE_TYPE (op
)) != INTEGER_TYPE
))
1422 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1423 << "Graphite cannot represent stmt:\n";
1424 print_gimple_stmt (dump_file
, stmt
, 0,
1425 TDF_VOPS
| TDF_MEMSYMS
));
1438 /* These nodes cut a new scope. */
1440 dp
<< "[scop-detection-fail] "
1441 << "Gimple stmt not handled in Graphite:\n";
1442 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
| TDF_MEMSYMS
));
1447 /* Return true only when STMT is simple enough for being handled by Graphite.
1448 This depends on SCOP, as the parameters are initialized relatively to
1449 this basic block, the linear functions are initialized based on the outermost
1450 loop containing STMT inside the SCOP. BB is the place where we try to
1451 evaluate the STMT. */
1454 scop_detection::stmt_simple_for_scop_p (sese_l scop
, gimple
*stmt
,
1455 basic_block bb
) const
1459 if (is_gimple_debug (stmt
))
1462 if (stmt_has_side_effects (stmt
))
1465 if (!stmt_has_simple_data_refs_p (scop
, stmt
))
1467 DEBUG_PRINT (dp
<< "[scop-detection-fail] "
1468 << "Graphite cannot handle data-refs in stmt:\n";
1469 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
););
1473 return graphite_can_represent_stmt (scop
, stmt
, bb
);
1476 /* Return true when BB contains a harmful operation for a scop: that
1477 can be a function call with side effects, the induction variables
1478 are not linear with respect to SCOP, etc. The current open
1479 scop should end before this statement. */
1482 scop_detection::harmful_stmt_in_bb (sese_l scop
, basic_block bb
) const
1484 gimple_stmt_iterator gsi
;
1486 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1487 if (!stmt_simple_for_scop_p (scop
, gsi_stmt (gsi
), bb
))
1493 /* Return true when the body of LOOP has statements that can be represented as a
1497 scop_detection::loop_body_is_valid_scop (loop_p loop
, sese_l scop
) const
1499 if (!loop_ivs_can_be_represented (loop
))
1501 DEBUG_PRINT (dp
<< "[scop-detection-fail] loop_" << loop
->num
1502 << "IV cannot be represented.\n");
1506 if (!loop_nest_has_data_refs (loop
))
1508 DEBUG_PRINT (dp
<< "[scop-detection-fail] loop_" << loop
->num
1509 << "does not have any data reference.\n");
1513 basic_block
*bbs
= get_loop_body (loop
);
1514 for (unsigned i
= 0; i
< loop
->num_nodes
; i
++)
1516 basic_block bb
= bbs
[i
];
1518 if (harmful_stmt_in_bb (scop
, bb
))
1531 if (!loop_body_is_valid_scop (loop
, scop
))
1540 /* Returns the number of pbbs that are in loops contained in SCOP. */
1543 scop_detection::nb_pbbs_in_loops (scop_p scop
)
1549 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
1550 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb
)), scop
->scop_info
->region
))
1556 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
1557 Otherwise returns -1. */
1560 parameter_index_in_region_1 (tree name
, sese_info_p region
)
1565 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
1567 FOR_EACH_VEC_ELT (region
->params
, i
, p
)
1574 /* When the parameter NAME is in REGION, returns its index in
1575 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
1576 and returns the index of NAME. */
1579 parameter_index_in_region (tree name
, sese_info_p region
)
1583 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
1585 /* Cannot constrain on anything else than INTEGER_TYPE parameters. */
1586 if (TREE_CODE (TREE_TYPE (name
)) != INTEGER_TYPE
)
1589 if (!invariant_in_sese_p_rec (name
, region
->region
, NULL
))
1592 i
= parameter_index_in_region_1 (name
, region
);
1596 i
= region
->params
.length ();
1597 region
->params
.safe_push (name
);
1601 /* In the context of sese S, scan the expression E and translate it to
1602 a linear expression C. When parsing a symbolic multiplication, K
1603 represents the constant multiplier of an expression containing
1607 scan_tree_for_params (sese_info_p s
, tree e
)
1609 if (e
== chrec_dont_know
)
1612 switch (TREE_CODE (e
))
1614 case POLYNOMIAL_CHREC
:
1615 scan_tree_for_params (s
, CHREC_LEFT (e
));
1619 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
1620 scan_tree_for_params (s
, TREE_OPERAND (e
, 0));
1622 scan_tree_for_params (s
, TREE_OPERAND (e
, 1));
1626 case POINTER_PLUS_EXPR
:
1628 scan_tree_for_params (s
, TREE_OPERAND (e
, 0));
1629 scan_tree_for_params (s
, TREE_OPERAND (e
, 1));
1635 case NON_LVALUE_EXPR
:
1636 scan_tree_for_params (s
, TREE_OPERAND (e
, 0));
1640 parameter_index_in_region (e
, s
);
1656 /* Find parameters with respect to REGION in BB. We are looking in memory
1657 access functions, conditions and loop bounds. */
1660 find_params_in_bb (sese_info_p region
, gimple_poly_bb_p gbb
)
1662 /* Find parameters in the access functions of data references. */
1664 data_reference_p dr
;
1665 FOR_EACH_VEC_ELT (GBB_DATA_REFS (gbb
), i
, dr
)
1666 for (unsigned j
= 0; j
< DR_NUM_DIMENSIONS (dr
); j
++)
1667 scan_tree_for_params (region
, DR_ACCESS_FN (dr
, j
));
1669 /* Find parameters in conditional statements. */
1671 loop_p loop
= GBB_BB (gbb
)->loop_father
;
1672 FOR_EACH_VEC_ELT (GBB_CONDITIONS (gbb
), i
, stmt
)
1674 tree lhs
= scalar_evolution_in_region (region
->region
, loop
,
1675 gimple_cond_lhs (stmt
));
1676 tree rhs
= scalar_evolution_in_region (region
->region
, loop
,
1677 gimple_cond_rhs (stmt
));
1679 scan_tree_for_params (region
, lhs
);
1680 scan_tree_for_params (region
, rhs
);
1684 /* Record the parameters used in the SCOP. A variable is a parameter
1685 in a scop if it does not vary during the execution of that scop. */
1688 find_scop_parameters (scop_p scop
)
1691 sese_info_p region
= scop
->scop_info
;
1694 /* Find the parameters used in the loop bounds. */
1695 FOR_EACH_VEC_ELT (region
->loop_nest
, i
, loop
)
1697 tree nb_iters
= number_of_latch_executions (loop
);
1699 if (!chrec_contains_symbols (nb_iters
))
1702 nb_iters
= scalar_evolution_in_region (region
->region
, loop
, nb_iters
);
1703 scan_tree_for_params (region
, nb_iters
);
1706 /* Find the parameters used in data accesses. */
1708 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
1709 find_params_in_bb (region
, PBB_BLACK_BOX (pbb
));
1711 int nbp
= sese_nb_params (region
);
1712 scop_set_nb_params (scop
, nbp
);
1715 /* Record DEF if it is used in other bbs different than DEF_BB in the SCOP. */
1718 build_cross_bb_scalars_def (scop_p scop
, tree def
, basic_block def_bb
,
1722 if (!is_gimple_reg (def
))
1725 /* Do not gather scalar variables that can be analyzed by SCEV as they can be
1726 generated out of the induction variables. */
1727 if (scev_analyzable_p (def
, scop
->scop_info
->region
))
1731 imm_use_iterator imm_iter
;
1732 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
1733 if (def_bb
!= gimple_bb (use_stmt
) && !is_gimple_debug (use_stmt
))
1735 writes
->safe_push (def
);
1736 DEBUG_PRINT (dp
<< "Adding scalar write: ";
1737 print_generic_expr (dump_file
, def
, 0);
1738 dp
<< "\nFrom stmt: ";
1739 print_gimple_stmt (dump_file
,
1740 SSA_NAME_DEF_STMT (def
), 0, 0));
1741 /* This is required by the FOR_EACH_IMM_USE_STMT when we want to break
1742 before all the uses have been visited. */
1743 BREAK_FROM_IMM_USE_STMT (imm_iter
);
1747 /* Record DEF if it is used in other bbs different than DEF_BB in the SCOP. */
1750 build_cross_bb_scalars_use (scop_p scop
, tree use
, gimple
*use_stmt
,
1751 vec
<scalar_use
> *reads
)
1754 if (!is_gimple_reg (use
))
1757 /* Do not gather scalar variables that can be analyzed by SCEV as they can be
1758 generated out of the induction variables. */
1759 if (scev_analyzable_p (use
, scop
->scop_info
->region
))
1762 gimple
*def_stmt
= SSA_NAME_DEF_STMT (use
);
1763 if (gimple_bb (def_stmt
) != gimple_bb (use_stmt
))
1765 DEBUG_PRINT (dp
<< "Adding scalar read: ";
1766 print_generic_expr (dump_file
, use
, 0);
1767 dp
<< "\nFrom stmt: ";
1768 print_gimple_stmt (dump_file
, use_stmt
, 0, 0));
1769 reads
->safe_push (std::make_pair (use_stmt
, use
));
1773 /* Record all scalar variables that are defined and used in different BBs of the
1777 graphite_find_cross_bb_scalar_vars (scop_p scop
, gimple
*stmt
,
1778 vec
<scalar_use
> *reads
, vec
<tree
> *writes
)
1782 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
1783 def
= gimple_assign_lhs (stmt
);
1784 else if (gimple_code (stmt
) == GIMPLE_CALL
)
1785 def
= gimple_call_lhs (stmt
);
1786 else if (gimple_code (stmt
) == GIMPLE_PHI
)
1787 def
= gimple_phi_result (stmt
);
1792 build_cross_bb_scalars_def (scop
, def
, gimple_bb (stmt
), writes
);
1795 use_operand_p use_p
;
1796 FOR_EACH_PHI_OR_STMT_USE (use_p
, stmt
, iter
, SSA_OP_USE
)
1798 tree use
= USE_FROM_PTR (use_p
);
1799 build_cross_bb_scalars_use (scop
, use
, stmt
, reads
);
1803 /* Generates a polyhedral black box only if the bb contains interesting
1806 static gimple_poly_bb_p
1807 try_generate_gimple_bb (scop_p scop
, basic_block bb
)
1809 vec
<data_reference_p
> drs
= vNULL
;
1810 vec
<tree
> writes
= vNULL
;
1811 vec
<scalar_use
> reads
= vNULL
;
1813 sese_l region
= scop
->scop_info
->region
;
1814 loop_p nest
= outermost_loop_in_sese (region
, bb
);
1816 loop_p loop
= bb
->loop_father
;
1817 if (!loop_in_sese_p (loop
, region
))
1820 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
1823 gimple
*stmt
= gsi_stmt (gsi
);
1824 if (is_gimple_debug (stmt
))
1827 graphite_find_data_references_in_stmt (nest
, loop
, stmt
, &drs
);
1828 graphite_find_cross_bb_scalar_vars (scop
, stmt
, &reads
, &writes
);
1831 for (gphi_iterator psi
= gsi_start_phis (bb
); !gsi_end_p (psi
);
1833 if (!virtual_operand_p (gimple_phi_result (psi
.phi ())))
1834 graphite_find_cross_bb_scalar_vars (scop
, psi
.phi (), &reads
, &writes
);
1836 if (drs
.is_empty () && writes
.is_empty () && reads
.is_empty ())
1839 return new_gimple_poly_bb (bb
, drs
, reads
, writes
);
1842 /* Compute alias-sets for all data references in DRS. */
1845 build_alias_set (scop_p scop
)
1847 int num_vertices
= scop
->drs
.length ();
1848 struct graph
*g
= new_graph (num_vertices
);
1853 FOR_EACH_VEC_ELT (scop
->drs
, i
, dr1
)
1854 for (j
= i
+1; scop
->drs
.iterate (j
, &dr2
); j
++)
1855 if (dr_may_alias_p (dr1
->dr
, dr2
->dr
, true))
1861 all_vertices
= XNEWVEC (int, num_vertices
);
1862 for (i
= 0; i
< num_vertices
; i
++)
1863 all_vertices
[i
] = i
;
1865 graphds_dfs (g
, all_vertices
, num_vertices
, NULL
, true, NULL
);
1866 free (all_vertices
);
1868 for (i
= 0; i
< g
->n_vertices
; i
++)
1869 scop
->drs
[i
].alias_set
= g
->vertices
[i
].component
+ 1;
1874 /* Gather BBs and conditions for a SCOP. */
1875 class gather_bbs
: public dom_walker
1878 gather_bbs (cdi_direction
, scop_p
);
1880 virtual edge
before_dom_children (basic_block
);
1881 virtual void after_dom_children (basic_block
);
1884 auto_vec
<gimple
*, 3> conditions
, cases
;
1888 gather_bbs::gather_bbs (cdi_direction direction
, scop_p scop
)
1889 : dom_walker (direction
), scop (scop
)
1893 /* Record in execution order the loops fully contained in the region. */
1896 record_loop_in_sese (basic_block bb
, sese_info_p region
)
1898 loop_p father
= bb
->loop_father
;
1899 if (loop_in_sese_p (father
, region
->region
))
1904 FOR_EACH_VEC_ELT (region
->loop_nest
, j
, loop0
)
1905 if (father
== loop0
)
1911 region
->loop_nest
.safe_push (father
);
1915 /* Call-back for dom_walk executed before visiting the dominated
1919 gather_bbs::before_dom_children (basic_block bb
)
1921 sese_info_p region
= scop
->scop_info
;
1922 if (!bb_in_sese_p (bb
, region
->region
))
1925 record_loop_in_sese (bb
, region
);
1927 gcond
*stmt
= single_pred_cond_non_loop_exit (bb
);
1931 edge e
= single_pred_edge (bb
);
1933 conditions
.safe_push (stmt
);
1935 if (e
->flags
& EDGE_TRUE_VALUE
)
1936 cases
.safe_push (stmt
);
1938 cases
.safe_push (NULL
);
1941 scop
->scop_info
->bbs
.safe_push (bb
);
1943 gimple_poly_bb_p gbb
= try_generate_gimple_bb (scop
, bb
);
1947 GBB_CONDITIONS (gbb
) = conditions
.copy ();
1948 GBB_CONDITION_CASES (gbb
) = cases
.copy ();
1950 poly_bb_p pbb
= new_poly_bb (scop
, gbb
);
1951 scop
->pbbs
.safe_push (pbb
);
1954 data_reference_p dr
;
1955 FOR_EACH_VEC_ELT (gbb
->data_refs
, i
, dr
)
1957 DEBUG_PRINT (dp
<< "Adding memory ";
1962 print_generic_expr (dump_file
, dr
->ref
, 0);
1963 dp
<< "\nFrom stmt: ";
1964 print_gimple_stmt (dump_file
, dr
->stmt
, 0, 0));
1966 scop
->drs
.safe_push (dr_info (dr
, pbb
));
1972 /* Call-back for dom_walk executed after visiting the dominated
1976 gather_bbs::after_dom_children (basic_block bb
)
1978 if (!bb_in_sese_p (bb
, scop
->scop_info
->region
))
1981 if (single_pred_cond_non_loop_exit (bb
))
1988 /* Find Static Control Parts (SCoP) in the current function and pushes
1992 build_scops (vec
<scop_p
> *scops
)
1995 dp
.set_dump_file (dump_file
);
1997 canonicalize_loop_closed_ssa_form ();
2000 sb
.build_scop_depth (scop_detection::invalid_sese
, current_loops
->tree_root
);
2002 /* Now create scops from the lightweight SESEs. */
2003 vec
<sese_l
> scops_l
= sb
.get_scops ();
2006 FOR_EACH_VEC_ELT (scops_l
, i
, s
)
2008 scop_p scop
= new_scop (s
->entry
, s
->exit
);
2010 /* Record all basic blocks and their conditions in REGION. */
2011 gather_bbs (CDI_DOMINATORS
, scop
).walk (cfun
->cfg
->x_entry_block_ptr
);
2013 build_alias_set (scop
);
2015 /* Do not optimize a scop containing only PBBs that do not belong
2017 if (sb
.nb_pbbs_in_loops (scop
) == 0)
2019 DEBUG_PRINT (dp
<< "[scop-detection-fail] no data references.\n");
2024 unsigned max_arrays
= PARAM_VALUE (PARAM_GRAPHITE_MAX_ARRAYS_PER_SCOP
);
2025 if (scop
->drs
.length () >= max_arrays
)
2027 DEBUG_PRINT (dp
<< "[scop-detection-fail] too many data references: "
2028 << scop
->drs
.length ()
2029 << " is larger than --param graphite-max-arrays-per-scop="
2030 << max_arrays
<< ".\n");
2035 find_scop_parameters (scop
);
2036 graphite_dim_t max_dim
= PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS
);
2038 if (scop_nb_params (scop
) > max_dim
)
2040 DEBUG_PRINT (dp
<< "[scop-detection-fail] too many parameters: "
2041 << scop_nb_params (scop
)
2042 << " larger than --param graphite-max-nb-scop-params="
2043 << max_dim
<< ".\n");
2048 scops
->safe_push (scop
);
2051 DEBUG_PRINT (dp
<< "number of SCoPs: " << (scops
? scops
->length () : 0););
2054 #endif /* HAVE_isl */