===================================================================
@@ -1674,6 +1674,228 @@ simplify_builtin_call (gimple_stmt_itera
return false;
}
+/* Checks if expression has type of one-bit precision, or is result of
+ a known boolean expression. */
+static bool
+operand_precision_onep (tree expr)
+{
+ enum tree_code code;
+
+ do
+ {
+ code = TREE_CODE (expr);
+ if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
+ return false;
+ if (TYPE_PRECISION (TREE_TYPE (expr)) == 1)
+ return true;
+ if (code == SSA_NAME)
+ {
+ gimple def_stmt = SSA_NAME_DEF_STMT (expr);
+ if (!def_stmt || !is_gimple_assign (def_stmt))
+ break;
+ code = gimple_assign_rhs_code (def_stmt);
+ if (!CONVERT_EXPR_CODE_P (code))
+ break;
+ expr = gimple_assign_rhs1 (def_stmt);
+ }
+ else
+ break;
+ }
+ while (CONVERT_EXPR_CODE_P (code));
+ if (code == TRUTH_NOT_EXPR || TREE_CODE_CLASS (code) == tcc_comparison)
+ return true;
+ return false;
+}
+
+/* Helper routine for simplify_bitwise_binary_1 function.
+ Ignored integral type cast is counted in CNT_CASTS. If a not expression
+ is found, the operand of the not-expression is stored in NEXPR.
+ This function returns the either the expression after the first
+ integral cast expression, or NAME.
+ Detected not-patterns are !X or X == 0 for X with integral type, and
+ X ^ 1 or ~X for X with integral type with precision of one.
+ The value of CNT_CASTS is either zero, or one. */
+static tree
+detect_not_expr_operand (tree name, int *cnt_casts, tree *nexpr)
+{
+ enum tree_code code = ERROR_MARK;
+ *cnt_casts = 0;
+ *nexpr = NULL_TREE;
+
+ while (1)
+ {
+ tree op1, op2;
+ gimple def_stmt;
+ code = TREE_CODE (name);
+ /* If name has none-intergal type, or isn't a SSA_NAME, then
+ stop search. */
+ if (code != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (name)))
+ break;
+ def_stmt = SSA_NAME_DEF_STMT (name);
+ if (!def_stmt || !is_gimple_assign (def_stmt))
+ break;
+
+ code = gimple_assign_rhs_code (def_stmt);
+ op1 = gimple_assign_rhs1 (def_stmt);
+ op2 = NULL_TREE;
+ /* Get for EQ_EXPR or BIT_XOR_EXPR operation the second operand.
+ If CODE isn't an EQ_EXPR, BIT_XOR_EXPR, TRUTH_NOT_EXPR,
+ BIT_NOT_EXPR, and no conversion, break search. */
+ if (code == EQ_EXPR || code == BIT_XOR_EXPR)
+ op2 = gimple_assign_rhs2 (def_stmt);
+ else if (!CONVERT_EXPR_CODE_P (code)
+ && code != TRUTH_NOT_EXPR
+ && code != BIT_NOT_EXPR)
+ break;
+ /* If !X or ~X is found, set NEXPR and break search. */
+ if (code == TRUTH_NOT_EXPR
+ || (code == BIT_NOT_EXPR
+ && INTEGRAL_TYPE_P (name)
+ && operand_precision_onep (name)))
+ {
+ *nexpr = op1;
+ break;
+ }
+ /* If X == 0 or X ^ 1 is found, set NEXPR and break search. */
+ else if (code == EQ_EXPR || code == BIT_XOR_EXPR)
+ {
+ if (!INTEGRAL_TYPE_P (TREE_TYPE (op1)))
+ break;
+ if (code == EQ_EXPR
+ && TREE_CODE (op2) == INTEGER_CST
+ && integer_zerop (op2))
+ *nexpr = op1;
+ else if (code == EQ_EXPR
+ && TREE_CODE (op1) == INTEGER_CST
+ && integer_zerop (op1))
+ *nexpr = op2;
+ else if (code == BIT_XOR_EXPR
+ && operand_precision_onep (op1)
+ && TREE_CODE (op2) == INTEGER_CST
+ && integer_onep (op2))
+ *nexpr = op1;
+ else if (code == BIT_XOR_EXPR
+ && operand_precision_onep (op2)
+ && TREE_CODE (op1) == INTEGER_CST
+ && integer_onep (op1))
+ *nexpr = op2;
+ break;
+ }
+ /* If expression isn't a conversion, break search. */
+ else if (!CONVERT_EXPR_CODE_P (code))
+ break;
+ /* Just allow sinking over one cast and this only for
+ integeral types. Otherwise value truncation
+ or intermediate float conversions need to be
+ considered. */
+ if (cnt_casts[0] != 0)
+ break;
+ cnt_casts[0] += 1;
+ name = op1;
+ }
+
+ return name;
+}
+
+/* Try to optimize patterns X & not(X) -> zero, X | not(X) -> one, and
+ X ^ not(X) -> one, if type of X is valid for this.
+ Additional handle the variants X & X -> X, X | X -> X, and X ^ X -> zero.
+ We also pattern match optional integral type casts on operation's
+ operands.
+
+ See for detected not-patterns the detect_not_expr_operand function. */
+static tree
+simplify_bitwise_binary_1 (enum tree_code code, tree arg1, tree arg2)
+{
+ int a1cnt = 0, a2cnt = 0;
+ tree a1strip, a1expr;
+ tree a2strip, a2expr;
+ tree op = NULL_TREE;
+
+ /* If CODE isn't a bitwise binary operation, return NULL_TREE. */
+ if (code != BIT_AND_EXPR && code != BIT_IOR_EXPR
+ && code != BIT_XOR_EXPR)
+ return NULL_TREE;
+
+ /* First check if operands ARG1 and ARG2 are equal, if so we
+ won't have a not pattern match. Fold these patterns, as
+ we have detected it already. */
+ if (operand_equal_p (arg1, arg2, 0))
+ {
+ /* X & X -> X, and X | X -> X. */
+ if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR)
+ return arg1;
+ /* X ^ X -> 0. */
+ return integer_zero_node;
+ }
+ /* Get for each operation operand its optional by one integral typed
+ cast stripped argument. And get the not-expression's operand, if
+ argument represents an not-expression. */
+ a1strip = detect_not_expr_operand (arg1, &a1cnt, &a1expr);
+ a2strip = detect_not_expr_operand (arg2, &a2cnt, &a2expr);
+
+ /* If there are no not-expressions found, return NULL_TREE. */
+ if (!a1expr && !a2expr)
+ return NULL_TREE;
+
+ /* Do we have case not(X) op not(X)? */
+ if (a1expr && a2expr)
+ {
+ /* Check if operands not(ARG1) and not(ARG2) are equal and
+ fold them if so.
+ if (operand_equal_p (a1expr, a2expr, 0))
+ {
+ /* !X & !X -> !X, and !X | i!X -> !X. */
+ if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR)
+ return arg1;
+ /* !X ^ !X -> 0. */
+ return integer_zero_node;
+ }
+ return NULL_TREE;
+ }
+
+ if (a2expr)
+ {
+ /* Check if X is equal for cases X CODE not(X), or
+ (type) X not(X). */
+ if (operand_equal_p (arg1, a2expr, 0)
+ || operand_equal_p (a1strip, a2expr, 0))
+ op = arg1;
+ }
+ /* If haven't found already a match, then see if we have
+ equal X for cases not(X) op X, or (not(X) op (type)X. */
+ if (!op && a1expr)
+ {
+ if (operand_equal_p (arg2, a1expr, 0)
+ || operand_equal_p (a2strip, a1expr, 0))
+ op = arg2;
+ }
+ /* Did we found no match, return NULL_TREE. */
+ if (!op)
+ return NULL_TREE;
+
+ /* We have found a X op not(X) operation. */
+ if (code == BIT_AND_EXPR)
+ return integer_zero_node;
+ else if (code == BIT_IOR_EXPR)
+ {
+ /* If X has type precision of one, then result is 1. */
+ if ((op == arg1 && operand_precision_onep (a1strip))
+ || (op == arg2 && operand_precision_onep (a2strip)))
+ return integer_one_node;
+ /* ??? Otherwise result is (X != 0 ? X : 1). not handled. */
+ }
+ else
+ {
+ /* Result for XOR for X with type precision of one is 1. */
+ if ((op == arg1 && operand_precision_onep (a1strip))
+ || (op == arg2 && operand_precision_onep (a2strip)))
+ return integer_one_node;
+ /* ??? Otherwise result is (X != 0 ? X : 1. not handled. */
+ }
+ return NULL_TREE;
+}
+
/* Simplify bitwise binary operations.
Return true if a transformation applied, otherwise return false. */
@@ -1840,7 +2062,31 @@ simplify_bitwise_binary (gimple_stmt_ite
update_stmt (stmt);
return true;
}
+ /* Try simple folding for X op !X, and X op X. */
+ res = simplify_bitwise_binary_1 (code, arg1, arg2);
+ if (res != NULL_TREE && TREE_CODE (res) == INTEGER_CST)
+ {
+ res = fold_convert_loc (gimple_location (stmt),
+ TREE_TYPE (arg1), res);
+ gimple_assign_set_rhs_from_tree (gsi, res);
+ update_stmt (gsi_stmt (*gsi));
+ return true;
+ }
+ else if (res)
+ {
+ if (!useless_type_conversion_p (TREE_TYPE (arg1), TREE_TYPE (res)))
+ {
+ res = force_gimple_operand_gsi (gsi, res, true, NULL_TREE, true,
+ GSI_SAME_STMT);
+ gimple_assign_set_rhs_with_ops_1 (gsi, NOP_EXPR,
+ res, NULL_TREE, NULL_TREE);
+ }
+ else
+ gimple_assign_set_rhs_from_tree (gsi, res);
+ update_stmt (gsi_stmt (*gsi));
+ return true;
+ }
return false;
}
===================================================================
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (char a, unsigned short b)
+{
+ return (a & !a) | (b & !b);
+}
+
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
===================================================================
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (int a)
+{
+ return (!a & 1) != (a == 0);
+}
+
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
===================================================================
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (short a)
+{
+ return (!a & 1) != ((a == 0) & 1);
+}
+
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
===================================================================
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (unsigned char a, _Bool b)
+{
+ return (!a & a) | (b & !b);
+}
+
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
===================================================================
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (long a, unsigned long b)
+{
+ return (a & (a == 0)) | (b & !b);
+}
+
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
===================================================================
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (unsigned long a, long b)
+{
+ return (a & !a) | (b & (b == 0));
+}
+
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
===================================================================
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (_Bool a, _Bool b)
+{
+ return (a | !a) | (!b | b);
+}
+
+/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
===================================================================
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (_Bool a, _Bool b)
+{
+ return (a | (a == 0)) | ((b ^ 1) | b);
+}
+
+/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
===================================================================
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (_Bool a, _Bool b)
+{
+ return (a ^ !a) | (!b ^ b);
+}
+
+/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
===================================================================
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (_Bool a, _Bool b)
+{
+ return (a ^ (a == 0)) | ((b == 0) ^ b);
+}
+
+/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */