]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/graphite-scop-detection.c
move param detection to scop detection
[thirdparty/gcc.git] / gcc / graphite-scop-detection.c
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>.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 #include "config.h"
23
24 #ifdef HAVE_isl
25 /* Workaround for GMP 5.1.3 bug, see PR56019. */
26 #include <stddef.h>
27
28 #include <isl/constraint.h>
29 #include <isl/set.h>
30 #include <isl/map.h>
31 #include <isl/union_map.h>
32
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"
58
59 /* Lightweight representation of sese for scop detection.
60 TODO: Make all this as a constant_edge. */
61 struct sese_l
62 {
63 sese_l (edge e, edge x) : entry (e), exit (x) {}
64
65 /* This is to push objects of sese_l in a vec. */
66 sese_l (int i) : entry (NULL), exit (NULL) { gcc_assert (i == 0); }
67
68 operator bool () const { return entry && exit; }
69
70 const sese_l &
71 operator= (const sese_l &s)
72 {
73 entry = s.entry;
74 exit = s.exit;
75 return *this;
76 }
77
78 edge entry;
79 edge exit;
80 };
81
82 /* APIs for getting entry/exit of an sese. */
83 static basic_block
84 get_entry_bb (edge e)
85 {
86 return e->dest;
87 }
88
89 static basic_block
90 get_exit_bb (edge e)
91 {
92 return e->src;
93 }
94
95 class debug_printer
96 {
97 private:
98 FILE *dump_file;
99
100 public:
101 void
102 set_dump_file (FILE *f)
103 {
104 gcc_assert (f);
105 dump_file = f;
106 }
107
108 friend debug_printer &
109 operator<< (debug_printer &output, int i)
110 {
111 fprintf (output.dump_file, "%d", i);
112 return output;
113 }
114 friend debug_printer &
115 operator<< (debug_printer &output, const char *s)
116 {
117 fprintf (output.dump_file, "%s", s);
118 return output;
119 }
120 } dp;
121
122 #define DEBUG_PRINT(args) do \
123 { \
124 if (dump_file && (dump_flags & TDF_DETAILS)) { args; } \
125 } while (0);
126
127
128 /* Return true if BB is empty, contains only DEBUG_INSNs. */
129
130 static bool
131 trivially_empty_bb_p (basic_block bb)
132 {
133 gimple_stmt_iterator gsi;
134
135 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
136 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_DEBUG)
137 return false;
138
139 return true;
140 }
141
142 /* Returns true when P1 and P2 are close phis with the same
143 argument. */
144
145 static inline bool
146 same_close_phi_node (gphi *p1, gphi *p2)
147 {
148 return operand_equal_p (gimple_phi_arg_def (p1, 0),
149 gimple_phi_arg_def (p2, 0), 0);
150 }
151
152 /* Compare the depth of two basic_block's P1 and P2. */
153
154 static int
155 compare_bb_depths (const void *p1, const void *p2)
156 {
157 const_basic_block const bb1 = *(const_basic_block const *)p1;
158 const_basic_block const bb2 = *(const_basic_block const *)p2;
159 int d1 = loop_depth (bb1->loop_father);
160 int d2 = loop_depth (bb2->loop_father);
161
162 if (d1 < d2)
163 return 1;
164
165 if (d1 > d2)
166 return -1;
167
168 return 0;
169 }
170
171 /* Sort the basic blocks from DOM such that the first are the ones at
172 a deepest loop level. */
173
174 static void
175 graphite_sort_dominated_info (vec<basic_block> dom)
176 {
177 dom.qsort (compare_bb_depths);
178 }
179
180 static void make_close_phi_nodes_unique (basic_block bb);
181
182 /* Remove the close phi node at GSI and replace its rhs with the rhs
183 of PHI. */
184
185 static void
186 remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
187 {
188 gimple *use_stmt;
189 use_operand_p use_p;
190 imm_use_iterator imm_iter;
191 tree res = gimple_phi_result (phi);
192 tree def = gimple_phi_result (gsi->phi ());
193
194 gcc_assert (same_close_phi_node (phi, gsi->phi ()));
195
196 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
197 {
198 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
199 SET_USE (use_p, res);
200
201 update_stmt (use_stmt);
202
203 /* It is possible that we just created a duplicate close-phi
204 for an already-processed containing loop. Check for this
205 case and clean it up. */
206 if (gimple_code (use_stmt) == GIMPLE_PHI
207 && gimple_phi_num_args (use_stmt) == 1)
208 make_close_phi_nodes_unique (gimple_bb (use_stmt));
209 }
210
211 remove_phi_node (gsi, true);
212 }
213
214 /* Removes all the close phi duplicates from BB. */
215
216 static void
217 make_close_phi_nodes_unique (basic_block bb)
218 {
219 gphi_iterator psi;
220
221 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
222 {
223 gphi_iterator gsi = psi;
224 gphi *phi = psi.phi ();
225
226 /* At this point, PHI should be a close phi in normal form. */
227 gcc_assert (gimple_phi_num_args (phi) == 1);
228
229 /* Iterate over the next phis and remove duplicates. */
230 gsi_next (&gsi);
231 while (!gsi_end_p (gsi))
232 if (same_close_phi_node (phi, gsi.phi ()))
233 remove_duplicate_close_phi (phi, &gsi);
234 else
235 gsi_next (&gsi);
236 }
237 }
238
239 /* Transforms LOOP to the canonical loop closed SSA form. */
240
241 static void
242 canonicalize_loop_closed_ssa (loop_p loop)
243 {
244 edge e = single_exit (loop);
245 basic_block bb;
246
247 if (!e || e->flags & EDGE_ABNORMAL)
248 return;
249
250 bb = e->dest;
251
252 if (single_pred_p (bb))
253 {
254 e = split_block_after_labels (bb);
255 DEBUG_PRINT (dp << "\nSplitting bb_" << bb->index);
256 make_close_phi_nodes_unique (e->src);
257 }
258 else
259 {
260 gphi_iterator psi;
261 basic_block close = split_edge (e);
262
263 e = single_succ_edge (close);
264 DEBUG_PRINT (dp << "\nSplitting edge (" << e->src->index << ","
265 << e->dest->index << ")\n");
266
267 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
268 {
269 gphi *phi = psi.phi ();
270 unsigned i;
271
272 for (i = 0; i < gimple_phi_num_args (phi); i++)
273 if (gimple_phi_arg_edge (phi, i) == e)
274 {
275 tree res, arg = gimple_phi_arg_def (phi, i);
276 use_operand_p use_p;
277 gphi *close_phi;
278
279 if (TREE_CODE (arg) != SSA_NAME)
280 continue;
281
282 close_phi = create_phi_node (NULL_TREE, close);
283 res = create_new_def_for (arg, close_phi,
284 gimple_phi_result_ptr (close_phi));
285 add_phi_arg (close_phi, arg,
286 gimple_phi_arg_edge (close_phi, 0),
287 UNKNOWN_LOCATION);
288 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
289 replace_exp (use_p, res);
290 update_stmt (phi);
291 }
292 }
293
294 make_close_phi_nodes_unique (close);
295 }
296
297 /* The code above does not properly handle changes in the post dominance
298 information (yet). */
299 recompute_all_dominators ();
300 }
301
302 /* Converts the current loop closed SSA form to a canonical form
303 expected by the Graphite code generation.
304
305 The loop closed SSA form has the following invariant: a variable
306 defined in a loop that is used outside the loop appears only in the
307 phi nodes in the destination of the loop exit. These phi nodes are
308 called close phi nodes.
309
310 The canonical loop closed SSA form contains the extra invariants:
311
312 - when the loop contains only one exit, the close phi nodes contain
313 only one argument. That implies that the basic block that contains
314 the close phi nodes has only one predecessor, that is a basic block
315 in the loop.
316
317 - the basic block containing the close phi nodes does not contain
318 other statements.
319
320 - there exist only one phi node per definition in the loop.
321 */
322
323 static void
324 canonicalize_loop_closed_ssa_form (void)
325 {
326 loop_p loop;
327
328 #ifdef ENABLE_CHECKING
329 verify_loop_closed_ssa (true);
330 #endif
331
332 FOR_EACH_LOOP (loop, 0)
333 canonicalize_loop_closed_ssa (loop);
334
335 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
336 update_ssa (TODO_update_ssa);
337
338 #ifdef ENABLE_CHECKING
339 verify_loop_closed_ssa (true);
340 #endif
341 }
342
343 /* Can all ivs be represented by a signed integer?
344 As ISL might generate negative values in its expressions, signed loop ivs
345 are required in the backend. */
346
347 static bool
348 loop_ivs_can_be_represented (loop_p loop)
349 {
350 unsigned type_long_long = TYPE_PRECISION (long_long_integer_type_node);
351 for (gphi_iterator psi = gsi_start_phis (loop->header); !gsi_end_p (psi);
352 gsi_next (&psi))
353 {
354 gphi *phi = psi.phi ();
355 tree res = PHI_RESULT (phi);
356 tree type = TREE_TYPE (res);
357
358 if (TYPE_UNSIGNED (type) && TYPE_PRECISION (type) >= type_long_long)
359 return false;
360 }
361
362 return true;
363 }
364
365 /* Returns a COND_EXPR statement when BB has a single predecessor, the
366 edge between BB and its predecessor is not a loop exit edge, and
367 the last statement of the single predecessor is a COND_EXPR. */
368
369 static gcond *
370 single_pred_cond_non_loop_exit (basic_block bb)
371 {
372 if (single_pred_p (bb))
373 {
374 edge e = single_pred_edge (bb);
375 basic_block pred = e->src;
376 gimple *stmt;
377
378 if (loop_depth (pred->loop_father) > loop_depth (bb->loop_father))
379 return NULL;
380
381 stmt = last_stmt (pred);
382
383 if (stmt && gimple_code (stmt) == GIMPLE_COND)
384 return as_a<gcond *> (stmt);
385 }
386
387 return NULL;
388 }
389
390 namespace
391 {
392
393 /* Build the maximal scop containing LOOPs and add it to SCOPS. */
394
395 class scop_detection
396 {
397 public:
398 scop_detection () : scops (vNULL) {}
399
400 /* A marker for invalid sese_l. */
401 static sese_l invalid_sese;
402
403 /* Return the SCOPS in this SCOP_DETECTION. */
404
405 vec<sese_l>
406 get_scops ()
407 {
408 return scops;
409 }
410
411 /* Return an sese_l around the LOOP. */
412
413 sese_l get_sese (loop_p loop);
414
415 /* Return the closest dominator with a single entry edge. In case of a
416 back-loop the back-edge is not counted. */
417
418 static edge get_nearest_dom_with_single_entry (basic_block dom);
419
420 /* Return the closest post-dominator with a single exit edge. In case of a
421 back-loop the back-edge is not counted. */
422
423 static edge get_nearest_pdom_with_single_exit (basic_block dom);
424
425 /* Print S to FILE. */
426
427 static void print_sese (FILE *file, sese_l s);
428
429 /* Merge scops at same loop depth and returns the new sese.
430 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
431
432 sese_l merge_sese (sese_l first, sese_l second) const;
433
434 /* Build scop outer->inner if possible. */
435
436 sese_l build_scop_depth (sese_l s, loop_p loop);
437
438 /* If loop and loop->next are valid scops, try to merge them. */
439
440 sese_l build_scop_breadth (sese_l s1, loop_p loop);
441
442 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
443 region of code that can be represented in the polyhedral model. SCOP
444 defines the region we analyse. */
445
446 bool loop_is_valid_scop (loop_p loop, sese_l scop) const;
447
448 /* Return true when BEGIN is the preheader edge of a loop with a single exit
449 END. */
450
451 static bool region_has_one_loop (sese_l s);
452
453 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
454
455 void add_scop (sese_l s);
456
457 /* Returns true if S1 subsumes/surrounds S2. */
458 static bool subsumes (sese_l s1, sese_l s2);
459
460 /* Remove a SCoP which is subsumed by S1. */
461 void remove_subscops (sese_l s1);
462
463 /* Returns true if S1 intersects with S2. Since we already know that S1 does
464 not subsume S2 or vice-versa, we only check for entry bbs. */
465
466 static bool intersects (sese_l s1, sese_l s2);
467
468 /* Remove one of the scops when it intersects with any other. */
469
470 void remove_intersecting_scops (sese_l s1);
471
472 /* Return true when the body of LOOP has statements that can be represented
473 as a valid scop. */
474
475 bool loop_body_is_valid_scop (loop_p loop, sese_l scop) const;
476
477 /* Return true when BB contains a harmful operation for a scop: that
478 can be a function call with side effects, the induction variables
479 are not linear with respect to SCOP, etc. The current open
480 scop should end before this statement. */
481
482 bool harmful_stmt_in_bb (sese_l scop, basic_block bb) const;
483
484 /* Return true when a statement in SCOP cannot be represented by Graphite.
485 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
486 Limit the number of bbs between adjacent loops to
487 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
488
489 bool harmful_stmt_in_region (sese_l scop) const;
490
491 /* Return true only when STMT is simple enough for being handled by Graphite.
492 This depends on SCOP, as the parameters are initialized relatively to
493 this basic block, the linear functions are initialized based on the
494 outermost loop containing STMT inside the SCOP. BB is the place where we
495 try to evaluate the STMT. */
496
497 bool stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
498 basic_block bb) const;
499
500 /* Something like "n * m" is not allowed. */
501
502 static bool graphite_can_represent_init (tree e);
503
504 /* Return true when SCEV can be represented in the polyhedral model.
505
506 An expression can be represented, if it can be expressed as an
507 affine expression. For loops (i, j) and parameters (m, n) all
508 affine expressions are of the form:
509
510 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
511
512 1 i + 20 j + (-2) m + 25
513
514 Something like "i * n" or "n * m" is not allowed. */
515
516 static bool graphite_can_represent_scev (tree scev);
517
518 /* Return true when EXPR can be represented in the polyhedral model.
519
520 This means an expression can be represented, if it is linear with respect
521 to the loops and the strides are non parametric. LOOP is the place where
522 the expr will be evaluated. SCOP defines the region we analyse. */
523
524 static bool graphite_can_represent_expr (sese_l scop, loop_p loop,
525 tree expr);
526
527 /* Return true if the data references of STMT can be represented by Graphite.
528 We try to analyze the data references in a loop contained in the SCOP. */
529
530 static bool stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt);
531
532 /* Remove the close phi node at GSI and replace its rhs with the rhs
533 of PHI. */
534
535 static void remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi);
536
537 /* Returns true when Graphite can represent LOOP in SCOP.
538 FIXME: For the moment, graphite cannot be used on loops that iterate using
539 induction variables that wrap. */
540
541 static bool can_represent_loop_1 (loop_p loop, sese_l scop);
542
543 /* Return true when all the loops within LOOP can be represented by
544 Graphite. */
545
546 static bool can_represent_loop (loop_p loop, sese_l scop);
547
548 /* Generates a polyhedral black box only if the bb contains interesting
549 information. */
550
551 static gimple_poly_bb_p try_generate_gimple_bb (scop_p scop, basic_block bb);
552
553 /* Returns true if all predecessors of BB, that are not dominated by BB, are
554 marked in MAP. The predecessors dominated by BB are loop latches and will
555 be handled after BB. */
556
557 static bool all_non_dominated_preds_marked_p (basic_block bb, sbitmap map);
558
559 /* Recursive helper function for build_scops_bbs. */
560
561 static void build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb);
562
563 /* Gather the basic blocks belonging to the SCOP. */
564
565 static void build_scop_bbs (scop_p scop);
566
567 /* Returns the number of pbbs that are in loops contained in SCOP. */
568
569 static int nb_pbbs_in_loops (scop_p scop);
570
571 static bool graphite_can_represent_stmt (sese_l, gimple *, basic_block);
572
573 private:
574 vec<sese_l> scops;
575 };
576
577 sese_l scop_detection::invalid_sese (0);
578
579 /* Return an sese_l around the LOOP. */
580
581 sese_l
582 scop_detection::get_sese (loop_p loop)
583 {
584 if (!loop)
585 return invalid_sese;
586
587 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS))
588 return invalid_sese;
589 edge scop_end = single_exit (loop);
590 if (!scop_end)
591 return invalid_sese;
592 edge scop_begin = loop_preheader_edge (loop);
593 sese_l s (scop_begin, scop_end);
594 return s;
595 }
596
597 /* Return the closest dominator with a single entry edge. */
598
599 edge
600 scop_detection::get_nearest_dom_with_single_entry (basic_block dom)
601 {
602 if (!dom->preds)
603 return NULL;
604 /* If e1->src dominates e2->src then e1->src will also dominate dom. */
605 if (dom->preds->length () == 2)
606 {
607 edge e1 = (*dom->preds)[0];
608 edge e2 = (*dom->preds)[1];
609 if (dominated_by_p (CDI_DOMINATORS, e2->src, e1->src))
610 return e1;
611 if (dominated_by_p (CDI_DOMINATORS, e1->src, e2->src))
612 return e2;
613 }
614
615 while (dom->preds->length () != 1)
616 {
617 if (dom->preds->length () < 1)
618 return NULL;
619 dom = get_immediate_dominator (CDI_DOMINATORS, dom);
620 if (!dom->preds)
621 return NULL;
622 }
623 return (*dom->preds)[0];
624 }
625
626 /* Return the closest post-dominator with a single exit edge. In case of a
627 back-loop the back-edge is not counted. */
628
629 edge
630 scop_detection::get_nearest_pdom_with_single_exit (basic_block dom)
631 {
632 if (!dom->succs)
633 return NULL;
634 if (dom->succs->length () == 2)
635 {
636 edge e1 = (*dom->succs)[0];
637 edge e2 = (*dom->succs)[1];
638 if (dominated_by_p (CDI_POST_DOMINATORS, e2->dest, e1->dest))
639 return e1;
640 if (dominated_by_p (CDI_POST_DOMINATORS, e1->dest, e2->dest))
641 return e2;
642 }
643
644 while (dom->succs->length () != 1)
645 {
646 if (dom->succs->length () < 1)
647 return NULL;
648 dom = get_immediate_dominator (CDI_POST_DOMINATORS, dom);
649 if (!dom->succs)
650 return NULL;
651 }
652 return (*dom->succs)[0];
653 }
654
655 /* Print S to FILE. */
656
657 void
658 scop_detection::print_sese (FILE *file, sese_l s)
659 {
660 fprintf (file, "(entry_edge (bb_%d, bb_%d), exit_edge (bb_%d, bb_%d))\n",
661 s.entry->src->index, s.entry->dest->index,
662 s.exit->src->index, s.exit->dest->index);
663 }
664
665 /* Merge scops at same loop depth and returns the new sese.
666 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
667
668 sese_l
669 scop_detection::merge_sese (sese_l first, sese_l second) const
670 {
671 /* In the trivial case first/second may be NULL. */
672 if (!first)
673 return second;
674 if (!second)
675 return first;
676
677 DEBUG_PRINT (dp << "[try-merging-sese] s1: "; print_sese (dump_file, first);
678 dp << "[try-merging-sese] s2: ";
679 print_sese (dump_file, second));
680
681 /* Assumption: Both the sese's should be at the same loop depth or one scop
682 should subsume the other like in case of nested loops. */
683
684 /* Find the common dominators for entry,
685 and common post-dominators for the exit. */
686 basic_block dom = nearest_common_dominator (CDI_DOMINATORS,
687 get_entry_bb (first.entry),
688 get_entry_bb (second.entry));
689
690 edge entry = get_nearest_dom_with_single_entry (dom);
691 if (!entry)
692 return invalid_sese;
693
694 basic_block pdom = nearest_common_dominator (CDI_POST_DOMINATORS,
695 get_exit_bb (first.exit),
696 get_exit_bb (second.exit));
697 pdom = nearest_common_dominator (CDI_POST_DOMINATORS, dom, pdom);
698
699 edge exit = get_nearest_pdom_with_single_exit (pdom);
700 if (!exit)
701 return invalid_sese;
702
703 sese_l combined (entry, exit);
704
705 /* FIXME: We could iterate to find the dom which dominates pdom, and pdom
706 which post-dominates dom, until it stabilizes. Also, ENTRY->SRC and
707 EXIT->DEST should be in the same loop nest. */
708 if (!dominated_by_p (CDI_DOMINATORS, pdom, dom)
709 || loop_depth (entry->src->loop_father)
710 != loop_depth (exit->dest->loop_father))
711 return invalid_sese;
712
713 /* For now we just want to bail out when exit does not post-dominate entry.
714 TODO: We might just add a basic_block at the exit to make exit
715 post-dominate entry (the entire region). */
716 if (!dominated_by_p (CDI_POST_DOMINATORS, get_entry_bb (entry),
717 get_exit_bb (exit))
718 || !dominated_by_p (CDI_DOMINATORS, get_exit_bb (exit),
719 get_entry_bb (entry)))
720 {
721 DEBUG_PRINT (dp << "[scop-detection-fail] cannot merge seses.\n");
722 return invalid_sese;
723 }
724
725 /* FIXME: We should remove this piece of code once
726 canonicalize_loop_closed_ssa has been removed, because that function
727 adds a BB with single exit. */
728 if (!trivially_empty_bb_p (get_exit_bb (combined.exit)))
729 {
730 /* Find the first empty succ (with single exit) of combined.exit. */
731 basic_block imm_succ = combined.exit->dest;
732 if (single_succ_p (imm_succ) && trivially_empty_bb_p (imm_succ))
733 combined.exit = single_succ_edge (imm_succ);
734 else
735 {
736 DEBUG_PRINT (dp << "\n[scop-detection-fail] Discarding SCoP because "
737 << "no single exit (empty succ) for sese exit";
738 print_sese (dump_file, combined));
739 return invalid_sese;
740 }
741 }
742
743 /* Analyze all the BBs in new sese. */
744 if (harmful_stmt_in_region (combined))
745 return invalid_sese;
746
747 DEBUG_PRINT (dp << "[merged-sese] s1: "; print_sese (dump_file, combined));
748
749 return combined;
750 }
751
752 /* Build scop outer->inner if possible. */
753
754 sese_l
755 scop_detection::build_scop_depth (sese_l s, loop_p loop)
756 {
757 if (!loop)
758 return s;
759
760 DEBUG_PRINT (dp << "\n[Depth loop_" << loop->num << "]");
761 s = build_scop_depth (s, loop->inner);
762
763 sese_l s2 = merge_sese (s, get_sese (loop));
764 if (!s2)
765 {
766 /* s might be a valid scop, so return it and start analyzing from the
767 adjacent loop. */
768 build_scop_depth (invalid_sese, loop->next);
769 return s;
770 }
771
772 if (!loop_is_valid_scop (loop, s2))
773 return build_scop_depth (invalid_sese, loop->next);
774
775 return build_scop_breadth (s2, loop);
776 }
777
778 /* If loop and loop->next are valid scops, try to merge them. */
779
780 sese_l
781 scop_detection::build_scop_breadth (sese_l s1, loop_p loop)
782 {
783 if (!loop)
784 return s1;
785 DEBUG_PRINT (dp << "\n[Breadth loop_" << loop->num << "]");
786 gcc_assert (s1);
787
788 loop_p l = loop;
789 sese_l s2 = build_scop_depth (invalid_sese, l->next);
790 if (!s2)
791 {
792 if (s1)
793 add_scop (s1);
794 return s1;
795 }
796
797 sese_l combined = merge_sese (s1, s2);
798
799 if (combined)
800 s1 = combined;
801 else
802 add_scop (s2);
803
804 if (s1)
805 add_scop (s1);
806 return s1;
807 }
808
809 /* Returns true when Graphite can represent LOOP in SCOP.
810 FIXME: For the moment, graphite cannot be used on loops that iterate using
811 induction variables that wrap. */
812
813 bool
814 scop_detection::can_represent_loop_1 (loop_p loop, sese_l scop)
815 {
816 tree niter;
817 struct tree_niter_desc niter_desc;
818
819 return single_exit (loop)
820 && number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
821 && niter_desc.control.no_overflow
822 && (niter = number_of_latch_executions (loop))
823 && !chrec_contains_undetermined (niter)
824 && graphite_can_represent_expr (scop, loop, niter);
825 }
826
827 /* Return true when all the loops within LOOP can be represented by
828 Graphite. */
829
830 bool
831 scop_detection::can_represent_loop (loop_p loop, sese_l scop)
832 {
833 if (!can_represent_loop_1 (loop, scop))
834 return false;
835 if (loop->inner && !can_represent_loop (loop->inner, scop))
836 return false;
837 if (loop->next && !can_represent_loop (loop->next, scop))
838 return false;
839
840 return true;
841 }
842
843 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
844 region of code that can be represented in the polyhedral model. SCOP
845 defines the region we analyse. */
846
847 bool
848 scop_detection::loop_is_valid_scop (loop_p loop, sese_l scop) const
849 {
850 if (!scop)
851 return false;
852
853 if (!can_represent_loop (loop, scop))
854 {
855 DEBUG_PRINT (dp << "[scop-detection-fail] cannot represent loop_"
856 << loop->num << "\n");
857 return false;
858 }
859
860 if (loop_body_is_valid_scop (loop, scop))
861 {
862 DEBUG_PRINT (dp << "[valid-scop] loop_" << loop->num
863 << "is a valid scop.\n");
864 return true;
865 }
866 return false;
867 }
868
869 /* Return true when BEGIN is the preheader edge of a loop with a single exit
870 END. */
871
872 bool
873 scop_detection::region_has_one_loop (sese_l s)
874 {
875 edge begin = s.entry;
876 edge end = s.exit;
877 /* Check for a single perfectly nested loop. */
878 if (begin->dest->loop_father->inner)
879 return false;
880
881 /* Otherwise, check whether we have adjacent loops. */
882 return begin->dest->loop_father == end->src->loop_father;
883 }
884
885 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
886
887 void
888 scop_detection::add_scop (sese_l s)
889 {
890 gcc_assert (s);
891
892 /* Do not add scops with only one loop. */
893 if (region_has_one_loop (s))
894 {
895 DEBUG_PRINT (dp << "\n[scop-detection-fail] Discarding one loop SCoP";
896 print_sese (dump_file, s));
897 return;
898 }
899
900 if (get_exit_bb (s.exit) == EXIT_BLOCK_PTR_FOR_FN (cfun))
901 {
902 DEBUG_PRINT (dp << "\n[scop-detection-fail] "
903 << "Discarding SCoP exiting to return";
904 print_sese (dump_file, s));
905 return;
906 }
907
908 /* Remove all the scops which are subsumed by s. */
909 remove_subscops (s);
910
911 /* Replace this with split-intersecting scops. */
912 remove_intersecting_scops (s);
913
914 scops.safe_push (s);
915 DEBUG_PRINT (dp << "\nAdding SCoP "; print_sese (dump_file, s));
916 }
917
918 /* Return true when a statement in SCOP cannot be represented by Graphite.
919 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
920 Limit the number of bbs between adjacent loops to
921 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
922
923 bool
924 scop_detection::harmful_stmt_in_region (sese_l scop) const
925 {
926 basic_block exit_bb = get_exit_bb (scop.exit);
927 basic_block entry_bb = get_entry_bb (scop.entry);
928
929 DEBUG_PRINT (dp << "\n[checking-harmful-bbs] ";
930 print_sese (dump_file, scop));
931 gcc_assert (dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb));
932
933 int depth = bb_dom_dfs_in (CDI_DOMINATORS, exit_bb)
934 - bb_dom_dfs_in (CDI_DOMINATORS, entry_bb);
935
936 gcc_assert (depth > 0);
937
938 vec<basic_block> dom
939 = get_dominated_to_depth (CDI_DOMINATORS, entry_bb, depth);
940 int i;
941 basic_block bb;
942 FOR_EACH_VEC_ELT (dom, i, bb)
943 {
944 DEBUG_PRINT (dp << "\nVisiting bb_" << bb->index);
945
946 /* We don't want to analyze any bb outside sese. */
947 if (!dominated_by_p (CDI_POST_DOMINATORS, bb, exit_bb))
948 continue;
949
950 if (harmful_stmt_in_bb (scop, bb))
951 return true;
952 }
953
954 return false;
955 }
956
957 /* Returns true if S1 subsumes/surrounds S2. */
958 bool
959 scop_detection::subsumes (sese_l s1, sese_l s2)
960 {
961 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2.entry),
962 get_entry_bb (s1.entry))
963 && dominated_by_p (CDI_POST_DOMINATORS, get_entry_bb (s2.exit),
964 get_entry_bb (s1.exit)))
965 return true;
966 return false;
967 }
968
969 /* Remove a SCoP which is subsumed by S1. */
970 void
971 scop_detection::remove_subscops (sese_l s1)
972 {
973 int j;
974 sese_l s2 (0);
975 FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
976 {
977 if (subsumes (s1, s2))
978 {
979 DEBUG_PRINT (dp << "\nRemoving sub-SCoP";
980 print_sese (dump_file, s2));
981 scops.unordered_remove (j);
982 }
983 }
984 }
985
986 /* Returns true if S1 intersects with S2. Since we already know that S1 does
987 not subsume S2 or vice-versa, we only check for entry bbs. */
988
989 bool
990 scop_detection::intersects (sese_l s1, sese_l s2)
991 {
992 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2.entry),
993 get_entry_bb (s1.entry))
994 && !dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2.entry),
995 get_exit_bb (s1.exit)))
996 return true;
997 if ((s1.exit == s2.entry) || (s2.exit == s1.entry))
998 return true;
999
1000 return false;
1001 }
1002
1003 /* Remove one of the scops when it intersects with any other. */
1004
1005 void
1006 scop_detection::remove_intersecting_scops (sese_l s1)
1007 {
1008 int j;
1009 sese_l s2 (0);
1010 FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
1011 {
1012 if (intersects (s1, s2))
1013 {
1014 DEBUG_PRINT (dp << "\nRemoving intersecting SCoP";
1015 print_sese (dump_file, s2); dp << "Intersects with:";
1016 print_sese (dump_file, s1));
1017 scops.unordered_remove (j);
1018 }
1019 }
1020 }
1021
1022 /* Something like "n * m" is not allowed. */
1023
1024 bool
1025 scop_detection::graphite_can_represent_init (tree e)
1026 {
1027 switch (TREE_CODE (e))
1028 {
1029 case POLYNOMIAL_CHREC:
1030 return graphite_can_represent_init (CHREC_LEFT (e))
1031 && graphite_can_represent_init (CHREC_RIGHT (e));
1032
1033 case MULT_EXPR:
1034 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
1035 return graphite_can_represent_init (TREE_OPERAND (e, 0))
1036 && tree_fits_shwi_p (TREE_OPERAND (e, 1));
1037 else
1038 return graphite_can_represent_init (TREE_OPERAND (e, 1))
1039 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
1040
1041 case PLUS_EXPR:
1042 case POINTER_PLUS_EXPR:
1043 case MINUS_EXPR:
1044 return graphite_can_represent_init (TREE_OPERAND (e, 0))
1045 && graphite_can_represent_init (TREE_OPERAND (e, 1));
1046
1047 case NEGATE_EXPR:
1048 case BIT_NOT_EXPR:
1049 CASE_CONVERT:
1050 case NON_LVALUE_EXPR:
1051 return graphite_can_represent_init (TREE_OPERAND (e, 0));
1052
1053 default:
1054 break;
1055 }
1056
1057 return true;
1058 }
1059
1060 /* Return true when SCEV can be represented in the polyhedral model.
1061
1062 An expression can be represented, if it can be expressed as an
1063 affine expression. For loops (i, j) and parameters (m, n) all
1064 affine expressions are of the form:
1065
1066 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
1067
1068 1 i + 20 j + (-2) m + 25
1069
1070 Something like "i * n" or "n * m" is not allowed. */
1071
1072 bool
1073 scop_detection::graphite_can_represent_scev (tree scev)
1074 {
1075 if (chrec_contains_undetermined (scev))
1076 return false;
1077
1078 /* We disable the handling of pointer types, because it’s currently not
1079 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
1080 the only nodes, which are disabled in case they are pointers to object
1081 types, but this can be changed. */
1082
1083 if (POINTER_TYPE_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
1084 return false;
1085
1086 switch (TREE_CODE (scev))
1087 {
1088 case NEGATE_EXPR:
1089 case BIT_NOT_EXPR:
1090 CASE_CONVERT:
1091 case NON_LVALUE_EXPR:
1092 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
1093
1094 case PLUS_EXPR:
1095 case POINTER_PLUS_EXPR:
1096 case MINUS_EXPR:
1097 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
1098 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
1099
1100 case MULT_EXPR:
1101 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
1102 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
1103 && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
1104 && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
1105 && graphite_can_represent_init (scev)
1106 && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
1107 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
1108
1109 case POLYNOMIAL_CHREC:
1110 /* Check for constant strides. With a non constant stride of
1111 'n' we would have a value of 'iv * n'. Also check that the
1112 initial value can represented: for example 'n * m' cannot be
1113 represented. */
1114 if (!evolution_function_right_is_integer_cst (scev)
1115 || !graphite_can_represent_init (scev))
1116 return false;
1117 return graphite_can_represent_scev (CHREC_LEFT (scev));
1118
1119 default:
1120 break;
1121 }
1122
1123 /* Only affine functions can be represented. */
1124 if (tree_contains_chrecs (scev, NULL) || !scev_is_linear_expression (scev))
1125 return false;
1126
1127 return true;
1128 }
1129
1130 /* Return true when EXPR can be represented in the polyhedral model.
1131
1132 This means an expression can be represented, if it is linear with respect to
1133 the loops and the strides are non parametric. LOOP is the place where the
1134 expr will be evaluated. SCOP defines the region we analyse. */
1135
1136 bool
1137 scop_detection::graphite_can_represent_expr (sese_l scop, loop_p loop,
1138 tree expr)
1139 {
1140 sese region = new_sese (scop.entry, scop.exit);
1141 tree scev = scalar_evolution_in_region (region, loop, expr);
1142 free_sese (region);
1143 return graphite_can_represent_scev (scev);
1144 }
1145
1146 /* Return true if the data references of STMT can be represented by Graphite.
1147 We try to analyze the data references in a loop contained in the SCOP. */
1148
1149 bool
1150 scop_detection::stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt)
1151 {
1152 sese region = new_sese (scop.entry, scop.exit);
1153 loop_p nest = outermost_loop_in_sese (region, gimple_bb (stmt));
1154 loop_p loop = loop_containing_stmt (stmt);
1155 vec<data_reference_p> drs = vNULL;
1156
1157 graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
1158
1159 int j;
1160 data_reference_p dr;
1161 FOR_EACH_VEC_ELT (drs, j, dr)
1162 {
1163 int nb_subscripts = DR_NUM_DIMENSIONS (dr);
1164
1165 if (nb_subscripts < 1)
1166 {
1167 free_data_refs (drs);
1168 return false;
1169 }
1170
1171 tree ref = DR_REF (dr);
1172
1173 for (int i = nb_subscripts - 1; i >= 0; i--)
1174 {
1175 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i))
1176 || (TREE_CODE (ref) != ARRAY_REF && TREE_CODE (ref) != MEM_REF
1177 && TREE_CODE (ref) != COMPONENT_REF))
1178 {
1179 free_data_refs (drs);
1180 return false;
1181 }
1182
1183 ref = TREE_OPERAND (ref, 0);
1184 }
1185 }
1186
1187 free_data_refs (drs);
1188 return true;
1189 }
1190
1191 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
1192 Calls have side-effects, except those to const or pure
1193 functions. */
1194
1195 static bool
1196 stmt_has_side_effects (gimple *stmt)
1197 {
1198 if (gimple_has_volatile_ops (stmt)
1199 || (gimple_code (stmt) == GIMPLE_CALL
1200 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
1201 || (gimple_code (stmt) == GIMPLE_ASM))
1202 {
1203 DEBUG_PRINT (dp << "[scop-detection-fail] "
1204 << "Statement has side-effects:\n";
1205 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS | TDF_MEMSYMS));
1206 return true;
1207 }
1208 return false;
1209 }
1210
1211 /* Returns true if STMT can be represented in polyhedral model. LABEL,
1212 simple COND stmts, pure calls, and assignments can be repesented. */
1213
1214 bool
1215 scop_detection::graphite_can_represent_stmt (sese_l scop, gimple *stmt,
1216 basic_block bb)
1217 {
1218 loop_p loop = bb->loop_father;
1219 switch (gimple_code (stmt))
1220 {
1221 case GIMPLE_LABEL:
1222 return true;
1223
1224 case GIMPLE_COND:
1225 {
1226 /* We can handle all binary comparisons. Inequalities are
1227 also supported as they can be represented with union of
1228 polyhedra. */
1229 enum tree_code code = gimple_cond_code (stmt);
1230 if (!(code == LT_EXPR
1231 || code == GT_EXPR
1232 || code == LE_EXPR
1233 || code == GE_EXPR
1234 || code == EQ_EXPR
1235 || code == NE_EXPR))
1236 {
1237 DEBUG_PRINT (dp << "[scop-detection-fail] "
1238 << "Graphite cannot handle cond stmt:\n";
1239 print_gimple_stmt (dump_file, stmt, 0,
1240 TDF_VOPS | TDF_MEMSYMS));
1241 return false;
1242 }
1243
1244 for (unsigned i = 0; i < 2; ++i)
1245 {
1246 tree op = gimple_op (stmt, i);
1247 if (!graphite_can_represent_expr (scop, loop, op)
1248 /* We can only constrain on integer type. */
1249 || (TREE_CODE (TREE_TYPE (op)) != INTEGER_TYPE))
1250 {
1251 DEBUG_PRINT (dp << "[scop-detection-fail] "
1252 << "Graphite cannot represent stmt:\n";
1253 print_gimple_stmt (dump_file, stmt, 0,
1254 TDF_VOPS | TDF_MEMSYMS));
1255 return false;
1256 }
1257 }
1258
1259 return true;
1260 }
1261
1262 case GIMPLE_ASSIGN:
1263 case GIMPLE_CALL:
1264 return true;
1265
1266 default:
1267 /* These nodes cut a new scope. */
1268 DEBUG_PRINT (
1269 dp << "[scop-detection-fail] "
1270 << "Gimple stmt not handled in Graphite:\n";
1271 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS | TDF_MEMSYMS));
1272 return false;
1273 }
1274 }
1275
1276 /* Return true only when STMT is simple enough for being handled by Graphite.
1277 This depends on SCOP, as the parameters are initialized relatively to
1278 this basic block, the linear functions are initialized based on the outermost
1279 loop containing STMT inside the SCOP. BB is the place where we try to
1280 evaluate the STMT. */
1281
1282 bool
1283 scop_detection::stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
1284 basic_block bb) const
1285 {
1286 gcc_assert (scop);
1287
1288 if (is_gimple_debug (stmt))
1289 return true;
1290
1291 if (stmt_has_side_effects (stmt))
1292 return false;
1293
1294 if (!stmt_has_simple_data_refs_p (scop, stmt))
1295 {
1296 DEBUG_PRINT (dp << "[scop-detection-fail] "
1297 << "Graphite cannot handle data-refs in stmt:\n";
1298 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS););
1299 return false;
1300 }
1301
1302 return graphite_can_represent_stmt (scop, stmt, bb);
1303 }
1304
1305 /* Return true when BB contains a harmful operation for a scop: that
1306 can be a function call with side effects, the induction variables
1307 are not linear with respect to SCOP, etc. The current open
1308 scop should end before this statement. */
1309
1310 bool
1311 scop_detection::harmful_stmt_in_bb (sese_l scop, basic_block bb) const
1312 {
1313 gimple_stmt_iterator gsi;
1314
1315 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1316 if (!stmt_simple_for_scop_p (scop, gsi_stmt (gsi), bb))
1317 return true;
1318
1319 return false;
1320 }
1321
1322 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1323 different colors. If there are not enough colors, paint the
1324 remaining SCoPs in gray.
1325
1326 Special nodes:
1327 - "*" after the node number denotes the entry of a SCoP,
1328 - "#" after the node number denotes the exit of a SCoP,
1329 - "()" around the node number denotes the entry or the
1330 exit nodes of the SCOP. These are not part of SCoP. */
1331
1332 static void
1333 dot_all_scops_1 (FILE *file, vec<scop_p> scops)
1334 {
1335 basic_block bb;
1336 edge e;
1337 edge_iterator ei;
1338 scop_p scop;
1339 const char *color;
1340 int i;
1341
1342 /* Disable debugging while printing graph. */
1343 int tmp_dump_flags = dump_flags;
1344 dump_flags = 0;
1345
1346 fprintf (file, "digraph all {\n");
1347
1348 FOR_ALL_BB_FN (bb, cfun)
1349 {
1350 int part_of_scop = false;
1351
1352 /* Use HTML for every bb label. So we are able to print bbs
1353 which are part of two different SCoPs, with two different
1354 background colors. */
1355 fprintf (file, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1356 bb->index);
1357 fprintf (file, "CELLSPACING=\"0\">\n");
1358
1359 /* Select color for SCoP. */
1360 FOR_EACH_VEC_ELT (scops, i, scop)
1361 {
1362 sese region = SCOP_REGION (scop);
1363 if (bb_in_sese_p (bb, region) || (SESE_EXIT_BB (region) == bb)
1364 || (SESE_ENTRY_BB (region) == bb))
1365 {
1366 switch (i % 17)
1367 {
1368 case 0: /* red */
1369 color = "#e41a1c";
1370 break;
1371 case 1: /* blue */
1372 color = "#377eb8";
1373 break;
1374 case 2: /* green */
1375 color = "#4daf4a";
1376 break;
1377 case 3: /* purple */
1378 color = "#984ea3";
1379 break;
1380 case 4: /* orange */
1381 color = "#ff7f00";
1382 break;
1383 case 5: /* yellow */
1384 color = "#ffff33";
1385 break;
1386 case 6: /* brown */
1387 color = "#a65628";
1388 break;
1389 case 7: /* rose */
1390 color = "#f781bf";
1391 break;
1392 case 8:
1393 color = "#8dd3c7";
1394 break;
1395 case 9:
1396 color = "#ffffb3";
1397 break;
1398 case 10:
1399 color = "#bebada";
1400 break;
1401 case 11:
1402 color = "#fb8072";
1403 break;
1404 case 12:
1405 color = "#80b1d3";
1406 break;
1407 case 13:
1408 color = "#fdb462";
1409 break;
1410 case 14:
1411 color = "#b3de69";
1412 break;
1413 case 15:
1414 color = "#fccde5";
1415 break;
1416 case 16:
1417 color = "#bc80bd";
1418 break;
1419 default: /* gray */
1420 color = "#999999";
1421 }
1422
1423 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">",
1424 color);
1425
1426 if (!bb_in_sese_p (bb, region))
1427 fprintf (file, " (");
1428
1429 if (bb == SESE_ENTRY_BB (region) && bb == SESE_EXIT_BB (region))
1430 fprintf (file, " %d*# ", bb->index);
1431 else if (bb == SESE_ENTRY_BB (region))
1432 fprintf (file, " %d* ", bb->index);
1433 else if (bb == SESE_EXIT_BB (region))
1434 fprintf (file, " %d# ", bb->index);
1435 else
1436 fprintf (file, " %d ", bb->index);
1437
1438 fprintf (file, "{lp_%d}", bb->loop_father->num);
1439
1440 if (!bb_in_sese_p (bb, region))
1441 fprintf (file, ")");
1442
1443 fprintf (file, "</TD></TR>\n");
1444 part_of_scop = true;
1445 }
1446 }
1447
1448 if (!part_of_scop)
1449 {
1450 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1451 fprintf (file, " %d {lp_%d} </TD></TR>\n", bb->index,
1452 bb->loop_father->num);
1453 }
1454 fprintf (file, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1455 }
1456
1457 FOR_ALL_BB_FN (bb, cfun)
1458 {
1459 FOR_EACH_EDGE (e, ei, bb->succs)
1460 fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
1461 }
1462
1463 fputs ("}\n\n", file);
1464
1465 /* Enable debugging again. */
1466 dump_flags = tmp_dump_flags;
1467 }
1468
1469 /* Display all SCoPs using dotty. */
1470
1471 DEBUG_FUNCTION void
1472 dot_all_scops (vec<scop_p> scops)
1473 {
1474 /* When debugging, enable the following code. This cannot be used
1475 in production compilers because it calls "system". */
1476 #if 0
1477 int x;
1478 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1479 gcc_assert (stream);
1480
1481 dot_all_scops_1 (stream, scops);
1482 fclose (stream);
1483
1484 x = system ("dotty /tmp/allscops.dot &");
1485 #else
1486 dot_all_scops_1 (stderr, scops);
1487 #endif
1488 }
1489
1490 /* Display all SCoPs using dotty. */
1491
1492 DEBUG_FUNCTION void
1493 dot_scop (scop_p scop)
1494 {
1495 auto_vec<scop_p, 1> scops;
1496
1497 if (scop)
1498 scops.safe_push (scop);
1499
1500 /* When debugging, enable the following code. This cannot be used
1501 in production compilers because it calls "system". */
1502 #if 0
1503 {
1504 int x;
1505 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1506 gcc_assert (stream);
1507
1508 dot_all_scops_1 (stream, scops);
1509 fclose (stream);
1510 x = system ("dotty /tmp/allscops.dot &");
1511 }
1512 #else
1513 dot_all_scops_1 (stderr, scops);
1514 #endif
1515 }
1516
1517 /* Return true when the body of LOOP has statements that can be represented as a
1518 valid scop. */
1519
1520 bool
1521 scop_detection::loop_body_is_valid_scop (loop_p loop, sese_l scop) const
1522 {
1523 if (!loop_ivs_can_be_represented (loop))
1524 {
1525 DEBUG_PRINT (dp << "[scop-detection-fail] loop_" << loop->num
1526 << "IV cannot be represented.\n");
1527 return false;
1528 }
1529
1530 if (!loop_nest_has_data_refs (loop))
1531 {
1532 DEBUG_PRINT (dp << "[scop-detection-fail] loop_" << loop->num
1533 << "does not have any data reference.\n");
1534 return false;
1535 }
1536
1537 basic_block *bbs = get_loop_body (loop);
1538 for (unsigned i = 0; i < loop->num_nodes; i++)
1539 {
1540 basic_block bb = bbs[i];
1541
1542 if (harmful_stmt_in_bb (scop, bb))
1543 return false;
1544 }
1545 free (bbs);
1546
1547 if (loop->inner)
1548 {
1549 loop = loop->inner;
1550 while (loop)
1551 {
1552 if (!loop_body_is_valid_scop (loop, scop))
1553 return false;
1554 loop = loop->next;
1555 }
1556 }
1557
1558 return true;
1559 }
1560
1561 /* Generates a polyhedral black box only if the bb contains interesting
1562 information. */
1563
1564 gimple_poly_bb_p
1565 scop_detection::try_generate_gimple_bb (scop_p scop, basic_block bb)
1566 {
1567 vec<data_reference_p> drs;
1568 drs.create (5);
1569 sese region = SCOP_REGION (scop);
1570 loop_p nest = outermost_loop_in_sese (region, bb);
1571
1572 loop_p loop = bb->loop_father;
1573 if (!loop_in_sese_p (loop, region))
1574 loop = nest;
1575
1576 gimple_stmt_iterator gsi;
1577 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1578 {
1579 gimple *stmt = gsi_stmt (gsi);
1580 if (is_gimple_debug (stmt))
1581 continue;
1582
1583 graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
1584 }
1585
1586 return new_gimple_poly_bb (bb, drs);
1587 }
1588
1589 /* Returns true if all predecessors of BB, that are not dominated by BB, are
1590 marked in MAP. The predecessors dominated by BB are loop latches and will
1591 be handled after BB. */
1592
1593 bool
1594 scop_detection::all_non_dominated_preds_marked_p (basic_block bb, sbitmap map)
1595 {
1596 edge e;
1597 edge_iterator ei;
1598
1599 FOR_EACH_EDGE (e, ei, bb->preds)
1600 if (!bitmap_bit_p (map, e->src->index)
1601 && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
1602 return false;
1603
1604 return true;
1605 }
1606
1607 /* Recursive helper function for build_scops_bbs. */
1608
1609 void
1610 scop_detection::build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb)
1611 {
1612 sese region = SCOP_REGION (scop);
1613 vec<basic_block> dom;
1614 poly_bb_p pbb;
1615
1616 if (bitmap_bit_p (visited, bb->index) || !bb_in_sese_p (bb, region))
1617 return;
1618
1619 pbb = new_poly_bb (scop, try_generate_gimple_bb (scop, bb));
1620 SCOP_BBS (scop).safe_push (pbb);
1621 bitmap_set_bit (visited, bb->index);
1622
1623 dom = get_dominated_by (CDI_DOMINATORS, bb);
1624
1625 if (!dom.exists ())
1626 return;
1627
1628 graphite_sort_dominated_info (dom);
1629
1630 while (!dom.is_empty ())
1631 {
1632 int i;
1633 basic_block dom_bb;
1634
1635 FOR_EACH_VEC_ELT (dom, i, dom_bb)
1636 if (all_non_dominated_preds_marked_p (dom_bb, visited))
1637 {
1638 build_scop_bbs_1 (scop, visited, dom_bb);
1639 dom.unordered_remove (i);
1640 break;
1641 }
1642 }
1643
1644 dom.release ();
1645 }
1646
1647 /* Gather the basic blocks belonging to the SCOP. */
1648
1649 void
1650 scop_detection::build_scop_bbs (scop_p scop)
1651 {
1652 sbitmap visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
1653 sese region = SCOP_REGION (scop);
1654
1655 bitmap_clear (visited);
1656 build_scop_bbs_1 (scop, visited, SESE_ENTRY_BB (region));
1657 sbitmap_free (visited);
1658 }
1659
1660 /* Returns the number of pbbs that are in loops contained in SCOP. */
1661
1662 int
1663 scop_detection::nb_pbbs_in_loops (scop_p scop)
1664 {
1665 int i;
1666 poly_bb_p pbb;
1667 int res = 0;
1668
1669 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
1670 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb)), SCOP_REGION (scop)))
1671 res++;
1672
1673 return res;
1674 }
1675
1676 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
1677 Otherwise returns -1. */
1678
1679 static inline int
1680 parameter_index_in_region_1 (tree name, sese region)
1681 {
1682 int i;
1683 tree p;
1684
1685 gcc_assert (TREE_CODE (name) == SSA_NAME);
1686
1687 FOR_EACH_VEC_ELT (SESE_PARAMS (region), i, p)
1688 if (p == name)
1689 return i;
1690
1691 return -1;
1692 }
1693
1694 /* When the parameter NAME is in REGION, returns its index in
1695 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
1696 and returns the index of NAME. */
1697
1698 static int
1699 parameter_index_in_region (tree name, sese region)
1700 {
1701 int i;
1702
1703 gcc_assert (TREE_CODE (name) == SSA_NAME);
1704
1705 /* Cannot constrain on anything else than INTEGER_TYPE parameters. */
1706 if (TREE_CODE (TREE_TYPE (name)) != INTEGER_TYPE)
1707 return -1;
1708
1709 if (!invariant_in_sese_p_rec (name, region))
1710 return -1;
1711
1712 i = parameter_index_in_region_1 (name, region);
1713 if (i != -1)
1714 return i;
1715
1716 gcc_assert (SESE_ADD_PARAMS (region));
1717
1718 i = SESE_PARAMS (region).length ();
1719 SESE_PARAMS (region).safe_push (name);
1720 return i;
1721 }
1722
1723 /* In the context of sese S, scan the expression E and translate it to
1724 a linear expression C. When parsing a symbolic multiplication, K
1725 represents the constant multiplier of an expression containing
1726 parameters. */
1727
1728 static void
1729 scan_tree_for_params (sese s, tree e)
1730 {
1731 if (e == chrec_dont_know)
1732 return;
1733
1734 switch (TREE_CODE (e))
1735 {
1736 case POLYNOMIAL_CHREC:
1737 scan_tree_for_params (s, CHREC_LEFT (e));
1738 break;
1739
1740 case MULT_EXPR:
1741 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
1742 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1743 else
1744 scan_tree_for_params (s, TREE_OPERAND (e, 1));
1745 break;
1746
1747 case PLUS_EXPR:
1748 case POINTER_PLUS_EXPR:
1749 case MINUS_EXPR:
1750 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1751 scan_tree_for_params (s, TREE_OPERAND (e, 1));
1752 break;
1753
1754 case NEGATE_EXPR:
1755 case BIT_NOT_EXPR:
1756 CASE_CONVERT:
1757 case NON_LVALUE_EXPR:
1758 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1759 break;
1760
1761 case SSA_NAME:
1762 parameter_index_in_region (e, s);
1763 break;
1764
1765 case INTEGER_CST:
1766 case ADDR_EXPR:
1767 case REAL_CST:
1768 case COMPLEX_CST:
1769 case VECTOR_CST:
1770 break;
1771
1772 default:
1773 gcc_unreachable ();
1774 break;
1775 }
1776 }
1777
1778 /* Find parameters with respect to REGION in BB. We are looking in memory
1779 access functions, conditions and loop bounds. */
1780
1781 static void
1782 find_params_in_bb (sese region, gimple_poly_bb_p gbb)
1783 {
1784 int i;
1785 unsigned j;
1786 data_reference_p dr;
1787 gimple *stmt;
1788 loop_p loop = GBB_BB (gbb)->loop_father;
1789
1790 /* Find parameters in the access functions of data references. */
1791 FOR_EACH_VEC_ELT (GBB_DATA_REFS (gbb), i, dr)
1792 for (j = 0; j < DR_NUM_DIMENSIONS (dr); j++)
1793 scan_tree_for_params (region, DR_ACCESS_FN (dr, j));
1794
1795 /* Find parameters in conditional statements. */
1796 FOR_EACH_VEC_ELT (GBB_CONDITIONS (gbb), i, stmt)
1797 {
1798 tree lhs = scalar_evolution_in_region (region, loop,
1799 gimple_cond_lhs (stmt));
1800 tree rhs = scalar_evolution_in_region (region, loop,
1801 gimple_cond_rhs (stmt));
1802
1803 scan_tree_for_params (region, lhs);
1804 scan_tree_for_params (region, rhs);
1805 }
1806 }
1807
1808 /* Record the parameters used in the SCOP. A variable is a parameter
1809 in a scop if it does not vary during the execution of that scop. */
1810
1811 static void
1812 find_scop_parameters (scop_p scop)
1813 {
1814 poly_bb_p pbb;
1815 unsigned i;
1816 sese region = SCOP_REGION (scop);
1817 struct loop *loop;
1818 int nbp;
1819
1820 /* Find the parameters used in the loop bounds. */
1821 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), i, loop)
1822 {
1823 tree nb_iters = number_of_latch_executions (loop);
1824
1825 if (!chrec_contains_symbols (nb_iters))
1826 continue;
1827
1828 nb_iters = scalar_evolution_in_region (region, loop, nb_iters);
1829 scan_tree_for_params (region, nb_iters);
1830 }
1831
1832 /* Find the parameters used in data accesses. */
1833 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
1834 find_params_in_bb (region, PBB_BLACK_BOX (pbb));
1835
1836 nbp = sese_nb_params (region);
1837 scop_set_nb_params (scop, nbp);
1838 SESE_ADD_PARAMS (region) = false;
1839 }
1840
1841 class sese_dom_walker : public dom_walker
1842 {
1843 public:
1844 sese_dom_walker (cdi_direction, sese);
1845
1846 virtual void before_dom_children (basic_block);
1847 virtual void after_dom_children (basic_block);
1848
1849 private:
1850 auto_vec<gimple *, 3> m_conditions, m_cases;
1851 sese m_region;
1852 };
1853 }
1854 sese_dom_walker::sese_dom_walker (cdi_direction direction, sese region)
1855 : dom_walker (direction), m_region (region)
1856 {
1857 }
1858
1859 /* Call-back for dom_walk executed before visiting the dominated
1860 blocks. */
1861
1862 void
1863 sese_dom_walker::before_dom_children (basic_block bb)
1864 {
1865 gimple_poly_bb_p gbb;
1866 gcond *stmt;
1867
1868 if (!bb_in_sese_p (bb, m_region))
1869 return;
1870
1871 stmt = single_pred_cond_non_loop_exit (bb);
1872
1873 if (stmt)
1874 {
1875 edge e = single_pred_edge (bb);
1876
1877 m_conditions.safe_push (stmt);
1878
1879 if (e->flags & EDGE_TRUE_VALUE)
1880 m_cases.safe_push (stmt);
1881 else
1882 m_cases.safe_push (NULL);
1883 }
1884
1885 gbb = gbb_from_bb (bb);
1886
1887 if (gbb)
1888 {
1889 GBB_CONDITIONS (gbb) = m_conditions.copy ();
1890 GBB_CONDITION_CASES (gbb) = m_cases.copy ();
1891 }
1892 }
1893
1894 /* Call-back for dom_walk executed after visiting the dominated
1895 blocks. */
1896
1897 void
1898 sese_dom_walker::after_dom_children (basic_block bb)
1899 {
1900 if (!bb_in_sese_p (bb, m_region))
1901 return;
1902
1903 if (single_pred_cond_non_loop_exit (bb))
1904 {
1905 m_conditions.pop ();
1906 m_cases.pop ();
1907 }
1908 }
1909
1910 /* Find Static Control Parts (SCoP) in the current function and pushes
1911 them to SCOPS. */
1912
1913 void
1914 build_scops (vec<scop_p> *scops)
1915 {
1916 if (dump_file)
1917 dp.set_dump_file (dump_file);
1918
1919 canonicalize_loop_closed_ssa_form ();
1920
1921 scop_detection sb;
1922 sb.build_scop_depth (scop_detection::invalid_sese, current_loops->tree_root);
1923
1924 /* Now create scops from the lightweight SESEs. */
1925 vec<sese_l> scops_l = sb.get_scops ();
1926 int i;
1927 sese_l s (0);
1928 FOR_EACH_VEC_ELT (scops_l, i, s)
1929 {
1930 scop_p scop = new_scop (s.entry, s.exit);
1931
1932 sb.build_scop_bbs (scop);
1933 /* Do not optimize a scop containing only PBBs that do not belong
1934 to any loops. */
1935 if (sb.nb_pbbs_in_loops (scop) == 0)
1936 {
1937 DEBUG_PRINT (dp << "[scop-detection-fail] no data references.\n");
1938 free_scop (scop);
1939 continue;
1940 }
1941
1942 build_sese_loop_nests (scop->region);
1943 /* Record all conditions in REGION. */
1944 sese_dom_walker (CDI_DOMINATORS, scop->region).walk
1945 (cfun->cfg->x_entry_block_ptr);
1946
1947 find_scop_parameters (scop);
1948 graphite_dim_t max_dim = PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS);
1949
1950 if (scop_nb_params (scop) > max_dim)
1951 {
1952 DEBUG_PRINT (dp << "[scop-detection-fail] too many parameters: "
1953 << scop_nb_params (scop)
1954 << " larger than --param graphite-max-nb-scop-params="
1955 << max_dim << ".\n");
1956
1957 free_scop (scop);
1958 continue;
1959 }
1960
1961 scops->safe_push (scop);
1962 }
1963
1964 DEBUG_PRINT (dp << "number of SCoPs: " << (scops ? scops->length () : 0););
1965 }
1966
1967 #endif /* HAVE_isl */