2017-09-18 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / graphite-scop-detection.c
blob71ddfd8e23464372d5a57cc58cc2784e9a317d57
1 /* Detection of Static Control Parts (SCoP) for Graphite.
2 Copyright (C) 2009-2017 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.com> and
4 Tobias Grosser <grosser@fim.uni-passau.de>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #define USES_ISL
24 #include "config.h"
26 #ifdef HAVE_isl
28 #include "system.h"
29 #include "coretypes.h"
30 #include "backend.h"
31 #include "cfghooks.h"
32 #include "domwalk.h"
33 #include "params.h"
34 #include "tree.h"
35 #include "gimple.h"
36 #include "ssa.h"
37 #include "fold-const.h"
38 #include "gimple-iterator.h"
39 #include "tree-cfg.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"
44 #include "tree-ssa.h"
45 #include "cfgloop.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"
51 #include "graphite.h"
53 class debug_printer
55 private:
56 FILE *dump_file;
58 public:
59 void
60 set_dump_file (FILE *f)
62 gcc_assert (f);
63 dump_file = f;
66 friend debug_printer &
67 operator<< (debug_printer &output, int i)
69 fprintf (output.dump_file, "%d", i);
70 return output;
72 friend debug_printer &
73 operator<< (debug_printer &output, const char *s)
75 fprintf (output.dump_file, "%s", s);
76 return output;
78 } dp;
80 #define DEBUG_PRINT(args) do \
81 { \
82 if (dump_file && (dump_flags & TDF_DETAILS)) { args; } \
83 } while (0);
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.
89 Special nodes:
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. */
95 DEBUG_FUNCTION void
96 dot_all_sese (FILE *file, vec<sese_l>& scops)
98 /* Disable debugging while printing graph. */
99 dump_flags_t tmp_dump_flags = dump_flags;
100 dump_flags = TDF_NONE;
102 fprintf (file, "digraph all {\n");
104 basic_block bb;
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\" ",
113 bb->index);
114 fprintf (file, "CELLSPACING=\"0\">\n");
116 /* Select color for SCoP. */
117 sese_l *region;
118 int i;
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))
125 const char *color;
126 switch (i % 17)
128 case 0: /* red */
129 color = "#e41a1c";
130 break;
131 case 1: /* blue */
132 color = "#377eb8";
133 break;
134 case 2: /* green */
135 color = "#4daf4a";
136 break;
137 case 3: /* purple */
138 color = "#984ea3";
139 break;
140 case 4: /* orange */
141 color = "#ff7f00";
142 break;
143 case 5: /* yellow */
144 color = "#ffff33";
145 break;
146 case 6: /* brown */
147 color = "#a65628";
148 break;
149 case 7: /* rose */
150 color = "#f781bf";
151 break;
152 case 8:
153 color = "#8dd3c7";
154 break;
155 case 9:
156 color = "#ffffb3";
157 break;
158 case 10:
159 color = "#bebada";
160 break;
161 case 11:
162 color = "#fb8072";
163 break;
164 case 12:
165 color = "#80b1d3";
166 break;
167 case 13:
168 color = "#fdb462";
169 break;
170 case 14:
171 color = "#b3de69";
172 break;
173 case 15:
174 color = "#fccde5";
175 break;
176 case 16:
177 color = "#bc80bd";
178 break;
179 default: /* gray */
180 color = "#999999";
183 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">",
184 color);
186 if (!sese_in_region)
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);
195 else
196 fprintf (file, " %d ", bb->index);
198 fprintf (file, "{lp_%d}", bb->loop_father->num);
200 if (!sese_in_region)
201 fprintf (file, ")");
203 fprintf (file, "</TD></TR>\n");
204 part_of_scop = true;
208 if (!part_of_scop)
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)
219 edge e;
220 edge_iterator ei;
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. */
233 DEBUG_FUNCTION void
234 dot_sese (sese_l& scop)
236 vec<sese_l> scops;
237 scops.create (1);
239 if (scop)
240 scops.safe_push (scop);
242 dot_all_sese (stderr, scops);
244 scops.release ();
247 DEBUG_FUNCTION void
248 dot_cfg ()
250 vec<sese_l> scops;
251 scops.create (1);
252 dot_all_sese (stderr, scops);
253 scops.release ();
256 /* Return true if BB is empty, contains only DEBUG_INSNs. */
258 static bool
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)
265 return false;
267 return true;
270 /* Returns true when P1 and P2 are close phis with the same
271 argument. */
273 static inline bool
274 same_close_phi_node (gphi *p1, gphi *p2)
276 return (types_compatible_p (TREE_TYPE (gimple_phi_result (p1)),
277 TREE_TYPE (gimple_phi_result (p2)))
278 && operand_equal_p (gimple_phi_arg_def (p1, 0),
279 gimple_phi_arg_def (p2, 0), 0));
282 static void make_close_phi_nodes_unique (basic_block bb);
284 /* Remove the close phi node at GSI and replace its rhs with the rhs
285 of PHI. */
287 static void
288 remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
290 gimple *use_stmt;
291 use_operand_p use_p;
292 imm_use_iterator imm_iter;
293 tree res = gimple_phi_result (phi);
294 tree def = gimple_phi_result (gsi->phi ());
296 gcc_assert (same_close_phi_node (phi, gsi->phi ()));
298 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
300 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
301 SET_USE (use_p, res);
303 update_stmt (use_stmt);
305 /* It is possible that we just created a duplicate close-phi
306 for an already-processed containing loop. Check for this
307 case and clean it up. */
308 if (gimple_code (use_stmt) == GIMPLE_PHI
309 && gimple_phi_num_args (use_stmt) == 1)
310 make_close_phi_nodes_unique (gimple_bb (use_stmt));
313 remove_phi_node (gsi, true);
316 /* Removes all the close phi duplicates from BB. */
318 static void
319 make_close_phi_nodes_unique (basic_block bb)
321 gphi_iterator psi;
323 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
325 gphi_iterator gsi = psi;
326 gphi *phi = psi.phi ();
328 /* At this point, PHI should be a close phi in normal form. */
329 gcc_assert (gimple_phi_num_args (phi) == 1);
331 /* Iterate over the next phis and remove duplicates. */
332 gsi_next (&gsi);
333 while (!gsi_end_p (gsi))
334 if (same_close_phi_node (phi, gsi.phi ()))
335 remove_duplicate_close_phi (phi, &gsi);
336 else
337 gsi_next (&gsi);
341 /* Return true when NAME is defined in LOOP. */
343 static bool
344 defined_in_loop_p (tree name, loop_p loop)
346 gcc_assert (TREE_CODE (name) == SSA_NAME);
347 return loop == loop_containing_stmt (SSA_NAME_DEF_STMT (name));
350 /* Transforms LOOP to the canonical loop closed SSA form. */
352 static void
353 canonicalize_loop_closed_ssa (loop_p loop)
355 edge e = single_exit (loop);
356 basic_block bb;
358 if (!e || e->flags & EDGE_ABNORMAL)
359 return;
361 bb = e->dest;
363 if (single_pred_p (bb))
365 e = split_block_after_labels (bb);
366 DEBUG_PRINT (dp << "Splitting bb_" << bb->index << ".\n");
367 make_close_phi_nodes_unique (e->src);
369 else
371 gphi_iterator psi;
372 basic_block close = split_edge (e);
374 e = single_succ_edge (close);
375 DEBUG_PRINT (dp << "Splitting edge (" << e->src->index << ","
376 << e->dest->index << ")\n");
378 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
380 gphi *phi = psi.phi ();
381 unsigned i;
383 for (i = 0; i < gimple_phi_num_args (phi); i++)
384 if (gimple_phi_arg_edge (phi, i) == e)
386 tree res, arg = gimple_phi_arg_def (phi, i);
387 use_operand_p use_p;
388 gphi *close_phi;
390 /* Only add close phi nodes for SSA_NAMEs defined in LOOP. */
391 if (TREE_CODE (arg) != SSA_NAME
392 || !defined_in_loop_p (arg, loop))
393 continue;
395 close_phi = create_phi_node (NULL_TREE, close);
396 res = create_new_def_for (arg, close_phi,
397 gimple_phi_result_ptr (close_phi));
398 add_phi_arg (close_phi, arg,
399 gimple_phi_arg_edge (close_phi, 0),
400 UNKNOWN_LOCATION);
401 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
402 replace_exp (use_p, res);
403 update_stmt (phi);
407 make_close_phi_nodes_unique (close);
410 /* The code above does not properly handle changes in the post dominance
411 information (yet). */
412 recompute_all_dominators ();
415 /* Converts the current loop closed SSA form to a canonical form
416 expected by the Graphite code generation.
418 The loop closed SSA form has the following invariant: a variable
419 defined in a loop that is used outside the loop appears only in the
420 phi nodes in the destination of the loop exit. These phi nodes are
421 called close phi nodes.
423 The canonical loop closed SSA form contains the extra invariants:
425 - when the loop contains only one exit, the close phi nodes contain
426 only one argument. That implies that the basic block that contains
427 the close phi nodes has only one predecessor, that is a basic block
428 in the loop.
430 - the basic block containing the close phi nodes does not contain
431 other statements.
433 - there exist only one phi node per definition in the loop.
436 static void
437 canonicalize_loop_closed_ssa_form (void)
439 checking_verify_loop_closed_ssa (true);
441 loop_p loop;
442 FOR_EACH_LOOP (loop, 0)
443 canonicalize_loop_closed_ssa (loop);
445 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
446 update_ssa (TODO_update_ssa);
448 checking_verify_loop_closed_ssa (true);
451 /* Can all ivs be represented by a signed integer?
452 As isl might generate negative values in its expressions, signed loop ivs
453 are required in the backend. */
455 static bool
456 loop_ivs_can_be_represented (loop_p loop)
458 unsigned type_long_long = TYPE_PRECISION (long_long_integer_type_node);
459 for (gphi_iterator psi = gsi_start_phis (loop->header); !gsi_end_p (psi);
460 gsi_next (&psi))
462 gphi *phi = psi.phi ();
463 tree res = PHI_RESULT (phi);
464 tree type = TREE_TYPE (res);
466 if (TYPE_UNSIGNED (type) && TYPE_PRECISION (type) >= type_long_long)
467 return false;
470 return true;
473 /* Returns a COND_EXPR statement when BB has a single predecessor, the
474 edge between BB and its predecessor is not a loop exit edge, and
475 the last statement of the single predecessor is a COND_EXPR. */
477 static gcond *
478 single_pred_cond_non_loop_exit (basic_block bb)
480 if (single_pred_p (bb))
482 edge e = single_pred_edge (bb);
483 basic_block pred = e->src;
484 gimple *stmt;
486 if (loop_depth (pred->loop_father) > loop_depth (bb->loop_father))
487 return NULL;
489 stmt = last_stmt (pred);
491 if (stmt && gimple_code (stmt) == GIMPLE_COND)
492 return as_a<gcond *> (stmt);
495 return NULL;
498 namespace
501 /* Build the maximal scop containing LOOPs and add it to SCOPS. */
503 class scop_detection
505 public:
506 scop_detection () : scops (vNULL) {}
508 ~scop_detection ()
510 scops.release ();
513 /* A marker for invalid sese_l. */
514 static sese_l invalid_sese;
516 /* Return the SCOPS in this SCOP_DETECTION. */
518 vec<sese_l>
519 get_scops ()
521 return scops;
524 /* Return an sese_l around the LOOP. */
526 sese_l get_sese (loop_p loop);
528 /* Return the closest dominator with a single entry edge. In case of a
529 back-loop the back-edge is not counted. */
531 static edge get_nearest_dom_with_single_entry (basic_block dom);
533 /* Return the closest post-dominator with a single exit edge. In case of a
534 back-loop the back-edge is not counted. */
536 static edge get_nearest_pdom_with_single_exit (basic_block dom);
538 /* Merge scops at same loop depth and returns the new sese.
539 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
541 sese_l merge_sese (sese_l first, sese_l second) const;
543 /* Build scop outer->inner if possible. */
545 sese_l build_scop_depth (sese_l s, loop_p loop);
547 /* If loop and loop->next are valid scops, try to merge them. */
549 sese_l build_scop_breadth (sese_l s1, loop_p loop);
551 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
552 region of code that can be represented in the polyhedral model. SCOP
553 defines the region we analyse. */
555 bool loop_is_valid_in_scop (loop_p loop, sese_l scop) const;
557 /* Return true when BEGIN is the preheader edge of a loop with a single exit
558 END. */
560 static bool region_has_one_loop (sese_l s);
562 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
564 void add_scop (sese_l s);
566 /* Returns true if S1 subsumes/surrounds S2. */
567 static bool subsumes (sese_l s1, sese_l s2);
569 /* Remove a SCoP which is subsumed by S1. */
570 void remove_subscops (sese_l s1);
572 /* Returns true if S1 intersects with S2. Since we already know that S1 does
573 not subsume S2 or vice-versa, we only check for entry bbs. */
575 static bool intersects (sese_l s1, sese_l s2);
577 /* Remove one of the scops when it intersects with any other. */
579 void remove_intersecting_scops (sese_l s1);
581 /* Return true when the body of LOOP has statements that can be represented
582 as a valid scop. */
584 bool loop_body_is_valid_scop (loop_p loop, sese_l scop) const;
586 /* Return true when BB contains a harmful operation for a scop: that
587 can be a function call with side effects, the induction variables
588 are not linear with respect to SCOP, etc. The current open
589 scop should end before this statement. */
591 bool harmful_stmt_in_bb (sese_l scop, basic_block bb) const;
593 /* Return true when a statement in SCOP cannot be represented by Graphite.
594 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
595 Limit the number of bbs between adjacent loops to
596 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
598 bool harmful_loop_in_region (sese_l scop) const;
600 /* Return true only when STMT is simple enough for being handled by Graphite.
601 This depends on SCOP, as the parameters are initialized relatively to
602 this basic block, the linear functions are initialized based on the
603 outermost loop containing STMT inside the SCOP. BB is the place where we
604 try to evaluate the STMT. */
606 bool stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
607 basic_block bb) const;
609 /* Something like "n * m" is not allowed. */
611 static bool graphite_can_represent_init (tree e);
613 /* Return true when SCEV can be represented in the polyhedral model.
615 An expression can be represented, if it can be expressed as an
616 affine expression. For loops (i, j) and parameters (m, n) all
617 affine expressions are of the form:
619 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
621 1 i + 20 j + (-2) m + 25
623 Something like "i * n" or "n * m" is not allowed. */
625 static bool graphite_can_represent_scev (tree scev);
627 /* Return true when EXPR can be represented in the polyhedral model.
629 This means an expression can be represented, if it is linear with respect
630 to the loops and the strides are non parametric. LOOP is the place where
631 the expr will be evaluated. SCOP defines the region we analyse. */
633 static bool graphite_can_represent_expr (sese_l scop, loop_p loop,
634 tree expr);
636 /* Return true if the data references of STMT can be represented by Graphite.
637 We try to analyze the data references in a loop contained in the SCOP. */
639 static bool stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt);
641 /* Remove the close phi node at GSI and replace its rhs with the rhs
642 of PHI. */
644 static void remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi);
646 /* Returns true when Graphite can represent LOOP in SCOP.
647 FIXME: For the moment, graphite cannot be used on loops that iterate using
648 induction variables that wrap. */
650 static bool can_represent_loop_1 (loop_p loop, sese_l scop);
652 /* Return true when all the loops within LOOP can be represented by
653 Graphite. */
655 static bool can_represent_loop (loop_p loop, sese_l scop);
657 /* Returns the number of pbbs that are in loops contained in SCOP. */
659 static int nb_pbbs_in_loops (scop_p scop);
661 static bool graphite_can_represent_stmt (sese_l, gimple *, basic_block);
663 private:
664 vec<sese_l> scops;
667 sese_l scop_detection::invalid_sese (NULL, NULL);
669 /* Return an sese_l around the LOOP. */
671 sese_l
672 scop_detection::get_sese (loop_p loop)
674 if (!loop)
675 return invalid_sese;
677 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS))
678 return invalid_sese;
679 edge scop_end = single_exit (loop);
680 if (!scop_end)
681 return invalid_sese;
682 edge scop_begin = loop_preheader_edge (loop);
683 sese_l s (scop_begin, scop_end);
684 return s;
687 /* Return the closest dominator with a single entry edge. */
689 edge
690 scop_detection::get_nearest_dom_with_single_entry (basic_block dom)
692 if (!dom->preds)
693 return NULL;
695 /* If any of the dominators has two predecessors but one of them is a back
696 edge, then that basic block also qualifies as a dominator with single
697 entry. */
698 if (dom->preds->length () == 2)
700 /* If e1->src dominates e2->src then e1->src will also dominate dom. */
701 edge e1 = (*dom->preds)[0];
702 edge e2 = (*dom->preds)[1];
703 loop_p l = dom->loop_father;
704 loop_p l1 = e1->src->loop_father;
705 loop_p l2 = e2->src->loop_father;
706 if (l != l1 && l == l2
707 && dominated_by_p (CDI_DOMINATORS, e2->src, e1->src))
708 return e1;
709 if (l != l2 && l == l1
710 && dominated_by_p (CDI_DOMINATORS, e1->src, e2->src))
711 return e2;
714 while (dom->preds->length () != 1)
716 if (dom->preds->length () < 1)
717 return NULL;
718 dom = get_immediate_dominator (CDI_DOMINATORS, dom);
719 if (!dom->preds)
720 return NULL;
722 return (*dom->preds)[0];
725 /* Return the closest post-dominator with a single exit edge. In case of a
726 back-loop the back-edge is not counted. */
728 edge
729 scop_detection::get_nearest_pdom_with_single_exit (basic_block pdom)
731 if (!pdom->succs)
732 return NULL;
734 /* If any of the post-dominators has two successors but one of them is a back
735 edge, then that basic block also qualifies as a post-dominator with single
736 exit. */
737 if (pdom->succs->length () == 2)
739 /* If e1->dest post-dominates e2->dest then e1->dest will also
740 post-dominate pdom. */
741 edge e1 = (*pdom->succs)[0];
742 edge e2 = (*pdom->succs)[1];
743 loop_p l = pdom->loop_father;
744 loop_p l1 = e1->dest->loop_father;
745 loop_p l2 = e2->dest->loop_father;
746 if (l != l1 && l == l2
747 && dominated_by_p (CDI_POST_DOMINATORS, e2->dest, e1->dest))
748 return e1;
749 if (l != l2 && l == l1
750 && dominated_by_p (CDI_POST_DOMINATORS, e1->dest, e2->dest))
751 return e2;
754 while (pdom->succs->length () != 1)
756 if (pdom->succs->length () < 1)
757 return NULL;
758 pdom = get_immediate_dominator (CDI_POST_DOMINATORS, pdom);
759 if (!pdom->succs)
760 return NULL;
763 return (*pdom->succs)[0];
766 /* Merge scops at same loop depth and returns the new sese.
767 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
769 sese_l
770 scop_detection::merge_sese (sese_l first, sese_l second) const
772 /* In the trivial case first/second may be NULL. */
773 if (!first)
774 return second;
775 if (!second)
776 return first;
778 DEBUG_PRINT (dp << "[scop-detection] try merging sese s1: ";
779 print_sese (dump_file, first);
780 dp << "[scop-detection] try merging sese s2: ";
781 print_sese (dump_file, second));
783 /* Assumption: Both the sese's should be at the same loop depth or one scop
784 should subsume the other like in case of nested loops. */
786 /* Find the common dominators for entry,
787 and common post-dominators for the exit. */
788 basic_block dom = nearest_common_dominator (CDI_DOMINATORS,
789 get_entry_bb (first),
790 get_entry_bb (second));
792 edge entry = get_nearest_dom_with_single_entry (dom);
794 if (!entry || (entry->flags & EDGE_IRREDUCIBLE_LOOP))
795 return invalid_sese;
797 basic_block pdom = nearest_common_dominator (CDI_POST_DOMINATORS,
798 get_exit_bb (first),
799 get_exit_bb (second));
800 pdom = nearest_common_dominator (CDI_POST_DOMINATORS, dom, pdom);
802 edge exit = get_nearest_pdom_with_single_exit (pdom);
804 if (!exit || (exit->flags & EDGE_IRREDUCIBLE_LOOP))
805 return invalid_sese;
807 sese_l combined (entry, exit);
809 DEBUG_PRINT (dp << "[scop-detection] checking combined sese: ";
810 print_sese (dump_file, combined));
812 /* FIXME: We could iterate to find the dom which dominates pdom, and pdom
813 which post-dominates dom, until it stabilizes. Also, ENTRY->SRC and
814 EXIT->DEST should be in the same loop nest. */
815 if (!dominated_by_p (CDI_DOMINATORS, pdom, dom)
816 || loop_depth (entry->src->loop_father)
817 != loop_depth (exit->dest->loop_father))
818 return invalid_sese;
820 /* For now we just bail out when there is a loop exit in the region
821 that is not also the exit of the region. We could enlarge the
822 region to cover the loop that region exits to. See PR79977. */
823 if (loop_outer (entry->src->loop_father))
825 vec<edge> exits = get_loop_exit_edges (entry->src->loop_father);
826 for (unsigned i = 0; i < exits.length (); ++i)
828 if (exits[i] != exit
829 && bb_in_region (exits[i]->src, entry->dest, exit->src))
831 DEBUG_PRINT (dp << "[scop-detection-fail] cannot merge seses.\n");
832 exits.release ();
833 return invalid_sese;
836 exits.release ();
839 /* For now we just want to bail out when exit does not post-dominate entry.
840 TODO: We might just add a basic_block at the exit to make exit
841 post-dominate entry (the entire region). */
842 if (!dominated_by_p (CDI_POST_DOMINATORS, get_entry_bb (combined),
843 get_exit_bb (combined))
844 || !dominated_by_p (CDI_DOMINATORS, get_exit_bb (combined),
845 get_entry_bb (combined)))
847 DEBUG_PRINT (dp << "[scop-detection-fail] cannot merge seses.\n");
848 return invalid_sese;
851 /* FIXME: We should remove this piece of code once
852 canonicalize_loop_closed_ssa has been removed, because that function
853 adds a BB with single exit. */
854 if (!trivially_empty_bb_p (get_exit_bb (combined)))
856 /* Find the first empty succ (with single exit) of combined.exit. */
857 basic_block imm_succ = combined.exit->dest;
858 if (single_succ_p (imm_succ)
859 && single_pred_p (imm_succ)
860 && trivially_empty_bb_p (imm_succ))
861 combined.exit = single_succ_edge (imm_succ);
862 else
864 DEBUG_PRINT (dp << "[scop-detection-fail] Discarding SCoP because "
865 << "no single exit (empty succ) for sese exit";
866 print_sese (dump_file, combined));
867 return invalid_sese;
871 /* Analyze all the BBs in new sese. */
872 if (harmful_loop_in_region (combined))
873 return invalid_sese;
875 DEBUG_PRINT (dp << "[merged-sese] s1: "; print_sese (dump_file, combined));
877 return combined;
880 /* Build scop outer->inner if possible. */
882 sese_l
883 scop_detection::build_scop_depth (sese_l s, loop_p loop)
885 if (!loop)
886 return s;
888 DEBUG_PRINT (dp << "[Depth loop_" << loop->num << "]\n");
889 s = build_scop_depth (s, loop->inner);
891 sese_l s2 = merge_sese (s, get_sese (loop));
892 if (!s2)
894 /* s might be a valid scop, so return it and start analyzing from the
895 adjacent loop. */
896 build_scop_depth (invalid_sese, loop->next);
897 return s;
900 if (!loop_is_valid_in_scop (loop, s2))
901 return build_scop_depth (invalid_sese, loop->next);
903 return build_scop_breadth (s2, loop);
906 /* If loop and loop->next are valid scops, try to merge them. */
908 sese_l
909 scop_detection::build_scop_breadth (sese_l s1, loop_p loop)
911 if (!loop)
912 return s1;
913 DEBUG_PRINT (dp << "[Breadth loop_" << loop->num << "]\n");
914 gcc_assert (s1);
916 loop_p l = loop;
917 sese_l s2 = build_scop_depth (invalid_sese, l->next);
918 if (!s2)
920 if (s1)
921 add_scop (s1);
922 return s1;
925 sese_l combined = merge_sese (s1, s2);
927 /* Combining adjacent loops may add unrelated loops into the
928 region so we have to check all sub-loops of the outer loop
929 that are in the combined region. */
930 if (combined)
931 for (l = loop_outer (loop)->inner; l; l = l->next)
932 if (bb_in_sese_p (l->header, combined)
933 && ! loop_is_valid_in_scop (l, combined))
935 combined = invalid_sese;
936 break;
939 if (combined)
940 s1 = combined;
941 else
942 add_scop (s2);
944 if (s1)
945 add_scop (s1);
946 return s1;
949 /* Returns true when Graphite can represent LOOP in SCOP.
950 FIXME: For the moment, graphite cannot be used on loops that iterate using
951 induction variables that wrap. */
953 bool
954 scop_detection::can_represent_loop_1 (loop_p loop, sese_l scop)
956 tree niter;
957 struct tree_niter_desc niter_desc;
959 return single_exit (loop)
960 && !(loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP)
961 && number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
962 && niter_desc.control.no_overflow
963 && (niter = number_of_latch_executions (loop))
964 && !chrec_contains_undetermined (niter)
965 && !chrec_contains_undetermined (scalar_evolution_in_region (scop,
966 loop, niter))
967 && graphite_can_represent_expr (scop, loop, niter);
970 /* Return true when all the loops within LOOP can be represented by
971 Graphite. */
973 bool
974 scop_detection::can_represent_loop (loop_p loop, sese_l scop)
976 if (!can_represent_loop_1 (loop, scop))
977 return false;
978 if (loop->inner && !can_represent_loop (loop->inner, scop))
979 return false;
980 if (loop->next && !can_represent_loop (loop->next, scop))
981 return false;
983 return true;
986 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
987 region of code that can be represented in the polyhedral model. SCOP
988 defines the region we analyse. */
990 bool
991 scop_detection::loop_is_valid_in_scop (loop_p loop, sese_l scop) const
993 if (!scop)
994 return false;
996 if (!optimize_loop_nest_for_speed_p (loop))
998 DEBUG_PRINT (dp << "[scop-detection-fail] loop_"
999 << loop->num << " is not on a hot path.\n");
1000 return false;
1003 if (!can_represent_loop (loop, scop))
1005 DEBUG_PRINT (dp << "[scop-detection-fail] cannot represent loop_"
1006 << loop->num << "\n");
1007 return false;
1010 if (loop_body_is_valid_scop (loop, scop))
1012 DEBUG_PRINT (dp << "[valid-scop] loop_" << loop->num
1013 << " is a valid scop.\n");
1014 return true;
1016 return false;
1019 /* Return true when BEGIN is the preheader edge of a loop with a single exit
1020 END. */
1022 bool
1023 scop_detection::region_has_one_loop (sese_l s)
1025 edge begin = s.entry;
1026 edge end = s.exit;
1027 /* Check for a single perfectly nested loop. */
1028 if (begin->dest->loop_father->inner)
1029 return false;
1031 /* Otherwise, check whether we have adjacent loops. */
1032 return begin->dest->loop_father == end->src->loop_father;
1035 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
1037 void
1038 scop_detection::add_scop (sese_l s)
1040 gcc_assert (s);
1042 /* Do not add scops with only one loop. */
1043 if (region_has_one_loop (s))
1045 DEBUG_PRINT (dp << "[scop-detection-fail] Discarding one loop SCoP: ";
1046 print_sese (dump_file, s));
1047 return;
1050 if (get_exit_bb (s) == EXIT_BLOCK_PTR_FOR_FN (cfun))
1052 DEBUG_PRINT (dp << "[scop-detection-fail] "
1053 << "Discarding SCoP exiting to return: ";
1054 print_sese (dump_file, s));
1055 return;
1058 /* Remove all the scops which are subsumed by s. */
1059 remove_subscops (s);
1061 /* Remove intersecting scops. FIXME: It will be a good idea to keep
1062 the non-intersecting part of the scop already in the list. */
1063 remove_intersecting_scops (s);
1065 scops.safe_push (s);
1066 DEBUG_PRINT (dp << "[scop-detection] Adding SCoP: "; print_sese (dump_file, s));
1069 /* Return true when a statement in SCOP cannot be represented by Graphite.
1070 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
1071 Limit the number of bbs between adjacent loops to
1072 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
1074 bool
1075 scop_detection::harmful_loop_in_region (sese_l scop) const
1077 basic_block exit_bb = get_exit_bb (scop);
1078 basic_block entry_bb = get_entry_bb (scop);
1080 DEBUG_PRINT (dp << "[checking-harmful-bbs] ";
1081 print_sese (dump_file, scop));
1082 gcc_assert (dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb));
1084 auto_vec<basic_block> worklist;
1085 auto_bitmap loops;
1087 worklist.safe_push (entry_bb);
1088 while (! worklist.is_empty ())
1090 basic_block bb = worklist.pop ();
1091 DEBUG_PRINT (dp << "Visiting bb_" << bb->index << "\n");
1093 /* The basic block should not be part of an irreducible loop. */
1094 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1095 return true;
1097 /* Check for unstructured control flow: CFG not generated by structured
1098 if-then-else. */
1099 if (bb->succs->length () > 1)
1101 edge e;
1102 edge_iterator ei;
1103 FOR_EACH_EDGE (e, ei, bb->succs)
1104 if (!dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest)
1105 && !dominated_by_p (CDI_DOMINATORS, e->dest, bb))
1106 return true;
1109 /* Collect all loops in the current region. */
1110 loop_p loop = bb->loop_father;
1111 if (loop_in_sese_p (loop, scop))
1112 bitmap_set_bit (loops, loop->num);
1113 else
1115 /* We only check for harmful statements in basic blocks not part of
1116 any loop fully contained in the scop: other bbs are checked below
1117 in loop_is_valid_in_scop. */
1118 if (harmful_stmt_in_bb (scop, bb))
1119 return true;
1122 if (bb != exit_bb)
1123 for (basic_block dom = first_dom_son (CDI_DOMINATORS, bb);
1124 dom;
1125 dom = next_dom_son (CDI_DOMINATORS, dom))
1126 worklist.safe_push (dom);
1129 /* Go through all loops and check that they are still valid in the combined
1130 scop. */
1131 unsigned j;
1132 bitmap_iterator bi;
1133 EXECUTE_IF_SET_IN_BITMAP (loops, 0, j, bi)
1135 loop_p loop = (*current_loops->larray)[j];
1136 gcc_assert (loop->num == (int) j);
1138 if (!loop_is_valid_in_scop (loop, scop))
1139 return true;
1142 return false;
1145 /* Returns true if S1 subsumes/surrounds S2. */
1146 bool
1147 scop_detection::subsumes (sese_l s1, sese_l s2)
1149 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
1150 get_entry_bb (s1))
1151 && dominated_by_p (CDI_POST_DOMINATORS, s2.exit->dest,
1152 s1.exit->dest))
1153 return true;
1154 return false;
1157 /* Remove a SCoP which is subsumed by S1. */
1158 void
1159 scop_detection::remove_subscops (sese_l s1)
1161 int j;
1162 sese_l *s2;
1163 FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
1165 if (subsumes (s1, *s2))
1167 DEBUG_PRINT (dp << "Removing sub-SCoP";
1168 print_sese (dump_file, *s2));
1169 scops.unordered_remove (j);
1174 /* Returns true if S1 intersects with S2. Since we already know that S1 does
1175 not subsume S2 or vice-versa, we only check for entry bbs. */
1177 bool
1178 scop_detection::intersects (sese_l s1, sese_l s2)
1180 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
1181 get_entry_bb (s1))
1182 && !dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
1183 get_exit_bb (s1)))
1184 return true;
1185 if ((s1.exit == s2.entry) || (s2.exit == s1.entry))
1186 return true;
1188 return false;
1191 /* Remove one of the scops when it intersects with any other. */
1193 void
1194 scop_detection::remove_intersecting_scops (sese_l s1)
1196 int j;
1197 sese_l *s2;
1198 FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
1200 if (intersects (s1, *s2))
1202 DEBUG_PRINT (dp << "Removing intersecting SCoP";
1203 print_sese (dump_file, *s2);
1204 dp << "Intersects with:";
1205 print_sese (dump_file, s1));
1206 scops.unordered_remove (j);
1211 /* Something like "n * m" is not allowed. */
1213 bool
1214 scop_detection::graphite_can_represent_init (tree e)
1216 switch (TREE_CODE (e))
1218 case POLYNOMIAL_CHREC:
1219 return graphite_can_represent_init (CHREC_LEFT (e))
1220 && graphite_can_represent_init (CHREC_RIGHT (e));
1222 case MULT_EXPR:
1223 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
1224 return graphite_can_represent_init (TREE_OPERAND (e, 0))
1225 && tree_fits_shwi_p (TREE_OPERAND (e, 1));
1226 else
1227 return graphite_can_represent_init (TREE_OPERAND (e, 1))
1228 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
1230 case PLUS_EXPR:
1231 case POINTER_PLUS_EXPR:
1232 case MINUS_EXPR:
1233 return graphite_can_represent_init (TREE_OPERAND (e, 0))
1234 && graphite_can_represent_init (TREE_OPERAND (e, 1));
1236 case NEGATE_EXPR:
1237 case BIT_NOT_EXPR:
1238 CASE_CONVERT:
1239 case NON_LVALUE_EXPR:
1240 return graphite_can_represent_init (TREE_OPERAND (e, 0));
1242 default:
1243 break;
1246 return true;
1249 /* Return true when SCEV can be represented in the polyhedral model.
1251 An expression can be represented, if it can be expressed as an
1252 affine expression. For loops (i, j) and parameters (m, n) all
1253 affine expressions are of the form:
1255 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
1257 1 i + 20 j + (-2) m + 25
1259 Something like "i * n" or "n * m" is not allowed. */
1261 bool
1262 scop_detection::graphite_can_represent_scev (tree scev)
1264 if (chrec_contains_undetermined (scev))
1265 return false;
1267 /* We disable the handling of pointer types, because it’s currently not
1268 supported by Graphite with the isl AST generator. SSA_NAME nodes are
1269 the only nodes, which are disabled in case they are pointers to object
1270 types, but this can be changed. */
1272 if (POINTER_TYPE_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
1273 return false;
1275 switch (TREE_CODE (scev))
1277 case NEGATE_EXPR:
1278 case BIT_NOT_EXPR:
1279 CASE_CONVERT:
1280 case NON_LVALUE_EXPR:
1281 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
1283 case PLUS_EXPR:
1284 case POINTER_PLUS_EXPR:
1285 case MINUS_EXPR:
1286 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
1287 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
1289 case MULT_EXPR:
1290 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
1291 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
1292 && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
1293 && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
1294 && graphite_can_represent_init (scev)
1295 && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
1296 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
1298 case POLYNOMIAL_CHREC:
1299 /* Check for constant strides. With a non constant stride of
1300 'n' we would have a value of 'iv * n'. Also check that the
1301 initial value can represented: for example 'n * m' cannot be
1302 represented. */
1303 if (!evolution_function_right_is_integer_cst (scev)
1304 || !graphite_can_represent_init (scev))
1305 return false;
1306 return graphite_can_represent_scev (CHREC_LEFT (scev));
1308 default:
1309 break;
1312 /* Only affine functions can be represented. */
1313 if (tree_contains_chrecs (scev, NULL) || !scev_is_linear_expression (scev))
1314 return false;
1316 return true;
1319 /* Return true when EXPR can be represented in the polyhedral model.
1321 This means an expression can be represented, if it is linear with respect to
1322 the loops and the strides are non parametric. LOOP is the place where the
1323 expr will be evaluated. SCOP defines the region we analyse. */
1325 bool
1326 scop_detection::graphite_can_represent_expr (sese_l scop, loop_p loop,
1327 tree expr)
1329 tree scev = scalar_evolution_in_region (scop, loop, expr);
1330 return graphite_can_represent_scev (scev);
1333 /* Return true if the data references of STMT can be represented by Graphite.
1334 We try to analyze the data references in a loop contained in the SCOP. */
1336 bool
1337 scop_detection::stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt)
1339 loop_p nest = outermost_loop_in_sese (scop, gimple_bb (stmt));
1340 loop_p loop = loop_containing_stmt (stmt);
1341 if (!loop_in_sese_p (loop, scop))
1342 loop = nest;
1344 auto_vec<data_reference_p> drs;
1345 if (! graphite_find_data_references_in_stmt (nest, loop, stmt, &drs))
1346 return false;
1348 int j;
1349 data_reference_p dr;
1350 FOR_EACH_VEC_ELT (drs, j, dr)
1352 for (unsigned i = 0; i < DR_NUM_DIMENSIONS (dr); ++i)
1353 if (! graphite_can_represent_scev (DR_ACCESS_FN (dr, i)))
1354 return false;
1357 return true;
1360 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
1361 Calls have side-effects, except those to const or pure
1362 functions. */
1364 static bool
1365 stmt_has_side_effects (gimple *stmt)
1367 if (gimple_has_volatile_ops (stmt)
1368 || (gimple_code (stmt) == GIMPLE_CALL
1369 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
1370 || (gimple_code (stmt) == GIMPLE_ASM))
1372 DEBUG_PRINT (dp << "[scop-detection-fail] "
1373 << "Statement has side-effects:\n";
1374 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS | TDF_MEMSYMS));
1375 return true;
1377 return false;
1380 /* Returns true if STMT can be represented in polyhedral model. LABEL,
1381 simple COND stmts, pure calls, and assignments can be repesented. */
1383 bool
1384 scop_detection::graphite_can_represent_stmt (sese_l scop, gimple *stmt,
1385 basic_block bb)
1387 loop_p loop = bb->loop_father;
1388 switch (gimple_code (stmt))
1390 case GIMPLE_LABEL:
1391 return true;
1393 case GIMPLE_COND:
1395 /* We can handle all binary comparisons. Inequalities are
1396 also supported as they can be represented with union of
1397 polyhedra. */
1398 enum tree_code code = gimple_cond_code (stmt);
1399 if (!(code == LT_EXPR
1400 || code == GT_EXPR
1401 || code == LE_EXPR
1402 || code == GE_EXPR
1403 || code == EQ_EXPR
1404 || code == NE_EXPR))
1406 DEBUG_PRINT (dp << "[scop-detection-fail] "
1407 << "Graphite cannot handle cond stmt:\n";
1408 print_gimple_stmt (dump_file, stmt, 0,
1409 TDF_VOPS | TDF_MEMSYMS));
1410 return false;
1413 for (unsigned i = 0; i < 2; ++i)
1415 tree op = gimple_op (stmt, i);
1416 if (!graphite_can_represent_expr (scop, loop, op)
1417 /* We can only constrain on integer type. */
1418 || (TREE_CODE (TREE_TYPE (op)) != INTEGER_TYPE))
1420 DEBUG_PRINT (dp << "[scop-detection-fail] "
1421 << "Graphite cannot represent stmt:\n";
1422 print_gimple_stmt (dump_file, stmt, 0,
1423 TDF_VOPS | TDF_MEMSYMS));
1424 return false;
1428 return true;
1431 case GIMPLE_ASSIGN:
1432 case GIMPLE_CALL:
1433 return true;
1435 default:
1436 /* These nodes cut a new scope. */
1437 DEBUG_PRINT (
1438 dp << "[scop-detection-fail] "
1439 << "Gimple stmt not handled in Graphite:\n";
1440 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS | TDF_MEMSYMS));
1441 return false;
1445 /* Return true only when STMT is simple enough for being handled by Graphite.
1446 This depends on SCOP, as the parameters are initialized relatively to
1447 this basic block, the linear functions are initialized based on the outermost
1448 loop containing STMT inside the SCOP. BB is the place where we try to
1449 evaluate the STMT. */
1451 bool
1452 scop_detection::stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
1453 basic_block bb) const
1455 gcc_assert (scop);
1457 if (is_gimple_debug (stmt))
1458 return true;
1460 if (stmt_has_side_effects (stmt))
1461 return false;
1463 if (!stmt_has_simple_data_refs_p (scop, stmt))
1465 DEBUG_PRINT (dp << "[scop-detection-fail] "
1466 << "Graphite cannot handle data-refs in stmt:\n";
1467 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS););
1468 return false;
1471 return graphite_can_represent_stmt (scop, stmt, bb);
1474 /* Return true when BB contains a harmful operation for a scop: that
1475 can be a function call with side effects, the induction variables
1476 are not linear with respect to SCOP, etc. The current open
1477 scop should end before this statement. */
1479 bool
1480 scop_detection::harmful_stmt_in_bb (sese_l scop, basic_block bb) const
1482 gimple_stmt_iterator gsi;
1484 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1485 if (!stmt_simple_for_scop_p (scop, gsi_stmt (gsi), bb))
1486 return true;
1488 return false;
1491 /* Return true when the body of LOOP has statements that can be represented as a
1492 valid scop. */
1494 bool
1495 scop_detection::loop_body_is_valid_scop (loop_p loop, sese_l scop) const
1497 if (!loop_ivs_can_be_represented (loop))
1499 DEBUG_PRINT (dp << "[scop-detection-fail] loop_" << loop->num
1500 << "IV cannot be represented.\n");
1501 return false;
1504 if (!loop_nest_has_data_refs (loop))
1506 DEBUG_PRINT (dp << "[scop-detection-fail] loop_" << loop->num
1507 << "does not have any data reference.\n");
1508 return false;
1511 basic_block *bbs = get_loop_body (loop);
1512 for (unsigned i = 0; i < loop->num_nodes; i++)
1514 basic_block bb = bbs[i];
1516 if (harmful_stmt_in_bb (scop, bb))
1518 free (bbs);
1519 return false;
1522 free (bbs);
1524 if (loop->inner)
1526 loop = loop->inner;
1527 while (loop)
1529 if (!loop_body_is_valid_scop (loop, scop))
1530 return false;
1531 loop = loop->next;
1535 return true;
1538 /* Returns the number of pbbs that are in loops contained in SCOP. */
1541 scop_detection::nb_pbbs_in_loops (scop_p scop)
1543 int i;
1544 poly_bb_p pbb;
1545 int res = 0;
1547 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
1548 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb)), scop->scop_info->region))
1549 res++;
1551 return res;
1554 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
1555 Otherwise returns -1. */
1557 static inline int
1558 parameter_index_in_region_1 (tree name, sese_info_p region)
1560 int i;
1561 tree p;
1563 gcc_assert (TREE_CODE (name) == SSA_NAME);
1565 FOR_EACH_VEC_ELT (region->params, i, p)
1566 if (p == name)
1567 return i;
1569 return -1;
1572 /* When the parameter NAME is in REGION, returns its index in
1573 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
1574 and returns the index of NAME. */
1576 static int
1577 parameter_index_in_region (tree name, sese_info_p region)
1579 int i;
1581 gcc_assert (TREE_CODE (name) == SSA_NAME);
1583 /* Cannot constrain on anything else than INTEGER_TYPE parameters. */
1584 if (TREE_CODE (TREE_TYPE (name)) != INTEGER_TYPE)
1585 return -1;
1587 if (!invariant_in_sese_p_rec (name, region->region, NULL))
1588 return -1;
1590 i = parameter_index_in_region_1 (name, region);
1591 if (i != -1)
1592 return i;
1594 i = region->params.length ();
1595 region->params.safe_push (name);
1596 return i;
1599 /* In the context of sese S, scan the expression E and translate it to
1600 a linear expression C. When parsing a symbolic multiplication, K
1601 represents the constant multiplier of an expression containing
1602 parameters. */
1604 static void
1605 scan_tree_for_params (sese_info_p s, tree e)
1607 if (e == chrec_dont_know)
1608 return;
1610 switch (TREE_CODE (e))
1612 case POLYNOMIAL_CHREC:
1613 scan_tree_for_params (s, CHREC_LEFT (e));
1614 break;
1616 case MULT_EXPR:
1617 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
1618 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1619 else
1620 scan_tree_for_params (s, TREE_OPERAND (e, 1));
1621 break;
1623 case PLUS_EXPR:
1624 case POINTER_PLUS_EXPR:
1625 case MINUS_EXPR:
1626 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1627 scan_tree_for_params (s, TREE_OPERAND (e, 1));
1628 break;
1630 case NEGATE_EXPR:
1631 case BIT_NOT_EXPR:
1632 CASE_CONVERT:
1633 case NON_LVALUE_EXPR:
1634 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1635 break;
1637 case SSA_NAME:
1638 parameter_index_in_region (e, s);
1639 break;
1641 case INTEGER_CST:
1642 case ADDR_EXPR:
1643 case REAL_CST:
1644 case COMPLEX_CST:
1645 case VECTOR_CST:
1646 break;
1648 default:
1649 gcc_unreachable ();
1650 break;
1654 /* Find parameters with respect to REGION in BB. We are looking in memory
1655 access functions, conditions and loop bounds. */
1657 static void
1658 find_params_in_bb (sese_info_p region, gimple_poly_bb_p gbb)
1660 /* Find parameters in the access functions of data references. */
1661 int i;
1662 data_reference_p dr;
1663 FOR_EACH_VEC_ELT (GBB_DATA_REFS (gbb), i, dr)
1664 for (unsigned j = 0; j < DR_NUM_DIMENSIONS (dr); j++)
1665 scan_tree_for_params (region, DR_ACCESS_FN (dr, j));
1667 /* Find parameters in conditional statements. */
1668 gimple *stmt;
1669 loop_p loop = GBB_BB (gbb)->loop_father;
1670 FOR_EACH_VEC_ELT (GBB_CONDITIONS (gbb), i, stmt)
1672 tree lhs = scalar_evolution_in_region (region->region, loop,
1673 gimple_cond_lhs (stmt));
1674 tree rhs = scalar_evolution_in_region (region->region, loop,
1675 gimple_cond_rhs (stmt));
1677 scan_tree_for_params (region, lhs);
1678 scan_tree_for_params (region, rhs);
1682 /* Record the parameters used in the SCOP. A variable is a parameter
1683 in a scop if it does not vary during the execution of that scop. */
1685 static void
1686 find_scop_parameters (scop_p scop)
1688 unsigned i;
1689 sese_info_p region = scop->scop_info;
1690 struct loop *loop;
1692 /* Find the parameters used in the loop bounds. */
1693 FOR_EACH_VEC_ELT (region->loop_nest, i, loop)
1695 tree nb_iters = number_of_latch_executions (loop);
1697 if (!chrec_contains_symbols (nb_iters))
1698 continue;
1700 nb_iters = scalar_evolution_in_region (region->region, loop, nb_iters);
1701 scan_tree_for_params (region, nb_iters);
1704 /* Find the parameters used in data accesses. */
1705 poly_bb_p pbb;
1706 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
1707 find_params_in_bb (region, PBB_BLACK_BOX (pbb));
1709 int nbp = sese_nb_params (region);
1710 scop_set_nb_params (scop, nbp);
1713 /* Record DEF if it is used in other bbs different than DEF_BB in the SCOP. */
1715 static void
1716 build_cross_bb_scalars_def (scop_p scop, tree def, basic_block def_bb,
1717 vec<tree> *writes)
1719 if (!def || !is_gimple_reg (def))
1720 return;
1722 /* Do not gather scalar variables that can be analyzed by SCEV as they can be
1723 generated out of the induction variables. */
1724 if (scev_analyzable_p (def, scop->scop_info->region))
1725 return;
1727 gimple *use_stmt;
1728 imm_use_iterator imm_iter;
1729 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
1730 if ((def_bb != gimple_bb (use_stmt) && !is_gimple_debug (use_stmt))
1731 /* PHIs have their effect at "BBs" on the edges. See PR79622. */
1732 || gimple_code (SSA_NAME_DEF_STMT (def)) == GIMPLE_PHI)
1734 writes->safe_push (def);
1735 DEBUG_PRINT (dp << "Adding scalar write: ";
1736 print_generic_expr (dump_file, def);
1737 dp << "\nFrom stmt: ";
1738 print_gimple_stmt (dump_file,
1739 SSA_NAME_DEF_STMT (def), 0));
1740 /* This is required by the FOR_EACH_IMM_USE_STMT when we want to break
1741 before all the uses have been visited. */
1742 BREAK_FROM_IMM_USE_STMT (imm_iter);
1746 /* Record USE if it is defined in other bbs different than USE_STMT
1747 in the SCOP. */
1749 static void
1750 build_cross_bb_scalars_use (scop_p scop, tree use, gimple *use_stmt,
1751 vec<scalar_use> *reads)
1753 gcc_assert (use);
1754 if (!is_gimple_reg (use))
1755 return;
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))
1760 return;
1762 gimple *def_stmt = SSA_NAME_DEF_STMT (use);
1763 if (gimple_bb (def_stmt) != gimple_bb (use_stmt)
1764 /* PHIs have their effect at "BBs" on the edges. See PR79622. */
1765 || gimple_code (def_stmt) == GIMPLE_PHI)
1767 DEBUG_PRINT (dp << "Adding scalar read: ";
1768 print_generic_expr (dump_file, use);
1769 dp << "\nFrom stmt: ";
1770 print_gimple_stmt (dump_file, use_stmt, 0));
1771 reads->safe_push (std::make_pair (use_stmt, use));
1775 /* Record all scalar variables that are defined and used in different BBs of the
1776 SCOP. */
1778 static void
1779 graphite_find_cross_bb_scalar_vars (scop_p scop, gimple *stmt,
1780 vec<scalar_use> *reads, vec<tree> *writes)
1782 tree def;
1784 if (gimple_code (stmt) == GIMPLE_ASSIGN)
1785 def = gimple_assign_lhs (stmt);
1786 else if (gimple_code (stmt) == GIMPLE_CALL)
1787 def = gimple_call_lhs (stmt);
1788 else if (gimple_code (stmt) == GIMPLE_PHI)
1789 def = gimple_phi_result (stmt);
1790 else
1791 return;
1794 build_cross_bb_scalars_def (scop, def, gimple_bb (stmt), writes);
1796 ssa_op_iter iter;
1797 use_operand_p use_p;
1798 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
1800 tree use = USE_FROM_PTR (use_p);
1801 build_cross_bb_scalars_use (scop, use, stmt, reads);
1805 /* Generates a polyhedral black box only if the bb contains interesting
1806 information. */
1808 static gimple_poly_bb_p
1809 try_generate_gimple_bb (scop_p scop, basic_block bb)
1811 vec<data_reference_p> drs = vNULL;
1812 vec<tree> writes = vNULL;
1813 vec<scalar_use> reads = vNULL;
1815 sese_l region = scop->scop_info->region;
1816 loop_p nest = outermost_loop_in_sese (region, bb);
1818 loop_p loop = bb->loop_father;
1819 if (!loop_in_sese_p (loop, region))
1820 loop = nest;
1822 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1823 gsi_next (&gsi))
1825 gimple *stmt = gsi_stmt (gsi);
1826 if (is_gimple_debug (stmt))
1827 continue;
1829 graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
1830 graphite_find_cross_bb_scalar_vars (scop, stmt, &reads, &writes);
1833 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1834 gsi_next (&psi))
1835 if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
1836 graphite_find_cross_bb_scalar_vars (scop, psi.phi (), &reads, &writes);
1838 if (drs.is_empty () && writes.is_empty () && reads.is_empty ())
1839 return NULL;
1841 return new_gimple_poly_bb (bb, drs, reads, writes);
1844 /* Compute alias-sets for all data references in DRS. */
1846 static bool
1847 build_alias_set (scop_p scop)
1849 int num_vertices = scop->drs.length ();
1850 struct graph *g = new_graph (num_vertices);
1851 dr_info *dr1, *dr2;
1852 int i, j;
1853 int *all_vertices;
1855 FOR_EACH_VEC_ELT (scop->drs, i, dr1)
1856 for (j = i+1; scop->drs.iterate (j, &dr2); j++)
1857 if (dr_may_alias_p (dr1->dr, dr2->dr, true))
1859 /* Dependences in the same alias set need to be handled
1860 by just looking at DR_ACCESS_FNs. */
1861 if (DR_NUM_DIMENSIONS (dr1->dr) == 0
1862 || DR_NUM_DIMENSIONS (dr1->dr) != DR_NUM_DIMENSIONS (dr2->dr)
1863 || ! operand_equal_p (DR_BASE_OBJECT (dr1->dr),
1864 DR_BASE_OBJECT (dr2->dr),
1865 OEP_ADDRESS_OF)
1866 || ! types_compatible_p (TREE_TYPE (DR_BASE_OBJECT (dr1->dr)),
1867 TREE_TYPE (DR_BASE_OBJECT (dr2->dr))))
1869 free_graph (g);
1870 return false;
1872 add_edge (g, i, j);
1873 add_edge (g, j, i);
1876 all_vertices = XNEWVEC (int, num_vertices);
1877 for (i = 0; i < num_vertices; i++)
1878 all_vertices[i] = i;
1880 graphds_dfs (g, all_vertices, num_vertices, NULL, true, NULL);
1881 free (all_vertices);
1883 for (i = 0; i < g->n_vertices; i++)
1884 scop->drs[i].alias_set = g->vertices[i].component + 1;
1886 free_graph (g);
1887 return true;
1890 /* Gather BBs and conditions for a SCOP. */
1891 class gather_bbs : public dom_walker
1893 public:
1894 gather_bbs (cdi_direction, scop_p);
1896 virtual edge before_dom_children (basic_block);
1897 virtual void after_dom_children (basic_block);
1899 private:
1900 auto_vec<gimple *, 3> conditions, cases;
1901 scop_p scop;
1904 gather_bbs::gather_bbs (cdi_direction direction, scop_p scop)
1905 : dom_walker (direction), scop (scop)
1909 /* Record in execution order the loops fully contained in the region. */
1911 static void
1912 record_loop_in_sese (basic_block bb, sese_info_p region)
1914 loop_p father = bb->loop_father;
1915 if (loop_in_sese_p (father, region->region))
1917 bool found = false;
1918 loop_p loop0;
1919 int j;
1920 FOR_EACH_VEC_ELT (region->loop_nest, j, loop0)
1921 if (father == loop0)
1923 found = true;
1924 break;
1926 if (!found)
1927 region->loop_nest.safe_push (father);
1931 /* Call-back for dom_walk executed before visiting the dominated
1932 blocks. */
1934 edge
1935 gather_bbs::before_dom_children (basic_block bb)
1937 sese_info_p region = scop->scop_info;
1938 if (!bb_in_sese_p (bb, region->region))
1939 return NULL;
1941 record_loop_in_sese (bb, region);
1943 gcond *stmt = single_pred_cond_non_loop_exit (bb);
1945 if (stmt)
1947 edge e = single_pred_edge (bb);
1949 conditions.safe_push (stmt);
1951 if (e->flags & EDGE_TRUE_VALUE)
1952 cases.safe_push (stmt);
1953 else
1954 cases.safe_push (NULL);
1957 scop->scop_info->bbs.safe_push (bb);
1959 gimple_poly_bb_p gbb = try_generate_gimple_bb (scop, bb);
1960 if (!gbb)
1961 return NULL;
1963 GBB_CONDITIONS (gbb) = conditions.copy ();
1964 GBB_CONDITION_CASES (gbb) = cases.copy ();
1966 poly_bb_p pbb = new_poly_bb (scop, gbb);
1967 scop->pbbs.safe_push (pbb);
1969 int i;
1970 data_reference_p dr;
1971 FOR_EACH_VEC_ELT (gbb->data_refs, i, dr)
1973 DEBUG_PRINT (dp << "Adding memory ";
1974 if (dr->is_read)
1975 dp << "read: ";
1976 else
1977 dp << "write: ";
1978 print_generic_expr (dump_file, dr->ref);
1979 dp << "\nFrom stmt: ";
1980 print_gimple_stmt (dump_file, dr->stmt, 0));
1982 scop->drs.safe_push (dr_info (dr, pbb));
1985 return NULL;
1988 /* Call-back for dom_walk executed after visiting the dominated
1989 blocks. */
1991 void
1992 gather_bbs::after_dom_children (basic_block bb)
1994 if (!bb_in_sese_p (bb, scop->scop_info->region))
1995 return;
1997 if (single_pred_cond_non_loop_exit (bb))
1999 conditions.pop ();
2000 cases.pop ();
2005 /* Compute sth like an execution order, dominator order with first executing
2006 edges that stay inside the current loop, delaying processing exit edges. */
2008 static vec<unsigned> order;
2010 static void
2011 get_order (scop_p scop, basic_block bb, vec<unsigned> *order, unsigned *dfs_num)
2013 if (! bb_in_sese_p (bb, scop->scop_info->region))
2014 return;
2016 (*order)[bb->index] = (*dfs_num)++;
2017 for (basic_block son = first_dom_son (CDI_DOMINATORS, bb);
2018 son;
2019 son = next_dom_son (CDI_DOMINATORS, son))
2020 if (flow_bb_inside_loop_p (bb->loop_father, son))
2021 get_order (scop, son, order, dfs_num);
2022 for (basic_block son = first_dom_son (CDI_DOMINATORS, bb);
2023 son;
2024 son = next_dom_son (CDI_DOMINATORS, son))
2025 if (! flow_bb_inside_loop_p (bb->loop_father, son))
2026 get_order (scop, son, order, dfs_num);
2029 /* Helper for qsort, sorting after order above. */
2031 static int
2032 cmp_pbbs (const void *pa, const void *pb)
2034 poly_bb_p bb1 = *((const poly_bb_p *)pa);
2035 poly_bb_p bb2 = *((const poly_bb_p *)pb);
2036 if (order[bb1->black_box->bb->index] < order[bb2->black_box->bb->index])
2037 return -1;
2038 else if (order[bb1->black_box->bb->index] > order[bb2->black_box->bb->index])
2039 return 1;
2040 else
2041 return 0;
2044 /* Find Static Control Parts (SCoP) in the current function and pushes
2045 them to SCOPS. */
2047 void
2048 build_scops (vec<scop_p> *scops)
2050 if (dump_file)
2051 dp.set_dump_file (dump_file);
2053 canonicalize_loop_closed_ssa_form ();
2055 scop_detection sb;
2056 sb.build_scop_depth (scop_detection::invalid_sese, current_loops->tree_root);
2058 /* Now create scops from the lightweight SESEs. */
2059 vec<sese_l> scops_l = sb.get_scops ();
2060 int i;
2061 sese_l *s;
2062 FOR_EACH_VEC_ELT (scops_l, i, s)
2064 scop_p scop = new_scop (s->entry, s->exit);
2066 /* Record all basic blocks and their conditions in REGION. */
2067 gather_bbs (CDI_DOMINATORS, scop).walk (s->entry->dest);
2069 /* domwalk does not fulfil our code-generations constraints on the
2070 order of pbb which is to produce sth like execution order, delaying
2071 exection of loop exit edges. So compute such order and sort after
2072 that. */
2073 order.create (last_basic_block_for_fn (cfun));
2074 order.quick_grow (last_basic_block_for_fn (cfun));
2075 unsigned dfs_num = 0;
2076 get_order (scop, s->entry->dest, &order, &dfs_num);
2077 scop->pbbs.qsort (cmp_pbbs);
2078 order.release ();
2080 if (! build_alias_set (scop))
2082 DEBUG_PRINT (dp << "[scop-detection-fail] cannot handle dependences\n");
2083 free_scop (scop);
2084 continue;
2087 /* Do not optimize a scop containing only PBBs that do not belong
2088 to any loops. */
2089 if (sb.nb_pbbs_in_loops (scop) == 0)
2091 DEBUG_PRINT (dp << "[scop-detection-fail] no data references.\n");
2092 free_scop (scop);
2093 continue;
2096 unsigned max_arrays = PARAM_VALUE (PARAM_GRAPHITE_MAX_ARRAYS_PER_SCOP);
2097 if (scop->drs.length () >= max_arrays)
2099 DEBUG_PRINT (dp << "[scop-detection-fail] too many data references: "
2100 << scop->drs.length ()
2101 << " is larger than --param graphite-max-arrays-per-scop="
2102 << max_arrays << ".\n");
2103 free_scop (scop);
2104 continue;
2107 find_scop_parameters (scop);
2108 graphite_dim_t max_dim = PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS);
2110 if (scop_nb_params (scop) > max_dim)
2112 DEBUG_PRINT (dp << "[scop-detection-fail] too many parameters: "
2113 << scop_nb_params (scop)
2114 << " larger than --param graphite-max-nb-scop-params="
2115 << max_dim << ".\n");
2116 free_scop (scop);
2117 continue;
2120 scops->safe_push (scop);
2123 DEBUG_PRINT (dp << "number of SCoPs: " << (scops ? scops->length () : 0););
2126 #endif /* HAVE_isl */