https://gcc.gnu.org/g:6488d5b86126a538ebb8560556dd462ec3d5d62e

commit r15-6504-g6488d5b86126a538ebb8560556dd462ec3d5d62e
Author: Richard Biener <rguent...@suse.de>
Date:   Wed Jan 1 16:07:03 2025 +0100

    tree-optimization/118171 - GENERIC folding in PRE results in invalid GIMPLE
    
    PRE applies GENERIC folding to some component ref components which
    might result in invalid GIMPLE, like a VIEW_CONVERT_EXPR wrapping
    a REALPART_EXPR as in the PR.  The following removes all GENERIC
    folding in the code re-constructing a GENERIC component-ref from
    the PRE VN IL.
    
            PR tree-optimization/118171
            * tree-ssa-pre.cc (create_component_ref_by_pieces_1): Do not
            fold any component ref parts.
    
            * gcc.dg/torture/pr118171.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr118171.c | 12 ++++++++++++
 gcc/tree-ssa-pre.cc                     |  8 ++++----
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr118171.c 
b/gcc/testsuite/gcc.dg/torture/pr118171.c
new file mode 100644
index 000000000000..3cdf0020aaa1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr118171.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+
+_Complex float f;
+static int d;
+
+void
+foo(char *p)
+{
+  do {
+    __builtin_memcpy(&d, 3 + p, 2);
+  } while (*(_Complex char *)&f);
+}
diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc
index fea060e51c79..32bcd9b1b7af 100644
--- a/gcc/tree-ssa-pre.cc
+++ b/gcc/tree-ssa-pre.cc
@@ -2622,7 +2622,7 @@ create_component_ref_by_pieces_1 (basic_block block, 
vn_reference_t ref,
                                                        stmts);
        if (!genop0)
          return NULL_TREE;
-       return fold_build1 (currop->opcode, currop->type, genop0);
+       return build1 (currop->opcode, currop->type, genop0);
       }
 
     case WITH_SIZE_EXPR:
@@ -2634,7 +2634,7 @@ create_component_ref_by_pieces_1 (basic_block block, 
vn_reference_t ref,
        tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
        if (!genop1)
          return NULL_TREE;
-       return fold_build2 (currop->opcode, currop->type, genop0, genop1);
+       return build2 (currop->opcode, currop->type, genop0, genop1);
       }
 
     case BIT_FIELD_REF:
@@ -2647,7 +2647,7 @@ create_component_ref_by_pieces_1 (basic_block block, 
vn_reference_t ref,
        tree op2 = currop->op1;
        tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
        REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
-       return fold (t);
+       return t;
       }
 
       /* For array ref vn_reference_op's, operand 1 of the array ref
@@ -2725,7 +2725,7 @@ create_component_ref_by_pieces_1 (basic_block block, 
vn_reference_t ref,
            if (!genop2)
              return NULL_TREE;
          }
-       return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
+       return build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
       }
 
     case SSA_NAME:

Reply via email to