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.

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

        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.
---
 gcc/testsuite/gcc.dg/torture/pr118171.c | 12 ++++++++++++
 gcc/tree-ssa-pre.cc                     |  8 ++++----
 2 files changed, 16 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr118171.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr118171.c 
b/gcc/testsuite/gcc.dg/torture/pr118171.c
new file mode 100644
index 00000000000..3cdf0020aaa
--- /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 c696111690f..b8deff288c1 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:
-- 
2.43.0

Reply via email to