1 /* Single entry single exit control flow regions.
2 Copyright (C) 2008-2015 Free Software Foundation, Inc.
3 Contributed by Jan Sjodin <jan.sjodin@amd.com> and
4 Sebastian Pop <sebastian.pop@amd.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "double-int.h"
37 #include "fold-const.h"
38 #include "tree-pretty-print.h"
41 #include "hard-reg-set.h"
44 #include "dominance.h"
46 #include "basic-block.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "gimple-fold.h"
51 #include "gimple-expr.h"
55 #include "gimple-iterator.h"
56 #include "gimplify-me.h"
57 #include "gimple-ssa.h"
59 #include "tree-phinodes.h"
60 #include "ssa-iterators.h"
61 #include "stringpool.h"
62 #include "tree-ssanames.h"
63 #include "tree-ssa-loop.h"
64 #include "tree-into-ssa.h"
66 #include "tree-chrec.h"
67 #include "tree-data-ref.h"
68 #include "tree-scalar-evolution.h"
69 #include "tree-pass.h"
70 #include "value-prof.h"
72 #include "tree-ssa-propagate.h"
74 /* Helper function for debug_rename_map. */
77 debug_rename_map_1 (tree_node
*const &old_name
, tree_node
*const &expr
,
80 fprintf (stderr
, "(");
81 print_generic_expr (stderr
, old_name
, 0);
82 fprintf (stderr
, ", ");
83 print_generic_expr (stderr
, expr
, 0);
84 fprintf (stderr
, ")\n");
89 /* Hashtable helpers. */
91 struct rename_map_hasher
: default_hashmap_traits
93 static inline hashval_t
hash (tree
);
96 /* Computes a hash function for database element ELT. */
99 rename_map_hasher::hash (tree old_name
)
101 return SSA_NAME_VERSION (old_name
);
104 typedef hash_map
<tree
, tree
, rename_map_hasher
> rename_map_type
;
107 /* Print to stderr all the elements of RENAME_MAP. */
110 debug_rename_map (rename_map_type
*rename_map
)
112 rename_map
->traverse
<void *, debug_rename_map_1
> (NULL
);
116 /* Record LOOP as occurring in REGION. */
119 sese_record_loop (sese region
, loop_p loop
)
121 if (sese_contains_loop (region
, loop
))
124 bitmap_set_bit (SESE_LOOPS (region
), loop
->num
);
125 SESE_LOOP_NEST (region
).safe_push (loop
);
128 /* Build the loop nests contained in REGION. Returns true when the
129 operation was successful. */
132 build_sese_loop_nests (sese region
)
136 struct loop
*loop0
, *loop1
;
138 FOR_EACH_BB_FN (bb
, cfun
)
139 if (bb_in_sese_p (bb
, region
))
141 struct loop
*loop
= bb
->loop_father
;
143 /* Only add loops if they are completely contained in the SCoP. */
144 if (loop
->header
== bb
145 && bb_in_sese_p (loop
->latch
, region
))
146 sese_record_loop (region
, loop
);
149 /* Make sure that the loops in the SESE_LOOP_NEST are ordered. It
150 can be the case that an inner loop is inserted before an outer
151 loop. To avoid this, semi-sort once. */
152 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region
), i
, loop0
)
154 if (SESE_LOOP_NEST (region
).length () == i
+ 1)
157 loop1
= SESE_LOOP_NEST (region
)[i
+ 1];
158 if (loop0
->num
> loop1
->num
)
160 SESE_LOOP_NEST (region
)[i
] = loop1
;
161 SESE_LOOP_NEST (region
)[i
+ 1] = loop0
;
166 /* For a USE in BB, if BB is outside REGION, mark the USE in the
170 sese_build_liveouts_use (sese region
, bitmap liveouts
, basic_block bb
,
176 if (TREE_CODE (use
) != SSA_NAME
)
179 ver
= SSA_NAME_VERSION (use
);
180 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (use
));
183 || !bb_in_sese_p (def_bb
, region
)
184 || bb_in_sese_p (bb
, region
))
187 bitmap_set_bit (liveouts
, ver
);
190 /* Marks for rewrite all the SSA_NAMES defined in REGION and that are
191 used in BB that is outside of the REGION. */
194 sese_build_liveouts_bb (sese region
, bitmap liveouts
, basic_block bb
)
201 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
202 for (gphi_iterator bsi
= gsi_start_phis (e
->dest
); !gsi_end_p (bsi
);
204 sese_build_liveouts_use (region
, liveouts
, bb
,
205 PHI_ARG_DEF_FROM_EDGE (bsi
.phi (), e
));
207 for (gimple_stmt_iterator bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
);
210 gimple stmt
= gsi_stmt (bsi
);
212 if (is_gimple_debug (stmt
))
215 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
216 sese_build_liveouts_use (region
, liveouts
, bb
, USE_FROM_PTR (use_p
));
220 /* For a USE in BB, return true if BB is outside REGION and it's not
221 in the LIVEOUTS set. */
224 sese_bad_liveouts_use (sese region
, bitmap liveouts
, basic_block bb
,
230 if (TREE_CODE (use
) != SSA_NAME
)
233 ver
= SSA_NAME_VERSION (use
);
235 /* If it's in liveouts, the variable will get a new PHI node, and
236 the debug use will be properly adjusted. */
237 if (bitmap_bit_p (liveouts
, ver
))
240 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (use
));
243 || !bb_in_sese_p (def_bb
, region
)
244 || bb_in_sese_p (bb
, region
))
250 /* Reset debug stmts that reference SSA_NAMES defined in REGION that
251 are not marked as liveouts. */
254 sese_reset_debug_liveouts_bb (sese region
, bitmap liveouts
, basic_block bb
)
256 gimple_stmt_iterator bsi
;
260 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
262 gimple stmt
= gsi_stmt (bsi
);
264 if (!is_gimple_debug (stmt
))
267 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
268 if (sese_bad_liveouts_use (region
, liveouts
, bb
,
269 USE_FROM_PTR (use_p
)))
271 gimple_debug_bind_reset_value (stmt
);
278 /* Build the LIVEOUTS of REGION: the set of variables defined inside
279 and used outside the REGION. */
282 sese_build_liveouts (sese region
, bitmap liveouts
)
286 FOR_EACH_BB_FN (bb
, cfun
)
287 sese_build_liveouts_bb (region
, liveouts
, bb
);
288 if (MAY_HAVE_DEBUG_STMTS
)
289 FOR_EACH_BB_FN (bb
, cfun
)
290 sese_reset_debug_liveouts_bb (region
, liveouts
, bb
);
293 /* Builds a new SESE region from edges ENTRY and EXIT. */
296 new_sese (edge entry
, edge exit
)
298 sese region
= XNEW (struct sese_s
);
300 SESE_ENTRY (region
) = entry
;
301 SESE_EXIT (region
) = exit
;
302 SESE_LOOPS (region
) = BITMAP_ALLOC (NULL
);
303 SESE_LOOP_NEST (region
).create (3);
304 SESE_ADD_PARAMS (region
) = true;
305 SESE_PARAMS (region
).create (3);
310 /* Deletes REGION. */
313 free_sese (sese region
)
315 if (SESE_LOOPS (region
))
316 SESE_LOOPS (region
) = BITMAP_ALLOC (NULL
);
318 SESE_PARAMS (region
).release ();
319 SESE_LOOP_NEST (region
).release ();
324 /* Add exit phis for USE on EXIT. */
327 sese_add_exit_phis_edge (basic_block exit
, tree use
, edge false_e
, edge true_e
)
329 gphi
*phi
= create_phi_node (NULL_TREE
, exit
);
330 create_new_def_for (use
, phi
, gimple_phi_result_ptr (phi
));
331 add_phi_arg (phi
, use
, false_e
, UNKNOWN_LOCATION
);
332 add_phi_arg (phi
, use
, true_e
, UNKNOWN_LOCATION
);
335 /* Insert in the block BB phi nodes for variables defined in REGION
336 and used outside the REGION. The code generation moves REGION in
337 the else clause of an "if (1)" and generates code in the then
338 clause that is at this point empty:
347 sese_insert_phis_for_liveouts (sese region
, basic_block bb
,
348 edge false_e
, edge true_e
)
352 bitmap liveouts
= BITMAP_ALLOC (NULL
);
354 update_ssa (TODO_update_ssa
);
356 sese_build_liveouts (region
, liveouts
);
357 EXECUTE_IF_SET_IN_BITMAP (liveouts
, 0, i
, bi
)
358 sese_add_exit_phis_edge (bb
, ssa_name (i
), false_e
, true_e
);
359 BITMAP_FREE (liveouts
);
361 update_ssa (TODO_update_ssa
);
364 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set. */
367 get_true_edge_from_guard_bb (basic_block bb
)
372 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
373 if (e
->flags
& EDGE_TRUE_VALUE
)
380 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared. */
383 get_false_edge_from_guard_bb (basic_block bb
)
388 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
389 if (!(e
->flags
& EDGE_TRUE_VALUE
))
396 /* Returns the expression associated to OLD_NAME in RENAME_MAP. */
399 get_rename (rename_map_type
*rename_map
, tree old_name
)
401 gcc_assert (TREE_CODE (old_name
) == SSA_NAME
);
402 tree
*expr
= rename_map
->get (old_name
);
409 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR). */
412 set_rename (rename_map_type
*rename_map
, tree old_name
, tree expr
)
414 if (old_name
== expr
)
417 rename_map
->put (old_name
, expr
);
420 /* Renames the scalar uses of the statement COPY, using the
421 substitution map RENAME_MAP, inserting the gimplification code at
422 GSI_TGT, for the translation REGION, with the original copied
423 statement in LOOP, and using the induction variable renaming map
424 IV_MAP. Returns true when something has been renamed. GLOOG_ERROR
425 is set when the code generation cannot continue. */
428 rename_uses (gimple copy
, rename_map_type
*rename_map
,
429 gimple_stmt_iterator
*gsi_tgt
,
430 sese region
, loop_p loop
, vec
<tree
> iv_map
,
435 bool changed
= false;
437 if (is_gimple_debug (copy
))
439 if (gimple_debug_bind_p (copy
))
440 gimple_debug_bind_reset_value (copy
);
441 else if (gimple_debug_source_bind_p (copy
))
449 FOR_EACH_SSA_USE_OPERAND (use_p
, copy
, op_iter
, SSA_OP_USE
)
451 tree old_name
= USE_FROM_PTR (use_p
);
455 if (TREE_CODE (old_name
) != SSA_NAME
456 || SSA_NAME_IS_DEFAULT_DEF (old_name
))
460 new_expr
= get_rename (rename_map
, old_name
);
463 tree type_old_name
= TREE_TYPE (old_name
);
464 tree type_new_expr
= TREE_TYPE (new_expr
);
466 if (type_old_name
!= type_new_expr
467 || TREE_CODE (new_expr
) != SSA_NAME
)
469 tree var
= create_tmp_var (type_old_name
, "var");
471 if (!useless_type_conversion_p (type_old_name
, type_new_expr
))
472 new_expr
= fold_convert (type_old_name
, new_expr
);
474 new_expr
= force_gimple_operand (new_expr
, &stmts
, true, var
);
475 gsi_insert_seq_before (gsi_tgt
, stmts
, GSI_SAME_STMT
);
478 replace_exp (use_p
, new_expr
);
482 scev
= scalar_evolution_in_region (region
, loop
, old_name
);
484 /* At this point we should know the exact scev for each
485 scalar SSA_NAME used in the scop: all the other scalar
486 SSA_NAMEs should have been translated out of SSA using
487 arrays with one element. */
488 if (chrec_contains_undetermined (scev
))
491 new_expr
= build_zero_cst (TREE_TYPE (old_name
));
494 new_expr
= chrec_apply_map (scev
, iv_map
);
496 /* The apply should produce an expression tree containing
497 the uses of the new induction variables. We should be
498 able to use new_expr instead of the old_name in the newly
499 generated loop nest. */
500 if (chrec_contains_undetermined (new_expr
)
501 || tree_contains_chrecs (new_expr
, NULL
))
504 new_expr
= build_zero_cst (TREE_TYPE (old_name
));
507 /* Replace the old_name with the new_expr. */
508 new_expr
= force_gimple_operand (unshare_expr (new_expr
), &stmts
,
511 gsi_insert_seq_before (gsi_tgt
, stmts
, GSI_SAME_STMT
);
512 replace_exp (use_p
, new_expr
);
514 if (TREE_CODE (new_expr
) == INTEGER_CST
515 && is_gimple_assign (copy
))
517 tree rhs
= gimple_assign_rhs1 (copy
);
519 if (TREE_CODE (rhs
) == ADDR_EXPR
)
520 recompute_tree_invariant_for_addr_expr (rhs
);
523 set_rename (rename_map
, old_name
, new_expr
);
529 /* Duplicates the statements of basic block BB into basic block NEW_BB
530 and compute the new induction variables according to the IV_MAP.
531 GLOOG_ERROR is set when the code generation cannot continue. */
534 graphite_copy_stmts_from_block (basic_block bb
, basic_block new_bb
,
535 rename_map_type
*rename_map
,
536 vec
<tree
> iv_map
, sese region
,
539 gimple_stmt_iterator gsi
, gsi_tgt
;
540 loop_p loop
= bb
->loop_father
;
542 gsi_tgt
= gsi_start_bb (new_bb
);
543 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
547 gimple stmt
= gsi_stmt (gsi
);
551 /* Do not copy labels or conditions. */
552 if (gimple_code (stmt
) == GIMPLE_LABEL
553 || gimple_code (stmt
) == GIMPLE_COND
)
556 /* Do not copy induction variables. */
557 if (is_gimple_assign (stmt
)
558 && (lhs
= gimple_assign_lhs (stmt
))
559 && TREE_CODE (lhs
) == SSA_NAME
560 && is_gimple_reg (lhs
)
561 && scev_analyzable_p (lhs
, region
))
564 /* Create a new copy of STMT and duplicate STMT's virtual
566 copy
= gimple_copy (stmt
);
567 gsi_insert_after (&gsi_tgt
, copy
, GSI_NEW_STMT
);
569 maybe_duplicate_eh_stmt (copy
, stmt
);
570 gimple_duplicate_stmt_histograms (cfun
, copy
, cfun
, stmt
);
572 /* Create new names for all the definitions created by COPY and
573 add replacement mappings for each new name. */
574 FOR_EACH_SSA_DEF_OPERAND (def_p
, copy
, op_iter
, SSA_OP_ALL_DEFS
)
576 tree old_name
= DEF_FROM_PTR (def_p
);
577 tree new_name
= create_new_def_for (old_name
, copy
, def_p
);
578 set_rename (rename_map
, old_name
, new_name
);
581 if (rename_uses (copy
, rename_map
, &gsi_tgt
, region
, loop
, iv_map
,
584 gcc_assert (gsi_stmt (gsi_tgt
) == copy
);
585 fold_stmt_inplace (&gsi_tgt
);
592 /* Copies BB and includes in the copied BB all the statements that can
593 be reached following the use-def chains from the memory accesses,
594 and returns the next edge following this new block. GLOOG_ERROR is
595 set when the code generation cannot continue. */
598 copy_bb_and_scalar_dependences (basic_block bb
, sese region
,
599 edge next_e
, vec
<tree
> iv_map
,
602 basic_block new_bb
= split_edge (next_e
);
603 rename_map_type
rename_map (10);
605 next_e
= single_succ_edge (new_bb
);
606 graphite_copy_stmts_from_block (bb
, new_bb
, &rename_map
, iv_map
, region
,
608 remove_phi_nodes (new_bb
);
613 /* Returns the outermost loop in SCOP that contains BB. */
616 outermost_loop_in_sese (sese region
, basic_block bb
)
620 nest
= bb
->loop_father
;
621 while (loop_outer (nest
)
622 && loop_in_sese_p (loop_outer (nest
), region
))
623 nest
= loop_outer (nest
);
628 /* Sets the false region of an IF_REGION to REGION. */
631 if_region_set_false_region (ifsese if_region
, sese region
)
633 basic_block condition
= if_region_get_condition_block (if_region
);
634 edge false_edge
= get_false_edge_from_guard_bb (condition
);
635 basic_block dummy
= false_edge
->dest
;
636 edge entry_region
= SESE_ENTRY (region
);
637 edge exit_region
= SESE_EXIT (region
);
638 basic_block before_region
= entry_region
->src
;
639 basic_block last_in_region
= exit_region
->src
;
640 hashval_t hash
= htab_hash_pointer (exit_region
);
642 = current_loops
->exits
->find_slot_with_hash (exit_region
, hash
, NO_INSERT
);
644 entry_region
->flags
= false_edge
->flags
;
645 false_edge
->flags
= exit_region
->flags
;
647 redirect_edge_pred (entry_region
, condition
);
648 redirect_edge_pred (exit_region
, before_region
);
649 redirect_edge_pred (false_edge
, last_in_region
);
650 redirect_edge_succ (false_edge
, single_succ (dummy
));
651 delete_basic_block (dummy
);
653 exit_region
->flags
= EDGE_FALLTHRU
;
654 recompute_all_dominators ();
656 SESE_EXIT (region
) = false_edge
;
658 free (if_region
->false_region
);
659 if_region
->false_region
= region
;
663 struct loop_exit
*loop_exit
= ggc_cleared_alloc
<struct loop_exit
> ();
665 memcpy (loop_exit
, *((struct loop_exit
**) slot
), sizeof (struct loop_exit
));
666 current_loops
->exits
->clear_slot (slot
);
668 hashval_t hash
= htab_hash_pointer (false_edge
);
669 slot
= current_loops
->exits
->find_slot_with_hash (false_edge
, hash
,
671 loop_exit
->e
= false_edge
;
673 false_edge
->src
->loop_father
->exits
->next
= loop_exit
;
677 /* Creates an IFSESE with CONDITION on edge ENTRY. */
680 create_if_region_on_edge (edge entry
, tree condition
)
684 sese sese_region
= XNEW (struct sese_s
);
685 sese true_region
= XNEW (struct sese_s
);
686 sese false_region
= XNEW (struct sese_s
);
687 ifsese if_region
= XNEW (struct ifsese_s
);
688 edge exit
= create_empty_if_region_on_edge (entry
, condition
);
690 if_region
->region
= sese_region
;
691 if_region
->region
->entry
= entry
;
692 if_region
->region
->exit
= exit
;
694 FOR_EACH_EDGE (e
, ei
, entry
->dest
->succs
)
696 if (e
->flags
& EDGE_TRUE_VALUE
)
698 true_region
->entry
= e
;
699 true_region
->exit
= single_succ_edge (e
->dest
);
700 if_region
->true_region
= true_region
;
702 else if (e
->flags
& EDGE_FALSE_VALUE
)
704 false_region
->entry
= e
;
705 false_region
->exit
= single_succ_edge (e
->dest
);
706 if_region
->false_region
= false_region
;
713 /* Moves REGION in a condition expression:
721 move_sese_in_condition (sese region
)
723 basic_block pred_block
= split_edge (SESE_ENTRY (region
));
726 SESE_ENTRY (region
) = single_succ_edge (pred_block
);
727 if_region
= create_if_region_on_edge (single_pred_edge (pred_block
), integer_one_node
);
728 if_region_set_false_region (if_region
, region
);
733 /* Replaces the condition of the IF_REGION with CONDITION:
741 set_ifsese_condition (ifsese if_region
, tree condition
)
743 sese region
= if_region
->region
;
744 edge entry
= region
->entry
;
745 basic_block bb
= entry
->dest
;
746 gimple last
= last_stmt (bb
);
747 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
750 gcc_assert (gimple_code (last
) == GIMPLE_COND
);
752 gsi_remove (&gsi
, true);
753 gsi
= gsi_last_bb (bb
);
754 condition
= force_gimple_operand_gsi (&gsi
, condition
, true, NULL
,
755 false, GSI_NEW_STMT
);
756 cond_stmt
= gimple_build_cond_from_tree (condition
, NULL_TREE
, NULL_TREE
);
757 gsi
= gsi_last_bb (bb
);
758 gsi_insert_after (&gsi
, cond_stmt
, GSI_NEW_STMT
);
761 /* Returns the scalar evolution of T in REGION. Every variable that
762 is not defined in the REGION is considered a parameter. */
765 scalar_evolution_in_region (sese region
, loop_p loop
, tree t
)
768 struct loop
*def_loop
;
769 basic_block before
= block_before_sese (region
);
771 /* SCOP parameters. */
772 if (TREE_CODE (t
) == SSA_NAME
773 && !defined_in_sese_p (t
, region
))
776 if (TREE_CODE (t
) != SSA_NAME
777 || loop_in_sese_p (loop
, region
))
778 return instantiate_scev (before
, loop
,
779 analyze_scalar_evolution (loop
, t
));
781 def
= SSA_NAME_DEF_STMT (t
);
782 def_loop
= loop_containing_stmt (def
);
784 if (loop_in_sese_p (def_loop
, region
))
786 t
= analyze_scalar_evolution (def_loop
, t
);
787 def_loop
= superloop_at_depth (def_loop
, loop_depth (loop
) + 1);
788 t
= compute_overall_effect_of_inner_loop (def_loop
, t
);
792 return instantiate_scev (before
, loop
, t
);