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