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