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/>. */
25 /* Workaround for GMP 5.1.3 bug, see PR56019. */
30 #include <isl/union_map.h>
33 #include "coretypes.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"
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
{
61 GBB_LOOP_SING_EXIT_HEADER
,
62 GBB_LOOP_MULT_EXIT_HEADER
,
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
75 get_bb_type (basic_block bb
, struct loop
*last_loop
)
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
;
89 return GBB_COND_HEADER
;
92 dom
= get_dominated_by (CDI_DOMINATORS
, bb
);
93 nb_dom
= dom
.length ();
99 if (nb_dom
== 1 && single_succ_p (bb
))
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
121 / \ This region contains: {3, 4, 5, 6, 7, 8}
129 typedef struct sd_region_p
131 /* The entry bb dominates all bbs in the sd_region. It is part of
135 /* The exit bb postdominates all bbs in the sd_region, but is not
136 part of the region. */
142 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
145 move_sd_regions (vec
<sd_region
> *source
, vec
<sd_region
> *target
)
150 FOR_EACH_VEC_ELT (*source
, i
, s
)
151 target
->safe_push (*s
);
156 /* Something like "n * m" is not allowed. */
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
));
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));
172 return graphite_can_represent_init (TREE_OPERAND (e
, 1))
173 && tree_fits_shwi_p (TREE_OPERAND (e
, 0));
176 case POINTER_PLUS_EXPR
:
178 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
179 && graphite_can_represent_init (TREE_OPERAND (e
, 1));
184 case NON_LVALUE_EXPR
:
185 return graphite_can_represent_init (TREE_OPERAND (e
, 0));
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. */
207 graphite_can_represent_scev (tree scev
)
209 if (chrec_contains_undetermined (scev
))
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
)
220 switch (TREE_CODE (scev
))
225 case NON_LVALUE_EXPR
:
226 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0));
229 case POINTER_PLUS_EXPR
:
231 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
232 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
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
248 if (!evolution_function_right_is_integer_cst (scev
)
249 || !graphite_can_represent_init (scev
))
251 return graphite_can_represent_scev (CHREC_LEFT (scev
));
257 /* Only affine functions can be represented. */
258 if (tree_contains_chrecs (scev
, NULL
)
259 || !scev_is_linear_expression (scev
))
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. */
274 graphite_can_represent_expr (basic_block scop_entry
, loop_p loop
,
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
288 stmt_has_simple_data_refs_p (loop_p outermost_loop ATTRIBUTE_UNUSED
,
294 vec
<data_reference_p
> drs
= vNULL
;
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
),
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
);
319 ref
= TREE_OPERAND (ref
, 0);
323 free_data_refs (drs
);
327 free_data_refs (drs
);
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. */
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
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
))
354 if (is_gimple_debug (stmt
))
357 if (!stmt_has_simple_data_refs_p (outermost_loop
, stmt
))
360 switch (gimple_code (stmt
))
367 /* We can handle all binary comparisons. Inequalities are
368 also supported as they can be represented with union of
370 enum tree_code code
= gimple_cond_code (stmt
);
371 if (!(code
== LT_EXPR
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
)
396 /* These nodes cut a new scope. */
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. */
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
);
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. */
426 graphite_can_represent_loop (basic_block scop_entry
, loop_p loop
)
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. */
445 /* Exit of the open scop would stop if the current BB is harmful. */
448 /* Where the next scop would start if the current BB is harmful. */
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. */
455 /* The bb or one of its children contains only structures we can handle. */
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
;
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
);
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;
492 result
.exit
= single_succ (bb
);
497 result
.next
= single_succ (bb
);
498 result
.exits
= false;
499 result
.exit
= single_succ (bb
);
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
, ®ions
, 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. */
517 && loop_depth (outermost_loop
) + 1 == loop_depth (loop
))
519 outermost_loop
= loop
;
524 sinfo
= scopdet_basic_block_info (bb
, outermost_loop
, scops
, type
);
527 result
.difficult
= true;
530 move_sd_regions (®ions
, scops
);
534 open_scop
.entry
= bb
;
535 open_scop
.exit
= exit_e
->dest
;
536 scops
->safe_push (open_scop
);
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
))
551 if (exit_e
->src
->loop_father
!= loop
)
554 result
.exits
= false;
556 if (result
.difficult
)
557 move_sd_regions (®ions
, scops
);
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
);
573 build_scops_1 (bb
, loop
, ®ions
, 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.
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
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
594 if (e
->dest
->loop_father
->header
== e
->dest
)
595 build_scops_1 (e
->dest
, outermost_loop
, ®ions
,
596 loop_outer (e
->dest
->loop_father
));
598 build_scops_1 (e
->dest
, outermost_loop
, ®ions
,
599 e
->dest
->loop_father
);
604 result
.difficult
= true;
605 result
.exits
= false;
606 move_sd_regions (®ions
, scops
);
610 case GBB_COND_HEADER
:
612 auto_vec
<sd_region
, 3> regions
;
613 struct scopdet_info sinfo
;
614 vec
<basic_block
> dominated
;
617 basic_block last_exit
= NULL
;
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
627 if (loop_exits_to_bb_p (loop
, e
->dest
))
633 /* Do not follow edges that lead to the end of the
634 conditions block. For example, in
644 the edge from 0 => 6. Only check if all paths lead to
647 if (!single_pred_p (e
->dest
))
649 /* Check, if edge leads directly to the end of this
654 if (e
->dest
!= last_exit
)
655 result
.difficult
= true;
660 if (!dominated_by_p (CDI_DOMINATORS
, e
->dest
, bb
))
662 result
.difficult
= true;
666 sinfo
= build_scops_1 (e
->dest
, outermost_loop
, ®ions
, 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. */
677 last_exit
= sinfo
.exit
;
679 if (sinfo
.exit
!= last_exit
)
680 result
.difficult
= true;
683 result
.difficult
= true;
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
)
696 result
.next
= last_exit
;
700 result
.exit
= last_exit
;
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
))
719 /* Ignore the bbs processed above. */
720 if (single_pred_p (dom_bb
) && single_pred (dom_bb
) == bb
)
723 if (loop_depth (loop
) > loop_depth (dom_bb
->loop_father
))
724 sinfo
= build_scops_1 (dom_bb
, outermost_loop
, ®ions
,
727 sinfo
= build_scops_1 (dom_bb
, outermost_loop
, ®ions
, loop
);
729 result
.exits
|= sinfo
.exits
;
730 result
.difficult
= true;
734 dominated
.release ();
737 move_sd_regions (®ions
, scops
);
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
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;
763 struct scopdet_info sinfo
;
765 /* Initialize result. */
766 struct scopdet_info result
;
767 result
.exits
= false;
768 result
.difficult
= false;
771 open_scop
.entry
= NULL
;
772 open_scop
.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
;
789 else if (in_scop
&& (sinfo
.exits
|| sinfo
.difficult
))
791 open_scop
.exit
= current
;
792 scops
->safe_push (open_scop
);
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. */
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
);
811 sinfo
.difficult
= true;
817 result
.exit
= sinfo
.exit
;
821 /* Checks if a bb is contained in REGION. */
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. */
832 find_single_entry_edge (sd_region
*region
)
838 FOR_EACH_EDGE (e
, ei
, region
->entry
->preds
)
839 if (!bb_in_sd_region (e
->src
, region
))
854 /* Returns the single exit edge of REGION, if it does not exits NULL. */
857 find_single_exit_edge (sd_region
*region
)
863 FOR_EACH_EDGE (e
, ei
, region
->exit
->preds
)
864 if (bb_in_sd_region (e
->src
, region
))
879 /* Create a single entry edge for REGION. */
882 create_single_entry_edge (sd_region
*region
)
884 if (find_single_entry_edge (region
))
887 /* There are multiple predecessors for bb_3
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.
909 | |\ (3.0 -> 3.1) = single entry edge
918 If the loop is part of the SCoP, we have to redirect the loop latches.
924 | | (3.0 -> 3.1) = entry edge
933 if (region
->entry
->loop_father
->header
!= region
->entry
934 || dominated_by_p (CDI_DOMINATORS
,
935 loop_latch_edge (region
->entry
->loop_father
)->src
,
938 edge forwarder
= split_block_after_labels (region
->entry
);
939 region
->entry
= forwarder
->dest
;
942 /* This case is never executed, as the loop headers seem always to have a
943 single edge pointing from outside into the loop. */
946 gcc_checking_assert (find_single_entry_edge (region
));
949 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
952 sd_region_without_exit (edge e
)
954 sd_region
*r
= (sd_region
*) e
->aux
;
957 return r
->exit
== NULL
;
962 /* Create a single exit edge for REGION. */
965 create_single_exit_edge (sd_region
*region
)
969 edge forwarder
= NULL
;
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
987 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
988 | | \/ 3->5 no region, 4->5 no region,
990 \| / 5->6 region->exit = 6
993 Now there is only a single exit edge (5->6). */
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
)
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
1008 FOR_EACH_EDGE (e
, ei
, forwarder
->dest
->preds
)
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". */
1019 unmark_exit_edges (vec
<sd_region
> regions
)
1026 FOR_EACH_VEC_ELT (regions
, i
, s
)
1027 FOR_EACH_EDGE (e
, ei
, s
->exit
->preds
)
1032 /* Mark the exit edges of all REGIONS.
1033 See comment in "create_single_exit_edge". */
1036 mark_exit_edges (vec
<sd_region
> regions
)
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
))
1049 /* Create for all scop regions a single entry and a single exit edge. */
1052 create_sese_edges (vec
<sd_region
> regions
)
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);
1079 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1082 build_graphite_scops (vec
<sd_region
> regions
,
1088 FOR_EACH_VEC_ELT (regions
, i
, s
)
1090 edge entry
= find_single_entry_edge (s
);
1091 edge exit
= find_single_exit_edge (s
);
1097 scop
= new_scop (new_sese (entry
, exit
));
1098 scops
->safe_push (scop
);
1100 /* Are there overlapping SCoPs? */
1101 #ifdef ENABLE_CHECKING
1106 FOR_EACH_VEC_ELT (regions
, j
, s2
)
1108 gcc_assert (!bb_in_sd_region (s
->entry
, s2
));
1114 /* Returns true when BB contains only close phi nodes. */
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
)
1128 /* Print statistics for SCOP to FILE. */
1131 print_graphite_scop_statistics (FILE* file
, scop_p scop
)
1136 long n_conditions
= 0;
1140 long n_p_conditions
= 0;
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
)))
1153 n_p_bbs
+= bb
->count
;
1155 if (EDGE_COUNT (bb
->succs
) > 1)
1158 n_p_conditions
+= bb
->count
;
1161 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1164 n_p_stmts
+= bb
->count
;
1167 if (loop
->header
== bb
&& loop_in_sese_p (loop
, SCOP_REGION (scop
)))
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. */
1190 print_graphite_statistics (FILE* file
, vec
<scop_p
> scops
)
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.
1209 * SCoP frontier, as this line is not surrounded by any loop. *
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
1220 limit_scops (vec
<scop_p
> *scops
)
1222 auto_vec
<sd_region
, 3> regions
;
1227 FOR_EACH_VEC_ELT (*scops
, i
, scop
)
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
);
1255 create_sese_edges (regions
);
1256 build_graphite_scops (regions
, scops
);
1259 /* Returns true when P1 and P2 are close phis with the same
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
1273 remove_duplicate_close_phi (gphi
*phi
, gphi_iterator
*gsi
)
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. */
1304 make_close_phi_nodes_unique (basic_block bb
)
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. */
1318 while (!gsi_end_p (gsi
))
1319 if (same_close_phi_node (phi
, gsi
.phi ()))
1320 remove_duplicate_close_phi (phi
, &gsi
);
1326 /* Transforms LOOP to the canonical loop closed SSA form. */
1329 canonicalize_loop_closed_ssa (loop_p loop
)
1331 edge e
= single_exit (loop
);
1334 if (!e
|| e
->flags
& EDGE_ABNORMAL
)
1339 if (single_pred_p (bb
))
1341 e
= split_block_after_labels (bb
);
1342 make_close_phi_nodes_unique (e
->src
);
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 ();
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
;
1363 if (TREE_CODE (arg
) != SSA_NAME
)
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),
1372 use_p
= gimple_phi_arg_imm_use_ptr (phi
, i
);
1373 replace_exp (use_p
, res
);
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
1401 - the basic block containing the close phi nodes does not contain
1404 - there exist only one phi node per definition in the loop.
1408 canonicalize_loop_closed_ssa_form (void)
1412 #ifdef ENABLE_CHECKING
1413 verify_loop_closed_ssa (true);
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);
1427 /* Find Static Control Parts (SCoP) in the current function and pushes
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
,
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
);
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.
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. */
1465 dot_all_scops_1 (FILE *file
, vec
<scop_p
> scops
)
1474 /* Disable debugging while printing graph. */
1475 int tmp_dump_flags
= dump_flags
;
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\" ",
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
))
1510 case 3: /* purple */
1513 case 4: /* orange */
1516 case 5: /* yellow */
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
);
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;
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. */
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". */
1608 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
1609 gcc_assert (stream
);
1611 dot_all_scops_1 (stream
, scops
);
1614 x
= system ("dotty /tmp/allscops.dot &");
1616 dot_all_scops_1 (stderr
, scops
);
1620 /* Display all SCoPs using dotty. */
1623 dot_scop (scop_p scop
)
1625 auto_vec
<scop_p
, 1> scops
;
1628 scops
.safe_push (scop
);
1630 /* When debugging, enable the following code. This cannot be used
1631 in production compilers because it calls "system". */
1635 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
1636 gcc_assert (stream
);
1638 dot_all_scops_1 (stream
, scops
);
1640 x
= system ("dotty /tmp/allscops.dot &");
1643 dot_all_scops_1 (stderr
, scops
);
1647 #endif /* HAVE_isl */