Hi,

Please find attached the patch that fixes the tree optimization 23471.

Please review the patch and let me know if its okay?

Regression tested on X86_64.

Thanks,
Naveen

2016-03-31  Naveen H.S  <naveen.hurugalaw...@caviumnetworks.com>

        * fold-const.c (tree_binary_nonnegative_warnv_p) : Handle the case
        a * a; where it should be positive always.

        * gcc.dg/pr23471.c: New testcase.
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 788ecc3..a0b0def 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -12923,6 +12923,15 @@ tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
       break;
 
     case MULT_EXPR:
+      /* a * a is non-negative.  */
+      if (!flag_wrapv && operand_equal_p (op0, op1, 0))
+	{
+	  if (ANY_INTEGRAL_TYPE_P (type)
+	      && TYPE_OVERFLOW_UNDEFINED (type))
+	    *strict_overflow_p = true;
+	  return true;
+	}
+
       if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
 	{
 	  /* x * x is always non-negative for floating point x
diff --git a/gcc/testsuite/gcc.dg/pr23471.c b/gcc/testsuite/gcc.dg/pr23471.c
new file mode 100644
index 0000000..72b29cc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr23471.c
@@ -0,0 +1,16 @@
+/* PR tree-optimization/23471 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fno-wrapv -fdump-tree-optimized" } */
+
+extern void link_error (void);
+
+void
+f (int a)
+{
+  int b = a;
+  b *= a;
+  if (b < 0)
+    link_error ();
+}
+
+/* { dg-final { scan-tree-dump-not "link_error" "optimized" } } */

Reply via email to