PR libstdc++/66354
[official-gcc.git] / gcc / graphite-scop-detection.c
blob02e9e502eae1c85ad86765a85cde15a7f7760f36
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)
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 #endif
30 #include "system.h"
31 #include "coretypes.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "vec.h"
35 #include "double-int.h"
36 #include "input.h"
37 #include "alias.h"
38 #include "symtab.h"
39 #include "options.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "fold-const.h"
44 #include "predict.h"
45 #include "tm.h"
46 #include "hard-reg-set.h"
47 #include "input.h"
48 #include "function.h"
49 #include "dominance.h"
50 #include "cfg.h"
51 #include "basic-block.h"
52 #include "tree-ssa-alias.h"
53 #include "internal-fn.h"
54 #include "gimple-expr.h"
55 #include "is-a.h"
56 #include "gimple.h"
57 #include "gimple-iterator.h"
58 #include "gimple-ssa.h"
59 #include "tree-phinodes.h"
60 #include "ssa-iterators.h"
61 #include "tree-ssa-loop-manip.h"
62 #include "tree-ssa-loop-niter.h"
63 #include "tree-ssa-loop.h"
64 #include "tree-into-ssa.h"
65 #include "tree-ssa.h"
66 #include "cfgloop.h"
67 #include "tree-chrec.h"
68 #include "tree-data-ref.h"
69 #include "tree-scalar-evolution.h"
70 #include "tree-pass.h"
71 #include "sese.h"
72 #include "tree-ssa-propagate.h"
74 #ifdef HAVE_isl
75 #include "graphite-poly.h"
76 #include "graphite-scop-detection.h"
78 /* Forward declarations. */
79 static void make_close_phi_nodes_unique (basic_block);
81 /* The type of the analyzed basic block. */
83 typedef enum gbb_type {
84 GBB_UNKNOWN,
85 GBB_LOOP_SING_EXIT_HEADER,
86 GBB_LOOP_MULT_EXIT_HEADER,
87 GBB_LOOP_EXIT,
88 GBB_COND_HEADER,
89 GBB_SIMPLE,
90 GBB_LAST
91 } gbb_type;
93 /* Detect the type of BB. Loop headers are only marked, if they are
94 new. This means their loop_father is different to LAST_LOOP.
95 Otherwise they are treated like any other bb and their type can be
96 any other type. */
98 static gbb_type
99 get_bb_type (basic_block bb, struct loop *last_loop)
101 vec<basic_block> dom;
102 int nb_dom;
103 struct loop *loop = bb->loop_father;
105 /* Check, if we entry into a new loop. */
106 if (loop != last_loop)
108 if (single_exit (loop) != NULL)
109 return GBB_LOOP_SING_EXIT_HEADER;
110 else if (loop->num != 0)
111 return GBB_LOOP_MULT_EXIT_HEADER;
112 else
113 return GBB_COND_HEADER;
116 dom = get_dominated_by (CDI_DOMINATORS, bb);
117 nb_dom = dom.length ();
118 dom.release ();
120 if (nb_dom == 0)
121 return GBB_LAST;
123 if (nb_dom == 1 && single_succ_p (bb))
124 return GBB_SIMPLE;
126 return GBB_COND_HEADER;
129 /* A SCoP detection region, defined using bbs as borders.
131 All control flow touching this region, comes in passing basic_block
132 ENTRY and leaves passing basic_block EXIT. By using bbs instead of
133 edges for the borders we are able to represent also regions that do
134 not have a single entry or exit edge.
136 But as they have a single entry basic_block and a single exit
137 basic_block, we are able to generate for every sd_region a single
138 entry and exit edge.
142 3 <- entry
145 / \ This region contains: {3, 4, 5, 6, 7, 8}
150 9 <- exit */
153 typedef struct sd_region_p
155 /* The entry bb dominates all bbs in the sd_region. It is part of
156 the region. */
157 basic_block entry;
159 /* The exit bb postdominates all bbs in the sd_region, but is not
160 part of the region. */
161 basic_block exit;
162 } sd_region;
166 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
168 static void
169 move_sd_regions (vec<sd_region> *source, vec<sd_region> *target)
171 sd_region *s;
172 int i;
174 FOR_EACH_VEC_ELT (*source, i, s)
175 target->safe_push (*s);
177 source->release ();
180 /* Something like "n * m" is not allowed. */
182 static bool
183 graphite_can_represent_init (tree e)
185 switch (TREE_CODE (e))
187 case POLYNOMIAL_CHREC:
188 return graphite_can_represent_init (CHREC_LEFT (e))
189 && graphite_can_represent_init (CHREC_RIGHT (e));
191 case MULT_EXPR:
192 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
193 return graphite_can_represent_init (TREE_OPERAND (e, 0))
194 && tree_fits_shwi_p (TREE_OPERAND (e, 1));
195 else
196 return graphite_can_represent_init (TREE_OPERAND (e, 1))
197 && tree_fits_shwi_p (TREE_OPERAND (e, 0));
199 case PLUS_EXPR:
200 case POINTER_PLUS_EXPR:
201 case MINUS_EXPR:
202 return graphite_can_represent_init (TREE_OPERAND (e, 0))
203 && graphite_can_represent_init (TREE_OPERAND (e, 1));
205 case NEGATE_EXPR:
206 case BIT_NOT_EXPR:
207 CASE_CONVERT:
208 case NON_LVALUE_EXPR:
209 return graphite_can_represent_init (TREE_OPERAND (e, 0));
211 default:
212 break;
215 return true;
218 /* Return true when SCEV can be represented in the polyhedral model.
220 An expression can be represented, if it can be expressed as an
221 affine expression. For loops (i, j) and parameters (m, n) all
222 affine expressions are of the form:
224 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
226 1 i + 20 j + (-2) m + 25
228 Something like "i * n" or "n * m" is not allowed. */
230 static bool
231 graphite_can_represent_scev (tree scev)
233 if (chrec_contains_undetermined (scev))
234 return false;
236 /* We disable the handling of pointer types, because it’s currently not
237 supported by Graphite with the ISL AST generator. SSA_NAME nodes are
238 the only nodes, which are disabled in case they are pointers to object
239 types, but this can be changed. */
241 if (POINTER_TYPE_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
242 return false;
244 switch (TREE_CODE (scev))
246 case NEGATE_EXPR:
247 case BIT_NOT_EXPR:
248 CASE_CONVERT:
249 case NON_LVALUE_EXPR:
250 return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
252 case PLUS_EXPR:
253 case POINTER_PLUS_EXPR:
254 case MINUS_EXPR:
255 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
256 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
258 case MULT_EXPR:
259 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
260 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
261 && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
262 && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
263 && graphite_can_represent_init (scev)
264 && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
265 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
267 case POLYNOMIAL_CHREC:
268 /* Check for constant strides. With a non constant stride of
269 'n' we would have a value of 'iv * n'. Also check that the
270 initial value can represented: for example 'n * m' cannot be
271 represented. */
272 if (!evolution_function_right_is_integer_cst (scev)
273 || !graphite_can_represent_init (scev))
274 return false;
275 return graphite_can_represent_scev (CHREC_LEFT (scev));
277 default:
278 break;
281 /* Only affine functions can be represented. */
282 if (tree_contains_chrecs (scev, NULL)
283 || !scev_is_linear_expression (scev))
284 return false;
286 return true;
290 /* Return true when EXPR can be represented in the polyhedral model.
292 This means an expression can be represented, if it is linear with
293 respect to the loops and the strides are non parametric.
294 LOOP is the place where the expr will be evaluated. SCOP_ENTRY defines the
295 entry of the region we analyse. */
297 static bool
298 graphite_can_represent_expr (basic_block scop_entry, loop_p loop,
299 tree expr)
301 tree scev = analyze_scalar_evolution (loop, expr);
303 scev = instantiate_scev (scop_entry, loop, scev);
305 return graphite_can_represent_scev (scev);
308 /* Return true if the data references of STMT can be represented by
309 Graphite. */
311 static bool
312 stmt_has_simple_data_refs_p (loop_p outermost_loop ATTRIBUTE_UNUSED,
313 gimple stmt)
315 data_reference_p dr;
316 unsigned i;
317 int j;
318 bool res = true;
319 vec<data_reference_p> drs = vNULL;
320 loop_p outer;
322 for (outer = loop_containing_stmt (stmt); outer; outer = loop_outer (outer))
324 graphite_find_data_references_in_stmt (outer,
325 loop_containing_stmt (stmt),
326 stmt, &drs);
328 FOR_EACH_VEC_ELT (drs, j, dr)
329 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
330 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i)))
332 res = false;
333 goto done;
336 free_data_refs (drs);
337 drs.create (0);
340 done:
341 free_data_refs (drs);
342 return res;
345 /* Return true only when STMT is simple enough for being handled by
346 Graphite. This depends on SCOP_ENTRY, as the parameters are
347 initialized relatively to this basic block, the linear functions
348 are initialized to OUTERMOST_LOOP and BB is the place where we try
349 to evaluate the STMT. */
351 static bool
352 stmt_simple_for_scop_p (basic_block scop_entry, loop_p outermost_loop,
353 gimple stmt, basic_block bb)
355 loop_p loop = bb->loop_father;
357 gcc_assert (scop_entry);
359 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
360 Calls have side-effects, except those to const or pure
361 functions. */
362 if (gimple_has_volatile_ops (stmt)
363 || (gimple_code (stmt) == GIMPLE_CALL
364 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
365 || (gimple_code (stmt) == GIMPLE_ASM))
366 return false;
368 if (is_gimple_debug (stmt))
369 return true;
371 if (!stmt_has_simple_data_refs_p (outermost_loop, stmt))
372 return false;
374 switch (gimple_code (stmt))
376 case GIMPLE_RETURN:
377 case GIMPLE_LABEL:
378 return true;
380 case GIMPLE_COND:
382 /* We can handle all binary comparisons. Inequalities are
383 also supported as they can be represented with union of
384 polyhedra. */
385 enum tree_code code = gimple_cond_code (stmt);
386 if (!(code == LT_EXPR
387 || code == GT_EXPR
388 || code == LE_EXPR
389 || code == GE_EXPR
390 || code == EQ_EXPR
391 || code == NE_EXPR))
392 return false;
394 for (unsigned i = 0; i < 2; ++i)
396 tree op = gimple_op (stmt, i);
397 if (!graphite_can_represent_expr (scop_entry, loop, op)
398 /* We can not handle REAL_TYPE. Failed for pr39260. */
399 || TREE_CODE (TREE_TYPE (op)) == REAL_TYPE)
400 return false;
403 return true;
406 case GIMPLE_ASSIGN:
407 case GIMPLE_CALL:
408 return true;
410 default:
411 /* These nodes cut a new scope. */
412 return false;
415 return false;
418 /* Returns the statement of BB that contains a harmful operation: that
419 can be a function call with side effects, the induction variables
420 are not linear with respect to SCOP_ENTRY, etc. The current open
421 scop should end before this statement. The evaluation is limited using
422 OUTERMOST_LOOP as outermost loop that may change. */
424 static gimple
425 harmful_stmt_in_bb (basic_block scop_entry, loop_p outer_loop, basic_block bb)
427 gimple_stmt_iterator gsi;
429 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
430 if (!stmt_simple_for_scop_p (scop_entry, outer_loop, gsi_stmt (gsi), bb))
431 return gsi_stmt (gsi);
433 return NULL;
436 /* Return true if LOOP can be represented in the polyhedral
437 representation. This is evaluated taking SCOP_ENTRY and
438 OUTERMOST_LOOP in mind. */
440 static bool
441 graphite_can_represent_loop (basic_block scop_entry, loop_p loop)
443 tree niter;
444 struct tree_niter_desc niter_desc;
446 /* FIXME: For the moment, graphite cannot be used on loops that
447 iterate using induction variables that wrap. */
449 return number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
450 && niter_desc.control.no_overflow
451 && (niter = number_of_latch_executions (loop))
452 && !chrec_contains_undetermined (niter)
453 && graphite_can_represent_expr (scop_entry, loop, niter);
456 /* Store information needed by scopdet_* functions. */
458 struct scopdet_info
460 /* Exit of the open scop would stop if the current BB is harmful. */
461 basic_block exit;
463 /* Where the next scop would start if the current BB is harmful. */
464 basic_block next;
466 /* The bb or one of its children contains open loop exits. That means
467 loop exit nodes that are not surrounded by a loop dominated by bb. */
468 bool exits;
470 /* The bb or one of its children contains only structures we can handle. */
471 bool difficult;
474 static struct scopdet_info build_scops_1 (basic_block, loop_p,
475 vec<sd_region> *, loop_p);
477 /* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
478 to SCOPS. TYPE is the gbb_type of BB. */
480 static struct scopdet_info
481 scopdet_basic_block_info (basic_block bb, loop_p outermost_loop,
482 vec<sd_region> *scops, gbb_type type)
484 loop_p loop = bb->loop_father;
485 struct scopdet_info result;
486 gimple stmt;
488 /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps. */
489 basic_block entry_block = ENTRY_BLOCK_PTR_FOR_FN (cfun);
490 stmt = harmful_stmt_in_bb (entry_block, outermost_loop, bb);
491 result.difficult = (stmt != NULL);
492 result.exit = NULL;
494 switch (type)
496 case GBB_LAST:
497 result.next = NULL;
498 result.exits = false;
500 /* Mark bbs terminating a SESE region difficult, if they start
501 a condition or if the block it exits to cannot be split
502 with make_forwarder_block. */
503 if (!single_succ_p (bb)
504 || bb_has_abnormal_pred (single_succ (bb)))
505 result.difficult = true;
506 else
507 result.exit = single_succ (bb);
509 break;
511 case GBB_SIMPLE:
512 result.next = single_succ (bb);
513 result.exits = false;
514 result.exit = single_succ (bb);
515 break;
517 case GBB_LOOP_SING_EXIT_HEADER:
519 auto_vec<sd_region, 3> regions;
520 struct scopdet_info sinfo;
521 edge exit_e = single_exit (loop);
523 sinfo = build_scops_1 (bb, outermost_loop, &regions, loop);
525 if (!graphite_can_represent_loop (entry_block, loop))
526 result.difficult = true;
528 result.difficult |= sinfo.difficult;
530 /* Try again with another loop level. */
531 if (result.difficult
532 && loop_depth (outermost_loop) + 1 == loop_depth (loop))
534 outermost_loop = loop;
536 regions.release ();
537 regions.create (3);
539 sinfo = scopdet_basic_block_info (bb, outermost_loop, scops, type);
541 result = sinfo;
542 result.difficult = true;
544 if (sinfo.difficult)
545 move_sd_regions (&regions, scops);
546 else
548 sd_region open_scop;
549 open_scop.entry = bb;
550 open_scop.exit = exit_e->dest;
551 scops->safe_push (open_scop);
552 regions.release ();
555 else
557 result.exit = exit_e->dest;
558 result.next = exit_e->dest;
560 /* If we do not dominate result.next, remove it. It's either
561 the exit block, or another bb dominates it and will
562 call the scop detection for this bb. */
563 if (!dominated_by_p (CDI_DOMINATORS, result.next, bb))
564 result.next = NULL;
566 if (exit_e->src->loop_father != loop)
567 result.next = NULL;
569 result.exits = false;
571 if (result.difficult)
572 move_sd_regions (&regions, scops);
573 else
574 regions.release ();
577 break;
580 case GBB_LOOP_MULT_EXIT_HEADER:
582 /* XXX: For now we just do not join loops with multiple exits. If the
583 exits lead to the same bb it may be possible to join the loop. */
584 auto_vec<sd_region, 3> regions;
585 vec<edge> exits = get_loop_exit_edges (loop);
586 edge e;
587 int i;
588 build_scops_1 (bb, loop, &regions, loop);
590 /* Scan the code dominated by this loop. This means all bbs, that are
591 are dominated by a bb in this loop, but are not part of this loop.
593 The easiest case:
594 - The loop exit destination is dominated by the exit sources.
596 TODO: We miss here the more complex cases:
597 - The exit destinations are dominated by another bb inside
598 the loop.
599 - The loop dominates bbs, that are not exit destinations. */
600 FOR_EACH_VEC_ELT (exits, i, e)
601 if (e->src->loop_father == loop
602 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src))
604 if (loop_outer (outermost_loop))
605 outermost_loop = loop_outer (outermost_loop);
607 /* Pass loop_outer to recognize e->dest as loop header in
608 build_scops_1. */
609 if (e->dest->loop_father->header == e->dest)
610 build_scops_1 (e->dest, outermost_loop, &regions,
611 loop_outer (e->dest->loop_father));
612 else
613 build_scops_1 (e->dest, outermost_loop, &regions,
614 e->dest->loop_father);
617 result.next = NULL;
618 result.exit = NULL;
619 result.difficult = true;
620 result.exits = false;
621 move_sd_regions (&regions, scops);
622 exits.release ();
623 break;
625 case GBB_COND_HEADER:
627 auto_vec<sd_region, 3> regions;
628 struct scopdet_info sinfo;
629 vec<basic_block> dominated;
630 int i;
631 basic_block dom_bb;
632 basic_block last_exit = NULL;
633 edge e;
634 result.exits = false;
636 /* First check the successors of BB, and check if it is
637 possible to join the different branches. */
638 FOR_EACH_VEC_SAFE_ELT (bb->succs, i, e)
640 /* Ignore loop exits. They will be handled after the loop
641 body. */
642 if (loop_exits_to_bb_p (loop, e->dest))
644 result.exits = true;
645 continue;
648 /* Do not follow edges that lead to the end of the
649 conditions block. For example, in
652 | /|\
653 | 1 2 |
654 | | | |
655 | 3 4 |
656 | \|/
659 the edge from 0 => 6. Only check if all paths lead to
660 the same node 6. */
662 if (!single_pred_p (e->dest))
664 /* Check, if edge leads directly to the end of this
665 condition. */
666 if (!last_exit)
667 last_exit = e->dest;
669 if (e->dest != last_exit)
670 result.difficult = true;
672 continue;
675 if (!dominated_by_p (CDI_DOMINATORS, e->dest, bb))
677 result.difficult = true;
678 continue;
681 sinfo = build_scops_1 (e->dest, outermost_loop, &regions, loop);
683 result.exits |= sinfo.exits;
684 result.difficult |= sinfo.difficult;
686 /* Checks, if all branches end at the same point.
687 If that is true, the condition stays joinable.
688 Have a look at the example above. */
689 if (sinfo.exit)
691 if (!last_exit)
692 last_exit = sinfo.exit;
694 if (sinfo.exit != last_exit)
695 result.difficult = true;
697 else
698 result.difficult = true;
701 if (!last_exit)
702 result.difficult = true;
704 /* Join the branches of the condition if possible. */
705 if (!result.exits && !result.difficult)
707 /* Only return a next pointer if we dominate this pointer.
708 Otherwise it will be handled by the bb dominating it. */
709 if (dominated_by_p (CDI_DOMINATORS, last_exit, bb)
710 && last_exit != bb)
711 result.next = last_exit;
712 else
713 result.next = NULL;
715 result.exit = last_exit;
717 regions.release ();
718 break;
721 /* Scan remaining bbs dominated by BB. */
722 dominated = get_dominated_by (CDI_DOMINATORS, bb);
724 FOR_EACH_VEC_ELT (dominated, i, dom_bb)
726 /* Ignore loop exits: they will be handled after the loop body. */
727 if (loop_depth (find_common_loop (loop, dom_bb->loop_father))
728 < loop_depth (loop))
730 result.exits = true;
731 continue;
734 /* Ignore the bbs processed above. */
735 if (single_pred_p (dom_bb) && single_pred (dom_bb) == bb)
736 continue;
738 if (loop_depth (loop) > loop_depth (dom_bb->loop_father))
739 sinfo = build_scops_1 (dom_bb, outermost_loop, &regions,
740 loop_outer (loop));
741 else
742 sinfo = build_scops_1 (dom_bb, outermost_loop, &regions, loop);
744 result.exits |= sinfo.exits;
745 result.difficult = true;
746 result.exit = NULL;
749 dominated.release ();
751 result.next = NULL;
752 move_sd_regions (&regions, scops);
754 break;
757 default:
758 gcc_unreachable ();
761 return result;
764 /* Starting from CURRENT we walk the dominance tree and add new sd_regions to
765 SCOPS. The analyse if a sd_region can be handled is based on the value
766 of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change. LOOP
767 is the loop in which CURRENT is handled.
769 TODO: These functions got a little bit big. They definitely should be cleaned
770 up. */
772 static struct scopdet_info
773 build_scops_1 (basic_block current, loop_p outermost_loop,
774 vec<sd_region> *scops, loop_p loop)
776 bool in_scop = false;
777 sd_region open_scop;
778 struct scopdet_info sinfo;
780 /* Initialize result. */
781 struct scopdet_info result;
782 result.exits = false;
783 result.difficult = false;
784 result.next = NULL;
785 result.exit = NULL;
786 open_scop.entry = NULL;
787 open_scop.exit = NULL;
788 sinfo.exit = NULL;
790 /* Loop over the dominance tree. If we meet a difficult bb, close
791 the current SCoP. Loop and condition header start a new layer,
792 and can only be added if all bbs in deeper layers are simple. */
793 while (current != NULL)
795 sinfo = scopdet_basic_block_info (current, outermost_loop, scops,
796 get_bb_type (current, loop));
798 if (!in_scop && !(sinfo.exits || sinfo.difficult))
800 open_scop.entry = current;
801 open_scop.exit = NULL;
802 in_scop = true;
804 else if (in_scop && (sinfo.exits || sinfo.difficult))
806 open_scop.exit = current;
807 scops->safe_push (open_scop);
808 in_scop = false;
811 result.difficult |= sinfo.difficult;
812 result.exits |= sinfo.exits;
814 current = sinfo.next;
817 /* Try to close open_scop, if we are still in an open SCoP. */
818 if (in_scop)
820 open_scop.exit = sinfo.exit;
821 gcc_assert (open_scop.exit);
822 scops->safe_push (open_scop);
825 result.exit = sinfo.exit;
826 return result;
829 /* Checks if a bb is contained in REGION. */
831 static bool
832 bb_in_sd_region (basic_block bb, sd_region *region)
834 return bb_in_region (bb, region->entry, region->exit);
837 /* Returns the single entry edge of REGION, if it does not exits NULL. */
839 static edge
840 find_single_entry_edge (sd_region *region)
842 edge e;
843 edge_iterator ei;
844 edge entry = NULL;
846 FOR_EACH_EDGE (e, ei, region->entry->preds)
847 if (!bb_in_sd_region (e->src, region))
849 if (entry)
851 entry = NULL;
852 break;
855 else
856 entry = e;
859 return entry;
862 /* Returns the single exit edge of REGION, if it does not exits NULL. */
864 static edge
865 find_single_exit_edge (sd_region *region)
867 edge e;
868 edge_iterator ei;
869 edge exit = NULL;
871 FOR_EACH_EDGE (e, ei, region->exit->preds)
872 if (bb_in_sd_region (e->src, region))
874 if (exit)
876 exit = NULL;
877 break;
880 else
881 exit = e;
884 return exit;
887 /* Create a single entry edge for REGION. */
889 static void
890 create_single_entry_edge (sd_region *region)
892 if (find_single_entry_edge (region))
893 return;
895 /* There are multiple predecessors for bb_3
897 | 1 2
898 | | /
899 | |/
900 | 3 <- entry
901 | |\
902 | | |
903 | 4 ^
904 | | |
905 | |/
908 There are two edges (1->3, 2->3), that point from outside into the region,
909 and another one (5->3), a loop latch, lead to bb_3.
911 We split bb_3.
913 | 1 2
914 | | /
915 | |/
916 |3.0
917 | |\ (3.0 -> 3.1) = single entry edge
918 |3.1 | <- entry
919 | | |
920 | | |
921 | 4 ^
922 | | |
923 | |/
926 If the loop is part of the SCoP, we have to redirect the loop latches.
928 | 1 2
929 | | /
930 | |/
931 |3.0
932 | | (3.0 -> 3.1) = entry edge
933 |3.1 <- entry
934 | |\
935 | | |
936 | 4 ^
937 | | |
938 | |/
939 | 5 */
941 if (region->entry->loop_father->header != region->entry
942 || dominated_by_p (CDI_DOMINATORS,
943 loop_latch_edge (region->entry->loop_father)->src,
944 region->exit))
946 edge forwarder = split_block_after_labels (region->entry);
947 region->entry = forwarder->dest;
949 else
950 /* This case is never executed, as the loop headers seem always to have a
951 single edge pointing from outside into the loop. */
952 gcc_unreachable ();
954 gcc_checking_assert (find_single_entry_edge (region));
957 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
959 static bool
960 sd_region_without_exit (edge e)
962 sd_region *r = (sd_region *) e->aux;
964 if (r)
965 return r->exit == NULL;
966 else
967 return false;
970 /* Create a single exit edge for REGION. */
972 static void
973 create_single_exit_edge (sd_region *region)
975 edge e;
976 edge_iterator ei;
977 edge forwarder = NULL;
978 basic_block exit;
980 /* We create a forwarder bb (5) for all edges leaving this region
981 (3->5, 4->5). All other edges leading to the same bb, are moved
982 to a new bb (6). If these edges where part of another region (2->5)
983 we update the region->exit pointer, of this region.
985 To identify which edge belongs to which region we depend on the e->aux
986 pointer in every edge. It points to the region of the edge or to NULL,
987 if the edge is not part of any region.
989 1 2 3 4 1->5 no region, 2->5 region->exit = 5,
990 \| |/ 3->5 region->exit = NULL, 4->5 region->exit = NULL
991 5 <- exit
993 changes to
995 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
996 | | \/ 3->5 no region, 4->5 no region,
997 | | 5
998 \| / 5->6 region->exit = 6
1001 Now there is only a single exit edge (5->6). */
1002 exit = region->exit;
1003 region->exit = NULL;
1004 forwarder = make_forwarder_block (exit, &sd_region_without_exit, NULL);
1006 /* Unmark the edges, that are no longer exit edges. */
1007 FOR_EACH_EDGE (e, ei, forwarder->src->preds)
1008 if (e->aux)
1009 e->aux = NULL;
1011 /* Mark the new exit edge. */
1012 single_succ_edge (forwarder->src)->aux = region;
1014 /* Update the exit bb of all regions, where exit edges lead to
1015 forwarder->dest. */
1016 FOR_EACH_EDGE (e, ei, forwarder->dest->preds)
1017 if (e->aux)
1018 ((sd_region *) e->aux)->exit = forwarder->dest;
1020 gcc_checking_assert (find_single_exit_edge (region));
1023 /* Unmark the exit edges of all REGIONS.
1024 See comment in "create_single_exit_edge". */
1026 static void
1027 unmark_exit_edges (vec<sd_region> regions)
1029 int i;
1030 sd_region *s;
1031 edge e;
1032 edge_iterator ei;
1034 FOR_EACH_VEC_ELT (regions, i, s)
1035 FOR_EACH_EDGE (e, ei, s->exit->preds)
1036 e->aux = NULL;
1040 /* Mark the exit edges of all REGIONS.
1041 See comment in "create_single_exit_edge". */
1043 static void
1044 mark_exit_edges (vec<sd_region> regions)
1046 int i;
1047 sd_region *s;
1048 edge e;
1049 edge_iterator ei;
1051 FOR_EACH_VEC_ELT (regions, i, s)
1052 FOR_EACH_EDGE (e, ei, s->exit->preds)
1053 if (bb_in_sd_region (e->src, s))
1054 e->aux = s;
1057 /* Create for all scop regions a single entry and a single exit edge. */
1059 static void
1060 create_sese_edges (vec<sd_region> regions)
1062 int i;
1063 sd_region *s;
1065 FOR_EACH_VEC_ELT (regions, i, s)
1066 create_single_entry_edge (s);
1068 mark_exit_edges (regions);
1070 FOR_EACH_VEC_ELT (regions, i, s)
1071 /* Don't handle multiple edges exiting the function. */
1072 if (!find_single_exit_edge (s)
1073 && s->exit != EXIT_BLOCK_PTR_FOR_FN (cfun))
1074 create_single_exit_edge (s);
1076 unmark_exit_edges (regions);
1078 calculate_dominance_info (CDI_DOMINATORS);
1079 fix_loop_structure (NULL);
1081 #ifdef ENABLE_CHECKING
1082 verify_loop_structure ();
1083 verify_ssa (false, true);
1084 #endif
1087 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1089 static void
1090 build_graphite_scops (vec<sd_region> regions,
1091 vec<scop_p> *scops)
1093 int i;
1094 sd_region *s;
1096 FOR_EACH_VEC_ELT (regions, i, s)
1098 edge entry = find_single_entry_edge (s);
1099 edge exit = find_single_exit_edge (s);
1100 scop_p scop;
1102 if (!exit)
1103 continue;
1105 scop = new_scop (new_sese (entry, exit));
1106 scops->safe_push (scop);
1108 /* Are there overlapping SCoPs? */
1109 #ifdef ENABLE_CHECKING
1111 int j;
1112 sd_region *s2;
1114 FOR_EACH_VEC_ELT (regions, j, s2)
1115 if (s != s2)
1116 gcc_assert (!bb_in_sd_region (s->entry, s2));
1118 #endif
1122 /* Returns true when BB contains only close phi nodes. */
1124 static bool
1125 contains_only_close_phi_nodes (basic_block bb)
1127 gimple_stmt_iterator gsi;
1129 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1130 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
1131 return false;
1133 return true;
1136 /* Print statistics for SCOP to FILE. */
1138 static void
1139 print_graphite_scop_statistics (FILE* file, scop_p scop)
1141 long n_bbs = 0;
1142 long n_loops = 0;
1143 long n_stmts = 0;
1144 long n_conditions = 0;
1145 long n_p_bbs = 0;
1146 long n_p_loops = 0;
1147 long n_p_stmts = 0;
1148 long n_p_conditions = 0;
1150 basic_block bb;
1152 FOR_ALL_BB_FN (bb, cfun)
1154 gimple_stmt_iterator psi;
1155 loop_p loop = bb->loop_father;
1157 if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
1158 continue;
1160 n_bbs++;
1161 n_p_bbs += bb->count;
1163 if (EDGE_COUNT (bb->succs) > 1)
1165 n_conditions++;
1166 n_p_conditions += bb->count;
1169 for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
1171 n_stmts++;
1172 n_p_stmts += bb->count;
1175 if (loop->header == bb && loop_in_sese_p (loop, SCOP_REGION (scop)))
1177 n_loops++;
1178 n_p_loops += bb->count;
1183 fprintf (file, "\nBefore limit_scops SCoP statistics (");
1184 fprintf (file, "BBS:%ld, ", n_bbs);
1185 fprintf (file, "LOOPS:%ld, ", n_loops);
1186 fprintf (file, "CONDITIONS:%ld, ", n_conditions);
1187 fprintf (file, "STMTS:%ld)\n", n_stmts);
1188 fprintf (file, "\nBefore limit_scops SCoP profiling statistics (");
1189 fprintf (file, "BBS:%ld, ", n_p_bbs);
1190 fprintf (file, "LOOPS:%ld, ", n_p_loops);
1191 fprintf (file, "CONDITIONS:%ld, ", n_p_conditions);
1192 fprintf (file, "STMTS:%ld)\n", n_p_stmts);
1195 /* Print statistics for SCOPS to FILE. */
1197 static void
1198 print_graphite_statistics (FILE* file, vec<scop_p> scops)
1200 int i;
1201 scop_p scop;
1203 FOR_EACH_VEC_ELT (scops, i, scop)
1204 print_graphite_scop_statistics (file, scop);
1207 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
1209 Example:
1211 for (i |
1213 for (j | SCoP 1
1214 for (k |
1217 * SCoP frontier, as this line is not surrounded by any loop. *
1219 for (l | SCoP 2
1221 This is necessary as scalar evolution and parameter detection need a
1222 outermost loop to initialize parameters correctly.
1224 TODO: FIX scalar evolution and parameter detection to allow more flexible
1225 SCoP frontiers. */
1227 static void
1228 limit_scops (vec<scop_p> *scops)
1230 auto_vec<sd_region, 3> regions;
1232 int i;
1233 scop_p scop;
1235 FOR_EACH_VEC_ELT (*scops, i, scop)
1237 int j;
1238 loop_p loop;
1239 sese region = SCOP_REGION (scop);
1240 build_sese_loop_nests (region);
1242 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), j, loop)
1243 if (!loop_in_sese_p (loop_outer (loop), region)
1244 && single_exit (loop))
1246 sd_region open_scop;
1247 open_scop.entry = loop->header;
1248 open_scop.exit = single_exit (loop)->dest;
1250 /* This is a hack on top of the limit_scops hack. The
1251 limit_scops hack should disappear all together. */
1252 if (single_succ_p (open_scop.exit)
1253 && contains_only_close_phi_nodes (open_scop.exit))
1254 open_scop.exit = single_succ_edge (open_scop.exit)->dest;
1256 regions.safe_push (open_scop);
1260 free_scops (*scops);
1261 scops->create (3);
1263 create_sese_edges (regions);
1264 build_graphite_scops (regions, scops);
1267 /* Returns true when P1 and P2 are close phis with the same
1268 argument. */
1270 static inline bool
1271 same_close_phi_node (gphi *p1, gphi *p2)
1273 return operand_equal_p (gimple_phi_arg_def (p1, 0),
1274 gimple_phi_arg_def (p2, 0), 0);
1277 /* Remove the close phi node at GSI and replace its rhs with the rhs
1278 of PHI. */
1280 static void
1281 remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
1283 gimple use_stmt;
1284 use_operand_p use_p;
1285 imm_use_iterator imm_iter;
1286 tree res = gimple_phi_result (phi);
1287 tree def = gimple_phi_result (gsi->phi ());
1289 gcc_assert (same_close_phi_node (phi, gsi->phi ()));
1291 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
1293 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1294 SET_USE (use_p, res);
1296 update_stmt (use_stmt);
1298 /* It is possible that we just created a duplicate close-phi
1299 for an already-processed containing loop. Check for this
1300 case and clean it up. */
1301 if (gimple_code (use_stmt) == GIMPLE_PHI
1302 && gimple_phi_num_args (use_stmt) == 1)
1303 make_close_phi_nodes_unique (gimple_bb (use_stmt));
1306 remove_phi_node (gsi, true);
1309 /* Removes all the close phi duplicates from BB. */
1311 static void
1312 make_close_phi_nodes_unique (basic_block bb)
1314 gphi_iterator psi;
1316 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1318 gphi_iterator gsi = psi;
1319 gphi *phi = psi.phi ();
1321 /* At this point, PHI should be a close phi in normal form. */
1322 gcc_assert (gimple_phi_num_args (phi) == 1);
1324 /* Iterate over the next phis and remove duplicates. */
1325 gsi_next (&gsi);
1326 while (!gsi_end_p (gsi))
1327 if (same_close_phi_node (phi, gsi.phi ()))
1328 remove_duplicate_close_phi (phi, &gsi);
1329 else
1330 gsi_next (&gsi);
1334 /* Transforms LOOP to the canonical loop closed SSA form. */
1336 static void
1337 canonicalize_loop_closed_ssa (loop_p loop)
1339 edge e = single_exit (loop);
1340 basic_block bb;
1342 if (!e || e->flags & EDGE_ABNORMAL)
1343 return;
1345 bb = e->dest;
1347 if (single_pred_p (bb))
1349 e = split_block_after_labels (bb);
1350 make_close_phi_nodes_unique (e->src);
1352 else
1354 gphi_iterator psi;
1355 basic_block close = split_edge (e);
1357 e = single_succ_edge (close);
1359 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1361 gphi *phi = psi.phi ();
1362 unsigned i;
1364 for (i = 0; i < gimple_phi_num_args (phi); i++)
1365 if (gimple_phi_arg_edge (phi, i) == e)
1367 tree res, arg = gimple_phi_arg_def (phi, i);
1368 use_operand_p use_p;
1369 gphi *close_phi;
1371 if (TREE_CODE (arg) != SSA_NAME)
1372 continue;
1374 close_phi = create_phi_node (NULL_TREE, close);
1375 res = create_new_def_for (arg, close_phi,
1376 gimple_phi_result_ptr (close_phi));
1377 add_phi_arg (close_phi, arg,
1378 gimple_phi_arg_edge (close_phi, 0),
1379 UNKNOWN_LOCATION);
1380 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
1381 replace_exp (use_p, res);
1382 update_stmt (phi);
1386 make_close_phi_nodes_unique (close);
1389 /* The code above does not properly handle changes in the post dominance
1390 information (yet). */
1391 free_dominance_info (CDI_POST_DOMINATORS);
1394 /* Converts the current loop closed SSA form to a canonical form
1395 expected by the Graphite code generation.
1397 The loop closed SSA form has the following invariant: a variable
1398 defined in a loop that is used outside the loop appears only in the
1399 phi nodes in the destination of the loop exit. These phi nodes are
1400 called close phi nodes.
1402 The canonical loop closed SSA form contains the extra invariants:
1404 - when the loop contains only one exit, the close phi nodes contain
1405 only one argument. That implies that the basic block that contains
1406 the close phi nodes has only one predecessor, that is a basic block
1407 in the loop.
1409 - the basic block containing the close phi nodes does not contain
1410 other statements.
1412 - there exist only one phi node per definition in the loop.
1415 static void
1416 canonicalize_loop_closed_ssa_form (void)
1418 loop_p loop;
1420 #ifdef ENABLE_CHECKING
1421 verify_loop_closed_ssa (true);
1422 #endif
1424 FOR_EACH_LOOP (loop, 0)
1425 canonicalize_loop_closed_ssa (loop);
1427 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1428 update_ssa (TODO_update_ssa);
1430 #ifdef ENABLE_CHECKING
1431 verify_loop_closed_ssa (true);
1432 #endif
1435 /* Find Static Control Parts (SCoP) in the current function and pushes
1436 them to SCOPS. */
1438 void
1439 build_scops (vec<scop_p> *scops)
1441 struct loop *loop = current_loops->tree_root;
1442 auto_vec<sd_region, 3> regions;
1444 canonicalize_loop_closed_ssa_form ();
1445 build_scops_1 (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1446 ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father,
1447 &regions, loop);
1448 create_sese_edges (regions);
1449 build_graphite_scops (regions, scops);
1451 if (dump_file && (dump_flags & TDF_DETAILS))
1452 print_graphite_statistics (dump_file, *scops);
1454 limit_scops (scops);
1455 regions.release ();
1457 if (dump_file && (dump_flags & TDF_DETAILS))
1458 fprintf (dump_file, "\nnumber of SCoPs: %d\n",
1459 scops ? scops->length () : 0);
1462 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1463 different colors. If there are not enough colors, paint the
1464 remaining SCoPs in gray.
1466 Special nodes:
1467 - "*" after the node number denotes the entry of a SCoP,
1468 - "#" after the node number denotes the exit of a SCoP,
1469 - "()" around the node number denotes the entry or the
1470 exit nodes of the SCOP. These are not part of SCoP. */
1472 static void
1473 dot_all_scops_1 (FILE *file, vec<scop_p> scops)
1475 basic_block bb;
1476 edge e;
1477 edge_iterator ei;
1478 scop_p scop;
1479 const char* color;
1480 int i;
1482 /* Disable debugging while printing graph. */
1483 int tmp_dump_flags = dump_flags;
1484 dump_flags = 0;
1486 fprintf (file, "digraph all {\n");
1488 FOR_ALL_BB_FN (bb, cfun)
1490 int part_of_scop = false;
1492 /* Use HTML for every bb label. So we are able to print bbs
1493 which are part of two different SCoPs, with two different
1494 background colors. */
1495 fprintf (file, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1496 bb->index);
1497 fprintf (file, "CELLSPACING=\"0\">\n");
1499 /* Select color for SCoP. */
1500 FOR_EACH_VEC_ELT (scops, i, scop)
1502 sese region = SCOP_REGION (scop);
1503 if (bb_in_sese_p (bb, region)
1504 || (SESE_EXIT_BB (region) == bb)
1505 || (SESE_ENTRY_BB (region) == bb))
1507 switch (i % 17)
1509 case 0: /* red */
1510 color = "#e41a1c";
1511 break;
1512 case 1: /* blue */
1513 color = "#377eb8";
1514 break;
1515 case 2: /* green */
1516 color = "#4daf4a";
1517 break;
1518 case 3: /* purple */
1519 color = "#984ea3";
1520 break;
1521 case 4: /* orange */
1522 color = "#ff7f00";
1523 break;
1524 case 5: /* yellow */
1525 color = "#ffff33";
1526 break;
1527 case 6: /* brown */
1528 color = "#a65628";
1529 break;
1530 case 7: /* rose */
1531 color = "#f781bf";
1532 break;
1533 case 8:
1534 color = "#8dd3c7";
1535 break;
1536 case 9:
1537 color = "#ffffb3";
1538 break;
1539 case 10:
1540 color = "#bebada";
1541 break;
1542 case 11:
1543 color = "#fb8072";
1544 break;
1545 case 12:
1546 color = "#80b1d3";
1547 break;
1548 case 13:
1549 color = "#fdb462";
1550 break;
1551 case 14:
1552 color = "#b3de69";
1553 break;
1554 case 15:
1555 color = "#fccde5";
1556 break;
1557 case 16:
1558 color = "#bc80bd";
1559 break;
1560 default: /* gray */
1561 color = "#999999";
1564 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
1566 if (!bb_in_sese_p (bb, region))
1567 fprintf (file, " (");
1569 if (bb == SESE_ENTRY_BB (region)
1570 && bb == SESE_EXIT_BB (region))
1571 fprintf (file, " %d*# ", bb->index);
1572 else if (bb == SESE_ENTRY_BB (region))
1573 fprintf (file, " %d* ", bb->index);
1574 else if (bb == SESE_EXIT_BB (region))
1575 fprintf (file, " %d# ", bb->index);
1576 else
1577 fprintf (file, " %d ", bb->index);
1579 if (!bb_in_sese_p (bb,region))
1580 fprintf (file, ")");
1582 fprintf (file, "</TD></TR>\n");
1583 part_of_scop = true;
1587 if (!part_of_scop)
1589 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1590 fprintf (file, " %d </TD></TR>\n", bb->index);
1592 fprintf (file, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1595 FOR_ALL_BB_FN (bb, cfun)
1597 FOR_EACH_EDGE (e, ei, bb->succs)
1598 fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
1601 fputs ("}\n\n", file);
1603 /* Enable debugging again. */
1604 dump_flags = tmp_dump_flags;
1607 /* Display all SCoPs using dotty. */
1609 DEBUG_FUNCTION void
1610 dot_all_scops (vec<scop_p> scops)
1612 /* When debugging, enable the following code. This cannot be used
1613 in production compilers because it calls "system". */
1614 #if 0
1615 int x;
1616 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1617 gcc_assert (stream);
1619 dot_all_scops_1 (stream, scops);
1620 fclose (stream);
1622 x = system ("dotty /tmp/allscops.dot &");
1623 #else
1624 dot_all_scops_1 (stderr, scops);
1625 #endif
1628 /* Display all SCoPs using dotty. */
1630 DEBUG_FUNCTION void
1631 dot_scop (scop_p scop)
1633 auto_vec<scop_p, 1> scops;
1635 if (scop)
1636 scops.safe_push (scop);
1638 /* When debugging, enable the following code. This cannot be used
1639 in production compilers because it calls "system". */
1640 #if 0
1642 int x;
1643 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1644 gcc_assert (stream);
1646 dot_all_scops_1 (stream, scops);
1647 fclose (stream);
1648 x = system ("dotty /tmp/allscops.dot &");
1650 #else
1651 dot_all_scops_1 (stderr, scops);
1652 #endif
1655 #endif