]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/graphite-scop-detection.c
use sese_l throughout 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 "params.h"
38 #include "tree.h"
39 #include "gimple.h"
40 #include "ssa.h"
41 #include "fold-const.h"
42 #include "gimple-iterator.h"
43 #include "tree-ssa-loop-manip.h"
44 #include "tree-ssa-loop-niter.h"
45 #include "tree-ssa-loop.h"
46 #include "tree-into-ssa.h"
47 #include "tree-ssa.h"
48 #include "cfgloop.h"
49 #include "tree-data-ref.h"
50 #include "tree-scalar-evolution.h"
51 #include "tree-pass.h"
52 #include "graphite-poly.h"
53 #include "tree-ssa-propagate.h"
54 #include "graphite-scop-detection.h"
55 #include "gimple-pretty-print.h"
56
57 /* Lightweight representation of sese for scop detection.
58 TODO: Make all this as a constant_edge. */
59 struct sese_l
60 {
61 sese_l (edge e, edge x)
62 : entry (e), exit (x)
63 { }
64
65 /* This is to push objects of sese_l in a vec. */
66 sese_l (int i)
67 : entry (NULL), exit (NULL)
68 {
69 gcc_assert (i == 0);
70 }
71
72 operator bool () const
73 {
74 return entry && exit;
75 }
76
77 const sese_l&
78 operator= (const sese_l &s)
79 {
80 entry = s.entry;
81 exit = s.exit;
82 return *this;
83 }
84
85 edge entry;
86 edge exit;
87 };
88
89 /* APIs for getting entry/exit of an sese. */
90 static basic_block
91 get_entry_bb (edge e)
92 {
93 return e->dest;
94 }
95
96 static basic_block
97 get_exit_bb (edge e)
98 {
99 return e->src;
100 }
101
102 class debug_printer
103 {
104 private:
105 FILE *dump_file;
106 public:
107 void set_dump_file (FILE *f)
108 {
109 gcc_assert (f);
110 dump_file = f;
111 }
112
113 friend debug_printer &operator<<(debug_printer &output, int i)
114 {
115 fprintf (output.dump_file, "%d", i);
116 return output;
117 }
118 friend debug_printer &operator<<(debug_printer &output, const char *s)
119 {
120 fprintf (output.dump_file, "%s", s);
121 return output;
122 }
123 } dp;
124
125 #define DEBUG_PRINT(args) do \
126 { \
127 if (dump_file && (dump_flags & TDF_DETAILS)) { args; } \
128 } while (0);
129
130
131 /* Return true if BB is empty, contains only DEBUG_INSNs. */
132
133 static bool
134 trivially_empty_bb_p (basic_block bb)
135 {
136 gimple_stmt_iterator gsi;
137
138 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
139 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_DEBUG)
140 return false;
141
142 return true;
143 }
144
145
146 /* Forward declarations. */
147 static void make_close_phi_nodes_unique (basic_block);
148
149 /* Something like "n * m" is not allowed. */
150
151 static bool
152 graphite_can_represent_init (tree e)
153 {
154 switch (TREE_CODE (e))
155 {
156 case POLYNOMIAL_CHREC:
157 return graphite_can_represent_init (CHREC_LEFT (e))
158 && graphite_can_represent_init (CHREC_RIGHT (e));
159
160 case MULT_EXPR:
161 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
162 return graphite_can_represent_init (TREE_OPERAND (e, 0))
163 && tree_fits_shwi_p (TREE_OPERAND (e, 1));
164 else
165 return graphite_can_represent_init (TREE_OPERAND (e, 1))
166 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
167
168 case PLUS_EXPR:
169 case POINTER_PLUS_EXPR:
170 case MINUS_EXPR:
171 return graphite_can_represent_init (TREE_OPERAND (e, 0))
172 && graphite_can_represent_init (TREE_OPERAND (e, 1));
173
174 case NEGATE_EXPR:
175 case BIT_NOT_EXPR:
176 CASE_CONVERT:
177 case NON_LVALUE_EXPR:
178 return graphite_can_represent_init (TREE_OPERAND (e, 0));
179
180 default:
181 break;
182 }
183
184 return true;
185 }
186
187 /* Return true when SCEV can be represented in the polyhedral model.
188
189 An expression can be represented, if it can be expressed as an
190 affine expression. For loops (i, j) and parameters (m, n) all
191 affine expressions are of the form:
192
193 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
194
195 1 i + 20 j + (-2) m + 25
196
197 Something like "i * n" or "n * m" is not allowed. */
198
199 static bool
200 graphite_can_represent_scev (tree scev)
201 {
202 if (chrec_contains_undetermined (scev))
203 return false;
204
205 /* We disable the handling of pointer types, because it’s currently not
206 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
207 the only nodes, which are disabled in case they are pointers to object
208 types, but this can be changed. */
209
210 if (POINTER_TYPE_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
211 return false;
212
213 switch (TREE_CODE (scev))
214 {
215 case NEGATE_EXPR:
216 case BIT_NOT_EXPR:
217 CASE_CONVERT:
218 case NON_LVALUE_EXPR:
219 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
220
221 case PLUS_EXPR:
222 case POINTER_PLUS_EXPR:
223 case MINUS_EXPR:
224 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
225 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
226
227 case MULT_EXPR:
228 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
229 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
230 && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
231 && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
232 && graphite_can_represent_init (scev)
233 && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
234 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
235
236 case POLYNOMIAL_CHREC:
237 /* Check for constant strides. With a non constant stride of
238 'n' we would have a value of 'iv * n'. Also check that the
239 initial value can represented: for example 'n * m' cannot be
240 represented. */
241 if (!evolution_function_right_is_integer_cst (scev)
242 || !graphite_can_represent_init (scev))
243 return false;
244 return graphite_can_represent_scev (CHREC_LEFT (scev));
245
246 default:
247 break;
248 }
249
250 /* Only affine functions can be represented. */
251 if (tree_contains_chrecs (scev, NULL)
252 || !scev_is_linear_expression (scev))
253 return false;
254
255 return true;
256 }
257
258
259 /* Return true when EXPR can be represented in the polyhedral model.
260
261 This means an expression can be represented, if it is linear with respect to
262 the loops and the strides are non parametric. LOOP is the place where the
263 expr will be evaluated. SCOP defines the region we analyse. */
264
265 static bool
266 graphite_can_represent_expr (sese_l scop, loop_p loop, tree expr)
267 {
268 sese region = new_sese (scop.entry, scop.exit);
269 tree scev = scalar_evolution_in_region (region, loop, expr);
270 free_sese (region);
271 return graphite_can_represent_scev (scev);
272 }
273
274 /* Return true if the data references of STMT can be represented by Graphite.
275 We try to analyze the data references in a loop contained in the SCOP. */
276
277 static bool
278 stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt)
279 {
280 sese region = new_sese (scop.entry, scop.exit);
281 loop_p nest = outermost_loop_in_sese (region, gimple_bb (stmt));
282 loop_p loop = loop_containing_stmt (stmt);
283 vec<data_reference_p> drs = vNULL;
284
285 graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
286
287 int j;
288 data_reference_p dr;
289 FOR_EACH_VEC_ELT (drs, j, dr)
290 {
291 int nb_subscripts = DR_NUM_DIMENSIONS (dr);
292
293 if (nb_subscripts < 1)
294 {
295 free_data_refs (drs);
296 return false;
297 }
298
299 tree ref = DR_REF (dr);
300
301 for (int i = nb_subscripts - 1; i >= 0; i--)
302 {
303 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i))
304 || (TREE_CODE (ref) != ARRAY_REF
305 && TREE_CODE (ref) != MEM_REF
306 && TREE_CODE (ref) != COMPONENT_REF))
307 {
308 free_data_refs (drs);
309 return false;
310 }
311
312 ref = TREE_OPERAND (ref, 0);
313 }
314 }
315
316 free_data_refs (drs);
317 return true;
318 }
319
320 /* Return true only when STMT is simple enough for being handled by Graphite.
321 This depends on SCOP, as the parameters are initialized relatively to
322 this basic block, the linear functions are initialized based on the outermost
323 loop containing STMT inside the SCOP. BB is the place where we try to
324 evaluate the STMT. */
325
326 static bool
327 stmt_simple_for_scop_p (sese_l scop, gimple *stmt, basic_block bb)
328 {
329 loop_p loop = bb->loop_father;
330
331 gcc_assert (scop);
332
333 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
334 Calls have side-effects, except those to const or pure
335 functions. */
336 if (gimple_has_volatile_ops (stmt)
337 || (gimple_code (stmt) == GIMPLE_CALL
338 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
339 || (gimple_code (stmt) == GIMPLE_ASM))
340 {
341 DEBUG_PRINT (dp << "[scop-detection-fail] "
342 << "Graphite cannot handle this stmt:\n";
343 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS));
344 return false;
345 }
346
347 if (is_gimple_debug (stmt))
348 return true;
349
350 if (!stmt_has_simple_data_refs_p (scop, stmt))
351 {
352 DEBUG_PRINT (dp << "[scop-detection-fail] "
353 << "Graphite cannot handle data-refs in stmt:\n";
354 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS););
355 return false;
356 }
357
358 switch (gimple_code (stmt))
359 {
360 case GIMPLE_LABEL:
361 return true;
362
363 case GIMPLE_COND:
364 {
365 /* We can handle all binary comparisons. Inequalities are
366 also supported as they can be represented with union of
367 polyhedra. */
368 enum tree_code code = gimple_cond_code (stmt);
369 if (!(code == LT_EXPR
370 || code == GT_EXPR
371 || code == LE_EXPR
372 || code == GE_EXPR
373 || code == EQ_EXPR
374 || code == NE_EXPR))
375 {
376 DEBUG_PRINT (dp << "[scop-detection-fail] "
377 << "Graphite cannot handle cond stmt:\n";
378 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS));
379 return false;
380 }
381
382 for (unsigned i = 0; i < 2; ++i)
383 {
384 tree op = gimple_op (stmt, i);
385 if (!graphite_can_represent_expr (scop, loop, op)
386 /* We can only constrain on integer type. */
387 || (TREE_CODE (TREE_TYPE (op)) != INTEGER_TYPE))
388 {
389 DEBUG_PRINT (dp << "[scop-detection-fail] "
390 << "Graphite cannot represent stmt:\n";
391 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS));
392 return false;
393 }
394 }
395
396 return true;
397 }
398
399 case GIMPLE_ASSIGN:
400 case GIMPLE_CALL:
401 return true;
402
403 default:
404 /* These nodes cut a new scope. */
405 DEBUG_PRINT (dp << "[scop-detection-fail] "
406 << "Gimple stmt not handled in Graphite:\n";
407 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS));
408 return false;
409 }
410
411 return false;
412 }
413
414 /* Return true when BB contains a harmful operation for a scop: that
415 can be a function call with side effects, the induction variables
416 are not linear with respect to SCOP, etc. The current open
417 scop should end before this statement. */
418
419 static bool
420 harmful_stmt_in_bb (sese_l scop, basic_block bb)
421 {
422 gimple_stmt_iterator gsi;
423
424 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
425 if (!stmt_simple_for_scop_p (scop, gsi_stmt (gsi), bb))
426 return true;
427
428 return false;
429 }
430
431 /* Returns true when P1 and P2 are close phis with the same
432 argument. */
433
434 static inline bool
435 same_close_phi_node (gphi *p1, gphi *p2)
436 {
437 return operand_equal_p (gimple_phi_arg_def (p1, 0),
438 gimple_phi_arg_def (p2, 0), 0);
439 }
440
441 /* Remove the close phi node at GSI and replace its rhs with the rhs
442 of PHI. */
443
444 static void
445 remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
446 {
447 gimple *use_stmt;
448 use_operand_p use_p;
449 imm_use_iterator imm_iter;
450 tree res = gimple_phi_result (phi);
451 tree def = gimple_phi_result (gsi->phi ());
452
453 gcc_assert (same_close_phi_node (phi, gsi->phi ()));
454
455 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
456 {
457 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
458 SET_USE (use_p, res);
459
460 update_stmt (use_stmt);
461
462 /* It is possible that we just created a duplicate close-phi
463 for an already-processed containing loop. Check for this
464 case and clean it up. */
465 if (gimple_code (use_stmt) == GIMPLE_PHI
466 && gimple_phi_num_args (use_stmt) == 1)
467 make_close_phi_nodes_unique (gimple_bb (use_stmt));
468 }
469
470 remove_phi_node (gsi, true);
471 }
472
473 /* Removes all the close phi duplicates from BB. */
474
475 static void
476 make_close_phi_nodes_unique (basic_block bb)
477 {
478 gphi_iterator psi;
479
480 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
481 {
482 gphi_iterator gsi = psi;
483 gphi *phi = psi.phi ();
484
485 /* At this point, PHI should be a close phi in normal form. */
486 gcc_assert (gimple_phi_num_args (phi) == 1);
487
488 /* Iterate over the next phis and remove duplicates. */
489 gsi_next (&gsi);
490 while (!gsi_end_p (gsi))
491 if (same_close_phi_node (phi, gsi.phi ()))
492 remove_duplicate_close_phi (phi, &gsi);
493 else
494 gsi_next (&gsi);
495 }
496 }
497
498 /* Transforms LOOP to the canonical loop closed SSA form. */
499
500 static void
501 canonicalize_loop_closed_ssa (loop_p loop)
502 {
503 edge e = single_exit (loop);
504 basic_block bb;
505
506 if (!e || e->flags & EDGE_ABNORMAL)
507 return;
508
509 bb = e->dest;
510
511 if (single_pred_p (bb))
512 {
513 e = split_block_after_labels (bb);
514 DEBUG_PRINT (dp << "\nSplitting bb_" << bb->index);
515 make_close_phi_nodes_unique (e->src);
516 }
517 else
518 {
519 gphi_iterator psi;
520 basic_block close = split_edge (e);
521
522 e = single_succ_edge (close);
523 DEBUG_PRINT (dp << "\nSplitting edge ("
524 << e->src->index << "," << e->dest->index
525 << ")\n");
526
527 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
528 {
529 gphi *phi = psi.phi ();
530 unsigned i;
531
532 for (i = 0; i < gimple_phi_num_args (phi); i++)
533 if (gimple_phi_arg_edge (phi, i) == e)
534 {
535 tree res, arg = gimple_phi_arg_def (phi, i);
536 use_operand_p use_p;
537 gphi *close_phi;
538
539 if (TREE_CODE (arg) != SSA_NAME)
540 continue;
541
542 close_phi = create_phi_node (NULL_TREE, close);
543 res = create_new_def_for (arg, close_phi,
544 gimple_phi_result_ptr (close_phi));
545 add_phi_arg (close_phi, arg,
546 gimple_phi_arg_edge (close_phi, 0),
547 UNKNOWN_LOCATION);
548 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
549 replace_exp (use_p, res);
550 update_stmt (phi);
551 }
552 }
553
554 make_close_phi_nodes_unique (close);
555 }
556
557 /* The code above does not properly handle changes in the post dominance
558 information (yet). */
559 recompute_all_dominators ();
560 }
561
562 /* Converts the current loop closed SSA form to a canonical form
563 expected by the Graphite code generation.
564
565 The loop closed SSA form has the following invariant: a variable
566 defined in a loop that is used outside the loop appears only in the
567 phi nodes in the destination of the loop exit. These phi nodes are
568 called close phi nodes.
569
570 The canonical loop closed SSA form contains the extra invariants:
571
572 - when the loop contains only one exit, the close phi nodes contain
573 only one argument. That implies that the basic block that contains
574 the close phi nodes has only one predecessor, that is a basic block
575 in the loop.
576
577 - the basic block containing the close phi nodes does not contain
578 other statements.
579
580 - there exist only one phi node per definition in the loop.
581 */
582
583 static void
584 canonicalize_loop_closed_ssa_form (void)
585 {
586 loop_p loop;
587
588 #ifdef ENABLE_CHECKING
589 verify_loop_closed_ssa (true);
590 #endif
591
592 FOR_EACH_LOOP (loop, 0)
593 canonicalize_loop_closed_ssa (loop);
594
595 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
596 update_ssa (TODO_update_ssa);
597
598 #ifdef ENABLE_CHECKING
599 verify_loop_closed_ssa (true);
600 #endif
601 }
602
603 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
604 different colors. If there are not enough colors, paint the
605 remaining SCoPs in gray.
606
607 Special nodes:
608 - "*" after the node number denotes the entry of a SCoP,
609 - "#" after the node number denotes the exit of a SCoP,
610 - "()" around the node number denotes the entry or the
611 exit nodes of the SCOP. These are not part of SCoP. */
612
613 static void
614 dot_all_scops_1 (FILE *file, vec<scop_p> scops)
615 {
616 basic_block bb;
617 edge e;
618 edge_iterator ei;
619 scop_p scop;
620 const char* color;
621 int i;
622
623 /* Disable debugging while printing graph. */
624 int tmp_dump_flags = dump_flags;
625 dump_flags = 0;
626
627 fprintf (file, "digraph all {\n");
628
629 FOR_ALL_BB_FN (bb, cfun)
630 {
631 int part_of_scop = false;
632
633 /* Use HTML for every bb label. So we are able to print bbs
634 which are part of two different SCoPs, with two different
635 background colors. */
636 fprintf (file, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
637 bb->index);
638 fprintf (file, "CELLSPACING=\"0\">\n");
639
640 /* Select color for SCoP. */
641 FOR_EACH_VEC_ELT (scops, i, scop)
642 {
643 sese region = SCOP_REGION (scop);
644 if (bb_in_sese_p (bb, region)
645 || (SESE_EXIT_BB (region) == bb)
646 || (SESE_ENTRY_BB (region) == bb))
647 {
648 switch (i % 17)
649 {
650 case 0: /* red */
651 color = "#e41a1c";
652 break;
653 case 1: /* blue */
654 color = "#377eb8";
655 break;
656 case 2: /* green */
657 color = "#4daf4a";
658 break;
659 case 3: /* purple */
660 color = "#984ea3";
661 break;
662 case 4: /* orange */
663 color = "#ff7f00";
664 break;
665 case 5: /* yellow */
666 color = "#ffff33";
667 break;
668 case 6: /* brown */
669 color = "#a65628";
670 break;
671 case 7: /* rose */
672 color = "#f781bf";
673 break;
674 case 8:
675 color = "#8dd3c7";
676 break;
677 case 9:
678 color = "#ffffb3";
679 break;
680 case 10:
681 color = "#bebada";
682 break;
683 case 11:
684 color = "#fb8072";
685 break;
686 case 12:
687 color = "#80b1d3";
688 break;
689 case 13:
690 color = "#fdb462";
691 break;
692 case 14:
693 color = "#b3de69";
694 break;
695 case 15:
696 color = "#fccde5";
697 break;
698 case 16:
699 color = "#bc80bd";
700 break;
701 default: /* gray */
702 color = "#999999";
703 }
704
705 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
706
707 if (!bb_in_sese_p (bb, region))
708 fprintf (file, " (");
709
710 if (bb == SESE_ENTRY_BB (region)
711 && bb == SESE_EXIT_BB (region))
712 fprintf (file, " %d*# ", bb->index);
713 else if (bb == SESE_ENTRY_BB (region))
714 fprintf (file, " %d* ", bb->index);
715 else if (bb == SESE_EXIT_BB (region))
716 fprintf (file, " %d# ", bb->index);
717 else
718 fprintf (file, " %d ", bb->index);
719
720 fprintf (file, "{lp_%d}", bb->loop_father->num);
721
722 if (!bb_in_sese_p (bb,region))
723 fprintf (file, ")");
724
725 fprintf (file, "</TD></TR>\n");
726 part_of_scop = true;
727 }
728 }
729
730 if (!part_of_scop)
731 {
732 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
733 fprintf (file, " %d {lp_%d} </TD></TR>\n",
734 bb->index, bb->loop_father->num);
735 }
736 fprintf (file, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
737 }
738
739 FOR_ALL_BB_FN (bb, cfun)
740 {
741 FOR_EACH_EDGE (e, ei, bb->succs)
742 fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
743 }
744
745 fputs ("}\n\n", file);
746
747 /* Enable debugging again. */
748 dump_flags = tmp_dump_flags;
749 }
750
751 /* Display all SCoPs using dotty. */
752
753 DEBUG_FUNCTION void
754 dot_all_scops (vec<scop_p> scops)
755 {
756 /* When debugging, enable the following code. This cannot be used
757 in production compilers because it calls "system". */
758 #if 0
759 int x;
760 FILE *stream = fopen ("/tmp/allscops.dot", "w");
761 gcc_assert (stream);
762
763 dot_all_scops_1 (stream, scops);
764 fclose (stream);
765
766 x = system ("dotty /tmp/allscops.dot &");
767 #else
768 dot_all_scops_1 (stderr, scops);
769 #endif
770 }
771
772 /* Display all SCoPs using dotty. */
773
774 DEBUG_FUNCTION void
775 dot_scop (scop_p scop)
776 {
777 auto_vec<scop_p, 1> scops;
778
779 if (scop)
780 scops.safe_push (scop);
781
782 /* When debugging, enable the following code. This cannot be used
783 in production compilers because it calls "system". */
784 #if 0
785 {
786 int x;
787 FILE *stream = fopen ("/tmp/allscops.dot", "w");
788 gcc_assert (stream);
789
790 dot_all_scops_1 (stream, scops);
791 fclose (stream);
792 x = system ("dotty /tmp/allscops.dot &");
793 }
794 #else
795 dot_all_scops_1 (stderr, scops);
796 #endif
797 }
798
799 /* Return true when the body of LOOP has statements that can be represented as a
800 valid scop. */
801
802 static bool
803 loop_body_is_valid_scop (loop_p loop, sese_l scop)
804 {
805 if (!loop_nest_has_data_refs (loop))
806 {
807 DEBUG_PRINT (dp << "[scop-detection-fail] loop_"
808 << loop->num << "does not have any data reference.\n");
809 return false;
810 }
811
812 basic_block *bbs = get_loop_body (loop);
813 for (unsigned i = 0; i < loop->num_nodes; i++)
814 {
815 basic_block bb = bbs[i];
816
817 if (harmful_stmt_in_bb (scop, bb))
818 return false;
819 }
820 free (bbs);
821
822 if (loop->inner)
823 {
824 loop = loop->inner;
825 while (loop)
826 {
827 if (!loop_body_is_valid_scop (loop, scop))
828 return false;
829 loop = loop->next;
830 }
831 }
832
833 return true;
834 }
835
836 /* Build the maximal scop containing LOOP(s) and add it to SCOPS. */
837
838 class scop_builder
839 {
840 public:
841 scop_builder ()
842 : scops (vNULL)
843 { }
844
845 static sese_l invalid_sese;
846
847 vec<sese_l>
848 get_scops ()
849 {
850 return scops;
851 }
852
853 sese_l
854 get_sese (loop_p loop)
855 {
856 if (!loop)
857 return invalid_sese;
858
859 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS))
860 return invalid_sese;
861 edge scop_end = single_exit (loop);
862 if (!scop_end)
863 return invalid_sese;
864 edge scop_begin = loop_preheader_edge (loop);
865 sese_l s (scop_begin, scop_end);
866 return s;
867 }
868
869 static edge
870 get_nearest_dom_with_single_entry (basic_block dom)
871 {
872 if (!dom->preds)
873 return NULL;
874 /* If e1->src dominates e2->src then e1->src will also dominate dom. */
875 if (dom->preds->length () == 2)
876 {
877 edge e1 = (*dom->preds)[0];
878 edge e2 = (*dom->preds)[1];
879 if (dominated_by_p (CDI_DOMINATORS, e2->src, e1->src))
880 return e1;
881 if (dominated_by_p (CDI_DOMINATORS, e1->src, e2->src))
882 return e2;
883 }
884
885 while (dom->preds->length () != 1)
886 {
887 if (dom->preds->length () < 1)
888 return NULL;
889 dom = get_immediate_dominator (CDI_DOMINATORS, dom);
890 if (!dom->preds)
891 return NULL;
892 }
893 return (*dom->preds)[0];
894 }
895
896 static edge
897 get_nearest_pdom_with_single_exit (basic_block dom)
898 {
899 if (!dom->succs)
900 return NULL;
901 if (dom->succs->length () == 2)
902 {
903 edge e1 = (*dom->succs)[0];
904 edge e2 = (*dom->succs)[1];
905 if (dominated_by_p (CDI_POST_DOMINATORS, e2->dest, e1->dest))
906 return e1;
907 if (dominated_by_p (CDI_POST_DOMINATORS, e1->dest, e2->dest))
908 return e2;
909 }
910
911 while (dom->succs->length () != 1)
912 {
913 if (dom->succs->length () < 1)
914 return NULL;
915 dom = get_immediate_dominator (CDI_POST_DOMINATORS, dom);
916 if (!dom->succs)
917 return NULL;
918 }
919 return (*dom->succs)[0];
920 }
921
922 /* Print S to FILE. */
923
924 static void
925 print_sese (FILE *file, sese_l s)
926 {
927 fprintf (file, "(entry_edge (bb_%d, bb_%d), exit_edge (bb_%d, bb_%d))\n",
928 s.entry->src->index, s.entry->dest->index,
929 s.exit->src->index, s.exit->dest->index);
930 }
931
932 /* Merge scops at same loop depth and returns the new sese.
933 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
934
935 static sese_l
936 merge_sese (sese_l first, sese_l second)
937 {
938 /* In the trivial case first/second may be NULL. */
939 if (!first)
940 return second;
941 if (!second)
942 return first;
943
944 DEBUG_PRINT (dp << "[try-merging-sese] s1: ";
945 print_sese (dump_file, first);
946 dp << "[try-merging-sese] s2: ";
947 print_sese (dump_file, second));
948
949 /* Assumption: Both the sese's should be at the same loop depth or one scop
950 should subsume the other like in case of nested loops. */
951
952 /* Find the common dominators for entry,
953 and common post-dominators for the exit. */
954 basic_block dom = nearest_common_dominator (CDI_DOMINATORS,
955 get_entry_bb (first.entry),
956 get_entry_bb (second.entry));
957
958
959 edge entry = get_nearest_dom_with_single_entry (dom);
960 if (!entry)
961 return invalid_sese;
962
963 basic_block pdom = nearest_common_dominator (CDI_POST_DOMINATORS,
964 get_exit_bb (first.exit),
965 get_exit_bb (second.exit));
966 pdom = nearest_common_dominator (CDI_POST_DOMINATORS, dom, pdom);
967
968 edge exit = get_nearest_pdom_with_single_exit (pdom);
969 if (!exit)
970 return invalid_sese;
971
972 sese_l combined (entry, exit);
973
974 /* FIXME: We could iterate to find the dom which dominates pdom, and pdom
975 which post-dominates dom, until it stabilizes. Also, ENTRY->SRC and
976 EXIT->DEST should be in the same loop nest. */
977 if (!dominated_by_p (CDI_DOMINATORS, pdom, dom)
978 || loop_depth (entry->src->loop_father)
979 != loop_depth (exit->dest->loop_father))
980 return invalid_sese;
981
982 /* For now we just want to bail out when exit does not post-dominate entry.
983 TODO: We might just add a basic_block at the exit to make exit
984 post-dominate entry (the entire region). */
985 if (!dominated_by_p (CDI_POST_DOMINATORS, get_entry_bb (entry),
986 get_exit_bb (exit))
987 || !dominated_by_p (CDI_DOMINATORS, get_exit_bb (exit),
988 get_entry_bb (entry)))
989 {
990 DEBUG_PRINT (dp << "[scop-detection-fail] cannot merge seses.\n");
991 return invalid_sese;
992 }
993
994 /* FIXME: We should remove this piece of code once
995 canonicalize_loop_closed_ssa has been removed, because that function
996 adds a BB with single exit. */
997 if (!trivially_empty_bb_p (get_exit_bb (combined.exit)))
998 {
999 /* Find the first empty succ (with single exit) of combined.exit. */
1000 basic_block imm_succ = combined.exit->dest;
1001 if (single_succ_p (imm_succ) && trivially_empty_bb_p (imm_succ))
1002 combined.exit = single_succ_edge (imm_succ);
1003 else
1004 {
1005 DEBUG_PRINT (dp << "\n[scop-detection-fail] Discarding SCoP because "
1006 << "no single exit (empty succ) for sese exit";
1007 print_sese (dump_file, combined));
1008 return invalid_sese;
1009 }
1010 }
1011
1012 /* Analyze all the BBs in new sese. */
1013 if (harmful_stmt_in_region (combined))
1014 return invalid_sese;
1015
1016 DEBUG_PRINT (dp << "[merged-sese] s1: ";
1017 print_sese (dump_file, combined));
1018
1019 return combined;
1020 }
1021
1022 /* Build scop outer->inner if possible. */
1023 sese_l
1024 build_scop_depth (sese_l s, loop_p loop)
1025 {
1026 if (!loop)
1027 return s;
1028
1029 DEBUG_PRINT (dp << "\n[Depth loop_" << loop->num << "]");
1030 s = build_scop_depth (s, loop->inner);
1031
1032 sese_l s2 = merge_sese (s, get_sese (loop));
1033 if (!s2)
1034 {
1035 /* s might be a valid scop, so return it and start analyzing from the
1036 adjacent loop. */
1037 build_scop_depth (invalid_sese, loop->next);
1038 return s;
1039 }
1040
1041 if (!loop_is_valid_scop (loop, s2))
1042 return build_scop_depth (invalid_sese, loop->next);
1043
1044 return build_scop_breadth (s2, loop);
1045 }
1046
1047 /* If loop and loop->next are valid scops, try to merge them. */
1048
1049 sese_l
1050 build_scop_breadth (sese_l s1, loop_p loop)
1051 {
1052 if (!loop)
1053 return s1;
1054 DEBUG_PRINT (dp << "\n[Breadth loop_" << loop->num << "]");
1055 gcc_assert (s1);
1056
1057 loop_p l = loop;
1058 sese_l s2 = build_scop_depth (invalid_sese, l->next);
1059 if (!s2)
1060 {
1061 if (s1)
1062 add_scop (s1);
1063 return s1;
1064 }
1065
1066 sese_l combined = merge_sese (s1, s2);
1067
1068 if (combined)
1069 s1 = combined;
1070 else
1071 add_scop (s2);
1072
1073 if (s1)
1074 add_scop (s1);
1075 return s1;
1076 }
1077
1078 /* Returns true when Graphite can represent LOOP in SCOP.
1079 FIXME: For the moment, graphite cannot be used on loops that iterate using
1080 induction variables that wrap. */
1081 static bool
1082 can_represent_loop_1 (loop_p loop, sese_l scop)
1083 {
1084 tree niter;
1085 struct tree_niter_desc niter_desc;
1086
1087 return single_exit (loop)
1088 && number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
1089 && niter_desc.control.no_overflow
1090 && (niter = number_of_latch_executions (loop))
1091 && !chrec_contains_undetermined (niter)
1092 && graphite_can_represent_expr (scop, loop, niter);
1093 }
1094
1095 /* Return true when all the loops within LOOP can be represented by
1096 Graphite. */
1097
1098 static bool
1099 can_represent_loop (loop_p loop, sese_l scop)
1100 {
1101 if (!can_represent_loop_1 (loop, scop))
1102 return false;
1103 if (loop->inner && !can_represent_loop (loop->inner, scop))
1104 return false;
1105 if (loop->next && !can_represent_loop (loop->next, scop))
1106 return false;
1107
1108 return true;
1109 }
1110
1111 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
1112 region of code that can be represented in the polyhedral model. SCOP
1113 defines the region we analyse. */
1114
1115 static bool
1116 loop_is_valid_scop (loop_p loop, sese_l scop)
1117 {
1118 if (!scop)
1119 return false;
1120
1121 if (!can_represent_loop (loop, scop))
1122 {
1123 DEBUG_PRINT (dp << "[scop-detection-fail] cannot represent loop_"
1124 << loop->num << "\n");
1125 return false;
1126 }
1127
1128 if (loop_body_is_valid_scop (loop, scop))
1129 {
1130 DEBUG_PRINT (dp << "[valid-scop] loop_"
1131 << loop->num << "is a valid scop.\n");
1132 return true;
1133 }
1134 return false;
1135 }
1136
1137 /* Return true when BEGIN is the preheader edge of a loop with a single exit
1138 END. */
1139
1140 static bool
1141 region_has_one_loop (sese_l s)
1142 {
1143 edge begin = s.entry;
1144 edge end = s.exit;
1145 /* Check for a single perfectly nested loop. */
1146 if (begin->dest->loop_father->inner)
1147 return false;
1148
1149 /* Otherwise, check whether we have adjacent loops. */
1150 return begin->dest->loop_father == end->src->loop_father;
1151 }
1152
1153 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
1154
1155 void
1156 add_scop (sese_l s)
1157 {
1158 gcc_assert (s);
1159
1160 /* Do not add scops with only one loop. */
1161 if (region_has_one_loop (s))
1162 {
1163 DEBUG_PRINT (dp << "\n[scop-detection-fail] Discarding one loop SCoP";
1164 print_sese (dump_file, s));
1165 return;
1166 }
1167
1168 if (get_exit_bb (s.exit) == EXIT_BLOCK_PTR_FOR_FN (cfun))
1169 {
1170 DEBUG_PRINT (dp << "\n[scop-detection-fail] "
1171 << "Discarding SCoP exiting to return";
1172 print_sese (dump_file, s));
1173 return;
1174 }
1175
1176 /* Remove all the scops which are subsumed by s. */
1177 remove_subscops (s);
1178
1179 /* Replace this with split-intersecting scops. */
1180 remove_intersecting_scops (s);
1181
1182 scops.safe_push (s);
1183 DEBUG_PRINT (dp << "\nAdding SCoP "; print_sese (dump_file, s));
1184 }
1185
1186 /* Return true when a statement in SCOP cannot be represented by Graphite.
1187 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
1188 Limit the number of bbs between adjacent loops to
1189 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
1190
1191 static bool
1192 harmful_stmt_in_region (sese_l scop)
1193 {
1194 basic_block exit_bb = get_exit_bb (scop.exit);
1195 basic_block entry_bb = get_entry_bb (scop.entry);
1196
1197 DEBUG_PRINT (dp << "\n[checking-harmful-bbs] ";
1198 print_sese (dump_file, scop));
1199 gcc_assert (dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb));
1200
1201 int depth = bb_dom_dfs_in (CDI_DOMINATORS, exit_bb)
1202 - bb_dom_dfs_in (CDI_DOMINATORS, entry_bb);
1203
1204 gcc_assert (depth >0);
1205
1206 vec<basic_block> dom = get_dominated_to_depth (CDI_DOMINATORS,
1207 entry_bb, depth);
1208 int i;
1209 basic_block bb;
1210 FOR_EACH_VEC_ELT (dom, i, bb)
1211 {
1212 DEBUG_PRINT (dp << "\nVisiting bb_" << bb->index);
1213
1214 /* We don't want to analyze any bb outside sese. */
1215 if (!dominated_by_p (CDI_POST_DOMINATORS, bb, exit_bb))
1216 continue;
1217
1218 if (harmful_stmt_in_bb (scop, bb))
1219 return true;
1220 }
1221
1222 return false;
1223 }
1224
1225 /* Returns true if S1 subsumes/surrounds S2. */
1226 static bool
1227 subsumes (sese_l s1, sese_l s2)
1228 {
1229 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2.entry),
1230 get_entry_bb (s1.entry))
1231 && dominated_by_p (CDI_POST_DOMINATORS, get_entry_bb (s2.exit),
1232 get_entry_bb (s1.exit)))
1233 return true;
1234 return false;
1235 }
1236
1237 /* Remove a SCoP which is subsumed by S1. */
1238 void
1239 remove_subscops (sese_l s1)
1240 {
1241 int j;
1242 sese_l s2 (0);
1243 FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
1244 {
1245 if (subsumes (s1, s2))
1246 {
1247 DEBUG_PRINT (dp << "\nRemoving sub-SCoP";
1248 print_sese (dump_file, s2));
1249 scops.unordered_remove (j);
1250 }
1251 }
1252 }
1253
1254 /* Returns true if S1 intersects with S2. Since we already know that S1 does
1255 not subsume S2 or vice-versa, we only check for entry bbs. */
1256
1257 static bool
1258 intersects (sese_l s1, sese_l s2)
1259 {
1260 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2.entry),
1261 get_entry_bb (s1.entry))
1262 && !dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2.entry),
1263 get_exit_bb (s1.exit)))
1264 return true;
1265 if ((s1.exit == s2.entry)
1266 || (s2.exit == s1.entry))
1267 return true;
1268
1269 return false;
1270 }
1271
1272 /* Remove one of the scops when it intersects with any other. */
1273
1274 void
1275 remove_intersecting_scops (sese_l s1)
1276 {
1277 int j;
1278 sese_l s2 (0);
1279 FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
1280 {
1281 if (intersects (s1, s2))
1282 {
1283 DEBUG_PRINT (dp << "\nRemoving intersecting SCoP";
1284 print_sese (dump_file, s2);
1285 dp << "Intersects with:";
1286 print_sese (dump_file, s1));
1287 scops.unordered_remove (j);
1288 }
1289 }
1290 }
1291
1292 private:
1293 vec<sese_l> scops;
1294 };
1295
1296 sese_l scop_builder::invalid_sese (0);
1297
1298 /* Find Static Control Parts (SCoP) in the current function and pushes
1299 them to SCOPS. */
1300
1301 void
1302 build_scops (vec<scop_p> *scops)
1303 {
1304 if (dump_file)
1305 dp.set_dump_file (dump_file);
1306
1307 canonicalize_loop_closed_ssa_form ();
1308
1309 scop_builder sb;
1310 sb.build_scop_depth (scop_builder::invalid_sese, current_loops->tree_root);
1311
1312 /* Now create scops from the lightweight SESEs. */
1313 vec<sese_l> scops_l = sb.get_scops ();
1314 int i;
1315 sese_l s (0);
1316 FOR_EACH_VEC_ELT (scops_l, i, s)
1317 {
1318 sese sese_reg = new_sese (s.entry, s.exit);
1319 scops->safe_push (new_scop (sese_reg));
1320 }
1321
1322 DEBUG_PRINT (dp << "number of SCoPs: " << (scops ? scops->length () : 0););
1323 }
1324
1325 #endif /* HAVE_isl */