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