[v3,3/8] c: Range-check indexing of SVE ACLE vectors

Message ID 20241129035404.3363162-4-tejas.belagod@arm.com
State Committed
Commit 47fa008ad880c115bce4007fe9808e48b0dcf859
Headers
Series aarch64: Enable C/C++ operations on SVE ACLE types. |

Commit Message

Tejas Belagod Nov. 29, 2024, 3:53 a.m. UTC
  This patch adds a check for non-GNU vectors to warn that the index is outside
the range of a fixed vector size.  For VLA vectors, we don't diagnose.

gcc/ChangeLog:

	* c-family/c-common.cc (convert_vector_to_array_for_subscript): Add
	range-check for target vector types.
---
 gcc/c-family/c-common.cc | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
  

Patch

diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index 721407157bc..260c2e005e6 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -9045,10 +9045,12 @@  convert_vector_to_array_for_subscript (location_t loc,
       ret = !lvalue_p (*vecp);
 
       index = fold_for_warn (index);
-      if (TREE_CODE (index) == INTEGER_CST)
-        if (!tree_fits_uhwi_p (index)
-	    || maybe_ge (tree_to_uhwi (index), TYPE_VECTOR_SUBPARTS (type)))
-	  warning_at (loc, OPT_Warray_bounds_, "index value is out of bound");
+      /* Warn out-of-bounds index for vectors only if known.  */
+      if (poly_int_tree_p (index))
+	if (!tree_fits_poly_uint64_p (index)
+	    || known_ge (tree_to_poly_uint64 (index),
+			  TYPE_VECTOR_SUBPARTS (type)))
+	    warning_at (loc, OPT_Warray_bounds_, "index value is out of bound");
 
       /* We are building an ARRAY_REF so mark the vector as addressable
          to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P