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