static basic_block find_block_to_duplicate_for_splitting_paths (basic_block latch) { /* We should have simple latches at this point. So the latch should have a single successor. This implies the predecessor of the latch likely has the loop exit. And it's that predecessor we're most interested in. To keep things simple, we're going to require that the latch have a single predecessor too. */ if (single_succ_p (latch) && single_pred_p (latch)) { basic_block bb = get_immediate_dominator (CDI_DOMINATORS, latch); gcc_assert (single_pred_edge (latch)->src == bb); /* If BB has been marked as not to be duplicated, then honor that request. */ if (ignore_bb_p (bb)) return NULL; gimple *last = gsi_stmt (gsi_last_nondebug_bb (bb)); /* The immediate dominator of the latch must end in a conditional. */ if (!last || gimple_code (last) != GIMPLE_COND) return NULL; /* We're hoping that BB is a join point for an IF-THEN-ELSE diamond region. Verify that it is. First, verify that BB has two predecessors (each arm of the IF-THEN-ELSE) and two successors (the latch and exit). */ if (EDGE_COUNT (bb->preds) == 2 && EDGE_COUNT (bb->succs) == 2) { /* Now verify that BB's immediate dominator ends in a conditional as well. */ basic_block bb_idom = get_immediate_dominator (CDI_DOMINATORS, bb); gimple *last = gsi_stmt (gsi_last_nondebug_bb (bb_idom)); if (!last || gimple_code (last) != GIMPLE_COND) return NULL; /* And that BB's immediate dominator's successors are the predecessors of BB. */ if (!find_edge (bb_idom, EDGE_PRED (bb, 0)->src) || !find_edge (bb_idom, EDGE_PRED (bb, 1)->src)) return NULL; /* And that the predecessors of BB each have a single successor. */ if (!single_succ_p (EDGE_PRED (bb, 0)->src) || !single_succ_p (EDGE_PRED (bb, 1)->src)) return NULL; /* So at this point we have a simple diamond for an IF-THEN-ELSE construct starting at BB_IDOM, with a join point at BB. BB pass control outside the loop or to the loop latch. We're going to want to create two duplicates of BB, one for each successor of BB_IDOM. */ return bb; } } return NULL; }
static void naive_outof_ssa (void) { basic_block bb; hsa_cfun->m_in_ssa = false; FOR_ALL_BB_FN (bb, cfun) { hsa_bb *hbb = hsa_bb_for_bb (bb); hsa_insn_phi *phi; /* naive_process_phi can call split_edge on an incoming edge which order if the incoming edges to the basic block and thus make it inconsistent with the ordering of PHI arguments, so we collect them in advance. */ auto_vec<edge, 8> predecessors; unsigned pred_count = EDGE_COUNT (bb->preds); for (unsigned i = 0; i < pred_count; i++) predecessors.safe_push (EDGE_PRED (bb, i)); for (phi = hbb->m_first_phi; phi; phi = phi->m_next ? as_a <hsa_insn_phi *> (phi->m_next) : NULL) naive_process_phi (phi, predecessors); /* Zap PHI nodes, they will be deallocated when everything else will. */ hbb->m_first_phi = NULL; hbb->m_last_phi = NULL; }
void backprop::optimize_phi (gphi *phi, tree var, const usage_info *info) { /* If the sign of the result doesn't matter, try to strip sign operations from arguments. */ if (info->flags.ignore_sign) { basic_block bb = gimple_bb (phi); use_operand_p use; ssa_op_iter oi; bool replaced = false; FOR_EACH_PHI_ARG (use, phi, oi, SSA_OP_USE) { /* Propagating along abnormal edges is delicate, punt for now. */ const int index = PHI_ARG_INDEX_FROM_USE (use); if (EDGE_PRED (bb, index)->flags & EDGE_ABNORMAL) continue; tree new_arg = strip_sign_op (USE_FROM_PTR (use)); if (new_arg) { if (!replaced) prepare_change (var); if (dump_file && (dump_flags & TDF_DETAILS)) note_replacement (phi, USE_FROM_PTR (use), new_arg); replace_exp (use, new_arg); replaced = true; } } }
static gphi * input_phi (struct lto_input_block *ib, basic_block bb, struct data_in *data_in, struct function *fn) { unsigned HOST_WIDE_INT ix; tree phi_result; int i, len; gphi *result; ix = streamer_read_uhwi (ib); phi_result = (*SSANAMES (fn))[ix]; len = EDGE_COUNT (bb->preds); result = create_phi_node (phi_result, bb); /* We have to go through a lookup process here because the preds in the reconstructed graph are generally in a different order than they were in the original program. */ for (i = 0; i < len; i++) { tree def = stream_read_tree (ib, data_in); int src_index = streamer_read_uhwi (ib); bitpack_d bp = streamer_read_bitpack (ib); /* Do not cache a location - we do not have API to get pointer to the location in PHI statement and we may trigger reallocation. */ location_t arg_loc = stream_input_location_now (&bp, data_in); basic_block sbb = BASIC_BLOCK_FOR_FN (fn, src_index); edge e = NULL; int j; for (j = 0; j < len; j++) if (EDGE_PRED (bb, j)->src == sbb) { e = EDGE_PRED (bb, j); break; } add_phi_arg (result, def, e, arg_loc); } return result; }
static gimple input_phi (struct lto_input_block *ib, basic_block bb, struct data_in *data_in, struct function *fn) { unsigned HOST_WIDE_INT ix; tree phi_result; int i, len; gimple result; ix = streamer_read_uhwi (ib); phi_result = VEC_index (tree, SSANAMES (fn), ix); len = EDGE_COUNT (bb->preds); result = create_phi_node (phi_result, bb); SSA_NAME_DEF_STMT (phi_result) = result; /* We have to go through a lookup process here because the preds in the reconstructed graph are generally in a different order than they were in the original program. */ for (i = 0; i < len; i++) { tree def = stream_read_tree (ib, data_in); int src_index = streamer_read_uhwi (ib); location_t arg_loc = lto_input_location (ib, data_in); basic_block sbb = BASIC_BLOCK_FOR_FUNCTION (fn, src_index); edge e = NULL; int j; for (j = 0; j < len; j++) if (EDGE_PRED (bb, j)->src == sbb) { e = EDGE_PRED (bb, j); break; } add_phi_arg (result, def, e, arg_loc); } return result; }
static inline void disconnect_dest (edge e) { basic_block dest = e->dest; unsigned int dest_idx = e->dest_idx; VEC_unordered_remove (edge, dest->preds, dest_idx); /* If we removed an edge in the middle of the edge vector, we need to update dest_idx of the edge that moved into the "hole". */ if (dest_idx < EDGE_COUNT (dest->preds)) EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx; df_mark_solutions_dirty (); }
static bool candidate_bb_for_phi_optimization (basic_block bb, basic_block *cond_block_p, basic_block *other_block_p) { tree last0, last1; basic_block cond_block, other_block; /* One of the alternatives must come from a block ending with a COND_EXPR. */ last0 = last_stmt (EDGE_PRED (bb, 0)->src); last1 = last_stmt (EDGE_PRED (bb, 1)->src); if (last0 && TREE_CODE (last0) == COND_EXPR) { cond_block = EDGE_PRED (bb, 0)->src; other_block = EDGE_PRED (bb, 1)->src; } else if (last1 && TREE_CODE (last1) == COND_EXPR) { other_block = EDGE_PRED (bb, 0)->src; cond_block = EDGE_PRED (bb, 1)->src; } else return false; /* COND_BLOCK must have precisely two successors. We indirectly verify that those successors are BB and OTHER_BLOCK. */ if (EDGE_COUNT (cond_block->succs) != 2 || (EDGE_SUCC (cond_block, 0)->flags & EDGE_ABNORMAL) != 0 || (EDGE_SUCC (cond_block, 1)->flags & EDGE_ABNORMAL) != 0) return false; /* OTHER_BLOCK must have a single predecessor which is COND_BLOCK, OTHER_BLOCK must have a single successor which is BB and OTHER_BLOCK must have no PHI nodes. */ if (EDGE_COUNT (other_block->preds) != 1 || EDGE_PRED (other_block, 0)->src != cond_block || EDGE_COUNT (other_block->succs) != 1 || EDGE_SUCC (other_block, 0)->dest != bb || phi_nodes (other_block)) return false; *cond_block_p = cond_block; *other_block_p = other_block; /* Everything looks OK. */ return true; }
static bool split_paths () { bool changed = false; loop_p loop; loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS); initialize_original_copy_tables (); calculate_dominance_info (CDI_DOMINATORS); FOR_EACH_LOOP (loop, LI_FROM_INNERMOST) { /* Only split paths if we are optimizing this loop for speed. */ if (!optimize_loop_for_speed_p (loop)) continue; /* See if there is a block that we can duplicate to split the path to the loop latch. */ basic_block bb = find_block_to_duplicate_for_splitting_paths (loop->latch); /* BB is the merge point for an IF-THEN-ELSE we want to transform. Essentially we want to create a duplicate of bb and redirect the first predecessor of BB to the duplicate (leaving the second predecessor as is. This will split the path leading to the latch re-using BB to avoid useless copying. */ if (bb && is_feasible_trace (bb)) { if (dump_file && (dump_flags & TDF_DETAILS)) fprintf (dump_file, "Duplicating join block %d into predecessor paths\n", bb->index); basic_block pred0 = EDGE_PRED (bb, 0)->src; transform_duplicate (pred0, bb); changed = true; } } loop_optimizer_finalize (); free_original_copy_tables (); return changed; }
static bool verify_phi_args (tree phi, basic_block bb, basic_block *definition_block) { edge e; bool err = false; unsigned i, phi_num_args = PHI_NUM_ARGS (phi); if (EDGE_COUNT (bb->preds) != phi_num_args) { error ("incoming edge count does not match number of PHI arguments"); err = true; goto error; } for (i = 0; i < phi_num_args; i++) { use_operand_p op_p = PHI_ARG_DEF_PTR (phi, i); tree op = USE_FROM_PTR (op_p); e = EDGE_PRED (bb, i); if (op == NULL_TREE) { error ("PHI argument is missing for edge %d->%d", e->src->index, e->dest->index); err = true; goto error; } if (TREE_CODE (op) != SSA_NAME && !is_gimple_min_invariant (op)) { error ("PHI argument is not SSA_NAME, or invariant"); err = true; } if (TREE_CODE (op) == SSA_NAME) err = verify_use (e->src, definition_block[SSA_NAME_VERSION (op)], op_p, phi, e->flags & EDGE_ABNORMAL, !is_gimple_reg (PHI_RESULT (phi)), NULL); if (e->dest != bb) { error ("wrong edge %d->%d for PHI argument", e->src->index, e->dest->index); err = true; } if (err) { fprintf (stderr, "PHI argument\n"); print_generic_stmt (stderr, op, TDF_VOPS); goto error; } } error: if (err) { fprintf (stderr, "for PHI node\n"); print_generic_stmt (stderr, phi, TDF_VOPS); } return err; }
static bool split_paths () { bool changed = false; loop_p loop; loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS); initialize_original_copy_tables (); calculate_dominance_info (CDI_DOMINATORS); FOR_EACH_LOOP (loop, LI_FROM_INNERMOST) { /* Only split paths if we are optimizing this loop for speed. */ if (!optimize_loop_for_speed_p (loop)) continue; /* See if there is a block that we can duplicate to split the path to the loop latch. */ basic_block bb = find_block_to_duplicate_for_splitting_paths (loop->latch); /* BB is the merge point for an IF-THEN-ELSE we want to transform. Essentially we want to create a duplicate of bb and redirect the first predecessor of BB to the duplicate (leaving the second predecessor as is. This will split the path leading to the latch re-using BB to avoid useless copying. */ if (bb && is_feasible_trace (bb)) { if (dump_file && (dump_flags & TDF_DETAILS)) fprintf (dump_file, "Duplicating join block %d into predecessor paths\n", bb->index); basic_block pred0 = EDGE_PRED (bb, 0)->src; transform_duplicate (pred0, bb); changed = true; /* If BB has an outgoing edge marked as IRREDUCIBLE, then duplicating BB may result in an irreducible region turning into a natural loop. Long term we might want to hook this into the block duplication code, but as we've seen with similar changes for edge removal, that can be somewhat risky. */ if (EDGE_SUCC (bb, 0)->flags & EDGE_IRREDUCIBLE_LOOP || EDGE_SUCC (bb, 1)->flags & EDGE_IRREDUCIBLE_LOOP) { if (dump_file && (dump_flags & TDF_DETAILS)) fprintf (dump_file, "Join block %d has EDGE_IRREDUCIBLE_LOOP set. " "Scheduling loop fixups.\n", bb->index); loops_state_set (LOOPS_NEED_FIXUP); } } } loop_optimizer_finalize (); free_original_copy_tables (); return changed; }
static bool is_feasible_trace (basic_block bb) { basic_block pred1 = EDGE_PRED (bb, 0)->src; basic_block pred2 = EDGE_PRED (bb, 1)->src; int num_stmts_in_join = count_stmts_in_block (bb); int num_stmts_in_pred1 = count_stmts_in_block (pred1); int num_stmts_in_pred2 = count_stmts_in_block (pred2); /* This is meant to catch cases that are likely opportunities for if-conversion. Essentially we look for the case where BB's predecessors are both single statement blocks where the output of that statement feed the same PHI in BB. */ if (num_stmts_in_pred1 == 1 && num_stmts_in_pred2 == 1) { gimple *stmt1 = last_and_only_stmt (pred1); gimple *stmt2 = last_and_only_stmt (pred2); if (stmt1 && stmt2 && gimple_code (stmt1) == GIMPLE_ASSIGN && gimple_code (stmt2) == GIMPLE_ASSIGN) { enum tree_code code1 = gimple_assign_rhs_code (stmt1); enum tree_code code2 = gimple_assign_rhs_code (stmt2); if (!poor_ifcvt_candidate_code (code1) && !poor_ifcvt_candidate_code (code2)) { tree lhs1 = gimple_assign_lhs (stmt1); tree lhs2 = gimple_assign_lhs (stmt2); gimple_stmt_iterator gsi; for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) { gimple *phi = gsi_stmt (gsi); if ((gimple_phi_arg_def (phi, 0) == lhs1 && gimple_phi_arg_def (phi, 1) == lhs2) || (gimple_phi_arg_def (phi, 1) == lhs1 && gimple_phi_arg_def (phi, 0) == lhs2)) { if (dump_file && (dump_flags & TDF_DETAILS)) fprintf (dump_file, "Block %d appears to be a join point for " "if-convertable diamond.\n", bb->index); return false; } } } } } /* We may want something here which looks at dataflow and tries to guess if duplication of BB is likely to result in simplification of instructions in BB in either the original or the duplicate. */ /* Upper Hard limit on the number statements to copy. */ if (num_stmts_in_join >= PARAM_VALUE (PARAM_MAX_JUMP_THREAD_DUPLICATION_STMTS)) return false; return true; }