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. */
28 #include <isl/constraint.h>
31 #include <isl/union_map.h>
34 #include "coretypes.h"
40 #include "fold-const.h"
41 #include "gimple-iterator.h"
42 #include "tree-ssa-loop-manip.h"
43 #include "tree-ssa-loop-niter.h"
44 #include "tree-ssa-loop.h"
45 #include "tree-into-ssa.h"
48 #include "tree-data-ref.h"
49 #include "tree-scalar-evolution.h"
50 #include "tree-pass.h"
51 #include "graphite-poly.h"
52 #include "tree-ssa-propagate.h"
53 #include "graphite-scop-detection.h"
54 #include "gimple-pretty-print.h"
56 /* Forward declarations. */
57 static void make_close_phi_nodes_unique (basic_block
);
59 /* The type of the analyzed basic block. */
61 typedef enum gbb_type
{
63 GBB_LOOP_SING_EXIT_HEADER
,
64 GBB_LOOP_MULT_EXIT_HEADER
,
71 /* Detect the type of BB. Loop headers are only marked, if they are
72 new. This means their loop_father is different to LAST_LOOP.
73 Otherwise they are treated like any other bb and their type can be
77 get_bb_type (basic_block bb
, struct loop
*last_loop
)
81 struct loop
*loop
= bb
->loop_father
;
83 /* Check, if we entry into a new loop. */
84 if (loop
!= last_loop
)
86 if (single_exit (loop
) != NULL
)
87 return GBB_LOOP_SING_EXIT_HEADER
;
88 else if (loop
->num
!= 0)
89 return GBB_LOOP_MULT_EXIT_HEADER
;
91 return GBB_COND_HEADER
;
94 dom
= get_dominated_by (CDI_DOMINATORS
, bb
);
95 nb_dom
= dom
.length ();
101 if (nb_dom
== 1 && single_succ_p (bb
))
104 return GBB_COND_HEADER
;
107 /* A SCoP detection region, defined using bbs as borders.
109 All control flow touching this region, comes in passing basic_block
110 ENTRY and leaves passing basic_block EXIT. By using bbs instead of
111 edges for the borders we are able to represent also regions that do
112 not have a single entry or exit edge.
114 But as they have a single entry basic_block and a single exit
115 basic_block, we are able to generate for every sd_region a single
123 / \ This region contains: {3, 4, 5, 6, 7, 8}
131 typedef struct sd_region_p
133 /* The entry bb dominates all bbs in the sd_region. It is part of
137 /* The exit bb postdominates all bbs in the sd_region, but is not
138 part of the region. */
144 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
147 move_sd_regions (vec
<sd_region
> *source
, vec
<sd_region
> *target
)
152 FOR_EACH_VEC_ELT (*source
, i
, s
)
153 target
->safe_push (*s
);
158 /* Something like "n * m" is not allowed. */
161 graphite_can_represent_init (tree e
)
163 switch (TREE_CODE (e
))
165 case POLYNOMIAL_CHREC
:
166 return graphite_can_represent_init (CHREC_LEFT (e
))
167 && graphite_can_represent_init (CHREC_RIGHT (e
));
170 if (chrec_contains_symbols (TREE_OPERAND (e
, 0)))
171 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
172 && tree_fits_shwi_p (TREE_OPERAND (e
, 1));
174 return graphite_can_represent_init (TREE_OPERAND (e
, 1))
175 && tree_fits_shwi_p (TREE_OPERAND (e
, 0));
178 case POINTER_PLUS_EXPR
:
180 return graphite_can_represent_init (TREE_OPERAND (e
, 0))
181 && graphite_can_represent_init (TREE_OPERAND (e
, 1));
186 case NON_LVALUE_EXPR
:
187 return graphite_can_represent_init (TREE_OPERAND (e
, 0));
196 /* Return true when SCEV can be represented in the polyhedral model.
198 An expression can be represented, if it can be expressed as an
199 affine expression. For loops (i, j) and parameters (m, n) all
200 affine expressions are of the form:
202 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
204 1 i + 20 j + (-2) m + 25
206 Something like "i * n" or "n * m" is not allowed. */
209 graphite_can_represent_scev (tree scev
)
211 if (chrec_contains_undetermined (scev
))
214 /* We disable the handling of pointer types, because it’s currently not
215 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
216 the only nodes, which are disabled in case they are pointers to object
217 types, but this can be changed. */
219 if (POINTER_TYPE_P (TREE_TYPE (scev
)) && TREE_CODE (scev
) == SSA_NAME
)
222 switch (TREE_CODE (scev
))
227 case NON_LVALUE_EXPR
:
228 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0));
231 case POINTER_PLUS_EXPR
:
233 return graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
234 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
237 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 0)))
238 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev
, 1)))
239 && !(chrec_contains_symbols (TREE_OPERAND (scev
, 0))
240 && chrec_contains_symbols (TREE_OPERAND (scev
, 1)))
241 && graphite_can_represent_init (scev
)
242 && graphite_can_represent_scev (TREE_OPERAND (scev
, 0))
243 && graphite_can_represent_scev (TREE_OPERAND (scev
, 1));
245 case POLYNOMIAL_CHREC
:
246 /* Check for constant strides. With a non constant stride of
247 'n' we would have a value of 'iv * n'. Also check that the
248 initial value can represented: for example 'n * m' cannot be
250 if (!evolution_function_right_is_integer_cst (scev
)
251 || !graphite_can_represent_init (scev
))
253 return graphite_can_represent_scev (CHREC_LEFT (scev
));
259 /* Only affine functions can be represented. */
260 if (tree_contains_chrecs (scev
, NULL
)
261 || !scev_is_linear_expression (scev
))
268 /* Return true when EXPR can be represented in the polyhedral model.
270 This means an expression can be represented, if it is linear with
271 respect to the loops and the strides are non parametric.
272 LOOP is the place where the expr will be evaluated. SCOP_ENTRY defines the
273 entry of the region we analyse. */
276 graphite_can_represent_expr (basic_block scop_entry
, loop_p loop
,
279 tree scev
= analyze_scalar_evolution (loop
, expr
);
281 scev
= instantiate_scev (scop_entry
, loop
, scev
);
283 return graphite_can_represent_scev (scev
);
286 /* Return true if the data references of STMT can be represented by
290 stmt_has_simple_data_refs_p (loop_p outermost_loop ATTRIBUTE_UNUSED
,
296 vec
<data_reference_p
> drs
= vNULL
;
299 for (outer
= loop_containing_stmt (stmt
); outer
; outer
= loop_outer (outer
))
301 graphite_find_data_references_in_stmt (outer
,
302 loop_containing_stmt (stmt
),
305 FOR_EACH_VEC_ELT (drs
, j
, dr
)
307 int nb_subscripts
= DR_NUM_DIMENSIONS (dr
);
308 tree ref
= DR_REF (dr
);
310 for (int i
= nb_subscripts
- 1; i
>= 0; i
--)
312 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr
, i
))
313 || (TREE_CODE (ref
) != ARRAY_REF
314 && TREE_CODE (ref
) != MEM_REF
315 && TREE_CODE (ref
) != COMPONENT_REF
))
317 free_data_refs (drs
);
321 ref
= TREE_OPERAND (ref
, 0);
325 free_data_refs (drs
);
329 free_data_refs (drs
);
333 /* Return true only when STMT is simple enough for being handled by
334 Graphite. This depends on SCOP_ENTRY, as the parameters are
335 initialized relatively to this basic block, the linear functions
336 are initialized to OUTERMOST_LOOP and BB is the place where we try
337 to evaluate the STMT. */
340 stmt_simple_for_scop_p (basic_block scop_entry
, loop_p outermost_loop
,
341 gimple stmt
, basic_block bb
)
343 loop_p loop
= bb
->loop_father
;
345 gcc_assert (scop_entry
);
347 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
348 Calls have side-effects, except those to const or pure
350 if (gimple_has_volatile_ops (stmt
)
351 || (gimple_code (stmt
) == GIMPLE_CALL
352 && !(gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
)))
353 || (gimple_code (stmt
) == GIMPLE_ASM
))
355 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
357 fprintf (dump_file
, "[scop-detection-fail] ");
358 fprintf (dump_file
, "Graphite cannot handle this stmt:\n");
359 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
);
365 if (is_gimple_debug (stmt
))
368 if (!stmt_has_simple_data_refs_p (outermost_loop
, stmt
))
370 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
372 fprintf (dump_file
, "[scop-detection-fail] ");
373 fprintf (dump_file
, "Graphite cannot handle data-refs in stmt:\n");
374 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
);
380 switch (gimple_code (stmt
))
387 /* We can handle all binary comparisons. Inequalities are
388 also supported as they can be represented with union of
390 enum tree_code code
= gimple_cond_code (stmt
);
391 if (!(code
== LT_EXPR
398 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
400 fprintf (dump_file
, "[scop-detection-fail] ");
401 fprintf (dump_file
, "Graphite cannot handle cond stmt:\n");
402 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
);
408 for (unsigned i
= 0; i
< 2; ++i
)
410 tree op
= gimple_op (stmt
, i
);
411 if (!graphite_can_represent_expr (scop_entry
, loop
, op
)
412 /* We can not handle REAL_TYPE. Failed for pr39260. */
413 || TREE_CODE (TREE_TYPE (op
)) == REAL_TYPE
)
415 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
417 fprintf (dump_file
, "[scop-detection-fail] ");
418 fprintf (dump_file
, "Graphite cannot represent stmt:\n");
419 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
);
434 /* These nodes cut a new scope. */
435 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
437 fprintf (dump_file
, "[scop-detection-fail] ");
438 fprintf (dump_file
, "Gimple stmt not handled in Graphite:\n");
439 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
);
447 /* Returns the statement of BB that contains a harmful operation: that
448 can be a function call with side effects, the induction variables
449 are not linear with respect to SCOP_ENTRY, etc. The current open
450 scop should end before this statement. The evaluation is limited using
451 OUTERMOST_LOOP as outermost loop that may change. */
454 harmful_stmt_in_bb (basic_block scop_entry
, loop_p outer_loop
, basic_block bb
)
456 gimple_stmt_iterator gsi
;
458 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
459 if (!stmt_simple_for_scop_p (scop_entry
, outer_loop
, gsi_stmt (gsi
), bb
))
460 return gsi_stmt (gsi
);
465 /* Return true if LOOP can be represented in the polyhedral
466 representation. This is evaluated taking SCOP_ENTRY and
467 OUTERMOST_LOOP in mind. */
470 graphite_can_represent_loop (basic_block scop_entry
, loop_p loop
)
473 struct tree_niter_desc niter_desc
;
475 /* FIXME: For the moment, graphite cannot be used on loops that
476 iterate using induction variables that wrap. */
478 return number_of_iterations_exit (loop
, single_exit (loop
), &niter_desc
, false)
479 && niter_desc
.control
.no_overflow
480 && (niter
= number_of_latch_executions (loop
))
481 && !chrec_contains_undetermined (niter
)
482 && graphite_can_represent_expr (scop_entry
, loop
, niter
);
485 /* Store information needed by scopdet_* functions. */
489 /* Exit of the open scop would stop if the current BB is harmful. */
492 /* Where the next scop would start if the current BB is harmful. */
495 /* The bb or one of its children contains open loop exits. That means
496 loop exit nodes that are not surrounded by a loop dominated by bb. */
499 /* The bb or one of its children contains only structures we can handle. */
503 static struct scopdet_info
build_scops_1 (basic_block
, loop_p
,
504 vec
<sd_region
> *, loop_p
);
506 /* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
507 to SCOPS. TYPE is the gbb_type of BB. */
509 static struct scopdet_info
510 scopdet_basic_block_info (basic_block bb
, loop_p outermost_loop
,
511 vec
<sd_region
> *scops
, gbb_type type
)
513 loop_p loop
= bb
->loop_father
;
514 struct scopdet_info result
;
517 /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps. */
518 basic_block entry_block
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
519 stmt
= harmful_stmt_in_bb (entry_block
, outermost_loop
, bb
);
520 result
.difficult
= (stmt
!= NULL
);
527 result
.exits
= false;
529 /* Mark bbs terminating a SESE region difficult, if they start
530 a condition or if the block it exits to cannot be split
531 with make_forwarder_block. */
532 if (!single_succ_p (bb
)
533 || bb_has_abnormal_pred (single_succ (bb
)))
535 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
537 fprintf (dump_file
, "[scop-detection-fail] ");
538 fprintf (dump_file
, "BB %d cannot be part of a scop.\n",
542 result
.difficult
= true;
545 result
.exit
= single_succ (bb
);
550 result
.next
= single_succ (bb
);
551 result
.exits
= false;
552 result
.exit
= single_succ (bb
);
555 case GBB_LOOP_SING_EXIT_HEADER
:
557 auto_vec
<sd_region
, 3> regions
;
558 struct scopdet_info sinfo
;
559 edge exit_e
= single_exit (loop
);
561 sinfo
= build_scops_1 (bb
, outermost_loop
, ®ions
, loop
);
563 if (!graphite_can_represent_loop (entry_block
, loop
))
565 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
567 fprintf (dump_file
, "[scop-detection-fail] ");
568 fprintf (dump_file
, "Graphite cannot represent loop %d.\n",
571 result
.difficult
= true;
574 result
.difficult
|= sinfo
.difficult
;
576 /* Try again with another loop level. */
578 && loop_depth (outermost_loop
) + 1 == loop_depth (loop
))
580 outermost_loop
= loop
;
585 sinfo
= scopdet_basic_block_info (bb
, outermost_loop
, scops
, type
);
588 result
.difficult
= true;
591 move_sd_regions (®ions
, scops
);
595 open_scop
.entry
= bb
;
596 open_scop
.exit
= exit_e
->dest
;
597 scops
->safe_push (open_scop
);
603 result
.exit
= exit_e
->dest
;
604 result
.next
= exit_e
->dest
;
606 /* If we do not dominate result.next, remove it. It's either
607 the exit block, or another bb dominates it and will
608 call the scop detection for this bb. */
609 if (!dominated_by_p (CDI_DOMINATORS
, result
.next
, bb
))
612 if (exit_e
->src
->loop_father
!= loop
)
615 result
.exits
= false;
617 if (result
.difficult
)
618 move_sd_regions (®ions
, scops
);
626 case GBB_LOOP_MULT_EXIT_HEADER
:
628 /* XXX: For now we just do not join loops with multiple exits. If the
629 exits lead to the same bb it may be possible to join the loop. */
630 auto_vec
<sd_region
, 3> regions
;
631 vec
<edge
> exits
= get_loop_exit_edges (loop
);
634 build_scops_1 (bb
, loop
, ®ions
, loop
);
636 /* Scan the code dominated by this loop. This means all bbs, that are
637 are dominated by a bb in this loop, but are not part of this loop.
640 - The loop exit destination is dominated by the exit sources.
642 TODO: We miss here the more complex cases:
643 - The exit destinations are dominated by another bb inside
645 - The loop dominates bbs, that are not exit destinations. */
646 FOR_EACH_VEC_ELT (exits
, i
, e
)
647 if (e
->src
->loop_father
== loop
648 && dominated_by_p (CDI_DOMINATORS
, e
->dest
, e
->src
))
650 if (loop_outer (outermost_loop
))
651 outermost_loop
= loop_outer (outermost_loop
);
653 /* Pass loop_outer to recognize e->dest as loop header in
655 if (e
->dest
->loop_father
->header
== e
->dest
)
656 build_scops_1 (e
->dest
, outermost_loop
, ®ions
,
657 loop_outer (e
->dest
->loop_father
));
659 build_scops_1 (e
->dest
, outermost_loop
, ®ions
,
660 e
->dest
->loop_father
);
665 result
.difficult
= true;
666 result
.exits
= false;
667 move_sd_regions (®ions
, scops
);
671 case GBB_COND_HEADER
:
673 auto_vec
<sd_region
, 3> regions
;
674 struct scopdet_info sinfo
;
675 vec
<basic_block
> dominated
;
678 basic_block last_exit
= NULL
;
680 result
.exits
= false;
682 /* First check the successors of BB, and check if it is
683 possible to join the different branches. */
684 FOR_EACH_VEC_SAFE_ELT (bb
->succs
, i
, e
)
686 /* Ignore loop exits. They will be handled after the loop
688 if (loop_exits_to_bb_p (loop
, e
->dest
))
694 /* Do not follow edges that lead to the end of the
695 conditions block. For example, in
705 the edge from 0 => 6. Only check if all paths lead to
708 if (!single_pred_p (e
->dest
))
710 /* Check, if edge leads directly to the end of this
715 if (e
->dest
!= last_exit
)
716 result
.difficult
= true;
721 if (!dominated_by_p (CDI_DOMINATORS
, e
->dest
, bb
))
723 result
.difficult
= true;
727 sinfo
= build_scops_1 (e
->dest
, outermost_loop
, ®ions
, loop
);
729 result
.exits
|= sinfo
.exits
;
730 result
.difficult
|= sinfo
.difficult
;
732 /* Checks, if all branches end at the same point.
733 If that is true, the condition stays joinable.
734 Have a look at the example above. */
738 last_exit
= sinfo
.exit
;
740 if (sinfo
.exit
!= last_exit
)
741 result
.difficult
= true;
744 result
.difficult
= true;
748 result
.difficult
= true;
750 /* Join the branches of the condition if possible. */
751 if (!result
.exits
&& !result
.difficult
)
753 /* Only return a next pointer if we dominate this pointer.
754 Otherwise it will be handled by the bb dominating it. */
755 if (dominated_by_p (CDI_DOMINATORS
, last_exit
, bb
)
757 result
.next
= last_exit
;
761 result
.exit
= last_exit
;
767 /* Scan remaining bbs dominated by BB. */
768 dominated
= get_dominated_by (CDI_DOMINATORS
, bb
);
770 FOR_EACH_VEC_ELT (dominated
, i
, dom_bb
)
772 /* Ignore loop exits: they will be handled after the loop body. */
773 if (loop_depth (find_common_loop (loop
, dom_bb
->loop_father
))
780 /* Ignore the bbs processed above. */
781 if (single_pred_p (dom_bb
) && single_pred (dom_bb
) == bb
)
784 if (loop_depth (loop
) > loop_depth (dom_bb
->loop_father
))
785 sinfo
= build_scops_1 (dom_bb
, outermost_loop
, ®ions
,
788 sinfo
= build_scops_1 (dom_bb
, outermost_loop
, ®ions
, loop
);
790 result
.exits
|= sinfo
.exits
;
791 result
.difficult
= true;
795 dominated
.release ();
798 move_sd_regions (®ions
, scops
);
810 /* Starting from CURRENT we walk the dominance tree and add new sd_regions to
811 SCOPS. The analyse if a sd_region can be handled is based on the value
812 of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change. LOOP
813 is the loop in which CURRENT is handled.
815 TODO: These functions got a little bit big. They definitely should be cleaned
818 static struct scopdet_info
819 build_scops_1 (basic_block current
, loop_p outermost_loop
,
820 vec
<sd_region
> *scops
, loop_p loop
)
822 bool in_scop
= false;
824 struct scopdet_info sinfo
;
826 /* Initialize result. */
827 struct scopdet_info result
;
828 result
.exits
= false;
829 result
.difficult
= false;
832 open_scop
.entry
= NULL
;
833 open_scop
.exit
= NULL
;
836 /* Loop over the dominance tree. If we meet a difficult bb, close
837 the current SCoP. Loop and condition header start a new layer,
838 and can only be added if all bbs in deeper layers are simple. */
839 while (current
!= NULL
)
841 sinfo
= scopdet_basic_block_info (current
, outermost_loop
, scops
,
842 get_bb_type (current
, loop
));
844 if (!in_scop
&& !(sinfo
.exits
|| sinfo
.difficult
))
846 open_scop
.entry
= current
;
847 open_scop
.exit
= NULL
;
850 else if (in_scop
&& (sinfo
.exits
|| sinfo
.difficult
))
852 open_scop
.exit
= current
;
853 scops
->safe_push (open_scop
);
857 result
.difficult
|= sinfo
.difficult
;
858 result
.exits
|= sinfo
.exits
;
860 current
= sinfo
.next
;
863 /* Try to close open_scop, if we are still in an open SCoP. */
866 open_scop
.exit
= sinfo
.exit
;
867 gcc_assert (open_scop
.exit
);
868 if (open_scop
.entry
!= open_scop
.exit
)
869 scops
->safe_push (open_scop
);
872 sinfo
.difficult
= true;
878 result
.exit
= sinfo
.exit
;
882 /* Checks if a bb is contained in REGION. */
885 bb_in_sd_region (basic_block bb
, sd_region
*region
)
887 return bb_in_region (bb
, region
->entry
, region
->exit
);
890 /* Returns the single entry edge of REGION, if it does not exits NULL. */
893 find_single_entry_edge (sd_region
*region
)
899 FOR_EACH_EDGE (e
, ei
, region
->entry
->preds
)
900 if (!bb_in_sd_region (e
->src
, region
))
915 /* Returns the single exit edge of REGION, if it does not exits NULL. */
918 find_single_exit_edge (sd_region
*region
)
924 FOR_EACH_EDGE (e
, ei
, region
->exit
->preds
)
925 if (bb_in_sd_region (e
->src
, region
))
940 /* Create a single entry edge for REGION. */
943 create_single_entry_edge (sd_region
*region
)
945 if (find_single_entry_edge (region
))
948 /* There are multiple predecessors for bb_3
961 There are two edges (1->3, 2->3), that point from outside into the region,
962 and another one (5->3), a loop latch, lead to bb_3.
970 | |\ (3.0 -> 3.1) = single entry edge
979 If the loop is part of the SCoP, we have to redirect the loop latches.
985 | | (3.0 -> 3.1) = entry edge
994 if (region
->entry
->loop_father
->header
!= region
->entry
995 || dominated_by_p (CDI_DOMINATORS
,
996 loop_latch_edge (region
->entry
->loop_father
)->src
,
999 edge forwarder
= split_block_after_labels (region
->entry
);
1000 region
->entry
= forwarder
->dest
;
1003 /* This case is never executed, as the loop headers seem always to have a
1004 single edge pointing from outside into the loop. */
1007 gcc_checking_assert (find_single_entry_edge (region
));
1010 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
1013 sd_region_without_exit (edge e
)
1015 sd_region
*r
= (sd_region
*) e
->aux
;
1018 return r
->exit
== NULL
;
1023 /* Create a single exit edge for REGION. */
1026 create_single_exit_edge (sd_region
*region
)
1030 edge forwarder
= NULL
;
1033 /* We create a forwarder bb (5) for all edges leaving this region
1034 (3->5, 4->5). All other edges leading to the same bb, are moved
1035 to a new bb (6). If these edges where part of another region (2->5)
1036 we update the region->exit pointer, of this region.
1038 To identify which edge belongs to which region we depend on the e->aux
1039 pointer in every edge. It points to the region of the edge or to NULL,
1040 if the edge is not part of any region.
1042 1 2 3 4 1->5 no region, 2->5 region->exit = 5,
1043 \| |/ 3->5 region->exit = NULL, 4->5 region->exit = NULL
1048 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
1049 | | \/ 3->5 no region, 4->5 no region,
1051 \| / 5->6 region->exit = 6
1054 Now there is only a single exit edge (5->6). */
1055 exit
= region
->exit
;
1056 region
->exit
= NULL
;
1057 forwarder
= make_forwarder_block (exit
, &sd_region_without_exit
, NULL
);
1059 /* Unmark the edges, that are no longer exit edges. */
1060 FOR_EACH_EDGE (e
, ei
, forwarder
->src
->preds
)
1064 /* Mark the new exit edge. */
1065 single_succ_edge (forwarder
->src
)->aux
= region
;
1067 /* Update the exit bb of all regions, where exit edges lead to
1069 FOR_EACH_EDGE (e
, ei
, forwarder
->dest
->preds
)
1071 ((sd_region
*) e
->aux
)->exit
= forwarder
->dest
;
1073 gcc_checking_assert (find_single_exit_edge (region
));
1076 /* Unmark the exit edges of all REGIONS.
1077 See comment in "create_single_exit_edge". */
1080 unmark_exit_edges (vec
<sd_region
> regions
)
1087 FOR_EACH_VEC_ELT (regions
, i
, s
)
1088 FOR_EACH_EDGE (e
, ei
, s
->exit
->preds
)
1093 /* Mark the exit edges of all REGIONS.
1094 See comment in "create_single_exit_edge". */
1097 mark_exit_edges (vec
<sd_region
> regions
)
1104 FOR_EACH_VEC_ELT (regions
, i
, s
)
1105 FOR_EACH_EDGE (e
, ei
, s
->exit
->preds
)
1106 if (bb_in_sd_region (e
->src
, s
))
1110 /* Create for all scop regions a single entry and a single exit edge. */
1113 create_sese_edges (vec
<sd_region
> regions
)
1118 FOR_EACH_VEC_ELT (regions
, i
, s
)
1119 create_single_entry_edge (s
);
1121 mark_exit_edges (regions
);
1123 FOR_EACH_VEC_ELT (regions
, i
, s
)
1124 /* Don't handle multiple edges exiting the function. */
1125 if (!find_single_exit_edge (s
)
1126 && s
->exit
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1127 create_single_exit_edge (s
);
1129 unmark_exit_edges (regions
);
1131 calculate_dominance_info (CDI_DOMINATORS
);
1132 fix_loop_structure (NULL
);
1134 #ifdef ENABLE_CHECKING
1135 verify_loop_structure ();
1136 verify_ssa (false, true);
1140 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1143 build_graphite_scops (vec
<sd_region
> regions
,
1149 FOR_EACH_VEC_ELT (regions
, i
, s
)
1151 edge entry
= find_single_entry_edge (s
);
1152 edge exit
= find_single_exit_edge (s
);
1158 scop
= new_scop (new_sese (entry
, exit
));
1159 scops
->safe_push (scop
);
1161 /* Are there overlapping SCoPs? */
1162 #ifdef ENABLE_CHECKING
1167 FOR_EACH_VEC_ELT (regions
, j
, s2
)
1169 gcc_assert (!bb_in_sd_region (s
->entry
, s2
));
1175 /* Returns true when BB contains only close phi nodes. */
1178 contains_only_close_phi_nodes (basic_block bb
)
1180 gimple_stmt_iterator gsi
;
1182 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1183 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_LABEL
)
1189 /* Print statistics for SCOP to FILE. */
1192 print_graphite_scop_statistics (FILE* file
, scop_p scop
)
1197 long n_conditions
= 0;
1201 long n_p_conditions
= 0;
1205 FOR_ALL_BB_FN (bb
, cfun
)
1207 gimple_stmt_iterator psi
;
1208 loop_p loop
= bb
->loop_father
;
1210 if (!bb_in_sese_p (bb
, SCOP_REGION (scop
)))
1214 n_p_bbs
+= bb
->count
;
1216 if (EDGE_COUNT (bb
->succs
) > 1)
1219 n_p_conditions
+= bb
->count
;
1222 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1225 n_p_stmts
+= bb
->count
;
1228 if (loop
->header
== bb
&& loop_in_sese_p (loop
, SCOP_REGION (scop
)))
1231 n_p_loops
+= bb
->count
;
1236 fprintf (file
, "\nBefore limit_scops SCoP statistics (");
1237 fprintf (file
, "BBS:%ld, ", n_bbs
);
1238 fprintf (file
, "LOOPS:%ld, ", n_loops
);
1239 fprintf (file
, "CONDITIONS:%ld, ", n_conditions
);
1240 fprintf (file
, "STMTS:%ld)\n", n_stmts
);
1241 fprintf (file
, "\nBefore limit_scops SCoP profiling statistics (");
1242 fprintf (file
, "BBS:%ld, ", n_p_bbs
);
1243 fprintf (file
, "LOOPS:%ld, ", n_p_loops
);
1244 fprintf (file
, "CONDITIONS:%ld, ", n_p_conditions
);
1245 fprintf (file
, "STMTS:%ld)\n", n_p_stmts
);
1248 /* Print statistics for SCOPS to FILE. */
1251 print_graphite_statistics (FILE* file
, vec
<scop_p
> scops
)
1256 FOR_EACH_VEC_ELT (scops
, i
, scop
)
1257 print_graphite_scop_statistics (file
, scop
);
1260 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
1270 * SCoP frontier, as this line is not surrounded by any loop. *
1274 This is necessary as scalar evolution and parameter detection need a
1275 outermost loop to initialize parameters correctly.
1277 TODO: FIX scalar evolution and parameter detection to allow more flexible
1281 limit_scops (vec
<scop_p
> *scops
)
1283 auto_vec
<sd_region
, 3> regions
;
1288 FOR_EACH_VEC_ELT (*scops
, i
, scop
)
1292 sese region
= SCOP_REGION (scop
);
1293 build_sese_loop_nests (region
);
1295 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region
), j
, loop
)
1296 if (!loop_in_sese_p (loop_outer (loop
), region
)
1297 && single_exit (loop
))
1299 sd_region open_scop
;
1300 open_scop
.entry
= loop
->header
;
1301 open_scop
.exit
= single_exit (loop
)->dest
;
1303 /* This is a hack on top of the limit_scops hack. The
1304 limit_scops hack should disappear all together. */
1305 if (single_succ_p (open_scop
.exit
)
1306 && contains_only_close_phi_nodes (open_scop
.exit
))
1307 open_scop
.exit
= single_succ_edge (open_scop
.exit
)->dest
;
1309 regions
.safe_push (open_scop
);
1313 free_scops (*scops
);
1316 create_sese_edges (regions
);
1317 build_graphite_scops (regions
, scops
);
1320 /* Returns true when P1 and P2 are close phis with the same
1324 same_close_phi_node (gphi
*p1
, gphi
*p2
)
1326 return operand_equal_p (gimple_phi_arg_def (p1
, 0),
1327 gimple_phi_arg_def (p2
, 0), 0);
1330 /* Remove the close phi node at GSI and replace its rhs with the rhs
1334 remove_duplicate_close_phi (gphi
*phi
, gphi_iterator
*gsi
)
1337 use_operand_p use_p
;
1338 imm_use_iterator imm_iter
;
1339 tree res
= gimple_phi_result (phi
);
1340 tree def
= gimple_phi_result (gsi
->phi ());
1342 gcc_assert (same_close_phi_node (phi
, gsi
->phi ()));
1344 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, def
)
1346 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
1347 SET_USE (use_p
, res
);
1349 update_stmt (use_stmt
);
1351 /* It is possible that we just created a duplicate close-phi
1352 for an already-processed containing loop. Check for this
1353 case and clean it up. */
1354 if (gimple_code (use_stmt
) == GIMPLE_PHI
1355 && gimple_phi_num_args (use_stmt
) == 1)
1356 make_close_phi_nodes_unique (gimple_bb (use_stmt
));
1359 remove_phi_node (gsi
, true);
1362 /* Removes all the close phi duplicates from BB. */
1365 make_close_phi_nodes_unique (basic_block bb
)
1369 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1371 gphi_iterator gsi
= psi
;
1372 gphi
*phi
= psi
.phi ();
1374 /* At this point, PHI should be a close phi in normal form. */
1375 gcc_assert (gimple_phi_num_args (phi
) == 1);
1377 /* Iterate over the next phis and remove duplicates. */
1379 while (!gsi_end_p (gsi
))
1380 if (same_close_phi_node (phi
, gsi
.phi ()))
1381 remove_duplicate_close_phi (phi
, &gsi
);
1387 /* Transforms LOOP to the canonical loop closed SSA form. */
1390 canonicalize_loop_closed_ssa (loop_p loop
)
1392 edge e
= single_exit (loop
);
1395 if (!e
|| e
->flags
& EDGE_ABNORMAL
)
1400 if (single_pred_p (bb
))
1402 e
= split_block_after_labels (bb
);
1403 make_close_phi_nodes_unique (e
->src
);
1408 basic_block close
= split_edge (e
);
1410 e
= single_succ_edge (close
);
1412 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
1414 gphi
*phi
= psi
.phi ();
1417 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1418 if (gimple_phi_arg_edge (phi
, i
) == e
)
1420 tree res
, arg
= gimple_phi_arg_def (phi
, i
);
1421 use_operand_p use_p
;
1424 if (TREE_CODE (arg
) != SSA_NAME
)
1427 close_phi
= create_phi_node (NULL_TREE
, close
);
1428 res
= create_new_def_for (arg
, close_phi
,
1429 gimple_phi_result_ptr (close_phi
));
1430 add_phi_arg (close_phi
, arg
,
1431 gimple_phi_arg_edge (close_phi
, 0),
1433 use_p
= gimple_phi_arg_imm_use_ptr (phi
, i
);
1434 replace_exp (use_p
, res
);
1439 make_close_phi_nodes_unique (close
);
1442 /* The code above does not properly handle changes in the post dominance
1443 information (yet). */
1444 free_dominance_info (CDI_POST_DOMINATORS
);
1447 /* Converts the current loop closed SSA form to a canonical form
1448 expected by the Graphite code generation.
1450 The loop closed SSA form has the following invariant: a variable
1451 defined in a loop that is used outside the loop appears only in the
1452 phi nodes in the destination of the loop exit. These phi nodes are
1453 called close phi nodes.
1455 The canonical loop closed SSA form contains the extra invariants:
1457 - when the loop contains only one exit, the close phi nodes contain
1458 only one argument. That implies that the basic block that contains
1459 the close phi nodes has only one predecessor, that is a basic block
1462 - the basic block containing the close phi nodes does not contain
1465 - there exist only one phi node per definition in the loop.
1469 canonicalize_loop_closed_ssa_form (void)
1473 #ifdef ENABLE_CHECKING
1474 verify_loop_closed_ssa (true);
1477 FOR_EACH_LOOP (loop
, 0)
1478 canonicalize_loop_closed_ssa (loop
);
1480 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
1481 update_ssa (TODO_update_ssa
);
1483 #ifdef ENABLE_CHECKING
1484 verify_loop_closed_ssa (true);
1488 /* Find Static Control Parts (SCoP) in the current function and pushes
1492 build_scops (vec
<scop_p
> *scops
)
1494 struct loop
*loop
= current_loops
->tree_root
;
1495 auto_vec
<sd_region
, 3> regions
;
1497 canonicalize_loop_closed_ssa_form ();
1498 build_scops_1 (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
1499 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->loop_father
,
1501 create_sese_edges (regions
);
1502 build_graphite_scops (regions
, scops
);
1504 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1505 print_graphite_statistics (dump_file
, *scops
);
1507 limit_scops (scops
);
1510 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1511 fprintf (dump_file
, "\nnumber of SCoPs: %d\n",
1512 scops
? scops
->length () : 0);
1515 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1516 different colors. If there are not enough colors, paint the
1517 remaining SCoPs in gray.
1520 - "*" after the node number denotes the entry of a SCoP,
1521 - "#" after the node number denotes the exit of a SCoP,
1522 - "()" around the node number denotes the entry or the
1523 exit nodes of the SCOP. These are not part of SCoP. */
1526 dot_all_scops_1 (FILE *file
, vec
<scop_p
> scops
)
1535 /* Disable debugging while printing graph. */
1536 int tmp_dump_flags
= dump_flags
;
1539 fprintf (file
, "digraph all {\n");
1541 FOR_ALL_BB_FN (bb
, cfun
)
1543 int part_of_scop
= false;
1545 /* Use HTML for every bb label. So we are able to print bbs
1546 which are part of two different SCoPs, with two different
1547 background colors. */
1548 fprintf (file
, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1550 fprintf (file
, "CELLSPACING=\"0\">\n");
1552 /* Select color for SCoP. */
1553 FOR_EACH_VEC_ELT (scops
, i
, scop
)
1555 sese region
= SCOP_REGION (scop
);
1556 if (bb_in_sese_p (bb
, region
)
1557 || (SESE_EXIT_BB (region
) == bb
)
1558 || (SESE_ENTRY_BB (region
) == bb
))
1571 case 3: /* purple */
1574 case 4: /* orange */
1577 case 5: /* yellow */
1617 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color
);
1619 if (!bb_in_sese_p (bb
, region
))
1620 fprintf (file
, " (");
1622 if (bb
== SESE_ENTRY_BB (region
)
1623 && bb
== SESE_EXIT_BB (region
))
1624 fprintf (file
, " %d*# ", bb
->index
);
1625 else if (bb
== SESE_ENTRY_BB (region
))
1626 fprintf (file
, " %d* ", bb
->index
);
1627 else if (bb
== SESE_EXIT_BB (region
))
1628 fprintf (file
, " %d# ", bb
->index
);
1630 fprintf (file
, " %d ", bb
->index
);
1632 if (!bb_in_sese_p (bb
,region
))
1633 fprintf (file
, ")");
1635 fprintf (file
, "</TD></TR>\n");
1636 part_of_scop
= true;
1642 fprintf (file
, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1643 fprintf (file
, " %d </TD></TR>\n", bb
->index
);
1645 fprintf (file
, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1648 FOR_ALL_BB_FN (bb
, cfun
)
1650 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1651 fprintf (file
, "%d -> %d;\n", bb
->index
, e
->dest
->index
);
1654 fputs ("}\n\n", file
);
1656 /* Enable debugging again. */
1657 dump_flags
= tmp_dump_flags
;
1660 /* Display all SCoPs using dotty. */
1663 dot_all_scops (vec
<scop_p
> scops
)
1665 /* When debugging, enable the following code. This cannot be used
1666 in production compilers because it calls "system". */
1669 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
1670 gcc_assert (stream
);
1672 dot_all_scops_1 (stream
, scops
);
1675 x
= system ("dotty /tmp/allscops.dot &");
1677 dot_all_scops_1 (stderr
, scops
);
1681 /* Display all SCoPs using dotty. */
1684 dot_scop (scop_p scop
)
1686 auto_vec
<scop_p
, 1> scops
;
1689 scops
.safe_push (scop
);
1691 /* When debugging, enable the following code. This cannot be used
1692 in production compilers because it calls "system". */
1696 FILE *stream
= fopen ("/tmp/allscops.dot", "w");
1697 gcc_assert (stream
);
1699 dot_all_scops_1 (stream
, scops
);
1701 x
= system ("dotty /tmp/allscops.dot &");
1704 dot_all_scops_1 (stderr
, scops
);
1708 #endif /* HAVE_isl */