Patchwork Don't create out-of-bounds BIT_FIELD_REFs

login
register
mail settings
Submitter Tom de Vries
Date Dec. 3, 2013, 1:38 p.m.
Message ID <529DDECD.3090701@mentor.com>
Download mbox | patch
Permalink /patch/296200/
State New
Headers show

Comments

Tom de Vries - Dec. 3, 2013, 1:38 p.m.
On 03-12-13 13:49, Jakub Jelinek wrote:
> On Thu, Nov 28, 2013 at 12:23:43AM +0100, Tom de Vries wrote:
>> Committed to trunk.
>>
>> Also ok for 4.8 branch? It's a 4.8/4.9 regression.
>
> Ok, but I guess you need to adjust your patch for 4.8 (tree_to_*
> and tree_fits_* to host_integerp/tree_low_cst), so please make sure
> you test it before commiting.
>

Jakub,

Richard B. already ok'ed the patch, but with the checking bit left out.

The functions you mention were in that checking bit.

I've tested and committed attached patch.

Thanks,
- Tom

> 	Jakub
>

Patch

2013-11-27  Tom de Vries  <tom@codesourcery.com>
	    Marc Glisse  <marc.glisse@inria.fr>

	PR middle-end/59037
	* semantics.c (cxx_fold_indirect_ref): Don't create out-of-bounds
	BIT_FIELD_REF.

	* fold-const.c (fold_indirect_ref_1): Don't create out-of-bounds
	BIT_FIELD_REF.
	* gimplify.c (gimple_fold_indirect_ref): Same.

	* c-c++-common/pr59037.c: New testcase.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 571fc7c..e7e2034 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -7543,7 +7543,7 @@  cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
 	      unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
 	      tree index = bitsize_int (indexi);
 
-	      if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
+	      if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
 		return fold_build3_loc (loc,
 					BIT_FIELD_REF, type, op00,
 					part_width, index);
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index f666429..7433686 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -16595,7 +16595,7 @@  fold_indirect_ref_1 (location_t loc, tree type, tree op0)
 	      unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
 	      tree index = bitsize_int (indexi);
 
-	      if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
+	      if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
 		return fold_build3_loc (loc,
 					BIT_FIELD_REF, type, op00,
 					part_width, index);
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index e711928..1f1deb8 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -4369,7 +4369,7 @@  gimple_fold_indirect_ref (tree t)
           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
           tree index = bitsize_int (indexi);
           if (offset / part_widthi
-              <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
+              < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
                                 part_width, index);
 	}
diff --git a/gcc/testsuite/c-c++-common/pr59037.c b/gcc/testsuite/c-c++-common/pr59037.c
new file mode 100644
index 0000000..fae13c2
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/pr59037.c
@@ -0,0 +1,12 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O3" } */
+
+typedef int v4si __attribute__ ((vector_size (16)));
+
+int
+main (int argc, char** argv)
+{
+  v4si x = {0,1,2,3};
+  x = (v4si) {(x)[3], (x)[2], (x)[1], (x)[0]};
+  return x[4];
+}