Patchwork [tree-optimization/57124] Updated fix for 254.gap problems

login
register
mail settings
Submitter Jeff Law
Date May 24, 2013, 5:15 p.m.
Message ID <519FA02F.40507@redhat.com>
Download mbox | patch
Permalink /patch/246228/
State New
Headers show

Comments

Jeff Law - May 24, 2013, 5:15 p.m.
On 05/21/2013 02:23 AM, Richard Biener wrote:
> On Fri, May 17, 2013 at 5:51 PM, Jeff Law <law@redhat.com> wrote:
>> As I believe I pointed out in a follow-up message, 254.gap is depending on
>> signed overflow semantics.
>>
>> This patch avoids eliminating a cast feeding a conditional when the
>> SSA_NAME's range has overflowed unless -fstrict-overflow is in effect. Thus
>> 254.gap should be building with -fno-strict-overflow.
>>
>> This patch also introduces a warning when the optimization is applied and
>> the ranges have overflowed.
>>
>> Bootstrapped and regression tested on x86-unknown-linux-gnu.
>>
>> OK for the trunk?
>>
>>
>>
>>
>> commit 62bbaa8de0e8d929eb3c63331b47950e9b09d801
>> Author: Jeff Law <law@redhat.com>
>> Date:   Wed May 1 12:33:20 2013 -0600
>>
>>          PR tree-optimization/57124
>>          * tree-vrp.c (simplify_cond_using_ranges): Only simplify a
>>          conversion feeding a condition if the range has an overflow
>>          if -fstrict-overflow.  Add warnings for when we do make the
>>          transformation.
>>
>>          PR tree-optimization/57124
>>          * gcc.c-torture/execute/pr57124.c: New test.
>>          * gcc.c-torture/execute/pr57124.x: Set -fno-strict-overflow.
>>
>> diff --git a/gcc/ChangeLog b/gcc/ChangeLog
>> index 8e92c44..9320f21 100644
>> --- a/gcc/ChangeLog
>> +++ b/gcc/ChangeLog
>> @@ -1,3 +1,11 @@
>> +2013-05-17  Jeff Law  <law@redhat.com>
>> +
>> +       PR tree-optimization/57124
>> +       * tree-vrp.c (simplify_cond_using_ranges): Only simplify a
>> +       conversion feeding a condition if the range has an overflow
>> +       if -fstrict-overflow.  Add warnings for when we do make the
>> +       transformation.
>> +
>>   2013-05-16  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
>>
>>          * reorg.c (link_cc0_insns): Wrap in #ifdef HAVE_cc0.
>> diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
>> index 879b9bc..482151c 100644
>> --- a/gcc/testsuite/ChangeLog
>> +++ b/gcc/testsuite/ChangeLog
>> @@ -1,3 +1,9 @@
>> +2013-05-17  Jeff Law  <law@redhat.com>
>> +
>> +       PR tree-optimization/57124
>> +       * gcc.c-torture/execute/pr57124.c: New test.
>> +       * gcc.c-torture/execute/pr57124.x: Set -fno-strict-overflow.
>> +
>>   2013-05-16  Greta Yorsh  <Greta.Yorsh@arm.com>
>>
>>          * gcc.target/arm/unaligned-memcpy-2.c: Adjust expected output.
>> diff --git a/gcc/testsuite/gcc.c-torture/execute/pr57124.c
>> b/gcc/testsuite/gcc.c-torture/execute/pr57124.c
>> new file mode 100644
>> index 0000000..835d249
>> --- /dev/null
>> +++ b/gcc/testsuite/gcc.c-torture/execute/pr57124.c
>> @@ -0,0 +1,27 @@
>> +__attribute__ ((noinline))
>> +foo(short unsigned int *p1, short unsigned int *p2)
>> +{
>> +  short unsigned int x1, x4;
>> +  int x2, x3, x5, x6;
>> +  unsigned int x7;
>> +
>> +  x1 = *p1;
>> +  x2 = (int) x1;
>> +  x3 = x2 * 65536;
>> +  x4 = *p2;
>> +  x5 = (int) x4;
>> +  x6 = x3 + x4;
>> +  x7 = (unsigned int) x6;
>> +  if (x7 <= 268435455U)
>> +    abort ();
>> +  exit (0);
>> +}
>> +
>> +main()
>> +{
>> +  short unsigned int x, y;
>> +  x = -5;
>> +  y = -10;
>> +  foo (&x, &y);
>> +}
>> +
>> diff --git a/gcc/testsuite/gcc.c-torture/execute/pr57124.x
>> b/gcc/testsuite/gcc.c-torture/execute/pr57124.x
>> new file mode 100644
>> index 0000000..d8cacbe
>> --- /dev/null
>> +++ b/gcc/testsuite/gcc.c-torture/execute/pr57124.x
>> @@ -0,0 +1,2 @@
>> +set additional_flags "-fno-strict-overflow"
>> +return 0
>> diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c
>> index b5de683..b3eccf0 100644
>> --- a/gcc/tree-vrp.c
>> +++ b/gcc/tree-vrp.c
>> @@ -8669,8 +8669,32 @@ simplify_cond_using_ranges (gimple stmt)
>>                && range_fits_type_p (vr,
>>                                      TYPE_PRECISION (TREE_TYPE (op0)),
>>                                      TYPE_UNSIGNED (TREE_TYPE (op0)))
>> -             && int_fits_type_p (op1, TREE_TYPE (innerop)))
>> +             && int_fits_type_p (op1, TREE_TYPE (innerop))
>> +             /* The range must not have overflowed, or if it did overflow
>> +                we must not be wrapping/trapping overflow and optimizing
>> +                with strict overflow semantics.  */
>> +             && ((!is_negative_overflow_infinity (vr->min)
>> +                  && !is_positive_overflow_infinity (vr->max))
>> +                 || (!flag_wrapv && !flag_trapv && flag_strict_overflow)))
>
> Ok with changing this to
>
>                         || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop)))
That's handy :-)    Done.


