cp_gimplify_r doesn't always get to see the contents of a CONSTRUCTOR, so it can miss lowering PTRMEM_CSTs there. cp_genericize_r does look into CONSTRUCTORs, and lowering them earlier is fine.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit fff4c8804414258331785f08435259ecfdb7311c
Author: Jason Merrill <ja...@redhat.com>
Date:   Tue Apr 12 17:00:42 2016 -0400

    	PR c++/70615
    	* cp-gimplify.c (cp_genericize_r): Expand PTRMEM_CST here.
    	(cp_gimplify_expr): Not here.

diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 13f7b7c..30ac660 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -576,11 +576,6 @@ cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
 
   switch (code)
     {
-    case PTRMEM_CST:
-      *expr_p = cplus_expand_constant (*expr_p);
-      ret = GS_OK;
-      break;
-
     case AGGR_INIT_EXPR:
       simplify_aggr_init_expr (expr_p);
       ret = GS_OK;
@@ -1388,6 +1383,13 @@ cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
 	   || TREE_CODE (stmt) == OMP_SIMD
 	   || TREE_CODE (stmt) == OMP_DISTRIBUTE)
     genericize_omp_for_stmt (stmt_p, walk_subtrees, data);
+  else if (TREE_CODE (stmt) == PTRMEM_CST)
+    {
+      /* By the time we get here we're handing off to the back end, so we don't
+	 need or want to preserve PTRMEM_CST anymore.  */
+      *stmt_p = cplus_expand_constant (stmt);
+      *walk_subtrees = 0;
+    }
   else if ((flag_sanitize
 	    & (SANITIZE_NULL | SANITIZE_ALIGNMENT | SANITIZE_VPTR))
 	   && !wtd->no_sanitize_p)
diff --git a/gcc/testsuite/g++.dg/opt/ptrmem7.C b/gcc/testsuite/g++.dg/opt/ptrmem7.C
new file mode 100644
index 0000000..7d9e9b1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/ptrmem7.C
@@ -0,0 +1,31 @@
+// PR c++/70615
+// { dg-options -O }
+
+struct C
+{
+  virtual void f () {}
+};
+
+struct B
+{
+  virtual ~B () {}
+};
+
+class D : public B, public C
+{
+public:
+  D () {}
+};
+
+typedef void (C::*FP) ();
+typedef void (D::*D_f) ();
+
+int
+main ()
+{
+  D *d = new D ();
+  C *c = d;
+  const FP fptr = (FP) & D::f;
+  (d->*(D_f) fptr) ();
+  return 0;
+}

Reply via email to