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"
30 #include "tree-pass.h"
32 #include "tree-pretty-print.h"
33 #include "fold-const.h"
34 #include "gimple-fold.h"
37 #include "gimple-iterator.h"
38 #include "gimple-pretty-print.h"
39 #include "gimplify-me.h"
41 #include "tree-ssa-loop.h"
42 #include "tree-into-ssa.h"
44 #include "tree-data-ref.h"
45 #include "tree-scalar-evolution.h"
46 #include "value-prof.h"
48 #include "tree-ssa-propagate.h"
50 /* Record LOOP as occurring in REGION. */
53 sese_record_loop (sese_info_p region
, loop_p loop
)
55 if (sese_contains_loop (region
, loop
))
58 bitmap_set_bit (region
->loops
, loop
->num
);
59 region
->loop_nest
.safe_push (loop
);
62 /* Build the loop nests contained in REGION. Returns true when the
63 operation was successful. */
66 build_sese_loop_nests (sese_info_p region
)
70 struct loop
*loop0
, *loop1
;
72 FOR_EACH_BB_FN (bb
, cfun
)
73 if (bb_in_sese_p (bb
, region
->region
))
75 struct loop
*loop
= bb
->loop_father
;
77 /* Only add loops if they are completely contained in the SCoP. */
78 if (loop
->header
== bb
79 && bb_in_sese_p (loop
->latch
, region
->region
))
80 sese_record_loop (region
, loop
);
83 /* Make sure that the loops in the SESE_LOOP_NEST are ordered. It
84 can be the case that an inner loop is inserted before an outer
85 loop. To avoid this, semi-sort once. */
86 FOR_EACH_VEC_ELT (region
->loop_nest
, i
, loop0
)
88 if (region
->loop_nest
.length () == i
+ 1)
91 loop1
= region
->loop_nest
[i
+ 1];
92 if (loop0
->num
> loop1
->num
)
94 region
->loop_nest
[i
] = loop1
;
95 region
->loop_nest
[i
+ 1] = loop0
;
100 /* For a USE in BB, if BB is outside REGION, mark the USE in the
104 sese_build_liveouts_use (sese_info_p region
, bitmap liveouts
, basic_block bb
,
107 gcc_assert (!bb_in_sese_p (bb
, region
->region
));
108 if (TREE_CODE (use
) != SSA_NAME
)
111 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (use
));
113 if (!def_bb
|| !bb_in_sese_p (def_bb
, region
->region
))
116 unsigned ver
= SSA_NAME_VERSION (use
);
117 bitmap_set_bit (liveouts
, ver
);
120 /* Marks for rewrite all the SSA_NAMES defined in REGION and that are
121 used in BB that is outside of the REGION. */
124 sese_build_liveouts_bb (sese_info_p region
, bitmap liveouts
, basic_block bb
)
131 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
132 for (gphi_iterator bsi
= gsi_start_phis (e
->dest
); !gsi_end_p (bsi
);
134 sese_build_liveouts_use (region
, liveouts
, bb
,
135 PHI_ARG_DEF_FROM_EDGE (bsi
.phi (), e
));
137 for (gimple_stmt_iterator bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
);
140 gimple
*stmt
= gsi_stmt (bsi
);
142 if (is_gimple_debug (stmt
))
145 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
146 sese_build_liveouts_use (region
, liveouts
, bb
, USE_FROM_PTR (use_p
));
150 /* For a USE in BB, return true if BB is outside REGION and it's not
151 in the LIVEOUTS set. */
154 sese_bad_liveouts_use (sese_info_p region
, bitmap liveouts
, basic_block bb
,
157 gcc_assert (!bb_in_sese_p (bb
, region
->region
));
159 if (TREE_CODE (use
) != SSA_NAME
)
162 unsigned ver
= SSA_NAME_VERSION (use
);
164 /* If it's in liveouts, the variable will get a new PHI node, and
165 the debug use will be properly adjusted. */
166 if (bitmap_bit_p (liveouts
, ver
))
169 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (use
));
171 if (!def_bb
|| !bb_in_sese_p (def_bb
, region
->region
))
177 /* Reset debug stmts that reference SSA_NAMES defined in REGION that
178 are not marked as liveouts. */
181 sese_reset_debug_liveouts_bb (sese_info_p region
, bitmap liveouts
, basic_block bb
)
183 gimple_stmt_iterator bsi
;
187 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
189 gimple
*stmt
= gsi_stmt (bsi
);
191 if (!is_gimple_debug (stmt
))
194 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
195 if (sese_bad_liveouts_use (region
, liveouts
, bb
,
196 USE_FROM_PTR (use_p
)))
198 gimple_debug_bind_reset_value (stmt
);
205 /* Build the LIVEOUTS of REGION: the set of variables defined inside
206 and used outside the REGION. */
209 sese_build_liveouts (sese_info_p region
, bitmap liveouts
)
213 /* FIXME: We could start iterating form the successor of sese. */
214 FOR_EACH_BB_FN (bb
, cfun
)
215 if (!bb_in_sese_p (bb
, region
->region
))
216 sese_build_liveouts_bb (region
, liveouts
, bb
);
218 /* FIXME: We could start iterating form the successor of sese. */
219 if (MAY_HAVE_DEBUG_STMTS
)
220 FOR_EACH_BB_FN (bb
, cfun
)
221 if (!bb_in_sese_p (bb
, region
->region
))
222 sese_reset_debug_liveouts_bb (region
, liveouts
, bb
);
225 /* Builds a new SESE region from edges ENTRY and EXIT. */
228 new_sese_info (edge entry
, edge exit
)
230 sese_info_p region
= XNEW (struct sese_info_t
);
232 region
->region
.entry
= entry
;
233 region
->region
.exit
= exit
;
234 region
->loops
= BITMAP_ALLOC (NULL
);
235 region
->loop_nest
.create (3);
236 region
->params
.create (3);
237 region
->rename_map
= new rename_map_t
;
238 region
->copied_bb_map
= new bb_map_t
;
239 region
->bbs
.create (3);
240 region
->incomplete_phis
.create (3);
245 /* Deletes REGION. */
248 free_sese_info (sese_info_p region
)
251 region
->loops
= BITMAP_ALLOC (NULL
);
253 region
->params
.release ();
254 region
->loop_nest
.release ();
256 for (rename_map_t::iterator it
= region
->rename_map
->begin ();
257 it
!= region
->rename_map
->begin (); ++it
)
258 (*it
).second
.release ();
260 for (bb_map_t::iterator it
= region
->copied_bb_map
->begin ();
261 it
!= region
->copied_bb_map
->begin (); ++it
)
262 (*it
).second
.release ();
264 delete region
->rename_map
;
265 delete region
->copied_bb_map
;
267 region
->rename_map
= NULL
;
268 region
->copied_bb_map
= NULL
;
270 region
->bbs
.release ();
271 region
->incomplete_phis
.release ();
276 /* Add exit phis for USE on EXIT. */
279 sese_add_exit_phis_edge (basic_block exit
, tree use
, edge false_e
, edge true_e
)
281 gphi
*phi
= create_phi_node (NULL_TREE
, exit
);
282 create_new_def_for (use
, phi
, gimple_phi_result_ptr (phi
));
283 add_phi_arg (phi
, use
, false_e
, UNKNOWN_LOCATION
);
284 add_phi_arg (phi
, use
, true_e
, UNKNOWN_LOCATION
);
288 /* Insert in the block BB phi nodes for variables defined in REGION
289 and used outside the REGION. The code generation moves REGION in
290 the else clause of an "if (1)" and generates code in the then
291 clause that is at this point empty:
300 sese_insert_phis_for_liveouts (sese_info_p region
, basic_block bb
,
301 edge false_e
, edge true_e
)
305 bitmap liveouts
= BITMAP_ALLOC (NULL
);
307 update_ssa (TODO_update_ssa
);
309 sese_build_liveouts (region
, liveouts
);
311 EXECUTE_IF_SET_IN_BITMAP (liveouts
, 0, i
, bi
)
312 if (!virtual_operand_p (ssa_name (i
)))
313 sese_add_exit_phis_edge (bb
, ssa_name (i
), false_e
, true_e
);
315 BITMAP_FREE (liveouts
);
317 update_ssa (TODO_update_ssa
);
320 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set. */
323 get_true_edge_from_guard_bb (basic_block bb
)
328 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
329 if (e
->flags
& EDGE_TRUE_VALUE
)
336 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared. */
339 get_false_edge_from_guard_bb (basic_block bb
)
344 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
345 if (!(e
->flags
& EDGE_TRUE_VALUE
))
352 /* Check if USE is defined in a basic block from where the definition of USE can
353 propagate from all the paths. */
356 is_loop_closed_ssa_use (basic_block bb
, tree use
)
358 if (TREE_CODE (use
) != SSA_NAME
)
361 /* We should not have a rename for virtual operands. */
362 gcc_assert (!virtual_operand_p (use
));
364 /* For close-phi nodes def always comes from a loop which has a back-edge. */
365 if (bb_contains_loop_close_phi_nodes (bb
))
368 gimple
*def
= SSA_NAME_DEF_STMT (use
);
369 basic_block def_bb
= gimple_bb (def
);
371 || flow_bb_inside_loop_p (def_bb
->loop_father
, bb
));
374 /* Return the number of phi nodes in BB. */
377 number_of_phi_nodes (basic_block bb
)
380 for (gphi_iterator psi
= gsi_start_phis (bb
); !gsi_end_p (psi
);
386 /* Return true when BB contains loop close phi nodes. */
389 bb_contains_loop_close_phi_nodes (basic_block bb
)
391 return single_pred_p (bb
)
392 && bb
->loop_father
!= single_pred_edge (bb
)->src
->loop_father
;
395 /* Return true when BB contains loop phi nodes. */
398 bb_contains_loop_phi_nodes (basic_block bb
)
400 gcc_assert (EDGE_COUNT (bb
->preds
) <= 2);
402 if (bb
->preds
->length () == 1)
405 unsigned depth
= loop_depth (bb
->loop_father
);
407 edge preds
[2] = { (*bb
->preds
)[0], (*bb
->preds
)[1] };
409 if (depth
> loop_depth (preds
[0]->src
->loop_father
)
410 || depth
> loop_depth (preds
[1]->src
->loop_father
))
413 /* When one of the edges correspond to the same loop father and other
415 if (bb
->loop_father
!= preds
[0]->src
->loop_father
416 && bb
->loop_father
== preds
[1]->src
->loop_father
)
419 if (bb
->loop_father
!= preds
[1]->src
->loop_father
420 && bb
->loop_father
== preds
[0]->src
->loop_father
)
426 /* Returns true if BB uses name in one of its PHIs. */
429 phi_uses_name (basic_block bb
, tree name
)
431 for (gphi_iterator psi
= gsi_start_phis (bb
); !gsi_end_p (psi
);
434 gphi
*phi
= psi
.phi ();
435 for (unsigned i
= 0; i
< gimple_phi_num_args (phi
); i
++)
437 tree use_arg
= gimple_phi_arg_def (phi
, i
);
445 /* Return true if RENAME (defined in BB) is a valid use in NEW_BB. The
446 definition should flow into use, and the use should respect the loop-closed SSA
450 is_valid_rename (tree rename
, basic_block def_bb
,
451 basic_block use_bb
, bool loop_phi
,
452 tree old_name
, basic_block old_bb
)
454 /* The def of the rename must either dominate the uses or come from a
455 back-edge. Also the def must respect the loop closed ssa form. */
456 if (!is_loop_closed_ssa_use (use_bb
, rename
))
460 fprintf (dump_file
, "\n[codegen] rename not in loop closed ssa:");
461 print_generic_expr (dump_file
, rename
, 0);
466 if (dominated_by_p (CDI_DOMINATORS
, use_bb
, def_bb
))
469 if (bb_contains_loop_phi_nodes (use_bb
) && loop_phi
)
471 /* The loop-header dominates the loop-body. */
472 if (!dominated_by_p (CDI_DOMINATORS
, def_bb
, use_bb
))
475 /* RENAME would be used in loop-phi. */
476 gcc_assert (number_of_phi_nodes (use_bb
));
478 /* For definitions coming from back edges, we should check that
479 old_name is used in a loop PHI node. */
480 if (phi_uses_name (old_bb
, old_name
))
486 /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in
487 NEW_BB from RENAME_MAP. LOOP_PHI is true when we want to rename OLD_NAME
488 within a loop PHI instruction. */
491 get_rename (rename_map_t
*rename_map
, basic_block new_bb
, tree old_name
,
492 basic_block old_bb
, bool loop_phi
)
494 gcc_assert (TREE_CODE (old_name
) == SSA_NAME
);
495 vec
<tree
> *renames
= rename_map
->get (old_name
);
497 if (!renames
|| renames
->is_empty ())
500 if (1 == renames
->length ())
502 tree rename
= (*renames
)[0];
503 basic_block bb
= gimple_bb (SSA_NAME_DEF_STMT (rename
));
504 if (is_valid_rename (rename
, bb
, new_bb
, loop_phi
, old_name
, old_bb
))
509 /* More than one renames corresponding to the old_name. Find the rename for
510 which the definition flows into usage at new_bb. */
512 tree t1
= NULL_TREE
, t2
;
513 basic_block t1_bb
= NULL
;
514 FOR_EACH_VEC_ELT (*renames
, i
, t2
)
516 basic_block t2_bb
= gimple_bb (SSA_NAME_DEF_STMT (t2
));
518 /* Defined in the same basic block as used. */
522 /* NEW_BB and T2_BB are in two unrelated if-clauses. */
523 if (!dominated_by_p (CDI_DOMINATORS
, new_bb
, t2_bb
))
526 /* Compute the nearest dominator. */
527 if (!t1
|| dominated_by_p (CDI_DOMINATORS
, t2_bb
, t1_bb
))
532 //if (is_valid_rename (rename, bb, new_bb, loop_phi, old_name, old_bb))
539 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
540 When OLD_NAME and EXPR are the same we assert. */
543 set_rename (tree old_name
, tree expr
, sese_info_p region
)
547 fprintf (dump_file
, "\n[codegen] setting rename: old_name = ");
548 print_generic_expr (dump_file
, old_name
, 0);
549 fprintf (dump_file
, ", new_name = ");
550 print_generic_expr (dump_file
, expr
, 0);
553 if (old_name
== expr
)
556 vec
<tree
> *renames
= region
->rename_map
->get (old_name
);
559 renames
->safe_push (expr
);
565 region
->rename_map
->put (old_name
, r
);
569 /* Return an iterator to the instructions comes
570 last in the execution order. Either GSI1 and GSI2 should belong
571 to the same basic block or one of their respective basic blocks
572 should dominate the other. */
575 later_of_the_two (gimple_stmt_iterator gsi1
, gimple_stmt_iterator gsi2
)
577 basic_block bb1
= gsi_bb (gsi1
);
578 basic_block bb2
= gsi_bb (gsi2
);
580 /* Find the iterator which is the latest. */
583 /* For empty basic blocks gsis point to the end of the sequence. Since
584 there is no operator== defined for gimple_stmt_iterator and for gsis
585 not pointing to a valid statement gsi_next would assert. */
586 gimple_stmt_iterator gsi
= gsi1
;
588 if (gsi_stmt (gsi
) == gsi_stmt (gsi2
))
591 } while (!gsi_end_p (gsi
));
596 /* Find the basic block closest to the basic block which defines stmt. */
597 if (dominated_by_p (CDI_DOMINATORS
, bb1
, bb2
))
600 gcc_assert (dominated_by_p (CDI_DOMINATORS
, bb2
, bb1
));
604 /* Insert each statement from SEQ at its earliest insertion p. */
607 gsi_insert_earliest (gimple_seq seq
, sese_info_p region
)
609 update_modified_stmts (seq
);
610 sese_l
&codegen_region
= region
->if_region
->true_region
->region
;
611 basic_block begin_bb
= get_entry_bb (codegen_region
);
613 /* Inserting the gimple statements in a vector because gimple_seq behave
614 in strage ways when inserting the stmts from it into different basic
615 blocks one at a time. */
616 auto_vec
<gimple
*, 3> stmts
;
617 for (gimple_stmt_iterator gsi
= gsi_start (seq
); !gsi_end_p (gsi
);
619 stmts
.safe_push (gsi_stmt (gsi
));
623 FOR_EACH_VEC_ELT (stmts
, i
, use_stmt
)
625 gcc_assert (gimple_code (use_stmt
) != GIMPLE_PHI
);
626 gimple_stmt_iterator gsi_def_stmt
= gsi_start_bb_nondebug (begin_bb
);
630 FOR_EACH_SSA_USE_OPERAND (use_p
, use_stmt
, op_iter
, SSA_OP_USE
)
632 /* Iterator to the current def of use_p. For function parameters or
633 anything where def is not found, insert at the beginning of the
635 gimple_stmt_iterator gsi_stmt
= gsi_def_stmt
;
637 tree op
= USE_FROM_PTR (use_p
);
638 gimple
*stmt
= SSA_NAME_DEF_STMT (op
);
639 if (stmt
&& (gimple_code (stmt
) != GIMPLE_NOP
))
640 gsi_stmt
= gsi_for_stmt (stmt
);
642 /* For region parameters, insert at the beginning of the generated
644 if (!bb_in_sese_p (gsi_bb (gsi_stmt
), codegen_region
))
646 /* The parameter should have been inserted in the parameter
647 map or it must have a scev. */
648 gsi_stmt
= gsi_def_stmt
;
651 gsi_def_stmt
= later_of_the_two (gsi_stmt
, gsi_def_stmt
);
654 if (!gsi_stmt (gsi_def_stmt
))
656 gimple_stmt_iterator gsi
= gsi_after_labels (gsi_bb (gsi_def_stmt
));
657 gsi_insert_before (&gsi
, use_stmt
, GSI_NEW_STMT
);
659 else if (gimple_code (gsi_stmt (gsi_def_stmt
)) == GIMPLE_PHI
)
661 gimple_stmt_iterator bsi
662 = gsi_start_bb_nondebug (gsi_bb (gsi_def_stmt
));
663 /* Insert right after the PHI statements. */
664 gsi_insert_before (&bsi
, use_stmt
, GSI_NEW_STMT
);
667 gsi_insert_after (&gsi_def_stmt
, use_stmt
, GSI_NEW_STMT
);
671 fprintf (dump_file
, "\n[codegen] inserting statement: ");
672 print_gimple_stmt (dump_file
, use_stmt
, 0, TDF_VOPS
| TDF_MEMSYMS
);
673 print_loops_bb (dump_file
, gimple_bb (use_stmt
), 0, 3);
678 /* Collect all the operands of NEW_EXPR by recursively visiting each
682 collect_all_ssa_names (tree new_expr
, vec
<tree
> *vec_ssa
, sese_info_p region
)
685 /* Rename all uses in new_expr. */
686 if (TREE_CODE (new_expr
) == SSA_NAME
)
688 vec_ssa
->safe_push (new_expr
);
692 /* Iterate over SSA_NAMES in NEW_EXPR. */
693 for (int i
= 0; i
< (TREE_CODE_LENGTH (TREE_CODE (new_expr
))); i
++)
695 tree op
= TREE_OPERAND (new_expr
, i
);
696 collect_all_ssa_names (op
, vec_ssa
, region
);
701 substitute_ssa_name (tree exp
, tree f
, tree r
)
703 enum tree_code code
= TREE_CODE (exp
);
704 tree op0
, op1
, op2
, op3
;
707 /* We handle TREE_LIST and COMPONENT_REF separately. */
708 if (code
== TREE_LIST
)
710 op0
= substitute_ssa_name (TREE_CHAIN (exp
), f
, r
);
711 op1
= substitute_ssa_name (TREE_VALUE (exp
), f
, r
);
712 if (op0
== TREE_CHAIN (exp
) && op1
== TREE_VALUE (exp
))
715 return tree_cons (TREE_PURPOSE (exp
), op1
, op0
);
717 else if (code
== COMPONENT_REF
)
721 /* If this expression is getting a value from a PLACEHOLDER_EXPR
722 and it is the right field, replace it with R. */
723 for (inner
= TREE_OPERAND (exp
, 0);
724 REFERENCE_CLASS_P (inner
);
725 inner
= TREE_OPERAND (inner
, 0))
729 op1
= TREE_OPERAND (exp
, 1);
731 if (TREE_CODE (inner
) == PLACEHOLDER_EXPR
&& op1
== f
)
734 /* If this expression hasn't been completed let, leave it alone. */
735 if (TREE_CODE (inner
) == PLACEHOLDER_EXPR
&& !TREE_TYPE (inner
))
738 op0
= substitute_ssa_name (TREE_OPERAND (exp
, 0), f
, r
);
739 if (op0
== TREE_OPERAND (exp
, 0))
743 = fold_build3 (COMPONENT_REF
, TREE_TYPE (exp
), op0
, op1
, NULL_TREE
);
746 switch (TREE_CODE_CLASS (code
))
751 case tcc_declaration
:
761 /* Fall through... */
763 case tcc_exceptional
:
768 switch (TREE_CODE_LENGTH (code
))
776 op0
= substitute_ssa_name (TREE_OPERAND (exp
, 0), f
, r
);
777 if (op0
== TREE_OPERAND (exp
, 0))
780 new_tree
= fold_build1 (code
, TREE_TYPE (exp
), op0
);
784 op0
= substitute_ssa_name (TREE_OPERAND (exp
, 0), f
, r
);
785 op1
= substitute_ssa_name (TREE_OPERAND (exp
, 1), f
, r
);
787 if (op0
== TREE_OPERAND (exp
, 0) && op1
== TREE_OPERAND (exp
, 1))
790 new_tree
= fold_build2 (code
, TREE_TYPE (exp
), op0
, op1
);
794 op0
= substitute_ssa_name (TREE_OPERAND (exp
, 0), f
, r
);
795 op1
= substitute_ssa_name (TREE_OPERAND (exp
, 1), f
, r
);
796 op2
= substitute_ssa_name (TREE_OPERAND (exp
, 2), f
, r
);
798 if (op0
== TREE_OPERAND (exp
, 0) && op1
== TREE_OPERAND (exp
, 1)
799 && op2
== TREE_OPERAND (exp
, 2))
802 new_tree
= fold_build3 (code
, TREE_TYPE (exp
), op0
, op1
, op2
);
806 op0
= substitute_ssa_name (TREE_OPERAND (exp
, 0), f
, r
);
807 op1
= substitute_ssa_name (TREE_OPERAND (exp
, 1), f
, r
);
808 op2
= substitute_ssa_name (TREE_OPERAND (exp
, 2), f
, r
);
809 op3
= substitute_ssa_name (TREE_OPERAND (exp
, 3), f
, r
);
811 if (op0
== TREE_OPERAND (exp
, 0) && op1
== TREE_OPERAND (exp
, 1)
812 && op2
== TREE_OPERAND (exp
, 2)
813 && op3
== TREE_OPERAND (exp
, 3))
817 = fold (build4 (code
, TREE_TYPE (exp
), op0
, op1
, op2
, op3
));
830 TREE_READONLY (new_tree
) |= TREE_READONLY (exp
);
832 if (code
== INDIRECT_REF
|| code
== ARRAY_REF
|| code
== ARRAY_RANGE_REF
)
833 TREE_THIS_NOTRAP (new_tree
) |= TREE_THIS_NOTRAP (exp
);
838 /* Rename all the operands of NEW_EXPR by recursively visiting each operand. */
841 rename_all_uses (tree new_expr
, basic_block new_bb
, basic_block old_bb
,
845 ssa_names
.create (2);
846 collect_all_ssa_names (new_expr
, &ssa_names
, region
);
849 FOR_EACH_VEC_ELT (ssa_names
, i
, t
)
851 if (tree r
= get_rename (region
->rename_map
, new_bb
, t
, old_bb
, false))
852 new_expr
= substitute_ssa_name (new_expr
, t
, r
);
861 get_rename_from_scev (tree old_name
, gimple_seq
*stmts
, loop_p loop
,
862 basic_block new_bb
, basic_block old_bb
,
863 vec
<tree
> iv_map
, sese_info_p region
, bool *gloog_error
)
865 tree scev
= scalar_evolution_in_region (region
->region
, loop
, old_name
);
867 /* At this point we should know the exact scev for each
868 scalar SSA_NAME used in the scop: all the other scalar
869 SSA_NAMEs should have been translated out of SSA using
870 arrays with one element. */
872 if (chrec_contains_undetermined (scev
))
875 return build_zero_cst (TREE_TYPE (old_name
));
878 new_expr
= chrec_apply_map (scev
, iv_map
);
880 /* The apply should produce an expression tree containing
881 the uses of the new induction variables. We should be
882 able to use new_expr instead of the old_name in the newly
883 generated loop nest. */
884 if (chrec_contains_undetermined (new_expr
)
885 || tree_contains_chrecs (new_expr
, NULL
))
888 return build_zero_cst (TREE_TYPE (old_name
));
891 new_expr
= rename_all_uses (new_expr
, new_bb
, old_bb
, region
);
893 /* Replace the old_name with the new_expr. */
894 return force_gimple_operand (unshare_expr (new_expr
), stmts
,
898 /* Renames the scalar uses of the statement COPY, using the
899 substitution map RENAME_MAP, inserting the gimplification code at
900 GSI_TGT, for the translation REGION, with the original copied
901 statement in LOOP, and using the induction variable renaming map
902 IV_MAP. Returns true when something has been renamed. GLOOG_ERROR
903 is set when the code generation cannot continue. */
906 rename_uses (gimple
*copy
, gimple_stmt_iterator
*gsi_tgt
,
907 basic_block old_bb
, sese_info_p region
,
908 loop_p loop
, vec
<tree
> iv_map
, bool *gloog_error
)
910 bool changed
= false;
912 if (is_gimple_debug (copy
))
914 if (gimple_debug_bind_p (copy
))
915 gimple_debug_bind_reset_value (copy
);
916 else if (gimple_debug_source_bind_p (copy
))
926 fprintf (dump_file
, "\n[codegen] renaming uses of stmt: ");
927 print_gimple_stmt (dump_file
, copy
, 0, 0);
932 FOR_EACH_SSA_USE_OPERAND (use_p
, copy
, op_iter
, SSA_OP_USE
)
934 tree old_name
= USE_FROM_PTR (use_p
);
938 fprintf (dump_file
, "\n[codegen] renaming old_name = ");
939 print_generic_expr (dump_file
, old_name
, 0);
942 if (TREE_CODE (old_name
) != SSA_NAME
943 || SSA_NAME_IS_DEFAULT_DEF (old_name
))
947 tree new_expr
= get_rename (region
->rename_map
, gsi_tgt
->bb
, old_name
,
952 tree type_old_name
= TREE_TYPE (old_name
);
953 tree type_new_expr
= TREE_TYPE (new_expr
);
957 fprintf (dump_file
, "\n[codegen] from rename_map: new_name = ");
958 print_generic_expr (dump_file
, new_expr
, 0);
961 if (type_old_name
!= type_new_expr
962 || TREE_CODE (new_expr
) != SSA_NAME
)
964 tree var
= create_tmp_var (type_old_name
, "var");
966 if (!useless_type_conversion_p (type_old_name
, type_new_expr
))
967 new_expr
= fold_convert (type_old_name
, new_expr
);
970 new_expr
= force_gimple_operand (new_expr
, &stmts
, true, var
);
971 gsi_insert_earliest (stmts
, region
);
974 replace_exp (use_p
, new_expr
);
979 new_expr
= get_rename_from_scev (old_name
, &stmts
, loop
, gimple_bb (copy
),
980 old_bb
, iv_map
, region
, gloog_error
);
981 if (!new_expr
|| *gloog_error
)
986 fprintf (dump_file
, "\n[codegen] not in rename map, scev: ");
987 print_generic_expr (dump_file
, new_expr
, 0);
990 gsi_insert_earliest (stmts
, region
);
991 replace_exp (use_p
, new_expr
);
993 if (TREE_CODE (new_expr
) == INTEGER_CST
994 && is_gimple_assign (copy
))
996 tree rhs
= gimple_assign_rhs1 (copy
);
998 if (TREE_CODE (rhs
) == ADDR_EXPR
)
999 recompute_tree_invariant_for_addr_expr (rhs
);
1002 set_rename (old_name
, new_expr
, region
);
1008 /* Returns a basic block that could correspond to where a constant was defined
1009 in the original code. In the original code OLD_BB had the definition, we
1010 need to find which basic block out of the copies of old_bb, in the new
1011 region, should a definition correspond to if it has to reach BB. */
1014 get_def_bb_for_const (sese_info_p region
, basic_block bb
, basic_block old_bb
)
1016 vec
<basic_block
> *bbs
= region
->copied_bb_map
->get (old_bb
);
1018 if (!bbs
|| bbs
->is_empty ())
1021 if (1 == bbs
->length ())
1025 basic_block b1
= NULL
, b2
;
1026 FOR_EACH_VEC_ELT (*bbs
, i
, b2
)
1031 /* BB and B2 are in two unrelated if-clauses. */
1032 if (!dominated_by_p (CDI_DOMINATORS
, bb
, b2
))
1035 /* Compute the nearest dominator. */
1036 if (!b1
|| dominated_by_p (CDI_DOMINATORS
, b2
, b1
))
1044 /* LOOP_PHI is true when we want to rename an OP within a loop PHI
1048 get_new_name (sese_info_p region
, basic_block new_bb
, tree op
,
1049 basic_block old_bb
, bool loop_phi
)
1051 if (TREE_CODE (op
) == INTEGER_CST
1052 || TREE_CODE (op
) == REAL_CST
1053 || TREE_CODE (op
) == COMPLEX_CST
1054 || TREE_CODE (op
) == VECTOR_CST
)
1057 return get_rename (region
->rename_map
, new_bb
, op
, old_bb
, loop_phi
);
1060 /* Return a debug location for OP. */
1065 location_t loc
= UNKNOWN_LOCATION
;
1067 if (TREE_CODE (op
) == SSA_NAME
)
1068 loc
= gimple_location (SSA_NAME_DEF_STMT (op
));
1072 /* Returns the incoming edges of basic_block BB in the pair. The first edge is
1073 the init edge (from outside the loop) and the second one is the back edge
1074 from the same loop. */
1076 std::pair
<edge
, edge
>
1077 get_edges (basic_block bb
)
1079 std::pair
<edge
, edge
> edges
;
1082 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1083 if (bb
->loop_father
!= e
->src
->loop_father
)
1090 /* Copy the PHI arguments from OLD_PHI to the NEW_PHI. The arguments to NEW_PHI
1091 must be found unless they can be POSTPONEd for later. */
1094 copy_loop_phi_args (gphi
*old_phi
, init_back_edge_pair_t
&ibp_old_bb
,
1095 gphi
*new_phi
, init_back_edge_pair_t
&ibp_new_bb
,
1096 sese_info_p region
, bool postpone
)
1098 gcc_assert (gimple_phi_num_args (old_phi
) == gimple_phi_num_args (new_phi
));
1100 basic_block new_bb
= gimple_bb (new_phi
);
1101 for (unsigned i
= 0; i
< gimple_phi_num_args (old_phi
); i
++)
1104 if (gimple_phi_arg_edge (old_phi
, i
) == ibp_old_bb
.first
)
1105 e
= ibp_new_bb
.first
;
1107 e
= ibp_new_bb
.second
;
1109 tree old_name
= gimple_phi_arg_def (old_phi
, i
);
1110 tree new_name
= get_new_name (region
, new_bb
, old_name
,
1111 gimple_bb (old_phi
), true);
1114 add_phi_arg (new_phi
, new_name
, e
, get_loc (old_name
));
1118 gimple
*old_def_stmt
= SSA_NAME_DEF_STMT (old_name
);
1119 if (!old_def_stmt
|| gimple_code (old_def_stmt
) == GIMPLE_NOP
)
1120 /* If the phi arg was a function arg, or wasn't defined, just use the old
1122 add_phi_arg (new_phi
, old_name
, e
, get_loc (old_name
));
1125 /* Postpone code gen for later for those back-edges we don't have the
1127 region
->incomplete_phis
.safe_push (std::make_pair (old_phi
, new_phi
));
1129 fprintf (dump_file
, "\n[codegen] postpone loop phi nodes: ");
1132 /* Either we should add the arg to phi or, we should postpone. */
1137 /* Copy loop phi nodes from BB to NEW_BB. */
1140 copy_loop_phi_nodes (basic_block bb
, basic_block new_bb
, sese_info_p region
)
1143 fprintf (dump_file
, "\n[codegen] copying loop phi nodes in bb_%d.",
1146 /* Loop phi nodes should have only two arguments. */
1147 gcc_assert (2 == EDGE_COUNT (bb
->preds
));
1149 /* First edge is the init edge and second is the back edge. */
1150 init_back_edge_pair_t ibp_old_bb
= get_edges (bb
);
1152 /* First edge is the init edge and second is the back edge. */
1153 init_back_edge_pair_t ibp_new_bb
= get_edges (new_bb
);
1155 for (gphi_iterator psi
= gsi_start_phis (bb
); !gsi_end_p (psi
);
1158 gphi
*phi
= psi
.phi ();
1159 tree res
= gimple_phi_result (phi
);
1160 if (virtual_operand_p (res
))
1162 if (is_gimple_reg (res
) && scev_analyzable_p (res
, region
->region
))
1165 gphi
*new_phi
= create_phi_node (SSA_NAME_VAR (res
), new_bb
);
1166 tree new_res
= create_new_def_for (res
, new_phi
,
1167 gimple_phi_result_ptr (new_phi
));
1168 set_rename (res
, new_res
, region
);
1169 copy_loop_phi_args (phi
, ibp_old_bb
, new_phi
, ibp_new_bb
, region
, true);
1170 update_stmt (new_phi
);
1176 /* Return the init value of PHI, the value coming from outside the loop. */
1179 get_loop_init_value (gphi
*phi
)
1182 loop_p loop
= gimple_bb (phi
)->loop_father
;
1186 FOR_EACH_EDGE (e
, ei
, gimple_bb (phi
)->preds
)
1187 if (e
->src
->loop_father
!= loop
)
1188 return gimple_phi_arg_def (phi
, e
->dest_idx
);
1193 /* Find the init value (the value which comes from outside the loop), of one of
1194 the operands of DEF which is defined by a loop phi. */
1197 find_init_value (gimple
*def
)
1199 if (gimple_code (def
) == GIMPLE_PHI
)
1200 return get_loop_init_value (as_a
<gphi
*> (def
));
1202 if (gimple_vuse (def
))
1206 use_operand_p use_p
;
1207 FOR_EACH_SSA_USE_OPERAND (use_p
, def
, iter
, SSA_OP_USE
)
1209 tree use
= USE_FROM_PTR (use_p
);
1210 if (TREE_CODE (use
) == SSA_NAME
)
1212 if (tree res
= find_init_value (SSA_NAME_DEF_STMT (use
)))
1220 /* Return the init value, the value coming from outside the loop. */
1223 find_init_value_close_phi (gphi
*phi
)
1225 gcc_assert (gimple_phi_num_args (phi
) == 1);
1226 tree use_arg
= gimple_phi_arg_def (phi
, 0);
1227 gimple
*def
= SSA_NAME_DEF_STMT (use_arg
);
1228 return find_init_value (def
);
1231 /* Copy all the loop-close phi args from BB to NEW_BB. */
1234 copy_loop_close_phi_args (basic_block old_bb
, basic_block new_bb
,
1235 sese_info_p region
, bool postpone
)
1237 /* The successor of bb having close phi should be a merge of the diamond
1238 inserted to guard the loop during codegen. */
1239 basic_block close_phi_merge_bb
= single_succ (new_bb
);
1241 for (gphi_iterator psi
= gsi_start_phis (old_bb
); !gsi_end_p (psi
);
1244 gphi
*phi
= psi
.phi ();
1245 tree res
= gimple_phi_result (phi
);
1246 if (virtual_operand_p (res
))
1249 if (is_gimple_reg (res
) && scev_analyzable_p (res
, region
->region
))
1250 /* Loop close phi nodes should not be scev_analyzable_p. */
1253 gphi
*new_phi
= create_phi_node (SSA_NAME_VAR (res
), new_bb
);
1254 tree new_res
= create_new_def_for (res
, new_phi
,
1255 gimple_phi_result_ptr (new_phi
));
1256 set_rename (res
, new_res
, region
);
1258 tree old_name
= gimple_phi_arg_def (phi
, 0);
1259 tree new_name
= get_new_name (region
, new_bb
, old_name
, old_bb
, false);
1261 /* Predecessor basic blocks of a loop close phi should have been code
1262 generated before. FIXME: This is fixable by merging PHIs from inner
1263 loops as well. When we are looking at close-phi of an outer loop, and
1264 arguments flowing out of inner loop as not been collected by the
1265 outer-loop close phi, we will hit this situation. For now we just bail
1266 out. See: gfortran.dg/graphite/interchange-3.f90. */
1270 add_phi_arg (new_phi
, new_name
, single_pred_edge (new_bb
),
1271 get_loc (old_name
));
1274 fprintf (dump_file
, "\n[codegen] Adding loop-closed phi: ");
1275 print_gimple_stmt (dump_file
, new_phi
, 0, 0);
1278 update_stmt (new_phi
);
1280 /* When there is no loop guard around this codegenerated loop, there is no
1281 need to collect the close-phi arg. */
1282 if (2 != EDGE_COUNT (close_phi_merge_bb
->preds
))
1285 /* Add a PHI in the close_phi_merge_bb for each close phi of the loop. */
1286 tree init
= find_init_value_close_phi (new_phi
);
1288 /* A close phi must come from a loop-phi having an init value. */
1291 gcc_assert (postpone
);
1292 region
->incomplete_phis
.safe_push (std::make_pair (phi
, new_phi
));
1295 fprintf (dump_file
, "\n[codegen] postpone close phi nodes: ");
1296 print_gimple_stmt (dump_file
, new_phi
, 0, 0);
1301 gphi
*merge_phi
= create_phi_node (SSA_NAME_VAR (res
),
1302 close_phi_merge_bb
);
1303 tree merge_res
= create_new_def_for (res
, merge_phi
,
1304 gimple_phi_result_ptr (merge_phi
));
1305 set_rename (res
, merge_res
, region
);
1307 edge from_loop
= single_succ_edge (new_bb
);
1308 add_phi_arg (merge_phi
, new_res
, from_loop
, get_loc (old_name
));
1310 /* The edge coming from loop guard. */
1311 edge other
= from_loop
== (*close_phi_merge_bb
->preds
)[0]
1312 ? (*close_phi_merge_bb
->preds
)[1] : (*close_phi_merge_bb
->preds
)[0];
1314 add_phi_arg (merge_phi
, init
, other
, get_loc (old_name
));
1317 fprintf (dump_file
, "\n[codegen] Adding guard-phi: ");
1318 print_gimple_stmt (dump_file
, merge_phi
, 0, 0);
1321 update_stmt (new_phi
);
1327 /* Copy loop close phi nodes from BB to NEW_BB. */
1330 copy_loop_close_phi_nodes (basic_block old_bb
, basic_block new_bb
,
1334 fprintf (dump_file
, "\n[codegen] copying loop closed phi nodes in bb_%d.",
1336 /* Loop close phi nodes should have only one argument. */
1337 gcc_assert (1 == EDGE_COUNT (old_bb
->preds
));
1339 return copy_loop_close_phi_args (old_bb
, new_bb
, region
, true);
1343 /* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB.
1344 DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates the
1345 other pred of OLD_BB as well. If no such basic block exists then it is NULL.
1346 NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it cannot be
1349 Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice versa.
1350 In this case DOMINATING_PRED = NULL.
1352 Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
1354 Returns true on successful copy of the args, false otherwise. */
1357 add_phi_arg_for_new_expr (tree old_phi_args
[2], tree new_phi_args
[2],
1358 edge old_bb_dominating_edge
,
1359 edge old_bb_non_dominating_edge
,
1360 gphi
*phi
, gphi
*new_phi
,
1361 basic_block new_bb
, sese_info_p region
)
1363 basic_block def_pred
[2];
1364 int not_found_bb_index
= -1;
1365 for (int i
= 0; i
< 2; i
++)
1367 /* If the corresponding def_bb could not be found the entry will be
1369 if (TREE_CODE (old_phi_args
[i
]) == INTEGER_CST
)
1370 def_pred
[i
] = get_def_bb_for_const (region
, new_bb
,
1371 gimple_phi_arg_edge (phi
, i
)->src
);
1373 def_pred
[i
] = gimple_bb (SSA_NAME_DEF_STMT (new_phi_args
[i
]));
1376 gcc_assert (not_found_bb_index
== -1);
1377 not_found_bb_index
= i
;
1381 /* Here we are pattern matching on the structure of CFG w.r.t. old one. */
1382 if (old_bb_dominating_edge
)
1385 basic_block new_pred1
= (*new_bb
->preds
)[0]->src
;
1386 basic_block new_pred2
= (*new_bb
->preds
)[1]->src
;
1387 vec
<basic_block
> *bbs
1388 = region
->copied_bb_map
->get (old_bb_non_dominating_edge
->src
);
1390 basic_block new_pred
= NULL
;
1393 FOR_EACH_VEC_ELT (*bbs
, i
, b
)
1394 if (new_pred1
== b
|| new_pred2
== b
)
1396 gcc_assert (!new_pred
);
1400 gcc_assert (new_pred
);
1402 edge new_non_dominating_edge
= find_edge (new_pred
, new_bb
);
1403 /* By the process of elimination we first insert insert phi-edge for
1404 non-dominating pred which is computed above and then we insert the
1406 int inserted_edge
= 0;
1407 for (; inserted_edge
< 2; inserted_edge
++)
1409 edge new_bb_pred_edge
= gimple_phi_arg_edge (phi
, inserted_edge
);
1410 if (new_non_dominating_edge
== new_bb_pred_edge
)
1412 add_phi_arg (new_phi
, new_phi_args
[inserted_edge
],
1413 new_non_dominating_edge
,
1414 get_loc (old_phi_args
[inserted_edge
]));
1419 int edge_dominating
= 0;
1420 if (inserted_edge
== 0)
1421 edge_dominating
= 1;
1423 edge new_dominating_edge
= NULL
;
1424 for (int i
; i
< 2; i
++)
1426 edge e
= gimple_phi_arg_edge (new_phi
, i
);
1427 if (e
!= new_non_dominating_edge
)
1428 new_dominating_edge
= e
;
1431 add_phi_arg (new_phi
, new_phi_args
[edge_dominating
], new_dominating_edge
,
1432 get_loc (old_phi_args
[inserted_edge
]));
1436 /* Classic diamond structure: both edges are non-dominating. We need to
1437 find one unique edge then the other can be found be elimination. If
1438 any definition (def_pred) dominates both the preds of new_bb then we
1439 bail out. Entries of def_pred maybe NULL, in that case we must
1440 uniquely find pred with help of only one entry. */
1441 edge new_e
[2] = { NULL
, NULL
};
1442 for (int i
= 0; i
< 2; i
++)
1446 FOR_EACH_EDGE (e
, ei
, new_bb
->preds
)
1448 && dominated_by_p (CDI_DOMINATORS
, e
->src
, def_pred
[i
]))
1451 /* We do not know how to handle the case when def_pred
1452 dominates more than a predecessor. */
1458 gcc_assert (new_e
[0] || new_e
[1]);
1460 /* Find the other edge by process of elimination. */
1461 if (not_found_bb_index
!= -1)
1463 gcc_assert (!new_e
[not_found_bb_index
]);
1464 int found_bb_index
= not_found_bb_index
== 1 ? 0 : 1;
1467 FOR_EACH_EDGE (e
, ei
, new_bb
->preds
)
1469 if (new_e
[found_bb_index
] == e
)
1471 new_e
[not_found_bb_index
] = e
;
1475 /* Add edges to phi args. */
1476 for (int i
= 0; i
< 2; i
++)
1477 add_phi_arg (new_phi
, new_phi_args
[i
], new_e
[i
],
1478 get_loc (old_phi_args
[i
]));
1484 /* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated
1485 region. If postpone is true and it isn't possible to copy any arg of PHI,
1486 the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated
1487 later. Returns false if the copying was unsuccessful. */
1490 copy_cond_phi_args (gphi
*phi
, gphi
*new_phi
, vec
<tree
> iv_map
,
1491 sese_info_p region
, bool postpone
)
1494 fprintf (dump_file
, "\n[codegen] copying cond phi args: ");
1495 gcc_assert (2 == gimple_phi_num_args (phi
));
1497 basic_block new_bb
= gimple_bb (new_phi
);
1498 loop_p loop
= gimple_bb (phi
)->loop_father
;
1500 basic_block old_bb
= gimple_bb (phi
);
1501 edge old_bb_non_dominating_edge
= NULL
, old_bb_dominating_edge
= NULL
;
1505 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
1506 if (!dominated_by_p (CDI_DOMINATORS
, old_bb
, e
->src
))
1507 old_bb_non_dominating_edge
= e
;
1509 old_bb_dominating_edge
= e
;
1511 gcc_assert (!dominated_by_p (CDI_DOMINATORS
, old_bb
,
1512 old_bb_non_dominating_edge
->src
));
1514 tree new_phi_args
[2];
1515 tree old_phi_args
[2];
1517 for (unsigned i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1519 tree old_name
= gimple_phi_arg_def (phi
, i
);
1520 tree new_name
= get_new_name (region
, new_bb
, old_name
, old_bb
, false);
1521 old_phi_args
[i
] = old_name
;
1524 new_phi_args
[i
] = new_name
;
1528 if (vec_find (region
->params
, old_name
))
1530 new_phi_args
[i
] = old_name
;
1534 "\n[codegen] parameter argument to phi, new_expr: ");
1535 print_gimple_stmt (dump_file
, new_phi
, 0, 0);
1540 /* If the phi-arg is scev-analyzeable but only in the first stage. */
1541 if (postpone
&& is_gimple_reg (old_name
)
1542 && scev_analyzable_p (old_name
, region
->region
))
1545 bool gloog_error
= false;
1547 = get_rename_from_scev (old_name
, &stmts
, loop
, new_bb
,
1548 old_bb
, iv_map
, region
, &gloog_error
);
1552 gcc_assert (new_expr
);
1555 fprintf (dump_file
, "\n[codegen] scev analyzeable, new_expr: ");
1556 print_generic_expr (dump_file
, new_expr
, 0);
1558 gsi_insert_earliest (stmts
, region
);
1559 new_phi_args
[i
] = new_name
;
1563 gimple
*old_def_stmt
= SSA_NAME_DEF_STMT (old_name
);
1564 if (!old_def_stmt
|| gimple_code (old_def_stmt
) == GIMPLE_NOP
)
1565 /* If the phi arg was a function arg, or wasn't defined, just use the
1570 /* Postpone code gen for later for back-edges. */
1571 region
->incomplete_phis
.safe_push (std::make_pair (phi
, new_phi
));
1575 fprintf (dump_file
, "\n[codegen] postpone cond phi nodes: ");
1576 print_gimple_stmt (dump_file
, new_phi
, 0, 0);
1579 new_phi_args
[i
] = NULL_TREE
;
1586 return add_phi_arg_for_new_expr (old_phi_args
, new_phi_args
,
1587 old_bb_dominating_edge
,
1588 old_bb_non_dominating_edge
,
1589 phi
, new_phi
, new_bb
, region
);
1592 /* Copy cond phi nodes from BB to NEW_BB. */
1595 copy_cond_phi_nodes (basic_block bb
, basic_block new_bb
, vec
<tree
> iv_map
,
1599 gcc_assert (!bb_contains_loop_close_phi_nodes (bb
));
1602 fprintf (dump_file
, "\n[codegen] copying cond phi nodes in bb_%d:",
1605 /* Cond phi nodes should have exactly two arguments. */
1606 gcc_assert (2 == EDGE_COUNT (bb
->preds
));
1608 for (gphi_iterator psi
= gsi_start_phis (bb
); !gsi_end_p (psi
);
1611 gphi
*phi
= psi
.phi ();
1612 tree res
= gimple_phi_result (phi
);
1613 if (virtual_operand_p (res
))
1615 if (is_gimple_reg (res
) && scev_analyzable_p (res
, region
->region
))
1616 /* Cond phi nodes should not be scev_analyzable_p. */
1619 gphi
*new_phi
= create_phi_node (SSA_NAME_VAR (res
), new_bb
);
1620 tree new_res
= create_new_def_for (res
, new_phi
,
1621 gimple_phi_result_ptr (new_phi
));
1622 set_rename (res
, new_res
, region
);
1624 if (!copy_cond_phi_args (phi
, new_phi
, iv_map
, region
, true))
1627 update_stmt (new_phi
);
1633 /* Return true if STMT should be copied from region to the
1634 new code-generated region. LABELs, CONDITIONS, induction-variables
1635 and region parameters need not be copied. */
1638 should_copy_to_new_region (gimple
*stmt
, sese_info_p region
)
1640 /* Do not copy labels or conditions. */
1641 if (gimple_code (stmt
) == GIMPLE_LABEL
1642 || gimple_code (stmt
) == GIMPLE_COND
)
1646 /* Do not copy induction variables. */
1647 if (is_gimple_assign (stmt
)
1648 && (lhs
= gimple_assign_lhs (stmt
))
1649 && TREE_CODE (lhs
) == SSA_NAME
1650 && is_gimple_reg (lhs
)
1651 && scev_analyzable_p (lhs
, region
->region
))
1657 /* Create new names for all the definitions created by COPY and
1658 add replacement mappings for each new name. */
1661 set_rename_for_each_def (gimple
*stmt
, sese_info_p region
)
1663 def_operand_p def_p
;
1664 ssa_op_iter op_iter
;
1665 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, op_iter
, SSA_OP_ALL_DEFS
)
1667 tree old_name
= DEF_FROM_PTR (def_p
);
1668 tree new_name
= create_new_def_for (old_name
, stmt
, def_p
);
1669 set_rename (old_name
, new_name
, region
);
1673 /* Duplicates the statements of basic block BB into basic block NEW_BB
1674 and compute the new induction variables according to the IV_MAP.
1675 GLOOG_ERROR is set when the code generation cannot continue. */
1677 graphite_copy_stmts_from_block (basic_block bb
, basic_block new_bb
,
1678 vec
<tree
> iv_map
, sese_info_p region
,
1681 /* Iterator poining to the place where new statement (s) will be inserted. */
1682 gimple_stmt_iterator gsi_tgt
= gsi_last_bb (new_bb
);
1684 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
1687 gimple
*stmt
= gsi_stmt (gsi
);
1688 if (!should_copy_to_new_region (stmt
, region
))
1691 /* Create a new copy of STMT and duplicate STMT's virtual
1693 gimple
*copy
= gimple_copy (stmt
);
1694 gsi_insert_after (&gsi_tgt
, copy
, GSI_NEW_STMT
);
1698 fprintf (dump_file
, "\n[codegen] inserting statement: ");
1699 print_gimple_stmt (dump_file
, copy
, 0, 0);
1702 maybe_duplicate_eh_stmt (copy
, stmt
);
1703 gimple_duplicate_stmt_histograms (cfun
, copy
, cfun
, stmt
);
1705 /* Crete new names for each def in the copied stmt. */
1706 set_rename_for_each_def (copy
, region
);
1708 loop_p loop
= bb
->loop_father
;
1709 if (rename_uses (copy
, &gsi_tgt
, bb
, region
, loop
, iv_map
, gloog_error
))
1711 fold_stmt_inplace (&gsi_tgt
);
1712 gcc_assert (gsi_stmt (gsi_tgt
) == copy
);
1724 /* Copies BB and includes in the copied BB all the statements that can
1725 be reached following the use-def chains from the memory accesses,
1726 and returns the next edge following this new block. GLOOG_ERROR is
1727 set when the code generation cannot continue. */
1730 copy_bb_and_scalar_dependences (basic_block bb
, sese_info_p region
,
1731 edge next_e
, vec
<tree
> iv_map
,
1734 int num_phis
= number_of_phi_nodes (bb
);
1736 if (region
->copied_bb_map
->get (bb
))
1738 /* FIXME: We do not handle inner loop unrolling when the inner loop has
1739 phi-nodes. In that case inner loop will be copied multiple times
1740 outside the region. */
1743 *codegen_err
= true;
1748 basic_block new_bb
= split_edge (next_e
);
1749 if (num_phis
> 0 && bb_contains_loop_phi_nodes (bb
))
1751 basic_block phi_bb
= next_e
->dest
->loop_father
->header
;
1753 /* At this point we are unable to codegenerate by still preserving the SSA
1754 structure because maybe the loop is completely unrolled and the PHIs
1755 and cross-bb scalar dependencies are untrackable w.r.t. the original
1756 code. See gfortran.dg/graphite/pr29832.f90. */
1757 if (EDGE_COUNT (bb
->preds
) != EDGE_COUNT (phi_bb
->preds
))
1759 *codegen_err
= true;
1764 fprintf (dump_file
, "\n[codegen] bb_%d contains loop phi nodes",
1766 if (!copy_loop_phi_nodes (bb
, phi_bb
, region
))
1768 *codegen_err
= true;
1772 else if (bb_contains_loop_close_phi_nodes (bb
))
1775 fprintf (dump_file
, "\n[codegen] bb_%d contains close phi nodes",
1778 /* Make sure that NEW_BB is the loop->exit->dest. */
1779 edge e
= single_pred_edge (new_bb
);
1780 basic_block phi_bb
= new_bb
;
1781 if (e
->src
->loop_father
== e
->dest
->loop_father
)
1783 /* This is one of the places which shows preserving original structure
1784 is not always possible, as we may need to insert close PHI for a
1785 loop where the latch does not have any mapping, or the mapping is
1787 basic_block old_loop_bb
= single_pred_edge (bb
)->src
;
1788 vec
<basic_block
> *bbs
= region
->copied_bb_map
->get (old_loop_bb
);
1789 if (!bbs
|| bbs
->length () != 1)
1791 *codegen_err
= true;
1795 basic_block new_loop_bb
= (*bbs
)[0];
1796 loop_p new_loop
= new_loop_bb
->loop_father
;
1797 phi_bb
= single_exit (new_loop
)->dest
;
1798 e
= single_pred_edge (phi_bb
);
1801 gcc_assert (e
->src
->loop_father
!= e
->dest
->loop_father
);
1803 if (!copy_loop_close_phi_nodes (bb
, phi_bb
, region
))
1805 *codegen_err
= true;
1809 else if (num_phis
> 0)
1812 fprintf (dump_file
, "\n[codegen] bb_%d contains cond phi nodes",
1815 basic_block phi_bb
= single_pred (new_bb
);
1816 loop_p loop_father
= new_bb
->loop_father
;
1818 /* Move back until we find the block with two predecessors. */
1819 while (single_pred_p (phi_bb
))
1820 phi_bb
= single_pred_edge (phi_bb
)->src
;
1822 /* If a corresponding merge-point was not found, then abort codegen. */
1823 if (phi_bb
->loop_father
!= loop_father
1824 || !copy_cond_phi_nodes (bb
, phi_bb
, iv_map
, region
))
1826 *codegen_err
= true;
1832 fprintf (dump_file
, "\n[codegen] copying from bb_%d to bb_%d",
1833 bb
->index
, new_bb
->index
);
1835 vec
<basic_block
> *copied_bbs
= region
->copied_bb_map
->get (bb
);
1837 copied_bbs
->safe_push (new_bb
);
1840 vec
<basic_block
> bbs
;
1842 bbs
.safe_push (new_bb
);
1843 region
->copied_bb_map
->put (bb
, bbs
);
1846 if (!graphite_copy_stmts_from_block (bb
, new_bb
, iv_map
, region
, codegen_err
))
1848 *codegen_err
= true;
1852 return single_succ_edge (new_bb
);
1855 /* Returns the outermost loop in SCOP that contains BB. */
1858 outermost_loop_in_sese_1 (sese_l
®ion
, basic_block bb
)
1862 nest
= bb
->loop_father
;
1863 while (loop_outer (nest
)
1864 && loop_in_sese_p (loop_outer (nest
), region
))
1865 nest
= loop_outer (nest
);
1870 /* Same as outermost_loop_in_sese_1, returns the outermost loop
1871 containing BB in REGION, but makes sure that the returned loop
1872 belongs to the REGION, and so this returns the first loop in the
1873 REGION when the loop containing BB does not belong to REGION. */
1876 outermost_loop_in_sese (sese_l
®ion
, basic_block bb
)
1878 loop_p nest
= outermost_loop_in_sese_1 (region
, bb
);
1880 if (loop_in_sese_p (nest
, region
))
1883 /* When the basic block BB does not belong to a loop in the region,
1884 return the first loop in the region. */
1887 if (loop_in_sese_p (nest
, region
))
1896 /* Sets the false region of an IF_REGION to REGION. */
1899 if_region_set_false_region (ifsese if_region
, sese_info_p region
)
1901 basic_block condition
= if_region_get_condition_block (if_region
);
1902 edge false_edge
= get_false_edge_from_guard_bb (condition
);
1903 basic_block dummy
= false_edge
->dest
;
1904 edge entry_region
= region
->region
.entry
;
1905 edge exit_region
= region
->region
.exit
;
1906 basic_block before_region
= entry_region
->src
;
1907 basic_block last_in_region
= exit_region
->src
;
1908 hashval_t hash
= htab_hash_pointer (exit_region
);
1910 = current_loops
->exits
->find_slot_with_hash (exit_region
, hash
, NO_INSERT
);
1912 entry_region
->flags
= false_edge
->flags
;
1913 false_edge
->flags
= exit_region
->flags
;
1915 redirect_edge_pred (entry_region
, condition
);
1916 redirect_edge_pred (exit_region
, before_region
);
1917 redirect_edge_pred (false_edge
, last_in_region
);
1918 redirect_edge_succ (false_edge
, single_succ (dummy
));
1919 delete_basic_block (dummy
);
1921 exit_region
->flags
= EDGE_FALLTHRU
;
1922 recompute_all_dominators ();
1924 region
->region
.exit
= false_edge
;
1926 free (if_region
->false_region
);
1927 if_region
->false_region
= region
;
1931 struct loop_exit
*loop_exit
= ggc_cleared_alloc
<struct loop_exit
> ();
1933 memcpy (loop_exit
, *((struct loop_exit
**) slot
), sizeof (struct loop_exit
));
1934 current_loops
->exits
->clear_slot (slot
);
1936 hashval_t hash
= htab_hash_pointer (false_edge
);
1937 slot
= current_loops
->exits
->find_slot_with_hash (false_edge
, hash
,
1939 loop_exit
->e
= false_edge
;
1941 false_edge
->src
->loop_father
->exits
->next
= loop_exit
;
1945 /* Creates an IFSESE with CONDITION on edge ENTRY. */
1948 create_if_region_on_edge (edge entry
, tree condition
)
1952 sese_info_p sese_region
= XNEW (struct sese_info_t
);
1953 sese_info_p true_region
= XNEW (struct sese_info_t
);
1954 sese_info_p false_region
= XNEW (struct sese_info_t
);
1955 ifsese if_region
= XNEW (struct ifsese_s
);
1956 edge exit
= create_empty_if_region_on_edge (entry
, condition
);
1958 if_region
->region
= sese_region
;
1959 if_region
->region
->region
.entry
= entry
;
1960 if_region
->region
->region
.exit
= exit
;
1962 FOR_EACH_EDGE (e
, ei
, entry
->dest
->succs
)
1964 if (e
->flags
& EDGE_TRUE_VALUE
)
1966 true_region
->region
.entry
= e
;
1967 true_region
->region
.exit
= single_succ_edge (e
->dest
);
1968 if_region
->true_region
= true_region
;
1970 else if (e
->flags
& EDGE_FALSE_VALUE
)
1972 false_region
->region
.entry
= e
;
1973 false_region
->region
.exit
= single_succ_edge (e
->dest
);
1974 if_region
->false_region
= false_region
;
1981 /* Moves REGION in a condition expression:
1989 move_sese_in_condition (sese_info_p region
)
1991 basic_block pred_block
= split_edge (region
->region
.entry
);
1994 region
->region
.entry
= single_succ_edge (pred_block
);
1995 if_region
= create_if_region_on_edge (single_pred_edge (pred_block
),
1997 if_region_set_false_region (if_region
, region
);
2002 /* Replaces the condition of the IF_REGION with CONDITION:
2010 set_ifsese_condition (ifsese if_region
, tree condition
)
2012 sese_info_p region
= if_region
->region
;
2013 edge entry
= region
->region
.entry
;
2014 basic_block bb
= entry
->dest
;
2015 gimple
*last
= last_stmt (bb
);
2016 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
2019 gcc_assert (gimple_code (last
) == GIMPLE_COND
);
2021 gsi_remove (&gsi
, true);
2022 gsi
= gsi_last_bb (bb
);
2023 condition
= force_gimple_operand_gsi (&gsi
, condition
, true, NULL
,
2024 false, GSI_NEW_STMT
);
2025 cond_stmt
= gimple_build_cond_from_tree (condition
, NULL_TREE
, NULL_TREE
);
2026 gsi
= gsi_last_bb (bb
);
2027 gsi_insert_after (&gsi
, cond_stmt
, GSI_NEW_STMT
);
2030 /* Return true when T is defined outside REGION or when no definitions are
2031 variant in REGION. When HAS_VDEFS is a valid pointer, sets HAS_VDEFS to true
2032 when T depends on memory that may change in REGION. */
2035 invariant_in_sese_p_rec (tree t
, sese_l
®ion
, bool *has_vdefs
)
2037 if (!defined_in_sese_p (t
, region
))
2040 gimple
*stmt
= SSA_NAME_DEF_STMT (t
);
2042 if (gimple_code (stmt
) == GIMPLE_PHI
2043 || gimple_code (stmt
) == GIMPLE_CALL
)
2046 /* VDEF is variant when it is in the region. */
2047 if (gimple_vdef (stmt
))
2054 /* A VUSE may or may not be variant following the VDEFs. */
2055 if (tree vuse
= gimple_vuse (stmt
))
2056 return invariant_in_sese_p_rec (vuse
, region
, has_vdefs
);
2059 use_operand_p use_p
;
2060 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
2062 tree use
= USE_FROM_PTR (use_p
);
2064 if (!defined_in_sese_p (use
, region
))
2067 if (!invariant_in_sese_p_rec (use
, region
, has_vdefs
))
2074 /* Returns the scalar evolution of T in REGION. Every variable that
2075 is not defined in the REGION is considered a parameter. */
2078 scalar_evolution_in_region (sese_l
®ion
, loop_p loop
, tree t
)
2081 struct loop
*def_loop
;
2082 basic_block before
= region
.entry
->src
;
2084 /* SCOP parameters. */
2085 if (TREE_CODE (t
) == SSA_NAME
2086 && !defined_in_sese_p (t
, region
))
2089 if (TREE_CODE (t
) != SSA_NAME
2090 || loop_in_sese_p (loop
, region
))
2091 return instantiate_scev (before
, loop
,
2092 analyze_scalar_evolution (loop
, t
));
2094 def
= SSA_NAME_DEF_STMT (t
);
2095 def_loop
= loop_containing_stmt (def
);
2097 if (loop_in_sese_p (def_loop
, region
))
2099 t
= analyze_scalar_evolution (def_loop
, t
);
2100 def_loop
= superloop_at_depth (def_loop
, loop_depth (loop
) + 1);
2101 t
= compute_overall_effect_of_inner_loop (def_loop
, t
);
2105 bool has_vdefs
= false;
2106 if (invariant_in_sese_p_rec (t
, region
, &has_vdefs
))
2109 /* T variates in REGION. */
2111 return chrec_dont_know
;
2113 return instantiate_scev (before
, loop
, t
);