* config/avr/avr-protos.h (avr_mode_code_base_reg_class): New prototype.
[official-gcc.git] / gcc / graphite-scop-detection.c
blobe8370ef4e5a191875e00d0cc9c40d0750623c1ca
1 /* Detection of Static Control Parts (SCoP) for Graphite.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.com> and
4 Tobias Grosser <grosser@fim.uni-passau.de>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
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"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree-flow.h"
26 #include "cfgloop.h"
27 #include "tree-chrec.h"
28 #include "tree-data-ref.h"
29 #include "tree-scalar-evolution.h"
30 #include "tree-pass.h"
31 #include "sese.h"
33 #ifdef HAVE_cloog
34 #include "ppl_c.h"
35 #include "graphite-ppl.h"
36 #include "graphite-poly.h"
37 #include "graphite-scop-detection.h"
39 /* Forward declarations. */
40 static void make_close_phi_nodes_unique (basic_block);
42 /* The type of the analyzed basic block. */
44 typedef enum gbb_type {
45 GBB_UNKNOWN,
46 GBB_LOOP_SING_EXIT_HEADER,
47 GBB_LOOP_MULT_EXIT_HEADER,
48 GBB_LOOP_EXIT,
49 GBB_COND_HEADER,
50 GBB_SIMPLE,
51 GBB_LAST
52 } gbb_type;
54 /* Detect the type of BB. Loop headers are only marked, if they are
55 new. This means their loop_father is different to LAST_LOOP.
56 Otherwise they are treated like any other bb and their type can be
57 any other type. */
59 static gbb_type
60 get_bb_type (basic_block bb, struct loop *last_loop)
62 VEC (basic_block, heap) *dom;
63 int nb_dom, nb_suc;
64 struct loop *loop = bb->loop_father;
66 /* Check, if we entry into a new loop. */
67 if (loop != last_loop)
69 if (single_exit (loop) != NULL)
70 return GBB_LOOP_SING_EXIT_HEADER;
71 else if (loop->num != 0)
72 return GBB_LOOP_MULT_EXIT_HEADER;
73 else
74 return GBB_COND_HEADER;
77 dom = get_dominated_by (CDI_DOMINATORS, bb);
78 nb_dom = VEC_length (basic_block, dom);
79 VEC_free (basic_block, heap, dom);
81 if (nb_dom == 0)
82 return GBB_LAST;
84 nb_suc = VEC_length (edge, bb->succs);
86 if (nb_dom == 1 && nb_suc == 1)
87 return GBB_SIMPLE;
89 return GBB_COND_HEADER;
92 /* A SCoP detection region, defined using bbs as borders.
94 All control flow touching this region, comes in passing basic_block
95 ENTRY and leaves passing basic_block EXIT. By using bbs instead of
96 edges for the borders we are able to represent also regions that do
97 not have a single entry or exit edge.
99 But as they have a single entry basic_block and a single exit
100 basic_block, we are able to generate for every sd_region a single
101 entry and exit edge.
105 3 <- entry
108 / \ This region contains: {3, 4, 5, 6, 7, 8}
113 9 <- exit */
116 typedef struct sd_region_p
118 /* The entry bb dominates all bbs in the sd_region. It is part of
119 the region. */
120 basic_block entry;
122 /* The exit bb postdominates all bbs in the sd_region, but is not
123 part of the region. */
124 basic_block exit;
125 } sd_region;
127 DEF_VEC_O(sd_region);
128 DEF_VEC_ALLOC_O(sd_region, heap);
131 /* Moves the scops from SOURCE to TARGET and clean up SOURCE. */
133 static void
134 move_sd_regions (VEC (sd_region, heap) **source,
135 VEC (sd_region, heap) **target)
137 sd_region *s;
138 int i;
140 FOR_EACH_VEC_ELT (sd_region, *source, i, s)
141 VEC_safe_push (sd_region, heap, *target, s);
143 VEC_free (sd_region, heap, *source);
146 /* Something like "n * m" is not allowed. */
148 static bool
149 graphite_can_represent_init (tree e)
151 switch (TREE_CODE (e))
153 case POLYNOMIAL_CHREC:
154 return graphite_can_represent_init (CHREC_LEFT (e))
155 && graphite_can_represent_init (CHREC_RIGHT (e));
157 case MULT_EXPR:
158 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
159 return graphite_can_represent_init (TREE_OPERAND (e, 0))
160 && host_integerp (TREE_OPERAND (e, 1), 0);
161 else
162 return graphite_can_represent_init (TREE_OPERAND (e, 1))
163 && host_integerp (TREE_OPERAND (e, 0), 0);
165 case PLUS_EXPR:
166 case POINTER_PLUS_EXPR:
167 case MINUS_EXPR:
168 return graphite_can_represent_init (TREE_OPERAND (e, 0))
169 && graphite_can_represent_init (TREE_OPERAND (e, 1));
171 case NEGATE_EXPR:
172 case BIT_NOT_EXPR:
173 CASE_CONVERT:
174 case NON_LVALUE_EXPR:
175 return graphite_can_represent_init (TREE_OPERAND (e, 0));
177 default:
178 break;
181 return true;
184 /* Return true when SCEV can be represented in the polyhedral model.
186 An expression can be represented, if it can be expressed as an
187 affine expression. For loops (i, j) and parameters (m, n) all
188 affine expressions are of the form:
190 x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
192 1 i + 20 j + (-2) m + 25
194 Something like "i * n" or "n * m" is not allowed. */
196 static bool
197 graphite_can_represent_scev (tree scev)
199 if (chrec_contains_undetermined (scev))
200 return false;
202 switch (TREE_CODE (scev))
204 case PLUS_EXPR:
205 case MINUS_EXPR:
206 return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
207 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
209 case MULT_EXPR:
210 return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
211 && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
212 && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
213 && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
214 && graphite_can_represent_init (scev)
215 && graphite_can_represent_scev (TREE_OPERAND (scev, 0))
216 && graphite_can_represent_scev (TREE_OPERAND (scev, 1));
218 case POLYNOMIAL_CHREC:
219 /* Check for constant strides. With a non constant stride of
220 'n' we would have a value of 'iv * n'. Also check that the
221 initial value can represented: for example 'n * m' cannot be
222 represented. */
223 if (!evolution_function_right_is_integer_cst (scev)
224 || !graphite_can_represent_init (scev))
225 return false;
227 default:
228 break;
231 /* Only affine functions can be represented. */
232 if (!scev_is_linear_expression (scev))
233 return false;
235 return true;
239 /* Return true when EXPR can be represented in the polyhedral model.
241 This means an expression can be represented, if it is linear with
242 respect to the loops and the strides are non parametric.
243 LOOP is the place where the expr will be evaluated. SCOP_ENTRY defines the
244 entry of the region we analyse. */
246 static bool
247 graphite_can_represent_expr (basic_block scop_entry, loop_p loop,
248 tree expr)
250 tree scev = analyze_scalar_evolution (loop, expr);
252 scev = instantiate_scev (scop_entry, loop, scev);
254 return graphite_can_represent_scev (scev);
257 /* Return true if the data references of STMT can be represented by
258 Graphite. */
260 static bool
261 stmt_has_simple_data_refs_p (loop_p outermost_loop, gimple stmt)
263 data_reference_p dr;
264 unsigned i;
265 int j;
266 bool res = true;
267 VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 5);
269 graphite_find_data_references_in_stmt (outermost_loop,
270 loop_containing_stmt (stmt),
271 stmt, &drs);
273 FOR_EACH_VEC_ELT (data_reference_p, drs, j, dr)
274 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
275 if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i)))
277 res = false;
278 goto done;
281 done:
282 free_data_refs (drs);
283 return res;
286 /* Return true only when STMT is simple enough for being handled by
287 Graphite. This depends on SCOP_ENTRY, as the parameters are
288 initialized relatively to this basic block, the linear functions
289 are initialized to OUTERMOST_LOOP and BB is the place where we try
290 to evaluate the STMT. */
292 static bool
293 stmt_simple_for_scop_p (basic_block scop_entry, loop_p outermost_loop,
294 gimple stmt, basic_block bb)
296 loop_p loop = bb->loop_father;
298 gcc_assert (scop_entry);
300 /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
301 Calls have side-effects, except those to const or pure
302 functions. */
303 if (gimple_has_volatile_ops (stmt)
304 || (gimple_code (stmt) == GIMPLE_CALL
305 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
306 || (gimple_code (stmt) == GIMPLE_ASM))
307 return false;
309 if (is_gimple_debug (stmt))
310 return true;
312 if (!stmt_has_simple_data_refs_p (outermost_loop, stmt))
313 return false;
315 switch (gimple_code (stmt))
317 case GIMPLE_RETURN:
318 case GIMPLE_LABEL:
319 return true;
321 case GIMPLE_COND:
323 tree op;
324 ssa_op_iter op_iter;
325 enum tree_code code = gimple_cond_code (stmt);
327 /* We can handle all binary comparisons. Inequalities are
328 also supported as they can be represented with union of
329 polyhedra. */
330 if (!(code == LT_EXPR
331 || code == GT_EXPR
332 || code == LE_EXPR
333 || code == GE_EXPR
334 || code == EQ_EXPR
335 || code == NE_EXPR))
336 return false;
338 FOR_EACH_SSA_TREE_OPERAND (op, stmt, op_iter, SSA_OP_ALL_USES)
339 if (!graphite_can_represent_expr (scop_entry, loop, op)
340 /* We can not handle REAL_TYPE. Failed for pr39260. */
341 || TREE_CODE (TREE_TYPE (op)) == REAL_TYPE)
342 return false;
344 return true;
347 case GIMPLE_ASSIGN:
348 case GIMPLE_CALL:
349 return true;
351 default:
352 /* These nodes cut a new scope. */
353 return false;
356 return false;
359 /* Returns the statement of BB that contains a harmful operation: that
360 can be a function call with side effects, the induction variables
361 are not linear with respect to SCOP_ENTRY, etc. The current open
362 scop should end before this statement. The evaluation is limited using
363 OUTERMOST_LOOP as outermost loop that may change. */
365 static gimple
366 harmful_stmt_in_bb (basic_block scop_entry, loop_p outer_loop, basic_block bb)
368 gimple_stmt_iterator gsi;
370 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
371 if (!stmt_simple_for_scop_p (scop_entry, outer_loop, gsi_stmt (gsi), bb))
372 return gsi_stmt (gsi);
374 return NULL;
377 /* Return true if LOOP can be represented in the polyhedral
378 representation. This is evaluated taking SCOP_ENTRY and
379 OUTERMOST_LOOP in mind. */
381 static bool
382 graphite_can_represent_loop (basic_block scop_entry, loop_p loop)
384 tree niter;
385 struct tree_niter_desc niter_desc;
387 /* FIXME: For the moment, graphite cannot be used on loops that
388 iterate using induction variables that wrap. */
390 return number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
391 && niter_desc.control.no_overflow
392 && (niter = number_of_latch_executions (loop))
393 && !chrec_contains_undetermined (niter)
394 && graphite_can_represent_expr (scop_entry, loop, niter);
397 /* Store information needed by scopdet_* functions. */
399 struct scopdet_info
401 /* Exit of the open scop would stop if the current BB is harmful. */
402 basic_block exit;
404 /* Where the next scop would start if the current BB is harmful. */
405 basic_block next;
407 /* The bb or one of its children contains open loop exits. That means
408 loop exit nodes that are not surrounded by a loop dominated by bb. */
409 bool exits;
411 /* The bb or one of its children contains only structures we can handle. */
412 bool difficult;
415 static struct scopdet_info build_scops_1 (basic_block, loop_p,
416 VEC (sd_region, heap) **, loop_p);
418 /* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
419 to SCOPS. TYPE is the gbb_type of BB. */
421 static struct scopdet_info
422 scopdet_basic_block_info (basic_block bb, loop_p outermost_loop,
423 VEC (sd_region, heap) **scops, gbb_type type)
425 loop_p loop = bb->loop_father;
426 struct scopdet_info result;
427 gimple stmt;
429 /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps. */
430 basic_block entry_block = ENTRY_BLOCK_PTR;
431 stmt = harmful_stmt_in_bb (entry_block, outermost_loop, bb);
432 result.difficult = (stmt != NULL);
433 result.exit = NULL;
435 switch (type)
437 case GBB_LAST:
438 result.next = NULL;
439 result.exits = false;
441 /* Mark bbs terminating a SESE region difficult, if they start
442 a condition. */
443 if (!single_succ_p (bb))
444 result.difficult = true;
445 else
446 result.exit = single_succ (bb);
448 break;
450 case GBB_SIMPLE:
451 result.next = single_succ (bb);
452 result.exits = false;
453 result.exit = single_succ (bb);
454 break;
456 case GBB_LOOP_SING_EXIT_HEADER:
458 VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
459 struct scopdet_info sinfo;
460 edge exit_e = single_exit (loop);
462 sinfo = build_scops_1 (bb, outermost_loop, &regions, loop);
464 if (!graphite_can_represent_loop (entry_block, loop))
465 result.difficult = true;
467 result.difficult |= sinfo.difficult;
469 /* Try again with another loop level. */
470 if (result.difficult
471 && loop_depth (outermost_loop) + 1 == loop_depth (loop))
473 outermost_loop = loop;
475 VEC_free (sd_region, heap, regions);
476 regions = VEC_alloc (sd_region, heap, 3);
478 sinfo = scopdet_basic_block_info (bb, outermost_loop, scops, type);
480 result = sinfo;
481 result.difficult = true;
483 if (sinfo.difficult)
484 move_sd_regions (&regions, scops);
485 else
487 sd_region open_scop;
488 open_scop.entry = bb;
489 open_scop.exit = exit_e->dest;
490 VEC_safe_push (sd_region, heap, *scops, &open_scop);
491 VEC_free (sd_region, heap, regions);
494 else
496 result.exit = exit_e->dest;
497 result.next = exit_e->dest;
499 /* If we do not dominate result.next, remove it. It's either
500 the EXIT_BLOCK_PTR, or another bb dominates it and will
501 call the scop detection for this bb. */
502 if (!dominated_by_p (CDI_DOMINATORS, result.next, bb))
503 result.next = NULL;
505 if (exit_e->src->loop_father != loop)
506 result.next = NULL;
508 result.exits = false;
510 if (result.difficult)
511 move_sd_regions (&regions, scops);
512 else
513 VEC_free (sd_region, heap, regions);
516 break;
519 case GBB_LOOP_MULT_EXIT_HEADER:
521 /* XXX: For now we just do not join loops with multiple exits. If the
522 exits lead to the same bb it may be possible to join the loop. */
523 VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
524 VEC (edge, heap) *exits = get_loop_exit_edges (loop);
525 edge e;
526 int i;
527 build_scops_1 (bb, loop, &regions, loop);
529 /* Scan the code dominated by this loop. This means all bbs, that are
530 are dominated by a bb in this loop, but are not part of this loop.
532 The easiest case:
533 - The loop exit destination is dominated by the exit sources.
535 TODO: We miss here the more complex cases:
536 - The exit destinations are dominated by another bb inside
537 the loop.
538 - The loop dominates bbs, that are not exit destinations. */
539 FOR_EACH_VEC_ELT (edge, exits, i, e)
540 if (e->src->loop_father == loop
541 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src))
543 if (loop_outer (outermost_loop))
544 outermost_loop = loop_outer (outermost_loop);
546 /* Pass loop_outer to recognize e->dest as loop header in
547 build_scops_1. */
548 if (e->dest->loop_father->header == e->dest)
549 build_scops_1 (e->dest, outermost_loop, &regions,
550 loop_outer (e->dest->loop_father));
551 else
552 build_scops_1 (e->dest, outermost_loop, &regions,
553 e->dest->loop_father);
556 result.next = NULL;
557 result.exit = NULL;
558 result.difficult = true;
559 result.exits = false;
560 move_sd_regions (&regions, scops);
561 VEC_free (edge, heap, exits);
562 break;
564 case GBB_COND_HEADER:
566 VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
567 struct scopdet_info sinfo;
568 VEC (basic_block, heap) *dominated;
569 int i;
570 basic_block dom_bb;
571 basic_block last_exit = NULL;
572 edge e;
573 result.exits = false;
575 /* First check the successors of BB, and check if it is
576 possible to join the different branches. */
577 FOR_EACH_VEC_ELT (edge, bb->succs, i, e)
579 /* Ignore loop exits. They will be handled after the loop
580 body. */
581 if (loop_exits_to_bb_p (loop, e->dest))
583 result.exits = true;
584 continue;
587 /* Do not follow edges that lead to the end of the
588 conditions block. For example, in
591 | /|\
592 | 1 2 |
593 | | | |
594 | 3 4 |
595 | \|/
598 the edge from 0 => 6. Only check if all paths lead to
599 the same node 6. */
601 if (!single_pred_p (e->dest))
603 /* Check, if edge leads directly to the end of this
604 condition. */
605 if (!last_exit)
606 last_exit = e->dest;
608 if (e->dest != last_exit)
609 result.difficult = true;
611 continue;
614 if (!dominated_by_p (CDI_DOMINATORS, e->dest, bb))
616 result.difficult = true;
617 continue;
620 sinfo = build_scops_1 (e->dest, outermost_loop, &regions, loop);
622 result.exits |= sinfo.exits;
623 result.difficult |= sinfo.difficult;
625 /* Checks, if all branches end at the same point.
626 If that is true, the condition stays joinable.
627 Have a look at the example above. */
628 if (sinfo.exit)
630 if (!last_exit)
631 last_exit = sinfo.exit;
633 if (sinfo.exit != last_exit)
634 result.difficult = true;
636 else
637 result.difficult = true;
640 if (!last_exit)
641 result.difficult = true;
643 /* Join the branches of the condition if possible. */
644 if (!result.exits && !result.difficult)
646 /* Only return a next pointer if we dominate this pointer.
647 Otherwise it will be handled by the bb dominating it. */
648 if (dominated_by_p (CDI_DOMINATORS, last_exit, bb)
649 && last_exit != bb)
650 result.next = last_exit;
651 else
652 result.next = NULL;
654 result.exit = last_exit;
656 VEC_free (sd_region, heap, regions);
657 break;
660 /* Scan remaining bbs dominated by BB. */
661 dominated = get_dominated_by (CDI_DOMINATORS, bb);
663 FOR_EACH_VEC_ELT (basic_block, dominated, i, dom_bb)
665 /* Ignore loop exits: they will be handled after the loop body. */
666 if (loop_depth (find_common_loop (loop, dom_bb->loop_father))
667 < loop_depth (loop))
669 result.exits = true;
670 continue;
673 /* Ignore the bbs processed above. */
674 if (single_pred_p (dom_bb) && single_pred (dom_bb) == bb)
675 continue;
677 if (loop_depth (loop) > loop_depth (dom_bb->loop_father))
678 sinfo = build_scops_1 (dom_bb, outermost_loop, &regions,
679 loop_outer (loop));
680 else
681 sinfo = build_scops_1 (dom_bb, outermost_loop, &regions, loop);
683 result.exits |= sinfo.exits;
684 result.difficult = true;
685 result.exit = NULL;
688 VEC_free (basic_block, heap, dominated);
690 result.next = NULL;
691 move_sd_regions (&regions, scops);
693 break;
696 default:
697 gcc_unreachable ();
700 return result;
703 /* Starting from CURRENT we walk the dominance tree and add new sd_regions to
704 SCOPS. The analyse if a sd_region can be handled is based on the value
705 of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change. LOOP
706 is the loop in which CURRENT is handled.
708 TODO: These functions got a little bit big. They definitely should be cleaned
709 up. */
711 static struct scopdet_info
712 build_scops_1 (basic_block current, loop_p outermost_loop,
713 VEC (sd_region, heap) **scops, loop_p loop)
715 bool in_scop = false;
716 sd_region open_scop;
717 struct scopdet_info sinfo;
719 /* Initialize result. */
720 struct scopdet_info result;
721 result.exits = false;
722 result.difficult = false;
723 result.next = NULL;
724 result.exit = NULL;
725 open_scop.entry = NULL;
726 open_scop.exit = NULL;
727 sinfo.exit = NULL;
729 /* Loop over the dominance tree. If we meet a difficult bb, close
730 the current SCoP. Loop and condition header start a new layer,
731 and can only be added if all bbs in deeper layers are simple. */
732 while (current != NULL)
734 sinfo = scopdet_basic_block_info (current, outermost_loop, scops,
735 get_bb_type (current, loop));
737 if (!in_scop && !(sinfo.exits || sinfo.difficult))
739 open_scop.entry = current;
740 open_scop.exit = NULL;
741 in_scop = true;
743 else if (in_scop && (sinfo.exits || sinfo.difficult))
745 open_scop.exit = current;
746 VEC_safe_push (sd_region, heap, *scops, &open_scop);
747 in_scop = false;
750 result.difficult |= sinfo.difficult;
751 result.exits |= sinfo.exits;
753 current = sinfo.next;
756 /* Try to close open_scop, if we are still in an open SCoP. */
757 if (in_scop)
759 open_scop.exit = sinfo.exit;
760 gcc_assert (open_scop.exit);
761 VEC_safe_push (sd_region, heap, *scops, &open_scop);
764 result.exit = sinfo.exit;
765 return result;
768 /* Checks if a bb is contained in REGION. */
770 static bool
771 bb_in_sd_region (basic_block bb, sd_region *region)
773 return bb_in_region (bb, region->entry, region->exit);
776 /* Returns the single entry edge of REGION, if it does not exits NULL. */
778 static edge
779 find_single_entry_edge (sd_region *region)
781 edge e;
782 edge_iterator ei;
783 edge entry = NULL;
785 FOR_EACH_EDGE (e, ei, region->entry->preds)
786 if (!bb_in_sd_region (e->src, region))
788 if (entry)
790 entry = NULL;
791 break;
794 else
795 entry = e;
798 return entry;
801 /* Returns the single exit edge of REGION, if it does not exits NULL. */
803 static edge
804 find_single_exit_edge (sd_region *region)
806 edge e;
807 edge_iterator ei;
808 edge exit = NULL;
810 FOR_EACH_EDGE (e, ei, region->exit->preds)
811 if (bb_in_sd_region (e->src, region))
813 if (exit)
815 exit = NULL;
816 break;
819 else
820 exit = e;
823 return exit;
826 /* Create a single entry edge for REGION. */
828 static void
829 create_single_entry_edge (sd_region *region)
831 if (find_single_entry_edge (region))
832 return;
834 /* There are multiple predecessors for bb_3
836 | 1 2
837 | | /
838 | |/
839 | 3 <- entry
840 | |\
841 | | |
842 | 4 ^
843 | | |
844 | |/
847 There are two edges (1->3, 2->3), that point from outside into the region,
848 and another one (5->3), a loop latch, lead to bb_3.
850 We split bb_3.
852 | 1 2
853 | | /
854 | |/
855 |3.0
856 | |\ (3.0 -> 3.1) = single entry edge
857 |3.1 | <- entry
858 | | |
859 | | |
860 | 4 ^
861 | | |
862 | |/
865 If the loop is part of the SCoP, we have to redirect the loop latches.
867 | 1 2
868 | | /
869 | |/
870 |3.0
871 | | (3.0 -> 3.1) = entry edge
872 |3.1 <- entry
873 | |\
874 | | |
875 | 4 ^
876 | | |
877 | |/
878 | 5 */
880 if (region->entry->loop_father->header != region->entry
881 || dominated_by_p (CDI_DOMINATORS,
882 loop_latch_edge (region->entry->loop_father)->src,
883 region->exit))
885 edge forwarder = split_block_after_labels (region->entry);
886 region->entry = forwarder->dest;
888 else
889 /* This case is never executed, as the loop headers seem always to have a
890 single edge pointing from outside into the loop. */
891 gcc_unreachable ();
893 gcc_checking_assert (find_single_entry_edge (region));
896 /* Check if the sd_region, mentioned in EDGE, has no exit bb. */
898 static bool
899 sd_region_without_exit (edge e)
901 sd_region *r = (sd_region *) e->aux;
903 if (r)
904 return r->exit == NULL;
905 else
906 return false;
909 /* Create a single exit edge for REGION. */
911 static void
912 create_single_exit_edge (sd_region *region)
914 edge e;
915 edge_iterator ei;
916 edge forwarder = NULL;
917 basic_block exit;
919 /* We create a forwarder bb (5) for all edges leaving this region
920 (3->5, 4->5). All other edges leading to the same bb, are moved
921 to a new bb (6). If these edges where part of another region (2->5)
922 we update the region->exit pointer, of this region.
924 To identify which edge belongs to which region we depend on the e->aux
925 pointer in every edge. It points to the region of the edge or to NULL,
926 if the edge is not part of any region.
928 1 2 3 4 1->5 no region, 2->5 region->exit = 5,
929 \| |/ 3->5 region->exit = NULL, 4->5 region->exit = NULL
930 5 <- exit
932 changes to
934 1 2 3 4 1->6 no region, 2->6 region->exit = 6,
935 | | \/ 3->5 no region, 4->5 no region,
936 | | 5
937 \| / 5->6 region->exit = 6
940 Now there is only a single exit edge (5->6). */
941 exit = region->exit;
942 region->exit = NULL;
943 forwarder = make_forwarder_block (exit, &sd_region_without_exit, NULL);
945 /* Unmark the edges, that are no longer exit edges. */
946 FOR_EACH_EDGE (e, ei, forwarder->src->preds)
947 if (e->aux)
948 e->aux = NULL;
950 /* Mark the new exit edge. */
951 single_succ_edge (forwarder->src)->aux = region;
953 /* Update the exit bb of all regions, where exit edges lead to
954 forwarder->dest. */
955 FOR_EACH_EDGE (e, ei, forwarder->dest->preds)
956 if (e->aux)
957 ((sd_region *) e->aux)->exit = forwarder->dest;
959 gcc_checking_assert (find_single_exit_edge (region));
962 /* Unmark the exit edges of all REGIONS.
963 See comment in "create_single_exit_edge". */
965 static void
966 unmark_exit_edges (VEC (sd_region, heap) *regions)
968 int i;
969 sd_region *s;
970 edge e;
971 edge_iterator ei;
973 FOR_EACH_VEC_ELT (sd_region, regions, i, s)
974 FOR_EACH_EDGE (e, ei, s->exit->preds)
975 e->aux = NULL;
979 /* Mark the exit edges of all REGIONS.
980 See comment in "create_single_exit_edge". */
982 static void
983 mark_exit_edges (VEC (sd_region, heap) *regions)
985 int i;
986 sd_region *s;
987 edge e;
988 edge_iterator ei;
990 FOR_EACH_VEC_ELT (sd_region, regions, i, s)
991 FOR_EACH_EDGE (e, ei, s->exit->preds)
992 if (bb_in_sd_region (e->src, s))
993 e->aux = s;
996 /* Create for all scop regions a single entry and a single exit edge. */
998 static void
999 create_sese_edges (VEC (sd_region, heap) *regions)
1001 int i;
1002 sd_region *s;
1004 FOR_EACH_VEC_ELT (sd_region, regions, i, s)
1005 create_single_entry_edge (s);
1007 mark_exit_edges (regions);
1009 FOR_EACH_VEC_ELT (sd_region, regions, i, s)
1010 /* Don't handle multiple edges exiting the function. */
1011 if (!find_single_exit_edge (s)
1012 && s->exit != EXIT_BLOCK_PTR)
1013 create_single_exit_edge (s);
1015 unmark_exit_edges (regions);
1017 fix_loop_structure (NULL);
1019 #ifdef ENABLE_CHECKING
1020 verify_loop_structure ();
1021 verify_dominators (CDI_DOMINATORS);
1022 verify_ssa (false);
1023 #endif
1026 /* Create graphite SCoPs from an array of scop detection REGIONS. */
1028 static void
1029 build_graphite_scops (VEC (sd_region, heap) *regions,
1030 VEC (scop_p, heap) **scops)
1032 int i;
1033 sd_region *s;
1035 FOR_EACH_VEC_ELT (sd_region, regions, i, s)
1037 edge entry = find_single_entry_edge (s);
1038 edge exit = find_single_exit_edge (s);
1039 scop_p scop;
1041 if (!exit)
1042 continue;
1044 scop = new_scop (new_sese (entry, exit));
1045 VEC_safe_push (scop_p, heap, *scops, scop);
1047 /* Are there overlapping SCoPs? */
1048 #ifdef ENABLE_CHECKING
1050 int j;
1051 sd_region *s2;
1053 FOR_EACH_VEC_ELT (sd_region, regions, j, s2)
1054 if (s != s2)
1055 gcc_assert (!bb_in_sd_region (s->entry, s2));
1057 #endif
1061 /* Returns true when BB contains only close phi nodes. */
1063 static bool
1064 contains_only_close_phi_nodes (basic_block bb)
1066 gimple_stmt_iterator gsi;
1068 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1069 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
1070 return false;
1072 return true;
1075 /* Print statistics for SCOP to FILE. */
1077 static void
1078 print_graphite_scop_statistics (FILE* file, scop_p scop)
1080 long n_bbs = 0;
1081 long n_loops = 0;
1082 long n_stmts = 0;
1083 long n_conditions = 0;
1084 long n_p_bbs = 0;
1085 long n_p_loops = 0;
1086 long n_p_stmts = 0;
1087 long n_p_conditions = 0;
1089 basic_block bb;
1091 FOR_ALL_BB (bb)
1093 gimple_stmt_iterator psi;
1094 loop_p loop = bb->loop_father;
1096 if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
1097 continue;
1099 n_bbs++;
1100 n_p_bbs += bb->count;
1102 if (VEC_length (edge, bb->succs) > 1)
1104 n_conditions++;
1105 n_p_conditions += bb->count;
1108 for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
1110 n_stmts++;
1111 n_p_stmts += bb->count;
1114 if (loop->header == bb && loop_in_sese_p (loop, SCOP_REGION (scop)))
1116 n_loops++;
1117 n_p_loops += bb->count;
1122 fprintf (file, "\nBefore limit_scops SCoP statistics (");
1123 fprintf (file, "BBS:%ld, ", n_bbs);
1124 fprintf (file, "LOOPS:%ld, ", n_loops);
1125 fprintf (file, "CONDITIONS:%ld, ", n_conditions);
1126 fprintf (file, "STMTS:%ld)\n", n_stmts);
1127 fprintf (file, "\nBefore limit_scops SCoP profiling statistics (");
1128 fprintf (file, "BBS:%ld, ", n_p_bbs);
1129 fprintf (file, "LOOPS:%ld, ", n_p_loops);
1130 fprintf (file, "CONDITIONS:%ld, ", n_p_conditions);
1131 fprintf (file, "STMTS:%ld)\n", n_p_stmts);
1134 /* Print statistics for SCOPS to FILE. */
1136 static void
1137 print_graphite_statistics (FILE* file, VEC (scop_p, heap) *scops)
1139 int i;
1140 scop_p scop;
1142 FOR_EACH_VEC_ELT (scop_p, scops, i, scop)
1143 print_graphite_scop_statistics (file, scop);
1146 /* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
1148 Example:
1150 for (i |
1152 for (j | SCoP 1
1153 for (k |
1156 * SCoP frontier, as this line is not surrounded by any loop. *
1158 for (l | SCoP 2
1160 This is necessary as scalar evolution and parameter detection need a
1161 outermost loop to initialize parameters correctly.
1163 TODO: FIX scalar evolution and parameter detection to allow more flexible
1164 SCoP frontiers. */
1166 static void
1167 limit_scops (VEC (scop_p, heap) **scops)
1169 VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
1171 int i;
1172 scop_p scop;
1174 FOR_EACH_VEC_ELT (scop_p, *scops, i, scop)
1176 int j;
1177 loop_p loop;
1178 sese region = SCOP_REGION (scop);
1179 build_sese_loop_nests (region);
1181 FOR_EACH_VEC_ELT (loop_p, SESE_LOOP_NEST (region), j, loop)
1182 if (!loop_in_sese_p (loop_outer (loop), region)
1183 && single_exit (loop))
1185 sd_region open_scop;
1186 open_scop.entry = loop->header;
1187 open_scop.exit = single_exit (loop)->dest;
1189 /* This is a hack on top of the limit_scops hack. The
1190 limit_scops hack should disappear all together. */
1191 if (single_succ_p (open_scop.exit)
1192 && contains_only_close_phi_nodes (open_scop.exit))
1193 open_scop.exit = single_succ_edge (open_scop.exit)->dest;
1195 VEC_safe_push (sd_region, heap, regions, &open_scop);
1199 free_scops (*scops);
1200 *scops = VEC_alloc (scop_p, heap, 3);
1202 create_sese_edges (regions);
1203 build_graphite_scops (regions, scops);
1204 VEC_free (sd_region, heap, regions);
1207 /* Returns true when P1 and P2 are close phis with the same
1208 argument. */
1210 static inline bool
1211 same_close_phi_node (gimple p1, gimple p2)
1213 return operand_equal_p (gimple_phi_arg_def (p1, 0),
1214 gimple_phi_arg_def (p2, 0), 0);
1217 /* Remove the close phi node at GSI and replace its rhs with the rhs
1218 of PHI. */
1220 static void
1221 remove_duplicate_close_phi (gimple phi, gimple_stmt_iterator *gsi)
1223 gimple use_stmt;
1224 use_operand_p use_p;
1225 imm_use_iterator imm_iter;
1226 tree res = gimple_phi_result (phi);
1227 tree def = gimple_phi_result (gsi_stmt (*gsi));
1229 gcc_assert (same_close_phi_node (phi, gsi_stmt (*gsi)));
1231 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
1233 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1234 SET_USE (use_p, res);
1236 update_stmt (use_stmt);
1238 /* It is possible that we just created a duplicate close-phi
1239 for an already-processed containing loop. Check for this
1240 case and clean it up. */
1241 if (gimple_code (use_stmt) == GIMPLE_PHI
1242 && gimple_phi_num_args (use_stmt) == 1)
1243 make_close_phi_nodes_unique (gimple_bb (use_stmt));
1246 remove_phi_node (gsi, true);
1249 /* Removes all the close phi duplicates from BB. */
1251 static void
1252 make_close_phi_nodes_unique (basic_block bb)
1254 gimple_stmt_iterator psi;
1256 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1258 gimple_stmt_iterator gsi = psi;
1259 gimple phi = gsi_stmt (psi);
1261 /* At this point, PHI should be a close phi in normal form. */
1262 gcc_assert (gimple_phi_num_args (phi) == 1);
1264 /* Iterate over the next phis and remove duplicates. */
1265 gsi_next (&gsi);
1266 while (!gsi_end_p (gsi))
1267 if (same_close_phi_node (phi, gsi_stmt (gsi)))
1268 remove_duplicate_close_phi (phi, &gsi);
1269 else
1270 gsi_next (&gsi);
1274 /* Transforms LOOP to the canonical loop closed SSA form. */
1276 static void
1277 canonicalize_loop_closed_ssa (loop_p loop)
1279 edge e = single_exit (loop);
1280 basic_block bb;
1282 if (!e || e->flags & EDGE_ABNORMAL)
1283 return;
1285 bb = e->dest;
1287 if (VEC_length (edge, bb->preds) == 1)
1289 e = split_block_after_labels (bb);
1290 make_close_phi_nodes_unique (e->src);
1292 else
1294 gimple_stmt_iterator psi;
1295 basic_block close = split_edge (e);
1297 e = single_succ_edge (close);
1299 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1301 gimple phi = gsi_stmt (psi);
1302 unsigned i;
1304 for (i = 0; i < gimple_phi_num_args (phi); i++)
1305 if (gimple_phi_arg_edge (phi, i) == e)
1307 tree res, arg = gimple_phi_arg_def (phi, i);
1308 use_operand_p use_p;
1309 gimple close_phi;
1311 if (TREE_CODE (arg) != SSA_NAME)
1312 continue;
1314 close_phi = create_phi_node (arg, close);
1315 res = create_new_def_for (gimple_phi_result (close_phi),
1316 close_phi,
1317 gimple_phi_result_ptr (close_phi));
1318 add_phi_arg (close_phi, arg,
1319 gimple_phi_arg_edge (close_phi, 0),
1320 UNKNOWN_LOCATION);
1321 use_p = gimple_phi_arg_imm_use_ptr (phi, i);
1322 replace_exp (use_p, res);
1323 update_stmt (phi);
1327 make_close_phi_nodes_unique (close);
1330 /* The code above does not properly handle changes in the post dominance
1331 information (yet). */
1332 free_dominance_info (CDI_POST_DOMINATORS);
1335 /* Converts the current loop closed SSA form to a canonical form
1336 expected by the Graphite code generation.
1338 The loop closed SSA form has the following invariant: a variable
1339 defined in a loop that is used outside the loop appears only in the
1340 phi nodes in the destination of the loop exit. These phi nodes are
1341 called close phi nodes.
1343 The canonical loop closed SSA form contains the extra invariants:
1345 - when the loop contains only one exit, the close phi nodes contain
1346 only one argument. That implies that the basic block that contains
1347 the close phi nodes has only one predecessor, that is a basic block
1348 in the loop.
1350 - the basic block containing the close phi nodes does not contain
1351 other statements.
1353 - there exist only one phi node per definition in the loop.
1356 static void
1357 canonicalize_loop_closed_ssa_form (void)
1359 loop_iterator li;
1360 loop_p loop;
1362 #ifdef ENABLE_CHECKING
1363 verify_loop_closed_ssa (true);
1364 #endif
1366 FOR_EACH_LOOP (li, loop, 0)
1367 canonicalize_loop_closed_ssa (loop);
1369 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1370 update_ssa (TODO_update_ssa);
1372 #ifdef ENABLE_CHECKING
1373 verify_loop_closed_ssa (true);
1374 #endif
1377 /* Find Static Control Parts (SCoP) in the current function and pushes
1378 them to SCOPS. */
1380 void
1381 build_scops (VEC (scop_p, heap) **scops)
1383 struct loop *loop = current_loops->tree_root;
1384 VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
1386 canonicalize_loop_closed_ssa_form ();
1387 build_scops_1 (single_succ (ENTRY_BLOCK_PTR), ENTRY_BLOCK_PTR->loop_father,
1388 &regions, loop);
1389 create_sese_edges (regions);
1390 build_graphite_scops (regions, scops);
1392 if (dump_file && (dump_flags & TDF_DETAILS))
1393 print_graphite_statistics (dump_file, *scops);
1395 limit_scops (scops);
1396 VEC_free (sd_region, heap, regions);
1398 if (dump_file && (dump_flags & TDF_DETAILS))
1399 fprintf (dump_file, "\nnumber of SCoPs: %d\n",
1400 VEC_length (scop_p, *scops));
1403 /* Pretty print to FILE all the SCoPs in DOT format and mark them with
1404 different colors. If there are not enough colors, paint the
1405 remaining SCoPs in gray.
1407 Special nodes:
1408 - "*" after the node number denotes the entry of a SCoP,
1409 - "#" after the node number denotes the exit of a SCoP,
1410 - "()" around the node number denotes the entry or the
1411 exit nodes of the SCOP. These are not part of SCoP. */
1413 static void
1414 dot_all_scops_1 (FILE *file, VEC (scop_p, heap) *scops)
1416 basic_block bb;
1417 edge e;
1418 edge_iterator ei;
1419 scop_p scop;
1420 const char* color;
1421 int i;
1423 /* Disable debugging while printing graph. */
1424 int tmp_dump_flags = dump_flags;
1425 dump_flags = 0;
1427 fprintf (file, "digraph all {\n");
1429 FOR_ALL_BB (bb)
1431 int part_of_scop = false;
1433 /* Use HTML for every bb label. So we are able to print bbs
1434 which are part of two different SCoPs, with two different
1435 background colors. */
1436 fprintf (file, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
1437 bb->index);
1438 fprintf (file, "CELLSPACING=\"0\">\n");
1440 /* Select color for SCoP. */
1441 FOR_EACH_VEC_ELT (scop_p, scops, i, scop)
1443 sese region = SCOP_REGION (scop);
1444 if (bb_in_sese_p (bb, region)
1445 || (SESE_EXIT_BB (region) == bb)
1446 || (SESE_ENTRY_BB (region) == bb))
1448 switch (i % 17)
1450 case 0: /* red */
1451 color = "#e41a1c";
1452 break;
1453 case 1: /* blue */
1454 color = "#377eb8";
1455 break;
1456 case 2: /* green */
1457 color = "#4daf4a";
1458 break;
1459 case 3: /* purple */
1460 color = "#984ea3";
1461 break;
1462 case 4: /* orange */
1463 color = "#ff7f00";
1464 break;
1465 case 5: /* yellow */
1466 color = "#ffff33";
1467 break;
1468 case 6: /* brown */
1469 color = "#a65628";
1470 break;
1471 case 7: /* rose */
1472 color = "#f781bf";
1473 break;
1474 case 8:
1475 color = "#8dd3c7";
1476 break;
1477 case 9:
1478 color = "#ffffb3";
1479 break;
1480 case 10:
1481 color = "#bebada";
1482 break;
1483 case 11:
1484 color = "#fb8072";
1485 break;
1486 case 12:
1487 color = "#80b1d3";
1488 break;
1489 case 13:
1490 color = "#fdb462";
1491 break;
1492 case 14:
1493 color = "#b3de69";
1494 break;
1495 case 15:
1496 color = "#fccde5";
1497 break;
1498 case 16:
1499 color = "#bc80bd";
1500 break;
1501 default: /* gray */
1502 color = "#999999";
1505 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
1507 if (!bb_in_sese_p (bb, region))
1508 fprintf (file, " (");
1510 if (bb == SESE_ENTRY_BB (region)
1511 && bb == SESE_EXIT_BB (region))
1512 fprintf (file, " %d*# ", bb->index);
1513 else if (bb == SESE_ENTRY_BB (region))
1514 fprintf (file, " %d* ", bb->index);
1515 else if (bb == SESE_EXIT_BB (region))
1516 fprintf (file, " %d# ", bb->index);
1517 else
1518 fprintf (file, " %d ", bb->index);
1520 if (!bb_in_sese_p (bb,region))
1521 fprintf (file, ")");
1523 fprintf (file, "</TD></TR>\n");
1524 part_of_scop = true;
1528 if (!part_of_scop)
1530 fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
1531 fprintf (file, " %d </TD></TR>\n", bb->index);
1533 fprintf (file, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
1536 FOR_ALL_BB (bb)
1538 FOR_EACH_EDGE (e, ei, bb->succs)
1539 fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
1542 fputs ("}\n\n", file);
1544 /* Enable debugging again. */
1545 dump_flags = tmp_dump_flags;
1548 /* Display all SCoPs using dotty. */
1550 DEBUG_FUNCTION void
1551 dot_all_scops (VEC (scop_p, heap) *scops)
1553 /* When debugging, enable the following code. This cannot be used
1554 in production compilers because it calls "system". */
1555 #if 0
1556 int x;
1557 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1558 gcc_assert (stream);
1560 dot_all_scops_1 (stream, scops);
1561 fclose (stream);
1563 x = system ("dotty /tmp/allscops.dot &");
1564 #else
1565 dot_all_scops_1 (stderr, scops);
1566 #endif
1569 /* Display all SCoPs using dotty. */
1571 DEBUG_FUNCTION void
1572 dot_scop (scop_p scop)
1574 VEC (scop_p, heap) *scops = NULL;
1576 if (scop)
1577 VEC_safe_push (scop_p, heap, scops, scop);
1579 /* When debugging, enable the following code. This cannot be used
1580 in production compilers because it calls "system". */
1581 #if 0
1583 int x;
1584 FILE *stream = fopen ("/tmp/allscops.dot", "w");
1585 gcc_assert (stream);
1587 dot_all_scops_1 (stream, scops);
1588 fclose (stream);
1589 x = system ("dotty /tmp/allscops.dot &");
1591 #else
1592 dot_all_scops_1 (stderr, scops);
1593 #endif
1595 VEC_free (scop_p, heap, scops);
1598 #endif