@@ -0,0 +1,29 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -ffast-math -fdump-tree-reassoc1" } */
+
+float f1_float (float x)
+{
+ float y = x + x;
+ y = y + x;
+ y = y + x;
+ y = y + x;
+ y = y + x;
+ y = y + x;
+ y = y + x;
+ return y;
+}
+
+float f1_float2 (float x)
+{
+ float y = x + 3 * x + x;
+ return y;
+}
+
+int f1_int (int x)
+{
+ int y = x + 3 * x + x;
+ return y;
+}
+
+/* { dg-final { scan-tree-dump-times "\\\*" 4 "reassoc1" } } */
+
@@ -0,0 +1,59 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-reassoc1" } */
+
+unsigned f1 (unsigned x)
+{
+ unsigned y = x + x;
+ y = y + x;
+ y = y + x;
+ y = y + x;
+ y = y + x;
+ y = y + x;
+ y = y + x;
+ return y;
+}
+
+unsigned f2 (unsigned x, unsigned z)
+{
+ unsigned y = x + x;
+ y = y + x;
+ y = y + x;
+ y = y + z;
+ y = y + z;
+ y = y + z;
+ y = y + z;
+ return y;
+}
+
+unsigned f3 (unsigned x, unsigned z, unsigned k)
+{
+ unsigned y = x + x;
+ y = y + x;
+ y = y + x;
+ y = y + z;
+ y = y + z;
+ y = y + z;
+ y = y + k;
+ return y;
+}
+
+unsigned f4 (unsigned x, unsigned z, unsigned k)
+{
+ unsigned y = k + x;
+ y = y + x;
+ y = y + x;
+ y = y + z;
+ y = y + z;
+ y = y + z;
+ y = y + x;
+ return y;
+}
+
+unsigned f5 (unsigned x, unsigned y, unsigned z)
+{
+ return x + x + x + x + y + y + y + y + y \
+ + y + z + z + z + z;
+}
+
+/* { dg-final { scan-tree-dump-times "\\\*" 10 "reassoc1" } } */
+
@@ -19,6 +19,7 @@ unsigned int test2 (unsigned int x, unsigned int y, unsigned int z,
return tmp1 + tmp2 + tmp3;
}
-/* There should be one multiplication left in test1 and three in test2. */
+/* There should be two multiplication left in test1 (inculding one generated
+ when converting addition to multiplication) and three in test2. */
-/* { dg-final { scan-tree-dump-times "\\\*" 4 "reassoc1" } } */
+/* { dg-final { scan-tree-dump-times "\\\*" 5 "reassoc1" } } */
@@ -1756,6 +1756,95 @@ eliminate_redundant_comparison (enum tree_code opcode,
return false;
}
+/* Transform repeated addition of same values into multiply with
+ constant. */
+static bool
+transform_add_to_multiply (vec<operand_entry *> *ops)
+{
+ operand_entry *oe;
+ tree op = NULL_TREE;
+ int j;
+ int i, start = -1, end = 0, count = 0;
+ vec<std::pair <int, int> > indxs = vNULL;
+ bool changed = false;
+ gimple *stmt;
+
+ if (!INTEGRAL_TYPE_P (TREE_TYPE ((*ops)[0]->op))
+ && !flag_unsafe_math_optimizations)
+ return false;
+
+ /* Look for repeated operands. */
+ FOR_EACH_VEC_ELT (*ops, i, oe)
+ {
+ if (start == -1)
+ {
+ count = 1;
+ op = oe->op;
+ start = i;
+ }
+ else if (operand_equal_p (oe->op, op, 0))
+ {
+ count++;
+ end = i;
+ }
+ else
+ {
+ if (count > 1)
+ indxs.safe_push (std::make_pair (start, end));
+ count = 1;
+ op = oe->op;
+ start = i;
+ }
+ }
+
+ if (count > 1)
+ indxs.safe_push (std::make_pair (start, end));
+
+ for (j = indxs.length () - 1; j >= 0; --j)
+ {
+ /* Convert repeated operand addition to multiplication. */
+ start = indxs[j].first;
+ end = indxs[j].second;
+ op = (*ops)[start]->op;
+ count = end - start + 1;
+ for (i = end; i >= start; --i)
+ ops->unordered_remove (i);
+ tree tmp = make_ssa_name (TREE_TYPE (op));
+ tree cst = build_int_cst (integer_type_node, count);
+ gimple *def_stmt = SSA_NAME_DEF_STMT (op);
+ gassign *mul_stmt
+ = gimple_build_assign (tmp, MULT_EXPR,
+ op, fold_convert (TREE_TYPE (op), cst));
+ gimple_stmt_iterator gsi;
+ if (SSA_NAME_VAR (op) != NULL
+ && gimple_code (def_stmt) == GIMPLE_NOP)
+ {
+ gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
+ stmt = gsi_stmt (gsi);
+ gsi_insert_before (&gsi, mul_stmt, GSI_NEW_STMT);
+ }
+ else if (gimple_code (def_stmt) == GIMPLE_PHI)
+ {
+ gsi = gsi_after_labels (gimple_bb (def_stmt));
+ stmt = gsi_stmt (gsi);
+ gsi_insert_before (&gsi, mul_stmt, GSI_NEW_STMT);
+ }
+ else
+ {
+ gsi = gsi_for_stmt (def_stmt);
+ stmt = gsi_stmt (gsi);
+ gsi_insert_after (&gsi, mul_stmt, GSI_NEW_STMT);
+ }
+ gimple_set_uid (mul_stmt, gimple_uid (stmt));
+ gimple_set_visited (mul_stmt, true);
+ add_to_ops_vec (ops, tmp);
+ changed = true;
+ }
+
+ return changed;
+}
+
+
/* Perform various identities and other optimizations on the list of
operand entries, stored in OPS. The tree code for the binary
operation between all the operands is OPCODE. */
@@ -5127,6 +5216,10 @@ reassociate_bb (basic_block bb)
powi_result = attempt_builtin_powi (stmt, &ops);
}
+ if (rhs_code == PLUS_EXPR
+ && transform_add_to_multiply (&ops))
+ ops.qsort (sort_by_operand_rank);
+
/* If the operand vector is now empty, all operands were
consumed by the __builtin_powi optimization. */
if (ops.length () == 0)