@@ -9902,6 +9902,49 @@ 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 var;
+
+ /* Convert:
+ COND_RES = X COMPARE Y
+ TMP = (CAST) COND_RES
+ LHS = CST BINOP TMP
+
+ To:
+ LHS = COND_RES ? (CST BINOP 1) : (CST BINOP 0) */
+
+ if (TREE_CODE_CLASS (rhs_code) == tcc_binary
+ && TREE_CODE (rhs1) == INTEGER_CST
+ && TREE_CODE (rhs2) == SSA_NAME
+ && is_gimple_assign (SSA_NAME_DEF_STMT (rhs2))
+ && gimple_assign_rhs_code (SSA_NAME_DEF_STMT (rhs2)) == NOP_EXPR
+ && (var = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (rhs2)))
+ && TREE_CODE (var) == SSA_NAME
+ && is_gimple_assign (SSA_NAME_DEF_STMT (var))
+ && TREE_CODE_CLASS (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (var)))
+ == tcc_comparison)
+
+ {
+ value_range *vr = get_value_range (var);
+ if (range_int_cst_p (vr)
+ && integer_zerop (vr->min)
+ && integer_onep (vr->max))
+ {
+
+ tree new_rhs1 = int_const_binop (rhs_code, rhs1, vr->max);
+ tree new_rhs2 = int_const_binop (rhs_code, rhs1, vr->min);
+
+ if (new_rhs1 && new_rhs2)
+ {
+ gimple_assign_set_rhs_with_ops (gsi,
+ COND_EXPR, var,
+ new_rhs1,
+ new_rhs2);
+ update_stmt (gsi_stmt (*gsi));
+ return true;
+ }
+ }
+ }
switch (rhs_code)
{