>> +                 warning_at (location, OPT_Wstrict_overflow,
>> +                     "assuming signed overflow does not occur when "
>> +                     "simplifying conditional.");
>
> and the trailing '.' removed
Done.

Bootstrapped and retested on x86_64-unknown-linux-gnu.  Installed. 
Final patch attached for future reference.

Jeff
commit 65a8f1a1cd31f5b564a7e41e9146c638aaeb0942
Author: law <law@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Fri May 24 17:13:38 2013 +0000

    	PR tree-optimization/57124
    	* tree-vrp.c (simplify_cond_using_ranges): Only simplify a
    	conversion feeding a condition if the range has an overflow
    	if -fstrict-overflow.  Add warnings for when we do make the
    	transformation.
    
    	PR tree-optimization/57124
    	* gcc.c-torture/execute/pr57124.c: New test.
    	* gcc.c-torture/execute/pr57124.x: Set -fno-strict-overflow.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@199305 138bc75d-0d04-0410-961f-82ee72b054a4

Patch

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index f5cffee..3d665c3 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,11 @@ 
+2013-05-24  Jeff Law  <law@redhat.com>
+
+	PR tree-optimization/57124
+	* tree-vrp.c (simplify_cond_using_ranges): Only simplify a
+	conversion feeding a condition if the range has an overflow
+	if -fstrict-overflow.  Add warnings for when we do make the
+	transformation.
+
 2013-05-24  Dehao Chen  <dehao@google.com>
 
 	* gcc/tree-cfg.c (locus_discrim_map): Fix the typo.
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index af4b16e..8c2e293 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@ 
+2013-05-24  Jeff Law  <law@redhat.com>
+
+	PR tree-optimization/57124
+	* gcc.c-torture/execute/pr57124.c: New test.
+	* gcc.c-torture/execute/pr57124.x: Set -fno-strict-overflow.
+
 2013-05-24  Martin Jambor  <mjambor@suse.cz>
 
 	PR tree-optimization/57294
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr57124.c b/gcc/testsuite/gcc.c-torture/execute/pr57124.c
new file mode 100644
index 0000000..835d249
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr57124.c
@@ -0,0 +1,27 @@ 
+__attribute__ ((noinline))
+foo(short unsigned int *p1, short unsigned int *p2)
+{
+  short unsigned int x1, x4;
+  int x2, x3, x5, x6;
+  unsigned int x7;
+  
+  x1 = *p1;
+  x2 = (int) x1;
+  x3 = x2 * 65536;
+  x4 = *p2;
+  x5 = (int) x4;
+  x6 = x3 + x4;
+  x7 = (unsigned int) x6;
+  if (x7 <= 268435455U)
+    abort ();
+  exit (0);
+}
+
+main()
+{
+  short unsigned int x, y;
+  x = -5;
+  y = -10;
+  foo (&x, &y);
+}
+
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr57124.x b/gcc/testsuite/gcc.c-torture/execute/pr57124.x
new file mode 100644
index 0000000..d8cacbe
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr57124.x
@@ -0,0 +1,2 @@ 
+set additional_flags "-fno-strict-overflow"
+return 0
diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c
index 66c50ca..ec7ef8f 100644
--- a/gcc/tree-vrp.c
+++ b/gcc/tree-vrp.c
@@ -8670,8 +8670,32 @@  simplify_cond_using_ranges (gimple stmt)
 	      && range_fits_type_p (vr,
 				    TYPE_PRECISION (TREE_TYPE (op0)),
 				    TYPE_UNSIGNED (TREE_TYPE (op0)))
-	      && int_fits_type_p (op1, TREE_TYPE (innerop)))
+	      && int_fits_type_p (op1, TREE_TYPE (innerop))
+	      /* The range must not have overflowed, or if it did overflow
+		 we must not be wrapping/trapping overflow and optimizing
+		 with strict overflow semantics.  */
+	      && ((!is_negative_overflow_infinity (vr->min)
+	           && !is_positive_overflow_infinity (vr->max))
+		  || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
 	    {
+	      /* If the range overflowed and the user has asked for warnings
+		 when strict overflow semantics were used to optimize code,
+		 issue an appropriate warning.  */
+	      if ((is_negative_overflow_infinity (vr->min)
+		   || is_positive_overflow_infinity (vr->max))
+		  && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
+		{
+		  location_t location;
+
+		  if (!gimple_has_location (stmt))
+		    location = input_location;
+		  else
+		    location = gimple_location (stmt);
+		  warning_at (location, OPT_Wstrict_overflow,
+		      "assuming signed overflow does not occur when "
+		      "simplifying conditional");
+		}
+
 	      tree newconst = fold_convert (TREE_TYPE (innerop), op1);
 	      gimple_cond_set_lhs (stmt, innerop);
 	      gimple_cond_set_rhs (stmt, newconst);