47043d
diff --git a/gcc/testsuite/g++.dg/torture/phi-1.C b/gcc/testsuite/g++.dg/torture/phi-1.C
47043d
new file mode 100644
47043d
index 00000000000..69fb3d7ba38
47043d
--- /dev/null
47043d
+++ b/gcc/testsuite/g++.dg/torture/phi-1.C
47043d
@@ -0,0 +1,28 @@
47043d
+// { dg-do compile { target c++11 } }
47043d
+// { dg-options "--param early-inlining-insns=14" }
47043d
+
47043d
+struct Element;
47043d
+template <int _Nm> struct __array_traits { typedef Element _Type[_Nm]; };
47043d
+template <int _Nm> struct array {
47043d
+  typename __array_traits<_Nm>::_Type _M_elems;
47043d
+};
47043d
+bool logLevel();
47043d
+struct LogCapture {
47043d
+  void stream();
47043d
+};
47043d
+struct Element {
47043d
+  Element();
47043d
+  long data_;
47043d
+};
47043d
+using ElementArray = array<6>;
47043d
+struct ElementManager {
47043d
+  ElementManager();
47043d
+  ElementArray array_;
47043d
+};
47043d
+static ElementArray makeArray() {
47043d
+  if (logLevel())
47043d
+    LogCapture().stream();
47043d
+  ElementArray foo;
47043d
+  return foo;
47043d
+}
47043d
+ElementManager::ElementManager() : array_(makeArray()) {}
47043d
diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c
47043d
index 84e58e66628..78c0c6a4189 100644
47043d
--- a/gcc/tree-cfg.c
47043d
+++ b/gcc/tree-cfg.c
47043d
@@ -2944,35 +2944,6 @@ last_and_only_stmt (basic_block bb)
47043d
     return NULL;
47043d
 }
47043d
 
47043d
-/* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE.  */
47043d
-
47043d
-static void
47043d
-reinstall_phi_args (edge new_edge, edge old_edge)
47043d
-{
47043d
-  edge_var_map *vm;
47043d
-  int i;
47043d
-  gphi_iterator phis;
47043d
-
47043d
-  vec<edge_var_map> *v = redirect_edge_var_map_vector (old_edge);
47043d
-  if (!v)
47043d
-    return;
47043d
-
47043d
-  for (i = 0, phis = gsi_start_phis (new_edge->dest);
47043d
-       v->iterate (i, &vm) && !gsi_end_p (phis);
47043d
-       i++, gsi_next (&phis))
47043d
-    {
47043d
-      gphi *phi = phis.phi ();
47043d
-      tree result = redirect_edge_var_map_result (vm);
47043d
-      tree arg = redirect_edge_var_map_def (vm);
47043d
-
47043d
-      gcc_assert (result == gimple_phi_result (phi));
47043d
-
47043d
-      add_phi_arg (phi, arg, new_edge, redirect_edge_var_map_location (vm));
47043d
-    }
47043d
-
47043d
-  redirect_edge_var_map_clear (old_edge);
47043d
-}
47043d
-
47043d
 /* Returns the basic block after which the new basic block created
47043d
    by splitting edge EDGE_IN should be placed.  Tries to keep the new block
47043d
    near its "logical" location.  This is of most help to humans looking
47043d
@@ -3012,11 +2983,24 @@ gimple_split_edge (edge edge_in)
47043d
   new_bb = create_empty_bb (after_bb);
47043d
   new_bb->count = edge_in->count ();
47043d
 
47043d
-  e = redirect_edge_and_branch (edge_in, new_bb);
47043d
-  gcc_assert (e == edge_in);
47043d
-
47043d
+  /* We want to avoid re-allocating PHIs when we first
47043d
+     add the fallthru edge from new_bb to dest but we also
47043d
+     want to avoid changing PHI argument order when
47043d
+     first redirecting edge_in away from dest.  The former
47043d
+     avoids changing PHI argument order by adding them
47043d
+     last and then the redirection swapping it back into
47043d
+     place by means of unordered remove.
47043d
+     So hack around things by temporarily removing all PHIs
47043d
+     from the destination during the edge redirection and then
47043d
+     making sure the edges stay in order.  */
47043d
+  gimple_seq saved_phis = phi_nodes (dest);
47043d
+  unsigned old_dest_idx = edge_in->dest_idx;
47043d
+  set_phi_nodes (dest, NULL);
47043d
   new_edge = make_single_succ_edge (new_bb, dest, EDGE_FALLTHRU);
47043d
-  reinstall_phi_args (new_edge, e);
47043d
+  e = redirect_edge_and_branch (edge_in, new_bb);
47043d
+  gcc_assert (e == edge_in && new_edge->dest_idx == old_dest_idx);
47043d
+  /* set_phi_nodes sets the BB of the PHI nodes, so do it manually here.  */
47043d
+  dest->il.gimple.phi_nodes = saved_phis;
47043d
 
47043d
   return new_bb;
47043d
 }