1 /* Single entry single exit control flow regions.
2 Copyright (C) 2008, 2009 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 "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
33 #include "tree-dump.h"
36 #include "tree-chrec.h"
37 #include "tree-data-ref.h"
38 #include "tree-scalar-evolution.h"
39 #include "tree-pass.h"
41 #include "value-prof.h"
42 #include "pointer-set.h"
46 /* Print to stderr the element ELT. */
49 debug_rename_elt (rename_map_elt elt)
51 fprintf (stderr, "(");
52 print_generic_expr (stderr, elt->old_name, 0);
53 fprintf (stderr, ", ");
54 print_generic_expr (stderr, elt->expr, 0);
55 fprintf (stderr, ")\n");
58 /* Helper function for debug_rename_map. */
61 debug_rename_map_1 (void **slot, void *s ATTRIBUTE_UNUSED)
63 struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
64 debug_rename_elt (entry);
68 /* Print to stderr all the elements of MAP. */
71 debug_rename_map (htab_t map)
73 htab_traverse (map, debug_rename_map_1, NULL);
76 /* Computes a hash function for database element ELT. */
79 rename_map_elt_info (const void *elt)
81 return SSA_NAME_VERSION (((const struct rename_map_elt_s *) elt)->old_name);
84 /* Compares database elements E1 and E2. */
87 eq_rename_map_elts (const void *e1, const void *e2)
89 const struct rename_map_elt_s *elt1 = (const struct rename_map_elt_s *) e1;
90 const struct rename_map_elt_s *elt2 = (const struct rename_map_elt_s *) e2;
92 return (elt1->old_name == elt2->old_name);
97 /* Print to stderr the element ELT. */
100 debug_ivtype_elt (ivtype_map_elt elt)
102 fprintf (stderr, "(%s, ", elt->cloog_iv);
103 print_generic_expr (stderr, elt->type, 0);
104 fprintf (stderr, ")\n");
107 /* Helper function for debug_ivtype_map. */
110 debug_ivtype_map_1 (void **slot, void *s ATTRIBUTE_UNUSED)
112 struct ivtype_map_elt_s *entry = (struct ivtype_map_elt_s *) *slot;
113 debug_ivtype_elt (entry);
117 /* Print to stderr all the elements of MAP. */
120 debug_ivtype_map (htab_t map)
122 htab_traverse (map, debug_ivtype_map_1, NULL);
125 /* Computes a hash function for database element ELT. */
128 ivtype_map_elt_info (const void *elt)
130 return htab_hash_pointer (((const struct ivtype_map_elt_s *) elt)->cloog_iv);
133 /* Compares database elements E1 and E2. */
136 eq_ivtype_map_elts (const void *e1, const void *e2)
138 const struct ivtype_map_elt_s *elt1 = (const struct ivtype_map_elt_s *) e1;
139 const struct ivtype_map_elt_s *elt2 = (const struct ivtype_map_elt_s *) e2;
141 return (elt1->cloog_iv == elt2->cloog_iv);
146 /* Record LOOP as occuring in REGION. */
149 sese_record_loop (sese region, loop_p loop)
151 if (sese_contains_loop (region, loop))
154 bitmap_set_bit (SESE_LOOPS (region), loop->num);
155 VEC_safe_push (loop_p, heap, SESE_LOOP_NEST (region), loop);
158 /* Build the loop nests contained in REGION. Returns true when the
159 operation was successful. */
162 build_sese_loop_nests (sese region)
166 struct loop *loop0, *loop1;
169 if (bb_in_sese_p (bb, region))
171 struct loop *loop = bb->loop_father;
173 /* Only add loops if they are completely contained in the SCoP. */
174 if (loop->header == bb
175 && bb_in_sese_p (loop->latch, region))
176 sese_record_loop (region, loop);
179 /* Make sure that the loops in the SESE_LOOP_NEST are ordered. It
180 can be the case that an inner loop is inserted before an outer
181 loop. To avoid this, semi-sort once. */
182 for (i = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), i, loop0); i++)
184 if (VEC_length (loop_p, SESE_LOOP_NEST (region)) == i + 1)
187 loop1 = VEC_index (loop_p, SESE_LOOP_NEST (region), i + 1);
188 if (loop0->num > loop1->num)
190 VEC_replace (loop_p, SESE_LOOP_NEST (region), i, loop1);
191 VEC_replace (loop_p, SESE_LOOP_NEST (region), i + 1, loop0);
196 /* For a USE in BB, if BB is outside REGION, mark the USE in the
200 sese_build_liveouts_use (sese region, bitmap liveouts, basic_block bb,
206 if (TREE_CODE (use) != SSA_NAME)
209 ver = SSA_NAME_VERSION (use);
210 def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
213 || !bb_in_sese_p (def_bb, region)
214 || bb_in_sese_p (bb, region))
217 bitmap_set_bit (liveouts, ver);
220 /* Marks for rewrite all the SSA_NAMES defined in REGION and that are
221 used in BB that is outside of the REGION. */
224 sese_build_liveouts_bb (sese region, bitmap liveouts, basic_block bb)
226 gimple_stmt_iterator bsi;
232 FOR_EACH_EDGE (e, ei, bb->succs)
233 for (bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi); gsi_next (&bsi))
234 sese_build_liveouts_use (region, liveouts, bb,
235 PHI_ARG_DEF_FROM_EDGE (gsi_stmt (bsi), e));
237 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
239 gimple stmt = gsi_stmt (bsi);
241 if (is_gimple_debug (stmt))
244 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
245 sese_build_liveouts_use (region, liveouts, bb, USE_FROM_PTR (use_p));
249 /* For a USE in BB, return true if BB is outside REGION and it's not
250 in the LIVEOUTS set. */
253 sese_bad_liveouts_use (sese region, bitmap liveouts, basic_block bb,
259 if (TREE_CODE (use) != SSA_NAME)
262 ver = SSA_NAME_VERSION (use);
264 /* If it's in liveouts, the variable will get a new PHI node, and
265 the debug use will be properly adjusted. */
266 if (bitmap_bit_p (liveouts, ver))
269 def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
272 || !bb_in_sese_p (def_bb, region)
273 || bb_in_sese_p (bb, region))
279 /* Reset debug stmts that reference SSA_NAMES defined in REGION that
280 are not marked as liveouts. */
283 sese_reset_debug_liveouts_bb (sese region, bitmap liveouts, basic_block bb)
285 gimple_stmt_iterator bsi;
289 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
291 gimple stmt = gsi_stmt (bsi);
293 if (!is_gimple_debug (stmt))
296 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
297 if (sese_bad_liveouts_use (region, liveouts, bb,
298 USE_FROM_PTR (use_p)))
300 gimple_debug_bind_reset_value (stmt);
307 /* Build the LIVEOUTS of REGION: the set of variables defined inside
308 and used outside the REGION. */
311 sese_build_liveouts (sese region, bitmap liveouts)
316 sese_build_liveouts_bb (region, liveouts, bb);
317 if (MAY_HAVE_DEBUG_INSNS)
319 sese_reset_debug_liveouts_bb (region, liveouts, bb);
322 /* Builds a new SESE region from edges ENTRY and EXIT. */
325 new_sese (edge entry, edge exit)
327 sese region = XNEW (struct sese_s);
329 SESE_ENTRY (region) = entry;
330 SESE_EXIT (region) = exit;
331 SESE_LOOPS (region) = BITMAP_ALLOC (NULL);
332 SESE_LOOP_NEST (region) = VEC_alloc (loop_p, heap, 3);
333 SESE_ADD_PARAMS (region) = true;
334 SESE_PARAMS (region) = VEC_alloc (tree, heap, 3);
339 /* Deletes REGION. */
342 free_sese (sese region)
344 if (SESE_LOOPS (region))
345 SESE_LOOPS (region) = BITMAP_ALLOC (NULL);
347 VEC_free (tree, heap, SESE_PARAMS (region));
348 VEC_free (loop_p, heap, SESE_LOOP_NEST (region));
353 /* Add exit phis for USE on EXIT. */
356 sese_add_exit_phis_edge (basic_block exit, tree use, edge false_e, edge true_e)
358 gimple phi = create_phi_node (use, exit);
360 create_new_def_for (gimple_phi_result (phi), phi,
361 gimple_phi_result_ptr (phi));
362 add_phi_arg (phi, use, false_e, UNKNOWN_LOCATION);
363 add_phi_arg (phi, use, true_e, UNKNOWN_LOCATION);
366 /* Insert in the block BB phi nodes for variables defined in REGION
367 and used outside the REGION. The code generation moves REGION in
368 the else clause of an "if (1)" and generates code in the then
369 clause that is at this point empty:
378 sese_insert_phis_for_liveouts (sese region, basic_block bb,
379 edge false_e, edge true_e)
383 bitmap liveouts = BITMAP_ALLOC (NULL);
385 update_ssa (TODO_update_ssa);
387 sese_build_liveouts (region, liveouts);
388 EXECUTE_IF_SET_IN_BITMAP (liveouts, 0, i, bi)
389 sese_add_exit_phis_edge (bb, ssa_name (i), false_e, true_e);
390 BITMAP_FREE (liveouts);
392 update_ssa (TODO_update_ssa);
395 /* Get the definition of NAME before the SESE. Keep track of the
396 basic blocks that have been VISITED in a bitmap. */
399 get_vdef_before_sese (sese region, tree name, sbitmap visited)
402 gimple stmt = SSA_NAME_DEF_STMT (name);
403 basic_block def_bb = gimple_bb (stmt);
405 if (!def_bb || !bb_in_sese_p (def_bb, region))
408 if (TEST_BIT (visited, def_bb->index))
411 SET_BIT (visited, def_bb->index);
413 switch (gimple_code (stmt))
416 for (i = 0; i < gimple_phi_num_args (stmt); i++)
418 tree arg = gimple_phi_arg_def (stmt, i);
421 if (gimple_bb (SSA_NAME_DEF_STMT (arg))
422 && def_bb->index == gimple_bb (SSA_NAME_DEF_STMT (arg))->index)
425 res = get_vdef_before_sese (region, arg, visited);
434 use_operand_p use_p = gimple_vuse_op (stmt);
435 tree use = USE_FROM_PTR (use_p);
437 if (def_bb->index == gimple_bb (SSA_NAME_DEF_STMT (use))->index)
438 RESET_BIT (visited, def_bb->index);
440 return get_vdef_before_sese (region, use, visited);
448 /* Adjust a virtual phi node PHI that is placed at the end of the
449 generated code for SCOP:
452 | generated code from REGION;
456 The FALSE_E edge comes from the original code, TRUE_E edge comes
457 from the code generated for the SCOP. */
460 sese_adjust_vphi (sese region, gimple phi, edge true_e)
464 gcc_assert (gimple_phi_num_args (phi) == 2);
466 for (i = 0; i < gimple_phi_num_args (phi); i++)
467 if (gimple_phi_arg_edge (phi, i) == true_e)
469 tree true_arg, false_arg, before_scop_arg;
472 true_arg = gimple_phi_arg_def (phi, i);
473 if (!SSA_NAME_IS_DEFAULT_DEF (true_arg))
476 false_arg = gimple_phi_arg_def (phi, i == 0 ? 1 : 0);
477 if (SSA_NAME_IS_DEFAULT_DEF (false_arg))
480 visited = sbitmap_alloc (last_basic_block);
481 sbitmap_zero (visited);
482 before_scop_arg = get_vdef_before_sese (region, false_arg, visited);
483 gcc_assert (before_scop_arg != NULL_TREE);
484 SET_PHI_ARG_DEF (phi, i, before_scop_arg);
485 sbitmap_free (visited);
489 /* Returns the name associated to OLD_NAME in MAP. */
492 get_rename (htab_t map, tree old_name)
494 struct rename_map_elt_s tmp;
497 tmp.old_name = old_name;
498 slot = htab_find_slot (map, &tmp, NO_INSERT);
501 return ((rename_map_elt) *slot)->expr;
506 /* Register in MAP the rename tuple (old_name, expr). */
509 set_rename (htab_t map, tree old_name, tree expr)
511 struct rename_map_elt_s tmp;
514 if (old_name == expr)
517 tmp.old_name = old_name;
518 slot = htab_find_slot (map, &tmp, INSERT);
526 *slot = new_rename_map_elt (old_name, expr);
529 /* Adjusts the phi nodes in the block BB for variables defined in
530 SCOP_REGION and used outside the SCOP_REGION. The code generation
531 moves SCOP_REGION in the else clause of an "if (1)" and generates
532 code in the then clause:
535 | generated code from REGION;
539 To adjust the phi nodes after the condition, the RENAME_MAP is
543 sese_adjust_liveout_phis (sese region, htab_t rename_map, basic_block bb,
544 edge false_e, edge true_e)
546 gimple_stmt_iterator si;
548 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
551 unsigned false_i = 0;
552 gimple phi = gsi_stmt (si);
554 if (!is_gimple_reg (PHI_RESULT (phi)))
556 sese_adjust_vphi (region, phi, true_e);
560 for (i = 0; i < gimple_phi_num_args (phi); i++)
561 if (gimple_phi_arg_edge (phi, i) == false_e)
567 for (i = 0; i < gimple_phi_num_args (phi); i++)
568 if (gimple_phi_arg_edge (phi, i) == true_e)
570 tree old_name = gimple_phi_arg_def (phi, false_i);
571 tree expr = get_rename (rename_map, old_name);
574 gcc_assert (old_name != expr);
576 if (TREE_CODE (expr) != SSA_NAME
577 && is_gimple_reg (old_name))
579 tree type = TREE_TYPE (old_name);
580 tree var = create_tmp_var (type, "var");
582 expr = build2 (MODIFY_EXPR, type, var, expr);
583 expr = force_gimple_operand (expr, &stmts, true, NULL);
584 gsi_insert_seq_on_edge_immediate (true_e, stmts);
587 SET_PHI_ARG_DEF (phi, i, expr);
592 /* Rename the SSA_NAMEs used in STMT and that appear in MAP. */
595 rename_variables_in_stmt (gimple stmt, htab_t map, gimple_stmt_iterator *insert_gsi)
600 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
602 tree use = USE_FROM_PTR (use_p);
603 tree expr = get_rename (map, use);
604 tree type_use = TREE_TYPE (use);
605 tree type_expr = TREE_TYPE (expr);
611 if (type_use != type_expr
612 || (TREE_CODE (expr) != SSA_NAME
613 && is_gimple_reg (use)))
617 if (is_gimple_debug (stmt))
619 if (gimple_debug_bind_p (stmt))
620 gimple_debug_bind_reset_value (stmt);
627 var = create_tmp_var (type_use, "var");
629 if (type_use != type_expr)
630 expr = fold_convert (type_use, expr);
632 expr = build2 (MODIFY_EXPR, type_use, var, expr);
633 expr = force_gimple_operand (expr, &stmts, true, NULL);
634 gsi_insert_seq_before (insert_gsi, stmts, GSI_SAME_STMT);
637 replace_exp (use_p, expr);
643 /* Returns true if NAME is a parameter of SESE. */
646 is_parameter (sese region, tree name)
651 for (i = 0; VEC_iterate (tree, SESE_PARAMS (region), i, p); i++)
658 /* Returns true if NAME is an induction variable. */
663 return gimple_code (SSA_NAME_DEF_STMT (name)) == GIMPLE_PHI;
666 static void expand_scalar_variables_stmt (gimple, basic_block, sese,
667 htab_t, gimple_stmt_iterator *);
669 expand_scalar_variables_expr (tree, tree, enum tree_code, tree, basic_block,
670 sese, htab_t, gimple_stmt_iterator *);
673 expand_scalar_variables_call (gimple stmt, basic_block bb, sese region,
674 htab_t map, gimple_stmt_iterator *gsi)
676 int i, nargs = gimple_call_num_args (stmt);
677 VEC (tree, gc) *args = VEC_alloc (tree, gc, nargs);
678 tree fn_type = TREE_TYPE (gimple_call_fn (stmt));
679 tree fn = gimple_call_fndecl (stmt);
680 tree call_expr, var, lhs;
683 for (i = 0; i < nargs; i++)
685 tree arg = gimple_call_arg (stmt, i);
686 tree t = TREE_TYPE (arg);
688 var = create_tmp_var (t, "var");
689 arg = expand_scalar_variables_expr (t, arg, TREE_CODE (arg), NULL,
690 bb, region, map, gsi);
691 arg = build2 (MODIFY_EXPR, t, var, arg);
692 arg = force_gimple_operand_gsi (gsi, arg, true, NULL,
693 true, GSI_SAME_STMT);
694 VEC_quick_push (tree, args, arg);
697 lhs = gimple_call_lhs (stmt);
698 var = create_tmp_var (TREE_TYPE (lhs), "var");
699 call_expr = build_call_vec (fn_type, fn, args);
700 call = gimple_build_call_from_tree (call_expr);
701 var = make_ssa_name (var, call);
702 gimple_call_set_lhs (call, var);
703 gsi_insert_before (gsi, call, GSI_SAME_STMT);
708 /* Copies at GSI all the scalar computations on which the ssa_name OP0
709 depends on in the SESE: these are all the scalar variables used in
710 the definition of OP0, that are defined outside BB and still in the
711 SESE, i.e. not a parameter of the SESE. The expression that is
712 returned contains only induction variables from the generated code:
713 MAP contains the induction variables renaming mapping, and is used
714 to translate the names of induction variables. */
717 expand_scalar_variables_ssa_name (tree op0, basic_block bb,
718 sese region, htab_t map,
719 gimple_stmt_iterator *gsi)
724 if (is_parameter (region, op0)
726 return get_rename (map, op0);
728 def_stmt = SSA_NAME_DEF_STMT (op0);
730 /* Check whether we already have a rename for OP0. */
731 new_op = get_rename (map, op0);
734 && gimple_bb (SSA_NAME_DEF_STMT (new_op)) == bb)
737 if (gimple_bb (def_stmt) == bb)
739 /* If the defining statement is in the basic block already
740 we do not need to create a new expression for it, we
741 only need to ensure its operands are expanded. */
742 expand_scalar_variables_stmt (def_stmt, bb, region, map, gsi);
747 if (!gimple_bb (def_stmt)
748 || !bb_in_sese_p (gimple_bb (def_stmt), region))
751 switch (gimple_code (def_stmt))
755 tree var0 = gimple_assign_rhs1 (def_stmt);
756 enum tree_code subcode = gimple_assign_rhs_code (def_stmt);
757 tree var1 = gimple_assign_rhs2 (def_stmt);
758 tree type = gimple_expr_type (def_stmt);
760 return expand_scalar_variables_expr (type, var0, subcode, var1, bb,
765 return expand_scalar_variables_call (def_stmt, bb, region, map, gsi);
774 /* Copies at GSI all the scalar computations on which the expression
775 OP0 CODE OP1 depends on in the SESE: these are all the scalar
776 variables used in OP0 and OP1, defined outside BB and still defined
777 in the SESE, i.e. not a parameter of the SESE. The expression that
778 is returned contains only induction variables from the generated
779 code: MAP contains the induction variables renaming mapping, and is
780 used to translate the names of induction variables. */
783 expand_scalar_variables_expr (tree type, tree op0, enum tree_code code,
784 tree op1, basic_block bb, sese region,
785 htab_t map, gimple_stmt_iterator *gsi)
787 if (TREE_CODE_CLASS (code) == tcc_constant
788 || TREE_CODE_CLASS (code) == tcc_declaration)
791 /* For data references we have to duplicate also its memory
793 if (TREE_CODE_CLASS (code) == tcc_reference)
800 tree op = TREE_OPERAND (op0, 0);
801 tree res = expand_scalar_variables_expr
802 (type, op, TREE_CODE (op), NULL, bb, region, map, gsi);
803 return build1 (code, type, res);
808 tree old_name = TREE_OPERAND (op0, 0);
809 tree expr = expand_scalar_variables_ssa_name
810 (old_name, bb, region, map, gsi);
812 if (TREE_CODE (expr) != SSA_NAME
813 && is_gimple_reg (old_name))
815 tree type = TREE_TYPE (old_name);
816 tree var = create_tmp_var (type, "var");
818 expr = build2 (MODIFY_EXPR, type, var, expr);
819 expr = force_gimple_operand_gsi (gsi, expr, true, NULL,
820 true, GSI_SAME_STMT);
823 return fold_build1 (code, type, expr);
828 tree op00 = TREE_OPERAND (op0, 0);
829 tree op01 = TREE_OPERAND (op0, 1);
830 tree op02 = TREE_OPERAND (op0, 2);
831 tree op03 = TREE_OPERAND (op0, 3);
832 tree base = expand_scalar_variables_expr
833 (TREE_TYPE (op00), op00, TREE_CODE (op00), NULL, bb, region,
835 tree subscript = expand_scalar_variables_expr
836 (TREE_TYPE (op01), op01, TREE_CODE (op01), NULL, bb, region,
839 return build4 (ARRAY_REF, type, base, subscript, op02, op03);
843 /* The above cases should catch everything. */
848 if (TREE_CODE_CLASS (code) == tcc_unary)
850 tree op0_type = TREE_TYPE (op0);
851 enum tree_code op0_code = TREE_CODE (op0);
852 tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
853 NULL, bb, region, map, gsi);
855 return fold_build1 (code, type, op0_expr);
858 if (TREE_CODE_CLASS (code) == tcc_binary
859 || TREE_CODE_CLASS (code) == tcc_comparison)
861 tree op0_type = TREE_TYPE (op0);
862 enum tree_code op0_code = TREE_CODE (op0);
863 tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
864 NULL, bb, region, map, gsi);
865 tree op1_type = TREE_TYPE (op1);
866 enum tree_code op1_code = TREE_CODE (op1);
867 tree op1_expr = expand_scalar_variables_expr (op1_type, op1, op1_code,
868 NULL, bb, region, map, gsi);
870 return fold_build2 (code, type, op0_expr, op1_expr);
873 if (code == SSA_NAME)
874 return expand_scalar_variables_ssa_name (op0, bb, region, map, gsi);
876 if (code == ADDR_EXPR)
883 /* Copies at the beginning of BB all the scalar computations on which
884 STMT depends on in the SESE: these are all the scalar variables used
885 in STMT, defined outside BB and still defined in the SESE, i.e. not a
886 parameter of the SESE. The expression that is returned contains
887 only induction variables from the generated code: MAP contains the
888 induction variables renaming mapping, and is used to translate the
889 names of induction variables. */
892 expand_scalar_variables_stmt (gimple stmt, basic_block bb, sese region,
893 htab_t map, gimple_stmt_iterator *gsi)
898 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
900 tree use = USE_FROM_PTR (use_p);
901 tree type = TREE_TYPE (use);
902 enum tree_code code = TREE_CODE (use);
905 if (!is_gimple_reg (use))
908 /* Don't expand USE if we already have a rename for it. */
909 use_expr = get_rename (map, use);
913 use_expr = expand_scalar_variables_expr (type, use, code, NULL, bb,
915 use_expr = fold_convert (type, use_expr);
920 if (is_gimple_debug (stmt))
922 if (gimple_debug_bind_p (stmt))
923 gimple_debug_bind_reset_value (stmt);
930 if (TREE_CODE (use_expr) != SSA_NAME)
932 tree var = create_tmp_var (type, "var");
934 use_expr = build2 (MODIFY_EXPR, type, var, use_expr);
935 use_expr = force_gimple_operand_gsi (gsi, use_expr, true, NULL,
936 true, GSI_SAME_STMT);
939 replace_exp (use_p, use_expr);
945 /* Copies at the beginning of BB all the scalar computations on which
946 BB depends on in the SESE: these are all the scalar variables used
947 in BB, defined outside BB and still defined in the SESE, i.e. not a
948 parameter of the SESE. The expression that is returned contains
949 only induction variables from the generated code: MAP contains the
950 induction variables renaming mapping, and is used to translate the
951 names of induction variables. */
954 expand_scalar_variables (basic_block bb, sese region, htab_t map)
956 gimple_stmt_iterator gsi;
958 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
960 gimple stmt = gsi_stmt (gsi);
961 expand_scalar_variables_stmt (stmt, bb, region, map, &gsi);
966 /* Rename all the SSA_NAMEs from block BB according to the MAP. */
969 rename_variables (basic_block bb, htab_t map)
971 gimple_stmt_iterator gsi;
972 gimple_stmt_iterator insert_gsi = gsi_start_bb (bb);
974 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
975 rename_variables_in_stmt (gsi_stmt (gsi), map, &insert_gsi);
978 /* Remove condition from BB. */
981 remove_condition (basic_block bb)
983 gimple last = last_stmt (bb);
985 if (last && gimple_code (last) == GIMPLE_COND)
987 gimple_stmt_iterator gsi = gsi_last_bb (bb);
988 gsi_remove (&gsi, true);
992 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set. */
995 get_true_edge_from_guard_bb (basic_block bb)
1000 FOR_EACH_EDGE (e, ei, bb->succs)
1001 if (e->flags & EDGE_TRUE_VALUE)
1008 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared. */
1011 get_false_edge_from_guard_bb (basic_block bb)
1016 FOR_EACH_EDGE (e, ei, bb->succs)
1017 if (!(e->flags & EDGE_TRUE_VALUE))
1024 /* Returns true when NAME is defined in LOOP. */
1027 defined_in_loop_p (tree name, loop_p loop)
1029 gimple stmt = SSA_NAME_DEF_STMT (name);
1031 return (gimple_bb (stmt)->loop_father == loop);
1034 /* Returns the gimple statement that uses NAME outside the loop it is
1035 defined in, returns NULL if there is no such loop close phi node.
1036 An invariant of the loop closed SSA form is that the only use of a
1037 variable, outside the loop it is defined in, is in the loop close
1038 phi node that just follows the loop. */
1041 alive_after_loop (tree name)
1043 use_operand_p use_p;
1044 imm_use_iterator imm_iter;
1045 loop_p loop = gimple_bb (SSA_NAME_DEF_STMT (name))->loop_father;
1047 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
1049 gimple stmt = USE_STMT (use_p);
1051 if (gimple_code (stmt) == GIMPLE_PHI
1052 && gimple_bb (stmt)->loop_father != loop)
1059 /* Return true if a close phi has not yet been inserted for the use of
1060 variable NAME on the single exit of LOOP. */
1063 close_phi_not_yet_inserted_p (loop_p loop, tree name)
1065 gimple_stmt_iterator psi;
1066 basic_block bb = single_exit (loop)->dest;
1068 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1069 if (gimple_phi_arg_def (gsi_stmt (psi), 0) == name)
1075 /* A structure for passing parameters to add_loop_exit_phis. */
1077 typedef struct alep {
1079 VEC (rename_map_elt, heap) *new_renames;
1082 /* Helper function for htab_traverse in insert_loop_close_phis. */
1085 add_loop_exit_phis (void **slot, void *data)
1087 struct rename_map_elt_s *entry;
1090 tree expr, new_name;
1091 bool def_in_loop_p, used_outside_p, need_close_phi_p;
1092 gimple old_close_phi;
1097 entry = (struct rename_map_elt_s *) *slot;
1102 if (TREE_CODE (expr) != SSA_NAME)
1106 def_in_loop_p = defined_in_loop_p (new_name, loop);
1107 old_close_phi = alive_after_loop (entry->old_name);
1108 used_outside_p = (old_close_phi != NULL);
1109 need_close_phi_p = (def_in_loop_p && used_outside_p
1110 && close_phi_not_yet_inserted_p (loop, new_name));
1112 /* Insert a loop close phi node. */
1113 if (need_close_phi_p)
1115 basic_block bb = single_exit (loop)->dest;
1116 gimple phi = create_phi_node (new_name, bb);
1117 tree new_res = create_new_def_for (gimple_phi_result (phi), phi,
1118 gimple_phi_result_ptr (phi));
1120 add_phi_arg (phi, new_name, single_pred_edge (bb), UNKNOWN_LOCATION);
1121 VEC_safe_push (rename_map_elt, heap, a->new_renames,
1122 new_rename_map_elt (gimple_phi_result (old_close_phi),
1126 /* Remove the old rename from the map. */
1127 if (def_in_loop_p && *slot)
1136 /* Traverses MAP and removes from it all the tuples (OLD, NEW) where
1137 NEW is defined in LOOP. Inserts on the exit of LOOP the close phi
1138 node "RES = phi (NEW)" corresponding to "OLD_RES = phi (OLD)" in
1139 the original code. Inserts in MAP the tuple (OLD_RES, RES). */
1142 insert_loop_close_phis (htab_t map, loop_p loop)
1149 a.new_renames = VEC_alloc (rename_map_elt, heap, 3);
1150 update_ssa (TODO_update_ssa);
1151 htab_traverse (map, add_loop_exit_phis, &a);
1152 update_ssa (TODO_update_ssa);
1154 for (i = 0; VEC_iterate (rename_map_elt, a.new_renames, i, elt); i++)
1156 set_rename (map, elt->old_name, elt->expr);
1160 VEC_free (rename_map_elt, heap, a.new_renames);
1163 /* Helper structure for htab_traverse in insert_guard_phis. */
1167 edge true_edge, false_edge;
1168 htab_t before_guard;
1171 /* Return the default name that is before the guard. */
1174 default_before_guard (htab_t before_guard, tree old_name)
1176 tree res = get_rename (before_guard, old_name);
1178 if (res == old_name)
1180 if (is_gimple_reg (res))
1181 return fold_convert (TREE_TYPE (res), integer_zero_node);
1182 return gimple_default_def (cfun, SSA_NAME_VAR (res));
1188 /* Prepares EXPR to be a good phi argument when the phi result is
1189 RES. Insert needed statements on edge E. */
1192 convert_for_phi_arg (tree expr, tree res, edge e)
1194 tree type = TREE_TYPE (res);
1196 if (TREE_TYPE (expr) != type)
1197 expr = fold_convert (type, expr);
1199 if (TREE_CODE (expr) != SSA_NAME
1200 && !is_gimple_min_invariant (expr))
1202 tree var = create_tmp_var (type, "var");
1205 expr = build2 (MODIFY_EXPR, type, var, expr);
1206 expr = force_gimple_operand (expr, &stmts, true, NULL);
1207 gsi_insert_seq_on_edge_immediate (e, stmts);
1213 /* Helper function for htab_traverse in insert_guard_phis. */
1216 add_guard_exit_phis (void **slot, void *s)
1218 struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
1219 struct igp *i = (struct igp *) s;
1220 basic_block bb = i->bb;
1221 edge true_edge = i->true_edge;
1222 edge false_edge = i->false_edge;
1223 tree res = entry->old_name;
1224 tree name1 = entry->expr;
1225 tree name2 = default_before_guard (i->before_guard, res);
1228 /* Nothing to be merged if the name before the guard is the same as
1233 name1 = convert_for_phi_arg (name1, res, true_edge);
1234 name2 = convert_for_phi_arg (name2, res, false_edge);
1236 phi = create_phi_node (res, bb);
1237 res = create_new_def_for (gimple_phi_result (phi), phi,
1238 gimple_phi_result_ptr (phi));
1240 add_phi_arg (phi, name1, true_edge, UNKNOWN_LOCATION);
1241 add_phi_arg (phi, name2, false_edge, UNKNOWN_LOCATION);
1248 /* Iterate over RENAME_MAP and get tuples of the form (OLD, NAME1).
1249 If there is a correspondent tuple (OLD, NAME2) in BEFORE_GUARD,
1250 with NAME1 different than NAME2, then insert in BB the phi node:
1252 | RES = phi (NAME1 (on TRUE_EDGE), NAME2 (on FALSE_EDGE))"
1254 if there is no tuple for OLD in BEFORE_GUARD, insert
1256 | RES = phi (NAME1 (on TRUE_EDGE),
1257 | DEFAULT_DEFINITION of NAME1 (on FALSE_EDGE))".
1259 Finally register in RENAME_MAP the tuple (OLD, RES). */
1262 insert_guard_phis (basic_block bb, edge true_edge, edge false_edge,
1263 htab_t before_guard, htab_t rename_map)
1267 i.true_edge = true_edge;
1268 i.false_edge = false_edge;
1269 i.before_guard = before_guard;
1271 update_ssa (TODO_update_ssa);
1272 htab_traverse (rename_map, add_guard_exit_phis, &i);
1273 update_ssa (TODO_update_ssa);
1276 /* Create a duplicate of the basic block BB. NOTE: This does not
1277 preserve SSA form. */
1280 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb, htab_t map)
1282 gimple_stmt_iterator gsi, gsi_tgt;
1284 gsi_tgt = gsi_start_bb (new_bb);
1285 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1287 def_operand_p def_p;
1288 ssa_op_iter op_iter;
1289 gimple stmt = gsi_stmt (gsi);
1292 if (gimple_code (stmt) == GIMPLE_LABEL)
1295 /* Create a new copy of STMT and duplicate STMT's virtual
1297 copy = gimple_copy (stmt);
1298 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
1299 mark_sym_for_renaming (gimple_vop (cfun));
1301 maybe_duplicate_eh_stmt (copy, stmt);
1302 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
1304 /* Create new names for all the definitions created by COPY and
1305 add replacement mappings for each new name. */
1306 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
1308 tree old_name = DEF_FROM_PTR (def_p);
1309 tree new_name = create_new_def_for (old_name, copy, def_p);
1310 set_rename (map, old_name, new_name);
1315 /* Copies BB and includes in the copied BB all the statements that can
1316 be reached following the use-def chains from the memory accesses,
1317 and returns the next edge following this new block. */
1320 copy_bb_and_scalar_dependences (basic_block bb, sese region,
1321 edge next_e, htab_t map)
1323 basic_block new_bb = split_edge (next_e);
1325 next_e = single_succ_edge (new_bb);
1326 graphite_copy_stmts_from_block (bb, new_bb, map);
1327 remove_condition (new_bb);
1328 remove_phi_nodes (new_bb);
1329 expand_scalar_variables (new_bb, region, map);
1330 rename_variables (new_bb, map);
1335 /* Returns the outermost loop in SCOP that contains BB. */
1338 outermost_loop_in_sese (sese region, basic_block bb)
1342 nest = bb->loop_father;
1343 while (loop_outer (nest)
1344 && loop_in_sese_p (loop_outer (nest), region))
1345 nest = loop_outer (nest);
1350 /* Sets the false region of an IF_REGION to REGION. */
1353 if_region_set_false_region (ifsese if_region, sese region)
1355 basic_block condition = if_region_get_condition_block (if_region);
1356 edge false_edge = get_false_edge_from_guard_bb (condition);
1357 basic_block dummy = false_edge->dest;
1358 edge entry_region = SESE_ENTRY (region);
1359 edge exit_region = SESE_EXIT (region);
1360 basic_block before_region = entry_region->src;
1361 basic_block last_in_region = exit_region->src;
1362 void **slot = htab_find_slot_with_hash (current_loops->exits, exit_region,
1363 htab_hash_pointer (exit_region),
1366 entry_region->flags = false_edge->flags;
1367 false_edge->flags = exit_region->flags;
1369 redirect_edge_pred (entry_region, condition);
1370 redirect_edge_pred (exit_region, before_region);
1371 redirect_edge_pred (false_edge, last_in_region);
1372 redirect_edge_succ (false_edge, single_succ (dummy));
1373 delete_basic_block (dummy);
1375 exit_region->flags = EDGE_FALLTHRU;
1376 recompute_all_dominators ();
1378 SESE_EXIT (region) = false_edge;
1380 if (if_region->false_region)
1381 free (if_region->false_region);
1382 if_region->false_region = region;
1386 struct loop_exit *loop_exit = GGC_CNEW (struct loop_exit);
1388 memcpy (loop_exit, *((struct loop_exit **) slot), sizeof (struct loop_exit));
1389 htab_clear_slot (current_loops->exits, slot);
1391 slot = htab_find_slot_with_hash (current_loops->exits, false_edge,
1392 htab_hash_pointer (false_edge),
1394 loop_exit->e = false_edge;
1396 false_edge->src->loop_father->exits->next = loop_exit;
1400 /* Creates an IFSESE with CONDITION on edge ENTRY. */
1403 create_if_region_on_edge (edge entry, tree condition)
1407 sese sese_region = XNEW (struct sese_s);
1408 sese true_region = XNEW (struct sese_s);
1409 sese false_region = XNEW (struct sese_s);
1410 ifsese if_region = XNEW (struct ifsese_s);
1411 edge exit = create_empty_if_region_on_edge (entry, condition);
1413 if_region->region = sese_region;
1414 if_region->region->entry = entry;
1415 if_region->region->exit = exit;
1417 FOR_EACH_EDGE (e, ei, entry->dest->succs)
1419 if (e->flags & EDGE_TRUE_VALUE)
1421 true_region->entry = e;
1422 true_region->exit = single_succ_edge (e->dest);
1423 if_region->true_region = true_region;
1425 else if (e->flags & EDGE_FALSE_VALUE)
1427 false_region->entry = e;
1428 false_region->exit = single_succ_edge (e->dest);
1429 if_region->false_region = false_region;
1436 /* Moves REGION in a condition expression:
1444 move_sese_in_condition (sese region)
1446 basic_block pred_block = split_edge (SESE_ENTRY (region));
1449 SESE_ENTRY (region) = single_succ_edge (pred_block);
1450 if_region = create_if_region_on_edge (single_pred_edge (pred_block), integer_one_node);
1451 if_region_set_false_region (if_region, region);
1456 /* Returns the scalar evolution of T in REGION. Every variable that
1457 is not defined in the REGION is considered a parameter. */
1460 scalar_evolution_in_region (sese region, loop_p loop, tree t)
1463 struct loop *def_loop;
1464 basic_block before = block_before_sese (region);
1466 if (TREE_CODE (t) != SSA_NAME
1467 || loop_in_sese_p (loop, region))
1468 return instantiate_scev (before, loop,
1469 analyze_scalar_evolution (loop, t));
1471 if (!defined_in_sese_p (t, region))
1474 def = SSA_NAME_DEF_STMT (t);
1475 def_loop = loop_containing_stmt (def);
1477 if (loop_in_sese_p (def_loop, region))
1479 t = analyze_scalar_evolution (def_loop, t);
1480 def_loop = superloop_at_depth (def_loop, loop_depth (loop) + 1);
1481 t = compute_overall_effect_of_inner_loop (def_loop, t);
1485 return instantiate_scev (before, loop, t);