]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/graphite-scop-detection.c
improve debug of codegen
[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 /* A marker for invalid sese_l. */
526 static sese_l invalid_sese;
527
528 /* Return the SCOPS in this SCOP_DETECTION. */
529
530 vec<sese_l>
531 get_scops ()
532 {
533 return scops;
534 }
535
536 /* Return an sese_l around the LOOP. */
537
538 sese_l get_sese (loop_p loop);
539
540 /* Return the closest dominator with a single entry edge. In case of a
541 back-loop the back-edge is not counted. */
542
543 static edge get_nearest_dom_with_single_entry (basic_block dom);
544
545 /* Return the closest post-dominator with a single exit edge. In case of a
546 back-loop the back-edge is not counted. */
547
548 static edge get_nearest_pdom_with_single_exit (basic_block dom);
549
550 /* Print S to FILE. */
551
552 static void print_sese (FILE *file, sese_l s);
553
554 /* Merge scops at same loop depth and returns the new sese.
555 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
556
557 sese_l merge_sese (sese_l first, sese_l second) const;
558
559 /* Build scop outer->inner if possible. */
560
561 sese_l build_scop_depth (sese_l s, loop_p loop);
562
563 /* If loop and loop->next are valid scops, try to merge them. */
564
565 sese_l build_scop_breadth (sese_l s1, loop_p loop);
566
567 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
568 region of code that can be represented in the polyhedral model. SCOP
569 defines the region we analyse. */
570
571 bool loop_is_valid_scop (loop_p loop, sese_l scop) const;
572
573 /* Return true when BEGIN is the preheader edge of a loop with a single exit
574 END. */
575
576 static bool region_has_one_loop (sese_l s);
577
578 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
579
580 void add_scop (sese_l s);
581
582 /* Returns true if S1 subsumes/surrounds S2. */
583 static bool subsumes (sese_l s1, sese_l s2);
584
585 /* Remove a SCoP which is subsumed by S1. */
586 void remove_subscops (sese_l s1);
587
588 /* Returns true if S1 intersects with S2. Since we already know that S1 does
589 not subsume S2 or vice-versa, we only check for entry bbs. */
590
591 static bool intersects (sese_l s1, sese_l s2);
592
593 /* Remove one of the scops when it intersects with any other. */
594
595 void remove_intersecting_scops (sese_l s1);
596
597 /* Return true when the body of LOOP has statements that can be represented
598 as a valid scop. */
599
600 bool loop_body_is_valid_scop (loop_p loop, sese_l scop) const;
601
602 /* Return true when BB contains a harmful operation for a scop: that
603 can be a function call with side effects, the induction variables
604 are not linear with respect to SCOP, etc. The current open
605 scop should end before this statement. */
606
607 bool harmful_stmt_in_bb (sese_l scop, basic_block bb) const;
608
609 /* Return true when a statement in SCOP cannot be represented by Graphite.
610 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
611 Limit the number of bbs between adjacent loops to
612 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
613
614 bool harmful_stmt_in_region (sese_l scop) const;
615
616 /* Return true only when STMT is simple enough for being handled by Graphite.
617 This depends on SCOP, as the parameters are initialized relatively to
618 this basic block, the linear functions are initialized based on the
619 outermost loop containing STMT inside the SCOP. BB is the place where we
620 try to evaluate the STMT. */
621
622 bool stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
623 basic_block bb) const;
624
625 /* Something like "n * m" is not allowed. */
626
627 static bool graphite_can_represent_init (tree e);
628
629 /* Return true when SCEV can be represented in the polyhedral model.
630
631 An expression can be represented, if it can be expressed as an
632 affine expression. For loops (i, j) and parameters (m, n) all
633 affine expressions are of the form:
634
635 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
636
637 1 i + 20 j + (-2) m + 25
638
639 Something like "i * n" or "n * m" is not allowed. */
640
641 static bool graphite_can_represent_scev (tree scev);
642
643 /* Return true when EXPR can be represented in the polyhedral model.
644
645 This means an expression can be represented, if it is linear with respect
646 to the loops and the strides are non parametric. LOOP is the place where
647 the expr will be evaluated. SCOP defines the region we analyse. */
648
649 static bool graphite_can_represent_expr (sese_l scop, loop_p loop,
650 tree expr);
651
652 /* Return true if the data references of STMT can be represented by Graphite.
653 We try to analyze the data references in a loop contained in the SCOP. */
654
655 static bool stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt);
656
657 /* Remove the close phi node at GSI and replace its rhs with the rhs
658 of PHI. */
659
660 static void remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi);
661
662 /* Returns true when Graphite can represent LOOP in SCOP.
663 FIXME: For the moment, graphite cannot be used on loops that iterate using
664 induction variables that wrap. */
665
666 static bool can_represent_loop_1 (loop_p loop, sese_l scop);
667
668 /* Return true when all the loops within LOOP can be represented by
669 Graphite. */
670
671 static bool can_represent_loop (loop_p loop, sese_l scop);
672
673 /* Returns the number of pbbs that are in loops contained in SCOP. */
674
675 static int nb_pbbs_in_loops (scop_p scop);
676
677 static bool graphite_can_represent_stmt (sese_l, gimple *, basic_block);
678
679 private:
680 vec<sese_l> scops;
681 };
682
683 sese_l scop_detection::invalid_sese (NULL, NULL);
684
685 /* Return an sese_l around the LOOP. */
686
687 sese_l
688 scop_detection::get_sese (loop_p loop)
689 {
690 if (!loop)
691 return invalid_sese;
692
693 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS))
694 return invalid_sese;
695 edge scop_end = single_exit (loop);
696 if (!scop_end)
697 return invalid_sese;
698 edge scop_begin = loop_preheader_edge (loop);
699 sese_l s (scop_begin, scop_end);
700 return s;
701 }
702
703 /* Return the closest dominator with a single entry edge. */
704
705 edge
706 scop_detection::get_nearest_dom_with_single_entry (basic_block dom)
707 {
708 if (!dom->preds)
709 return NULL;
710 /* If e1->src dominates e2->src then e1->src will also dominate dom. */
711 if (dom->preds->length () == 2)
712 {
713 edge e1 = (*dom->preds)[0];
714 edge e2 = (*dom->preds)[1];
715 if (dominated_by_p (CDI_DOMINATORS, e2->src, e1->src))
716 return e1;
717 if (dominated_by_p (CDI_DOMINATORS, e1->src, e2->src))
718 return e2;
719 }
720
721 while (dom->preds->length () != 1)
722 {
723 if (dom->preds->length () < 1)
724 return NULL;
725 dom = get_immediate_dominator (CDI_DOMINATORS, dom);
726 if (!dom->preds)
727 return NULL;
728 }
729 return (*dom->preds)[0];
730 }
731
732 /* Return the closest post-dominator with a single exit edge. In case of a
733 back-loop the back-edge is not counted. */
734
735 edge
736 scop_detection::get_nearest_pdom_with_single_exit (basic_block dom)
737 {
738 if (!dom->succs)
739 return NULL;
740 if (dom->succs->length () == 2)
741 {
742 edge e1 = (*dom->succs)[0];
743 edge e2 = (*dom->succs)[1];
744 if (dominated_by_p (CDI_POST_DOMINATORS, e2->dest, e1->dest))
745 return e1;
746 if (dominated_by_p (CDI_POST_DOMINATORS, e1->dest, e2->dest))
747 return e2;
748 }
749
750 while (dom->succs->length () != 1)
751 {
752 if (dom->succs->length () < 1)
753 return NULL;
754 dom = get_immediate_dominator (CDI_POST_DOMINATORS, dom);
755 if (!dom->succs)
756 return NULL;
757 }
758 return (*dom->succs)[0];
759 }
760
761 /* Print S to FILE. */
762
763 void
764 scop_detection::print_sese (FILE *file, sese_l s)
765 {
766 fprintf (file, "(entry_edge (bb_%d, bb_%d), exit_edge (bb_%d, bb_%d))\n",
767 s.entry->src->index, s.entry->dest->index,
768 s.exit->src->index, s.exit->dest->index);
769 }
770
771 /* Merge scops at same loop depth and returns the new sese.
772 Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
773
774 sese_l
775 scop_detection::merge_sese (sese_l first, sese_l second) const
776 {
777 /* In the trivial case first/second may be NULL. */
778 if (!first)
779 return second;
780 if (!second)
781 return first;
782
783 DEBUG_PRINT (dp << "[try-merging-sese] s1: "; print_sese (dump_file, first);
784 dp << "[try-merging-sese] s2: ";
785 print_sese (dump_file, second));
786
787 /* Assumption: Both the sese's should be at the same loop depth or one scop
788 should subsume the other like in case of nested loops. */
789
790 /* Find the common dominators for entry,
791 and common post-dominators for the exit. */
792 basic_block dom = nearest_common_dominator (CDI_DOMINATORS,
793 get_entry_bb (first),
794 get_entry_bb (second));
795
796 edge entry = get_nearest_dom_with_single_entry (dom);
797 if (!entry)
798 return invalid_sese;
799
800 basic_block pdom = nearest_common_dominator (CDI_POST_DOMINATORS,
801 get_exit_bb (first),
802 get_exit_bb (second));
803 pdom = nearest_common_dominator (CDI_POST_DOMINATORS, dom, pdom);
804
805 edge exit = get_nearest_pdom_with_single_exit (pdom);
806 if (!exit)
807 return invalid_sese;
808
809 sese_l combined (entry, exit);
810
811 /* FIXME: We could iterate to find the dom which dominates pdom, and pdom
812 which post-dominates dom, until it stabilizes. Also, ENTRY->SRC and
813 EXIT->DEST should be in the same loop nest. */
814 if (!dominated_by_p (CDI_DOMINATORS, pdom, dom)
815 || loop_depth (entry->src->loop_father)
816 != loop_depth (exit->dest->loop_father))
817 return invalid_sese;
818
819 /* For now we just want to bail out when exit does not post-dominate entry.
820 TODO: We might just add a basic_block at the exit to make exit
821 post-dominate entry (the entire region). */
822 if (!dominated_by_p (CDI_POST_DOMINATORS, get_entry_bb (combined),
823 get_exit_bb (combined))
824 || !dominated_by_p (CDI_DOMINATORS, get_exit_bb (combined),
825 get_entry_bb (combined)))
826 {
827 DEBUG_PRINT (dp << "[scop-detection-fail] cannot merge seses.\n");
828 return invalid_sese;
829 }
830
831 /* FIXME: We should remove this piece of code once
832 canonicalize_loop_closed_ssa has been removed, because that function
833 adds a BB with single exit. */
834 if (!trivially_empty_bb_p (get_exit_bb (combined)))
835 {
836 /* Find the first empty succ (with single exit) of combined.exit. */
837 basic_block imm_succ = combined.exit->dest;
838 if (single_succ_p (imm_succ) && trivially_empty_bb_p (imm_succ))
839 combined.exit = single_succ_edge (imm_succ);
840 else
841 {
842 DEBUG_PRINT (dp << "\n[scop-detection-fail] Discarding SCoP because "
843 << "no single exit (empty succ) for sese exit";
844 print_sese (dump_file, combined));
845 return invalid_sese;
846 }
847 }
848
849 /* Analyze all the BBs in new sese. */
850 if (harmful_stmt_in_region (combined))
851 return invalid_sese;
852
853 DEBUG_PRINT (dp << "[merged-sese] s1: "; print_sese (dump_file, combined));
854
855 return combined;
856 }
857
858 /* Build scop outer->inner if possible. */
859
860 sese_l
861 scop_detection::build_scop_depth (sese_l s, loop_p loop)
862 {
863 if (!loop)
864 return s;
865
866 DEBUG_PRINT (dp << "\n[Depth loop_" << loop->num << "]");
867 s = build_scop_depth (s, loop->inner);
868
869 sese_l s2 = merge_sese (s, get_sese (loop));
870 if (!s2)
871 {
872 /* s might be a valid scop, so return it and start analyzing from the
873 adjacent loop. */
874 build_scop_depth (invalid_sese, loop->next);
875 return s;
876 }
877
878 if (!loop_is_valid_scop (loop, s2))
879 return build_scop_depth (invalid_sese, loop->next);
880
881 return build_scop_breadth (s2, loop);
882 }
883
884 /* If loop and loop->next are valid scops, try to merge them. */
885
886 sese_l
887 scop_detection::build_scop_breadth (sese_l s1, loop_p loop)
888 {
889 if (!loop)
890 return s1;
891 DEBUG_PRINT (dp << "\n[Breadth loop_" << loop->num << "]");
892 gcc_assert (s1);
893
894 loop_p l = loop;
895 sese_l s2 = build_scop_depth (invalid_sese, l->next);
896 if (!s2)
897 {
898 if (s1)
899 add_scop (s1);
900 return s1;
901 }
902
903 sese_l combined = merge_sese (s1, s2);
904
905 if (combined)
906 s1 = combined;
907 else
908 add_scop (s2);
909
910 if (s1)
911 add_scop (s1);
912 return s1;
913 }
914
915 /* Returns true when Graphite can represent LOOP in SCOP.
916 FIXME: For the moment, graphite cannot be used on loops that iterate using
917 induction variables that wrap. */
918
919 bool
920 scop_detection::can_represent_loop_1 (loop_p loop, sese_l scop)
921 {
922 tree niter;
923 struct tree_niter_desc niter_desc;
924
925 return single_exit (loop)
926 && number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
927 && niter_desc.control.no_overflow
928 && (niter = number_of_latch_executions (loop))
929 && !chrec_contains_undetermined (niter)
930 && graphite_can_represent_expr (scop, loop, niter);
931 }
932
933 /* Return true when all the loops within LOOP can be represented by
934 Graphite. */
935
936 bool
937 scop_detection::can_represent_loop (loop_p loop, sese_l scop)
938 {
939 if (!can_represent_loop_1 (loop, scop))
940 return false;
941 if (loop->inner && !can_represent_loop (loop->inner, scop))
942 return false;
943 if (loop->next && !can_represent_loop (loop->next, scop))
944 return false;
945
946 return true;
947 }
948
949 /* Return true when LOOP is a valid scop, that is a Static Control Part, a
950 region of code that can be represented in the polyhedral model. SCOP
951 defines the region we analyse. */
952
953 bool
954 scop_detection::loop_is_valid_scop (loop_p loop, sese_l scop) const
955 {
956 if (!scop)
957 return false;
958
959 if (!can_represent_loop (loop, scop))
960 {
961 DEBUG_PRINT (dp << "[scop-detection-fail] cannot represent loop_"
962 << loop->num << "\n");
963 return false;
964 }
965
966 if (loop_body_is_valid_scop (loop, scop))
967 {
968 DEBUG_PRINT (dp << "[valid-scop] loop_" << loop->num
969 << "is a valid scop.\n");
970 return true;
971 }
972 return false;
973 }
974
975 /* Return true when BEGIN is the preheader edge of a loop with a single exit
976 END. */
977
978 bool
979 scop_detection::region_has_one_loop (sese_l s)
980 {
981 edge begin = s.entry;
982 edge end = s.exit;
983 /* Check for a single perfectly nested loop. */
984 if (begin->dest->loop_father->inner)
985 return false;
986
987 /* Otherwise, check whether we have adjacent loops. */
988 return begin->dest->loop_father == end->src->loop_father;
989 }
990
991 /* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
992
993 void
994 scop_detection::add_scop (sese_l s)
995 {
996 gcc_assert (s);
997
998 /* Do not add scops with only one loop. */
999 if (region_has_one_loop (s))
1000 {
1001 DEBUG_PRINT (dp << "\n[scop-detection-fail] Discarding one loop SCoP";
1002 print_sese (dump_file, s));
1003 return;
1004 }
1005
1006 if (get_exit_bb (s) == EXIT_BLOCK_PTR_FOR_FN (cfun))
1007 {
1008 DEBUG_PRINT (dp << "\n[scop-detection-fail] "
1009 << "Discarding SCoP exiting to return";
1010 print_sese (dump_file, s));
1011 return;
1012 }
1013
1014 /* Remove all the scops which are subsumed by s. */
1015 remove_subscops (s);
1016
1017 /* Replace this with split-intersecting scops. */
1018 remove_intersecting_scops (s);
1019
1020 scops.safe_push (s);
1021 DEBUG_PRINT (dp << "\nAdding SCoP "; print_sese (dump_file, s));
1022 }
1023
1024 /* Return true when a statement in SCOP cannot be represented by Graphite.
1025 The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
1026 Limit the number of bbs between adjacent loops to
1027 PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
1028
1029 bool
1030 scop_detection::harmful_stmt_in_region (sese_l scop) const
1031 {
1032 basic_block exit_bb = get_exit_bb (scop);
1033 basic_block entry_bb = get_entry_bb (scop);
1034
1035 DEBUG_PRINT (dp << "\n[checking-harmful-bbs] ";
1036 print_sese (dump_file, scop));
1037 gcc_assert (dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb));
1038
1039 int depth = bb_dom_dfs_in (CDI_DOMINATORS, exit_bb)
1040 - bb_dom_dfs_in (CDI_DOMINATORS, entry_bb);
1041
1042 gcc_assert (depth > 0);
1043
1044 vec<basic_block> dom
1045 = get_dominated_to_depth (CDI_DOMINATORS, entry_bb, depth);
1046 int i;
1047 basic_block bb;
1048 FOR_EACH_VEC_ELT (dom, i, bb)
1049 {
1050 DEBUG_PRINT (dp << "\nVisiting bb_" << bb->index);
1051
1052 /* We don't want to analyze any bb outside sese. */
1053 if (!dominated_by_p (CDI_POST_DOMINATORS, bb, exit_bb))
1054 continue;
1055
1056 if (harmful_stmt_in_bb (scop, bb))
1057 return true;
1058 }
1059
1060 return false;
1061 }
1062
1063 /* Returns true if S1 subsumes/surrounds S2. */
1064 bool
1065 scop_detection::subsumes (sese_l s1, sese_l s2)
1066 {
1067 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
1068 get_entry_bb (s1))
1069 && dominated_by_p (CDI_POST_DOMINATORS, s2.exit->dest,
1070 s1.exit->dest))
1071 return true;
1072 return false;
1073 }
1074
1075 /* Remove a SCoP which is subsumed by S1. */
1076 void
1077 scop_detection::remove_subscops (sese_l s1)
1078 {
1079 int j;
1080 sese_l *s2;
1081 FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
1082 {
1083 if (subsumes (s1, *s2))
1084 {
1085 DEBUG_PRINT (dp << "\nRemoving sub-SCoP";
1086 print_sese (dump_file, *s2));
1087 scops.unordered_remove (j);
1088 }
1089 }
1090 }
1091
1092 /* Returns true if S1 intersects with S2. Since we already know that S1 does
1093 not subsume S2 or vice-versa, we only check for entry bbs. */
1094
1095 bool
1096 scop_detection::intersects (sese_l s1, sese_l s2)
1097 {
1098 if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
1099 get_entry_bb (s1))
1100 && !dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
1101 get_exit_bb (s1)))
1102 return true;
1103 if ((s1.exit == s2.entry) || (s2.exit == s1.entry))
1104 return true;
1105
1106 return false;
1107 }
1108
1109 /* Remove one of the scops when it intersects with any other. */
1110
1111 void
1112 scop_detection::remove_intersecting_scops (sese_l s1)
1113 {
1114 int j;
1115 sese_l *s2;
1116 FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
1117 {
1118 if (intersects (s1, *s2))
1119 {
1120 DEBUG_PRINT (dp << "\nRemoving intersecting SCoP";
1121 print_sese (dump_file, *s2); dp << "Intersects with:";
1122 print_sese (dump_file, s1));
1123 scops.unordered_remove (j);
1124 }
1125 }
1126 }
1127
1128 /* Something like "n * m" is not allowed. */
1129
1130 bool
1131 scop_detection::graphite_can_represent_init (tree e)
1132 {
1133 switch (TREE_CODE (e))
1134 {
1135 case POLYNOMIAL_CHREC:
1136 return graphite_can_represent_init (CHREC_LEFT (e))
1137 && graphite_can_represent_init (CHREC_RIGHT (e));
1138
1139 case MULT_EXPR:
1140 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
1141 return graphite_can_represent_init (TREE_OPERAND (e, 0))
1142 && tree_fits_shwi_p (TREE_OPERAND (e, 1));
1143 else
1144 return graphite_can_represent_init (TREE_OPERAND (e, 1))
1145 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
1146
1147 case PLUS_EXPR:
1148 case POINTER_PLUS_EXPR:
1149 case MINUS_EXPR:
1150 return graphite_can_represent_init (TREE_OPERAND (e, 0))
1151 && graphite_can_represent_init (TREE_OPERAND (e, 1));
1152
1153 case NEGATE_EXPR:
1154 case BIT_NOT_EXPR:
1155 CASE_CONVERT:
1156 case NON_LVALUE_EXPR:
1157 return graphite_can_represent_init (TREE_OPERAND (e, 0));
1158
1159 default:
1160 break;
1161 }
1162
1163 return true;
1164 }
1165
1166 /* Return true when SCEV can be represented in the polyhedral model.
1167
1168 An expression can be represented, if it can be expressed as an
1169 affine expression. For loops (i, j) and parameters (m, n) all
1170 affine expressions are of the form:
1171
1172 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
1173
1174 1 i + 20 j + (-2) m + 25
1175
1176 Something like "i * n" or "n * m" is not allowed. */
1177
1178 bool
1179 scop_detection::graphite_can_represent_scev (tree scev)
1180 {
1181 if (chrec_contains_undetermined (scev))
1182 return false;
1183
1184 /* We disable the handling of pointer types, because it’s currently not
1185 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
1186 the only nodes, which are disabled in case they are pointers to object
1187 types, but this can be changed. */
1188
1189 if (POINTER_TYPE_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
1190 return false;
1191
1192 switch (TREE_CODE (scev))
1193 {
1194 case NEGATE_EXPR:
1195 case BIT_NOT_EXPR:
1196 CASE_CONVERT:
1197 case NON_LVALUE_EXPR:
1198 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
1199
1200 case PLUS_EXPR:
1201 case POINTER_PLUS_EXPR:
1202 case MINUS_EXPR:
1203 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
1204 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
1205
1206 case MULT_EXPR:
1207 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
1208 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
1209 && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
1210 && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
1211 && graphite_can_represent_init (scev)
1212 && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
1213 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
1214
1215 case POLYNOMIAL_CHREC:
1216 /* Check for constant strides. With a non constant stride of
1217 'n' we would have a value of 'iv * n'. Also check that the
1218 initial value can represented: for example 'n * m' cannot be
1219 represented. */
1220 if (!evolution_function_right_is_integer_cst (scev)
1221 || !graphite_can_represent_init (scev))
1222 return false;
1223 return graphite_can_represent_scev (CHREC_LEFT (scev));
1224
1225 default:
1226 break;
1227 }
1228
1229 /* Only affine functions can be represented. */
1230 if (tree_contains_chrecs (scev, NULL) || !scev_is_linear_expression (scev))
1231 return false;
1232
1233 return true;
1234 }
1235
1236 /* Return true when EXPR can be represented in the polyhedral model.
1237
1238 This means an expression can be represented, if it is linear with respect to
1239 the loops and the strides are non parametric. LOOP is the place where the
1240 expr will be evaluated. SCOP defines the region we analyse. */
1241
1242 bool
1243 scop_detection::graphite_can_represent_expr (sese_l scop, loop_p loop,
1244 tree expr)
1245 {
1246 tree scev = scalar_evolution_in_region (scop, loop, expr);
1247 return graphite_can_represent_scev (scev);
1248 }
1249
1250 /* Return true if the data references of STMT can be represented by Graphite.
1251 We try to analyze the data references in a loop contained in the SCOP. */
1252
1253 bool
1254 scop_detection::stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt)
1255 {
1256 loop_p nest = outermost_loop_in_sese (scop, gimple_bb (stmt));
1257 loop_p loop = loop_containing_stmt (stmt);
1258 vec<data_reference_p> drs = vNULL;
1259
1260 graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
1261
1262 int j;
1263 data_reference_p dr;
1264 FOR_EACH_VEC_ELT (drs, j, dr)
1265 {
1266 int nb_subscripts = DR_NUM_DIMENSIONS (dr);
1267
1268 if (nb_subscripts < 1)
1269 {
1270 free_data_refs (drs);
1271 return false;
1272 }
1273
1274 tree ref = DR_REF (dr);
1275
1276 for (int i = nb_subscripts - 1; i >= 0; i--)
1277 {
1278 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i))
1279 || (TREE_CODE (ref) != ARRAY_REF && TREE_CODE (ref) != MEM_REF
1280 && TREE_CODE (ref) != COMPONENT_REF))
1281 {
1282 free_data_refs (drs);
1283 return false;
1284 }
1285
1286 ref = TREE_OPERAND (ref, 0);
1287 }
1288 }
1289
1290 free_data_refs (drs);
1291 return true;
1292 }
1293
1294 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
1295 Calls have side-effects, except those to const or pure
1296 functions. */
1297
1298 static bool
1299 stmt_has_side_effects (gimple *stmt)
1300 {
1301 if (gimple_has_volatile_ops (stmt)
1302 || (gimple_code (stmt) == GIMPLE_CALL
1303 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
1304 || (gimple_code (stmt) == GIMPLE_ASM))
1305 {
1306 DEBUG_PRINT (dp << "[scop-detection-fail] "
1307 << "Statement has side-effects:\n";
1308 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS | TDF_MEMSYMS));
1309 return true;
1310 }
1311 return false;
1312 }
1313
1314 /* Returns true if STMT can be represented in polyhedral model. LABEL,
1315 simple COND stmts, pure calls, and assignments can be repesented. */
1316
1317 bool
1318 scop_detection::graphite_can_represent_stmt (sese_l scop, gimple *stmt,
1319 basic_block bb)
1320 {
1321 loop_p loop = bb->loop_father;
1322 switch (gimple_code (stmt))
1323 {
1324 case GIMPLE_LABEL:
1325 return true;
1326
1327 case GIMPLE_COND:
1328 {
1329 /* We can handle all binary comparisons. Inequalities are
1330 also supported as they can be represented with union of
1331 polyhedra. */
1332 enum tree_code code = gimple_cond_code (stmt);
1333 if (!(code == LT_EXPR
1334 || code == GT_EXPR
1335 || code == LE_EXPR
1336 || code == GE_EXPR
1337 || code == EQ_EXPR
1338 || code == NE_EXPR))
1339 {
1340 DEBUG_PRINT (dp << "[scop-detection-fail] "
1341 << "Graphite cannot handle cond stmt:\n";
1342 print_gimple_stmt (dump_file, stmt, 0,
1343 TDF_VOPS | TDF_MEMSYMS));
1344 return false;
1345 }
1346
1347 for (unsigned i = 0; i < 2; ++i)
1348 {
1349 tree op = gimple_op (stmt, i);
1350 if (!graphite_can_represent_expr (scop, loop, op)
1351 /* We can only constrain on integer type. */
1352 || (TREE_CODE (TREE_TYPE (op)) != INTEGER_TYPE))
1353 {
1354 DEBUG_PRINT (dp << "[scop-detection-fail] "
1355 << "Graphite cannot represent stmt:\n";
1356 print_gimple_stmt (dump_file, stmt, 0,
1357 TDF_VOPS | TDF_MEMSYMS));
1358 return false;
1359 }
1360 }
1361
1362 return true;
1363 }
1364
1365 case GIMPLE_ASSIGN:
1366 case GIMPLE_CALL:
1367 return true;
1368
1369 default:
1370 /* These nodes cut a new scope. */
1371 DEBUG_PRINT (
1372 dp << "[scop-detection-fail] "
1373 << "Gimple stmt not handled in Graphite:\n";
1374 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS | TDF_MEMSYMS));
1375 return false;
1376 }
1377 }
1378
1379 /* Return true only when STMT is simple enough for being handled by Graphite.
1380 This depends on SCOP, as the parameters are initialized relatively to
1381 this basic block, the linear functions are initialized based on the outermost
1382 loop containing STMT inside the SCOP. BB is the place where we try to
1383 evaluate the STMT. */
1384
1385 bool
1386 scop_detection::stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
1387 basic_block bb) const
1388 {
1389 gcc_assert (scop);
1390
1391 if (is_gimple_debug (stmt))
1392 return true;
1393
1394 if (stmt_has_side_effects (stmt))
1395 return false;
1396
1397 if (!stmt_has_simple_data_refs_p (scop, stmt))
1398 {
1399 DEBUG_PRINT (dp << "[scop-detection-fail] "
1400 << "Graphite cannot handle data-refs in stmt:\n";
1401 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS););
1402 return false;
1403 }
1404
1405 return graphite_can_represent_stmt (scop, stmt, bb);
1406 }
1407
1408 /* Return true when BB contains a harmful operation for a scop: that
1409 can be a function call with side effects, the induction variables
1410 are not linear with respect to SCOP, etc. The current open
1411 scop should end before this statement. */
1412
1413 bool
1414 scop_detection::harmful_stmt_in_bb (sese_l scop, basic_block bb) const
1415 {
1416 gimple_stmt_iterator gsi;
1417
1418 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1419 if (!stmt_simple_for_scop_p (scop, gsi_stmt (gsi), bb))
1420 return true;
1421
1422 return false;
1423 }
1424
1425 /* Return true when the body of LOOP has statements that can be represented as a
1426 valid scop. */
1427
1428 bool
1429 scop_detection::loop_body_is_valid_scop (loop_p loop, sese_l scop) const
1430 {
1431 if (!loop_ivs_can_be_represented (loop))
1432 {
1433 DEBUG_PRINT (dp << "[scop-detection-fail] loop_" << loop->num
1434 << "IV cannot be represented.\n");
1435 return false;
1436 }
1437
1438 if (!loop_nest_has_data_refs (loop))
1439 {
1440 DEBUG_PRINT (dp << "[scop-detection-fail] loop_" << loop->num
1441 << "does not have any data reference.\n");
1442 return false;
1443 }
1444
1445 basic_block *bbs = get_loop_body (loop);
1446 for (unsigned i = 0; i < loop->num_nodes; i++)
1447 {
1448 basic_block bb = bbs[i];
1449
1450 if (harmful_stmt_in_bb (scop, bb))
1451 return false;
1452 }
1453 free (bbs);
1454
1455 if (loop->inner)
1456 {
1457 loop = loop->inner;
1458 while (loop)
1459 {
1460 if (!loop_body_is_valid_scop (loop, scop))
1461 return false;
1462 loop = loop->next;
1463 }
1464 }
1465
1466 return true;
1467 }
1468
1469 /* Returns the number of pbbs that are in loops contained in SCOP. */
1470
1471 int
1472 scop_detection::nb_pbbs_in_loops (scop_p scop)
1473 {
1474 int i;
1475 poly_bb_p pbb;
1476 int res = 0;
1477
1478 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
1479 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb)), scop->scop_info->region))
1480 res++;
1481
1482 return res;
1483 }
1484
1485 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
1486 Otherwise returns -1. */
1487
1488 static inline int
1489 parameter_index_in_region_1 (tree name, sese_info_p region)
1490 {
1491 int i;
1492 tree p;
1493
1494 gcc_assert (TREE_CODE (name) == SSA_NAME);
1495
1496 FOR_EACH_VEC_ELT (SESE_PARAMS (region), i, p)
1497 if (p == name)
1498 return i;
1499
1500 return -1;
1501 }
1502
1503 /* When the parameter NAME is in REGION, returns its index in
1504 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
1505 and returns the index of NAME. */
1506
1507 static int
1508 parameter_index_in_region (tree name, sese_info_p region)
1509 {
1510 int i;
1511
1512 gcc_assert (TREE_CODE (name) == SSA_NAME);
1513
1514 /* Cannot constrain on anything else than INTEGER_TYPE parameters. */
1515 if (TREE_CODE (TREE_TYPE (name)) != INTEGER_TYPE)
1516 return -1;
1517
1518 if (!invariant_in_sese_p_rec (name, region->region, NULL))
1519 return -1;
1520
1521 i = parameter_index_in_region_1 (name, region);
1522 if (i != -1)
1523 return i;
1524
1525 i = SESE_PARAMS (region).length ();
1526 SESE_PARAMS (region).safe_push (name);
1527 return i;
1528 }
1529
1530 /* In the context of sese S, scan the expression E and translate it to
1531 a linear expression C. When parsing a symbolic multiplication, K
1532 represents the constant multiplier of an expression containing
1533 parameters. */
1534
1535 static void
1536 scan_tree_for_params (sese_info_p s, tree e)
1537 {
1538 if (e == chrec_dont_know)
1539 return;
1540
1541 switch (TREE_CODE (e))
1542 {
1543 case POLYNOMIAL_CHREC:
1544 scan_tree_for_params (s, CHREC_LEFT (e));
1545 break;
1546
1547 case MULT_EXPR:
1548 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
1549 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1550 else
1551 scan_tree_for_params (s, TREE_OPERAND (e, 1));
1552 break;
1553
1554 case PLUS_EXPR:
1555 case POINTER_PLUS_EXPR:
1556 case MINUS_EXPR:
1557 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1558 scan_tree_for_params (s, TREE_OPERAND (e, 1));
1559 break;
1560
1561 case NEGATE_EXPR:
1562 case BIT_NOT_EXPR:
1563 CASE_CONVERT:
1564 case NON_LVALUE_EXPR:
1565 scan_tree_for_params (s, TREE_OPERAND (e, 0));
1566 break;
1567
1568 case SSA_NAME:
1569 parameter_index_in_region (e, s);
1570 break;
1571
1572 case INTEGER_CST:
1573 case ADDR_EXPR:
1574 case REAL_CST:
1575 case COMPLEX_CST:
1576 case VECTOR_CST:
1577 break;
1578
1579 default:
1580 gcc_unreachable ();
1581 break;
1582 }
1583 }
1584
1585 /* Find parameters with respect to REGION in BB. We are looking in memory
1586 access functions, conditions and loop bounds. */
1587
1588 static void
1589 find_params_in_bb (sese_info_p region, gimple_poly_bb_p gbb)
1590 {
1591 /* Find parameters in the access functions of data references. */
1592 int i;
1593 data_reference_p dr;
1594 FOR_EACH_VEC_ELT (GBB_DATA_REFS (gbb), i, dr)
1595 for (unsigned j = 0; j < DR_NUM_DIMENSIONS (dr); j++)
1596 scan_tree_for_params (region, DR_ACCESS_FN (dr, j));
1597
1598 /* Find parameters in conditional statements. */
1599 gimple *stmt;
1600 loop_p loop = GBB_BB (gbb)->loop_father;
1601 FOR_EACH_VEC_ELT (GBB_CONDITIONS (gbb), i, stmt)
1602 {
1603 tree lhs = scalar_evolution_in_region (region->region, loop,
1604 gimple_cond_lhs (stmt));
1605 tree rhs = scalar_evolution_in_region (region->region, loop,
1606 gimple_cond_rhs (stmt));
1607
1608 scan_tree_for_params (region, lhs);
1609 scan_tree_for_params (region, rhs);
1610 }
1611 }
1612
1613 /* Record the parameters used in the SCOP. A variable is a parameter
1614 in a scop if it does not vary during the execution of that scop. */
1615
1616 static void
1617 find_scop_parameters (scop_p scop)
1618 {
1619 unsigned i;
1620 sese_info_p region = scop->scop_info;
1621 struct loop *loop;
1622
1623 /* Find the parameters used in the loop bounds. */
1624 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), i, loop)
1625 {
1626 tree nb_iters = number_of_latch_executions (loop);
1627
1628 if (!chrec_contains_symbols (nb_iters))
1629 continue;
1630
1631 nb_iters = scalar_evolution_in_region (region->region, loop, nb_iters);
1632 scan_tree_for_params (region, nb_iters);
1633 }
1634
1635 /* Find the parameters used in data accesses. */
1636 poly_bb_p pbb;
1637 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
1638 find_params_in_bb (region, PBB_BLACK_BOX (pbb));
1639
1640 int nbp = sese_nb_params (region);
1641 scop_set_nb_params (scop, nbp);
1642 }
1643
1644 /* Generates a polyhedral black box only if the bb contains interesting
1645 information. */
1646
1647 static gimple_poly_bb_p
1648 try_generate_gimple_bb (scop_p scop, basic_block bb)
1649 {
1650 vec<data_reference_p> drs;
1651 drs.create (5);
1652 sese_l region = scop->scop_info->region;
1653 loop_p nest = outermost_loop_in_sese (region, bb);
1654
1655 loop_p loop = bb->loop_father;
1656 if (!loop_in_sese_p (loop, region))
1657 loop = nest;
1658
1659 gimple_stmt_iterator gsi;
1660 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1661 {
1662 gimple *stmt = gsi_stmt (gsi);
1663 if (is_gimple_debug (stmt))
1664 continue;
1665
1666 graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
1667 }
1668
1669 return new_gimple_poly_bb (bb, drs);
1670 }
1671
1672 /* Gather BBs and conditions for a SCOP. */
1673 class gather_bbs : public dom_walker
1674 {
1675 public:
1676 gather_bbs (cdi_direction, scop_p);
1677
1678 virtual void before_dom_children (basic_block);
1679 virtual void after_dom_children (basic_block);
1680
1681 private:
1682 auto_vec<gimple *, 3> conditions, cases;
1683 scop_p scop;
1684 };
1685 }
1686 gather_bbs::gather_bbs (cdi_direction direction, scop_p scop)
1687 : dom_walker (direction), scop (scop)
1688 {
1689 }
1690
1691 /* Call-back for dom_walk executed before visiting the dominated
1692 blocks. */
1693
1694 void
1695 gather_bbs::before_dom_children (basic_block bb)
1696 {
1697 if (!bb_in_sese_p (bb, scop->scop_info->region))
1698 return;
1699
1700 gcond *stmt = single_pred_cond_non_loop_exit (bb);
1701
1702 if (stmt)
1703 {
1704 edge e = single_pred_edge (bb);
1705
1706 conditions.safe_push (stmt);
1707
1708 if (e->flags & EDGE_TRUE_VALUE)
1709 cases.safe_push (stmt);
1710 else
1711 cases.safe_push (NULL);
1712 }
1713
1714 scop->scop_info->bbs.safe_push (bb);
1715
1716 gimple_poly_bb_p gbb = try_generate_gimple_bb (scop, bb);
1717 GBB_CONDITIONS (gbb) = conditions.copy ();
1718 GBB_CONDITION_CASES (gbb) = cases.copy ();
1719
1720 poly_bb_p pbb = new_poly_bb (scop, gbb);
1721 scop->pbbs.safe_push (pbb);
1722 }
1723
1724 /* Call-back for dom_walk executed after visiting the dominated
1725 blocks. */
1726
1727 void
1728 gather_bbs::after_dom_children (basic_block bb)
1729 {
1730 if (!bb_in_sese_p (bb, scop->scop_info->region))
1731 return;
1732
1733 if (single_pred_cond_non_loop_exit (bb))
1734 {
1735 conditions.pop ();
1736 cases.pop ();
1737 }
1738 }
1739
1740 /* Find Static Control Parts (SCoP) in the current function and pushes
1741 them to SCOPS. */
1742
1743 void
1744 build_scops (vec<scop_p> *scops)
1745 {
1746 if (dump_file)
1747 dp.set_dump_file (dump_file);
1748
1749 canonicalize_loop_closed_ssa_form ();
1750
1751 scop_detection sb;
1752 sb.build_scop_depth (scop_detection::invalid_sese, current_loops->tree_root);
1753
1754 /* Now create scops from the lightweight SESEs. */
1755 vec<sese_l> scops_l = sb.get_scops ();
1756 int i;
1757 sese_l *s;
1758 FOR_EACH_VEC_ELT (scops_l, i, s)
1759 {
1760 scop_p scop = new_scop (s->entry, s->exit);
1761
1762 /* Record all basic blocks and their conditions in REGION. */
1763 gather_bbs (CDI_DOMINATORS, scop).walk (cfun->cfg->x_entry_block_ptr);
1764
1765 /* Do not optimize a scop containing only PBBs that do not belong
1766 to any loops. */
1767 if (sb.nb_pbbs_in_loops (scop) == 0)
1768 {
1769 DEBUG_PRINT (dp << "[scop-detection-fail] no data references.\n");
1770 free_scop (scop);
1771 continue;
1772 }
1773
1774 unsigned max_arrays = PARAM_VALUE (PARAM_GRAPHITE_MAX_ARRAYS_PER_SCOP);
1775 if (scop->drs.length () >= max_arrays)
1776 {
1777 DEBUG_PRINT (dp << "[scop-detection-fail] too many data references: "
1778 << scop->drs.length ()
1779 << " is larger than --param graphite-max-arrays-per-scop="
1780 << max_arrays << ".\n");
1781 free_scop (scop);
1782 continue;
1783 }
1784
1785 build_sese_loop_nests (scop->scop_info);
1786
1787 find_scop_parameters (scop);
1788 graphite_dim_t max_dim = PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS);
1789
1790 if (scop_nb_params (scop) > max_dim)
1791 {
1792 DEBUG_PRINT (dp << "[scop-detection-fail] too many parameters: "
1793 << scop_nb_params (scop)
1794 << " larger than --param graphite-max-nb-scop-params="
1795 << max_dim << ".\n");
1796
1797 free_scop (scop);
1798 continue;
1799 }
1800
1801 scops->safe_push (scop);
1802 }
1803
1804 DEBUG_PRINT (dp << "number of SCoPs: " << (scops ? scops->length () : 0););
1805 }
1806
1807 #endif /* HAVE_isl */