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