+/* Returns the incoming edges of basic_block BB in the pair. The first edge is
+ the init edge (from outside the loop) and the second one is the back edge
+ from the same loop. */
+
+std::pair<edge, edge>
+get_edges (basic_block bb)
+{
+ std::pair<edge, edge> edges;
+ edge e;
+ edge_iterator ei;
+ FOR_EACH_EDGE (e, ei, bb->preds)
+ if (bb->loop_father != e->src->loop_father)
+ edges.first = e;
+ else
+ edges.second = e;
+ return edges;
+}
+
+/* Copy the PHI arguments from OLD_PHI to the NEW_PHI. The arguments to NEW_PHI
+ must be found unless they can be POSTPONEd for later. */
+
+void
+copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
+ gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
+ sese_info_p region, bool postpone)
+{
+ gcc_assert (gimple_phi_num_args (old_phi) == gimple_phi_num_args (new_phi));
+
+ basic_block new_bb = gimple_bb (new_phi);
+ for (unsigned i = 0; i < gimple_phi_num_args (old_phi); i++)
+ {
+ edge e;
+ if (gimple_phi_arg_edge (old_phi, i) == ibp_old_bb.first)
+ e = ibp_new_bb.first;
+ else
+ e = ibp_new_bb.second;
+
+ tree old_name = gimple_phi_arg_def (old_phi, i);
+ tree new_name = get_new_name (region, new_bb, old_name,
+ gimple_bb (old_phi), true);
+ if (new_name)
+ {
+ add_phi_arg (new_phi, new_name, e, get_loc (old_name));
+ continue;
+ }
+
+ gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
+ if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
+ /* If the phi arg was a function arg, or wasn't defined, just use the old
+ name. */
+ add_phi_arg (new_phi, old_name, e, get_loc (old_name));
+ else if (postpone)
+ {
+ /* Postpone code gen for later for those back-edges we don't have the
+ names yet. */
+ region->incomplete_phis.safe_push (std::make_pair (old_phi, new_phi));
+ if (dump_file)
+ fprintf (dump_file, "\n[codegen] postpone loop phi nodes: ");
+ }
+ else
+ /* Either we should add the arg to phi or, we should postpone. */
+ gcc_unreachable ();
+ }
+}
+
+/* Copy loop phi nodes from BB to NEW_BB. */
+
+static bool
+copy_loop_phi_nodes (basic_block bb, basic_block new_bb, sese_info_p region)
+{
+ if (dump_file)
+ fprintf (dump_file, "\n[codegen] copying loop phi nodes in bb_%d.",
+ new_bb->index);
+
+ /* Loop phi nodes should have only two arguments. */
+ gcc_assert (2 == EDGE_COUNT (bb->preds));
+
+ /* First edge is the init edge and second is the back edge. */
+ init_back_edge_pair_t ibp_old_bb = get_edges (bb);
+
+ /* First edge is the init edge and second is the back edge. */
+ init_back_edge_pair_t ibp_new_bb = get_edges (new_bb);
+
+ for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
+ gsi_next (&psi))
+ {
+ gphi *phi = psi.phi ();
+ tree res = gimple_phi_result (phi);
+ if (virtual_operand_p (res))
+ continue;
+ if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
+ continue;
+
+ gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb);
+ tree new_res = create_new_def_for (res, new_phi,
+ gimple_phi_result_ptr (new_phi));
+ set_rename (res, new_res, region);
+ copy_loop_phi_args (phi, ibp_old_bb, new_phi, ibp_new_bb, region, true);
+ update_stmt (new_phi);
+ }
+
+ return true;
+}
+
+/* Return the init value of PHI, the value coming from outside the loop. */
+
+static tree
+get_loop_init_value (gphi *phi)
+{
+
+ loop_p loop = gimple_bb (phi)->loop_father;
+
+ edge e;
+ edge_iterator ei;
+ FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
+ if (e->src->loop_father != loop)
+ return gimple_phi_arg_def (phi, e->dest_idx);
+
+ return NULL_TREE;
+}
+
+/* Find the init value (the value which comes from outside the loop), of one of
+ the operands of DEF which is defined by a loop phi. */
+
+static tree
+find_init_value (gimple *def)
+{
+ if (gimple_code (def) == GIMPLE_PHI)
+ return get_loop_init_value (as_a <gphi*> (def));
+
+ if (gimple_vuse (def))
+ return NULL_TREE;
+
+ ssa_op_iter iter;
+ use_operand_p use_p;
+ FOR_EACH_SSA_USE_OPERAND (use_p, def, iter, SSA_OP_USE)
+ {
+ tree use = USE_FROM_PTR (use_p);
+ if (TREE_CODE (use) == SSA_NAME)
+ {
+ if (tree res = find_init_value (SSA_NAME_DEF_STMT (use)))
+ return res;
+ }
+ }
+
+ return NULL_TREE;
+}
+
+/* Return the init value, the value coming from outside the loop. */
+
+static tree
+find_init_value_close_phi (gphi *phi)
+{
+ gcc_assert (gimple_phi_num_args (phi) == 1);
+ tree use_arg = gimple_phi_arg_def (phi, 0);
+ gimple *def = SSA_NAME_DEF_STMT (use_arg);
+ return find_init_value (def);
+}
+
+/* Copy all the loop-close phi args from BB to NEW_BB. */
+
+bool
+copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb,
+ sese_info_p region, bool postpone)
+{
+ /* The successor of bb having close phi should be a merge of the diamond
+ inserted to guard the loop during codegen. */
+ basic_block close_phi_merge_bb = single_succ (new_bb);
+
+ for (gphi_iterator psi = gsi_start_phis (old_bb); !gsi_end_p (psi);
+ gsi_next (&psi))
+ {
+ gphi *phi = psi.phi ();
+ tree res = gimple_phi_result (phi);
+ if (virtual_operand_p (res))
+ continue;
+
+ if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
+ /* Loop close phi nodes should not be scev_analyzable_p. */
+ gcc_unreachable ();
+
+ gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb);
+ tree new_res = create_new_def_for (res, new_phi,
+ gimple_phi_result_ptr (new_phi));
+ set_rename (res, new_res, region);
+
+ tree old_name = gimple_phi_arg_def (phi, 0);
+ tree new_name = get_new_name (region, new_bb, old_name, old_bb, false);
+
+ /* Predecessor basic blocks of a loop close phi should have been code
+ generated before. FIXME: This is fixable by merging PHIs from inner
+ loops as well. When we are looking at close-phi of an outer loop, and
+ arguments flowing out of inner loop as not been collected by the
+ outer-loop close phi, we will hit this situation. For now we just bail
+ out. See: gfortran.dg/graphite/interchange-3.f90. */
+ if (!new_name)
+ return false;
+
+ add_phi_arg (new_phi, new_name, single_pred_edge (new_bb),
+ get_loc (old_name));
+ if (dump_file)
+ {
+ fprintf (dump_file, "\n[codegen] Adding loop-closed phi: ");
+ print_gimple_stmt (dump_file, new_phi, 0, 0);
+ }
+
+ update_stmt (new_phi);
+
+ /* When there is no loop guard around this codegenerated loop, there is no
+ need to collect the close-phi arg. */
+ if (2 != EDGE_COUNT (close_phi_merge_bb->preds))
+ continue;
+
+ /* Add a PHI in the close_phi_merge_bb for each close phi of the loop. */
+ tree init = find_init_value_close_phi (new_phi);
+
+ /* A close phi must come from a loop-phi having an init value. */
+ if (!init)
+ {
+ gcc_assert (postpone);
+ region->incomplete_phis.safe_push (std::make_pair (phi, new_phi));
+ if (dump_file)
+ {
+ fprintf (dump_file, "\n[codegen] postpone close phi nodes: ");
+ print_gimple_stmt (dump_file, new_phi, 0, 0);
+ }
+ continue;
+ }
+
+ gphi *merge_phi = create_phi_node (SSA_NAME_VAR (res),
+ close_phi_merge_bb);
+ tree merge_res = create_new_def_for (res, merge_phi,
+ gimple_phi_result_ptr (merge_phi));
+ set_rename (res, merge_res, region);
+
+ edge from_loop = single_succ_edge (new_bb);
+ add_phi_arg (merge_phi, new_res, from_loop, get_loc (old_name));
+
+ /* The edge coming from loop guard. */
+ edge other = from_loop == (*close_phi_merge_bb->preds)[0]
+ ? (*close_phi_merge_bb->preds)[1] : (*close_phi_merge_bb->preds)[0];
+
+ add_phi_arg (merge_phi, init, other, get_loc (old_name));
+ if (dump_file)
+ {
+ fprintf (dump_file, "\n[codegen] Adding guard-phi: ");
+ print_gimple_stmt (dump_file, merge_phi, 0, 0);
+ }
+
+ update_stmt (new_phi);
+ }
+
+ return true;
+}
+
+/* Copy loop close phi nodes from BB to NEW_BB. */
+
+static bool
+copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb,
+ sese_info_p region)
+{
+ if (dump_file)
+ fprintf (dump_file, "\n[codegen] copying loop closed phi nodes in bb_%d.",
+ new_bb->index);
+ /* Loop close phi nodes should have only one argument. */
+ gcc_assert (1 == EDGE_COUNT (old_bb->preds));
+
+ return copy_loop_close_phi_args (old_bb, new_bb, region, true);
+}
+
+
+/* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB.
+ DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates the
+ other pred of OLD_BB as well. If no such basic block exists then it is NULL.
+ NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it cannot be
+ NULL.
+
+ Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice versa.
+ In this case DOMINATING_PRED = NULL.
+
+ Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
+
+ Returns true on successful copy of the args, false otherwise. */
+
+static bool
+add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
+ edge old_bb_dominating_edge,
+ edge old_bb_non_dominating_edge,
+ gphi *phi, gphi *new_phi,
+ basic_block new_bb, sese_info_p region)
+{
+ basic_block def_pred[2];
+ int not_found_bb_index = -1;
+ for (int i = 0; i < 2; i++)
+ {
+ /* If the corresponding def_bb could not be found the entry will be
+ NULL. */
+ if (TREE_CODE (old_phi_args[i]) == INTEGER_CST)
+ def_pred[i] = get_def_bb_for_const (region, new_bb,
+ gimple_phi_arg_edge (phi, i)->src);
+ else
+ def_pred[i] = gimple_bb (SSA_NAME_DEF_STMT (new_phi_args[i]));
+ if (!def_pred[i])
+ {
+ gcc_assert (not_found_bb_index == -1);
+ not_found_bb_index = i;
+ }
+ }
+
+ /* Here we are pattern matching on the structure of CFG w.r.t. old one. */
+ if (old_bb_dominating_edge)
+ {
+ return false;
+ basic_block new_pred1 = (*new_bb->preds)[0]->src;
+ basic_block new_pred2 = (*new_bb->preds)[1]->src;
+ vec <basic_block> *bbs
+ = region->copied_bb_map->get (old_bb_non_dominating_edge->src);
+ gcc_assert (bbs);
+ basic_block new_pred = NULL;
+ basic_block b;
+ int i;
+ FOR_EACH_VEC_ELT (*bbs, i, b)
+ if (new_pred1 == b || new_pred2 == b)
+ {
+ gcc_assert (!new_pred);
+ new_pred = b;
+ }
+
+ gcc_assert (new_pred);
+
+ edge new_non_dominating_edge = find_edge (new_pred, new_bb);
+ /* By the process of elimination we first insert insert phi-edge for
+ non-dominating pred which is computed above and then we insert the
+ remaining one. */
+ int inserted_edge = 0;
+ for (; inserted_edge < 2; inserted_edge++)
+ {
+ edge new_bb_pred_edge = gimple_phi_arg_edge (phi, inserted_edge);
+ if (new_non_dominating_edge == new_bb_pred_edge)
+ {
+ add_phi_arg (new_phi, new_phi_args[inserted_edge],
+ new_non_dominating_edge,
+ get_loc (old_phi_args[inserted_edge]));
+ break;
+ }
+ }
+
+ int edge_dominating = 0;
+ if (inserted_edge == 0)
+ edge_dominating = 1;
+
+ edge new_dominating_edge = NULL;
+ for (int i; i < 2; i++)
+ {
+ edge e = gimple_phi_arg_edge (new_phi, i);
+ if (e != new_non_dominating_edge)
+ new_dominating_edge = e;
+ }
+
+ add_phi_arg (new_phi, new_phi_args[edge_dominating], new_dominating_edge,
+ get_loc (old_phi_args[inserted_edge]));
+ }
+ else
+ {
+ /* Classic diamond structure: both edges are non-dominating. We need to
+ find one unique edge then the other can be found be elimination. If
+ any definition (def_pred) dominates both the preds of new_bb then we
+ bail out. Entries of def_pred maybe NULL, in that case we must
+ uniquely find pred with help of only one entry. */
+ edge new_e[2] = { NULL, NULL };
+ for (int i = 0; i < 2; i++)
+ {
+ edge e;
+ edge_iterator ei;
+ FOR_EACH_EDGE (e, ei, new_bb->preds)
+ if (def_pred[i]
+ && dominated_by_p (CDI_DOMINATORS, e->src, def_pred[i]))
+ {
+ if (new_e[i])
+ /* We do not know how to handle the case when def_pred
+ dominates more than a predecessor. */
+ return false;
+ new_e[i] = e;
+ }
+ }
+
+ gcc_assert (new_e[0] || new_e[1]);
+
+ /* Find the other edge by process of elimination. */
+ if (not_found_bb_index != -1)
+ {
+ gcc_assert (!new_e[not_found_bb_index]);
+ int found_bb_index = not_found_bb_index == 1 ? 0 : 1;
+ edge e;
+ edge_iterator ei;
+ FOR_EACH_EDGE (e, ei, new_bb->preds)
+ {
+ if (new_e[found_bb_index] == e)
+ continue;
+ new_e[not_found_bb_index] = e;
+ }
+ }
+
+ /* Add edges to phi args. */
+ for (int i = 0; i < 2; i++)
+ add_phi_arg (new_phi, new_phi_args[i], new_e[i],
+ get_loc (old_phi_args[i]));
+ }
+
+ return true;
+}
+
+/* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated
+ region. If postpone is true and it isn't possible to copy any arg of PHI,
+ the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated
+ later. Returns false if the copying was unsuccessful. */
+
+bool
+copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map,
+ sese_info_p region, bool postpone)
+{
+ if (dump_file)
+ fprintf (dump_file, "\n[codegen] copying cond phi args: ");
+ gcc_assert (2 == gimple_phi_num_args (phi));
+
+ basic_block new_bb = gimple_bb (new_phi);
+ loop_p loop = gimple_bb (phi)->loop_father;
+
+ basic_block old_bb = gimple_bb (phi);
+ edge old_bb_non_dominating_edge = NULL, old_bb_dominating_edge = NULL;
+
+ edge e;
+ edge_iterator ei;
+ FOR_EACH_EDGE (e, ei, old_bb->preds)
+ if (!dominated_by_p (CDI_DOMINATORS, old_bb, e->src))
+ old_bb_non_dominating_edge = e;
+ else
+ old_bb_dominating_edge = e;
+
+ gcc_assert (!dominated_by_p (CDI_DOMINATORS, old_bb,
+ old_bb_non_dominating_edge->src));
+
+ tree new_phi_args[2];
+ tree old_phi_args[2];
+
+ for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
+ {
+ tree old_name = gimple_phi_arg_def (phi, i);
+ tree new_name = get_new_name (region, new_bb, old_name, old_bb, false);
+ old_phi_args[i] = old_name;
+ if (new_name)
+ {
+ new_phi_args [i] = new_name;
+ continue;
+ }
+
+ if (vec_find (region->params, old_name))
+ {
+ new_phi_args [i] = old_name;
+ if (dump_file)
+ {
+ fprintf (dump_file,
+ "\n[codegen] parameter argument to phi, new_expr: ");
+ print_gimple_stmt (dump_file, new_phi, 0, 0);
+ }
+ continue;
+ }
+
+ /* If the phi-arg is scev-analyzeable but only in the first stage. */
+ if (postpone && is_gimple_reg (old_name)
+ && scev_analyzable_p (old_name, region->region))
+ {
+ gimple_seq stmts;
+ bool gloog_error = false;
+ tree new_expr
+ = get_rename_from_scev (old_name, &stmts, loop, new_bb,
+ old_bb, iv_map, region, &gloog_error);
+ if (gloog_error)
+ return false;
+
+ gcc_assert (new_expr);
+ if (dump_file)
+ {
+ fprintf (dump_file, "\n[codegen] scev analyzeable, new_expr: ");
+ print_generic_expr (dump_file, new_expr, 0);
+ }
+ gsi_insert_earliest (stmts, region);
+ new_phi_args [i] = new_name;
+ continue;
+ }
+
+ gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
+ if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
+ /* If the phi arg was a function arg, or wasn't defined, just use the
+ old name. */
+ gcc_unreachable ();
+ else if (postpone)
+ {
+ /* Postpone code gen for later for back-edges. */
+ region->incomplete_phis.safe_push (std::make_pair (phi, new_phi));
+
+ if (dump_file)
+ {
+ fprintf (dump_file, "\n[codegen] postpone cond phi nodes: ");
+ print_gimple_stmt (dump_file, new_phi, 0, 0);
+ }
+
+ new_phi_args [i] = NULL_TREE;
+ continue;
+ }
+ else
+ gcc_unreachable ();
+ }
+
+ return add_phi_arg_for_new_expr (old_phi_args, new_phi_args,
+ old_bb_dominating_edge,
+ old_bb_non_dominating_edge,
+ phi, new_phi, new_bb, region);
+}
+
+/* Copy cond phi nodes from BB to NEW_BB. */
+
+static bool
+copy_cond_phi_nodes (basic_block bb, basic_block new_bb, vec<tree> iv_map,
+ sese_info_p region)
+{
+
+ gcc_assert (!bb_contains_loop_close_phi_nodes (bb));
+
+ if (dump_file)
+ fprintf (dump_file, "\n[codegen] copying cond phi nodes in bb_%d:",
+ new_bb->index);
+
+ /* Cond phi nodes should have exactly two arguments. */
+ gcc_assert (2 == EDGE_COUNT (bb->preds));
+
+ for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
+ gsi_next (&psi))
+ {
+ gphi *phi = psi.phi ();
+ tree res = gimple_phi_result (phi);
+ if (virtual_operand_p (res))
+ continue;
+ if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
+ /* Cond phi nodes should not be scev_analyzable_p. */
+ gcc_unreachable ();
+
+ gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb);
+ tree new_res = create_new_def_for (res, new_phi,
+ gimple_phi_result_ptr (new_phi));
+ set_rename (res, new_res, region);
+
+ if (!copy_cond_phi_args (phi, new_phi, iv_map, region, true))
+ return false;
+
+ update_stmt (new_phi);
+ }
+
+ return true;
+}
+
+/* Return true if STMT should be copied from region to the
+ new code-generated region. LABELs, CONDITIONS, induction-variables
+ and region parameters need not be copied. */
+
+static bool
+should_copy_to_new_region (gimple *stmt, sese_info_p region)
+{
+ /* Do not copy labels or conditions. */
+ if (gimple_code (stmt) == GIMPLE_LABEL
+ || gimple_code (stmt) == GIMPLE_COND)
+ return false;
+
+ tree lhs;
+ /* Do not copy induction variables. */
+ if (is_gimple_assign (stmt)
+ && (lhs = gimple_assign_lhs (stmt))
+ && TREE_CODE (lhs) == SSA_NAME
+ && is_gimple_reg (lhs)
+ && scev_analyzable_p (lhs, region->region))
+ return false;
+
+ return true;
+}
+
+/* Create new names for all the definitions created by COPY and
+ add replacement mappings for each new name. */
+
+static void
+set_rename_for_each_def (gimple *stmt, sese_info_p region)
+{
+ def_operand_p def_p;
+ ssa_op_iter op_iter;
+ FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS)
+ {
+ tree old_name = DEF_FROM_PTR (def_p);
+ tree new_name = create_new_def_for (old_name, stmt, def_p);
+ set_rename (old_name, new_name, region);
+ }
+}
+
+/* Duplicates the statements of basic block BB into basic block NEW_BB
+ and compute the new induction variables according to the IV_MAP.
+ GLOOG_ERROR is set when the code generation cannot continue. */
+static bool
+graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
+ vec<tree> iv_map, sese_info_p region,
+ bool *gloog_error)
+{
+ /* Iterator poining to the place where new statement (s) will be inserted. */
+ gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
+
+ for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
+ gsi_next (&gsi))
+ {
+ gimple *stmt = gsi_stmt (gsi);
+ if (!should_copy_to_new_region (stmt, region))
+ continue;
+
+ /* Create a new copy of STMT and duplicate STMT's virtual
+ operands. */
+ gimple *copy = gimple_copy (stmt);
+ gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
+
+ if (dump_file)
+ {
+ fprintf (dump_file, "\n[codegen] inserting statement: ");
+ print_gimple_stmt (dump_file, copy, 0, 0);
+ }
+
+ maybe_duplicate_eh_stmt (copy, stmt);
+ gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
+
+ /* Crete new names for each def in the copied stmt. */
+ set_rename_for_each_def (copy, region);
+
+ loop_p loop = bb->loop_father;
+ if (rename_uses (copy, &gsi_tgt, bb, region, loop, iv_map, gloog_error))
+ {
+ fold_stmt_inplace (&gsi_tgt);
+ gcc_assert (gsi_stmt (gsi_tgt) == copy);
+ }
+
+ if (*gloog_error)
+ return false;
+
+ update_stmt (copy);
+ }
+
+ return true;
+}
+
+/* Copies BB and includes in the copied BB all the statements that can
+ be reached following the use-def chains from the memory accesses,
+ and returns the next edge following this new block. GLOOG_ERROR is
+ set when the code generation cannot continue. */
+
+edge
+copy_bb_and_scalar_dependences (basic_block bb, sese_info_p region,
+ edge next_e, vec<tree> iv_map,
+ bool *codegen_err)
+{
+ int num_phis = number_of_phi_nodes (bb);
+
+ if (region->copied_bb_map->get (bb))
+ {
+ /* FIXME: We do not handle inner loop unrolling when the inner loop has
+ phi-nodes. In that case inner loop will be copied multiple times
+ outside the region. */
+ if (num_phis)
+ {
+ *codegen_err = true;
+ return NULL;
+ }
+ }
+
+ basic_block new_bb = split_edge (next_e);
+ if (num_phis > 0 && bb_contains_loop_phi_nodes (bb))
+ {
+ basic_block phi_bb = next_e->dest->loop_father->header;
+
+ /* At this point we are unable to codegenerate by still preserving the SSA
+ structure because maybe the loop is completely unrolled and the PHIs
+ and cross-bb scalar dependencies are untrackable w.r.t. the original
+ code. See gfortran.dg/graphite/pr29832.f90. */
+ if (EDGE_COUNT (bb->preds) != EDGE_COUNT (phi_bb->preds))
+ {
+ *codegen_err = true;
+ return NULL;
+ }
+
+ if (dump_file)
+ fprintf (dump_file, "\n[codegen] bb_%d contains loop phi nodes",
+ bb->index);
+ if (!copy_loop_phi_nodes (bb, phi_bb, region))
+ {
+ *codegen_err = true;
+ return NULL;
+ }
+ }
+ else if (bb_contains_loop_close_phi_nodes (bb))
+ {
+ if (dump_file)
+ fprintf (dump_file, "\n[codegen] bb_%d contains close phi nodes",
+ bb->index);
+
+ /* Make sure that NEW_BB is the loop->exit->dest. */
+ edge e = single_pred_edge (new_bb);
+ basic_block phi_bb = new_bb;
+ if (e->src->loop_father == e->dest->loop_father)
+ {
+ /* This is one of the places which shows preserving original structure
+ is not always possible, as we may need to insert close PHI for a
+ loop where the latch does not have any mapping, or the mapping is
+ ambiguous. */
+ basic_block old_loop_bb = single_pred_edge (bb)->src;
+ vec <basic_block> *bbs = region->copied_bb_map->get (old_loop_bb);
+ if (!bbs || bbs->length () != 1)
+ {
+ *codegen_err = true;
+ return NULL;
+ }
+
+ basic_block new_loop_bb = (*bbs)[0];
+ loop_p new_loop = new_loop_bb->loop_father;
+ phi_bb = single_exit (new_loop)->dest;
+ e = single_pred_edge (phi_bb);
+ }
+
+ gcc_assert (e->src->loop_father != e->dest->loop_father);
+
+ if (!copy_loop_close_phi_nodes (bb, phi_bb, region))
+ {
+ *codegen_err = true;
+ return NULL;
+ }
+ }
+ else if (num_phis > 0)
+ {
+ if (dump_file)
+ fprintf (dump_file, "\n[codegen] bb_%d contains cond phi nodes",
+ bb->index);
+
+ basic_block phi_bb = single_pred (new_bb);
+ loop_p loop_father = new_bb->loop_father;
+
+ /* Move back until we find the block with two predecessors. */
+ while (single_pred_p (phi_bb))
+ phi_bb = single_pred_edge (phi_bb)->src;
+
+ /* If a corresponding merge-point was not found, then abort codegen. */
+ if (phi_bb->loop_father != loop_father
+ || !copy_cond_phi_nodes (bb, phi_bb, iv_map, region))
+ {
+ *codegen_err = true;
+ return NULL;
+ }
+ }
+
+ if (dump_file)
+ fprintf (dump_file, "\n[codegen] copying from bb_%d to bb_%d",
+ bb->index, new_bb->index);
+
+ vec <basic_block> *copied_bbs = region->copied_bb_map->get (bb);
+ if (copied_bbs)
+ copied_bbs->safe_push (new_bb);
+ else
+ {
+ vec<basic_block> bbs;
+ bbs.create (2);
+ bbs.safe_push (new_bb);
+ region->copied_bb_map->put (bb, bbs);
+ }
+
+ if (!graphite_copy_stmts_from_block (bb, new_bb, iv_map, region, codegen_err))
+ {
+ *codegen_err = true;
+ return NULL;
+ }
+
+ return single_succ_edge (new_bb);
+}
+
+/* Returns the outermost loop in SCOP that contains BB. */