@@ -0,0 +1,44 @@
+/* PR tree-optimization/61839. */
+/* { dg-do run } */
+/* { dg-options "-O2 -fdump-tree-vrp1 -fdump-tree-optimized" } */
+/* { dg-require-effective-target int32plus } */
+
+__attribute__ ((noinline))
+int foo ()
+{
+ int a = -1;
+ volatile unsigned b = 1U;
+ int c = 1;
+ c = (a + 972195718) >> (1LU <= b);
+ if (c == 486097858)
+ ;
+ else
+ __builtin_abort ();
+ return 0;
+}
+
+__attribute__ ((noinline))
+int bar ()
+{
+ int a = -1;
+ volatile unsigned b = 1U;
+ int c = 1;
+ c = (a + 972195718) >> (b ? 2 : 3);
+ if (c == 243048929)
+ ;
+ else
+ __builtin_abort ();
+ return 0;
+}
+
+int main ()
+{
+ foo ();
+ bar ();
+}
+
+/* Scan for c = 972195717) >> [0, 1] in function foo. */
+/* { dg-final { scan-tree-dump-times "972195717 : 486097858" 1 "vrp1" } } */
+/* Scan for c = 972195717) >> [2, 3] in function bar. */
+/* { dg-final { scan-tree-dump-times "243048929 : 121524464" 2 "vrp1" } } */
+/* { dg-final { scan-tree-dump-times "486097858" 0 "optimized" } } */
@@ -10004,6 +10004,39 @@ simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
return true;
}
+/* Return true if VAR is a two-valued variable. Set a and b with the
+ two-values when it is true. Return false otherwise. */
+
+static bool
+two_valued_val_range_p (tree var, tree *a, tree *b)
+{
+ value_range *vr = get_value_range (var);
+ if ((vr->type != VR_RANGE
+ && vr->type != VR_ANTI_RANGE)
+ || !range_int_cst_p (vr))
+ return false;
+
+ if (vr->type == VR_RANGE
+ && wi::sub (vr->max, vr->min) == 1)
+ {
+ *a = vr->min;
+ *b = vr->max;
+ return true;
+ }
+
+ /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
+ if (vr->type == VR_ANTI_RANGE
+ && wi::sub (vr->min, wi::min_value (TREE_TYPE (var))) == 1
+ && wi::sub (wi::max_value (TREE_TYPE (var)), vr->max) == 1)
+ {
+ *a = TYPE_MIN_VALUE (TREE_TYPE (var));
+ *b = TYPE_MAX_VALUE (TREE_TYPE (var));
+ return true;
+ }
+
+ return false;
+}
+
/* Simplify STMT using ranges if possible. */
static bool
@@ -10014,6 +10047,61 @@ simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
{
enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
tree rhs1 = gimple_assign_rhs1 (stmt);
+ tree rhs2 = gimple_assign_rhs2 (stmt);
+ tree lhs = gimple_assign_lhs (stmt);
+ tree val1 = NULL_TREE, val2 = NULL_TREE;
+ use_operand_p use_p;
+ gimple *use_stmt;
+
+ /* First canonicalize to simplify tests. */
+ if (commutative_tree_code (rhs_code)
+ && TREE_CODE (rhs2) == INTEGER_CST)
+ std::swap (rhs1, rhs2);
+
+ /* Convert:
+ LHS = CST BINOP VAR
+ Where VAR is two-valued and LHS is used in GIMPLE_COND only
+ To:
+ LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2) */
+
+ if (TREE_CODE_CLASS (rhs_code) == tcc_binary
+ && TREE_CODE (rhs1) == INTEGER_CST
+ && TREE_CODE (rhs2) == SSA_NAME
+ && single_imm_use (lhs, &use_p, &use_stmt)
+ && gimple_code (use_stmt) == GIMPLE_COND
+ && two_valued_val_range_p (rhs2, &val1, &val2))
+
+ {
+ tree new_rhs1 = NULL_TREE;
+ tree new_rhs2 = NULL_TREE;
+ switch (rhs_code)
+ {
+ case TRUNC_DIV_EXPR:
+ case CEIL_DIV_EXPR:
+ case FLOOR_DIV_EXPR:
+ case ROUND_DIV_EXPR:
+ case EXACT_DIV_EXPR:
+ /* Prevent divide by zero. */
+ if (integer_zerop (val1)
+ || integer_zerop (val2))
+ break;
+ default:
+ new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
+ new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
+ break;
+ }
+
+ if (new_rhs1 && new_rhs2)
+ {
+ tree cond = build2 (EQ_EXPR, TREE_TYPE (rhs2), rhs2, val1);
+ gimple_assign_set_rhs_with_ops (gsi,
+ COND_EXPR, cond,
+ new_rhs1,
+ new_rhs2);
+ update_stmt (gsi_stmt (*gsi));
+ return true;
+ }
+ }
switch (rhs_code)
{