+static void try_remove_unnecessary_phi(ir_node *phi)
+{
+ ir_node *phi_value = NULL;
+ int arity = get_irn_arity(phi);
+ int i;
+
+ /* see if all inputs are either pointing to a single value or
+ * are self references */
+ for (i = 0; i < arity; ++i) {
+ ir_node *in = get_irn_n(phi, i);
+ if (in == phi)
+ continue;
+ if (in == phi_value)
+ continue;
+ /** found a different value from the one we already found, can't remove
+ * the phi (yet) */
+ if (phi_value != NULL)
+ return;
+ phi_value = in;
+ }
+ if (phi_value == NULL)
+ return;
+
+ /* if we're here then all phi inputs have been either phi_value
+ * or self-references, we can replace the phi by phi_value.
+ * We do this with an Id-node */
+ exchange(phi, phi_value);
+
+ /* recursively check phi_value, because it could be that we were the last
+ * phi-node in a loop-body. Then our arguments is an unnecessary phi in
+ * the loop header which can be eliminated now */
+ if (is_Phi(phi_value)) {
+ try_remove_unnecessary_phi(phi_value);
+ }
+}
+