diff mbox series

[Backport,GCC14] tree-optimization/116585 - SSA corruption with split_constant_offset

Message ID 20241001185342.1330657-1-qing.zhao@oracle.com
State New
Headers show
Series [Backport,GCC14] tree-optimization/116585 - SSA corruption with split_constant_offset | expand

Commit Message

Qing Zhao Oct. 1, 2024, 6:53 p.m. UTC
From: Richard Biener <rguenther@suse.de>

Hi, this is the backport of the fix for PR116585 to GCC14.
bootstrapped and regress tested on both X86 and aarch64.

Okay for committing?

thanks.

Qing.

===============================

split_constant_offset when looking through SSA defs can end up
picking SSA leafs that are subject to abnormal coalescing.  This
can lead to downstream consumers to insert code based on the
result (like from dataref analysis) in places that violate constraints
for abnormal coalescing.  It's best to not expand defs whose operands
are subject to abnormal coalescing - and not either do something when
a subexpression has operands like that already.

	PR tree-optimization/116585
	* tree-data-ref.cc (split_constant_offset_1): When either
	operand is subject to abnormal coalescing do no further
	processing.

	* gcc.dg/torture/pr116585.c: New testcase.

(cherry picked from commit 1d0cb3b5fca69b81e69cfdb4aea0eebc1ac04750)
---
 gcc/testsuite/gcc.dg/torture/pr116585.c | 32 +++++++++++++++++++++++++
 gcc/tree-data-ref.cc                    | 11 ++++++---
 2 files changed, 40 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr116585.c

Comments

Richard Biener Oct. 2, 2024, 6:50 a.m. UTC | #1
On Tue, 1 Oct 2024, Qing Zhao wrote:

> From: Richard Biener <rguenther@suse.de>
> 
> Hi, this is the backport of the fix for PR116585 to GCC14.
> bootstrapped and regress tested on both X86 and aarch64.
> 
> Okay for committing?

OK.

> thanks.
> 
> Qing.
> 
> ===============================
> 
> split_constant_offset when looking through SSA defs can end up
> picking SSA leafs that are subject to abnormal coalescing.  This
> can lead to downstream consumers to insert code based on the
> result (like from dataref analysis) in places that violate constraints
> for abnormal coalescing.  It's best to not expand defs whose operands
> are subject to abnormal coalescing - and not either do something when
> a subexpression has operands like that already.
> 
> 	PR tree-optimization/116585
> 	* tree-data-ref.cc (split_constant_offset_1): When either
> 	operand is subject to abnormal coalescing do no further
> 	processing.
> 
> 	* gcc.dg/torture/pr116585.c: New testcase.
> 
> (cherry picked from commit 1d0cb3b5fca69b81e69cfdb4aea0eebc1ac04750)
> ---
>  gcc/testsuite/gcc.dg/torture/pr116585.c | 32 +++++++++++++++++++++++++
>  gcc/tree-data-ref.cc                    | 11 ++++++---
>  2 files changed, 40 insertions(+), 3 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/torture/pr116585.c
> 
> diff --git a/gcc/testsuite/gcc.dg/torture/pr116585.c b/gcc/testsuite/gcc.dg/torture/pr116585.c
> new file mode 100644
> index 00000000000..108c481e104
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/torture/pr116585.c
> @@ -0,0 +1,32 @@
> +/* { dg-do compile } */
> +
> +char *s1, *s2;
> +extern int* my_alloc (int);
> +extern int _setjmp ();
> +extern void bar();
> +void foo(int s1len, int s2len)
> +{
> +  int e;
> +  e = _setjmp ();
> +    {
> +      int l, i;
> +      int *md = my_alloc(((sizeof(int)) * (s1len + 1) * (s2len)));
> +      s1len++;
> +      for (; s1len; l)
> +	for (; s2len; l)
> +	  for (; s1len; i)
> +	    {
> +	      int j = 1;
> +	      for (; j < s2len; j++)
> +		{
> +		  int cost;
> +		  if (s1[1] == s2[1])
> +		    cost = 0;
> +		  else
> +		    cost = 1;
> +		  md[j * s1len ] = ((cost));
> +		}
> +	    }
> +      bar();
> +    }
> +}
> diff --git a/gcc/tree-data-ref.cc b/gcc/tree-data-ref.cc
> index 654a8220214..69b421f3c94 100644
> --- a/gcc/tree-data-ref.cc
> +++ b/gcc/tree-data-ref.cc
> @@ -765,6 +765,14 @@ split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
>    if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
>      return false;
>  
> +  if (TREE_CODE (op0) == SSA_NAME
> +      && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
> +    return false;
> +  if (op1
> +      && TREE_CODE (op1) == SSA_NAME
> +      && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))
> +    return false;
> +
>    switch (code)
>      {
>      case INTEGER_CST:
> @@ -860,9 +868,6 @@ split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
>  
>      case SSA_NAME:
>        {
> -	if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
> -	  return false;
> -
>  	gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
>  	enum tree_code subcode;
>  
>
diff mbox series

Patch

diff --git a/gcc/testsuite/gcc.dg/torture/pr116585.c b/gcc/testsuite/gcc.dg/torture/pr116585.c
new file mode 100644
index 00000000000..108c481e104
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr116585.c
@@ -0,0 +1,32 @@ 
+/* { dg-do compile } */
+
+char *s1, *s2;
+extern int* my_alloc (int);
+extern int _setjmp ();
+extern void bar();
+void foo(int s1len, int s2len)
+{
+  int e;
+  e = _setjmp ();
+    {
+      int l, i;
+      int *md = my_alloc(((sizeof(int)) * (s1len + 1) * (s2len)));
+      s1len++;
+      for (; s1len; l)
+	for (; s2len; l)
+	  for (; s1len; i)
+	    {
+	      int j = 1;
+	      for (; j < s2len; j++)
+		{
+		  int cost;
+		  if (s1[1] == s2[1])
+		    cost = 0;
+		  else
+		    cost = 1;
+		  md[j * s1len ] = ((cost));
+		}
+	    }
+      bar();
+    }
+}
diff --git a/gcc/tree-data-ref.cc b/gcc/tree-data-ref.cc
index 654a8220214..69b421f3c94 100644
--- a/gcc/tree-data-ref.cc
+++ b/gcc/tree-data-ref.cc
@@ -765,6 +765,14 @@  split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
   if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
     return false;
 
+  if (TREE_CODE (op0) == SSA_NAME
+      && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
+    return false;
+  if (op1
+      && TREE_CODE (op1) == SSA_NAME
+      && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))
+    return false;
+
   switch (code)
     {
     case INTEGER_CST:
@@ -860,9 +868,6 @@  split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
 
     case SSA_NAME:
       {
-	if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
-	  return false;
-
 	gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
 	enum tree_code subcode;