/cp
[official-gcc.git] / gcc / graphite-scop-detection.c
blob46a55347eeba1e63b368a5faa39d0396aab0c35b
1 /* Detection of Static Control Parts (SCoP) for Graphite.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.com> and
4 Tobias Grosser <grosser@fim.uni-passau.de>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
24 #ifdef HAVE_isl
25 /* Workaround for GMP 5.1.3 bug, see PR56019. */
26 #include <stddef.h>
28 #include <isl/set.h>
29 #include <isl/map.h>
30 #include <isl/union_map.h>
31 #endif
33 #include "system.h"
34 #include "coretypes.h"
35 #include "alias.h"
36 #include "backend.h"
37 #include "cfghooks.h"
38 #include "tree.h"
39 #include "gimple.h"
40 #include "hard-reg-set.h"
41 #include "ssa.h"
42 #include "options.h"
43 #include "fold-const.h"
44 #include "internal-fn.h"
45 #include "gimple-iterator.h"
46 #include "tree-ssa-loop-manip.h"
47 #include "tree-ssa-loop-niter.h"
48 #include "tree-ssa-loop.h"
49 #include "tree-into-ssa.h"
50 #include "tree-ssa.h"
51 #include "cfgloop.h"
52 #include "tree-chrec.h"
53 #include "tree-data-ref.h"
54 #include "tree-scalar-evolution.h"
55 #include "tree-pass.h"
56 #include "sese.h"
57 #include "tree-ssa-propagate.h"
59 #ifdef HAVE_isl
60 #include "graphite-poly.h"
61 #include "graphite-scop-detection.h"
63 /* Forward declarations. */
64 static void make_close_phi_nodes_unique (basic_block);
66 /* The type of the analyzed basic block. */
68 typedef enum gbb_type {
69 GBB_UNKNOWN,
70 GBB_LOOP_SING_EXIT_HEADER,
71 GBB_LOOP_MULT_EXIT_HEADER,
72 GBB_LOOP_EXIT,
73 GBB_COND_HEADER,
74 GBB_SIMPLE,
75 GBB_LAST
76 } gbb_type;
78 /* Detect the type of BB. Loop headers are only marked, if they are
79 new. This means their loop_father is different to LAST_LOOP.
80 Otherwise they are treated like any other bb and their type can be
81 any other type. */
83 static gbb_type
84 get_bb_type (basic_block bb, struct loop *last_loop)
86 vec<basic_block> dom;
87 int nb_dom;
88 struct loop *loop = bb->loop_father;
90 /* Check, if we entry into a new loop. */
91 if (loop != last_loop)
93 if (single_exit (loop) != NULL)
94 return GBB_LOOP_SING_EXIT_HEADER;
95 else if (loop->num != 0)
96 return GBB_LOOP_MULT_EXIT_HEADER;
97 else
98 return GBB_COND_HEADER;
101 dom = get_dominated_by (CDI_DOMINATORS, bb);
102 nb_dom = dom.length ();
103 dom.release ();
105 if (nb_dom == 0)
106 return GBB_LAST;
108 if (nb_dom == 1 && single_succ_p (bb))
109 return GBB_SIMPLE;
111 return GBB_COND_HEADER;
114 /* A SCoP detection region, defined using bbs as borders.
116 All control flow touching this region, comes in passing basic_block
117 ENTRY and leaves passing basic_block EXIT. By using bbs instead of
118 edges for the borders we are able to represent also regions that do
119 not have a single entry or exit edge.
121 But as they have a single entry basic_block and a single exit
122 basic_block, we are able to generate for every sd_region a single
123 entry and exit edge.
127 3 <- entry
130 / \ This region contains: {3, 4, 5, 6, 7, 8}
135 9 <- exit */
138 typedef struct sd_region_p
140 /* The entry bb dominates all bbs in the sd_region. It is part of
141 the region. */
142 basic_block entry;
144 /* The exit bb postdominates all bbs in the sd_region, but is not
145 part of the region. */
146 basic_block exit;
147 } sd_region;
151 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
153 static void
154 move_sd_regions (vec<sd_region> *source, vec<sd_region> *target)
156 sd_region *s;
157 int i;
159 FOR_EACH_VEC_ELT (*source, i, s)
160 target->safe_push (*s);
162 source->release ();
165 /* Something like "n * m" is not allowed. */
167 static bool
168 graphite_can_represent_init (tree e)
170 switch (TREE_CODE (e))
172 case POLYNOMIAL_CHREC:
173 return graphite_can_represent_init (CHREC_LEFT (e))
174 && graphite_can_represent_init (CHREC_RIGHT (e));
176 case MULT_EXPR:
177 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
178 return graphite_can_represent_init (TREE_OPERAND (e, 0))
179 && tree_fits_shwi_p (TREE_OPERAND (e, 1));
180 else
181 return graphite_can_represent_init (TREE_OPERAND (e, 1))
182 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
184 case PLUS_EXPR:
185 case POINTER_PLUS_EXPR:
186 case MINUS_EXPR:
187 return graphite_can_represent_init (TREE_OPERAND (e, 0))
188 && graphite_can_represent_init (TREE_OPERAND (e, 1));
190 case NEGATE_EXPR:
191 case BIT_NOT_EXPR:
192 CASE_CONVERT:
193 case NON_LVALUE_EXPR:
194 return graphite_can_represent_init (TREE_OPERAND (e, 0));
196 default:
197 break;
200 return true;
203 /* Return true when SCEV can be represented in the polyhedral model.
205 An expression can be represented, if it can be expressed as an
206 affine expression. For loops (i, j) and parameters (m, n) all
207 affine expressions are of the form:
209 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
211 1 i + 20 j + (-2) m + 25
213 Something like "i * n" or "n * m" is not allowed. */
215 static bool
216 graphite_can_represent_scev (tree scev)
218 if (chrec_contains_undetermined (scev))
219 return false;
221 /* We disable the handling of pointer types, because it’s currently not
222 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
223 the only nodes, which are disabled in case they are pointers to object
224 types, but this can be changed. */
226 if (POINTER_TYPE_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
227 return false;
229 switch (TREE_CODE (scev))
231 case NEGATE_EXPR:
232 case BIT_NOT_EXPR:
233 CASE_CONVERT:
234 case NON_LVALUE_EXPR:
235 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
237 case PLUS_EXPR:
238 case POINTER_PLUS_EXPR:
239 case MINUS_EXPR:
240 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
241 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
243 case MULT_EXPR:
244 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
245 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
246 && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
247 && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
248 && graphite_can_represent_init (scev)
249 && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
250 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
252 case POLYNOMIAL_CHREC:
253 /* Check for constant strides. With a non constant stride of
254 'n' we would have a value of 'iv * n'. Also check that the
255 initial value can represented: for example 'n * m' cannot be
256 represented. */
257 if (!evolution_function_right_is_integer_cst (scev)
258 || !graphite_can_represent_init (scev))
259 return false;
260 return graphite_can_represent_scev (CHREC_LEFT (scev));
262 default:
263 break;
266 /* Only affine functions can be represented. */
267 if (tree_contains_chrecs (scev, NULL)
268 || !scev_is_linear_expression (scev))
269 return false;
271 return true;
275 /* Return true when EXPR can be represented in the polyhedral model.
277 This means an expression can be represented, if it is linear with
278 respect to the loops and the strides are non parametric.
279 LOOP is the place where the expr will be evaluated. SCOP_ENTRY defines the
280 entry of the region we analyse. */
282 static bool
283 graphite_can_represent_expr (basic_block scop_entry, loop_p loop,
284 tree expr)
286 tree scev = analyze_scalar_evolution (loop, expr);
288 scev = instantiate_scev (scop_entry, loop, scev);
290 return graphite_can_represent_scev (scev);
293 /* Return true if the data references of STMT can be represented by
294 Graphite. */
296 static bool
297 stmt_has_simple_data_refs_p (loop_p outermost_loop ATTRIBUTE_UNUSED,
298 gimple stmt)
300 data_reference_p dr;
301 unsigned i;
302 int j;
303 bool res = true;
304 vec<data_reference_p> drs = vNULL;
305 loop_p outer;
307 for (outer = loop_containing_stmt (stmt); outer; outer = loop_outer (outer))
309 graphite_find_data_references_in_stmt (outer,
310 loop_containing_stmt (stmt),
311 stmt, &drs);
313 FOR_EACH_VEC_ELT (drs, j, dr)
314 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
315 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i)))
317 res = false;
318 goto done;
321 free_data_refs (drs);
322 drs.create (0);
325 done:
326 free_data_refs (drs);
327 return res;
330 /* Return true only when STMT is simple enough for being handled by
331 Graphite. This depends on SCOP_ENTRY, as the parameters are
332 initialized relatively to this basic block, the linear functions
333 are initialized to OUTERMOST_LOOP and BB is the place where we try
334 to evaluate the STMT. */
336 static bool
337 stmt_simple_for_scop_p (basic_block scop_entry, loop_p outermost_loop,
338 gimple stmt, basic_block bb)
340 loop_p loop = bb->loop_father;
342 gcc_assert (scop_entry);
344 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
345 Calls have side-effects, except those to const or pure
346 functions. */
347 if (gimple_has_volatile_ops (stmt)
348 || (gimple_code (stmt) == GIMPLE_CALL
349 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
350 || (gimple_code (stmt) == GIMPLE_ASM))
351 return false;
353 if (is_gimple_debug (stmt))
354 return true;
356 if (!stmt_has_simple_data_refs_p (outermost_loop, stmt))
357 return false;
359 switch (gimple_code (stmt))
361 case GIMPLE_RETURN:
362 case GIMPLE_LABEL:
363 return true;
365 case GIMPLE_COND:
367 /* We can handle all binary comparisons. Inequalities are
368 also supported as they can be represented with union of
369 polyhedra. */
370 enum tree_code code = gimple_cond_code (stmt);
371 if (!(code == LT_EXPR
372 || code == GT_EXPR
373 || code == LE_EXPR
374 || code == GE_EXPR
375 || code == EQ_EXPR
376 || code == NE_EXPR))
377 return false;
379 for (unsigned i = 0; i < 2; ++i)
381 tree op = gimple_op (stmt, i);
382 if (!graphite_can_represent_expr (scop_entry, loop, op)
383 /* We can not handle REAL_TYPE. Failed for pr39260. */
384 || TREE_CODE (TREE_TYPE (op)) == REAL_TYPE)
385 return false;
388 return true;
391 case GIMPLE_ASSIGN:
392 case GIMPLE_CALL:
393 return true;
395 default:
396 /* These nodes cut a new scope. */
397 return false;
400 return false;
403 /* Returns the statement of BB that contains a harmful operation: that
404 can be a function call with side effects, the induction variables
405 are not linear with respect to SCOP_ENTRY, etc. The current open
406 scop should end before this statement. The evaluation is limited using
407 OUTERMOST_LOOP as outermost loop that may change. */
409 static gimple
410 harmful_stmt_in_bb (basic_block scop_entry, loop_p outer_loop, basic_block bb)
412 gimple_stmt_iterator gsi;
414 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
415 if (!stmt_simple_for_scop_p (scop_entry, outer_loop, gsi_stmt (gsi), bb))
416 return gsi_stmt (gsi);
418 return NULL;
421 /* Return true if LOOP can be represented in the polyhedral
422 representation. This is evaluated taking SCOP_ENTRY and
423 OUTERMOST_LOOP in mind. */
425 static bool
426 graphite_can_represent_loop (basic_block scop_entry, loop_p loop)
428 tree niter;
429 struct tree_niter_desc niter_desc;
431 /* FIXME: For the moment, graphite cannot be used on loops that
432 iterate using induction variables that wrap. */
434 return number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
435 && niter_desc.control.no_overflow
436 && (niter = number_of_latch_executions (loop))
437 && !chrec_contains_undetermined (niter)
438 && graphite_can_represent_expr (scop_entry, loop, niter);
441 /* Store information needed by scopdet_* functions. */
443 struct scopdet_info
445 /* Exit of the open scop would stop if the current BB is harmful. */
446 basic_block exit;
448 /* Where the next scop would start if the current BB is harmful. */
449 basic_block next;
451 /* The bb or one of its children contains open loop exits. That means
452 loop exit nodes that are not surrounded by a loop dominated by bb. */
453 bool exits;
455 /* The bb or one of its children contains only structures we can handle. */
456 bool difficult;
459 static struct scopdet_info build_scops_1 (basic_block, loop_p,
460 vec<sd_region> *, loop_p);
462 /* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
463 to SCOPS. TYPE is the gbb_type of BB. */
465 static struct scopdet_info
466 scopdet_basic_block_info (basic_block bb, loop_p outermost_loop,
467 vec<sd_region> *scops, gbb_type type)
469 loop_p loop = bb->loop_father;
470 struct scopdet_info result;
471 gimple stmt;
473 /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps. */
474 basic_block entry_block = ENTRY_BLOCK_PTR_FOR_FN (cfun);
475 stmt = harmful_stmt_in_bb (entry_block, outermost_loop, bb);
476 result.difficult = (stmt != NULL);
477 result.exit = NULL;
479 switch (type)
481 case GBB_LAST:
482 result.next = NULL;
483 result.exits = false;
485 /* Mark bbs terminating a SESE region difficult, if they start
486 a condition or if the block it exits to cannot be split
487 with make_forwarder_block. */
488 if (!single_succ_p (bb)
489 || bb_has_abnormal_pred (single_succ (bb)))
490 result.difficult = true;
491 else
492 result.exit = single_succ (bb);
494 break;
496 case GBB_SIMPLE:
497 result.next = single_succ (bb);
498 result.exits = false;
499 result.exit = single_succ (bb);
500 break;
502 case GBB_LOOP_SING_EXIT_HEADER:
504 auto_vec<sd_region, 3> regions;
505 struct scopdet_info sinfo;
506 edge exit_e = single_exit (loop);
508 sinfo = build_scops_1 (bb, outermost_loop, &regions, loop);
510 if (!graphite_can_represent_loop (entry_block, loop))
511 result.difficult = true;
513 result.difficult |= sinfo.difficult;
515 /* Try again with another loop level. */
516 if (result.difficult
517 && loop_depth (outermost_loop) + 1 == loop_depth (loop))
519 outermost_loop = loop;
521 regions.release ();
522 regions.create (3);
524 sinfo = scopdet_basic_block_info (bb, outermost_loop, scops, type);
526 result = sinfo;
527 result.difficult = true;
529 if (sinfo.difficult)
530 move_sd_regions (&regions, scops);
531 else
533 sd_region open_scop;
534 open_scop.entry = bb;
535 open_scop.exit = exit_e->dest;
536 scops->safe_push (open_scop);
537 regions.release ();
540 else
542 result.exit = exit_e->dest;
543 result.next = exit_e->dest;
545 /* If we do not dominate result.next, remove it. It's either
546 the exit block, or another bb dominates it and will
547 call the scop detection for this bb. */
548 if (!dominated_by_p (CDI_DOMINATORS, result.next, bb))
549 result.next = NULL;
551 if (exit_e->src->loop_father != loop)
552 result.next = NULL;
554 result.exits = false;
556 if (result.difficult)
557 move_sd_regions (&regions, scops);
558 else
559 regions.release ();
562 break;
565 case GBB_LOOP_MULT_EXIT_HEADER:
567 /* XXX: For now we just do not join loops with multiple exits. If the
568 exits lead to the same bb it may be possible to join the loop. */
569 auto_vec<sd_region, 3> regions;
570 vec<edge> exits = get_loop_exit_edges (loop);
571 edge e;
572 int i;
573 build_scops_1 (bb, loop, &regions, loop);
575 /* Scan the code dominated by this loop. This means all bbs, that are
576 are dominated by a bb in this loop, but are not part of this loop.
578 The easiest case:
579 - The loop exit destination is dominated by the exit sources.
581 TODO: We miss here the more complex cases:
582 - The exit destinations are dominated by another bb inside
583 the loop.
584 - The loop dominates bbs, that are not exit destinations. */
585 FOR_EACH_VEC_ELT (exits, i, e)
586 if (e->src->loop_father == loop
587 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src))
589 if (loop_outer (outermost_loop))
590 outermost_loop = loop_outer (outermost_loop);
592 /* Pass loop_outer to recognize e->dest as loop header in
593 build_scops_1. */
594 if (e->dest->loop_father->header == e->dest)
595 build_scops_1 (e->dest, outermost_loop, &regions,
596 loop_outer (e->dest->loop_father));
597 else
598 build_scops_1 (e->dest, outermost_loop, &regions,
599 e->dest->loop_father);
602 result.next = NULL;
603 result.exit = NULL;
604 result.difficult = true;
605 result.exits = false;
606 move_sd_regions (&regions, scops);
607 exits.release ();
608 break;
610 case GBB_COND_HEADER:
612 auto_vec<sd_region, 3> regions;
613 struct scopdet_info sinfo;
614 vec<basic_block> dominated;
615 int i;
616 basic_block dom_bb;
617 basic_block last_exit = NULL;
618 edge e;
619 result.exits = false;
621 /* First check the successors of BB, and check if it is
622 possible to join the different branches. */
623 FOR_EACH_VEC_SAFE_ELT (bb->succs, i, e)
625 /* Ignore loop exits. They will be handled after the loop
626 body. */
627 if (loop_exits_to_bb_p (loop, e->dest))
629 result.exits = true;
630 continue;
633 /* Do not follow edges that lead to the end of the
634 conditions block. For example, in
637 | /|\
638 | 1 2 |
639 | | | |
640 | 3 4 |
641 | \|/
644 the edge from 0 => 6. Only check if all paths lead to
645 the same node 6. */
647 if (!single_pred_p (e->dest))
649 /* Check, if edge leads directly to the end of this
650 condition. */
651 if (!last_exit)
652 last_exit = e->dest;
654 if (e->dest != last_exit)
655 result.difficult = true;
657 continue;
660 if (!dominated_by_p (CDI_DOMINATORS, e->dest, bb))
662 result.difficult = true;
663 continue;
666 sinfo = build_scops_1 (e->dest, outermost_loop, &regions, loop);
668 result.exits |= sinfo.exits;
669 result.difficult |= sinfo.difficult;
671 /* Checks, if all branches end at the same point.
672 If that is true, the condition stays joinable.
673 Have a look at the example above. */
674 if (sinfo.exit)
676 if (!last_exit)
677 last_exit = sinfo.exit;
679 if (sinfo.exit != last_exit)
680 result.difficult = true;
682 else
683 result.difficult = true;
686 if (!last_exit)
687 result.difficult = true;
689 /* Join the branches of the condition if possible. */
690 if (!result.exits && !result.difficult)
692 /* Only return a next pointer if we dominate this pointer.
693 Otherwise it will be handled by the bb dominating it. */
694 if (dominated_by_p (CDI_DOMINATORS, last_exit, bb)
695 && last_exit != bb)
696 result.next = last_exit;
697 else
698 result.next = NULL;
700 result.exit = last_exit;
702 regions.release ();
703 break;
706 /* Scan remaining bbs dominated by BB. */
707 dominated = get_dominated_by (CDI_DOMINATORS, bb);
709 FOR_EACH_VEC_ELT (dominated, i, dom_bb)
711 /* Ignore loop exits: they will be handled after the loop body. */
712 if (loop_depth (find_common_loop (loop, dom_bb->loop_father))
713 < loop_depth (loop))
715 result.exits = true;
716 continue;
719 /* Ignore the bbs processed above. */
720 if (single_pred_p (dom_bb) && single_pred (dom_bb) == bb)
721 continue;
723 if (loop_depth (loop) > loop_depth (dom_bb->loop_father))
724 sinfo = build_scops_1 (dom_bb, outermost_loop, &regions,
725 loop_outer (loop));
726 else
727 sinfo = build_scops_1 (dom_bb, outermost_loop, &regions, loop);
729 result.exits |= sinfo.exits;
730 result.difficult = true;
731 result.exit = NULL;
734 dominated.release ();
736 result.next = NULL;
737 move_sd_regions (&regions, scops);
739 break;
742 default:
743 gcc_unreachable ();
746 return result;
749 /* Starting from CURRENT we walk the dominance tree and add new sd_regions to
750 SCOPS. The analyse if a sd_region can be handled is based on the value
751 of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change. LOOP
752 is the loop in which CURRENT is handled.
754 TODO: These functions got a little bit big. They definitely should be cleaned
755 up. */
757 static struct scopdet_info
758 build_scops_1 (basic_block current, loop_p outermost_loop,
759 vec<sd_region> *scops, loop_p loop)
761 bool in_scop = false;
762 sd_region open_scop;
763 struct scopdet_info sinfo;
765 /* Initialize result. */
766 struct scopdet_info result;
767 result.exits = false;
768 result.difficult = false;
769 result.next = NULL;
770 result.exit = NULL;
771 open_scop.entry = NULL;
772 open_scop.exit = NULL;
773 sinfo.exit = NULL;
775 /* Loop over the dominance tree. If we meet a difficult bb, close
776 the current SCoP. Loop and condition header start a new layer,
777 and can only be added if all bbs in deeper layers are simple. */
778 while (current != NULL)
780 sinfo = scopdet_basic_block_info (current, outermost_loop, scops,
781 get_bb_type (current, loop));
783 if (!in_scop && !(sinfo.exits || sinfo.difficult))
785 open_scop.entry = current;
786 open_scop.exit = NULL;
787 in_scop = true;
789 else if (in_scop && (sinfo.exits || sinfo.difficult))
791 open_scop.exit = current;
792 scops->safe_push (open_scop);
793 in_scop = false;
796 result.difficult |= sinfo.difficult;
797 result.exits |= sinfo.exits;
799 current = sinfo.next;
802 /* Try to close open_scop, if we are still in an open SCoP. */
803 if (in_scop)
805 open_scop.exit = sinfo.exit;
806 gcc_assert (open_scop.exit);
807 scops->safe_push (open_scop);
810 result.exit = sinfo.exit;
811 return result;
814 /* Checks if a bb is contained in REGION. */
816 static bool
817 bb_in_sd_region (basic_block bb, sd_region *region)
819 return bb_in_region (bb, region->entry, region->exit);
822 /* Returns the single entry edge of REGION, if it does not exits NULL. */
824 static edge
825 find_single_entry_edge (sd_region *region)
827 edge e;
828 edge_iterator ei;
829 edge entry = NULL;
831 FOR_EACH_EDGE (e, ei, region->entry->preds)
832 if (!bb_in_sd_region (e->src, region))
834 if (entry)
836 entry = NULL;
837 break;
840 else
841 entry = e;
844 return entry;
847 /* Returns the single exit edge of REGION, if it does not exits NULL. */
849 static edge
850 find_single_exit_edge (sd_region *region)
852 edge e;
853 edge_iterator ei;
854 edge exit = NULL;
856 FOR_EACH_EDGE (e, ei, region->exit->preds)
857 if (bb_in_sd_region (e->src, region))
859 if (exit)
861 exit = NULL;
862 break;
865 else
866 exit = e;
869 return exit;
872 /* Create a single entry edge for REGION. */
874 static void
875 create_single_entry_edge (sd_region *region)
877 if (find_single_entry_edge (region))
878 return;
880 /* There are multiple predecessors for bb_3
882 | 1 2
883 | | /
884 | |/
885 | 3 <- entry
886 | |\
887 | | |
888 | 4 ^
889 | | |
890 | |/
893 There are two edges (1->3, 2->3), that point from outside into the region,
894 and another one (5->3), a loop latch, lead to bb_3.
896 We split bb_3.
898 | 1 2
899 | | /
900 | |/
901 |3.0
902 | |\ (3.0 -> 3.1) = single entry edge
903 |3.1 | <- entry
904 | | |
905 | | |
906 | 4 ^
907 | | |
908 | |/
911 If the loop is part of the SCoP, we have to redirect the loop latches.
913 | 1 2
914 | | /
915 | |/
916 |3.0
917 | | (3.0 -> 3.1) = entry edge
918 |3.1 <- entry
919 | |\
920 | | |
921 | 4 ^
922 | | |
923 | |/
924 | 5 */
926 if (region->entry->loop_father->header != region->entry
927 || dominated_by_p (CDI_DOMINATORS,
928 loop_latch_edge (region->entry->loop_father)->src,
929 region->exit))
931 edge forwarder = split_block_after_labels (region->entry);
932 region->entry = forwarder->dest;
934 else
935 /* This case is never executed, as the loop headers seem always to have a
936 single edge pointing from outside into the loop. */
937 gcc_unreachable ();
939 gcc_checking_assert (find_single_entry_edge (region));
942 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
944 static bool
945 sd_region_without_exit (edge e)
947 sd_region *r = (sd_region *) e->aux;
949 if (r)
950 return r->exit == NULL;
951 else
952 return false;
955 /* Create a single exit edge for REGION. */
957 static void
958 create_single_exit_edge (sd_region *region)
960 edge e;
961 edge_iterator ei;
962 edge forwarder = NULL;
963 basic_block exit;
965 /* We create a forwarder bb (5) for all edges leaving this region
966 (3->5, 4->5). All other edges leading to the same bb, are moved
967 to a new bb (6). If these edges where part of another region (2->5)
968 we update the region->exit pointer, of this region.
970 To identify which edge belongs to which region we depend on the e->aux
971 pointer in every edge. It points to the region of the edge or to NULL,
972 if the edge is not part of any region.
974 1 2 3 4 1->5 no region, 2->5 region->exit = 5,
975 \| |/ 3->5 region->exit = NULL, 4->5 region->exit = NULL
976 5 <- exit
978 changes to
980 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
981 | | \/ 3->5 no region, 4->5 no region,
982 | | 5
983 \| / 5->6 region->exit = 6
986 Now there is only a single exit edge (5->6). */
987 exit = region->exit;
988 region->exit = NULL;
989 forwarder = make_forwarder_block (exit, &sd_region_without_exit, NULL);
991 /* Unmark the edges, that are no longer exit edges. */
992 FOR_EACH_EDGE (e, ei, forwarder->src->preds)
993 if (e->aux)
994 e->aux = NULL;
996 /* Mark the new exit edge. */
997 single_succ_edge (forwarder->src)->aux = region;
999 /* Update the exit bb of all regions, where exit edges lead to
1000 forwarder->dest. */
1001 FOR_EACH_EDGE (e, ei, forwarder->dest->preds)
1002 if (e->aux)
1003 ((sd_region *) e->aux)->exit = forwarder->dest;
1005 gcc_checking_assert (find_single_exit_edge (region));
1008 /* Unmark the exit edges of all REGIONS.
1009 See comment in "create_single_exit_edge". */
1011 static void
1012 unmark_exit_edges (vec<sd_region> regions)
1014 int i;
1015 sd_region *s;
1016 edge e;
1017 edge_iterator ei;
1019 FOR_EACH_VEC_ELT (regions, i, s)
1020 FOR_EACH_EDGE (e, ei, s->exit->preds)
1021 e->aux = NULL;
1025 /* Mark the exit edges of all REGIONS.
1026 See comment in "create_single_exit_edge". */
1028 static void
1029 mark_exit_edges (vec<sd_region> regions)
1031 int i;
1032 sd_region *s;
1033 edge e;
1034 edge_iterator ei;
1036 FOR_EACH_VEC_ELT (regions, i, s)
1037 FOR_EACH_EDGE (e, ei, s->exit->preds)
1038 if (bb_in_sd_region (e->src, s))
1039 e->aux = s;
1042 /* Create for all scop regions a single entry and a single exit edge. */
1044 static void
1045 create_sese_edges (vec<sd_region> regions)
1047 int i;
1048 sd_region *s;
1050 FOR_EACH_VEC_ELT (regions, i, s)
1051 create_single_entry_edge (s);
1053 mark_exit_edges (regions);
1055 FOR_EACH_VEC_ELT (regions, i, s)
1056 /* Don't handle multiple edges exiting the function. */
1057 if (!find_single_exit_edge (s)
1058 && s->exit != EXIT_BLOCK_PTR_FOR_FN (cfun))
1059 create_single_exit_edge (s);
1061 unmark_exit_edges (regions);
1063 calculate_dominance_info (CDI_DOMINATORS);
1064 fix_loop_structure (NULL);
1066 #ifdef ENABLE_CHECKING
1067 verify_loop_structure ();
1068 verify_ssa (false, true);
1069 #endif
1072 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1074 static void
1075 build_graphite_scops (vec<sd_region> regions,
1076 vec<scop_p> *scops)
1078 int i;
1079 sd_region *s;
1081 FOR_EACH_VEC_ELT (regions, i, s)
1083 edge entry = find_single_entry_edge (s);
1084 edge exit = find_single_exit_edge (s);
1085 scop_p scop;
1087 if (!exit)
1088 continue;
1090 scop = new_scop (new_sese (entry, exit));
1091 scops->safe_push (scop);
1093 /* Are there overlapping SCoPs? */
1094 #ifdef ENABLE_CHECKING
1096 int j;
1097 sd_region *s2;
1099 FOR_EACH_VEC_ELT (regions, j, s2)
1100 if (s != s2)
1101 gcc_assert (!bb_in_sd_region (s->entry, s2));
1103 #endif
1107 /* Returns true when BB contains only close phi nodes. */
1109 static bool
1110 contains_only_close_phi_nodes (basic_block bb)
1112 gimple_stmt_iterator gsi;
1114 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1115 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
1116 return false;
1118 return true;
1121 /* Print statistics for SCOP to FILE. */
1123 static void
1124 print_graphite_scop_statistics (FILE* file, scop_p scop)
1126 long n_bbs = 0;
1127 long n_loops = 0;
1128 long n_stmts = 0;
1129 long n_conditions = 0;
1130 long n_p_bbs = 0;
1131 long n_p_loops = 0;
1132 long n_p_stmts = 0;
1133 long n_p_conditions = 0;
1135 basic_block bb;
1137 FOR_ALL_BB_FN (bb, cfun)
1139 gimple_stmt_iterator psi;
1140 loop_p loop = bb->loop_father;
1142 if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
1143 continue;
1145 n_bbs++;
1146 n_p_bbs += bb->count;
1148 if (EDGE_COUNT (bb->succs) > 1)
1150 n_conditions++;
1151 n_p_conditions += bb->count;
1154 for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
1156 n_stmts++;
1157 n_p_stmts += bb->count;
1160 if (loop->header == bb && loop_in_sese_p (loop, SCOP_REGION (scop)))
1162 n_loops++;
1163 n_p_loops += bb->count;
1168 fprintf (file, "\nBefore limit_scops SCoP statistics (");
1169 fprintf (file, "BBS:%ld, ", n_bbs);
1170 fprintf (file, "LOOPS:%ld, ", n_loops);
1171 fprintf (file, "CONDITIONS:%ld, ", n_conditions);
1172 fprintf (file, "STMTS:%ld)\n", n_stmts);
1173 fprintf (file, "\nBefore limit_scops SCoP profiling statistics (");
1174 fprintf (file, "BBS:%ld, ", n_p_bbs);
1175 fprintf (file, "LOOPS:%ld, ", n_p_loops);
1176 fprintf (file, "CONDITIONS:%ld, ", n_p_conditions);
1177 fprintf (file, "STMTS:%ld)\n", n_p_stmts);
1180 /* Print statistics for SCOPS to FILE. */
1182 static void
1183 print_graphite_statistics (FILE* file, vec<scop_p> scops)
1185 int i;
1186 scop_p scop;
1188 FOR_EACH_VEC_ELT (scops, i, scop)
1189 print_graphite_scop_statistics (file, scop);
1192 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
1194 Example:
1196 for (i |
1198 for (j | SCoP 1
1199 for (k |
1202 * SCoP frontier, as this line is not surrounded by any loop. *
1204 for (l | SCoP 2
1206 This is necessary as scalar evolution and parameter detection need a
1207 outermost loop to initialize parameters correctly.
1209 TODO: FIX scalar evolution and parameter detection to allow more flexible
1210 SCoP frontiers. */
1212 static void
1213 limit_scops (vec<scop_p> *scops)
1215 auto_vec<sd_region, 3> regions;
1217 int i;
1218 scop_p scop;
1220 FOR_EACH_VEC_ELT (*scops, i, scop)
1222 int j;
1223 loop_p loop;
1224 sese region = SCOP_REGION (scop);
1225 build_sese_loop_nests (region);
1227 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), j, loop)
1228 if (!loop_in_sese_p (loop_outer (loop), region)
1229 && single_exit (loop))
1231 sd_region open_scop;
1232 open_scop.entry = loop->header;
1233 open_scop.exit = single_exit (loop)->dest;
1235 /* This is a hack on top of the limit_scops hack. The
1236 limit_scops hack should disappear all together. */
1237 if (single_succ_p (open_scop.exit)
1238 && contains_only_close_phi_nodes (open_scop.exit))
1239 open_scop.exit = single_succ_edge (open_scop.exit)->dest;
1241 regions.safe_push (open_scop);
1245 free_scops (*scops);
1246 scops->create (3);
1248 create_sese_edges (regions);
1249 build_graphite_scops (regions, scops);
1252 /* Returns true when P1 and P2 are close phis with the same
1253 argument. */
1255 static inline bool
1256 same_close_phi_node (gphi *p1, gphi *p2)
1258 return operand_equal_p (gimple_phi_arg_def (p1, 0),
1259 gimple_phi_arg_def (p2, 0), 0);
1262 /* Remove the close phi node at GSI and replace its rhs with the rhs
1263 of PHI. */
1265 static void
1266 remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
1268 gimple use_stmt;
1269 use_operand_p use_p;
1270 imm_use_iterator imm_iter;
1271 tree res = gimple_phi_result (phi);
1272 tree def = gimple_phi_result (gsi->phi ());
1274 gcc_assert (same_close_phi_node (phi, gsi->phi ()));
1276 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
1278 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1279 SET_USE (use_p, res);
1281 update_stmt (use_stmt);
1283 /* It is possible that we just created a duplicate close-phi
1284 for an already-processed containing loop. Check for this
1285 case and clean it up. */
1286 if (gimple_code (use_stmt) == GIMPLE_PHI
1287 && gimple_phi_num_args (use_stmt) == 1)
1288 make_close_phi_nodes_unique (gimple_bb (use_stmt));
1291 remove_phi_node (gsi, true);
1294 /* Removes all the close phi duplicates from BB. */
1296 static void
1297 make_close_phi_nodes_unique (basic_block bb)
1299 gphi_iterator psi;
1301 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1303 gphi_iterator gsi = psi;
1304 gphi *phi = psi.phi ();
1306 /* At this point, PHI should be a close phi in normal form. */
1307 gcc_assert (gimple_phi_num_args (phi) == 1);
1309 /* Iterate over the next phis and remove duplicates. */
1310 gsi_next (&gsi);
1311 while (!gsi_end_p (gsi))
1312 if (same_close_phi_node (phi, gsi.phi ()))
1313 remove_duplicate_close_phi (phi, &gsi);
1314 else
1315 gsi_next (&gsi);
1319 /* Transforms LOOP to the canonical loop closed SSA form. */
1321 static void
1322 canonicalize_loop_closed_ssa (loop_p loop)
1324 edge e = single_exit (loop);
1325 basic_block bb;
1327 if (!e || e->flags & EDGE_ABNORMAL)
1328 return;
1330 bb = e->dest;
1332 if (single_pred_p (bb))
1334 e = split_block_after_labels (bb);
1335 make_close_phi_nodes_unique (e->src);
1337 else
1339 gphi_iterator psi;
1340 basic_block close = split_edge (e);
1342 e = single_succ_edge (close);
1344 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1346 gphi *phi = psi.phi ();
1347 unsigned i;
1349 for (i = 0; i < gimple_phi_num_args (phi); i++)
1350 if (gimple_phi_arg_edge (phi, i) == e)
1352 tree res, arg = gimple_phi_arg_def (phi, i);
1353 use_operand_p use_p;
1354 gphi *close_phi;
1356 if (TREE_CODE (arg) != SSA_NAME)
1357 continue;
1359 close_phi = create_phi_node (NULL_TREE, close);
1360 res = create_new_def_for (arg, close_phi,
1361 gimple_phi_result_ptr (close_phi));
1362 add_phi_arg (close_phi, arg,
1363 gimple_phi_arg_edge (close_phi, 0),
1364 UNKNOWN_LOCATION);
1365 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
1366 replace_exp (use_p, res);
1367 update_stmt (phi);
1371 make_close_phi_nodes_unique (close);
1374 /* The code above does not properly handle changes in the post dominance
1375 information (yet). */
1376 free_dominance_info (CDI_POST_DOMINATORS);
1379 /* Converts the current loop closed SSA form to a canonical form
1380 expected by the Graphite code generation.
1382 The loop closed SSA form has the following invariant: a variable
1383 defined in a loop that is used outside the loop appears only in the
1384 phi nodes in the destination of the loop exit. These phi nodes are
1385 called close phi nodes.
1387 The canonical loop closed SSA form contains the extra invariants:
1389 - when the loop contains only one exit, the close phi nodes contain
1390 only one argument. That implies that the basic block that contains
1391 the close phi nodes has only one predecessor, that is a basic block
1392 in the loop.
1394 - the basic block containing the close phi nodes does not contain
1395 other statements.
1397 - there exist only one phi node per definition in the loop.
1400 static void
1401 canonicalize_loop_closed_ssa_form (void)
1403 loop_p loop;
1405 #ifdef ENABLE_CHECKING
1406 verify_loop_closed_ssa (true);
1407 #endif
1409 FOR_EACH_LOOP (loop, 0)
1410 canonicalize_loop_closed_ssa (loop);
1412 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1413 update_ssa (TODO_update_ssa);
1415 #ifdef ENABLE_CHECKING
1416 verify_loop_closed_ssa (true);
1417 #endif
1420 /* Find Static Control Parts (SCoP) in the current function and pushes
1421 them to SCOPS. */
1423 void
1424 build_scops (vec<scop_p> *scops)
1426 struct loop *loop = current_loops->tree_root;
1427 auto_vec<sd_region, 3> regions;
1429 canonicalize_loop_closed_ssa_form ();
1430 build_scops_1 (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1431 ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father,
1432 &regions, loop);
1433 create_sese_edges (regions);
1434 build_graphite_scops (regions, scops);
1436 if (dump_file && (dump_flags & TDF_DETAILS))
1437 print_graphite_statistics (dump_file, *scops);
1439 limit_scops (scops);
1440 regions.release ();
1442 if (dump_file && (dump_flags & TDF_DETAILS))
1443 fprintf (dump_file, "\nnumber of SCoPs: %d\n",
1444 scops ? scops->length () : 0);
1447 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1448 different colors. If there are not enough colors, paint the
1449 remaining SCoPs in gray.
1451 Special nodes:
1452 - "*" after the node number denotes the entry of a SCoP,
1453 - "#" after the node number denotes the exit of a SCoP,
1454 - "()" around the node number denotes the entry or the
1455 exit nodes of the SCOP. These are not part of SCoP. */
1457 static void
1458 dot_all_scops_1 (FILE *file, vec<scop_p> scops)
1460 basic_block bb;
1461 edge e;
1462 edge_iterator ei;
1463 scop_p scop;
1464 const char* color;
1465 int i;
1467 /* Disable debugging while printing graph. */
1468 int tmp_dump_flags = dump_flags;
1469 dump_flags = 0;
1471 fprintf (file, "digraph all {\n");
1473 FOR_ALL_BB_FN (bb, cfun)
1475 int part_of_scop = false;
1477 /* Use HTML for every bb label. So we are able to print bbs
1478 which are part of two different SCoPs, with two different
1479 background colors. */
1480 fprintf (file, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1481 bb->index);
1482 fprintf (file, "CELLSPACING=\"0\">\n");
1484 /* Select color for SCoP. */
1485 FOR_EACH_VEC_ELT (scops, i, scop)
1487 sese region = SCOP_REGION (scop);
1488 if (bb_in_sese_p (bb, region)
1489 || (SESE_EXIT_BB (region) == bb)
1490 || (SESE_ENTRY_BB (region) == bb))
1492 switch (i % 17)
1494 case 0: /* red */
1495 color = "#e41a1c";
1496 break;
1497 case 1: /* blue */
1498 color = "#377eb8";
1499 break;
1500 case 2: /* green */
1501 color = "#4daf4a";
1502 break;
1503 case 3: /* purple */
1504 color = "#984ea3";
1505 break;
1506 case 4: /* orange */
1507 color = "#ff7f00";
1508 break;
1509 case 5: /* yellow */
1510 color = "#ffff33";
1511 break;
1512 case 6: /* brown */
1513 color = "#a65628";
1514 break;
1515 case 7: /* rose */
1516 color = "#f781bf";
1517 break;
1518 case 8:
1519 color = "#8dd3c7";
1520 break;
1521 case 9:
1522 color = "#ffffb3";
1523 break;
1524 case 10:
1525 color = "#bebada";
1526 break;
1527 case 11:
1528 color = "#fb8072";
1529 break;
1530 case 12:
1531 color = "#80b1d3";
1532 break;
1533 case 13:
1534 color = "#fdb462";
1535 break;
1536 case 14:
1537 color = "#b3de69";
1538 break;
1539 case 15:
1540 color = "#fccde5";
1541 break;
1542 case 16:
1543 color = "#bc80bd";
1544 break;
1545 default: /* gray */
1546 color = "#999999";
1549 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
1551 if (!bb_in_sese_p (bb, region))
1552 fprintf (file, " (");
1554 if (bb == SESE_ENTRY_BB (region)
1555 && bb == SESE_EXIT_BB (region))
1556 fprintf (file, " %d*# ", bb->index);
1557 else if (bb == SESE_ENTRY_BB (region))
1558 fprintf (file, " %d* ", bb->index);
1559 else if (bb == SESE_EXIT_BB (region))
1560 fprintf (file, " %d# ", bb->index);
1561 else
1562 fprintf (file, " %d ", bb->index);
1564 if (!bb_in_sese_p (bb,region))
1565 fprintf (file, ")");
1567 fprintf (file, "</TD></TR>\n");
1568 part_of_scop = true;
1572 if (!part_of_scop)
1574 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1575 fprintf (file, " %d </TD></TR>\n", bb->index);
1577 fprintf (file, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1580 FOR_ALL_BB_FN (bb, cfun)
1582 FOR_EACH_EDGE (e, ei, bb->succs)
1583 fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
1586 fputs ("}\n\n", file);
1588 /* Enable debugging again. */
1589 dump_flags = tmp_dump_flags;
1592 /* Display all SCoPs using dotty. */
1594 DEBUG_FUNCTION void
1595 dot_all_scops (vec<scop_p> scops)
1597 /* When debugging, enable the following code. This cannot be used
1598 in production compilers because it calls "system". */
1599 #if 0
1600 int x;
1601 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1602 gcc_assert (stream);
1604 dot_all_scops_1 (stream, scops);
1605 fclose (stream);
1607 x = system ("dotty /tmp/allscops.dot &");
1608 #else
1609 dot_all_scops_1 (stderr, scops);
1610 #endif
1613 /* Display all SCoPs using dotty. */
1615 DEBUG_FUNCTION void
1616 dot_scop (scop_p scop)
1618 auto_vec<scop_p, 1> scops;
1620 if (scop)
1621 scops.safe_push (scop);
1623 /* When debugging, enable the following code. This cannot be used
1624 in production compilers because it calls "system". */
1625 #if 0
1627 int x;
1628 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1629 gcc_assert (stream);
1631 dot_all_scops_1 (stream, scops);
1632 fclose (stream);
1633 x = system ("dotty /tmp/allscops.dot &");
1635 #else
1636 dot_all_scops_1 (stderr, scops);
1637 #endif
1640 #endif