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