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)
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/>. */
27 #include <isl/union_map.h>
31 #include "coretypes.h"
35 #include "double-int.h"
43 #include "fold-const.h"
46 #include "hard-reg-set.h"
49 #include "dominance.h"
51 #include "basic-block.h"
52 #include "tree-ssa-alias.h"
53 #include "internal-fn.h"
54 #include "gimple-expr.h"
57 #include "gimple-iterator.h"
58 #include "gimple-ssa.h"
59 #include "tree-phinodes.h"
60 #include "ssa-iterators.h"
61 #include "tree-ssa-loop-manip.h"
62 #include "tree-ssa-loop-niter.h"
63 #include "tree-ssa-loop.h"
64 #include "tree-into-ssa.h"
67 #include "tree-chrec.h"
68 #include "tree-data-ref.h"
69 #include "tree-scalar-evolution.h"
70 #include "tree-pass.h"
72 #include "tree-ssa-propagate.h"
75 #include "graphite-poly.h"
76 #include "graphite-scop-detection.h"
78 /* Forward declarations. */
79 static void make_close_phi_nodes_unique (basic_block
);
81 /* The type of the analyzed basic block. */
83 typedef enum gbb_type
{
85 GBB_LOOP_SING_EXIT_HEADER
,
86 GBB_LOOP_MULT_EXIT_HEADER
,
93 /* Detect the type of BB. Loop headers are only marked, if they are
94 new. This means their loop_father is different to LAST_LOOP.
95 Otherwise they are treated like any other bb and their type can be
99 get_bb_type (basic_block bb
, struct loop
*last_loop
)
101 vec
<basic_block
> dom
;
103 struct loop
*loop
= bb
->loop_father
;
105 /* Check, if we entry into a new loop. */
106 if (loop
!= last_loop
)
108 if (single_exit (loop
) != NULL
)
109 return GBB_LOOP_SING_EXIT_HEADER
;
110 else if (loop
->num
!= 0)
111 return GBB_LOOP_MULT_EXIT_HEADER
;
113 return GBB_COND_HEADER
;
116 dom
= get_dominated_by (CDI_DOMINATORS
, bb
);
117 nb_dom
= dom
.length ();
123 if (nb_dom
== 1 && single_succ_p (bb
))
126 return GBB_COND_HEADER
;
129 /* A SCoP detection region, defined using bbs as borders.
131 All control flow touching this region, comes in passing basic_block
132 ENTRY and leaves passing basic_block EXIT. By using bbs instead of
133 edges for the borders we are able to represent also regions that do
134 not have a single entry or exit edge.
136 But as they have a single entry basic_block and a single exit
137 basic_block, we are able to generate for every sd_region a single
145 / \ This region contains: {3, 4, 5, 6, 7, 8}
153 typedef struct sd_region_p
155 /* The entry bb dominates all bbs in the sd_region. It is part of
159 /* The exit bb postdominates all bbs in the sd_region, but is not
160 part of the region. */
166 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
169 move_sd_regions (vec
<sd_region
> *source
, vec
<sd_region
> *target
)
174 FOR_EACH_VEC_ELT (*source
, i
, s
)
175 target
->safe_push (*s
);
180 /* Something like "n * m" is not allowed. */
183 graphite_can_represent_init (tree e
)
185 switch (TREE_CODE (e
))
187 case POLYNOMIAL_CHREC
:
188 return graphite_can_represent_init (CHREC_LEFT (e
))
189 && graphite_can_represent_init (CHREC_RIGHT (e
));
192 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
193 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
194 && tree_fits_shwi_p (TREE_OPERAND (e
, 1));
196 return graphite_can_represent_init (TREE_OPERAND (e
, 1))
197 && tree_fits_shwi_p (TREE_OPERAND (e
, 0));
200 case POINTER_PLUS_EXPR
:
202 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
203 && graphite_can_represent_init (TREE_OPERAND (e
, 1));
208 case NON_LVALUE_EXPR
:
209 return graphite_can_represent_init (TREE_OPERAND (e
, 0));
218 /* Return true when SCEV can be represented in the polyhedral model.
220 An expression can be represented, if it can be expressed as an
221 affine expression. For loops (i, j) and parameters (m, n) all
222 affine expressions are of the form:
224 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
226 1 i + 20 j + (-2) m + 25
228 Something like "i * n" or "n * m" is not allowed. */
231 graphite_can_represent_scev (tree scev
)
233 if (chrec_contains_undetermined (scev
))
236 /* We disable the handling of pointer types, because it’s currently not
237 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
238 the only nodes, which are disabled in case they are pointers to object
239 types, but this can be changed. */
241 if (POINTER_TYPE_P (TREE_TYPE (scev
)) && TREE_CODE (scev
) == SSA_NAME
)
244 switch (TREE_CODE (scev
))
249 case NON_LVALUE_EXPR
:
250 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0));
253 case POINTER_PLUS_EXPR
:
255 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
256 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
259 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 0)))
260 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 1)))
261 && !(chrec_contains_symbols (TREE_OPERAND (scev
, 0))
262 && chrec_contains_symbols (TREE_OPERAND (scev
, 1)))
263 && graphite_can_represent_init (scev
)
264 && graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
265 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
267 case POLYNOMIAL_CHREC
:
268 /* Check for constant strides. With a non constant stride of
269 'n' we would have a value of 'iv * n'. Also check that the
270 initial value can represented: for example 'n * m' cannot be
272 if (!evolution_function_right_is_integer_cst (scev
)
273 || !graphite_can_represent_init (scev
))
275 return graphite_can_represent_scev (CHREC_LEFT (scev
));
281 /* Only affine functions can be represented. */
282 if (tree_contains_chrecs (scev
, NULL
)
283 || !scev_is_linear_expression (scev
))
290 /* Return true when EXPR can be represented in the polyhedral model.
292 This means an expression can be represented, if it is linear with
293 respect to the loops and the strides are non parametric.
294 LOOP is the place where the expr will be evaluated. SCOP_ENTRY defines the
295 entry of the region we analyse. */
298 graphite_can_represent_expr (basic_block scop_entry
, loop_p loop
,
301 tree scev
= analyze_scalar_evolution (loop
, expr
);
303 scev
= instantiate_scev (scop_entry
, loop
, scev
);
305 return graphite_can_represent_scev (scev
);
308 /* Return true if the data references of STMT can be represented by
312 stmt_has_simple_data_refs_p (loop_p outermost_loop ATTRIBUTE_UNUSED
,
319 vec
<data_reference_p
> drs
= vNULL
;
322 for (outer
= loop_containing_stmt (stmt
); outer
; outer
= loop_outer (outer
))
324 graphite_find_data_references_in_stmt (outer
,
325 loop_containing_stmt (stmt
),
328 FOR_EACH_VEC_ELT (drs
, j
, dr
)
329 for (i
= 0; i
< DR_NUM_DIMENSIONS (dr
); i
++)
330 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr
, i
)))
336 free_data_refs (drs
);
341 free_data_refs (drs
);
345 /* Return true only when STMT is simple enough for being handled by
346 Graphite. This depends on SCOP_ENTRY, as the parameters are
347 initialized relatively to this basic block, the linear functions
348 are initialized to OUTERMOST_LOOP and BB is the place where we try
349 to evaluate the STMT. */
352 stmt_simple_for_scop_p (basic_block scop_entry
, loop_p outermost_loop
,
353 gimple stmt
, basic_block bb
)
355 loop_p loop
= bb
->loop_father
;
357 gcc_assert (scop_entry
);
359 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
360 Calls have side-effects, except those to const or pure
362 if (gimple_has_volatile_ops (stmt
)
363 || (gimple_code (stmt
) == GIMPLE_CALL
364 && !(gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
)))
365 || (gimple_code (stmt
) == GIMPLE_ASM
))
368 if (is_gimple_debug (stmt
))
371 if (!stmt_has_simple_data_refs_p (outermost_loop
, stmt
))
374 switch (gimple_code (stmt
))
382 /* We can handle all binary comparisons. Inequalities are
383 also supported as they can be represented with union of
385 enum tree_code code
= gimple_cond_code (stmt
);
386 if (!(code
== LT_EXPR
394 for (unsigned i
= 0; i
< 2; ++i
)
396 tree op
= gimple_op (stmt
, i
);
397 if (!graphite_can_represent_expr (scop_entry
, loop
, op
)
398 /* We can not handle REAL_TYPE. Failed for pr39260. */
399 || TREE_CODE (TREE_TYPE (op
)) == REAL_TYPE
)
411 /* These nodes cut a new scope. */
418 /* Returns the statement of BB that contains a harmful operation: that
419 can be a function call with side effects, the induction variables
420 are not linear with respect to SCOP_ENTRY, etc. The current open
421 scop should end before this statement. The evaluation is limited using
422 OUTERMOST_LOOP as outermost loop that may change. */
425 harmful_stmt_in_bb (basic_block scop_entry
, loop_p outer_loop
, basic_block bb
)
427 gimple_stmt_iterator gsi
;
429 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
430 if (!stmt_simple_for_scop_p (scop_entry
, outer_loop
, gsi_stmt (gsi
), bb
))
431 return gsi_stmt (gsi
);
436 /* Return true if LOOP can be represented in the polyhedral
437 representation. This is evaluated taking SCOP_ENTRY and
438 OUTERMOST_LOOP in mind. */
441 graphite_can_represent_loop (basic_block scop_entry
, loop_p loop
)
444 struct tree_niter_desc niter_desc
;
446 /* FIXME: For the moment, graphite cannot be used on loops that
447 iterate using induction variables that wrap. */
449 return number_of_iterations_exit (loop
, single_exit (loop
), &niter_desc
, false)
450 && niter_desc
.control
.no_overflow
451 && (niter
= number_of_latch_executions (loop
))
452 && !chrec_contains_undetermined (niter
)
453 && graphite_can_represent_expr (scop_entry
, loop
, niter
);
456 /* Store information needed by scopdet_* functions. */
460 /* Exit of the open scop would stop if the current BB is harmful. */
463 /* Where the next scop would start if the current BB is harmful. */
466 /* The bb or one of its children contains open loop exits. That means
467 loop exit nodes that are not surrounded by a loop dominated by bb. */
470 /* The bb or one of its children contains only structures we can handle. */
474 static struct scopdet_info
build_scops_1 (basic_block
, loop_p
,
475 vec
<sd_region
> *, loop_p
);
477 /* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
478 to SCOPS. TYPE is the gbb_type of BB. */
480 static struct scopdet_info
481 scopdet_basic_block_info (basic_block bb
, loop_p outermost_loop
,
482 vec
<sd_region
> *scops
, gbb_type type
)
484 loop_p loop
= bb
->loop_father
;
485 struct scopdet_info result
;
488 /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps. */
489 basic_block entry_block
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
490 stmt
= harmful_stmt_in_bb (entry_block
, outermost_loop
, bb
);
491 result
.difficult
= (stmt
!= NULL
);
498 result
.exits
= false;
500 /* Mark bbs terminating a SESE region difficult, if they start
501 a condition or if the block it exits to cannot be split
502 with make_forwarder_block. */
503 if (!single_succ_p (bb
)
504 || bb_has_abnormal_pred (single_succ (bb
)))
505 result
.difficult
= true;
507 result
.exit
= single_succ (bb
);
512 result
.next
= single_succ (bb
);
513 result
.exits
= false;
514 result
.exit
= single_succ (bb
);
517 case GBB_LOOP_SING_EXIT_HEADER
:
519 auto_vec
<sd_region
, 3> regions
;
520 struct scopdet_info sinfo
;
521 edge exit_e
= single_exit (loop
);
523 sinfo
= build_scops_1 (bb
, outermost_loop
, ®ions
, loop
);
525 if (!graphite_can_represent_loop (entry_block
, loop
))
526 result
.difficult
= true;
528 result
.difficult
|= sinfo
.difficult
;
530 /* Try again with another loop level. */
532 && loop_depth (outermost_loop
) + 1 == loop_depth (loop
))
534 outermost_loop
= loop
;
539 sinfo
= scopdet_basic_block_info (bb
, outermost_loop
, scops
, type
);
542 result
.difficult
= true;
545 move_sd_regions (®ions
, scops
);
549 open_scop
.entry
= bb
;
550 open_scop
.exit
= exit_e
->dest
;
551 scops
->safe_push (open_scop
);
557 result
.exit
= exit_e
->dest
;
558 result
.next
= exit_e
->dest
;
560 /* If we do not dominate result.next, remove it. It's either
561 the exit block, or another bb dominates it and will
562 call the scop detection for this bb. */
563 if (!dominated_by_p (CDI_DOMINATORS
, result
.next
, bb
))
566 if (exit_e
->src
->loop_father
!= loop
)
569 result
.exits
= false;
571 if (result
.difficult
)
572 move_sd_regions (®ions
, scops
);
580 case GBB_LOOP_MULT_EXIT_HEADER
:
582 /* XXX: For now we just do not join loops with multiple exits. If the
583 exits lead to the same bb it may be possible to join the loop. */
584 auto_vec
<sd_region
, 3> regions
;
585 vec
<edge
> exits
= get_loop_exit_edges (loop
);
588 build_scops_1 (bb
, loop
, ®ions
, loop
);
590 /* Scan the code dominated by this loop. This means all bbs, that are
591 are dominated by a bb in this loop, but are not part of this loop.
594 - The loop exit destination is dominated by the exit sources.
596 TODO: We miss here the more complex cases:
597 - The exit destinations are dominated by another bb inside
599 - The loop dominates bbs, that are not exit destinations. */
600 FOR_EACH_VEC_ELT (exits
, i
, e
)
601 if (e
->src
->loop_father
== loop
602 && dominated_by_p (CDI_DOMINATORS
, e
->dest
, e
->src
))
604 if (loop_outer (outermost_loop
))
605 outermost_loop
= loop_outer (outermost_loop
);
607 /* Pass loop_outer to recognize e->dest as loop header in
609 if (e
->dest
->loop_father
->header
== e
->dest
)
610 build_scops_1 (e
->dest
, outermost_loop
, ®ions
,
611 loop_outer (e
->dest
->loop_father
));
613 build_scops_1 (e
->dest
, outermost_loop
, ®ions
,
614 e
->dest
->loop_father
);
619 result
.difficult
= true;
620 result
.exits
= false;
621 move_sd_regions (®ions
, scops
);
625 case GBB_COND_HEADER
:
627 auto_vec
<sd_region
, 3> regions
;
628 struct scopdet_info sinfo
;
629 vec
<basic_block
> dominated
;
632 basic_block last_exit
= NULL
;
634 result
.exits
= false;
636 /* First check the successors of BB, and check if it is
637 possible to join the different branches. */
638 FOR_EACH_VEC_SAFE_ELT (bb
->succs
, i
, e
)
640 /* Ignore loop exits. They will be handled after the loop
642 if (loop_exits_to_bb_p (loop
, e
->dest
))
648 /* Do not follow edges that lead to the end of the
649 conditions block. For example, in
659 the edge from 0 => 6. Only check if all paths lead to
662 if (!single_pred_p (e
->dest
))
664 /* Check, if edge leads directly to the end of this
669 if (e
->dest
!= last_exit
)
670 result
.difficult
= true;
675 if (!dominated_by_p (CDI_DOMINATORS
, e
->dest
, bb
))
677 result
.difficult
= true;
681 sinfo
= build_scops_1 (e
->dest
, outermost_loop
, ®ions
, loop
);
683 result
.exits
|= sinfo
.exits
;
684 result
.difficult
|= sinfo
.difficult
;
686 /* Checks, if all branches end at the same point.
687 If that is true, the condition stays joinable.
688 Have a look at the example above. */
692 last_exit
= sinfo
.exit
;
694 if (sinfo
.exit
!= last_exit
)
695 result
.difficult
= true;
698 result
.difficult
= true;
702 result
.difficult
= true;
704 /* Join the branches of the condition if possible. */
705 if (!result
.exits
&& !result
.difficult
)
707 /* Only return a next pointer if we dominate this pointer.
708 Otherwise it will be handled by the bb dominating it. */
709 if (dominated_by_p (CDI_DOMINATORS
, last_exit
, bb
)
711 result
.next
= last_exit
;
715 result
.exit
= last_exit
;
721 /* Scan remaining bbs dominated by BB. */
722 dominated
= get_dominated_by (CDI_DOMINATORS
, bb
);
724 FOR_EACH_VEC_ELT (dominated
, i
, dom_bb
)
726 /* Ignore loop exits: they will be handled after the loop body. */
727 if (loop_depth (find_common_loop (loop
, dom_bb
->loop_father
))
734 /* Ignore the bbs processed above. */
735 if (single_pred_p (dom_bb
) && single_pred (dom_bb
) == bb
)
738 if (loop_depth (loop
) > loop_depth (dom_bb
->loop_father
))
739 sinfo
= build_scops_1 (dom_bb
, outermost_loop
, ®ions
,
742 sinfo
= build_scops_1 (dom_bb
, outermost_loop
, ®ions
, loop
);
744 result
.exits
|= sinfo
.exits
;
745 result
.difficult
= true;
749 dominated
.release ();
752 move_sd_regions (®ions
, scops
);
764 /* Starting from CURRENT we walk the dominance tree and add new sd_regions to
765 SCOPS. The analyse if a sd_region can be handled is based on the value
766 of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change. LOOP
767 is the loop in which CURRENT is handled.
769 TODO: These functions got a little bit big. They definitely should be cleaned
772 static struct scopdet_info
773 build_scops_1 (basic_block current
, loop_p outermost_loop
,
774 vec
<sd_region
> *scops
, loop_p loop
)
776 bool in_scop
= false;
778 struct scopdet_info sinfo
;
780 /* Initialize result. */
781 struct scopdet_info result
;
782 result
.exits
= false;
783 result
.difficult
= false;
786 open_scop
.entry
= NULL
;
787 open_scop
.exit
= NULL
;
790 /* Loop over the dominance tree. If we meet a difficult bb, close
791 the current SCoP. Loop and condition header start a new layer,
792 and can only be added if all bbs in deeper layers are simple. */
793 while (current
!= NULL
)
795 sinfo
= scopdet_basic_block_info (current
, outermost_loop
, scops
,
796 get_bb_type (current
, loop
));
798 if (!in_scop
&& !(sinfo
.exits
|| sinfo
.difficult
))
800 open_scop
.entry
= current
;
801 open_scop
.exit
= NULL
;
804 else if (in_scop
&& (sinfo
.exits
|| sinfo
.difficult
))
806 open_scop
.exit
= current
;
807 scops
->safe_push (open_scop
);
811 result
.difficult
|= sinfo
.difficult
;
812 result
.exits
|= sinfo
.exits
;
814 current
= sinfo
.next
;
817 /* Try to close open_scop, if we are still in an open SCoP. */
820 open_scop
.exit
= sinfo
.exit
;
821 gcc_assert (open_scop
.exit
);
822 scops
->safe_push (open_scop
);
825 result
.exit
= sinfo
.exit
;
829 /* Checks if a bb is contained in REGION. */
832 bb_in_sd_region (basic_block bb
, sd_region
*region
)
834 return bb_in_region (bb
, region
->entry
, region
->exit
);
837 /* Returns the single entry edge of REGION, if it does not exits NULL. */
840 find_single_entry_edge (sd_region
*region
)
846 FOR_EACH_EDGE (e
, ei
, region
->entry
->preds
)
847 if (!bb_in_sd_region (e
->src
, region
))
862 /* Returns the single exit edge of REGION, if it does not exits NULL. */
865 find_single_exit_edge (sd_region
*region
)
871 FOR_EACH_EDGE (e
, ei
, region
->exit
->preds
)
872 if (bb_in_sd_region (e
->src
, region
))
887 /* Create a single entry edge for REGION. */
890 create_single_entry_edge (sd_region
*region
)
892 if (find_single_entry_edge (region
))
895 /* There are multiple predecessors for bb_3
908 There are two edges (1->3, 2->3), that point from outside into the region,
909 and another one (5->3), a loop latch, lead to bb_3.
917 | |\ (3.0 -> 3.1) = single entry edge
926 If the loop is part of the SCoP, we have to redirect the loop latches.
932 | | (3.0 -> 3.1) = entry edge
941 if (region
->entry
->loop_father
->header
!= region
->entry
942 || dominated_by_p (CDI_DOMINATORS
,
943 loop_latch_edge (region
->entry
->loop_father
)->src
,
946 edge forwarder
= split_block_after_labels (region
->entry
);
947 region
->entry
= forwarder
->dest
;
950 /* This case is never executed, as the loop headers seem always to have a
951 single edge pointing from outside into the loop. */
954 gcc_checking_assert (find_single_entry_edge (region
));
957 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
960 sd_region_without_exit (edge e
)
962 sd_region
*r
= (sd_region
*) e
->aux
;
965 return r
->exit
== NULL
;
970 /* Create a single exit edge for REGION. */
973 create_single_exit_edge (sd_region
*region
)
977 edge forwarder
= NULL
;
980 /* We create a forwarder bb (5) for all edges leaving this region
981 (3->5, 4->5). All other edges leading to the same bb, are moved
982 to a new bb (6). If these edges where part of another region (2->5)
983 we update the region->exit pointer, of this region.
985 To identify which edge belongs to which region we depend on the e->aux
986 pointer in every edge. It points to the region of the edge or to NULL,
987 if the edge is not part of any region.
989 1 2 3 4 1->5 no region, 2->5 region->exit = 5,
990 \| |/ 3->5 region->exit = NULL, 4->5 region->exit = NULL
995 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
996 | | \/ 3->5 no region, 4->5 no region,
998 \| / 5->6 region->exit = 6
1001 Now there is only a single exit edge (5->6). */
1002 exit
= region
->exit
;
1003 region
->exit
= NULL
;
1004 forwarder
= make_forwarder_block (exit
, &sd_region_without_exit
, NULL
);
1006 /* Unmark the edges, that are no longer exit edges. */
1007 FOR_EACH_EDGE (e
, ei
, forwarder
->src
->preds
)
1011 /* Mark the new exit edge. */
1012 single_succ_edge (forwarder
->src
)->aux
= region
;
1014 /* Update the exit bb of all regions, where exit edges lead to
1016 FOR_EACH_EDGE (e
, ei
, forwarder
->dest
->preds
)
1018 ((sd_region
*) e
->aux
)->exit
= forwarder
->dest
;
1020 gcc_checking_assert (find_single_exit_edge (region
));
1023 /* Unmark the exit edges of all REGIONS.
1024 See comment in "create_single_exit_edge". */
1027 unmark_exit_edges (vec
<sd_region
> regions
)
1034 FOR_EACH_VEC_ELT (regions
, i
, s
)
1035 FOR_EACH_EDGE (e
, ei
, s
->exit
->preds
)
1040 /* Mark the exit edges of all REGIONS.
1041 See comment in "create_single_exit_edge". */
1044 mark_exit_edges (vec
<sd_region
> regions
)
1051 FOR_EACH_VEC_ELT (regions
, i
, s
)
1052 FOR_EACH_EDGE (e
, ei
, s
->exit
->preds
)
1053 if (bb_in_sd_region (e
->src
, s
))
1057 /* Create for all scop regions a single entry and a single exit edge. */
1060 create_sese_edges (vec
<sd_region
> regions
)
1065 FOR_EACH_VEC_ELT (regions
, i
, s
)
1066 create_single_entry_edge (s
);
1068 mark_exit_edges (regions
);
1070 FOR_EACH_VEC_ELT (regions
, i
, s
)
1071 /* Don't handle multiple edges exiting the function. */
1072 if (!find_single_exit_edge (s
)
1073 && s
->exit
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1074 create_single_exit_edge (s
);
1076 unmark_exit_edges (regions
);
1078 calculate_dominance_info (CDI_DOMINATORS
);
1079 fix_loop_structure (NULL
);
1081 #ifdef ENABLE_CHECKING
1082 verify_loop_structure ();
1083 verify_ssa (false, true);
1087 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1090 build_graphite_scops (vec
<sd_region
> regions
,
1096 FOR_EACH_VEC_ELT (regions
, i
, s
)
1098 edge entry
= find_single_entry_edge (s
);
1099 edge exit
= find_single_exit_edge (s
);
1105 scop
= new_scop (new_sese (entry
, exit
));
1106 scops
->safe_push (scop
);
1108 /* Are there overlapping SCoPs? */
1109 #ifdef ENABLE_CHECKING
1114 FOR_EACH_VEC_ELT (regions
, j
, s2
)
1116 gcc_assert (!bb_in_sd_region (s
->entry
, s2
));
1122 /* Returns true when BB contains only close phi nodes. */
1125 contains_only_close_phi_nodes (basic_block bb
)
1127 gimple_stmt_iterator gsi
;
1129 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1130 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_LABEL
)
1136 /* Print statistics for SCOP to FILE. */
1139 print_graphite_scop_statistics (FILE* file
, scop_p scop
)
1144 long n_conditions
= 0;
1148 long n_p_conditions
= 0;
1152 FOR_ALL_BB_FN (bb
, cfun
)
1154 gimple_stmt_iterator psi
;
1155 loop_p loop
= bb
->loop_father
;
1157 if (!bb_in_sese_p (bb
, SCOP_REGION (scop
)))
1161 n_p_bbs
+= bb
->count
;
1163 if (EDGE_COUNT (bb
->succs
) > 1)
1166 n_p_conditions
+= bb
->count
;
1169 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1172 n_p_stmts
+= bb
->count
;
1175 if (loop
->header
== bb
&& loop_in_sese_p (loop
, SCOP_REGION (scop
)))
1178 n_p_loops
+= bb
->count
;
1183 fprintf (file
, "\nBefore limit_scops SCoP statistics (");
1184 fprintf (file
, "BBS:%ld, ", n_bbs
);
1185 fprintf (file
, "LOOPS:%ld, ", n_loops
);
1186 fprintf (file
, "CONDITIONS:%ld, ", n_conditions
);
1187 fprintf (file
, "STMTS:%ld)\n", n_stmts
);
1188 fprintf (file
, "\nBefore limit_scops SCoP profiling statistics (");
1189 fprintf (file
, "BBS:%ld, ", n_p_bbs
);
1190 fprintf (file
, "LOOPS:%ld, ", n_p_loops
);
1191 fprintf (file
, "CONDITIONS:%ld, ", n_p_conditions
);
1192 fprintf (file
, "STMTS:%ld)\n", n_p_stmts
);
1195 /* Print statistics for SCOPS to FILE. */
1198 print_graphite_statistics (FILE* file
, vec
<scop_p
> scops
)
1203 FOR_EACH_VEC_ELT (scops
, i
, scop
)
1204 print_graphite_scop_statistics (file
, scop
);
1207 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
1217 * SCoP frontier, as this line is not surrounded by any loop. *
1221 This is necessary as scalar evolution and parameter detection need a
1222 outermost loop to initialize parameters correctly.
1224 TODO: FIX scalar evolution and parameter detection to allow more flexible
1228 limit_scops (vec
<scop_p
> *scops
)
1230 auto_vec
<sd_region
, 3> regions
;
1235 FOR_EACH_VEC_ELT (*scops
, i
, scop
)
1239 sese region
= SCOP_REGION (scop
);
1240 build_sese_loop_nests (region
);
1242 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region
), j
, loop
)
1243 if (!loop_in_sese_p (loop_outer (loop
), region
)
1244 && single_exit (loop
))
1246 sd_region open_scop
;
1247 open_scop
.entry
= loop
->header
;
1248 open_scop
.exit
= single_exit (loop
)->dest
;
1250 /* This is a hack on top of the limit_scops hack. The
1251 limit_scops hack should disappear all together. */
1252 if (single_succ_p (open_scop
.exit
)
1253 && contains_only_close_phi_nodes (open_scop
.exit
))
1254 open_scop
.exit
= single_succ_edge (open_scop
.exit
)->dest
;
1256 regions
.safe_push (open_scop
);
1260 free_scops (*scops
);
1263 create_sese_edges (regions
);
1264 build_graphite_scops (regions
, scops
);
1267 /* Returns true when P1 and P2 are close phis with the same
1271 same_close_phi_node (gphi
*p1
, gphi
*p2
)
1273 return operand_equal_p (gimple_phi_arg_def (p1
, 0),
1274 gimple_phi_arg_def (p2
, 0), 0);
1277 /* Remove the close phi node at GSI and replace its rhs with the rhs
1281 remove_duplicate_close_phi (gphi
*phi
, gphi_iterator
*gsi
)
1284 use_operand_p use_p
;
1285 imm_use_iterator imm_iter
;
1286 tree res
= gimple_phi_result (phi
);
1287 tree def
= gimple_phi_result (gsi
->phi ());
1289 gcc_assert (same_close_phi_node (phi
, gsi
->phi ()));
1291 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
1293 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
1294 SET_USE (use_p
, res
);
1296 update_stmt (use_stmt
);
1298 /* It is possible that we just created a duplicate close-phi
1299 for an already-processed containing loop. Check for this
1300 case and clean it up. */
1301 if (gimple_code (use_stmt
) == GIMPLE_PHI
1302 && gimple_phi_num_args (use_stmt
) == 1)
1303 make_close_phi_nodes_unique (gimple_bb (use_stmt
));
1306 remove_phi_node (gsi
, true);
1309 /* Removes all the close phi duplicates from BB. */
1312 make_close_phi_nodes_unique (basic_block bb
)
1316 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1318 gphi_iterator gsi
= psi
;
1319 gphi
*phi
= psi
.phi ();
1321 /* At this point, PHI should be a close phi in normal form. */
1322 gcc_assert (gimple_phi_num_args (phi
) == 1);
1324 /* Iterate over the next phis and remove duplicates. */
1326 while (!gsi_end_p (gsi
))
1327 if (same_close_phi_node (phi
, gsi
.phi ()))
1328 remove_duplicate_close_phi (phi
, &gsi
);
1334 /* Transforms LOOP to the canonical loop closed SSA form. */
1337 canonicalize_loop_closed_ssa (loop_p loop
)
1339 edge e
= single_exit (loop
);
1342 if (!e
|| e
->flags
& EDGE_ABNORMAL
)
1347 if (single_pred_p (bb
))
1349 e
= split_block_after_labels (bb
);
1350 make_close_phi_nodes_unique (e
->src
);
1355 basic_block close
= split_edge (e
);
1357 e
= single_succ_edge (close
);
1359 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1361 gphi
*phi
= psi
.phi ();
1364 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1365 if (gimple_phi_arg_edge (phi
, i
) == e
)
1367 tree res
, arg
= gimple_phi_arg_def (phi
, i
);
1368 use_operand_p use_p
;
1371 if (TREE_CODE (arg
) != SSA_NAME
)
1374 close_phi
= create_phi_node (NULL_TREE
, close
);
1375 res
= create_new_def_for (arg
, close_phi
,
1376 gimple_phi_result_ptr (close_phi
));
1377 add_phi_arg (close_phi
, arg
,
1378 gimple_phi_arg_edge (close_phi
, 0),
1380 use_p
= gimple_phi_arg_imm_use_ptr (phi
, i
);
1381 replace_exp (use_p
, res
);
1386 make_close_phi_nodes_unique (close
);
1389 /* The code above does not properly handle changes in the post dominance
1390 information (yet). */
1391 free_dominance_info (CDI_POST_DOMINATORS
);
1394 /* Converts the current loop closed SSA form to a canonical form
1395 expected by the Graphite code generation.
1397 The loop closed SSA form has the following invariant: a variable
1398 defined in a loop that is used outside the loop appears only in the
1399 phi nodes in the destination of the loop exit. These phi nodes are
1400 called close phi nodes.
1402 The canonical loop closed SSA form contains the extra invariants:
1404 - when the loop contains only one exit, the close phi nodes contain
1405 only one argument. That implies that the basic block that contains
1406 the close phi nodes has only one predecessor, that is a basic block
1409 - the basic block containing the close phi nodes does not contain
1412 - there exist only one phi node per definition in the loop.
1416 canonicalize_loop_closed_ssa_form (void)
1420 #ifdef ENABLE_CHECKING
1421 verify_loop_closed_ssa (true);
1424 FOR_EACH_LOOP (loop
, 0)
1425 canonicalize_loop_closed_ssa (loop
);
1427 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
1428 update_ssa (TODO_update_ssa
);
1430 #ifdef ENABLE_CHECKING
1431 verify_loop_closed_ssa (true);
1435 /* Find Static Control Parts (SCoP) in the current function and pushes
1439 build_scops (vec
<scop_p
> *scops
)
1441 struct loop
*loop
= current_loops
->tree_root
;
1442 auto_vec
<sd_region
, 3> regions
;
1444 canonicalize_loop_closed_ssa_form ();
1445 build_scops_1 (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
1446 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->loop_father
,
1448 create_sese_edges (regions
);
1449 build_graphite_scops (regions
, scops
);
1451 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1452 print_graphite_statistics (dump_file
, *scops
);
1454 limit_scops (scops
);
1457 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1458 fprintf (dump_file
, "\nnumber of SCoPs: %d\n",
1459 scops
? scops
->length () : 0);
1462 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1463 different colors. If there are not enough colors, paint the
1464 remaining SCoPs in gray.
1467 - "*" after the node number denotes the entry of a SCoP,
1468 - "#" after the node number denotes the exit of a SCoP,
1469 - "()" around the node number denotes the entry or the
1470 exit nodes of the SCOP. These are not part of SCoP. */
1473 dot_all_scops_1 (FILE *file
, vec
<scop_p
> scops
)
1482 /* Disable debugging while printing graph. */
1483 int tmp_dump_flags
= dump_flags
;
1486 fprintf (file
, "digraph all {\n");
1488 FOR_ALL_BB_FN (bb
, cfun
)
1490 int part_of_scop
= false;
1492 /* Use HTML for every bb label. So we are able to print bbs
1493 which are part of two different SCoPs, with two different
1494 background colors. */
1495 fprintf (file
, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1497 fprintf (file
, "CELLSPACING=\"0\">\n");
1499 /* Select color for SCoP. */
1500 FOR_EACH_VEC_ELT (scops
, i
, scop
)
1502 sese region
= SCOP_REGION (scop
);
1503 if (bb_in_sese_p (bb
, region
)
1504 || (SESE_EXIT_BB (region
) == bb
)
1505 || (SESE_ENTRY_BB (region
) == bb
))
1518 case 3: /* purple */
1521 case 4: /* orange */
1524 case 5: /* yellow */
1564 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color
);
1566 if (!bb_in_sese_p (bb
, region
))
1567 fprintf (file
, " (");
1569 if (bb
== SESE_ENTRY_BB (region
)
1570 && bb
== SESE_EXIT_BB (region
))
1571 fprintf (file
, " %d*# ", bb
->index
);
1572 else if (bb
== SESE_ENTRY_BB (region
))
1573 fprintf (file
, " %d* ", bb
->index
);
1574 else if (bb
== SESE_EXIT_BB (region
))
1575 fprintf (file
, " %d# ", bb
->index
);
1577 fprintf (file
, " %d ", bb
->index
);
1579 if (!bb_in_sese_p (bb
,region
))
1580 fprintf (file
, ")");
1582 fprintf (file
, "</TD></TR>\n");
1583 part_of_scop
= true;
1589 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1590 fprintf (file
, " %d </TD></TR>\n", bb
->index
);
1592 fprintf (file
, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1595 FOR_ALL_BB_FN (bb
, cfun
)
1597 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1598 fprintf (file
, "%d -> %d;\n", bb
->index
, e
->dest
->index
);
1601 fputs ("}\n\n", file
);
1603 /* Enable debugging again. */
1604 dump_flags
= tmp_dump_flags
;
1607 /* Display all SCoPs using dotty. */
1610 dot_all_scops (vec
<scop_p
> scops
)
1612 /* When debugging, enable the following code. This cannot be used
1613 in production compilers because it calls "system". */
1616 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
1617 gcc_assert (stream
);
1619 dot_all_scops_1 (stream
, scops
);
1622 x
= system ("dotty /tmp/allscops.dot &");
1624 dot_all_scops_1 (stderr
, scops
);
1628 /* Display all SCoPs using dotty. */
1631 dot_scop (scop_p scop
)
1633 auto_vec
<scop_p
, 1> scops
;
1636 scops
.safe_push (scop
);
1638 /* When debugging, enable the following code. This cannot be used
1639 in production compilers because it calls "system". */
1643 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
1644 gcc_assert (stream
);
1646 dot_all_scops_1 (stream
, scops
);
1648 x
= system ("dotty /tmp/allscops.dot &");
1651 dot_all_scops_1 (stderr
, scops
);