diff mbox

Fix PR23471

Message ID SN2PR0701MB1024D1CF026995DC14B0B3308E990@SN2PR0701MB1024.namprd07.prod.outlook.com
State New
Headers show

Commit Message

Hurugalawadi, Naveen March 31, 2016, 8:55 a.m. UTC
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.Hurugalawadi@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.

Comments

Marc Glisse March 31, 2016, 9:19 a.m. UTC | #1
On Thu, 31 Mar 2016, Hurugalawadi, Naveen wrote:

> 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.Hurugalawadi@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.

Er, the code just below your patch should already handle this case, no?
Hurugalawadi, Naveen March 31, 2016, 9:55 a.m. UTC | #2
>> Er, the code just below your patch should already handle this case, no?

Hi,

Thanks for the review and your comments on the patch.

The code below seems to handle this case for O2 or higher optimization.

However, somehow its not being handled with O1 and hence has this
implemented for better optimization.
Can you please let me know if it can be handled in a better way or neglect
this one just for this case?

Thanks,
Naveen
Richard Biener March 31, 2016, 10:03 a.m. UTC | #3
On Thu, Mar 31, 2016 at 11:55 AM, Hurugalawadi, Naveen
<Naveen.Hurugalawadi@caviumnetworks.com> wrote:
>>> Er, the code just below your patch should already handle this case, no?
>
> Hi,
>
> Thanks for the review and your comments on the patch.
>
> The code below seems to handle this case for O2 or higher optimization.
>
> However, somehow its not being handled with O1 and hence has this
> implemented for better optimization.
> Can you please let me know if it can be handled in a better way or neglect
> this one just for this case?

With -O1 there is -fno-strict-overflow in effect which is not
TYPE_OVERFLOW_UNDEFINED.
Your !flag_wrapv check is simply wrong (for -fno-strict-overflow
neither TYPE_OVERFLOW_WRAPS nor
TYPE_OVERFLOW_UNDEFINED is set).

Richard.

> Thanks,
> Naveen
Hurugalawadi, Naveen March 31, 2016, 10:12 a.m. UTC | #4
>> With -O1 there is -fno-strict-overflow in effect which is not
>> TYPE_OVERFLOW_UNDEFINED.

Thanks for the details. The below code has the following condition
and hence not working for O1. 
if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))

Then PR23471 is fixed with the current sources.

Thanks,
Naveen
Richard Biener March 31, 2016, 10:25 a.m. UTC | #5
On Thu, Mar 31, 2016 at 12:12 PM, Hurugalawadi, Naveen
<Naveen.Hurugalawadi@caviumnetworks.com> wrote:
>>> With -O1 there is -fno-strict-overflow in effect which is not
>>> TYPE_OVERFLOW_UNDEFINED.
>
> Thanks for the details. The below code has the following condition
> and hence not working for O1.
> if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))

Yes, and that's correct.

> Then PR23471 is fixed with the current sources.

Yes.

Richard.

> Thanks,
> Naveen
diff mbox

Patch

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" } } */