Daily bump.
[official-gcc.git] / gcc / graphite-scop-detection.c
blob53823b8678735f914579691c889046b11df92f04
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)
11 any later version.
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/>. */
22 #include "config.h"
24 #ifdef HAVE_isl
25 #include <isl/set.h>
26 #include <isl/map.h>
27 #include <isl/union_map.h>
28 #ifdef HAVE_cloog
29 #include <cloog/cloog.h>
30 #include <cloog/isl/domain.h>
31 #endif
32 #endif
34 #include "system.h"
35 #include "coretypes.h"
36 #include "tree.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.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"
51 #include "tree-ssa.h"
52 #include "cfgloop.h"
53 #include "tree-chrec.h"
54 #include "tree-data-ref.h"
55 #include "tree-scalar-evolution.h"
56 #include "tree-pass.h"
57 #include "sese.h"
58 #include "tree-ssa-propagate.h"
59 #include "cp/cp-tree.h"
61 #ifdef HAVE_isl
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 {
71 GBB_UNKNOWN,
72 GBB_LOOP_SING_EXIT_HEADER,
73 GBB_LOOP_MULT_EXIT_HEADER,
74 GBB_LOOP_EXIT,
75 GBB_COND_HEADER,
76 GBB_SIMPLE,
77 GBB_LAST
78 } gbb_type;
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
83 any other type. */
85 static gbb_type
86 get_bb_type (basic_block bb, struct loop *last_loop)
88 vec<basic_block> dom;
89 int nb_dom;
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;
99 else
100 return GBB_COND_HEADER;
103 dom = get_dominated_by (CDI_DOMINATORS, bb);
104 nb_dom = dom.length ();
105 dom.release ();
107 if (nb_dom == 0)
108 return GBB_LAST;
110 if (nb_dom == 1 && single_succ_p (bb))
111 return GBB_SIMPLE;
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
125 entry and exit edge.
129 3 <- entry
132 / \ This region contains: {3, 4, 5, 6, 7, 8}
137 9 <- exit */
140 typedef struct sd_region_p
142 /* The entry bb dominates all bbs in the sd_region. It is part of
143 the region. */
144 basic_block entry;
146 /* The exit bb postdominates all bbs in the sd_region, but is not
147 part of the region. */
148 basic_block exit;
149 } sd_region;
153 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
155 static void
156 move_sd_regions (vec<sd_region> *source, vec<sd_region> *target)
158 sd_region *s;
159 int i;
161 FOR_EACH_VEC_ELT (*source, i, s)
162 target->safe_push (*s);
164 source->release ();
167 /* Something like "n * m" is not allowed. */
169 static bool
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));
178 case MULT_EXPR:
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));
182 else
183 return graphite_can_represent_init (TREE_OPERAND (e, 1))
184 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
186 case PLUS_EXPR:
187 case POINTER_PLUS_EXPR:
188 case MINUS_EXPR:
189 return graphite_can_represent_init (TREE_OPERAND (e, 0))
190 && graphite_can_represent_init (TREE_OPERAND (e, 1));
192 case NEGATE_EXPR:
193 case BIT_NOT_EXPR:
194 CASE_CONVERT:
195 case NON_LVALUE_EXPR:
196 return graphite_can_represent_init (TREE_OPERAND (e, 0));
198 default:
199 break;
202 return true;
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. */
217 static bool
218 graphite_can_represent_scev (tree scev)
220 if (chrec_contains_undetermined (scev))
221 return false;
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)
229 return false;
231 switch (TREE_CODE (scev))
233 case NEGATE_EXPR:
234 case BIT_NOT_EXPR:
235 CASE_CONVERT:
236 case NON_LVALUE_EXPR:
237 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
239 case PLUS_EXPR:
240 case POINTER_PLUS_EXPR:
241 case MINUS_EXPR:
242 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
243 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
245 case MULT_EXPR:
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
258 represented. */
259 if (!evolution_function_right_is_integer_cst (scev)
260 || !graphite_can_represent_init (scev))
261 return false;
262 return graphite_can_represent_scev (CHREC_LEFT (scev));
264 default:
265 break;
268 /* Only affine functions can be represented. */
269 if (tree_contains_chrecs (scev, NULL)
270 || !scev_is_linear_expression (scev))
271 return false;
273 return true;
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. */
284 static bool
285 graphite_can_represent_expr (basic_block scop_entry, loop_p loop,
286 tree expr)
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
296 Graphite. */
298 static bool
299 stmt_has_simple_data_refs_p (loop_p outermost_loop ATTRIBUTE_UNUSED,
300 gimple stmt)
302 data_reference_p dr;
303 unsigned i;
304 int j;
305 bool res = true;
306 vec<data_reference_p> drs = vNULL;
307 loop_p outer;
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),
313 stmt, &drs);
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)))
319 res = false;
320 goto done;
323 free_data_refs (drs);
324 drs.create (0);
327 done:
328 free_data_refs (drs);
329 return res;
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. */
338 static bool
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
348 functions. */
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))
353 return false;
355 if (is_gimple_debug (stmt))
356 return true;
358 if (!stmt_has_simple_data_refs_p (outermost_loop, stmt))
359 return false;
361 switch (gimple_code (stmt))
363 case GIMPLE_RETURN:
364 case GIMPLE_LABEL:
365 return true;
367 case GIMPLE_COND:
369 /* We can handle all binary comparisons. Inequalities are
370 also supported as they can be represented with union of
371 polyhedra. */
372 enum tree_code code = gimple_cond_code (stmt);
373 if (!(code == LT_EXPR
374 || code == GT_EXPR
375 || code == LE_EXPR
376 || code == GE_EXPR
377 || code == EQ_EXPR
378 || code == NE_EXPR))
379 return false;
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)
387 return false;
390 return true;
393 case GIMPLE_ASSIGN:
394 case GIMPLE_CALL:
395 return true;
397 default:
398 /* These nodes cut a new scope. */
399 return false;
402 return false;
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. */
411 static gimple
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);
420 return NULL;
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. */
427 static bool
428 graphite_can_represent_loop (basic_block scop_entry, loop_p loop)
430 tree niter;
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. */
445 struct scopdet_info
447 /* Exit of the open scop would stop if the current BB is harmful. */
448 basic_block exit;
450 /* Where the next scop would start if the current BB is harmful. */
451 basic_block next;
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. */
455 bool exits;
457 /* The bb or one of its children contains only structures we can handle. */
458 bool difficult;
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;
473 gimple stmt;
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);
479 result.exit = NULL;
481 switch (type)
483 case GBB_LAST:
484 result.next = 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;
493 else
494 result.exit = single_succ (bb);
496 break;
498 case GBB_SIMPLE:
499 result.next = single_succ (bb);
500 result.exits = false;
501 result.exit = single_succ (bb);
502 break;
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, &regions, 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. */
518 if (result.difficult
519 && loop_depth (outermost_loop) + 1 == loop_depth (loop))
521 outermost_loop = loop;
523 regions.release ();
524 regions.create (3);
526 sinfo = scopdet_basic_block_info (bb, outermost_loop, scops, type);
528 result = sinfo;
529 result.difficult = true;
531 if (sinfo.difficult)
532 move_sd_regions (&regions, scops);
533 else
535 sd_region open_scop;
536 open_scop.entry = bb;
537 open_scop.exit = exit_e->dest;
538 scops->safe_push (open_scop);
539 regions.release ();
542 else
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))
551 result.next = NULL;
553 if (exit_e->src->loop_father != loop)
554 result.next = NULL;
556 result.exits = false;
558 if (result.difficult)
559 move_sd_regions (&regions, scops);
560 else
561 regions.release ();
564 break;
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);
573 edge e;
574 int i;
575 build_scops_1 (bb, loop, &regions, 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.
580 The easiest case:
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
585 the loop.
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
595 build_scops_1. */
596 if (e->dest->loop_father->header == e->dest)
597 build_scops_1 (e->dest, outermost_loop, &regions,
598 loop_outer (e->dest->loop_father));
599 else
600 build_scops_1 (e->dest, outermost_loop, &regions,
601 e->dest->loop_father);
604 result.next = NULL;
605 result.exit = NULL;
606 result.difficult = true;
607 result.exits = false;
608 move_sd_regions (&regions, scops);
609 exits.release ();
610 break;
612 case GBB_COND_HEADER:
614 auto_vec<sd_region, 3> regions;
615 struct scopdet_info sinfo;
616 vec<basic_block> dominated;
617 int i;
618 basic_block dom_bb;
619 basic_block last_exit = NULL;
620 edge e;
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
628 body. */
629 if (loop_exits_to_bb_p (loop, e->dest))
631 result.exits = true;
632 continue;
635 /* Do not follow edges that lead to the end of the
636 conditions block. For example, in
639 | /|\
640 | 1 2 |
641 | | | |
642 | 3 4 |
643 | \|/
646 the edge from 0 => 6. Only check if all paths lead to
647 the same node 6. */
649 if (!single_pred_p (e->dest))
651 /* Check, if edge leads directly to the end of this
652 condition. */
653 if (!last_exit)
654 last_exit = e->dest;
656 if (e->dest != last_exit)
657 result.difficult = true;
659 continue;
662 if (!dominated_by_p (CDI_DOMINATORS, e->dest, bb))
664 result.difficult = true;
665 continue;
668 sinfo = build_scops_1 (e->dest, outermost_loop, &regions, 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. */
676 if (sinfo.exit)
678 if (!last_exit)
679 last_exit = sinfo.exit;
681 if (sinfo.exit != last_exit)
682 result.difficult = true;
684 else
685 result.difficult = true;
688 if (!last_exit)
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)
697 && last_exit != bb)
698 result.next = last_exit;
699 else
700 result.next = NULL;
702 result.exit = last_exit;
704 regions.release ();
705 break;
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))
715 < loop_depth (loop))
717 result.exits = true;
718 continue;
721 /* Ignore the bbs processed above. */
722 if (single_pred_p (dom_bb) && single_pred (dom_bb) == bb)
723 continue;
725 if (loop_depth (loop) > loop_depth (dom_bb->loop_father))
726 sinfo = build_scops_1 (dom_bb, outermost_loop, &regions,
727 loop_outer (loop));
728 else
729 sinfo = build_scops_1 (dom_bb, outermost_loop, &regions, loop);
731 result.exits |= sinfo.exits;
732 result.difficult = true;
733 result.exit = NULL;
736 dominated.release ();
738 result.next = NULL;
739 move_sd_regions (&regions, scops);
741 break;
744 default:
745 gcc_unreachable ();
748 return result;
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
757 up. */
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;
764 sd_region open_scop;
765 struct scopdet_info sinfo;
767 /* Initialize result. */
768 struct scopdet_info result;
769 result.exits = false;
770 result.difficult = false;
771 result.next = NULL;
772 result.exit = NULL;
773 open_scop.entry = NULL;
774 open_scop.exit = NULL;
775 sinfo.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;
789 in_scop = true;
791 else if (in_scop && (sinfo.exits || sinfo.difficult))
793 open_scop.exit = current;
794 scops->safe_push (open_scop);
795 in_scop = false;
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. */
805 if (in_scop)
807 open_scop.exit = sinfo.exit;
808 gcc_assert (open_scop.exit);
809 scops->safe_push (open_scop);
812 result.exit = sinfo.exit;
813 return result;
816 /* Checks if a bb is contained in REGION. */
818 static bool
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. */
826 static edge
827 find_single_entry_edge (sd_region *region)
829 edge e;
830 edge_iterator ei;
831 edge entry = NULL;
833 FOR_EACH_EDGE (e, ei, region->entry->preds)
834 if (!bb_in_sd_region (e->src, region))
836 if (entry)
838 entry = NULL;
839 break;
842 else
843 entry = e;
846 return entry;
849 /* Returns the single exit edge of REGION, if it does not exits NULL. */
851 static edge
852 find_single_exit_edge (sd_region *region)
854 edge e;
855 edge_iterator ei;
856 edge exit = NULL;
858 FOR_EACH_EDGE (e, ei, region->exit->preds)
859 if (bb_in_sd_region (e->src, region))
861 if (exit)
863 exit = NULL;
864 break;
867 else
868 exit = e;
871 return exit;
874 /* Create a single entry edge for REGION. */
876 static void
877 create_single_entry_edge (sd_region *region)
879 if (find_single_entry_edge (region))
880 return;
882 /* There are multiple predecessors for bb_3
884 | 1 2
885 | | /
886 | |/
887 | 3 <- entry
888 | |\
889 | | |
890 | 4 ^
891 | | |
892 | |/
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.
898 We split bb_3.
900 | 1 2
901 | | /
902 | |/
903 |3.0
904 | |\ (3.0 -> 3.1) = single entry edge
905 |3.1 | <- entry
906 | | |
907 | | |
908 | 4 ^
909 | | |
910 | |/
913 If the loop is part of the SCoP, we have to redirect the loop latches.
915 | 1 2
916 | | /
917 | |/
918 |3.0
919 | | (3.0 -> 3.1) = entry edge
920 |3.1 <- entry
921 | |\
922 | | |
923 | 4 ^
924 | | |
925 | |/
926 | 5 */
928 if (region->entry->loop_father->header != region->entry
929 || dominated_by_p (CDI_DOMINATORS,
930 loop_latch_edge (region->entry->loop_father)->src,
931 region->exit))
933 edge forwarder = split_block_after_labels (region->entry);
934 region->entry = forwarder->dest;
936 else
937 /* This case is never executed, as the loop headers seem always to have a
938 single edge pointing from outside into the loop. */
939 gcc_unreachable ();
941 gcc_checking_assert (find_single_entry_edge (region));
944 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
946 static bool
947 sd_region_without_exit (edge e)
949 sd_region *r = (sd_region *) e->aux;
951 if (r)
952 return r->exit == NULL;
953 else
954 return false;
957 /* Create a single exit edge for REGION. */
959 static void
960 create_single_exit_edge (sd_region *region)
962 edge e;
963 edge_iterator ei;
964 edge forwarder = NULL;
965 basic_block exit;
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
978 5 <- exit
980 changes to
982 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
983 | | \/ 3->5 no region, 4->5 no region,
984 | | 5
985 \| / 5->6 region->exit = 6
988 Now there is only a single exit edge (5->6). */
989 exit = region->exit;
990 region->exit = NULL;
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)
995 if (e->aux)
996 e->aux = NULL;
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
1002 forwarder->dest. */
1003 FOR_EACH_EDGE (e, ei, forwarder->dest->preds)
1004 if (e->aux)
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". */
1013 static void
1014 unmark_exit_edges (vec<sd_region> regions)
1016 int i;
1017 sd_region *s;
1018 edge e;
1019 edge_iterator ei;
1021 FOR_EACH_VEC_ELT (regions, i, s)
1022 FOR_EACH_EDGE (e, ei, s->exit->preds)
1023 e->aux = NULL;
1027 /* Mark the exit edges of all REGIONS.
1028 See comment in "create_single_exit_edge". */
1030 static void
1031 mark_exit_edges (vec<sd_region> regions)
1033 int i;
1034 sd_region *s;
1035 edge e;
1036 edge_iterator ei;
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))
1041 e->aux = s;
1044 /* Create for all scop regions a single entry and a single exit edge. */
1046 static void
1047 create_sese_edges (vec<sd_region> regions)
1049 int i;
1050 sd_region *s;
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);
1071 #endif
1074 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1076 static void
1077 build_graphite_scops (vec<sd_region> regions,
1078 vec<scop_p> *scops)
1080 int i;
1081 sd_region *s;
1083 FOR_EACH_VEC_ELT (regions, i, s)
1085 edge entry = find_single_entry_edge (s);
1086 edge exit = find_single_exit_edge (s);
1087 scop_p scop;
1089 if (!exit)
1090 continue;
1092 scop = new_scop (new_sese (entry, exit));
1093 scops->safe_push (scop);
1095 /* Are there overlapping SCoPs? */
1096 #ifdef ENABLE_CHECKING
1098 int j;
1099 sd_region *s2;
1101 FOR_EACH_VEC_ELT (regions, j, s2)
1102 if (s != s2)
1103 gcc_assert (!bb_in_sd_region (s->entry, s2));
1105 #endif
1109 /* Returns true when BB contains only close phi nodes. */
1111 static bool
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)
1118 return false;
1120 return true;
1123 /* Print statistics for SCOP to FILE. */
1125 static void
1126 print_graphite_scop_statistics (FILE* file, scop_p scop)
1128 long n_bbs = 0;
1129 long n_loops = 0;
1130 long n_stmts = 0;
1131 long n_conditions = 0;
1132 long n_p_bbs = 0;
1133 long n_p_loops = 0;
1134 long n_p_stmts = 0;
1135 long n_p_conditions = 0;
1137 basic_block bb;
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)))
1145 continue;
1147 n_bbs++;
1148 n_p_bbs += bb->count;
1150 if (EDGE_COUNT (bb->succs) > 1)
1152 n_conditions++;
1153 n_p_conditions += bb->count;
1156 for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
1158 n_stmts++;
1159 n_p_stmts += bb->count;
1162 if (loop->header == bb && loop_in_sese_p (loop, SCOP_REGION (scop)))
1164 n_loops++;
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. */
1184 static void
1185 print_graphite_statistics (FILE* file, vec<scop_p> scops)
1187 int i;
1188 scop_p scop;
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.
1196 Example:
1198 for (i |
1200 for (j | SCoP 1
1201 for (k |
1204 * SCoP frontier, as this line is not surrounded by any loop. *
1206 for (l | SCoP 2
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
1212 SCoP frontiers. */
1214 static void
1215 limit_scops (vec<scop_p> *scops)
1217 auto_vec<sd_region, 3> regions;
1219 int i;
1220 scop_p scop;
1222 FOR_EACH_VEC_ELT (*scops, i, scop)
1224 int j;
1225 loop_p loop;
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);
1248 scops->create (3);
1250 create_sese_edges (regions);
1251 build_graphite_scops (regions, scops);
1254 /* Returns true when P1 and P2 are close phis with the same
1255 argument. */
1257 static inline bool
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
1265 of PHI. */
1267 static void
1268 remove_duplicate_close_phi (gimple phi, gimple_stmt_iterator *gsi)
1270 gimple use_stmt;
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. */
1298 static void
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. */
1312 gsi_next (&gsi);
1313 while (!gsi_end_p (gsi))
1314 if (same_close_phi_node (phi, gsi_stmt (gsi)))
1315 remove_duplicate_close_phi (phi, &gsi);
1316 else
1317 gsi_next (&gsi);
1321 /* Transforms LOOP to the canonical loop closed SSA form. */
1323 static void
1324 canonicalize_loop_closed_ssa (loop_p loop)
1326 edge e = single_exit (loop);
1327 basic_block bb;
1329 if (!e || e->flags & EDGE_ABNORMAL)
1330 return;
1332 bb = e->dest;
1334 if (single_pred_p (bb))
1336 e = split_block_after_labels (bb);
1337 make_close_phi_nodes_unique (e->src);
1339 else
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);
1349 unsigned i;
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;
1356 gimple close_phi;
1358 if (TREE_CODE (arg) != SSA_NAME)
1359 continue;
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),
1366 UNKNOWN_LOCATION);
1367 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
1368 replace_exp (use_p, res);
1369 update_stmt (phi);
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
1394 in the loop.
1396 - the basic block containing the close phi nodes does not contain
1397 other statements.
1399 - there exist only one phi node per definition in the loop.
1402 static void
1403 canonicalize_loop_closed_ssa_form (void)
1405 loop_p loop;
1407 #ifdef ENABLE_CHECKING
1408 verify_loop_closed_ssa (true);
1409 #endif
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);
1419 #endif
1422 /* Find Static Control Parts (SCoP) in the current function and pushes
1423 them to SCOPS. */
1425 void
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,
1434 &regions, loop);
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);
1442 regions.release ();
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.
1453 Special nodes:
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. */
1459 static void
1460 dot_all_scops_1 (FILE *file, vec<scop_p> scops)
1462 basic_block bb;
1463 edge e;
1464 edge_iterator ei;
1465 scop_p scop;
1466 const char* color;
1467 int i;
1469 /* Disable debugging while printing graph. */
1470 int tmp_dump_flags = dump_flags;
1471 dump_flags = 0;
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\" ",
1483 bb->index);
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))
1494 switch (i % 17)
1496 case 0: /* red */
1497 color = "#e41a1c";
1498 break;
1499 case 1: /* blue */
1500 color = "#377eb8";
1501 break;
1502 case 2: /* green */
1503 color = "#4daf4a";
1504 break;
1505 case 3: /* purple */
1506 color = "#984ea3";
1507 break;
1508 case 4: /* orange */
1509 color = "#ff7f00";
1510 break;
1511 case 5: /* yellow */
1512 color = "#ffff33";
1513 break;
1514 case 6: /* brown */
1515 color = "#a65628";
1516 break;
1517 case 7: /* rose */
1518 color = "#f781bf";
1519 break;
1520 case 8:
1521 color = "#8dd3c7";
1522 break;
1523 case 9:
1524 color = "#ffffb3";
1525 break;
1526 case 10:
1527 color = "#bebada";
1528 break;
1529 case 11:
1530 color = "#fb8072";
1531 break;
1532 case 12:
1533 color = "#80b1d3";
1534 break;
1535 case 13:
1536 color = "#fdb462";
1537 break;
1538 case 14:
1539 color = "#b3de69";
1540 break;
1541 case 15:
1542 color = "#fccde5";
1543 break;
1544 case 16:
1545 color = "#bc80bd";
1546 break;
1547 default: /* gray */
1548 color = "#999999";
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);
1563 else
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;
1574 if (!part_of_scop)
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. */
1596 DEBUG_FUNCTION void
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". */
1601 #if 0
1602 int x;
1603 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1604 gcc_assert (stream);
1606 dot_all_scops_1 (stream, scops);
1607 fclose (stream);
1609 x = system ("dotty /tmp/allscops.dot &");
1610 #else
1611 dot_all_scops_1 (stderr, scops);
1612 #endif
1615 /* Display all SCoPs using dotty. */
1617 DEBUG_FUNCTION void
1618 dot_scop (scop_p scop)
1620 auto_vec<scop_p, 1> scops;
1622 if (scop)
1623 scops.safe_push (scop);
1625 /* When debugging, enable the following code. This cannot be used
1626 in production compilers because it calls "system". */
1627 #if 0
1629 int x;
1630 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1631 gcc_assert (stream);
1633 dot_all_scops_1 (stream, scops);
1634 fclose (stream);
1635 x = system ("dotty /tmp/allscops.dot &");
1637 #else
1638 dot_all_scops_1 (stderr, scops);
1639 #endif
1642 #endif