Patchwork Fix PR52754

login
register
mail settings
Submitter Richard Guenther
Date March 30, 2012, 1:38 p.m.
Message ID <Pine.LNX.4.64.1203301536280.25270@jbgna.fhfr.qr>
Download mbox | patch
Permalink /patch/149652/
State New
Headers show

Comments

Richard Guenther - March 30, 2012, 1:38 p.m.
This fixes another case of bogusly reconstructed array references.
The symptom is that predictive commoning creates negative array
indices (and in a wrong way, too).

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk.

Richard.

2012-03-30  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/52754
	* tree-ssa-forwprop.c (forward_propagate_addr_expr_1): Only
	propagate arbitrary addresses into really plain dereferences.

	* gcc.target/i386/pr52754.c: New testcase.

Patch

Index: gcc/tree-ssa-forwprop.c
===================================================================
--- gcc/tree-ssa-forwprop.c	(revision 185999)
+++ gcc/tree-ssa-forwprop.c	(working copy)
@@ -905,6 +905,7 @@  forward_propagate_addr_expr_1 (tree name
          that of the pointed-to type of the address we can put the
 	 dereferenced address on the LHS preserving the original alias-type.  */
       else if (gimple_assign_lhs (use_stmt) == lhs
+	       && integer_zerop (TREE_OPERAND (lhs, 1))
 	       && useless_type_conversion_p
 	            (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
 		     TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
@@ -917,9 +918,8 @@  forward_propagate_addr_expr_1 (tree name
 	  if (TREE_CODE (*def_rhs_basep) == MEM_REF)
 	    {
 	      new_base = TREE_OPERAND (*def_rhs_basep, 0);
-	      new_offset
-		= int_const_binop (PLUS_EXPR, TREE_OPERAND (lhs, 1),
-				   TREE_OPERAND (*def_rhs_basep, 1));
+	      new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
+					 TREE_OPERAND (*def_rhs_basep, 1));
 	    }
 	  else
 	    {
@@ -989,6 +989,7 @@  forward_propagate_addr_expr_1 (tree name
          that of the pointed-to type of the address we can put the
 	 dereferenced address on the RHS preserving the original alias-type.  */
       else if (gimple_assign_rhs1 (use_stmt) == rhs
+	       && integer_zerop (TREE_OPERAND (rhs, 1))
 	       && useless_type_conversion_p
 		    (TREE_TYPE (gimple_assign_lhs (use_stmt)),
 		     TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
@@ -1001,9 +1002,8 @@  forward_propagate_addr_expr_1 (tree name
 	  if (TREE_CODE (*def_rhs_basep) == MEM_REF)
 	    {
 	      new_base = TREE_OPERAND (*def_rhs_basep, 0);
-	      new_offset
-		= int_const_binop (PLUS_EXPR, TREE_OPERAND (rhs, 1),
-				   TREE_OPERAND (*def_rhs_basep, 1));
+	      new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
+					 TREE_OPERAND (*def_rhs_basep, 1));
 	    }
 	  else
 	    {
Index: gcc/testsuite/gcc.target/i386/pr52754.c
===================================================================
--- gcc/testsuite/gcc.target/i386/pr52754.c	(revision 0)
+++ gcc/testsuite/gcc.target/i386/pr52754.c	(revision 0)
@@ -0,0 +1,33 @@ 
+/* { dg-do run } */
+/* { dg-options "-O2 -fpredictive-commoning -msse2 -std=c99" } */
+/* { dg-require-effective-target sse2 } */
+
+#include <x86intrin.h>
+
+#include "isa-check.h"
+#include "sse-os-support.h"
+
+int main()
+{
+    const float mem[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
+
+    unsigned int indexes[8];
+    for (unsigned int i = 0; i < 8; ++i) indexes[i] = i;
+
+    check_isa ();
+
+    if (!sse_os_support ())
+      exit (0);
+
+    __m128 x = _mm_setr_ps(0, 1, 2, 3);
+    for (unsigned int i = 0; i + 4 < 6; ++i) {
+        const unsigned int *ii = &indexes[i];
+        const __m128 tmp = _mm_setr_ps(mem[ii[0]], mem[ii[1]], mem[ii[2]], mem[ii[3]]);
+        if (0xf != _mm_movemask_ps(_mm_cmpeq_ps(tmp, x))) {
+            __builtin_abort();
+        }
+        x = _mm_add_ps(x, _mm_set1_ps(1));
+    }
+
+    return 0;
+}