+/* After we've cloned almost everything, we have to walk the list of phi
+ * sources and fix them up. Thanks to loops, the block and SSA value for a
+ * phi source may not be defined when we first encounter it. Instead, we
+ * add it to the phi_srcs list and we fix it up here.
+ */
+static void
+fixup_phi_srcs(clone_state *state)
+{
+ list_for_each_entry_safe(nir_phi_src, src, &state->phi_srcs, src.use_link) {
+ src->pred = remap_local(state, src->pred);
+
+ /* Remove from this list */
+ list_del(&src->src.use_link);
+
+ if (src->src.is_ssa) {
+ src->src.ssa = remap_local(state, src->src.ssa);
+ list_addtail(&src->src.use_link, &src->src.ssa->uses);
+ } else {
+ src->src.reg.reg = remap_reg(state, src->src.reg.reg);
+ list_addtail(&src->src.use_link, &src->src.reg.reg->uses);
+ }
+ }
+ assert(list_empty(&state->phi_srcs));
+}
+
+void
+nir_cf_list_clone(nir_cf_list *dst, nir_cf_list *src, nir_cf_node *parent,
+ struct hash_table *remap_table)
+{
+ exec_list_make_empty(&dst->list);
+ dst->impl = src->impl;
+
+ if (exec_list_is_empty(&src->list))
+ return;
+
+ clone_state state;
+ init_clone_state(&state, remap_table, false, true);
+
+ /* We use the same shader */
+ state.ns = src->impl->function->shader;
+
+ /* The control-flow code assumes that the list of cf_nodes always starts
+ * and ends with a block. We start by adding an empty block.
+ */
+ nir_block *nblk = nir_block_create(state.ns);
+ nblk->cf_node.parent = parent;
+ exec_list_push_tail(&dst->list, &nblk->cf_node.node);
+
+ clone_cf_list(&state, &dst->list, &src->list);
+
+ fixup_phi_srcs(&state);
+}
+