1 /* Detection of Static Control Parts (SCoP) for Graphite.
2 Copyright (C) 2009-2014 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>
29 #include <cloog/cloog.h>
30 #include <cloog/isl/domain.h>
35 #include "coretypes.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
43 #include "gimple-iterator.h"
44 #include "gimple-ssa.h"
45 #include "tree-phinodes.h"
46 #include "ssa-iterators.h"
47 #include "tree-ssa-loop-manip.h"
48 #include "tree-ssa-loop-niter.h"
49 #include "tree-ssa-loop.h"
50 #include "tree-into-ssa.h"
53 #include "tree-chrec.h"
54 #include "tree-data-ref.h"
55 #include "tree-scalar-evolution.h"
56 #include "tree-pass.h"
58 #include "tree-ssa-propagate.h"
59 #include "cp/cp-tree.h"
62 #include "graphite-poly.h"
63 #include "graphite-scop-detection.h"
65 /* Forward declarations. */
66 static void make_close_phi_nodes_unique (basic_block
);
68 /* The type of the analyzed basic block. */
70 typedef enum gbb_type
{
72 GBB_LOOP_SING_EXIT_HEADER
,
73 GBB_LOOP_MULT_EXIT_HEADER
,
80 /* Detect the type of BB. Loop headers are only marked, if they are
81 new. This means their loop_father is different to LAST_LOOP.
82 Otherwise they are treated like any other bb and their type can be
86 get_bb_type (basic_block bb
, struct loop
*last_loop
)
90 struct loop
*loop
= bb
->loop_father
;
92 /* Check, if we entry into a new loop. */
93 if (loop
!= last_loop
)
95 if (single_exit (loop
) != NULL
)
96 return GBB_LOOP_SING_EXIT_HEADER
;
97 else if (loop
->num
!= 0)
98 return GBB_LOOP_MULT_EXIT_HEADER
;
100 return GBB_COND_HEADER
;
103 dom
= get_dominated_by (CDI_DOMINATORS
, bb
);
104 nb_dom
= dom
.length ();
110 if (nb_dom
== 1 && single_succ_p (bb
))
113 return GBB_COND_HEADER
;
116 /* A SCoP detection region, defined using bbs as borders.
118 All control flow touching this region, comes in passing basic_block
119 ENTRY and leaves passing basic_block EXIT. By using bbs instead of
120 edges for the borders we are able to represent also regions that do
121 not have a single entry or exit edge.
123 But as they have a single entry basic_block and a single exit
124 basic_block, we are able to generate for every sd_region a single
132 / \ This region contains: {3, 4, 5, 6, 7, 8}
140 typedef struct sd_region_p
142 /* The entry bb dominates all bbs in the sd_region. It is part of
146 /* The exit bb postdominates all bbs in the sd_region, but is not
147 part of the region. */
153 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
156 move_sd_regions (vec
<sd_region
> *source
, vec
<sd_region
> *target
)
161 FOR_EACH_VEC_ELT (*source
, i
, s
)
162 target
->safe_push (*s
);
167 /* Something like "n * m" is not allowed. */
170 graphite_can_represent_init (tree e
)
172 switch (TREE_CODE (e
))
174 case POLYNOMIAL_CHREC
:
175 return graphite_can_represent_init (CHREC_LEFT (e
))
176 && graphite_can_represent_init (CHREC_RIGHT (e
));
179 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
180 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
181 && tree_fits_shwi_p (TREE_OPERAND (e
, 1));
183 return graphite_can_represent_init (TREE_OPERAND (e
, 1))
184 && tree_fits_shwi_p (TREE_OPERAND (e
, 0));
187 case POINTER_PLUS_EXPR
:
189 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
190 && graphite_can_represent_init (TREE_OPERAND (e
, 1));
195 case NON_LVALUE_EXPR
:
196 return graphite_can_represent_init (TREE_OPERAND (e
, 0));
205 /* Return true when SCEV can be represented in the polyhedral model.
207 An expression can be represented, if it can be expressed as an
208 affine expression. For loops (i, j) and parameters (m, n) all
209 affine expressions are of the form:
211 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
213 1 i + 20 j + (-2) m + 25
215 Something like "i * n" or "n * m" is not allowed. */
218 graphite_can_represent_scev (tree scev
)
220 if (chrec_contains_undetermined (scev
))
223 /* We disable the handling of pointer types, because it’s currently not
224 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
225 the only nodes, which are disabled in case they are pointers to object
226 types, but this can be changed. */
228 if (TYPE_PTROB_P (TREE_TYPE (scev
)) && TREE_CODE (scev
) == SSA_NAME
)
231 switch (TREE_CODE (scev
))
236 case NON_LVALUE_EXPR
:
237 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0));
240 case POINTER_PLUS_EXPR
:
242 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
243 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
246 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 0)))
247 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 1)))
248 && !(chrec_contains_symbols (TREE_OPERAND (scev
, 0))
249 && chrec_contains_symbols (TREE_OPERAND (scev
, 1)))
250 && graphite_can_represent_init (scev
)
251 && graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
252 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
254 case POLYNOMIAL_CHREC
:
255 /* Check for constant strides. With a non constant stride of
256 'n' we would have a value of 'iv * n'. Also check that the
257 initial value can represented: for example 'n * m' cannot be
259 if (!evolution_function_right_is_integer_cst (scev
)
260 || !graphite_can_represent_init (scev
))
262 return graphite_can_represent_scev (CHREC_LEFT (scev
));
268 /* Only affine functions can be represented. */
269 if (tree_contains_chrecs (scev
, NULL
)
270 || !scev_is_linear_expression (scev
))
277 /* Return true when EXPR can be represented in the polyhedral model.
279 This means an expression can be represented, if it is linear with
280 respect to the loops and the strides are non parametric.
281 LOOP is the place where the expr will be evaluated. SCOP_ENTRY defines the
282 entry of the region we analyse. */
285 graphite_can_represent_expr (basic_block scop_entry
, loop_p loop
,
288 tree scev
= analyze_scalar_evolution (loop
, expr
);
290 scev
= instantiate_scev (scop_entry
, loop
, scev
);
292 return graphite_can_represent_scev (scev
);
295 /* Return true if the data references of STMT can be represented by
299 stmt_has_simple_data_refs_p (loop_p outermost_loop ATTRIBUTE_UNUSED
,
306 vec
<data_reference_p
> drs
= vNULL
;
309 for (outer
= loop_containing_stmt (stmt
); outer
; outer
= loop_outer (outer
))
311 graphite_find_data_references_in_stmt (outer
,
312 loop_containing_stmt (stmt
),
315 FOR_EACH_VEC_ELT (drs
, j
, dr
)
316 for (i
= 0; i
< DR_NUM_DIMENSIONS (dr
); i
++)
317 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr
, i
)))
323 free_data_refs (drs
);
328 free_data_refs (drs
);
332 /* Return true only when STMT is simple enough for being handled by
333 Graphite. This depends on SCOP_ENTRY, as the parameters are
334 initialized relatively to this basic block, the linear functions
335 are initialized to OUTERMOST_LOOP and BB is the place where we try
336 to evaluate the STMT. */
339 stmt_simple_for_scop_p (basic_block scop_entry
, loop_p outermost_loop
,
340 gimple stmt
, basic_block bb
)
342 loop_p loop
= bb
->loop_father
;
344 gcc_assert (scop_entry
);
346 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
347 Calls have side-effects, except those to const or pure
349 if (gimple_has_volatile_ops (stmt
)
350 || (gimple_code (stmt
) == GIMPLE_CALL
351 && !(gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
)))
352 || (gimple_code (stmt
) == GIMPLE_ASM
))
355 if (is_gimple_debug (stmt
))
358 if (!stmt_has_simple_data_refs_p (outermost_loop
, stmt
))
361 switch (gimple_code (stmt
))
369 /* We can handle all binary comparisons. Inequalities are
370 also supported as they can be represented with union of
372 enum tree_code code
= gimple_cond_code (stmt
);
373 if (!(code
== LT_EXPR
381 for (unsigned i
= 0; i
< 2; ++i
)
383 tree op
= gimple_op (stmt
, i
);
384 if (!graphite_can_represent_expr (scop_entry
, loop
, op
)
385 /* We can not handle REAL_TYPE. Failed for pr39260. */
386 || TREE_CODE (TREE_TYPE (op
)) == REAL_TYPE
)
398 /* These nodes cut a new scope. */
405 /* Returns the statement of BB that contains a harmful operation: that
406 can be a function call with side effects, the induction variables
407 are not linear with respect to SCOP_ENTRY, etc. The current open
408 scop should end before this statement. The evaluation is limited using
409 OUTERMOST_LOOP as outermost loop that may change. */
412 harmful_stmt_in_bb (basic_block scop_entry
, loop_p outer_loop
, basic_block bb
)
414 gimple_stmt_iterator gsi
;
416 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
417 if (!stmt_simple_for_scop_p (scop_entry
, outer_loop
, gsi_stmt (gsi
), bb
))
418 return gsi_stmt (gsi
);
423 /* Return true if LOOP can be represented in the polyhedral
424 representation. This is evaluated taking SCOP_ENTRY and
425 OUTERMOST_LOOP in mind. */
428 graphite_can_represent_loop (basic_block scop_entry
, loop_p loop
)
431 struct tree_niter_desc niter_desc
;
433 /* FIXME: For the moment, graphite cannot be used on loops that
434 iterate using induction variables that wrap. */
436 return number_of_iterations_exit (loop
, single_exit (loop
), &niter_desc
, false)
437 && niter_desc
.control
.no_overflow
438 && (niter
= number_of_latch_executions (loop
))
439 && !chrec_contains_undetermined (niter
)
440 && graphite_can_represent_expr (scop_entry
, loop
, niter
);
443 /* Store information needed by scopdet_* functions. */
447 /* Exit of the open scop would stop if the current BB is harmful. */
450 /* Where the next scop would start if the current BB is harmful. */
453 /* The bb or one of its children contains open loop exits. That means
454 loop exit nodes that are not surrounded by a loop dominated by bb. */
457 /* The bb or one of its children contains only structures we can handle. */
461 static struct scopdet_info
build_scops_1 (basic_block
, loop_p
,
462 vec
<sd_region
> *, loop_p
);
464 /* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
465 to SCOPS. TYPE is the gbb_type of BB. */
467 static struct scopdet_info
468 scopdet_basic_block_info (basic_block bb
, loop_p outermost_loop
,
469 vec
<sd_region
> *scops
, gbb_type type
)
471 loop_p loop
= bb
->loop_father
;
472 struct scopdet_info result
;
475 /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps. */
476 basic_block entry_block
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
477 stmt
= harmful_stmt_in_bb (entry_block
, outermost_loop
, bb
);
478 result
.difficult
= (stmt
!= NULL
);
485 result
.exits
= false;
487 /* Mark bbs terminating a SESE region difficult, if they start
488 a condition or if the block it exits to cannot be split
489 with make_forwarder_block. */
490 if (!single_succ_p (bb
)
491 || bb_has_abnormal_pred (single_succ (bb
)))
492 result
.difficult
= true;
494 result
.exit
= single_succ (bb
);
499 result
.next
= single_succ (bb
);
500 result
.exits
= false;
501 result
.exit
= single_succ (bb
);
504 case GBB_LOOP_SING_EXIT_HEADER
:
506 auto_vec
<sd_region
, 3> regions
;
507 struct scopdet_info sinfo
;
508 edge exit_e
= single_exit (loop
);
510 sinfo
= build_scops_1 (bb
, outermost_loop
, ®ions
, loop
);
512 if (!graphite_can_represent_loop (entry_block
, loop
))
513 result
.difficult
= true;
515 result
.difficult
|= sinfo
.difficult
;
517 /* Try again with another loop level. */
519 && loop_depth (outermost_loop
) + 1 == loop_depth (loop
))
521 outermost_loop
= loop
;
526 sinfo
= scopdet_basic_block_info (bb
, outermost_loop
, scops
, type
);
529 result
.difficult
= true;
532 move_sd_regions (®ions
, scops
);
536 open_scop
.entry
= bb
;
537 open_scop
.exit
= exit_e
->dest
;
538 scops
->safe_push (open_scop
);
544 result
.exit
= exit_e
->dest
;
545 result
.next
= exit_e
->dest
;
547 /* If we do not dominate result.next, remove it. It's either
548 the exit block, or another bb dominates it and will
549 call the scop detection for this bb. */
550 if (!dominated_by_p (CDI_DOMINATORS
, result
.next
, bb
))
553 if (exit_e
->src
->loop_father
!= loop
)
556 result
.exits
= false;
558 if (result
.difficult
)
559 move_sd_regions (®ions
, scops
);
567 case GBB_LOOP_MULT_EXIT_HEADER
:
569 /* XXX: For now we just do not join loops with multiple exits. If the
570 exits lead to the same bb it may be possible to join the loop. */
571 auto_vec
<sd_region
, 3> regions
;
572 vec
<edge
> exits
= get_loop_exit_edges (loop
);
575 build_scops_1 (bb
, loop
, ®ions
, loop
);
577 /* Scan the code dominated by this loop. This means all bbs, that are
578 are dominated by a bb in this loop, but are not part of this loop.
581 - The loop exit destination is dominated by the exit sources.
583 TODO: We miss here the more complex cases:
584 - The exit destinations are dominated by another bb inside
586 - The loop dominates bbs, that are not exit destinations. */
587 FOR_EACH_VEC_ELT (exits
, i
, e
)
588 if (e
->src
->loop_father
== loop
589 && dominated_by_p (CDI_DOMINATORS
, e
->dest
, e
->src
))
591 if (loop_outer (outermost_loop
))
592 outermost_loop
= loop_outer (outermost_loop
);
594 /* Pass loop_outer to recognize e->dest as loop header in
596 if (e
->dest
->loop_father
->header
== e
->dest
)
597 build_scops_1 (e
->dest
, outermost_loop
, ®ions
,
598 loop_outer (e
->dest
->loop_father
));
600 build_scops_1 (e
->dest
, outermost_loop
, ®ions
,
601 e
->dest
->loop_father
);
606 result
.difficult
= true;
607 result
.exits
= false;
608 move_sd_regions (®ions
, scops
);
612 case GBB_COND_HEADER
:
614 auto_vec
<sd_region
, 3> regions
;
615 struct scopdet_info sinfo
;
616 vec
<basic_block
> dominated
;
619 basic_block last_exit
= NULL
;
621 result
.exits
= false;
623 /* First check the successors of BB, and check if it is
624 possible to join the different branches. */
625 FOR_EACH_VEC_SAFE_ELT (bb
->succs
, i
, e
)
627 /* Ignore loop exits. They will be handled after the loop
629 if (loop_exits_to_bb_p (loop
, e
->dest
))
635 /* Do not follow edges that lead to the end of the
636 conditions block. For example, in
646 the edge from 0 => 6. Only check if all paths lead to
649 if (!single_pred_p (e
->dest
))
651 /* Check, if edge leads directly to the end of this
656 if (e
->dest
!= last_exit
)
657 result
.difficult
= true;
662 if (!dominated_by_p (CDI_DOMINATORS
, e
->dest
, bb
))
664 result
.difficult
= true;
668 sinfo
= build_scops_1 (e
->dest
, outermost_loop
, ®ions
, loop
);
670 result
.exits
|= sinfo
.exits
;
671 result
.difficult
|= sinfo
.difficult
;
673 /* Checks, if all branches end at the same point.
674 If that is true, the condition stays joinable.
675 Have a look at the example above. */
679 last_exit
= sinfo
.exit
;
681 if (sinfo
.exit
!= last_exit
)
682 result
.difficult
= true;
685 result
.difficult
= true;
689 result
.difficult
= true;
691 /* Join the branches of the condition if possible. */
692 if (!result
.exits
&& !result
.difficult
)
694 /* Only return a next pointer if we dominate this pointer.
695 Otherwise it will be handled by the bb dominating it. */
696 if (dominated_by_p (CDI_DOMINATORS
, last_exit
, bb
)
698 result
.next
= last_exit
;
702 result
.exit
= last_exit
;
708 /* Scan remaining bbs dominated by BB. */
709 dominated
= get_dominated_by (CDI_DOMINATORS
, bb
);
711 FOR_EACH_VEC_ELT (dominated
, i
, dom_bb
)
713 /* Ignore loop exits: they will be handled after the loop body. */
714 if (loop_depth (find_common_loop (loop
, dom_bb
->loop_father
))
721 /* Ignore the bbs processed above. */
722 if (single_pred_p (dom_bb
) && single_pred (dom_bb
) == bb
)
725 if (loop_depth (loop
) > loop_depth (dom_bb
->loop_father
))
726 sinfo
= build_scops_1 (dom_bb
, outermost_loop
, ®ions
,
729 sinfo
= build_scops_1 (dom_bb
, outermost_loop
, ®ions
, loop
);
731 result
.exits
|= sinfo
.exits
;
732 result
.difficult
= true;
736 dominated
.release ();
739 move_sd_regions (®ions
, scops
);
751 /* Starting from CURRENT we walk the dominance tree and add new sd_regions to
752 SCOPS. The analyse if a sd_region can be handled is based on the value
753 of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change. LOOP
754 is the loop in which CURRENT is handled.
756 TODO: These functions got a little bit big. They definitely should be cleaned
759 static struct scopdet_info
760 build_scops_1 (basic_block current
, loop_p outermost_loop
,
761 vec
<sd_region
> *scops
, loop_p loop
)
763 bool in_scop
= false;
765 struct scopdet_info sinfo
;
767 /* Initialize result. */
768 struct scopdet_info result
;
769 result
.exits
= false;
770 result
.difficult
= false;
773 open_scop
.entry
= NULL
;
774 open_scop
.exit
= NULL
;
777 /* Loop over the dominance tree. If we meet a difficult bb, close
778 the current SCoP. Loop and condition header start a new layer,
779 and can only be added if all bbs in deeper layers are simple. */
780 while (current
!= NULL
)
782 sinfo
= scopdet_basic_block_info (current
, outermost_loop
, scops
,
783 get_bb_type (current
, loop
));
785 if (!in_scop
&& !(sinfo
.exits
|| sinfo
.difficult
))
787 open_scop
.entry
= current
;
788 open_scop
.exit
= NULL
;
791 else if (in_scop
&& (sinfo
.exits
|| sinfo
.difficult
))
793 open_scop
.exit
= current
;
794 scops
->safe_push (open_scop
);
798 result
.difficult
|= sinfo
.difficult
;
799 result
.exits
|= sinfo
.exits
;
801 current
= sinfo
.next
;
804 /* Try to close open_scop, if we are still in an open SCoP. */
807 open_scop
.exit
= sinfo
.exit
;
808 gcc_assert (open_scop
.exit
);
809 scops
->safe_push (open_scop
);
812 result
.exit
= sinfo
.exit
;
816 /* Checks if a bb is contained in REGION. */
819 bb_in_sd_region (basic_block bb
, sd_region
*region
)
821 return bb_in_region (bb
, region
->entry
, region
->exit
);
824 /* Returns the single entry edge of REGION, if it does not exits NULL. */
827 find_single_entry_edge (sd_region
*region
)
833 FOR_EACH_EDGE (e
, ei
, region
->entry
->preds
)
834 if (!bb_in_sd_region (e
->src
, region
))
849 /* Returns the single exit edge of REGION, if it does not exits NULL. */
852 find_single_exit_edge (sd_region
*region
)
858 FOR_EACH_EDGE (e
, ei
, region
->exit
->preds
)
859 if (bb_in_sd_region (e
->src
, region
))
874 /* Create a single entry edge for REGION. */
877 create_single_entry_edge (sd_region
*region
)
879 if (find_single_entry_edge (region
))
882 /* There are multiple predecessors for bb_3
895 There are two edges (1->3, 2->3), that point from outside into the region,
896 and another one (5->3), a loop latch, lead to bb_3.
904 | |\ (3.0 -> 3.1) = single entry edge
913 If the loop is part of the SCoP, we have to redirect the loop latches.
919 | | (3.0 -> 3.1) = entry edge
928 if (region
->entry
->loop_father
->header
!= region
->entry
929 || dominated_by_p (CDI_DOMINATORS
,
930 loop_latch_edge (region
->entry
->loop_father
)->src
,
933 edge forwarder
= split_block_after_labels (region
->entry
);
934 region
->entry
= forwarder
->dest
;
937 /* This case is never executed, as the loop headers seem always to have a
938 single edge pointing from outside into the loop. */
941 gcc_checking_assert (find_single_entry_edge (region
));
944 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
947 sd_region_without_exit (edge e
)
949 sd_region
*r
= (sd_region
*) e
->aux
;
952 return r
->exit
== NULL
;
957 /* Create a single exit edge for REGION. */
960 create_single_exit_edge (sd_region
*region
)
964 edge forwarder
= NULL
;
967 /* We create a forwarder bb (5) for all edges leaving this region
968 (3->5, 4->5). All other edges leading to the same bb, are moved
969 to a new bb (6). If these edges where part of another region (2->5)
970 we update the region->exit pointer, of this region.
972 To identify which edge belongs to which region we depend on the e->aux
973 pointer in every edge. It points to the region of the edge or to NULL,
974 if the edge is not part of any region.
976 1 2 3 4 1->5 no region, 2->5 region->exit = 5,
977 \| |/ 3->5 region->exit = NULL, 4->5 region->exit = NULL
982 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
983 | | \/ 3->5 no region, 4->5 no region,
985 \| / 5->6 region->exit = 6
988 Now there is only a single exit edge (5->6). */
991 forwarder
= make_forwarder_block (exit
, &sd_region_without_exit
, NULL
);
993 /* Unmark the edges, that are no longer exit edges. */
994 FOR_EACH_EDGE (e
, ei
, forwarder
->src
->preds
)
998 /* Mark the new exit edge. */
999 single_succ_edge (forwarder
->src
)->aux
= region
;
1001 /* Update the exit bb of all regions, where exit edges lead to
1003 FOR_EACH_EDGE (e
, ei
, forwarder
->dest
->preds
)
1005 ((sd_region
*) e
->aux
)->exit
= forwarder
->dest
;
1007 gcc_checking_assert (find_single_exit_edge (region
));
1010 /* Unmark the exit edges of all REGIONS.
1011 See comment in "create_single_exit_edge". */
1014 unmark_exit_edges (vec
<sd_region
> regions
)
1021 FOR_EACH_VEC_ELT (regions
, i
, s
)
1022 FOR_EACH_EDGE (e
, ei
, s
->exit
->preds
)
1027 /* Mark the exit edges of all REGIONS.
1028 See comment in "create_single_exit_edge". */
1031 mark_exit_edges (vec
<sd_region
> regions
)
1038 FOR_EACH_VEC_ELT (regions
, i
, s
)
1039 FOR_EACH_EDGE (e
, ei
, s
->exit
->preds
)
1040 if (bb_in_sd_region (e
->src
, s
))
1044 /* Create for all scop regions a single entry and a single exit edge. */
1047 create_sese_edges (vec
<sd_region
> regions
)
1052 FOR_EACH_VEC_ELT (regions
, i
, s
)
1053 create_single_entry_edge (s
);
1055 mark_exit_edges (regions
);
1057 FOR_EACH_VEC_ELT (regions
, i
, s
)
1058 /* Don't handle multiple edges exiting the function. */
1059 if (!find_single_exit_edge (s
)
1060 && s
->exit
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1061 create_single_exit_edge (s
);
1063 unmark_exit_edges (regions
);
1065 calculate_dominance_info (CDI_DOMINATORS
);
1066 fix_loop_structure (NULL
);
1068 #ifdef ENABLE_CHECKING
1069 verify_loop_structure ();
1070 verify_ssa (false, true);
1074 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1077 build_graphite_scops (vec
<sd_region
> regions
,
1083 FOR_EACH_VEC_ELT (regions
, i
, s
)
1085 edge entry
= find_single_entry_edge (s
);
1086 edge exit
= find_single_exit_edge (s
);
1092 scop
= new_scop (new_sese (entry
, exit
));
1093 scops
->safe_push (scop
);
1095 /* Are there overlapping SCoPs? */
1096 #ifdef ENABLE_CHECKING
1101 FOR_EACH_VEC_ELT (regions
, j
, s2
)
1103 gcc_assert (!bb_in_sd_region (s
->entry
, s2
));
1109 /* Returns true when BB contains only close phi nodes. */
1112 contains_only_close_phi_nodes (basic_block bb
)
1114 gimple_stmt_iterator gsi
;
1116 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1117 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_LABEL
)
1123 /* Print statistics for SCOP to FILE. */
1126 print_graphite_scop_statistics (FILE* file
, scop_p scop
)
1131 long n_conditions
= 0;
1135 long n_p_conditions
= 0;
1139 FOR_ALL_BB_FN (bb
, cfun
)
1141 gimple_stmt_iterator psi
;
1142 loop_p loop
= bb
->loop_father
;
1144 if (!bb_in_sese_p (bb
, SCOP_REGION (scop
)))
1148 n_p_bbs
+= bb
->count
;
1150 if (EDGE_COUNT (bb
->succs
) > 1)
1153 n_p_conditions
+= bb
->count
;
1156 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1159 n_p_stmts
+= bb
->count
;
1162 if (loop
->header
== bb
&& loop_in_sese_p (loop
, SCOP_REGION (scop
)))
1165 n_p_loops
+= bb
->count
;
1170 fprintf (file
, "\nBefore limit_scops SCoP statistics (");
1171 fprintf (file
, "BBS:%ld, ", n_bbs
);
1172 fprintf (file
, "LOOPS:%ld, ", n_loops
);
1173 fprintf (file
, "CONDITIONS:%ld, ", n_conditions
);
1174 fprintf (file
, "STMTS:%ld)\n", n_stmts
);
1175 fprintf (file
, "\nBefore limit_scops SCoP profiling statistics (");
1176 fprintf (file
, "BBS:%ld, ", n_p_bbs
);
1177 fprintf (file
, "LOOPS:%ld, ", n_p_loops
);
1178 fprintf (file
, "CONDITIONS:%ld, ", n_p_conditions
);
1179 fprintf (file
, "STMTS:%ld)\n", n_p_stmts
);
1182 /* Print statistics for SCOPS to FILE. */
1185 print_graphite_statistics (FILE* file
, vec
<scop_p
> scops
)
1190 FOR_EACH_VEC_ELT (scops
, i
, scop
)
1191 print_graphite_scop_statistics (file
, scop
);
1194 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
1204 * SCoP frontier, as this line is not surrounded by any loop. *
1208 This is necessary as scalar evolution and parameter detection need a
1209 outermost loop to initialize parameters correctly.
1211 TODO: FIX scalar evolution and parameter detection to allow more flexible
1215 limit_scops (vec
<scop_p
> *scops
)
1217 auto_vec
<sd_region
, 3> regions
;
1222 FOR_EACH_VEC_ELT (*scops
, i
, scop
)
1226 sese region
= SCOP_REGION (scop
);
1227 build_sese_loop_nests (region
);
1229 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region
), j
, loop
)
1230 if (!loop_in_sese_p (loop_outer (loop
), region
)
1231 && single_exit (loop
))
1233 sd_region open_scop
;
1234 open_scop
.entry
= loop
->header
;
1235 open_scop
.exit
= single_exit (loop
)->dest
;
1237 /* This is a hack on top of the limit_scops hack. The
1238 limit_scops hack should disappear all together. */
1239 if (single_succ_p (open_scop
.exit
)
1240 && contains_only_close_phi_nodes (open_scop
.exit
))
1241 open_scop
.exit
= single_succ_edge (open_scop
.exit
)->dest
;
1243 regions
.safe_push (open_scop
);
1247 free_scops (*scops
);
1250 create_sese_edges (regions
);
1251 build_graphite_scops (regions
, scops
);
1254 /* Returns true when P1 and P2 are close phis with the same
1258 same_close_phi_node (gimple p1
, gimple p2
)
1260 return operand_equal_p (gimple_phi_arg_def (p1
, 0),
1261 gimple_phi_arg_def (p2
, 0), 0);
1264 /* Remove the close phi node at GSI and replace its rhs with the rhs
1268 remove_duplicate_close_phi (gimple phi
, gimple_stmt_iterator
*gsi
)
1271 use_operand_p use_p
;
1272 imm_use_iterator imm_iter
;
1273 tree res
= gimple_phi_result (phi
);
1274 tree def
= gimple_phi_result (gsi_stmt (*gsi
));
1276 gcc_assert (same_close_phi_node (phi
, gsi_stmt (*gsi
)));
1278 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
1280 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
1281 SET_USE (use_p
, res
);
1283 update_stmt (use_stmt
);
1285 /* It is possible that we just created a duplicate close-phi
1286 for an already-processed containing loop. Check for this
1287 case and clean it up. */
1288 if (gimple_code (use_stmt
) == GIMPLE_PHI
1289 && gimple_phi_num_args (use_stmt
) == 1)
1290 make_close_phi_nodes_unique (gimple_bb (use_stmt
));
1293 remove_phi_node (gsi
, true);
1296 /* Removes all the close phi duplicates from BB. */
1299 make_close_phi_nodes_unique (basic_block bb
)
1301 gimple_stmt_iterator psi
;
1303 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1305 gimple_stmt_iterator gsi
= psi
;
1306 gimple phi
= gsi_stmt (psi
);
1308 /* At this point, PHI should be a close phi in normal form. */
1309 gcc_assert (gimple_phi_num_args (phi
) == 1);
1311 /* Iterate over the next phis and remove duplicates. */
1313 while (!gsi_end_p (gsi
))
1314 if (same_close_phi_node (phi
, gsi_stmt (gsi
)))
1315 remove_duplicate_close_phi (phi
, &gsi
);
1321 /* Transforms LOOP to the canonical loop closed SSA form. */
1324 canonicalize_loop_closed_ssa (loop_p loop
)
1326 edge e
= single_exit (loop
);
1329 if (!e
|| e
->flags
& EDGE_ABNORMAL
)
1334 if (single_pred_p (bb
))
1336 e
= split_block_after_labels (bb
);
1337 make_close_phi_nodes_unique (e
->src
);
1341 gimple_stmt_iterator psi
;
1342 basic_block close
= split_edge (e
);
1344 e
= single_succ_edge (close
);
1346 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1348 gimple phi
= gsi_stmt (psi
);
1351 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1352 if (gimple_phi_arg_edge (phi
, i
) == e
)
1354 tree res
, arg
= gimple_phi_arg_def (phi
, i
);
1355 use_operand_p use_p
;
1358 if (TREE_CODE (arg
) != SSA_NAME
)
1361 close_phi
= create_phi_node (NULL_TREE
, close
);
1362 res
= create_new_def_for (arg
, close_phi
,
1363 gimple_phi_result_ptr (close_phi
));
1364 add_phi_arg (close_phi
, arg
,
1365 gimple_phi_arg_edge (close_phi
, 0),
1367 use_p
= gimple_phi_arg_imm_use_ptr (phi
, i
);
1368 replace_exp (use_p
, res
);
1373 make_close_phi_nodes_unique (close
);
1376 /* The code above does not properly handle changes in the post dominance
1377 information (yet). */
1378 free_dominance_info (CDI_POST_DOMINATORS
);
1381 /* Converts the current loop closed SSA form to a canonical form
1382 expected by the Graphite code generation.
1384 The loop closed SSA form has the following invariant: a variable
1385 defined in a loop that is used outside the loop appears only in the
1386 phi nodes in the destination of the loop exit. These phi nodes are
1387 called close phi nodes.
1389 The canonical loop closed SSA form contains the extra invariants:
1391 - when the loop contains only one exit, the close phi nodes contain
1392 only one argument. That implies that the basic block that contains
1393 the close phi nodes has only one predecessor, that is a basic block
1396 - the basic block containing the close phi nodes does not contain
1399 - there exist only one phi node per definition in the loop.
1403 canonicalize_loop_closed_ssa_form (void)
1407 #ifdef ENABLE_CHECKING
1408 verify_loop_closed_ssa (true);
1411 FOR_EACH_LOOP (loop
, 0)
1412 canonicalize_loop_closed_ssa (loop
);
1414 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
1415 update_ssa (TODO_update_ssa
);
1417 #ifdef ENABLE_CHECKING
1418 verify_loop_closed_ssa (true);
1422 /* Find Static Control Parts (SCoP) in the current function and pushes
1426 build_scops (vec
<scop_p
> *scops
)
1428 struct loop
*loop
= current_loops
->tree_root
;
1429 auto_vec
<sd_region
, 3> regions
;
1431 canonicalize_loop_closed_ssa_form ();
1432 build_scops_1 (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
1433 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->loop_father
,
1435 create_sese_edges (regions
);
1436 build_graphite_scops (regions
, scops
);
1438 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1439 print_graphite_statistics (dump_file
, *scops
);
1441 limit_scops (scops
);
1444 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1445 fprintf (dump_file
, "\nnumber of SCoPs: %d\n",
1446 scops
? scops
->length () : 0);
1449 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1450 different colors. If there are not enough colors, paint the
1451 remaining SCoPs in gray.
1454 - "*" after the node number denotes the entry of a SCoP,
1455 - "#" after the node number denotes the exit of a SCoP,
1456 - "()" around the node number denotes the entry or the
1457 exit nodes of the SCOP. These are not part of SCoP. */
1460 dot_all_scops_1 (FILE *file
, vec
<scop_p
> scops
)
1469 /* Disable debugging while printing graph. */
1470 int tmp_dump_flags
= dump_flags
;
1473 fprintf (file
, "digraph all {\n");
1475 FOR_ALL_BB_FN (bb
, cfun
)
1477 int part_of_scop
= false;
1479 /* Use HTML for every bb label. So we are able to print bbs
1480 which are part of two different SCoPs, with two different
1481 background colors. */
1482 fprintf (file
, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1484 fprintf (file
, "CELLSPACING=\"0\">\n");
1486 /* Select color for SCoP. */
1487 FOR_EACH_VEC_ELT (scops
, i
, scop
)
1489 sese region
= SCOP_REGION (scop
);
1490 if (bb_in_sese_p (bb
, region
)
1491 || (SESE_EXIT_BB (region
) == bb
)
1492 || (SESE_ENTRY_BB (region
) == bb
))
1505 case 3: /* purple */
1508 case 4: /* orange */
1511 case 5: /* yellow */
1551 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color
);
1553 if (!bb_in_sese_p (bb
, region
))
1554 fprintf (file
, " (");
1556 if (bb
== SESE_ENTRY_BB (region
)
1557 && bb
== SESE_EXIT_BB (region
))
1558 fprintf (file
, " %d*# ", bb
->index
);
1559 else if (bb
== SESE_ENTRY_BB (region
))
1560 fprintf (file
, " %d* ", bb
->index
);
1561 else if (bb
== SESE_EXIT_BB (region
))
1562 fprintf (file
, " %d# ", bb
->index
);
1564 fprintf (file
, " %d ", bb
->index
);
1566 if (!bb_in_sese_p (bb
,region
))
1567 fprintf (file
, ")");
1569 fprintf (file
, "</TD></TR>\n");
1570 part_of_scop
= true;
1576 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1577 fprintf (file
, " %d </TD></TR>\n", bb
->index
);
1579 fprintf (file
, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1582 FOR_ALL_BB_FN (bb
, cfun
)
1584 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1585 fprintf (file
, "%d -> %d;\n", bb
->index
, e
->dest
->index
);
1588 fputs ("}\n\n", file
);
1590 /* Enable debugging again. */
1591 dump_flags
= tmp_dump_flags
;
1594 /* Display all SCoPs using dotty. */
1597 dot_all_scops (vec
<scop_p
> scops
)
1599 /* When debugging, enable the following code. This cannot be used
1600 in production compilers because it calls "system". */
1603 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
1604 gcc_assert (stream
);
1606 dot_all_scops_1 (stream
, scops
);
1609 x
= system ("dotty /tmp/allscops.dot &");
1611 dot_all_scops_1 (stderr
, scops
);
1615 /* Display all SCoPs using dotty. */
1618 dot_scop (scop_p scop
)
1620 auto_vec
<scop_p
, 1> scops
;
1623 scops
.safe_push (scop
);
1625 /* When debugging, enable the following code. This cannot be used
1626 in production compilers because it calls "system". */
1630 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
1631 gcc_assert (stream
);
1633 dot_all_scops_1 (stream
, scops
);
1635 x
= system ("dotty /tmp/allscops.dot &");
1638 dot_all_scops_1 (stderr
, scops
);