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