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