Wmisleading-indentation: add reproducer for PR c/70085
[official-gcc.git] / gcc / graphite-scop-detection.c
blob03b1c49d728eb28d2050ad03bd8fcfe8060c58d9
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)
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 int tmp_dump_flags = dump_flags;
100 dump_flags = 0;
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 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
283 of PHI. */
285 static void
286 remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
288 gimple *use_stmt;
289 use_operand_p use_p;
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. */
316 static void
317 make_close_phi_nodes_unique (basic_block bb)
319 gphi_iterator psi;
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. */
330 gsi_next (&gsi);
331 while (!gsi_end_p (gsi))
332 if (same_close_phi_node (phi, gsi.phi ()))
333 remove_duplicate_close_phi (phi, &gsi);
334 else
335 gsi_next (&gsi);
339 /* Return true when NAME is defined in LOOP. */
341 static bool
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. */
350 static void
351 canonicalize_loop_closed_ssa (loop_p loop)
353 edge e = single_exit (loop);
354 basic_block bb;
356 if (!e || e->flags & EDGE_ABNORMAL)
357 return;
359 bb = e->dest;
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);
367 else
369 gphi_iterator psi;
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 ();
379 unsigned i;
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);
385 use_operand_p use_p;
386 gphi *close_phi;
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))
391 continue;
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),
398 UNKNOWN_LOCATION);
399 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
400 replace_exp (use_p, res);
401 update_stmt (phi);
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
426 in the loop.
428 - the basic block containing the close phi nodes does not contain
429 other statements.
431 - there exist only one phi node per definition in the loop.
434 static void
435 canonicalize_loop_closed_ssa_form (void)
437 checking_verify_loop_closed_ssa (true);
439 loop_p loop;
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. */
453 static bool
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);
458 gsi_next (&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)
465 return false;
468 return true;
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. */
475 static gcond *
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;
482 gimple *stmt;
484 if (loop_depth (pred->loop_father) > loop_depth (bb->loop_father))
485 return NULL;
487 stmt = last_stmt (pred);
489 if (stmt && gimple_code (stmt) == GIMPLE_COND)
490 return as_a<gcond *> (stmt);
493 return NULL;
496 namespace
499 /* Build the maximal scop containing LOOPs and add it to SCOPS. */
501 class scop_detection
503 public:
504 scop_detection () : scops (vNULL) {}
506 ~scop_detection ()
508 scops.release ();
511 /* A marker for invalid sese_l. */
512 static sese_l invalid_sese;
514 /* Return the SCOPS in this SCOP_DETECTION. */
516 vec<sese_l>
517 get_scops ()
519 return scops;
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
556 END. */
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
580 as a valid scop. */
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,
632 tree expr);
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
640 of PHI. */
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
651 Graphite. */
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);
661 private:
662 vec<sese_l> scops;
665 sese_l scop_detection::invalid_sese (NULL, NULL);
667 /* Return an sese_l around the LOOP. */
669 sese_l
670 scop_detection::get_sese (loop_p loop)
672 if (!loop)
673 return invalid_sese;
675 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS))
676 return invalid_sese;
677 edge scop_end = single_exit (loop);
678 if (!scop_end)
679 return invalid_sese;
680 edge scop_begin = loop_preheader_edge (loop);
681 sese_l s (scop_begin, scop_end);
682 return s;
685 /* Return the closest dominator with a single entry edge. */
687 edge
688 scop_detection::get_nearest_dom_with_single_entry (basic_block dom)
690 if (!dom->preds)
691 return NULL;
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
695 entry. */
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))
706 return e1;
707 if (l != l2 && l == l1
708 && dominated_by_p (CDI_DOMINATORS, e1->src, e2->src))
709 return e2;
712 while (dom->preds->length () != 1)
714 if (dom->preds->length () < 1)
715 return NULL;
716 dom = get_immediate_dominator (CDI_DOMINATORS, dom);
717 if (!dom->preds)
718 return NULL;
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. */
726 edge
727 scop_detection::get_nearest_pdom_with_single_exit (basic_block pdom)
729 if (!pdom->succs)
730 return NULL;
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
734 exit. */
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))
746 return e1;
747 if (l != l2 && l == l1
748 && dominated_by_p (CDI_POST_DOMINATORS, e1->dest, e2->dest))
749 return e2;
752 while (pdom->succs->length () != 1)
754 if (pdom->succs->length () < 1)
755 return NULL;
756 pdom = get_immediate_dominator (CDI_POST_DOMINATORS, pdom);
757 if (!pdom->succs)
758 return NULL;
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. */
767 sese_l
768 scop_detection::merge_sese (sese_l first, sese_l second) const
770 /* In the trivial case first/second may be NULL. */
771 if (!first)
772 return second;
773 if (!second)
774 return first;
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))
793 return invalid_sese;
795 basic_block pdom = nearest_common_dominator (CDI_POST_DOMINATORS,
796 get_exit_bb (first),
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))
803 return invalid_sese;
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))
816 return invalid_sese;
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");
827 return invalid_sese;
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);
839 else
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));
844 return invalid_sese;
848 /* Analyze all the BBs in new sese. */
849 if (harmful_loop_in_region (combined))
850 return invalid_sese;
852 DEBUG_PRINT (dp << "[merged-sese] s1: "; print_sese (dump_file, combined));
854 return combined;
857 /* Build scop outer->inner if possible. */
859 sese_l
860 scop_detection::build_scop_depth (sese_l s, loop_p loop)
862 if (!loop)
863 return s;
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));
869 if (!s2)
871 /* s might be a valid scop, so return it and start analyzing from the
872 adjacent loop. */
873 build_scop_depth (invalid_sese, loop->next);
874 return s;
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. */
885 sese_l
886 scop_detection::build_scop_breadth (sese_l s1, loop_p loop)
888 if (!loop)
889 return s1;
890 DEBUG_PRINT (dp << "[Breadth loop_" << loop->num << "]\n");
891 gcc_assert (s1);
893 loop_p l = loop;
894 sese_l s2 = build_scop_depth (invalid_sese, l->next);
895 if (!s2)
897 if (s1)
898 add_scop (s1);
899 return s1;
902 sese_l combined = merge_sese (s1, s2);
904 if (combined)
905 s1 = combined;
906 else
907 add_scop (s2);
909 if (s1)
910 add_scop (s1);
911 return s1;
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. */
918 bool
919 scop_detection::can_represent_loop_1 (loop_p loop, sese_l scop)
921 tree niter;
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
934 Graphite. */
936 bool
937 scop_detection::can_represent_loop (loop_p loop, sese_l scop)
939 if (!can_represent_loop_1 (loop, scop))
940 return false;
941 if (loop->inner && !can_represent_loop (loop->inner, scop))
942 return false;
943 if (loop->next && !can_represent_loop (loop->next, scop))
944 return false;
946 return true;
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. */
953 bool
954 scop_detection::loop_is_valid_in_scop (loop_p loop, sese_l scop) const
956 if (!scop)
957 return false;
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");
963 return false;
966 if (!can_represent_loop (loop, scop))
968 DEBUG_PRINT (dp << "[scop-detection-fail] cannot represent loop_"
969 << loop->num << "\n");
970 return false;
973 if (loop_body_is_valid_scop (loop, scop))
975 DEBUG_PRINT (dp << "[valid-scop] loop_" << loop->num
976 << " is a valid scop.\n");
977 return true;
979 return false;
982 /* Return true when BEGIN is the preheader edge of a loop with a single exit
983 END. */
985 bool
986 scop_detection::region_has_one_loop (sese_l s)
988 edge begin = s.entry;
989 edge end = s.exit;
990 /* Check for a single perfectly nested loop. */
991 if (begin->dest->loop_father->inner)
992 return false;
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. */
1000 void
1001 scop_detection::add_scop (sese_l s)
1003 gcc_assert (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));
1010 return;
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));
1018 return;
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. */
1037 bool
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);
1054 int i;
1055 basic_block bb;
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))
1063 continue;
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))
1067 continue;
1069 /* The basic block should not be part of an irreducible loop. */
1070 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1072 dom.release ();
1073 BITMAP_FREE (loops);
1074 return true;
1077 /* Check for unstructured control flow: CFG not generated by structured
1078 if-then-else. */
1079 if (bb->succs->length () > 1)
1081 edge e;
1082 edge_iterator ei;
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))
1086 return true;
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);
1093 else
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))
1100 dom.release ();
1101 BITMAP_FREE (loops);
1102 return true;
1108 /* Go through all loops and check that they are still valid in the combined
1109 scop. */
1110 unsigned j;
1111 bitmap_iterator bi;
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))
1119 dom.release ();
1120 BITMAP_FREE (loops);
1121 return true;
1125 dom.release ();
1126 BITMAP_FREE (loops);
1127 return false;
1130 /* Returns true if S1 subsumes/surrounds S2. */
1131 bool
1132 scop_detection::subsumes (sese_l s1, sese_l s2)
1134 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
1135 get_entry_bb (s1))
1136 && dominated_by_p (CDI_POST_DOMINATORS, s2.exit->dest,
1137 s1.exit->dest))
1138 return true;
1139 return false;
1142 /* Remove a SCoP which is subsumed by S1. */
1143 void
1144 scop_detection::remove_subscops (sese_l s1)
1146 int j;
1147 sese_l *s2;
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. */
1162 bool
1163 scop_detection::intersects (sese_l s1, sese_l s2)
1165 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
1166 get_entry_bb (s1))
1167 && !dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
1168 get_exit_bb (s1)))
1169 return true;
1170 if ((s1.exit == s2.entry) || (s2.exit == s1.entry))
1171 return true;
1173 return false;
1176 /* Remove one of the scops when it intersects with any other. */
1178 void
1179 scop_detection::remove_intersecting_scops (sese_l s1)
1181 int j;
1182 sese_l *s2;
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. */
1198 bool
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));
1207 case MULT_EXPR:
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));
1211 else
1212 return graphite_can_represent_init (TREE_OPERAND (e, 1))
1213 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
1215 case PLUS_EXPR:
1216 case POINTER_PLUS_EXPR:
1217 case MINUS_EXPR:
1218 return graphite_can_represent_init (TREE_OPERAND (e, 0))
1219 && graphite_can_represent_init (TREE_OPERAND (e, 1));
1221 case NEGATE_EXPR:
1222 case BIT_NOT_EXPR:
1223 CASE_CONVERT:
1224 case NON_LVALUE_EXPR:
1225 return graphite_can_represent_init (TREE_OPERAND (e, 0));
1227 default:
1228 break;
1231 return true;
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. */
1246 bool
1247 scop_detection::graphite_can_represent_scev (tree scev)
1249 if (chrec_contains_undetermined (scev))
1250 return false;
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)
1258 return false;
1260 switch (TREE_CODE (scev))
1262 case NEGATE_EXPR:
1263 case BIT_NOT_EXPR:
1264 CASE_CONVERT:
1265 case NON_LVALUE_EXPR:
1266 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
1268 case PLUS_EXPR:
1269 case POINTER_PLUS_EXPR:
1270 case MINUS_EXPR:
1271 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
1272 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
1274 case MULT_EXPR:
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
1287 represented. */
1288 if (!evolution_function_right_is_integer_cst (scev)
1289 || !graphite_can_represent_init (scev))
1290 return false;
1291 return graphite_can_represent_scev (CHREC_LEFT (scev));
1293 default:
1294 break;
1297 /* Only affine functions can be represented. */
1298 if (tree_contains_chrecs (scev, NULL) || !scev_is_linear_expression (scev))
1299 return false;
1301 return true;
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. */
1310 bool
1311 scop_detection::graphite_can_represent_expr (sese_l scop, loop_p loop,
1312 tree expr)
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. */
1321 bool
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);
1330 int j;
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);
1339 return false;
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);
1351 return false;
1354 ref = TREE_OPERAND (ref, 0);
1358 free_data_refs (drs);
1359 return true;
1362 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
1363 Calls have side-effects, except those to const or pure
1364 functions. */
1366 static bool
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));
1377 return true;
1379 return false;
1382 /* Returns true if STMT can be represented in polyhedral model. LABEL,
1383 simple COND stmts, pure calls, and assignments can be repesented. */
1385 bool
1386 scop_detection::graphite_can_represent_stmt (sese_l scop, gimple *stmt,
1387 basic_block bb)
1389 loop_p loop = bb->loop_father;
1390 switch (gimple_code (stmt))
1392 case GIMPLE_LABEL:
1393 return true;
1395 case GIMPLE_COND:
1397 /* We can handle all binary comparisons. Inequalities are
1398 also supported as they can be represented with union of
1399 polyhedra. */
1400 enum tree_code code = gimple_cond_code (stmt);
1401 if (!(code == LT_EXPR
1402 || code == GT_EXPR
1403 || code == LE_EXPR
1404 || code == GE_EXPR
1405 || code == EQ_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));
1412 return false;
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));
1426 return false;
1430 return true;
1433 case GIMPLE_ASSIGN:
1434 case GIMPLE_CALL:
1435 return true;
1437 default:
1438 /* These nodes cut a new scope. */
1439 DEBUG_PRINT (
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));
1443 return false;
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. */
1453 bool
1454 scop_detection::stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
1455 basic_block bb) const
1457 gcc_assert (scop);
1459 if (is_gimple_debug (stmt))
1460 return true;
1462 if (stmt_has_side_effects (stmt))
1463 return false;
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););
1470 return false;
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. */
1481 bool
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))
1488 return true;
1490 return false;
1493 /* Return true when the body of LOOP has statements that can be represented as a
1494 valid scop. */
1496 bool
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");
1503 return false;
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");
1510 return false;
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))
1520 free (bbs);
1521 return false;
1524 free (bbs);
1526 if (loop->inner)
1528 loop = loop->inner;
1529 while (loop)
1531 if (!loop_body_is_valid_scop (loop, scop))
1532 return false;
1533 loop = loop->next;
1537 return true;
1540 /* Returns the number of pbbs that are in loops contained in SCOP. */
1543 scop_detection::nb_pbbs_in_loops (scop_p scop)
1545 int i;
1546 poly_bb_p pbb;
1547 int res = 0;
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))
1551 res++;
1553 return res;
1556 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
1557 Otherwise returns -1. */
1559 static inline int
1560 parameter_index_in_region_1 (tree name, sese_info_p region)
1562 int i;
1563 tree p;
1565 gcc_assert (TREE_CODE (name) == SSA_NAME);
1567 FOR_EACH_VEC_ELT (region->params, i, p)
1568 if (p == name)
1569 return i;
1571 return -1;
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. */
1578 static int
1579 parameter_index_in_region (tree name, sese_info_p region)
1581 int i;
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)
1587 return -1;
1589 if (!invariant_in_sese_p_rec (name, region->region, NULL))
1590 return -1;
1592 i = parameter_index_in_region_1 (name, region);
1593 if (i != -1)
1594 return i;
1596 i = region->params.length ();
1597 region->params.safe_push (name);
1598 return i;
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
1604 parameters. */
1606 static void
1607 scan_tree_for_params (sese_info_p s, tree e)
1609 if (e == chrec_dont_know)
1610 return;
1612 switch (TREE_CODE (e))
1614 case POLYNOMIAL_CHREC:
1615 scan_tree_for_params (s, CHREC_LEFT (e));
1616 break;
1618 case MULT_EXPR:
1619 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
1620 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1621 else
1622 scan_tree_for_params (s, TREE_OPERAND (e, 1));
1623 break;
1625 case PLUS_EXPR:
1626 case POINTER_PLUS_EXPR:
1627 case MINUS_EXPR:
1628 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1629 scan_tree_for_params (s, TREE_OPERAND (e, 1));
1630 break;
1632 case NEGATE_EXPR:
1633 case BIT_NOT_EXPR:
1634 CASE_CONVERT:
1635 case NON_LVALUE_EXPR:
1636 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1637 break;
1639 case SSA_NAME:
1640 parameter_index_in_region (e, s);
1641 break;
1643 case INTEGER_CST:
1644 case ADDR_EXPR:
1645 case REAL_CST:
1646 case COMPLEX_CST:
1647 case VECTOR_CST:
1648 break;
1650 default:
1651 gcc_unreachable ();
1652 break;
1656 /* Find parameters with respect to REGION in BB. We are looking in memory
1657 access functions, conditions and loop bounds. */
1659 static void
1660 find_params_in_bb (sese_info_p region, gimple_poly_bb_p gbb)
1662 /* Find parameters in the access functions of data references. */
1663 int i;
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. */
1670 gimple *stmt;
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. */
1687 static void
1688 find_scop_parameters (scop_p scop)
1690 unsigned i;
1691 sese_info_p region = scop->scop_info;
1692 struct loop *loop;
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))
1700 continue;
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. */
1707 poly_bb_p pbb;
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. */
1717 static void
1718 build_cross_bb_scalars_def (scop_p scop, tree def, basic_block def_bb,
1719 vec<tree> *writes)
1721 gcc_assert (def);
1722 if (!is_gimple_reg (def))
1723 return;
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))
1728 return;
1730 gimple *use_stmt;
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. */
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))
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
1774 SCOP. */
1776 static void
1777 graphite_find_cross_bb_scalar_vars (scop_p scop, gimple *stmt,
1778 vec<scalar_use> *reads, vec<tree> *writes)
1780 tree def;
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);
1788 else
1789 return;
1792 build_cross_bb_scalars_def (scop, def, gimple_bb (stmt), writes);
1794 ssa_op_iter iter;
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
1804 information. */
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))
1818 loop = nest;
1820 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1821 gsi_next (&gsi))
1823 gimple *stmt = gsi_stmt (gsi);
1824 if (is_gimple_debug (stmt))
1825 continue;
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);
1832 gsi_next (&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 ())
1837 return NULL;
1839 return new_gimple_poly_bb (bb, drs, reads, writes);
1842 /* Compute alias-sets for all data references in DRS. */
1844 static void
1845 build_alias_set (scop_p scop)
1847 int num_vertices = scop->drs.length ();
1848 struct graph *g = new_graph (num_vertices);
1849 dr_info *dr1, *dr2;
1850 int i, j;
1851 int *all_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))
1857 add_edge (g, i, j);
1858 add_edge (g, j, i);
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;
1871 free_graph (g);
1874 /* Gather BBs and conditions for a SCOP. */
1875 class gather_bbs : public dom_walker
1877 public:
1878 gather_bbs (cdi_direction, scop_p);
1880 virtual edge before_dom_children (basic_block);
1881 virtual void after_dom_children (basic_block);
1883 private:
1884 auto_vec<gimple *, 3> conditions, cases;
1885 scop_p scop;
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. */
1895 static void
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))
1901 bool found = false;
1902 loop_p loop0;
1903 int j;
1904 FOR_EACH_VEC_ELT (region->loop_nest, j, loop0)
1905 if (father == loop0)
1907 found = true;
1908 break;
1910 if (!found)
1911 region->loop_nest.safe_push (father);
1915 /* Call-back for dom_walk executed before visiting the dominated
1916 blocks. */
1918 edge
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))
1923 return NULL;
1925 record_loop_in_sese (bb, region);
1927 gcond *stmt = single_pred_cond_non_loop_exit (bb);
1929 if (stmt)
1931 edge e = single_pred_edge (bb);
1933 conditions.safe_push (stmt);
1935 if (e->flags & EDGE_TRUE_VALUE)
1936 cases.safe_push (stmt);
1937 else
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);
1944 if (!gbb)
1945 return NULL;
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);
1953 int i;
1954 data_reference_p dr;
1955 FOR_EACH_VEC_ELT (gbb->data_refs, i, dr)
1957 DEBUG_PRINT (dp << "Adding memory ";
1958 if (dr->is_read)
1959 dp << "read: ";
1960 else
1961 dp << "write: ";
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));
1969 return NULL;
1972 /* Call-back for dom_walk executed after visiting the dominated
1973 blocks. */
1975 void
1976 gather_bbs::after_dom_children (basic_block bb)
1978 if (!bb_in_sese_p (bb, scop->scop_info->region))
1979 return;
1981 if (single_pred_cond_non_loop_exit (bb))
1983 conditions.pop ();
1984 cases.pop ();
1988 /* Find Static Control Parts (SCoP) in the current function and pushes
1989 them to SCOPS. */
1991 void
1992 build_scops (vec<scop_p> *scops)
1994 if (dump_file)
1995 dp.set_dump_file (dump_file);
1997 canonicalize_loop_closed_ssa_form ();
1999 scop_detection sb;
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 ();
2004 int i;
2005 sese_l *s;
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
2016 to any loops. */
2017 if (sb.nb_pbbs_in_loops (scop) == 0)
2019 DEBUG_PRINT (dp << "[scop-detection-fail] no data references.\n");
2020 free_scop (scop);
2021 continue;
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");
2031 free_scop (scop);
2032 continue;
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");
2044 free_scop (scop);
2045 continue;
2048 scops->safe_push (scop);
2051 DEBUG_PRINT (dp << "number of SCoPs: " << (scops ? scops->length () : 0););
2054 #endif /* HAVE_isl */