+/* A helper of dse_optimize_stmt.
+ Given a GIMPLE_ASSIGN in STMT, find a candidate statement *USE_STMT that
+ may prove STMT to be dead.
+ Return TRUE if the above conditions are met, otherwise FALSE. */
+
+static bool
+dse_possible_dead_store_p (gimple stmt, gimple *use_stmt)
+{
+ gimple temp;
+ unsigned cnt = 0;
+
+ *use_stmt = NULL;
+
+ /* Find the first dominated statement that clobbers (part of) the
+ memory stmt stores to with no intermediate statement that may use
+ part of the memory stmt stores. That is, find a store that may
+ prove stmt to be a dead store. */
+ temp = stmt;
+ do
+ {
+ gimple use_stmt;
+ imm_use_iterator ui;
+ bool fail = false;
+ tree defvar;
+
+ /* Limit stmt walking to be linear in the number of possibly
+ dead stores. */
+ if (++cnt > 256)
+ return false;
+
+ if (gimple_code (temp) == GIMPLE_PHI)
+ defvar = PHI_RESULT (temp);
+ else
+ defvar = gimple_vdef (temp);
+ temp = NULL;
+ FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
+ {
+ cnt++;
+
+ /* If we ever reach our DSE candidate stmt again fail. We
+ cannot handle dead stores in loops. */
+ if (use_stmt == stmt)
+ {
+ fail = true;
+ BREAK_FROM_IMM_USE_STMT (ui);
+ }
+ /* In simple cases we can look through PHI nodes, but we
+ have to be careful with loops and with memory references
+ containing operands that are also operands of PHI nodes.
+ See gcc.c-torture/execute/20051110-*.c. */
+ else if (gimple_code (use_stmt) == GIMPLE_PHI)
+ {
+ if (temp
+ /* Make sure we are not in a loop latch block. */
+ || gimple_bb (stmt) == gimple_bb (use_stmt)
+ || dominated_by_p (CDI_DOMINATORS,
+ gimple_bb (stmt), gimple_bb (use_stmt))
+ /* We can look through PHIs to regions post-dominating
+ the DSE candidate stmt. */
+ || !dominated_by_p (CDI_POST_DOMINATORS,
+ gimple_bb (stmt), gimple_bb (use_stmt)))
+ {
+ fail = true;
+ BREAK_FROM_IMM_USE_STMT (ui);
+ }
+ temp = use_stmt;
+ }
+ /* If the statement is a use the store is not dead. */
+ else if (ref_maybe_used_by_stmt_p (use_stmt,
+ gimple_assign_lhs (stmt)))
+ {
+ fail = true;
+ BREAK_FROM_IMM_USE_STMT (ui);
+ }
+ /* If this is a store, remember it or bail out if we have
+ multiple ones (the will be in different CFG parts then). */
+ else if (gimple_vdef (use_stmt))
+ {
+ if (temp)
+ {
+ fail = true;
+ BREAK_FROM_IMM_USE_STMT (ui);
+ }
+ temp = use_stmt;
+ }
+ }
+
+ if (fail)
+ return false;
+
+ /* If we didn't find any definition this means the store is dead
+ if it isn't a store to global reachable memory. In this case
+ just pretend the stmt makes itself dead. Otherwise fail. */
+ if (!temp)
+ {
+ if (is_hidden_global_store (stmt))
+ return false;
+
+ temp = stmt;
+ break;
+ }
+ }
+ /* We deliberately stop on clobbering statements and not only on
+ killing ones to make walking cheaper. Otherwise we can just
+ continue walking until both stores have equal reference trees. */
+ while (!stmt_may_clobber_ref_p (temp, gimple_assign_lhs (stmt)));
+
+ if (!is_gimple_assign (temp))
+ return false;
+
+ *use_stmt = temp;
+
+ return true;
+}
+
+