compiler: Create dummy labels for blank labels.
[official-gcc.git] / gcc / graphite-scop-detection.c
blob28de4ab3a5b3bffedc5bda291c5d79270a1f3131
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>
32 #include "system.h"
33 #include "coretypes.h"
34 #include "backend.h"
35 #include "cfghooks.h"
36 #include "tree.h"
37 #include "gimple.h"
38 #include "ssa.h"
39 #include "fold-const.h"
40 #include "gimple-iterator.h"
41 #include "tree-ssa-loop-manip.h"
42 #include "tree-ssa-loop-niter.h"
43 #include "tree-ssa-loop.h"
44 #include "tree-into-ssa.h"
45 #include "tree-ssa.h"
46 #include "cfgloop.h"
47 #include "tree-data-ref.h"
48 #include "tree-scalar-evolution.h"
49 #include "tree-pass.h"
50 #include "graphite-poly.h"
51 #include "tree-ssa-propagate.h"
52 #include "graphite-scop-detection.h"
54 /* Forward declarations. */
55 static void make_close_phi_nodes_unique (basic_block);
57 /* The type of the analyzed basic block. */
59 typedef enum gbb_type {
60 GBB_UNKNOWN,
61 GBB_LOOP_SING_EXIT_HEADER,
62 GBB_LOOP_MULT_EXIT_HEADER,
63 GBB_LOOP_EXIT,
64 GBB_COND_HEADER,
65 GBB_SIMPLE,
66 GBB_LAST
67 } gbb_type;
69 /* Detect the type of BB. Loop headers are only marked, if they are
70 new. This means their loop_father is different to LAST_LOOP.
71 Otherwise they are treated like any other bb and their type can be
72 any other type. */
74 static gbb_type
75 get_bb_type (basic_block bb, struct loop *last_loop)
77 vec<basic_block> dom;
78 int nb_dom;
79 struct loop *loop = bb->loop_father;
81 /* Check, if we entry into a new loop. */
82 if (loop != last_loop)
84 if (single_exit (loop) != NULL)
85 return GBB_LOOP_SING_EXIT_HEADER;
86 else if (loop->num != 0)
87 return GBB_LOOP_MULT_EXIT_HEADER;
88 else
89 return GBB_COND_HEADER;
92 dom = get_dominated_by (CDI_DOMINATORS, bb);
93 nb_dom = dom.length ();
94 dom.release ();
96 if (nb_dom == 0)
97 return GBB_LAST;
99 if (nb_dom == 1 && single_succ_p (bb))
100 return GBB_SIMPLE;
102 return GBB_COND_HEADER;
105 /* A SCoP detection region, defined using bbs as borders.
107 All control flow touching this region, comes in passing basic_block
108 ENTRY and leaves passing basic_block EXIT. By using bbs instead of
109 edges for the borders we are able to represent also regions that do
110 not have a single entry or exit edge.
112 But as they have a single entry basic_block and a single exit
113 basic_block, we are able to generate for every sd_region a single
114 entry and exit edge.
118 3 <- entry
121 / \ This region contains: {3, 4, 5, 6, 7, 8}
126 9 <- exit */
129 typedef struct sd_region_p
131 /* The entry bb dominates all bbs in the sd_region. It is part of
132 the region. */
133 basic_block entry;
135 /* The exit bb postdominates all bbs in the sd_region, but is not
136 part of the region. */
137 basic_block exit;
138 } sd_region;
142 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
144 static void
145 move_sd_regions (vec<sd_region> *source, vec<sd_region> *target)
147 sd_region *s;
148 int i;
150 FOR_EACH_VEC_ELT (*source, i, s)
151 target->safe_push (*s);
153 source->release ();
156 /* Something like "n * m" is not allowed. */
158 static bool
159 graphite_can_represent_init (tree e)
161 switch (TREE_CODE (e))
163 case POLYNOMIAL_CHREC:
164 return graphite_can_represent_init (CHREC_LEFT (e))
165 && graphite_can_represent_init (CHREC_RIGHT (e));
167 case MULT_EXPR:
168 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
169 return graphite_can_represent_init (TREE_OPERAND (e, 0))
170 && tree_fits_shwi_p (TREE_OPERAND (e, 1));
171 else
172 return graphite_can_represent_init (TREE_OPERAND (e, 1))
173 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
175 case PLUS_EXPR:
176 case POINTER_PLUS_EXPR:
177 case MINUS_EXPR:
178 return graphite_can_represent_init (TREE_OPERAND (e, 0))
179 && graphite_can_represent_init (TREE_OPERAND (e, 1));
181 case NEGATE_EXPR:
182 case BIT_NOT_EXPR:
183 CASE_CONVERT:
184 case NON_LVALUE_EXPR:
185 return graphite_can_represent_init (TREE_OPERAND (e, 0));
187 default:
188 break;
191 return true;
194 /* Return true when SCEV can be represented in the polyhedral model.
196 An expression can be represented, if it can be expressed as an
197 affine expression. For loops (i, j) and parameters (m, n) all
198 affine expressions are of the form:
200 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
202 1 i + 20 j + (-2) m + 25
204 Something like "i * n" or "n * m" is not allowed. */
206 static bool
207 graphite_can_represent_scev (tree scev)
209 if (chrec_contains_undetermined (scev))
210 return false;
212 /* We disable the handling of pointer types, because it’s currently not
213 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
214 the only nodes, which are disabled in case they are pointers to object
215 types, but this can be changed. */
217 if (POINTER_TYPE_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
218 return false;
220 switch (TREE_CODE (scev))
222 case NEGATE_EXPR:
223 case BIT_NOT_EXPR:
224 CASE_CONVERT:
225 case NON_LVALUE_EXPR:
226 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
228 case PLUS_EXPR:
229 case POINTER_PLUS_EXPR:
230 case MINUS_EXPR:
231 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
232 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
234 case MULT_EXPR:
235 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
236 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
237 && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
238 && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
239 && graphite_can_represent_init (scev)
240 && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
241 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
243 case POLYNOMIAL_CHREC:
244 /* Check for constant strides. With a non constant stride of
245 'n' we would have a value of 'iv * n'. Also check that the
246 initial value can represented: for example 'n * m' cannot be
247 represented. */
248 if (!evolution_function_right_is_integer_cst (scev)
249 || !graphite_can_represent_init (scev))
250 return false;
251 return graphite_can_represent_scev (CHREC_LEFT (scev));
253 default:
254 break;
257 /* Only affine functions can be represented. */
258 if (tree_contains_chrecs (scev, NULL)
259 || !scev_is_linear_expression (scev))
260 return false;
262 return true;
266 /* Return true when EXPR can be represented in the polyhedral model.
268 This means an expression can be represented, if it is linear with
269 respect to the loops and the strides are non parametric.
270 LOOP is the place where the expr will be evaluated. SCOP_ENTRY defines the
271 entry of the region we analyse. */
273 static bool
274 graphite_can_represent_expr (basic_block scop_entry, loop_p loop,
275 tree expr)
277 tree scev = analyze_scalar_evolution (loop, expr);
279 scev = instantiate_scev (scop_entry, loop, scev);
281 return graphite_can_represent_scev (scev);
284 /* Return true if the data references of STMT can be represented by
285 Graphite. */
287 static bool
288 stmt_has_simple_data_refs_p (loop_p outermost_loop ATTRIBUTE_UNUSED,
289 gimple stmt)
291 data_reference_p dr;
292 int j;
293 bool res = true;
294 vec<data_reference_p> drs = vNULL;
295 loop_p outer;
297 for (outer = loop_containing_stmt (stmt); outer; outer = loop_outer (outer))
299 graphite_find_data_references_in_stmt (outer,
300 loop_containing_stmt (stmt),
301 stmt, &drs);
303 FOR_EACH_VEC_ELT (drs, j, dr)
305 int nb_subscripts = DR_NUM_DIMENSIONS (dr);
306 tree ref = DR_REF (dr);
308 for (int i = nb_subscripts - 1; i >= 0; i--)
310 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i))
311 || (TREE_CODE (ref) != ARRAY_REF
312 && TREE_CODE (ref) != MEM_REF
313 && TREE_CODE (ref) != COMPONENT_REF))
315 free_data_refs (drs);
316 return false;
319 ref = TREE_OPERAND (ref, 0);
323 free_data_refs (drs);
324 drs.create (0);
327 free_data_refs (drs);
328 return res;
331 /* Return true only when STMT is simple enough for being handled by
332 Graphite. This depends on SCOP_ENTRY, as the parameters are
333 initialized relatively to this basic block, the linear functions
334 are initialized to OUTERMOST_LOOP and BB is the place where we try
335 to evaluate the STMT. */
337 static bool
338 stmt_simple_for_scop_p (basic_block scop_entry, loop_p outermost_loop,
339 gimple stmt, basic_block bb)
341 loop_p loop = bb->loop_father;
343 gcc_assert (scop_entry);
345 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
346 Calls have side-effects, except those to const or pure
347 functions. */
348 if (gimple_has_volatile_ops (stmt)
349 || (gimple_code (stmt) == GIMPLE_CALL
350 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
351 || (gimple_code (stmt) == GIMPLE_ASM))
352 return false;
354 if (is_gimple_debug (stmt))
355 return true;
357 if (!stmt_has_simple_data_refs_p (outermost_loop, stmt))
358 return false;
360 switch (gimple_code (stmt))
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 if (open_scop.entry != open_scop.exit)
808 scops->safe_push (open_scop);
809 else
811 sinfo.difficult = true;
812 sinfo.exits = false;
813 sinfo.exit = NULL;
817 result.exit = sinfo.exit;
818 return result;
821 /* Checks if a bb is contained in REGION. */
823 static bool
824 bb_in_sd_region (basic_block bb, sd_region *region)
826 return bb_in_region (bb, region->entry, region->exit);
829 /* Returns the single entry edge of REGION, if it does not exits NULL. */
831 static edge
832 find_single_entry_edge (sd_region *region)
834 edge e;
835 edge_iterator ei;
836 edge entry = NULL;
838 FOR_EACH_EDGE (e, ei, region->entry->preds)
839 if (!bb_in_sd_region (e->src, region))
841 if (entry)
843 entry = NULL;
844 break;
847 else
848 entry = e;
851 return entry;
854 /* Returns the single exit edge of REGION, if it does not exits NULL. */
856 static edge
857 find_single_exit_edge (sd_region *region)
859 edge e;
860 edge_iterator ei;
861 edge exit = NULL;
863 FOR_EACH_EDGE (e, ei, region->exit->preds)
864 if (bb_in_sd_region (e->src, region))
866 if (exit)
868 exit = NULL;
869 break;
872 else
873 exit = e;
876 return exit;
879 /* Create a single entry edge for REGION. */
881 static void
882 create_single_entry_edge (sd_region *region)
884 if (find_single_entry_edge (region))
885 return;
887 /* There are multiple predecessors for bb_3
889 | 1 2
890 | | /
891 | |/
892 | 3 <- entry
893 | |\
894 | | |
895 | 4 ^
896 | | |
897 | |/
900 There are two edges (1->3, 2->3), that point from outside into the region,
901 and another one (5->3), a loop latch, lead to bb_3.
903 We split bb_3.
905 | 1 2
906 | | /
907 | |/
908 |3.0
909 | |\ (3.0 -> 3.1) = single entry edge
910 |3.1 | <- entry
911 | | |
912 | | |
913 | 4 ^
914 | | |
915 | |/
918 If the loop is part of the SCoP, we have to redirect the loop latches.
920 | 1 2
921 | | /
922 | |/
923 |3.0
924 | | (3.0 -> 3.1) = entry edge
925 |3.1 <- entry
926 | |\
927 | | |
928 | 4 ^
929 | | |
930 | |/
931 | 5 */
933 if (region->entry->loop_father->header != region->entry
934 || dominated_by_p (CDI_DOMINATORS,
935 loop_latch_edge (region->entry->loop_father)->src,
936 region->exit))
938 edge forwarder = split_block_after_labels (region->entry);
939 region->entry = forwarder->dest;
941 else
942 /* This case is never executed, as the loop headers seem always to have a
943 single edge pointing from outside into the loop. */
944 gcc_unreachable ();
946 gcc_checking_assert (find_single_entry_edge (region));
949 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
951 static bool
952 sd_region_without_exit (edge e)
954 sd_region *r = (sd_region *) e->aux;
956 if (r)
957 return r->exit == NULL;
958 else
959 return false;
962 /* Create a single exit edge for REGION. */
964 static void
965 create_single_exit_edge (sd_region *region)
967 edge e;
968 edge_iterator ei;
969 edge forwarder = NULL;
970 basic_block exit;
972 /* We create a forwarder bb (5) for all edges leaving this region
973 (3->5, 4->5). All other edges leading to the same bb, are moved
974 to a new bb (6). If these edges where part of another region (2->5)
975 we update the region->exit pointer, of this region.
977 To identify which edge belongs to which region we depend on the e->aux
978 pointer in every edge. It points to the region of the edge or to NULL,
979 if the edge is not part of any region.
981 1 2 3 4 1->5 no region, 2->5 region->exit = 5,
982 \| |/ 3->5 region->exit = NULL, 4->5 region->exit = NULL
983 5 <- exit
985 changes to
987 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
988 | | \/ 3->5 no region, 4->5 no region,
989 | | 5
990 \| / 5->6 region->exit = 6
993 Now there is only a single exit edge (5->6). */
994 exit = region->exit;
995 region->exit = NULL;
996 forwarder = make_forwarder_block (exit, &sd_region_without_exit, NULL);
998 /* Unmark the edges, that are no longer exit edges. */
999 FOR_EACH_EDGE (e, ei, forwarder->src->preds)
1000 if (e->aux)
1001 e->aux = NULL;
1003 /* Mark the new exit edge. */
1004 single_succ_edge (forwarder->src)->aux = region;
1006 /* Update the exit bb of all regions, where exit edges lead to
1007 forwarder->dest. */
1008 FOR_EACH_EDGE (e, ei, forwarder->dest->preds)
1009 if (e->aux)
1010 ((sd_region *) e->aux)->exit = forwarder->dest;
1012 gcc_checking_assert (find_single_exit_edge (region));
1015 /* Unmark the exit edges of all REGIONS.
1016 See comment in "create_single_exit_edge". */
1018 static void
1019 unmark_exit_edges (vec<sd_region> regions)
1021 int i;
1022 sd_region *s;
1023 edge e;
1024 edge_iterator ei;
1026 FOR_EACH_VEC_ELT (regions, i, s)
1027 FOR_EACH_EDGE (e, ei, s->exit->preds)
1028 e->aux = NULL;
1032 /* Mark the exit edges of all REGIONS.
1033 See comment in "create_single_exit_edge". */
1035 static void
1036 mark_exit_edges (vec<sd_region> regions)
1038 int i;
1039 sd_region *s;
1040 edge e;
1041 edge_iterator ei;
1043 FOR_EACH_VEC_ELT (regions, i, s)
1044 FOR_EACH_EDGE (e, ei, s->exit->preds)
1045 if (bb_in_sd_region (e->src, s))
1046 e->aux = s;
1049 /* Create for all scop regions a single entry and a single exit edge. */
1051 static void
1052 create_sese_edges (vec<sd_region> regions)
1054 int i;
1055 sd_region *s;
1057 FOR_EACH_VEC_ELT (regions, i, s)
1058 create_single_entry_edge (s);
1060 mark_exit_edges (regions);
1062 FOR_EACH_VEC_ELT (regions, i, s)
1063 /* Don't handle multiple edges exiting the function. */
1064 if (!find_single_exit_edge (s)
1065 && s->exit != EXIT_BLOCK_PTR_FOR_FN (cfun))
1066 create_single_exit_edge (s);
1068 unmark_exit_edges (regions);
1070 calculate_dominance_info (CDI_DOMINATORS);
1071 fix_loop_structure (NULL);
1073 #ifdef ENABLE_CHECKING
1074 verify_loop_structure ();
1075 verify_ssa (false, true);
1076 #endif
1079 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1081 static void
1082 build_graphite_scops (vec<sd_region> regions,
1083 vec<scop_p> *scops)
1085 int i;
1086 sd_region *s;
1088 FOR_EACH_VEC_ELT (regions, i, s)
1090 edge entry = find_single_entry_edge (s);
1091 edge exit = find_single_exit_edge (s);
1092 scop_p scop;
1094 if (!exit)
1095 continue;
1097 scop = new_scop (new_sese (entry, exit));
1098 scops->safe_push (scop);
1100 /* Are there overlapping SCoPs? */
1101 #ifdef ENABLE_CHECKING
1103 int j;
1104 sd_region *s2;
1106 FOR_EACH_VEC_ELT (regions, j, s2)
1107 if (s != s2)
1108 gcc_assert (!bb_in_sd_region (s->entry, s2));
1110 #endif
1114 /* Returns true when BB contains only close phi nodes. */
1116 static bool
1117 contains_only_close_phi_nodes (basic_block bb)
1119 gimple_stmt_iterator gsi;
1121 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1122 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
1123 return false;
1125 return true;
1128 /* Print statistics for SCOP to FILE. */
1130 static void
1131 print_graphite_scop_statistics (FILE* file, scop_p scop)
1133 long n_bbs = 0;
1134 long n_loops = 0;
1135 long n_stmts = 0;
1136 long n_conditions = 0;
1137 long n_p_bbs = 0;
1138 long n_p_loops = 0;
1139 long n_p_stmts = 0;
1140 long n_p_conditions = 0;
1142 basic_block bb;
1144 FOR_ALL_BB_FN (bb, cfun)
1146 gimple_stmt_iterator psi;
1147 loop_p loop = bb->loop_father;
1149 if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
1150 continue;
1152 n_bbs++;
1153 n_p_bbs += bb->count;
1155 if (EDGE_COUNT (bb->succs) > 1)
1157 n_conditions++;
1158 n_p_conditions += bb->count;
1161 for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
1163 n_stmts++;
1164 n_p_stmts += bb->count;
1167 if (loop->header == bb && loop_in_sese_p (loop, SCOP_REGION (scop)))
1169 n_loops++;
1170 n_p_loops += bb->count;
1175 fprintf (file, "\nBefore limit_scops SCoP statistics (");
1176 fprintf (file, "BBS:%ld, ", n_bbs);
1177 fprintf (file, "LOOPS:%ld, ", n_loops);
1178 fprintf (file, "CONDITIONS:%ld, ", n_conditions);
1179 fprintf (file, "STMTS:%ld)\n", n_stmts);
1180 fprintf (file, "\nBefore limit_scops SCoP profiling statistics (");
1181 fprintf (file, "BBS:%ld, ", n_p_bbs);
1182 fprintf (file, "LOOPS:%ld, ", n_p_loops);
1183 fprintf (file, "CONDITIONS:%ld, ", n_p_conditions);
1184 fprintf (file, "STMTS:%ld)\n", n_p_stmts);
1187 /* Print statistics for SCOPS to FILE. */
1189 static void
1190 print_graphite_statistics (FILE* file, vec<scop_p> scops)
1192 int i;
1193 scop_p scop;
1195 FOR_EACH_VEC_ELT (scops, i, scop)
1196 print_graphite_scop_statistics (file, scop);
1199 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
1201 Example:
1203 for (i |
1205 for (j | SCoP 1
1206 for (k |
1209 * SCoP frontier, as this line is not surrounded by any loop. *
1211 for (l | SCoP 2
1213 This is necessary as scalar evolution and parameter detection need a
1214 outermost loop to initialize parameters correctly.
1216 TODO: FIX scalar evolution and parameter detection to allow more flexible
1217 SCoP frontiers. */
1219 static void
1220 limit_scops (vec<scop_p> *scops)
1222 auto_vec<sd_region, 3> regions;
1224 int i;
1225 scop_p scop;
1227 FOR_EACH_VEC_ELT (*scops, i, scop)
1229 int j;
1230 loop_p loop;
1231 sese region = SCOP_REGION (scop);
1232 build_sese_loop_nests (region);
1234 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), j, loop)
1235 if (!loop_in_sese_p (loop_outer (loop), region)
1236 && single_exit (loop))
1238 sd_region open_scop;
1239 open_scop.entry = loop->header;
1240 open_scop.exit = single_exit (loop)->dest;
1242 /* This is a hack on top of the limit_scops hack. The
1243 limit_scops hack should disappear all together. */
1244 if (single_succ_p (open_scop.exit)
1245 && contains_only_close_phi_nodes (open_scop.exit))
1246 open_scop.exit = single_succ_edge (open_scop.exit)->dest;
1248 regions.safe_push (open_scop);
1252 free_scops (*scops);
1253 scops->create (3);
1255 create_sese_edges (regions);
1256 build_graphite_scops (regions, scops);
1259 /* Returns true when P1 and P2 are close phis with the same
1260 argument. */
1262 static inline bool
1263 same_close_phi_node (gphi *p1, gphi *p2)
1265 return operand_equal_p (gimple_phi_arg_def (p1, 0),
1266 gimple_phi_arg_def (p2, 0), 0);
1269 /* Remove the close phi node at GSI and replace its rhs with the rhs
1270 of PHI. */
1272 static void
1273 remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
1275 gimple use_stmt;
1276 use_operand_p use_p;
1277 imm_use_iterator imm_iter;
1278 tree res = gimple_phi_result (phi);
1279 tree def = gimple_phi_result (gsi->phi ());
1281 gcc_assert (same_close_phi_node (phi, gsi->phi ()));
1283 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
1285 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1286 SET_USE (use_p, res);
1288 update_stmt (use_stmt);
1290 /* It is possible that we just created a duplicate close-phi
1291 for an already-processed containing loop. Check for this
1292 case and clean it up. */
1293 if (gimple_code (use_stmt) == GIMPLE_PHI
1294 && gimple_phi_num_args (use_stmt) == 1)
1295 make_close_phi_nodes_unique (gimple_bb (use_stmt));
1298 remove_phi_node (gsi, true);
1301 /* Removes all the close phi duplicates from BB. */
1303 static void
1304 make_close_phi_nodes_unique (basic_block bb)
1306 gphi_iterator psi;
1308 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1310 gphi_iterator gsi = psi;
1311 gphi *phi = psi.phi ();
1313 /* At this point, PHI should be a close phi in normal form. */
1314 gcc_assert (gimple_phi_num_args (phi) == 1);
1316 /* Iterate over the next phis and remove duplicates. */
1317 gsi_next (&gsi);
1318 while (!gsi_end_p (gsi))
1319 if (same_close_phi_node (phi, gsi.phi ()))
1320 remove_duplicate_close_phi (phi, &gsi);
1321 else
1322 gsi_next (&gsi);
1326 /* Transforms LOOP to the canonical loop closed SSA form. */
1328 static void
1329 canonicalize_loop_closed_ssa (loop_p loop)
1331 edge e = single_exit (loop);
1332 basic_block bb;
1334 if (!e || e->flags & EDGE_ABNORMAL)
1335 return;
1337 bb = e->dest;
1339 if (single_pred_p (bb))
1341 e = split_block_after_labels (bb);
1342 make_close_phi_nodes_unique (e->src);
1344 else
1346 gphi_iterator psi;
1347 basic_block close = split_edge (e);
1349 e = single_succ_edge (close);
1351 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1353 gphi *phi = psi.phi ();
1354 unsigned i;
1356 for (i = 0; i < gimple_phi_num_args (phi); i++)
1357 if (gimple_phi_arg_edge (phi, i) == e)
1359 tree res, arg = gimple_phi_arg_def (phi, i);
1360 use_operand_p use_p;
1361 gphi *close_phi;
1363 if (TREE_CODE (arg) != SSA_NAME)
1364 continue;
1366 close_phi = create_phi_node (NULL_TREE, close);
1367 res = create_new_def_for (arg, close_phi,
1368 gimple_phi_result_ptr (close_phi));
1369 add_phi_arg (close_phi, arg,
1370 gimple_phi_arg_edge (close_phi, 0),
1371 UNKNOWN_LOCATION);
1372 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
1373 replace_exp (use_p, res);
1374 update_stmt (phi);
1378 make_close_phi_nodes_unique (close);
1381 /* The code above does not properly handle changes in the post dominance
1382 information (yet). */
1383 free_dominance_info (CDI_POST_DOMINATORS);
1386 /* Converts the current loop closed SSA form to a canonical form
1387 expected by the Graphite code generation.
1389 The loop closed SSA form has the following invariant: a variable
1390 defined in a loop that is used outside the loop appears only in the
1391 phi nodes in the destination of the loop exit. These phi nodes are
1392 called close phi nodes.
1394 The canonical loop closed SSA form contains the extra invariants:
1396 - when the loop contains only one exit, the close phi nodes contain
1397 only one argument. That implies that the basic block that contains
1398 the close phi nodes has only one predecessor, that is a basic block
1399 in the loop.
1401 - the basic block containing the close phi nodes does not contain
1402 other statements.
1404 - there exist only one phi node per definition in the loop.
1407 static void
1408 canonicalize_loop_closed_ssa_form (void)
1410 loop_p loop;
1412 #ifdef ENABLE_CHECKING
1413 verify_loop_closed_ssa (true);
1414 #endif
1416 FOR_EACH_LOOP (loop, 0)
1417 canonicalize_loop_closed_ssa (loop);
1419 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1420 update_ssa (TODO_update_ssa);
1422 #ifdef ENABLE_CHECKING
1423 verify_loop_closed_ssa (true);
1424 #endif
1427 /* Find Static Control Parts (SCoP) in the current function and pushes
1428 them to SCOPS. */
1430 void
1431 build_scops (vec<scop_p> *scops)
1433 struct loop *loop = current_loops->tree_root;
1434 auto_vec<sd_region, 3> regions;
1436 canonicalize_loop_closed_ssa_form ();
1437 build_scops_1 (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1438 ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father,
1439 &regions, loop);
1440 create_sese_edges (regions);
1441 build_graphite_scops (regions, scops);
1443 if (dump_file && (dump_flags & TDF_DETAILS))
1444 print_graphite_statistics (dump_file, *scops);
1446 limit_scops (scops);
1447 regions.release ();
1449 if (dump_file && (dump_flags & TDF_DETAILS))
1450 fprintf (dump_file, "\nnumber of SCoPs: %d\n",
1451 scops ? scops->length () : 0);
1454 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1455 different colors. If there are not enough colors, paint the
1456 remaining SCoPs in gray.
1458 Special nodes:
1459 - "*" after the node number denotes the entry of a SCoP,
1460 - "#" after the node number denotes the exit of a SCoP,
1461 - "()" around the node number denotes the entry or the
1462 exit nodes of the SCOP. These are not part of SCoP. */
1464 static void
1465 dot_all_scops_1 (FILE *file, vec<scop_p> scops)
1467 basic_block bb;
1468 edge e;
1469 edge_iterator ei;
1470 scop_p scop;
1471 const char* color;
1472 int i;
1474 /* Disable debugging while printing graph. */
1475 int tmp_dump_flags = dump_flags;
1476 dump_flags = 0;
1478 fprintf (file, "digraph all {\n");
1480 FOR_ALL_BB_FN (bb, cfun)
1482 int part_of_scop = false;
1484 /* Use HTML for every bb label. So we are able to print bbs
1485 which are part of two different SCoPs, with two different
1486 background colors. */
1487 fprintf (file, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1488 bb->index);
1489 fprintf (file, "CELLSPACING=\"0\">\n");
1491 /* Select color for SCoP. */
1492 FOR_EACH_VEC_ELT (scops, i, scop)
1494 sese region = SCOP_REGION (scop);
1495 if (bb_in_sese_p (bb, region)
1496 || (SESE_EXIT_BB (region) == bb)
1497 || (SESE_ENTRY_BB (region) == bb))
1499 switch (i % 17)
1501 case 0: /* red */
1502 color = "#e41a1c";
1503 break;
1504 case 1: /* blue */
1505 color = "#377eb8";
1506 break;
1507 case 2: /* green */
1508 color = "#4daf4a";
1509 break;
1510 case 3: /* purple */
1511 color = "#984ea3";
1512 break;
1513 case 4: /* orange */
1514 color = "#ff7f00";
1515 break;
1516 case 5: /* yellow */
1517 color = "#ffff33";
1518 break;
1519 case 6: /* brown */
1520 color = "#a65628";
1521 break;
1522 case 7: /* rose */
1523 color = "#f781bf";
1524 break;
1525 case 8:
1526 color = "#8dd3c7";
1527 break;
1528 case 9:
1529 color = "#ffffb3";
1530 break;
1531 case 10:
1532 color = "#bebada";
1533 break;
1534 case 11:
1535 color = "#fb8072";
1536 break;
1537 case 12:
1538 color = "#80b1d3";
1539 break;
1540 case 13:
1541 color = "#fdb462";
1542 break;
1543 case 14:
1544 color = "#b3de69";
1545 break;
1546 case 15:
1547 color = "#fccde5";
1548 break;
1549 case 16:
1550 color = "#bc80bd";
1551 break;
1552 default: /* gray */
1553 color = "#999999";
1556 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
1558 if (!bb_in_sese_p (bb, region))
1559 fprintf (file, " (");
1561 if (bb == SESE_ENTRY_BB (region)
1562 && bb == SESE_EXIT_BB (region))
1563 fprintf (file, " %d*# ", bb->index);
1564 else if (bb == SESE_ENTRY_BB (region))
1565 fprintf (file, " %d* ", bb->index);
1566 else if (bb == SESE_EXIT_BB (region))
1567 fprintf (file, " %d# ", bb->index);
1568 else
1569 fprintf (file, " %d ", bb->index);
1571 if (!bb_in_sese_p (bb,region))
1572 fprintf (file, ")");
1574 fprintf (file, "</TD></TR>\n");
1575 part_of_scop = true;
1579 if (!part_of_scop)
1581 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1582 fprintf (file, " %d </TD></TR>\n", bb->index);
1584 fprintf (file, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1587 FOR_ALL_BB_FN (bb, cfun)
1589 FOR_EACH_EDGE (e, ei, bb->succs)
1590 fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
1593 fputs ("}\n\n", file);
1595 /* Enable debugging again. */
1596 dump_flags = tmp_dump_flags;
1599 /* Display all SCoPs using dotty. */
1601 DEBUG_FUNCTION void
1602 dot_all_scops (vec<scop_p> scops)
1604 /* When debugging, enable the following code. This cannot be used
1605 in production compilers because it calls "system". */
1606 #if 0
1607 int x;
1608 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1609 gcc_assert (stream);
1611 dot_all_scops_1 (stream, scops);
1612 fclose (stream);
1614 x = system ("dotty /tmp/allscops.dot &");
1615 #else
1616 dot_all_scops_1 (stderr, scops);
1617 #endif
1620 /* Display all SCoPs using dotty. */
1622 DEBUG_FUNCTION void
1623 dot_scop (scop_p scop)
1625 auto_vec<scop_p, 1> scops;
1627 if (scop)
1628 scops.safe_push (scop);
1630 /* When debugging, enable the following code. This cannot be used
1631 in production compilers because it calls "system". */
1632 #if 0
1634 int x;
1635 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1636 gcc_assert (stream);
1638 dot_all_scops_1 (stream, scops);
1639 fclose (stream);
1640 x = system ("dotty /tmp/allscops.dot &");
1642 #else
1643 dot_all_scops_1 (stderr, scops);
1644 #endif
1647 #endif /* HAVE_isl */