PR c/68024
[official-gcc.git] / gcc / graphite-scop-detection.c
blob6c0987dd0297438e0969c6621fb02e757d05098b
1 /* Detection of Static Control Parts (SCoP) for Graphite.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.com> and
4 Tobias Grosser <grosser@fim.uni-passau.de>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
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 #include "config.h"
24 #ifdef HAVE_isl
25 /* Workaround for GMP 5.1.3 bug, see PR56019. */
26 #include <stddef.h>
28 #include <isl/constraint.h>
29 #include <isl/set.h>
30 #include <isl/map.h>
31 #include <isl/union_map.h>
33 #include "system.h"
34 #include "coretypes.h"
35 #include "backend.h"
36 #include "cfghooks.h"
37 #include "domwalk.h"
38 #include "params.h"
39 #include "tree.h"
40 #include "gimple.h"
41 #include "ssa.h"
42 #include "fold-const.h"
43 #include "gimple-iterator.h"
44 #include "tree-cfg.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
49 #include "tree-ssa.h"
50 #include "cfgloop.h"
51 #include "tree-data-ref.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-pass.h"
54 #include "graphite-poly.h"
55 #include "tree-ssa-propagate.h"
56 #include "graphite-scop-detection.h"
57 #include "gimple-pretty-print.h"
59 class debug_printer
61 private:
62 FILE *dump_file;
64 public:
65 void
66 set_dump_file (FILE *f)
68 gcc_assert (f);
69 dump_file = f;
72 friend debug_printer &
73 operator<< (debug_printer &output, int i)
75 fprintf (output.dump_file, "%d", i);
76 return output;
78 friend debug_printer &
79 operator<< (debug_printer &output, const char *s)
81 fprintf (output.dump_file, "%s", s);
82 return output;
84 } dp;
86 #define DEBUG_PRINT(args) do \
87 { \
88 if (dump_file && (dump_flags & TDF_DETAILS)) { args; } \
89 } while (0);
92 /* Return true if BB is empty, contains only DEBUG_INSNs. */
94 static bool
95 trivially_empty_bb_p (basic_block bb)
97 gimple_stmt_iterator gsi;
99 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
100 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_DEBUG)
101 return false;
103 return true;
106 /* Returns true when P1 and P2 are close phis with the same
107 argument. */
109 static inline bool
110 same_close_phi_node (gphi *p1, gphi *p2)
112 return operand_equal_p (gimple_phi_arg_def (p1, 0),
113 gimple_phi_arg_def (p2, 0), 0);
116 static void make_close_phi_nodes_unique (basic_block bb);
118 /* Remove the close phi node at GSI and replace its rhs with the rhs
119 of PHI. */
121 static void
122 remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
124 gimple *use_stmt;
125 use_operand_p use_p;
126 imm_use_iterator imm_iter;
127 tree res = gimple_phi_result (phi);
128 tree def = gimple_phi_result (gsi->phi ());
130 gcc_assert (same_close_phi_node (phi, gsi->phi ()));
132 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
134 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
135 SET_USE (use_p, res);
137 update_stmt (use_stmt);
139 /* It is possible that we just created a duplicate close-phi
140 for an already-processed containing loop. Check for this
141 case and clean it up. */
142 if (gimple_code (use_stmt) == GIMPLE_PHI
143 && gimple_phi_num_args (use_stmt) == 1)
144 make_close_phi_nodes_unique (gimple_bb (use_stmt));
147 remove_phi_node (gsi, true);
150 /* Removes all the close phi duplicates from BB. */
152 static void
153 make_close_phi_nodes_unique (basic_block bb)
155 gphi_iterator psi;
157 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
159 gphi_iterator gsi = psi;
160 gphi *phi = psi.phi ();
162 /* At this point, PHI should be a close phi in normal form. */
163 gcc_assert (gimple_phi_num_args (phi) == 1);
165 /* Iterate over the next phis and remove duplicates. */
166 gsi_next (&gsi);
167 while (!gsi_end_p (gsi))
168 if (same_close_phi_node (phi, gsi.phi ()))
169 remove_duplicate_close_phi (phi, &gsi);
170 else
171 gsi_next (&gsi);
175 /* Transforms LOOP to the canonical loop closed SSA form. */
177 static void
178 canonicalize_loop_closed_ssa (loop_p loop)
180 edge e = single_exit (loop);
181 basic_block bb;
183 if (!e || e->flags & EDGE_ABNORMAL)
184 return;
186 bb = e->dest;
188 if (single_pred_p (bb))
190 e = split_block_after_labels (bb);
191 DEBUG_PRINT (dp << "\nSplitting bb_" << bb->index);
192 make_close_phi_nodes_unique (e->src);
194 else
196 gphi_iterator psi;
197 basic_block close = split_edge (e);
199 e = single_succ_edge (close);
200 DEBUG_PRINT (dp << "\nSplitting edge (" << e->src->index << ","
201 << e->dest->index << ")\n");
203 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
205 gphi *phi = psi.phi ();
206 unsigned i;
208 for (i = 0; i < gimple_phi_num_args (phi); i++)
209 if (gimple_phi_arg_edge (phi, i) == e)
211 tree res, arg = gimple_phi_arg_def (phi, i);
212 use_operand_p use_p;
213 gphi *close_phi;
215 if (TREE_CODE (arg) != SSA_NAME)
216 continue;
218 close_phi = create_phi_node (NULL_TREE, close);
219 res = create_new_def_for (arg, close_phi,
220 gimple_phi_result_ptr (close_phi));
221 add_phi_arg (close_phi, arg,
222 gimple_phi_arg_edge (close_phi, 0),
223 UNKNOWN_LOCATION);
224 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
225 replace_exp (use_p, res);
226 update_stmt (phi);
230 make_close_phi_nodes_unique (close);
233 /* The code above does not properly handle changes in the post dominance
234 information (yet). */
235 recompute_all_dominators ();
238 /* Converts the current loop closed SSA form to a canonical form
239 expected by the Graphite code generation.
241 The loop closed SSA form has the following invariant: a variable
242 defined in a loop that is used outside the loop appears only in the
243 phi nodes in the destination of the loop exit. These phi nodes are
244 called close phi nodes.
246 The canonical loop closed SSA form contains the extra invariants:
248 - when the loop contains only one exit, the close phi nodes contain
249 only one argument. That implies that the basic block that contains
250 the close phi nodes has only one predecessor, that is a basic block
251 in the loop.
253 - the basic block containing the close phi nodes does not contain
254 other statements.
256 - there exist only one phi node per definition in the loop.
259 static void
260 canonicalize_loop_closed_ssa_form (void)
262 loop_p loop;
264 #ifdef ENABLE_CHECKING
265 verify_loop_closed_ssa (true);
266 #endif
268 FOR_EACH_LOOP (loop, 0)
269 canonicalize_loop_closed_ssa (loop);
271 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
272 update_ssa (TODO_update_ssa);
274 #ifdef ENABLE_CHECKING
275 verify_loop_closed_ssa (true);
276 #endif
279 /* Can all ivs be represented by a signed integer?
280 As ISL might generate negative values in its expressions, signed loop ivs
281 are required in the backend. */
283 static bool
284 loop_ivs_can_be_represented (loop_p loop)
286 unsigned type_long_long = TYPE_PRECISION (long_long_integer_type_node);
287 for (gphi_iterator psi = gsi_start_phis (loop->header); !gsi_end_p (psi);
288 gsi_next (&psi))
290 gphi *phi = psi.phi ();
291 tree res = PHI_RESULT (phi);
292 tree type = TREE_TYPE (res);
294 if (TYPE_UNSIGNED (type) && TYPE_PRECISION (type) >= type_long_long)
295 return false;
298 return true;
301 /* Returns a COND_EXPR statement when BB has a single predecessor, the
302 edge between BB and its predecessor is not a loop exit edge, and
303 the last statement of the single predecessor is a COND_EXPR. */
305 static gcond *
306 single_pred_cond_non_loop_exit (basic_block bb)
308 if (single_pred_p (bb))
310 edge e = single_pred_edge (bb);
311 basic_block pred = e->src;
312 gimple *stmt;
314 if (loop_depth (pred->loop_father) > loop_depth (bb->loop_father))
315 return NULL;
317 stmt = last_stmt (pred);
319 if (stmt && gimple_code (stmt) == GIMPLE_COND)
320 return as_a<gcond *> (stmt);
323 return NULL;
326 namespace
329 /* Build the maximal scop containing LOOPs and add it to SCOPS. */
331 class scop_detection
333 public:
334 scop_detection () : scops (vNULL) {}
336 /* A marker for invalid sese_l. */
337 static sese_l invalid_sese;
339 /* Return the SCOPS in this SCOP_DETECTION. */
341 vec<sese_l>
342 get_scops ()
344 return scops;
347 /* Return an sese_l around the LOOP. */
349 sese_l get_sese (loop_p loop);
351 /* Return the closest dominator with a single entry edge. In case of a
352 back-loop the back-edge is not counted. */
354 static edge get_nearest_dom_with_single_entry (basic_block dom);
356 /* Return the closest post-dominator with a single exit edge. In case of a
357 back-loop the back-edge is not counted. */
359 static edge get_nearest_pdom_with_single_exit (basic_block dom);
361 /* Print S to FILE. */
363 static void print_sese (FILE *file, sese_l s);
365 /* Merge scops at same loop depth and returns the new sese.
366 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
368 sese_l merge_sese (sese_l first, sese_l second) const;
370 /* Build scop outer->inner if possible. */
372 sese_l build_scop_depth (sese_l s, loop_p loop);
374 /* If loop and loop->next are valid scops, try to merge them. */
376 sese_l build_scop_breadth (sese_l s1, loop_p loop);
378 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
379 region of code that can be represented in the polyhedral model. SCOP
380 defines the region we analyse. */
382 bool loop_is_valid_scop (loop_p loop, sese_l scop) const;
384 /* Return true when BEGIN is the preheader edge of a loop with a single exit
385 END. */
387 static bool region_has_one_loop (sese_l s);
389 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
391 void add_scop (sese_l s);
393 /* Returns true if S1 subsumes/surrounds S2. */
394 static bool subsumes (sese_l s1, sese_l s2);
396 /* Remove a SCoP which is subsumed by S1. */
397 void remove_subscops (sese_l s1);
399 /* Returns true if S1 intersects with S2. Since we already know that S1 does
400 not subsume S2 or vice-versa, we only check for entry bbs. */
402 static bool intersects (sese_l s1, sese_l s2);
404 /* Remove one of the scops when it intersects with any other. */
406 void remove_intersecting_scops (sese_l s1);
408 /* Return true when the body of LOOP has statements that can be represented
409 as a valid scop. */
411 bool loop_body_is_valid_scop (loop_p loop, sese_l scop) const;
413 /* Return true when BB contains a harmful operation for a scop: that
414 can be a function call with side effects, the induction variables
415 are not linear with respect to SCOP, etc. The current open
416 scop should end before this statement. */
418 bool harmful_stmt_in_bb (sese_l scop, basic_block bb) const;
420 /* Return true when a statement in SCOP cannot be represented by Graphite.
421 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
422 Limit the number of bbs between adjacent loops to
423 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
425 bool harmful_stmt_in_region (sese_l scop) const;
427 /* Return true only when STMT is simple enough for being handled by Graphite.
428 This depends on SCOP, as the parameters are initialized relatively to
429 this basic block, the linear functions are initialized based on the
430 outermost loop containing STMT inside the SCOP. BB is the place where we
431 try to evaluate the STMT. */
433 bool stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
434 basic_block bb) const;
436 /* Something like "n * m" is not allowed. */
438 static bool graphite_can_represent_init (tree e);
440 /* Return true when SCEV can be represented in the polyhedral model.
442 An expression can be represented, if it can be expressed as an
443 affine expression. For loops (i, j) and parameters (m, n) all
444 affine expressions are of the form:
446 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
448 1 i + 20 j + (-2) m + 25
450 Something like "i * n" or "n * m" is not allowed. */
452 static bool graphite_can_represent_scev (tree scev);
454 /* Return true when EXPR can be represented in the polyhedral model.
456 This means an expression can be represented, if it is linear with respect
457 to the loops and the strides are non parametric. LOOP is the place where
458 the expr will be evaluated. SCOP defines the region we analyse. */
460 static bool graphite_can_represent_expr (sese_l scop, loop_p loop,
461 tree expr);
463 /* Return true if the data references of STMT can be represented by Graphite.
464 We try to analyze the data references in a loop contained in the SCOP. */
466 static bool stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt);
468 /* Remove the close phi node at GSI and replace its rhs with the rhs
469 of PHI. */
471 static void remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi);
473 /* Returns true when Graphite can represent LOOP in SCOP.
474 FIXME: For the moment, graphite cannot be used on loops that iterate using
475 induction variables that wrap. */
477 static bool can_represent_loop_1 (loop_p loop, sese_l scop);
479 /* Return true when all the loops within LOOP can be represented by
480 Graphite. */
482 static bool can_represent_loop (loop_p loop, sese_l scop);
484 /* Returns the number of pbbs that are in loops contained in SCOP. */
486 static int nb_pbbs_in_loops (scop_p scop);
488 static bool graphite_can_represent_stmt (sese_l, gimple *, basic_block);
490 private:
491 vec<sese_l> scops;
494 sese_l scop_detection::invalid_sese (0);
496 /* Return an sese_l around the LOOP. */
498 sese_l
499 scop_detection::get_sese (loop_p loop)
501 if (!loop)
502 return invalid_sese;
504 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS))
505 return invalid_sese;
506 edge scop_end = single_exit (loop);
507 if (!scop_end)
508 return invalid_sese;
509 edge scop_begin = loop_preheader_edge (loop);
510 sese_l s (scop_begin, scop_end);
511 return s;
514 /* Return the closest dominator with a single entry edge. */
516 edge
517 scop_detection::get_nearest_dom_with_single_entry (basic_block dom)
519 if (!dom->preds)
520 return NULL;
521 /* If e1->src dominates e2->src then e1->src will also dominate dom. */
522 if (dom->preds->length () == 2)
524 edge e1 = (*dom->preds)[0];
525 edge e2 = (*dom->preds)[1];
526 if (dominated_by_p (CDI_DOMINATORS, e2->src, e1->src))
527 return e1;
528 if (dominated_by_p (CDI_DOMINATORS, e1->src, e2->src))
529 return e2;
532 while (dom->preds->length () != 1)
534 if (dom->preds->length () < 1)
535 return NULL;
536 dom = get_immediate_dominator (CDI_DOMINATORS, dom);
537 if (!dom->preds)
538 return NULL;
540 return (*dom->preds)[0];
543 /* Return the closest post-dominator with a single exit edge. In case of a
544 back-loop the back-edge is not counted. */
546 edge
547 scop_detection::get_nearest_pdom_with_single_exit (basic_block dom)
549 if (!dom->succs)
550 return NULL;
551 if (dom->succs->length () == 2)
553 edge e1 = (*dom->succs)[0];
554 edge e2 = (*dom->succs)[1];
555 if (dominated_by_p (CDI_POST_DOMINATORS, e2->dest, e1->dest))
556 return e1;
557 if (dominated_by_p (CDI_POST_DOMINATORS, e1->dest, e2->dest))
558 return e2;
561 while (dom->succs->length () != 1)
563 if (dom->succs->length () < 1)
564 return NULL;
565 dom = get_immediate_dominator (CDI_POST_DOMINATORS, dom);
566 if (!dom->succs)
567 return NULL;
569 return (*dom->succs)[0];
572 /* Print S to FILE. */
574 void
575 scop_detection::print_sese (FILE *file, sese_l s)
577 fprintf (file, "(entry_edge (bb_%d, bb_%d), exit_edge (bb_%d, bb_%d))\n",
578 s.entry->src->index, s.entry->dest->index,
579 s.exit->src->index, s.exit->dest->index);
582 /* Merge scops at same loop depth and returns the new sese.
583 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
585 sese_l
586 scop_detection::merge_sese (sese_l first, sese_l second) const
588 /* In the trivial case first/second may be NULL. */
589 if (!first)
590 return second;
591 if (!second)
592 return first;
594 DEBUG_PRINT (dp << "[try-merging-sese] s1: "; print_sese (dump_file, first);
595 dp << "[try-merging-sese] s2: ";
596 print_sese (dump_file, second));
598 /* Assumption: Both the sese's should be at the same loop depth or one scop
599 should subsume the other like in case of nested loops. */
601 /* Find the common dominators for entry,
602 and common post-dominators for the exit. */
603 basic_block dom = nearest_common_dominator (CDI_DOMINATORS,
604 get_entry_bb (first),
605 get_entry_bb (second));
607 edge entry = get_nearest_dom_with_single_entry (dom);
608 if (!entry)
609 return invalid_sese;
611 basic_block pdom = nearest_common_dominator (CDI_POST_DOMINATORS,
612 get_exit_bb (first),
613 get_exit_bb (second));
614 pdom = nearest_common_dominator (CDI_POST_DOMINATORS, dom, pdom);
616 edge exit = get_nearest_pdom_with_single_exit (pdom);
617 if (!exit)
618 return invalid_sese;
620 sese_l combined (entry, exit);
622 /* FIXME: We could iterate to find the dom which dominates pdom, and pdom
623 which post-dominates dom, until it stabilizes. Also, ENTRY->SRC and
624 EXIT->DEST should be in the same loop nest. */
625 if (!dominated_by_p (CDI_DOMINATORS, pdom, dom)
626 || loop_depth (entry->src->loop_father)
627 != loop_depth (exit->dest->loop_father))
628 return invalid_sese;
630 /* For now we just want to bail out when exit does not post-dominate entry.
631 TODO: We might just add a basic_block at the exit to make exit
632 post-dominate entry (the entire region). */
633 if (!dominated_by_p (CDI_POST_DOMINATORS, get_entry_bb (combined),
634 get_exit_bb (combined))
635 || !dominated_by_p (CDI_DOMINATORS, get_exit_bb (combined),
636 get_entry_bb (combined)))
638 DEBUG_PRINT (dp << "[scop-detection-fail] cannot merge seses.\n");
639 return invalid_sese;
642 /* FIXME: We should remove this piece of code once
643 canonicalize_loop_closed_ssa has been removed, because that function
644 adds a BB with single exit. */
645 if (!trivially_empty_bb_p (get_exit_bb (combined)))
647 /* Find the first empty succ (with single exit) of combined.exit. */
648 basic_block imm_succ = combined.exit->dest;
649 if (single_succ_p (imm_succ) && trivially_empty_bb_p (imm_succ))
650 combined.exit = single_succ_edge (imm_succ);
651 else
653 DEBUG_PRINT (dp << "\n[scop-detection-fail] Discarding SCoP because "
654 << "no single exit (empty succ) for sese exit";
655 print_sese (dump_file, combined));
656 return invalid_sese;
660 /* Analyze all the BBs in new sese. */
661 if (harmful_stmt_in_region (combined))
662 return invalid_sese;
664 DEBUG_PRINT (dp << "[merged-sese] s1: "; print_sese (dump_file, combined));
666 return combined;
669 /* Build scop outer->inner if possible. */
671 sese_l
672 scop_detection::build_scop_depth (sese_l s, loop_p loop)
674 if (!loop)
675 return s;
677 DEBUG_PRINT (dp << "\n[Depth loop_" << loop->num << "]");
678 s = build_scop_depth (s, loop->inner);
680 sese_l s2 = merge_sese (s, get_sese (loop));
681 if (!s2)
683 /* s might be a valid scop, so return it and start analyzing from the
684 adjacent loop. */
685 build_scop_depth (invalid_sese, loop->next);
686 return s;
689 if (!loop_is_valid_scop (loop, s2))
690 return build_scop_depth (invalid_sese, loop->next);
692 return build_scop_breadth (s2, loop);
695 /* If loop and loop->next are valid scops, try to merge them. */
697 sese_l
698 scop_detection::build_scop_breadth (sese_l s1, loop_p loop)
700 if (!loop)
701 return s1;
702 DEBUG_PRINT (dp << "\n[Breadth loop_" << loop->num << "]");
703 gcc_assert (s1);
705 loop_p l = loop;
706 sese_l s2 = build_scop_depth (invalid_sese, l->next);
707 if (!s2)
709 if (s1)
710 add_scop (s1);
711 return s1;
714 sese_l combined = merge_sese (s1, s2);
716 if (combined)
717 s1 = combined;
718 else
719 add_scop (s2);
721 if (s1)
722 add_scop (s1);
723 return s1;
726 /* Returns true when Graphite can represent LOOP in SCOP.
727 FIXME: For the moment, graphite cannot be used on loops that iterate using
728 induction variables that wrap. */
730 bool
731 scop_detection::can_represent_loop_1 (loop_p loop, sese_l scop)
733 tree niter;
734 struct tree_niter_desc niter_desc;
736 return single_exit (loop)
737 && number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
738 && niter_desc.control.no_overflow
739 && (niter = number_of_latch_executions (loop))
740 && !chrec_contains_undetermined (niter)
741 && graphite_can_represent_expr (scop, loop, niter);
744 /* Return true when all the loops within LOOP can be represented by
745 Graphite. */
747 bool
748 scop_detection::can_represent_loop (loop_p loop, sese_l scop)
750 if (!can_represent_loop_1 (loop, scop))
751 return false;
752 if (loop->inner && !can_represent_loop (loop->inner, scop))
753 return false;
754 if (loop->next && !can_represent_loop (loop->next, scop))
755 return false;
757 return true;
760 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
761 region of code that can be represented in the polyhedral model. SCOP
762 defines the region we analyse. */
764 bool
765 scop_detection::loop_is_valid_scop (loop_p loop, sese_l scop) const
767 if (!scop)
768 return false;
770 if (!can_represent_loop (loop, scop))
772 DEBUG_PRINT (dp << "[scop-detection-fail] cannot represent loop_"
773 << loop->num << "\n");
774 return false;
777 if (loop_body_is_valid_scop (loop, scop))
779 DEBUG_PRINT (dp << "[valid-scop] loop_" << loop->num
780 << "is a valid scop.\n");
781 return true;
783 return false;
786 /* Return true when BEGIN is the preheader edge of a loop with a single exit
787 END. */
789 bool
790 scop_detection::region_has_one_loop (sese_l s)
792 edge begin = s.entry;
793 edge end = s.exit;
794 /* Check for a single perfectly nested loop. */
795 if (begin->dest->loop_father->inner)
796 return false;
798 /* Otherwise, check whether we have adjacent loops. */
799 return begin->dest->loop_father == end->src->loop_father;
802 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
804 void
805 scop_detection::add_scop (sese_l s)
807 gcc_assert (s);
809 /* Do not add scops with only one loop. */
810 if (region_has_one_loop (s))
812 DEBUG_PRINT (dp << "\n[scop-detection-fail] Discarding one loop SCoP";
813 print_sese (dump_file, s));
814 return;
817 if (get_exit_bb (s) == EXIT_BLOCK_PTR_FOR_FN (cfun))
819 DEBUG_PRINT (dp << "\n[scop-detection-fail] "
820 << "Discarding SCoP exiting to return";
821 print_sese (dump_file, s));
822 return;
825 /* Remove all the scops which are subsumed by s. */
826 remove_subscops (s);
828 /* Replace this with split-intersecting scops. */
829 remove_intersecting_scops (s);
831 scops.safe_push (s);
832 DEBUG_PRINT (dp << "\nAdding SCoP "; print_sese (dump_file, s));
835 /* Return true when a statement in SCOP cannot be represented by Graphite.
836 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
837 Limit the number of bbs between adjacent loops to
838 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
840 bool
841 scop_detection::harmful_stmt_in_region (sese_l scop) const
843 basic_block exit_bb = get_exit_bb (scop);
844 basic_block entry_bb = get_entry_bb (scop);
846 DEBUG_PRINT (dp << "\n[checking-harmful-bbs] ";
847 print_sese (dump_file, scop));
848 gcc_assert (dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb));
850 int depth = bb_dom_dfs_in (CDI_DOMINATORS, exit_bb)
851 - bb_dom_dfs_in (CDI_DOMINATORS, entry_bb);
853 gcc_assert (depth > 0);
855 vec<basic_block> dom
856 = get_dominated_to_depth (CDI_DOMINATORS, entry_bb, depth);
857 int i;
858 basic_block bb;
859 FOR_EACH_VEC_ELT (dom, i, bb)
861 DEBUG_PRINT (dp << "\nVisiting bb_" << bb->index);
863 /* We don't want to analyze any bb outside sese. */
864 if (!dominated_by_p (CDI_POST_DOMINATORS, bb, exit_bb))
865 continue;
867 if (harmful_stmt_in_bb (scop, bb))
868 return true;
871 return false;
874 /* Returns true if S1 subsumes/surrounds S2. */
875 bool
876 scop_detection::subsumes (sese_l s1, sese_l s2)
878 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
879 get_entry_bb (s1))
880 && dominated_by_p (CDI_POST_DOMINATORS, s2.exit->dest,
881 s1.exit->dest))
882 return true;
883 return false;
886 /* Remove a SCoP which is subsumed by S1. */
887 void
888 scop_detection::remove_subscops (sese_l s1)
890 int j;
891 sese_l s2 (0);
892 FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
894 if (subsumes (s1, s2))
896 DEBUG_PRINT (dp << "\nRemoving sub-SCoP";
897 print_sese (dump_file, s2));
898 scops.unordered_remove (j);
903 /* Returns true if S1 intersects with S2. Since we already know that S1 does
904 not subsume S2 or vice-versa, we only check for entry bbs. */
906 bool
907 scop_detection::intersects (sese_l s1, sese_l s2)
909 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
910 get_entry_bb (s1))
911 && !dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
912 get_exit_bb (s1)))
913 return true;
914 if ((s1.exit == s2.entry) || (s2.exit == s1.entry))
915 return true;
917 return false;
920 /* Remove one of the scops when it intersects with any other. */
922 void
923 scop_detection::remove_intersecting_scops (sese_l s1)
925 int j;
926 sese_l s2 (0);
927 FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
929 if (intersects (s1, s2))
931 DEBUG_PRINT (dp << "\nRemoving intersecting SCoP";
932 print_sese (dump_file, s2); dp << "Intersects with:";
933 print_sese (dump_file, s1));
934 scops.unordered_remove (j);
939 /* Something like "n * m" is not allowed. */
941 bool
942 scop_detection::graphite_can_represent_init (tree e)
944 switch (TREE_CODE (e))
946 case POLYNOMIAL_CHREC:
947 return graphite_can_represent_init (CHREC_LEFT (e))
948 && graphite_can_represent_init (CHREC_RIGHT (e));
950 case MULT_EXPR:
951 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
952 return graphite_can_represent_init (TREE_OPERAND (e, 0))
953 && tree_fits_shwi_p (TREE_OPERAND (e, 1));
954 else
955 return graphite_can_represent_init (TREE_OPERAND (e, 1))
956 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
958 case PLUS_EXPR:
959 case POINTER_PLUS_EXPR:
960 case MINUS_EXPR:
961 return graphite_can_represent_init (TREE_OPERAND (e, 0))
962 && graphite_can_represent_init (TREE_OPERAND (e, 1));
964 case NEGATE_EXPR:
965 case BIT_NOT_EXPR:
966 CASE_CONVERT:
967 case NON_LVALUE_EXPR:
968 return graphite_can_represent_init (TREE_OPERAND (e, 0));
970 default:
971 break;
974 return true;
977 /* Return true when SCEV can be represented in the polyhedral model.
979 An expression can be represented, if it can be expressed as an
980 affine expression. For loops (i, j) and parameters (m, n) all
981 affine expressions are of the form:
983 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
985 1 i + 20 j + (-2) m + 25
987 Something like "i * n" or "n * m" is not allowed. */
989 bool
990 scop_detection::graphite_can_represent_scev (tree scev)
992 if (chrec_contains_undetermined (scev))
993 return false;
995 /* We disable the handling of pointer types, because it’s currently not
996 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
997 the only nodes, which are disabled in case they are pointers to object
998 types, but this can be changed. */
1000 if (POINTER_TYPE_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
1001 return false;
1003 switch (TREE_CODE (scev))
1005 case NEGATE_EXPR:
1006 case BIT_NOT_EXPR:
1007 CASE_CONVERT:
1008 case NON_LVALUE_EXPR:
1009 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
1011 case PLUS_EXPR:
1012 case POINTER_PLUS_EXPR:
1013 case MINUS_EXPR:
1014 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
1015 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
1017 case MULT_EXPR:
1018 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
1019 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
1020 && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
1021 && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
1022 && graphite_can_represent_init (scev)
1023 && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
1024 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
1026 case POLYNOMIAL_CHREC:
1027 /* Check for constant strides. With a non constant stride of
1028 'n' we would have a value of 'iv * n'. Also check that the
1029 initial value can represented: for example 'n * m' cannot be
1030 represented. */
1031 if (!evolution_function_right_is_integer_cst (scev)
1032 || !graphite_can_represent_init (scev))
1033 return false;
1034 return graphite_can_represent_scev (CHREC_LEFT (scev));
1036 default:
1037 break;
1040 /* Only affine functions can be represented. */
1041 if (tree_contains_chrecs (scev, NULL) || !scev_is_linear_expression (scev))
1042 return false;
1044 return true;
1047 /* Return true when EXPR can be represented in the polyhedral model.
1049 This means an expression can be represented, if it is linear with respect to
1050 the loops and the strides are non parametric. LOOP is the place where the
1051 expr will be evaluated. SCOP defines the region we analyse. */
1053 bool
1054 scop_detection::graphite_can_represent_expr (sese_l scop, loop_p loop,
1055 tree expr)
1057 tree scev = scalar_evolution_in_region (scop, loop, expr);
1058 return graphite_can_represent_scev (scev);
1061 /* Return true if the data references of STMT can be represented by Graphite.
1062 We try to analyze the data references in a loop contained in the SCOP. */
1064 bool
1065 scop_detection::stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt)
1067 loop_p nest = outermost_loop_in_sese (scop, gimple_bb (stmt));
1068 loop_p loop = loop_containing_stmt (stmt);
1069 vec<data_reference_p> drs = vNULL;
1071 graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
1073 int j;
1074 data_reference_p dr;
1075 FOR_EACH_VEC_ELT (drs, j, dr)
1077 int nb_subscripts = DR_NUM_DIMENSIONS (dr);
1079 if (nb_subscripts < 1)
1081 free_data_refs (drs);
1082 return false;
1085 tree ref = DR_REF (dr);
1087 for (int i = nb_subscripts - 1; i >= 0; i--)
1089 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i))
1090 || (TREE_CODE (ref) != ARRAY_REF && TREE_CODE (ref) != MEM_REF
1091 && TREE_CODE (ref) != COMPONENT_REF))
1093 free_data_refs (drs);
1094 return false;
1097 ref = TREE_OPERAND (ref, 0);
1101 free_data_refs (drs);
1102 return true;
1105 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
1106 Calls have side-effects, except those to const or pure
1107 functions. */
1109 static bool
1110 stmt_has_side_effects (gimple *stmt)
1112 if (gimple_has_volatile_ops (stmt)
1113 || (gimple_code (stmt) == GIMPLE_CALL
1114 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
1115 || (gimple_code (stmt) == GIMPLE_ASM))
1117 DEBUG_PRINT (dp << "[scop-detection-fail] "
1118 << "Statement has side-effects:\n";
1119 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS | TDF_MEMSYMS));
1120 return true;
1122 return false;
1125 /* Returns true if STMT can be represented in polyhedral model. LABEL,
1126 simple COND stmts, pure calls, and assignments can be repesented. */
1128 bool
1129 scop_detection::graphite_can_represent_stmt (sese_l scop, gimple *stmt,
1130 basic_block bb)
1132 loop_p loop = bb->loop_father;
1133 switch (gimple_code (stmt))
1135 case GIMPLE_LABEL:
1136 return true;
1138 case GIMPLE_COND:
1140 /* We can handle all binary comparisons. Inequalities are
1141 also supported as they can be represented with union of
1142 polyhedra. */
1143 enum tree_code code = gimple_cond_code (stmt);
1144 if (!(code == LT_EXPR
1145 || code == GT_EXPR
1146 || code == LE_EXPR
1147 || code == GE_EXPR
1148 || code == EQ_EXPR
1149 || code == NE_EXPR))
1151 DEBUG_PRINT (dp << "[scop-detection-fail] "
1152 << "Graphite cannot handle cond stmt:\n";
1153 print_gimple_stmt (dump_file, stmt, 0,
1154 TDF_VOPS | TDF_MEMSYMS));
1155 return false;
1158 for (unsigned i = 0; i < 2; ++i)
1160 tree op = gimple_op (stmt, i);
1161 if (!graphite_can_represent_expr (scop, loop, op)
1162 /* We can only constrain on integer type. */
1163 || (TREE_CODE (TREE_TYPE (op)) != INTEGER_TYPE))
1165 DEBUG_PRINT (dp << "[scop-detection-fail] "
1166 << "Graphite cannot represent stmt:\n";
1167 print_gimple_stmt (dump_file, stmt, 0,
1168 TDF_VOPS | TDF_MEMSYMS));
1169 return false;
1173 return true;
1176 case GIMPLE_ASSIGN:
1177 case GIMPLE_CALL:
1178 return true;
1180 default:
1181 /* These nodes cut a new scope. */
1182 DEBUG_PRINT (
1183 dp << "[scop-detection-fail] "
1184 << "Gimple stmt not handled in Graphite:\n";
1185 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS | TDF_MEMSYMS));
1186 return false;
1190 /* Return true only when STMT is simple enough for being handled by Graphite.
1191 This depends on SCOP, as the parameters are initialized relatively to
1192 this basic block, the linear functions are initialized based on the outermost
1193 loop containing STMT inside the SCOP. BB is the place where we try to
1194 evaluate the STMT. */
1196 bool
1197 scop_detection::stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
1198 basic_block bb) const
1200 gcc_assert (scop);
1202 if (is_gimple_debug (stmt))
1203 return true;
1205 if (stmt_has_side_effects (stmt))
1206 return false;
1208 if (!stmt_has_simple_data_refs_p (scop, stmt))
1210 DEBUG_PRINT (dp << "[scop-detection-fail] "
1211 << "Graphite cannot handle data-refs in stmt:\n";
1212 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS););
1213 return false;
1216 return graphite_can_represent_stmt (scop, stmt, bb);
1219 /* Return true when BB contains a harmful operation for a scop: that
1220 can be a function call with side effects, the induction variables
1221 are not linear with respect to SCOP, etc. The current open
1222 scop should end before this statement. */
1224 bool
1225 scop_detection::harmful_stmt_in_bb (sese_l scop, basic_block bb) const
1227 gimple_stmt_iterator gsi;
1229 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1230 if (!stmt_simple_for_scop_p (scop, gsi_stmt (gsi), bb))
1231 return true;
1233 return false;
1236 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1237 different colors. If there are not enough colors, paint the
1238 remaining SCoPs in gray.
1240 Special nodes:
1241 - "*" after the node number denotes the entry of a SCoP,
1242 - "#" after the node number denotes the exit of a SCoP,
1243 - "()" around the node number denotes the entry or the
1244 exit nodes of the SCOP. These are not part of SCoP. */
1246 static void
1247 dot_all_scops_1 (FILE *file, vec<scop_p> scops)
1249 basic_block bb;
1250 edge e;
1251 edge_iterator ei;
1252 scop_p scop;
1253 const char *color;
1254 int i;
1256 /* Disable debugging while printing graph. */
1257 int tmp_dump_flags = dump_flags;
1258 dump_flags = 0;
1260 fprintf (file, "digraph all {\n");
1262 FOR_ALL_BB_FN (bb, cfun)
1264 int part_of_scop = false;
1266 /* Use HTML for every bb label. So we are able to print bbs
1267 which are part of two different SCoPs, with two different
1268 background colors. */
1269 fprintf (file, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1270 bb->index);
1271 fprintf (file, "CELLSPACING=\"0\">\n");
1273 /* Select color for SCoP. */
1274 FOR_EACH_VEC_ELT (scops, i, scop)
1276 sese_l region = scop->region->region;
1277 if (bb_in_sese_p (bb, region) || (region.exit->dest == bb)
1278 || (region.entry->dest == bb))
1280 switch (i % 17)
1282 case 0: /* red */
1283 color = "#e41a1c";
1284 break;
1285 case 1: /* blue */
1286 color = "#377eb8";
1287 break;
1288 case 2: /* green */
1289 color = "#4daf4a";
1290 break;
1291 case 3: /* purple */
1292 color = "#984ea3";
1293 break;
1294 case 4: /* orange */
1295 color = "#ff7f00";
1296 break;
1297 case 5: /* yellow */
1298 color = "#ffff33";
1299 break;
1300 case 6: /* brown */
1301 color = "#a65628";
1302 break;
1303 case 7: /* rose */
1304 color = "#f781bf";
1305 break;
1306 case 8:
1307 color = "#8dd3c7";
1308 break;
1309 case 9:
1310 color = "#ffffb3";
1311 break;
1312 case 10:
1313 color = "#bebada";
1314 break;
1315 case 11:
1316 color = "#fb8072";
1317 break;
1318 case 12:
1319 color = "#80b1d3";
1320 break;
1321 case 13:
1322 color = "#fdb462";
1323 break;
1324 case 14:
1325 color = "#b3de69";
1326 break;
1327 case 15:
1328 color = "#fccde5";
1329 break;
1330 case 16:
1331 color = "#bc80bd";
1332 break;
1333 default: /* gray */
1334 color = "#999999";
1337 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">",
1338 color);
1340 if (!bb_in_sese_p (bb, region))
1341 fprintf (file, " (");
1343 if (bb == region.entry->dest && bb == region.exit->dest)
1344 fprintf (file, " %d*# ", bb->index);
1345 else if (bb == region.entry->dest)
1346 fprintf (file, " %d* ", bb->index);
1347 else if (bb == region.exit->dest)
1348 fprintf (file, " %d# ", bb->index);
1349 else
1350 fprintf (file, " %d ", bb->index);
1352 fprintf (file, "{lp_%d}", bb->loop_father->num);
1354 if (!bb_in_sese_p (bb, region))
1355 fprintf (file, ")");
1357 fprintf (file, "</TD></TR>\n");
1358 part_of_scop = true;
1362 if (!part_of_scop)
1364 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1365 fprintf (file, " %d {lp_%d} </TD></TR>\n", bb->index,
1366 bb->loop_father->num);
1368 fprintf (file, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1371 FOR_ALL_BB_FN (bb, cfun)
1373 FOR_EACH_EDGE (e, ei, bb->succs)
1374 fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
1377 fputs ("}\n\n", file);
1379 /* Enable debugging again. */
1380 dump_flags = tmp_dump_flags;
1383 /* Display all SCoPs using dotty. */
1385 DEBUG_FUNCTION void
1386 dot_all_scops (vec<scop_p> scops)
1388 /* When debugging, enable the following code. This cannot be used
1389 in production compilers because it calls "system". */
1390 #if 0
1391 int x;
1392 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1393 gcc_assert (stream);
1395 dot_all_scops_1 (stream, scops);
1396 fclose (stream);
1398 x = system ("dotty /tmp/allscops.dot &");
1399 #else
1400 dot_all_scops_1 (stderr, scops);
1401 #endif
1404 /* Display all SCoPs using dotty. */
1406 DEBUG_FUNCTION void
1407 dot_scop (scop_p scop)
1409 auto_vec<scop_p, 1> scops;
1411 if (scop)
1412 scops.safe_push (scop);
1414 /* When debugging, enable the following code. This cannot be used
1415 in production compilers because it calls "system". */
1416 #if 0
1418 int x;
1419 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1420 gcc_assert (stream);
1422 dot_all_scops_1 (stream, scops);
1423 fclose (stream);
1424 x = system ("dotty /tmp/allscops.dot &");
1426 #else
1427 dot_all_scops_1 (stderr, scops);
1428 #endif
1431 /* Return true when the body of LOOP has statements that can be represented as a
1432 valid scop. */
1434 bool
1435 scop_detection::loop_body_is_valid_scop (loop_p loop, sese_l scop) const
1437 if (!loop_ivs_can_be_represented (loop))
1439 DEBUG_PRINT (dp << "[scop-detection-fail] loop_" << loop->num
1440 << "IV cannot be represented.\n");
1441 return false;
1444 if (!loop_nest_has_data_refs (loop))
1446 DEBUG_PRINT (dp << "[scop-detection-fail] loop_" << loop->num
1447 << "does not have any data reference.\n");
1448 return false;
1451 basic_block *bbs = get_loop_body (loop);
1452 for (unsigned i = 0; i < loop->num_nodes; i++)
1454 basic_block bb = bbs[i];
1456 if (harmful_stmt_in_bb (scop, bb))
1457 return false;
1459 free (bbs);
1461 if (loop->inner)
1463 loop = loop->inner;
1464 while (loop)
1466 if (!loop_body_is_valid_scop (loop, scop))
1467 return false;
1468 loop = loop->next;
1472 return true;
1475 /* Returns the number of pbbs that are in loops contained in SCOP. */
1478 scop_detection::nb_pbbs_in_loops (scop_p scop)
1480 int i;
1481 poly_bb_p pbb;
1482 int res = 0;
1484 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
1485 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb)), scop->region->region))
1486 res++;
1488 return res;
1491 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
1492 Otherwise returns -1. */
1494 static inline int
1495 parameter_index_in_region_1 (tree name, sese_info_p region)
1497 int i;
1498 tree p;
1500 gcc_assert (TREE_CODE (name) == SSA_NAME);
1502 FOR_EACH_VEC_ELT (SESE_PARAMS (region), i, p)
1503 if (p == name)
1504 return i;
1506 return -1;
1509 /* When the parameter NAME is in REGION, returns its index in
1510 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
1511 and returns the index of NAME. */
1513 static int
1514 parameter_index_in_region (tree name, sese_info_p region)
1516 int i;
1518 gcc_assert (TREE_CODE (name) == SSA_NAME);
1520 /* Cannot constrain on anything else than INTEGER_TYPE parameters. */
1521 if (TREE_CODE (TREE_TYPE (name)) != INTEGER_TYPE)
1522 return -1;
1524 if (!invariant_in_sese_p_rec (name, region->region))
1525 return -1;
1527 i = parameter_index_in_region_1 (name, region);
1528 if (i != -1)
1529 return i;
1531 i = SESE_PARAMS (region).length ();
1532 SESE_PARAMS (region).safe_push (name);
1533 return i;
1536 /* In the context of sese S, scan the expression E and translate it to
1537 a linear expression C. When parsing a symbolic multiplication, K
1538 represents the constant multiplier of an expression containing
1539 parameters. */
1541 static void
1542 scan_tree_for_params (sese_info_p s, tree e)
1544 if (e == chrec_dont_know)
1545 return;
1547 switch (TREE_CODE (e))
1549 case POLYNOMIAL_CHREC:
1550 scan_tree_for_params (s, CHREC_LEFT (e));
1551 break;
1553 case MULT_EXPR:
1554 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
1555 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1556 else
1557 scan_tree_for_params (s, TREE_OPERAND (e, 1));
1558 break;
1560 case PLUS_EXPR:
1561 case POINTER_PLUS_EXPR:
1562 case MINUS_EXPR:
1563 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1564 scan_tree_for_params (s, TREE_OPERAND (e, 1));
1565 break;
1567 case NEGATE_EXPR:
1568 case BIT_NOT_EXPR:
1569 CASE_CONVERT:
1570 case NON_LVALUE_EXPR:
1571 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1572 break;
1574 case SSA_NAME:
1575 parameter_index_in_region (e, s);
1576 break;
1578 case INTEGER_CST:
1579 case ADDR_EXPR:
1580 case REAL_CST:
1581 case COMPLEX_CST:
1582 case VECTOR_CST:
1583 break;
1585 default:
1586 gcc_unreachable ();
1587 break;
1591 /* Find parameters with respect to REGION in BB. We are looking in memory
1592 access functions, conditions and loop bounds. */
1594 static void
1595 find_params_in_bb (sese_info_p region, gimple_poly_bb_p gbb)
1597 /* Find parameters in the access functions of data references. */
1598 int i;
1599 data_reference_p dr;
1600 FOR_EACH_VEC_ELT (GBB_DATA_REFS (gbb), i, dr)
1601 for (unsigned j = 0; j < DR_NUM_DIMENSIONS (dr); j++)
1602 scan_tree_for_params (region, DR_ACCESS_FN (dr, j));
1604 /* Find parameters in conditional statements. */
1605 gimple *stmt;
1606 loop_p loop = GBB_BB (gbb)->loop_father;
1607 FOR_EACH_VEC_ELT (GBB_CONDITIONS (gbb), i, stmt)
1609 tree lhs = scalar_evolution_in_region (region->region, loop,
1610 gimple_cond_lhs (stmt));
1611 tree rhs = scalar_evolution_in_region (region->region, loop,
1612 gimple_cond_rhs (stmt));
1614 scan_tree_for_params (region, lhs);
1615 scan_tree_for_params (region, rhs);
1619 /* Record the parameters used in the SCOP. A variable is a parameter
1620 in a scop if it does not vary during the execution of that scop. */
1622 static void
1623 find_scop_parameters (scop_p scop)
1625 unsigned i;
1626 sese_info_p region = scop->region;
1627 struct loop *loop;
1629 /* Find the parameters used in the loop bounds. */
1630 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), i, loop)
1632 tree nb_iters = number_of_latch_executions (loop);
1634 if (!chrec_contains_symbols (nb_iters))
1635 continue;
1637 nb_iters = scalar_evolution_in_region (region->region, loop, nb_iters);
1638 scan_tree_for_params (region, nb_iters);
1641 /* Find the parameters used in data accesses. */
1642 poly_bb_p pbb;
1643 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
1644 find_params_in_bb (region, PBB_BLACK_BOX (pbb));
1646 int nbp = sese_nb_params (region);
1647 scop_set_nb_params (scop, nbp);
1650 /* Generates a polyhedral black box only if the bb contains interesting
1651 information. */
1653 static gimple_poly_bb_p
1654 try_generate_gimple_bb (scop_p scop, basic_block bb)
1656 vec<data_reference_p> drs;
1657 drs.create (5);
1658 sese_l region = scop->region->region;
1659 loop_p nest = outermost_loop_in_sese (region, bb);
1661 loop_p loop = bb->loop_father;
1662 if (!loop_in_sese_p (loop, region))
1663 loop = nest;
1665 gimple_stmt_iterator gsi;
1666 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1668 gimple *stmt = gsi_stmt (gsi);
1669 if (is_gimple_debug (stmt))
1670 continue;
1672 graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
1675 return new_gimple_poly_bb (bb, drs);
1678 /* Gather BBs and conditions for a SCOP. */
1679 class gather_bbs : public dom_walker
1681 public:
1682 gather_bbs (cdi_direction, scop_p);
1684 virtual void before_dom_children (basic_block);
1685 virtual void after_dom_children (basic_block);
1687 private:
1688 auto_vec<gimple *, 3> conditions, cases;
1689 scop_p scop;
1692 gather_bbs::gather_bbs (cdi_direction direction, scop_p scop)
1693 : dom_walker (direction), scop (scop)
1697 /* Call-back for dom_walk executed before visiting the dominated
1698 blocks. */
1700 void
1701 gather_bbs::before_dom_children (basic_block bb)
1703 if (!bb_in_sese_p (bb, scop->region->region))
1704 return;
1706 gcond *stmt = single_pred_cond_non_loop_exit (bb);
1708 if (stmt)
1710 edge e = single_pred_edge (bb);
1712 conditions.safe_push (stmt);
1714 if (e->flags & EDGE_TRUE_VALUE)
1715 cases.safe_push (stmt);
1716 else
1717 cases.safe_push (NULL);
1720 scop->region->bbs.safe_push (bb);
1722 gimple_poly_bb_p gbb = try_generate_gimple_bb (scop, bb);
1723 GBB_CONDITIONS (gbb) = conditions.copy ();
1724 GBB_CONDITION_CASES (gbb) = cases.copy ();
1726 poly_bb_p pbb = new_poly_bb (scop, gbb);
1727 scop->pbbs.safe_push (pbb);
1730 /* Call-back for dom_walk executed after visiting the dominated
1731 blocks. */
1733 void
1734 gather_bbs::after_dom_children (basic_block bb)
1736 if (!bb_in_sese_p (bb, scop->region->region))
1737 return;
1739 if (single_pred_cond_non_loop_exit (bb))
1741 conditions.pop ();
1742 cases.pop ();
1746 /* Find Static Control Parts (SCoP) in the current function and pushes
1747 them to SCOPS. */
1749 void
1750 build_scops (vec<scop_p> *scops)
1752 if (dump_file)
1753 dp.set_dump_file (dump_file);
1755 canonicalize_loop_closed_ssa_form ();
1757 scop_detection sb;
1758 sb.build_scop_depth (scop_detection::invalid_sese, current_loops->tree_root);
1760 /* Now create scops from the lightweight SESEs. */
1761 vec<sese_l> scops_l = sb.get_scops ();
1762 int i;
1763 sese_l s (0);
1764 FOR_EACH_VEC_ELT (scops_l, i, s)
1766 scop_p scop = new_scop (s.entry, s.exit);
1768 /* Record all basic blocks and their conditions in REGION. */
1769 gather_bbs (CDI_DOMINATORS, scop).walk (cfun->cfg->x_entry_block_ptr);
1771 /* Do not optimize a scop containing only PBBs that do not belong
1772 to any loops. */
1773 if (sb.nb_pbbs_in_loops (scop) == 0)
1775 DEBUG_PRINT (dp << "[scop-detection-fail] no data references.\n");
1776 free_scop (scop);
1777 continue;
1780 build_sese_loop_nests (scop->region);
1782 find_scop_parameters (scop);
1783 graphite_dim_t max_dim = PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS);
1785 if (scop_nb_params (scop) > max_dim)
1787 DEBUG_PRINT (dp << "[scop-detection-fail] too many parameters: "
1788 << scop_nb_params (scop)
1789 << " larger than --param graphite-max-nb-scop-params="
1790 << max_dim << ".\n");
1792 free_scop (scop);
1793 continue;
1796 scops->safe_push (scop);
1799 DEBUG_PRINT (dp << "number of SCoPs: " << (scops ? scops->length () : 0););
1802 #endif /* HAVE_isl */