cfg_changed = true;
if (TREE_CODE (TREE_OPERAND (stmt, 1)) == ADDR_EXPR)
- recompute_tree_invarant_for_addr_expr (TREE_OPERAND (stmt, 1));
+ recompute_tree_invariant_for_addr_expr (TREE_OPERAND (stmt, 1));
update_stmt (stmt);
}
return all;
}
+/* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
+ If so, we can change STMT into lhs = y which can later be copy
+ propagated. Similarly for negation.
+
+ This could trivially be formulated as a forward propagation
+ to immediate uses. However, we already had an implementation
+ from DOM which used backward propagation via the use-def links.
+
+ It turns out that backward propagation is actually faster as
+ there's less work to do for each NOT/NEG expression we find.
+ Backwards propagation needs to look at the statement in a single
+ backlink. Forward propagation needs to look at potentially more
+ than one forward link. */
+
+static void
+simplify_not_neg_expr (tree stmt)
+{
+ tree rhs = TREE_OPERAND (stmt, 1);
+ tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
+
+ /* See if the RHS_DEF_STMT has the same form as our statement. */
+ if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR
+ && TREE_CODE (TREE_OPERAND (rhs_def_stmt, 1)) == TREE_CODE (rhs))
+ {
+ tree rhs_def_operand = TREE_OPERAND (TREE_OPERAND (rhs_def_stmt, 1), 0);
+
+ /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME. */
+ if (TREE_CODE (rhs_def_operand) == SSA_NAME
+ && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
+ {
+ TREE_OPERAND (stmt, 1) = rhs_def_operand;
+ update_stmt (stmt);
+ }
+ }
+}
+
+/* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
+ the condition which we may be able to optimize better. */
+
+static void
+simplify_switch_expr (tree stmt)
+{
+ tree cond = SWITCH_COND (stmt);
+ tree def, to, ti;
+
+ /* The optimization that we really care about is removing unnecessary
+ casts. That will let us do much better in propagating the inferred
+ constant at the switch target. */
+ if (TREE_CODE (cond) == SSA_NAME)
+ {
+ def = SSA_NAME_DEF_STMT (cond);
+ if (TREE_CODE (def) == MODIFY_EXPR)
+ {
+ def = TREE_OPERAND (def, 1);
+ if (TREE_CODE (def) == NOP_EXPR)
+ {
+ int need_precision;
+ bool fail;
+
+ def = TREE_OPERAND (def, 0);
+
+#ifdef ENABLE_CHECKING
+ /* ??? Why was Jeff testing this? We are gimple... */
+ gcc_assert (is_gimple_val (def));
+#endif
+
+ to = TREE_TYPE (cond);
+ ti = TREE_TYPE (def);
+
+ /* If we have an extension that preserves value, then we
+ can copy the source value into the switch. */
+
+ need_precision = TYPE_PRECISION (ti);
+ fail = false;
+ if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
+ fail = true;
+ else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
+ need_precision += 1;
+ if (TYPE_PRECISION (to) < need_precision)
+ fail = true;
+
+ if (!fail)
+ {
+ SWITCH_COND (stmt) = def;
+ update_stmt (stmt);
+ }
+ }
+ }
+ }
+}
/* Main entry point for the forward propagation optimizer. */
/* If this statement sets an SSA_NAME to an address,
try to propagate the address into the uses of the SSA_NAME. */
- if (TREE_CODE (stmt) == MODIFY_EXPR
- && TREE_CODE (TREE_OPERAND (stmt, 1)) == ADDR_EXPR
- && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME)
+ if (TREE_CODE (stmt) == MODIFY_EXPR)
{
- if (forward_propagate_addr_expr (stmt))
- bsi_remove (&bsi);
+ tree lhs = TREE_OPERAND (stmt, 0);
+ tree rhs = TREE_OPERAND (stmt, 1);
+
+
+ if (TREE_CODE (lhs) != SSA_NAME)
+ {
+ bsi_next (&bsi);
+ continue;
+ }
+
+ if (TREE_CODE (rhs) == ADDR_EXPR)
+ {
+ if (forward_propagate_addr_expr (stmt))
+ bsi_remove (&bsi);
+ else
+ bsi_next (&bsi);
+ }
+ else if ((TREE_CODE (rhs) == BIT_NOT_EXPR
+ || TREE_CODE (rhs) == NEGATE_EXPR)
+ && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
+ {
+ simplify_not_neg_expr (stmt);
+ bsi_next (&bsi);
+ }
else
bsi_next (&bsi);
}
+ else if (TREE_CODE (stmt) == SWITCH_EXPR)
+ {
+ simplify_switch_expr (stmt);
+ bsi_next (&bsi);
+ }
else if (TREE_CODE (stmt) == COND_EXPR)
{
forward_propagate_into_cond (stmt);