http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46675

--- Comment #24 from H.J. Lu <hjl.tools at gmail dot com> 2010-11-28 21:04:10 
UTC ---
Does this patch make any senses?

---
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index c195073..4bcdd07 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -10225,6 +10225,22 @@ fold_binary_loc (location_t loc,
                 fold_convert_loc (loc, type,
                           negate_expr (arg1)));

+      /* A - B -> A + (-B) if B is negative integer constant.  */
+      if (TREE_CODE (arg1) == INTEGER_CST && !TYPE_UNSIGNED (type))
+    {
+      double_int di = tree_to_double_int (arg1);
+      if (double_int_negative_p (di))
+        {
+          tree unsigned_type = unsigned_type_for (type);
+          di = double_int_neg (di);
+          return fold_build2_loc (loc, PLUS_EXPR, unsigned_type,
+                      fold_convert_loc (loc, unsigned_type, arg0
),
+                      fold_convert_loc (loc,
+                            unsigned_type,
+                            double_int_to_tree (type
, di)));
+        }
+    }
+
       /* Try folding difference of addresses.  */
       {
     HOST_WIDE_INT diff;
---

Reply via email to