static void output_phi (struct output_block *ob, gimple phi) { unsigned i, len = gimple_phi_num_args (phi); streamer_write_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI)); streamer_write_uhwi (ob, SSA_NAME_VERSION (PHI_RESULT (phi))); for (i = 0; i < len; i++) { stream_write_tree (ob, gimple_phi_arg_def (phi, i), true); streamer_write_uhwi (ob, gimple_phi_arg_edge (phi, i)->src->index); bitpack_d bp = bitpack_create (ob->main_stream); stream_output_location (ob, &bp, gimple_phi_arg_location (phi, i)); streamer_write_bitpack (&bp); } }
static bool check_final_bb (void) { gimple_stmt_iterator gsi; info.phi_count = 0; for (gsi = gsi_start_phis (info.final_bb); !gsi_end_p (gsi); gsi_next (&gsi)) { gimple phi = gsi_stmt (gsi); unsigned int i; info.phi_count++; for (i = 0; i < gimple_phi_num_args (phi); i++) { basic_block bb = gimple_phi_arg_edge (phi, i)->src; if (bb == info.switch_bb || (single_pred_p (bb) && single_pred (bb) == info.switch_bb)) { tree reloc, val; val = gimple_phi_arg_def (phi, i); if (!is_gimple_ip_invariant (val)) { info.reason = " Non-invariant value from a case\n"; return false; /* Non-invariant argument. */ } reloc = initializer_constant_valid_p (val, TREE_TYPE (val)); if ((flag_pic && reloc != null_pointer_node) || (!flag_pic && reloc == NULL_TREE)) { if (reloc) info.reason = " Value from a case would need runtime relocations\n"; else info.reason = " Value from a case is not a valid initializer\n"; return false; } } } } return true; }
static basic_block nearest_common_dominator_of_uses (def_operand_p def_p, bool *debug_stmts) { tree var = DEF_FROM_PTR (def_p); bitmap blocks = BITMAP_ALLOC (NULL); basic_block commondom; unsigned int j; bitmap_iterator bi; imm_use_iterator imm_iter; use_operand_p use_p; FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var) { gimple *usestmt = USE_STMT (use_p); basic_block useblock; if (gphi *phi = dyn_cast <gphi *> (usestmt)) { int idx = PHI_ARG_INDEX_FROM_USE (use_p); useblock = gimple_phi_arg_edge (phi, idx)->src; } else if (is_gimple_debug (usestmt)) { *debug_stmts = true; continue; } else { useblock = gimple_bb (usestmt); } /* Short circuit. Nothing dominates the entry block. */ if (useblock == ENTRY_BLOCK_PTR_FOR_FN (cfun)) { BITMAP_FREE (blocks); return NULL; } bitmap_set_bit (blocks, useblock->index); }
static enum ssa_prop_result copy_prop_visit_phi_node (gimple phi) { enum ssa_prop_result retval; unsigned i; prop_value_t phi_val = { NULL_TREE }; tree lhs = gimple_phi_result (phi); if (dump_file && (dump_flags & TDF_DETAILS)) { fprintf (dump_file, "\nVisiting PHI node: "); print_gimple_stmt (dump_file, phi, 0, dump_flags); } for (i = 0; i < gimple_phi_num_args (phi); i++) { prop_value_t *arg_val; tree arg_value; tree arg = gimple_phi_arg_def (phi, i); edge e = gimple_phi_arg_edge (phi, i); /* We don't care about values flowing through non-executable edges. */ if (!(e->flags & EDGE_EXECUTABLE)) continue; /* Names that flow through abnormal edges cannot be used to derive copies. */ if (TREE_CODE (arg) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg)) { phi_val.value = lhs; break; } if (dump_file && (dump_flags & TDF_DETAILS)) { fprintf (dump_file, "\tArgument #%d: ", i); dump_copy_of (dump_file, arg); fprintf (dump_file, "\n"); } if (TREE_CODE (arg) == SSA_NAME) { arg_val = get_copy_of_val (arg); /* If we didn't visit the definition of arg yet treat it as UNDEFINED. This also handles PHI arguments that are the same as lhs. We'll come here again. */ if (!arg_val->value) continue; arg_value = arg_val->value; } else arg_value = valueize_val (arg); /* Avoid copy propagation from an inner into an outer loop. Otherwise, this may move loop variant variables outside of their loops and prevent coalescing opportunities. If the value was loop invariant, it will be hoisted by LICM and exposed for copy propagation. ??? The value will be always loop invariant. In loop-closed SSA form do not copy-propagate through PHI nodes in blocks with a loop exit edge predecessor. */ if (current_loops && TREE_CODE (arg_value) == SSA_NAME && (loop_depth_of_name (arg_value) > loop_depth_of_name (lhs) || (loops_state_satisfies_p (LOOP_CLOSED_SSA) && loop_exit_edge_p (e->src->loop_father, e)))) { phi_val.value = lhs; break; } /* If the LHS didn't have a value yet, make it a copy of the first argument we find. */ if (phi_val.value == NULL_TREE) { phi_val.value = arg_value; continue; } /* If PHI_VAL and ARG don't have a common copy-of chain, then this PHI node cannot be a copy operation. */ if (phi_val.value != arg_value && !operand_equal_p (phi_val.value, arg_value, 0)) { phi_val.value = lhs; break; } } if (phi_val.value && may_propagate_copy (lhs, phi_val.value) && set_copy_of_val (lhs, phi_val.value)) retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING; else retval = SSA_PROP_NOT_INTERESTING; if (dump_file && (dump_flags & TDF_DETAILS)) { fprintf (dump_file, "PHI node "); dump_copy_of (dump_file, lhs); fprintf (dump_file, "\nTelling the propagator to "); if (retval == SSA_PROP_INTERESTING) fprintf (dump_file, "add SSA edges out of this PHI and continue."); else if (retval == SSA_PROP_VARYING) fprintf (dump_file, "add SSA edges out of this PHI and never visit again."); else fprintf (dump_file, "do nothing with SSA edges and keep iterating."); fprintf (dump_file, "\n\n"); } return retval; }
static enum ssa_prop_result copy_prop_visit_phi_node (gimple phi) { enum ssa_prop_result retval; unsigned i; prop_value_t phi_val = { 0, NULL_TREE }; tree lhs = gimple_phi_result (phi); if (dump_file && (dump_flags & TDF_DETAILS)) { fprintf (dump_file, "\nVisiting PHI node: "); print_gimple_stmt (dump_file, phi, 0, dump_flags); fprintf (dump_file, "\n\n"); } for (i = 0; i < gimple_phi_num_args (phi); i++) { prop_value_t *arg_val; tree arg = gimple_phi_arg_def (phi, i); edge e = gimple_phi_arg_edge (phi, i); /* We don't care about values flowing through non-executable edges. */ if (!(e->flags & EDGE_EXECUTABLE)) continue; /* Constants in the argument list never generate a useful copy. Similarly, names that flow through abnormal edges cannot be used to derive copies. */ if (TREE_CODE (arg) != SSA_NAME || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg)) { phi_val.value = lhs; break; } /* Avoid copy propagation from an inner into an outer loop. Otherwise, this may move loop variant variables outside of their loops and prevent coalescing opportunities. If the value was loop invariant, it will be hoisted by LICM and exposed for copy propagation. Not a problem for virtual operands though. */ if (is_gimple_reg (lhs) && loop_depth_of_name (arg) > loop_depth_of_name (lhs)) { phi_val.value = lhs; break; } /* If the LHS appears in the argument list, ignore it. It is irrelevant as a copy. */ if (arg == lhs || get_last_copy_of (arg) == lhs) continue; if (dump_file && (dump_flags & TDF_DETAILS)) { fprintf (dump_file, "\tArgument #%d: ", i); dump_copy_of (dump_file, arg); fprintf (dump_file, "\n"); } arg_val = get_copy_of_val (arg); /* If the LHS didn't have a value yet, make it a copy of the first argument we find. Notice that while we make the LHS be a copy of the argument itself, we take the memory reference from the argument's value so that we can compare it to the memory reference of all the other arguments. */ if (phi_val.value == NULL_TREE) { phi_val.value = arg_val->value ? arg_val->value : arg; continue; } /* If PHI_VAL and ARG don't have a common copy-of chain, then this PHI node cannot be a copy operation. Also, if we are copy propagating stores and these two arguments came from different memory references, they cannot be considered copies. */ if (get_last_copy_of (phi_val.value) != get_last_copy_of (arg)) { phi_val.value = lhs; break; } } if (phi_val.value && may_propagate_copy (lhs, phi_val.value) && set_copy_of_val (lhs, phi_val.value)) retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING; else retval = SSA_PROP_NOT_INTERESTING; if (dump_file && (dump_flags & TDF_DETAILS)) { fprintf (dump_file, "\nPHI node "); dump_copy_of (dump_file, lhs); fprintf (dump_file, "\nTelling the propagator to "); if (retval == SSA_PROP_INTERESTING) fprintf (dump_file, "add SSA edges out of this PHI and continue."); else if (retval == SSA_PROP_VARYING) fprintf (dump_file, "add SSA edges out of this PHI and never visit again."); else fprintf (dump_file, "do nothing with SSA edges and keep iterating."); fprintf (dump_file, "\n\n"); } return retval; }
/* Look for PHI nodes which feed statements in the same block where the value of the PHI node implies the statement is erroneous. For example, a NULL PHI arg value which then feeds a pointer dereference. When found isolate and optimize the path associated with the PHI argument feeding the erroneous statement. */ static void find_implicit_erroneous_behaviour (void) { basic_block bb; FOR_EACH_BB_FN (bb, cfun) { gimple_stmt_iterator si; /* Out of an abundance of caution, do not isolate paths to a block where the block has any abnormal outgoing edges. We might be able to relax this in the future. We have to detect when we have to split the block with the NULL dereference and the trap we insert. We have to preserve abnormal edges out of the isolated block which in turn means updating PHIs at the targets of those abnormal outgoing edges. */ if (has_abnormal_or_eh_outgoing_edge_p (bb)) continue; /* First look for a PHI which sets a pointer to NULL and which is then dereferenced within BB. This is somewhat overly conservative, but probably catches most of the interesting cases. */ for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si)) { gimple phi = gsi_stmt (si); tree lhs = gimple_phi_result (phi); /* If the result is not a pointer, then there is no need to examine the arguments. */ if (!POINTER_TYPE_P (TREE_TYPE (lhs))) continue; /* PHI produces a pointer result. See if any of the PHI's arguments are NULL. When we remove an edge, we want to reprocess the current index, hence the ugly way we update I for each iteration. */ basic_block duplicate = NULL; for (unsigned i = 0, next_i = 0; i < gimple_phi_num_args (phi); i = next_i) { tree op = gimple_phi_arg_def (phi, i); next_i = i + 1; if (!integer_zerop (op)) continue; edge e = gimple_phi_arg_edge (phi, i); imm_use_iterator iter; gimple use_stmt; /* We've got a NULL PHI argument. Now see if the PHI's result is dereferenced within BB. */ FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs) { /* We only care about uses in BB. Catching cases in in other blocks would require more complex path isolation code. */ if (gimple_bb (use_stmt) != bb) continue; if (infer_nonnull_range (use_stmt, lhs, flag_isolate_erroneous_paths_dereference, flag_isolate_erroneous_paths_attribute)) { duplicate = isolate_path (bb, duplicate, e, use_stmt, lhs); /* When we remove an incoming edge, we need to reprocess the Ith element. */ next_i = i; cfg_altered = true; } } } } }