1 /* Detection of Static Control Parts (SCoP) for Graphite.
2 Copyright (C) 2009, 2010 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/>. */
24 #include "coretypes.h"
25 #include "tree-flow.h"
27 #include "tree-chrec.h"
28 #include "tree-data-ref.h"
29 #include "tree-scalar-evolution.h"
30 #include "tree-pass.h"
35 #include "graphite-ppl.h"
36 #include "graphite-poly.h"
37 #include "graphite-scop-detection.h"
39 /* Forward declarations. */
40 static void make_close_phi_nodes_unique (basic_block
);
42 /* The type of the analyzed basic block. */
44 typedef enum gbb_type
{
46 GBB_LOOP_SING_EXIT_HEADER
,
47 GBB_LOOP_MULT_EXIT_HEADER
,
54 /* Detect the type of BB. Loop headers are only marked, if they are
55 new. This means their loop_father is different to LAST_LOOP.
56 Otherwise they are treated like any other bb and their type can be
60 get_bb_type (basic_block bb
, struct loop
*last_loop
)
62 VEC (basic_block
, heap
) *dom
;
64 struct loop
*loop
= bb
->loop_father
;
66 /* Check, if we entry into a new loop. */
67 if (loop
!= last_loop
)
69 if (single_exit (loop
) != NULL
)
70 return GBB_LOOP_SING_EXIT_HEADER
;
71 else if (loop
->num
!= 0)
72 return GBB_LOOP_MULT_EXIT_HEADER
;
74 return GBB_COND_HEADER
;
77 dom
= get_dominated_by (CDI_DOMINATORS
, bb
);
78 nb_dom
= VEC_length (basic_block
, dom
);
79 VEC_free (basic_block
, heap
, dom
);
84 nb_suc
= VEC_length (edge
, bb
->succs
);
86 if (nb_dom
== 1 && nb_suc
== 1)
89 return GBB_COND_HEADER
;
92 /* A SCoP detection region, defined using bbs as borders.
94 All control flow touching this region, comes in passing basic_block
95 ENTRY and leaves passing basic_block EXIT. By using bbs instead of
96 edges for the borders we are able to represent also regions that do
97 not have a single entry or exit edge.
99 But as they have a single entry basic_block and a single exit
100 basic_block, we are able to generate for every sd_region a single
108 / \ This region contains: {3, 4, 5, 6, 7, 8}
116 typedef struct sd_region_p
118 /* The entry bb dominates all bbs in the sd_region. It is part of
122 /* The exit bb postdominates all bbs in the sd_region, but is not
123 part of the region. */
127 DEF_VEC_O(sd_region
);
128 DEF_VEC_ALLOC_O(sd_region
, heap
);
131 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
134 move_sd_regions (VEC (sd_region
, heap
) **source
,
135 VEC (sd_region
, heap
) **target
)
140 FOR_EACH_VEC_ELT (sd_region
, *source
, i
, s
)
141 VEC_safe_push (sd_region
, heap
, *target
, s
);
143 VEC_free (sd_region
, heap
, *source
);
146 /* Something like "n * m" is not allowed. */
149 graphite_can_represent_init (tree e
)
151 switch (TREE_CODE (e
))
153 case POLYNOMIAL_CHREC
:
154 return graphite_can_represent_init (CHREC_LEFT (e
))
155 && graphite_can_represent_init (CHREC_RIGHT (e
));
158 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
159 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
160 && host_integerp (TREE_OPERAND (e
, 1), 0);
162 return graphite_can_represent_init (TREE_OPERAND (e
, 1))
163 && host_integerp (TREE_OPERAND (e
, 0), 0);
166 case POINTER_PLUS_EXPR
:
168 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
169 && graphite_can_represent_init (TREE_OPERAND (e
, 1));
174 case NON_LVALUE_EXPR
:
175 return graphite_can_represent_init (TREE_OPERAND (e
, 0));
184 /* Return true when SCEV can be represented in the polyhedral model.
186 An expression can be represented, if it can be expressed as an
187 affine expression. For loops (i, j) and parameters (m, n) all
188 affine expressions are of the form:
190 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
192 1 i + 20 j + (-2) m + 25
194 Something like "i * n" or "n * m" is not allowed. */
197 graphite_can_represent_scev (tree scev
)
199 if (chrec_contains_undetermined (scev
))
202 switch (TREE_CODE (scev
))
206 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
207 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
210 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 0)))
211 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 1)))
212 && !(chrec_contains_symbols (TREE_OPERAND (scev
, 0))
213 && chrec_contains_symbols (TREE_OPERAND (scev
, 1)))
214 && graphite_can_represent_init (scev
)
215 && graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
216 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
218 case POLYNOMIAL_CHREC
:
219 /* Check for constant strides. With a non constant stride of
220 'n' we would have a value of 'iv * n'. Also check that the
221 initial value can represented: for example 'n * m' cannot be
223 if (!evolution_function_right_is_integer_cst (scev
)
224 || !graphite_can_represent_init (scev
))
231 /* Only affine functions can be represented. */
232 if (!scev_is_linear_expression (scev
))
239 /* Return true when EXPR can be represented in the polyhedral model.
241 This means an expression can be represented, if it is linear with
242 respect to the loops and the strides are non parametric.
243 LOOP is the place where the expr will be evaluated. SCOP_ENTRY defines the
244 entry of the region we analyse. */
247 graphite_can_represent_expr (basic_block scop_entry
, loop_p loop
,
250 tree scev
= analyze_scalar_evolution (loop
, expr
);
252 scev
= instantiate_scev (scop_entry
, loop
, scev
);
254 return graphite_can_represent_scev (scev
);
257 /* Return true if the data references of STMT can be represented by
261 stmt_has_simple_data_refs_p (loop_p outermost_loop
, gimple stmt
)
267 VEC (data_reference_p
, heap
) *drs
= VEC_alloc (data_reference_p
, heap
, 5);
269 graphite_find_data_references_in_stmt (outermost_loop
,
270 loop_containing_stmt (stmt
),
273 FOR_EACH_VEC_ELT (data_reference_p
, drs
, j
, dr
)
274 for (i
= 0; i
< DR_NUM_DIMENSIONS (dr
); i
++)
275 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr
, i
)))
282 free_data_refs (drs
);
286 /* Return true only when STMT is simple enough for being handled by
287 Graphite. This depends on SCOP_ENTRY, as the parameters are
288 initialized relatively to this basic block, the linear functions
289 are initialized to OUTERMOST_LOOP and BB is the place where we try
290 to evaluate the STMT. */
293 stmt_simple_for_scop_p (basic_block scop_entry
, loop_p outermost_loop
,
294 gimple stmt
, basic_block bb
)
296 loop_p loop
= bb
->loop_father
;
298 gcc_assert (scop_entry
);
300 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
301 Calls have side-effects, except those to const or pure
303 if (gimple_has_volatile_ops (stmt
)
304 || (gimple_code (stmt
) == GIMPLE_CALL
305 && !(gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
)))
306 || (gimple_code (stmt
) == GIMPLE_ASM
))
309 if (is_gimple_debug (stmt
))
312 if (!stmt_has_simple_data_refs_p (outermost_loop
, stmt
))
315 switch (gimple_code (stmt
))
325 enum tree_code code
= gimple_cond_code (stmt
);
327 /* We can handle all binary comparisons. Inequalities are
328 also supported as they can be represented with union of
330 if (!(code
== LT_EXPR
338 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, op_iter
, SSA_OP_ALL_USES
)
339 if (!graphite_can_represent_expr (scop_entry
, loop
, op
)
340 /* We can not handle REAL_TYPE. Failed for pr39260. */
341 || TREE_CODE (TREE_TYPE (op
)) == REAL_TYPE
)
352 /* These nodes cut a new scope. */
359 /* Returns the statement of BB that contains a harmful operation: that
360 can be a function call with side effects, the induction variables
361 are not linear with respect to SCOP_ENTRY, etc. The current open
362 scop should end before this statement. The evaluation is limited using
363 OUTERMOST_LOOP as outermost loop that may change. */
366 harmful_stmt_in_bb (basic_block scop_entry
, loop_p outer_loop
, basic_block bb
)
368 gimple_stmt_iterator gsi
;
370 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
371 if (!stmt_simple_for_scop_p (scop_entry
, outer_loop
, gsi_stmt (gsi
), bb
))
372 return gsi_stmt (gsi
);
377 /* Return true if LOOP can be represented in the polyhedral
378 representation. This is evaluated taking SCOP_ENTRY and
379 OUTERMOST_LOOP in mind. */
382 graphite_can_represent_loop (basic_block scop_entry
, loop_p loop
)
385 struct tree_niter_desc niter_desc
;
387 /* FIXME: For the moment, graphite cannot be used on loops that
388 iterate using induction variables that wrap. */
390 return number_of_iterations_exit (loop
, single_exit (loop
), &niter_desc
, false)
391 && niter_desc
.control
.no_overflow
392 && (niter
= number_of_latch_executions (loop
))
393 && !chrec_contains_undetermined (niter
)
394 && graphite_can_represent_expr (scop_entry
, loop
, niter
);
397 /* Store information needed by scopdet_* functions. */
401 /* Exit of the open scop would stop if the current BB is harmful. */
404 /* Where the next scop would start if the current BB is harmful. */
407 /* The bb or one of its children contains open loop exits. That means
408 loop exit nodes that are not surrounded by a loop dominated by bb. */
411 /* The bb or one of its children contains only structures we can handle. */
415 static struct scopdet_info
build_scops_1 (basic_block
, loop_p
,
416 VEC (sd_region
, heap
) **, loop_p
);
418 /* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
419 to SCOPS. TYPE is the gbb_type of BB. */
421 static struct scopdet_info
422 scopdet_basic_block_info (basic_block bb
, loop_p outermost_loop
,
423 VEC (sd_region
, heap
) **scops
, gbb_type type
)
425 loop_p loop
= bb
->loop_father
;
426 struct scopdet_info result
;
429 /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps. */
430 basic_block entry_block
= ENTRY_BLOCK_PTR
;
431 stmt
= harmful_stmt_in_bb (entry_block
, outermost_loop
, bb
);
432 result
.difficult
= (stmt
!= NULL
);
439 result
.exits
= false;
441 /* Mark bbs terminating a SESE region difficult, if they start
443 if (!single_succ_p (bb
))
444 result
.difficult
= true;
446 result
.exit
= single_succ (bb
);
451 result
.next
= single_succ (bb
);
452 result
.exits
= false;
453 result
.exit
= single_succ (bb
);
456 case GBB_LOOP_SING_EXIT_HEADER
:
458 VEC (sd_region
, heap
) *regions
= VEC_alloc (sd_region
, heap
, 3);
459 struct scopdet_info sinfo
;
460 edge exit_e
= single_exit (loop
);
462 sinfo
= build_scops_1 (bb
, outermost_loop
, ®ions
, loop
);
464 if (!graphite_can_represent_loop (entry_block
, loop
))
465 result
.difficult
= true;
467 result
.difficult
|= sinfo
.difficult
;
469 /* Try again with another loop level. */
471 && loop_depth (outermost_loop
) + 1 == loop_depth (loop
))
473 outermost_loop
= loop
;
475 VEC_free (sd_region
, heap
, regions
);
476 regions
= VEC_alloc (sd_region
, heap
, 3);
478 sinfo
= scopdet_basic_block_info (bb
, outermost_loop
, scops
, type
);
481 result
.difficult
= true;
484 move_sd_regions (®ions
, scops
);
488 open_scop
.entry
= bb
;
489 open_scop
.exit
= exit_e
->dest
;
490 VEC_safe_push (sd_region
, heap
, *scops
, &open_scop
);
491 VEC_free (sd_region
, heap
, regions
);
496 result
.exit
= exit_e
->dest
;
497 result
.next
= exit_e
->dest
;
499 /* If we do not dominate result.next, remove it. It's either
500 the EXIT_BLOCK_PTR, or another bb dominates it and will
501 call the scop detection for this bb. */
502 if (!dominated_by_p (CDI_DOMINATORS
, result
.next
, bb
))
505 if (exit_e
->src
->loop_father
!= loop
)
508 result
.exits
= false;
510 if (result
.difficult
)
511 move_sd_regions (®ions
, scops
);
513 VEC_free (sd_region
, heap
, regions
);
519 case GBB_LOOP_MULT_EXIT_HEADER
:
521 /* XXX: For now we just do not join loops with multiple exits. If the
522 exits lead to the same bb it may be possible to join the loop. */
523 VEC (sd_region
, heap
) *regions
= VEC_alloc (sd_region
, heap
, 3);
524 VEC (edge
, heap
) *exits
= get_loop_exit_edges (loop
);
527 build_scops_1 (bb
, loop
, ®ions
, loop
);
529 /* Scan the code dominated by this loop. This means all bbs, that are
530 are dominated by a bb in this loop, but are not part of this loop.
533 - The loop exit destination is dominated by the exit sources.
535 TODO: We miss here the more complex cases:
536 - The exit destinations are dominated by another bb inside
538 - The loop dominates bbs, that are not exit destinations. */
539 FOR_EACH_VEC_ELT (edge
, exits
, i
, e
)
540 if (e
->src
->loop_father
== loop
541 && dominated_by_p (CDI_DOMINATORS
, e
->dest
, e
->src
))
543 if (loop_outer (outermost_loop
))
544 outermost_loop
= loop_outer (outermost_loop
);
546 /* Pass loop_outer to recognize e->dest as loop header in
548 if (e
->dest
->loop_father
->header
== e
->dest
)
549 build_scops_1 (e
->dest
, outermost_loop
, ®ions
,
550 loop_outer (e
->dest
->loop_father
));
552 build_scops_1 (e
->dest
, outermost_loop
, ®ions
,
553 e
->dest
->loop_father
);
558 result
.difficult
= true;
559 result
.exits
= false;
560 move_sd_regions (®ions
, scops
);
561 VEC_free (edge
, heap
, exits
);
564 case GBB_COND_HEADER
:
566 VEC (sd_region
, heap
) *regions
= VEC_alloc (sd_region
, heap
, 3);
567 struct scopdet_info sinfo
;
568 VEC (basic_block
, heap
) *dominated
;
571 basic_block last_exit
= NULL
;
573 result
.exits
= false;
575 /* First check the successors of BB, and check if it is
576 possible to join the different branches. */
577 FOR_EACH_VEC_ELT (edge
, bb
->succs
, i
, e
)
579 /* Ignore loop exits. They will be handled after the loop
581 if (loop_exits_to_bb_p (loop
, e
->dest
))
587 /* Do not follow edges that lead to the end of the
588 conditions block. For example, in
598 the edge from 0 => 6. Only check if all paths lead to
601 if (!single_pred_p (e
->dest
))
603 /* Check, if edge leads directly to the end of this
608 if (e
->dest
!= last_exit
)
609 result
.difficult
= true;
614 if (!dominated_by_p (CDI_DOMINATORS
, e
->dest
, bb
))
616 result
.difficult
= true;
620 sinfo
= build_scops_1 (e
->dest
, outermost_loop
, ®ions
, loop
);
622 result
.exits
|= sinfo
.exits
;
623 result
.difficult
|= sinfo
.difficult
;
625 /* Checks, if all branches end at the same point.
626 If that is true, the condition stays joinable.
627 Have a look at the example above. */
631 last_exit
= sinfo
.exit
;
633 if (sinfo
.exit
!= last_exit
)
634 result
.difficult
= true;
637 result
.difficult
= true;
641 result
.difficult
= true;
643 /* Join the branches of the condition if possible. */
644 if (!result
.exits
&& !result
.difficult
)
646 /* Only return a next pointer if we dominate this pointer.
647 Otherwise it will be handled by the bb dominating it. */
648 if (dominated_by_p (CDI_DOMINATORS
, last_exit
, bb
)
650 result
.next
= last_exit
;
654 result
.exit
= last_exit
;
656 VEC_free (sd_region
, heap
, regions
);
660 /* Scan remaining bbs dominated by BB. */
661 dominated
= get_dominated_by (CDI_DOMINATORS
, bb
);
663 FOR_EACH_VEC_ELT (basic_block
, dominated
, i
, dom_bb
)
665 /* Ignore loop exits: they will be handled after the loop body. */
666 if (loop_depth (find_common_loop (loop
, dom_bb
->loop_father
))
673 /* Ignore the bbs processed above. */
674 if (single_pred_p (dom_bb
) && single_pred (dom_bb
) == bb
)
677 if (loop_depth (loop
) > loop_depth (dom_bb
->loop_father
))
678 sinfo
= build_scops_1 (dom_bb
, outermost_loop
, ®ions
,
681 sinfo
= build_scops_1 (dom_bb
, outermost_loop
, ®ions
, loop
);
683 result
.exits
|= sinfo
.exits
;
684 result
.difficult
= true;
688 VEC_free (basic_block
, heap
, dominated
);
691 move_sd_regions (®ions
, scops
);
703 /* Starting from CURRENT we walk the dominance tree and add new sd_regions to
704 SCOPS. The analyse if a sd_region can be handled is based on the value
705 of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change. LOOP
706 is the loop in which CURRENT is handled.
708 TODO: These functions got a little bit big. They definitely should be cleaned
711 static struct scopdet_info
712 build_scops_1 (basic_block current
, loop_p outermost_loop
,
713 VEC (sd_region
, heap
) **scops
, loop_p loop
)
715 bool in_scop
= false;
717 struct scopdet_info sinfo
;
719 /* Initialize result. */
720 struct scopdet_info result
;
721 result
.exits
= false;
722 result
.difficult
= false;
725 open_scop
.entry
= NULL
;
726 open_scop
.exit
= NULL
;
729 /* Loop over the dominance tree. If we meet a difficult bb, close
730 the current SCoP. Loop and condition header start a new layer,
731 and can only be added if all bbs in deeper layers are simple. */
732 while (current
!= NULL
)
734 sinfo
= scopdet_basic_block_info (current
, outermost_loop
, scops
,
735 get_bb_type (current
, loop
));
737 if (!in_scop
&& !(sinfo
.exits
|| sinfo
.difficult
))
739 open_scop
.entry
= current
;
740 open_scop
.exit
= NULL
;
743 else if (in_scop
&& (sinfo
.exits
|| sinfo
.difficult
))
745 open_scop
.exit
= current
;
746 VEC_safe_push (sd_region
, heap
, *scops
, &open_scop
);
750 result
.difficult
|= sinfo
.difficult
;
751 result
.exits
|= sinfo
.exits
;
753 current
= sinfo
.next
;
756 /* Try to close open_scop, if we are still in an open SCoP. */
759 open_scop
.exit
= sinfo
.exit
;
760 gcc_assert (open_scop
.exit
);
761 VEC_safe_push (sd_region
, heap
, *scops
, &open_scop
);
764 result
.exit
= sinfo
.exit
;
768 /* Checks if a bb is contained in REGION. */
771 bb_in_sd_region (basic_block bb
, sd_region
*region
)
773 return bb_in_region (bb
, region
->entry
, region
->exit
);
776 /* Returns the single entry edge of REGION, if it does not exits NULL. */
779 find_single_entry_edge (sd_region
*region
)
785 FOR_EACH_EDGE (e
, ei
, region
->entry
->preds
)
786 if (!bb_in_sd_region (e
->src
, region
))
801 /* Returns the single exit edge of REGION, if it does not exits NULL. */
804 find_single_exit_edge (sd_region
*region
)
810 FOR_EACH_EDGE (e
, ei
, region
->exit
->preds
)
811 if (bb_in_sd_region (e
->src
, region
))
826 /* Create a single entry edge for REGION. */
829 create_single_entry_edge (sd_region
*region
)
831 if (find_single_entry_edge (region
))
834 /* There are multiple predecessors for bb_3
847 There are two edges (1->3, 2->3), that point from outside into the region,
848 and another one (5->3), a loop latch, lead to bb_3.
856 | |\ (3.0 -> 3.1) = single entry edge
865 If the loop is part of the SCoP, we have to redirect the loop latches.
871 | | (3.0 -> 3.1) = entry edge
880 if (region
->entry
->loop_father
->header
!= region
->entry
881 || dominated_by_p (CDI_DOMINATORS
,
882 loop_latch_edge (region
->entry
->loop_father
)->src
,
885 edge forwarder
= split_block_after_labels (region
->entry
);
886 region
->entry
= forwarder
->dest
;
889 /* This case is never executed, as the loop headers seem always to have a
890 single edge pointing from outside into the loop. */
893 gcc_checking_assert (find_single_entry_edge (region
));
896 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
899 sd_region_without_exit (edge e
)
901 sd_region
*r
= (sd_region
*) e
->aux
;
904 return r
->exit
== NULL
;
909 /* Create a single exit edge for REGION. */
912 create_single_exit_edge (sd_region
*region
)
916 edge forwarder
= NULL
;
919 /* We create a forwarder bb (5) for all edges leaving this region
920 (3->5, 4->5). All other edges leading to the same bb, are moved
921 to a new bb (6). If these edges where part of another region (2->5)
922 we update the region->exit pointer, of this region.
924 To identify which edge belongs to which region we depend on the e->aux
925 pointer in every edge. It points to the region of the edge or to NULL,
926 if the edge is not part of any region.
928 1 2 3 4 1->5 no region, 2->5 region->exit = 5,
929 \| |/ 3->5 region->exit = NULL, 4->5 region->exit = NULL
934 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
935 | | \/ 3->5 no region, 4->5 no region,
937 \| / 5->6 region->exit = 6
940 Now there is only a single exit edge (5->6). */
943 forwarder
= make_forwarder_block (exit
, &sd_region_without_exit
, NULL
);
945 /* Unmark the edges, that are no longer exit edges. */
946 FOR_EACH_EDGE (e
, ei
, forwarder
->src
->preds
)
950 /* Mark the new exit edge. */
951 single_succ_edge (forwarder
->src
)->aux
= region
;
953 /* Update the exit bb of all regions, where exit edges lead to
955 FOR_EACH_EDGE (e
, ei
, forwarder
->dest
->preds
)
957 ((sd_region
*) e
->aux
)->exit
= forwarder
->dest
;
959 gcc_checking_assert (find_single_exit_edge (region
));
962 /* Unmark the exit edges of all REGIONS.
963 See comment in "create_single_exit_edge". */
966 unmark_exit_edges (VEC (sd_region
, heap
) *regions
)
973 FOR_EACH_VEC_ELT (sd_region
, regions
, i
, s
)
974 FOR_EACH_EDGE (e
, ei
, s
->exit
->preds
)
979 /* Mark the exit edges of all REGIONS.
980 See comment in "create_single_exit_edge". */
983 mark_exit_edges (VEC (sd_region
, heap
) *regions
)
990 FOR_EACH_VEC_ELT (sd_region
, regions
, i
, s
)
991 FOR_EACH_EDGE (e
, ei
, s
->exit
->preds
)
992 if (bb_in_sd_region (e
->src
, s
))
996 /* Create for all scop regions a single entry and a single exit edge. */
999 create_sese_edges (VEC (sd_region
, heap
) *regions
)
1004 FOR_EACH_VEC_ELT (sd_region
, regions
, i
, s
)
1005 create_single_entry_edge (s
);
1007 mark_exit_edges (regions
);
1009 FOR_EACH_VEC_ELT (sd_region
, regions
, i
, s
)
1010 /* Don't handle multiple edges exiting the function. */
1011 if (!find_single_exit_edge (s
)
1012 && s
->exit
!= EXIT_BLOCK_PTR
)
1013 create_single_exit_edge (s
);
1015 unmark_exit_edges (regions
);
1017 fix_loop_structure (NULL
);
1019 #ifdef ENABLE_CHECKING
1020 verify_loop_structure ();
1021 verify_dominators (CDI_DOMINATORS
);
1026 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1029 build_graphite_scops (VEC (sd_region
, heap
) *regions
,
1030 VEC (scop_p
, heap
) **scops
)
1035 FOR_EACH_VEC_ELT (sd_region
, regions
, i
, s
)
1037 edge entry
= find_single_entry_edge (s
);
1038 edge exit
= find_single_exit_edge (s
);
1044 scop
= new_scop (new_sese (entry
, exit
));
1045 VEC_safe_push (scop_p
, heap
, *scops
, scop
);
1047 /* Are there overlapping SCoPs? */
1048 #ifdef ENABLE_CHECKING
1053 FOR_EACH_VEC_ELT (sd_region
, regions
, j
, s2
)
1055 gcc_assert (!bb_in_sd_region (s
->entry
, s2
));
1061 /* Returns true when BB contains only close phi nodes. */
1064 contains_only_close_phi_nodes (basic_block bb
)
1066 gimple_stmt_iterator gsi
;
1068 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1069 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_LABEL
)
1075 /* Print statistics for SCOP to FILE. */
1078 print_graphite_scop_statistics (FILE* file
, scop_p scop
)
1083 long n_conditions
= 0;
1087 long n_p_conditions
= 0;
1093 gimple_stmt_iterator psi
;
1094 loop_p loop
= bb
->loop_father
;
1096 if (!bb_in_sese_p (bb
, SCOP_REGION (scop
)))
1100 n_p_bbs
+= bb
->count
;
1102 if (VEC_length (edge
, bb
->succs
) > 1)
1105 n_p_conditions
+= bb
->count
;
1108 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1111 n_p_stmts
+= bb
->count
;
1114 if (loop
->header
== bb
&& loop_in_sese_p (loop
, SCOP_REGION (scop
)))
1117 n_p_loops
+= bb
->count
;
1122 fprintf (file
, "\nBefore limit_scops SCoP statistics (");
1123 fprintf (file
, "BBS:%ld, ", n_bbs
);
1124 fprintf (file
, "LOOPS:%ld, ", n_loops
);
1125 fprintf (file
, "CONDITIONS:%ld, ", n_conditions
);
1126 fprintf (file
, "STMTS:%ld)\n", n_stmts
);
1127 fprintf (file
, "\nBefore limit_scops SCoP profiling statistics (");
1128 fprintf (file
, "BBS:%ld, ", n_p_bbs
);
1129 fprintf (file
, "LOOPS:%ld, ", n_p_loops
);
1130 fprintf (file
, "CONDITIONS:%ld, ", n_p_conditions
);
1131 fprintf (file
, "STMTS:%ld)\n", n_p_stmts
);
1134 /* Print statistics for SCOPS to FILE. */
1137 print_graphite_statistics (FILE* file
, VEC (scop_p
, heap
) *scops
)
1142 FOR_EACH_VEC_ELT (scop_p
, scops
, i
, scop
)
1143 print_graphite_scop_statistics (file
, scop
);
1146 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
1156 * SCoP frontier, as this line is not surrounded by any loop. *
1160 This is necessary as scalar evolution and parameter detection need a
1161 outermost loop to initialize parameters correctly.
1163 TODO: FIX scalar evolution and parameter detection to allow more flexible
1167 limit_scops (VEC (scop_p
, heap
) **scops
)
1169 VEC (sd_region
, heap
) *regions
= VEC_alloc (sd_region
, heap
, 3);
1174 FOR_EACH_VEC_ELT (scop_p
, *scops
, i
, scop
)
1178 sese region
= SCOP_REGION (scop
);
1179 build_sese_loop_nests (region
);
1181 FOR_EACH_VEC_ELT (loop_p
, SESE_LOOP_NEST (region
), j
, loop
)
1182 if (!loop_in_sese_p (loop_outer (loop
), region
)
1183 && single_exit (loop
))
1185 sd_region open_scop
;
1186 open_scop
.entry
= loop
->header
;
1187 open_scop
.exit
= single_exit (loop
)->dest
;
1189 /* This is a hack on top of the limit_scops hack. The
1190 limit_scops hack should disappear all together. */
1191 if (single_succ_p (open_scop
.exit
)
1192 && contains_only_close_phi_nodes (open_scop
.exit
))
1193 open_scop
.exit
= single_succ_edge (open_scop
.exit
)->dest
;
1195 VEC_safe_push (sd_region
, heap
, regions
, &open_scop
);
1199 free_scops (*scops
);
1200 *scops
= VEC_alloc (scop_p
, heap
, 3);
1202 create_sese_edges (regions
);
1203 build_graphite_scops (regions
, scops
);
1204 VEC_free (sd_region
, heap
, regions
);
1207 /* Returns true when P1 and P2 are close phis with the same
1211 same_close_phi_node (gimple p1
, gimple p2
)
1213 return operand_equal_p (gimple_phi_arg_def (p1
, 0),
1214 gimple_phi_arg_def (p2
, 0), 0);
1217 /* Remove the close phi node at GSI and replace its rhs with the rhs
1221 remove_duplicate_close_phi (gimple phi
, gimple_stmt_iterator
*gsi
)
1224 use_operand_p use_p
;
1225 imm_use_iterator imm_iter
;
1226 tree res
= gimple_phi_result (phi
);
1227 tree def
= gimple_phi_result (gsi_stmt (*gsi
));
1229 gcc_assert (same_close_phi_node (phi
, gsi_stmt (*gsi
)));
1231 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
1233 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
1234 SET_USE (use_p
, res
);
1236 update_stmt (use_stmt
);
1238 /* It is possible that we just created a duplicate close-phi
1239 for an already-processed containing loop. Check for this
1240 case and clean it up. */
1241 if (gimple_code (use_stmt
) == GIMPLE_PHI
1242 && gimple_phi_num_args (use_stmt
) == 1)
1243 make_close_phi_nodes_unique (gimple_bb (use_stmt
));
1246 remove_phi_node (gsi
, true);
1249 /* Removes all the close phi duplicates from BB. */
1252 make_close_phi_nodes_unique (basic_block bb
)
1254 gimple_stmt_iterator psi
;
1256 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1258 gimple_stmt_iterator gsi
= psi
;
1259 gimple phi
= gsi_stmt (psi
);
1261 /* At this point, PHI should be a close phi in normal form. */
1262 gcc_assert (gimple_phi_num_args (phi
) == 1);
1264 /* Iterate over the next phis and remove duplicates. */
1266 while (!gsi_end_p (gsi
))
1267 if (same_close_phi_node (phi
, gsi_stmt (gsi
)))
1268 remove_duplicate_close_phi (phi
, &gsi
);
1274 /* Transforms LOOP to the canonical loop closed SSA form. */
1277 canonicalize_loop_closed_ssa (loop_p loop
)
1279 edge e
= single_exit (loop
);
1282 if (!e
|| e
->flags
& EDGE_ABNORMAL
)
1287 if (VEC_length (edge
, bb
->preds
) == 1)
1289 e
= split_block_after_labels (bb
);
1290 make_close_phi_nodes_unique (e
->src
);
1294 gimple_stmt_iterator psi
;
1295 basic_block close
= split_edge (e
);
1297 e
= single_succ_edge (close
);
1299 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1301 gimple phi
= gsi_stmt (psi
);
1304 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1305 if (gimple_phi_arg_edge (phi
, i
) == e
)
1307 tree res
, arg
= gimple_phi_arg_def (phi
, i
);
1308 use_operand_p use_p
;
1311 if (TREE_CODE (arg
) != SSA_NAME
)
1314 close_phi
= create_phi_node (arg
, close
);
1315 res
= create_new_def_for (gimple_phi_result (close_phi
),
1317 gimple_phi_result_ptr (close_phi
));
1318 add_phi_arg (close_phi
, arg
,
1319 gimple_phi_arg_edge (close_phi
, 0),
1321 use_p
= gimple_phi_arg_imm_use_ptr (phi
, i
);
1322 replace_exp (use_p
, res
);
1327 make_close_phi_nodes_unique (close
);
1330 /* The code above does not properly handle changes in the post dominance
1331 information (yet). */
1332 free_dominance_info (CDI_POST_DOMINATORS
);
1335 /* Converts the current loop closed SSA form to a canonical form
1336 expected by the Graphite code generation.
1338 The loop closed SSA form has the following invariant: a variable
1339 defined in a loop that is used outside the loop appears only in the
1340 phi nodes in the destination of the loop exit. These phi nodes are
1341 called close phi nodes.
1343 The canonical loop closed SSA form contains the extra invariants:
1345 - when the loop contains only one exit, the close phi nodes contain
1346 only one argument. That implies that the basic block that contains
1347 the close phi nodes has only one predecessor, that is a basic block
1350 - the basic block containing the close phi nodes does not contain
1353 - there exist only one phi node per definition in the loop.
1357 canonicalize_loop_closed_ssa_form (void)
1362 #ifdef ENABLE_CHECKING
1363 verify_loop_closed_ssa (true);
1366 FOR_EACH_LOOP (li
, loop
, 0)
1367 canonicalize_loop_closed_ssa (loop
);
1369 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
1370 update_ssa (TODO_update_ssa
);
1372 #ifdef ENABLE_CHECKING
1373 verify_loop_closed_ssa (true);
1377 /* Find Static Control Parts (SCoP) in the current function and pushes
1381 build_scops (VEC (scop_p
, heap
) **scops
)
1383 struct loop
*loop
= current_loops
->tree_root
;
1384 VEC (sd_region
, heap
) *regions
= VEC_alloc (sd_region
, heap
, 3);
1386 canonicalize_loop_closed_ssa_form ();
1387 build_scops_1 (single_succ (ENTRY_BLOCK_PTR
), ENTRY_BLOCK_PTR
->loop_father
,
1389 create_sese_edges (regions
);
1390 build_graphite_scops (regions
, scops
);
1392 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1393 print_graphite_statistics (dump_file
, *scops
);
1395 limit_scops (scops
);
1396 VEC_free (sd_region
, heap
, regions
);
1398 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1399 fprintf (dump_file
, "\nnumber of SCoPs: %d\n",
1400 VEC_length (scop_p
, *scops
));
1403 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1404 different colors. If there are not enough colors, paint the
1405 remaining SCoPs in gray.
1408 - "*" after the node number denotes the entry of a SCoP,
1409 - "#" after the node number denotes the exit of a SCoP,
1410 - "()" around the node number denotes the entry or the
1411 exit nodes of the SCOP. These are not part of SCoP. */
1414 dot_all_scops_1 (FILE *file
, VEC (scop_p
, heap
) *scops
)
1423 /* Disable debugging while printing graph. */
1424 int tmp_dump_flags
= dump_flags
;
1427 fprintf (file
, "digraph all {\n");
1431 int part_of_scop
= false;
1433 /* Use HTML for every bb label. So we are able to print bbs
1434 which are part of two different SCoPs, with two different
1435 background colors. */
1436 fprintf (file
, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1438 fprintf (file
, "CELLSPACING=\"0\">\n");
1440 /* Select color for SCoP. */
1441 FOR_EACH_VEC_ELT (scop_p
, scops
, i
, scop
)
1443 sese region
= SCOP_REGION (scop
);
1444 if (bb_in_sese_p (bb
, region
)
1445 || (SESE_EXIT_BB (region
) == bb
)
1446 || (SESE_ENTRY_BB (region
) == bb
))
1459 case 3: /* purple */
1462 case 4: /* orange */
1465 case 5: /* yellow */
1505 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color
);
1507 if (!bb_in_sese_p (bb
, region
))
1508 fprintf (file
, " (");
1510 if (bb
== SESE_ENTRY_BB (region
)
1511 && bb
== SESE_EXIT_BB (region
))
1512 fprintf (file
, " %d*# ", bb
->index
);
1513 else if (bb
== SESE_ENTRY_BB (region
))
1514 fprintf (file
, " %d* ", bb
->index
);
1515 else if (bb
== SESE_EXIT_BB (region
))
1516 fprintf (file
, " %d# ", bb
->index
);
1518 fprintf (file
, " %d ", bb
->index
);
1520 if (!bb_in_sese_p (bb
,region
))
1521 fprintf (file
, ")");
1523 fprintf (file
, "</TD></TR>\n");
1524 part_of_scop
= true;
1530 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1531 fprintf (file
, " %d </TD></TR>\n", bb
->index
);
1533 fprintf (file
, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1538 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1539 fprintf (file
, "%d -> %d;\n", bb
->index
, e
->dest
->index
);
1542 fputs ("}\n\n", file
);
1544 /* Enable debugging again. */
1545 dump_flags
= tmp_dump_flags
;
1548 /* Display all SCoPs using dotty. */
1551 dot_all_scops (VEC (scop_p
, heap
) *scops
)
1553 /* When debugging, enable the following code. This cannot be used
1554 in production compilers because it calls "system". */
1557 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
1558 gcc_assert (stream
);
1560 dot_all_scops_1 (stream
, scops
);
1563 x
= system ("dotty /tmp/allscops.dot &");
1565 dot_all_scops_1 (stderr
, scops
);
1569 /* Display all SCoPs using dotty. */
1572 dot_scop (scop_p scop
)
1574 VEC (scop_p
, heap
) *scops
= NULL
;
1577 VEC_safe_push (scop_p
, heap
, scops
, scop
);
1579 /* When debugging, enable the following code. This cannot be used
1580 in production compilers because it calls "system". */
1584 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
1585 gcc_assert (stream
);
1587 dot_all_scops_1 (stream
, scops
);
1589 x
= system ("dotty /tmp/allscops.dot &");
1592 dot_all_scops_1 (stderr
, scops
);
1595 VEC_free (scop_p
, heap
, scops
);