Hello,

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 3b397ae2941..b8add24f874 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -150,6 +150,49 @@ static tree fold_convert_const (enum tree_code, tree, 
tree);
  static tree fold_view_convert_expr (tree, tree);
  static tree fold_negate_expr (location_t, tree);
+/* This is a helper function to detect min/max for some operands of COND_EXPR.
+   The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
+tree_code
+minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree 
exp3)
+{
+  enum tree_code code = ERROR_MARK;
+
+  if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
+    return ERROR_MARK;
+
+  if (!operand_equal_p (exp0, exp2))
+    return ERROR_MARK;
+
+  if (TREE_CODE (exp1) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST

one of these conditions should probably be TREE_CODE (exp3) == INTEGER_CST?

+      && wi::to_widest (exp1) == (wi::to_widest (exp3) - 1))
+    {
+      /* X <= Y - 1 equals to X < Y.  */
+      if (cmp == LE_EXPR)
+       code = LT_EXPR;
+      /* X > Y - 1 equals to X >= Y.  */
+      if (cmp == GT_EXPR)
+       code = GE_EXPR;
+    }
+  if (TREE_CODE (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST
+      && wi::to_widest (exp1) == (wi::to_widest (exp3) + 1))
+    {
+     /* X < Y + 1 equals to X <= Y.  */
+     if (cmp == LT_EXPR)
+       code = LE_EXPR;
+    /* X >= Y + 1 equals to X > Y.  */
+    if (cmp == GE_EXPR)
+       code = GT_EXPR;
+    }


Mikael

Reply via email to