- /* We must have found at least one definition, since there must be at
- * least one forward edge.
- */
- assert(def != NULL);
+ if (!def) {
+ /* In this case, the phi had no sources. So turn it into an undef. */
+
+ b->cursor = nir_after_phis(block);
+ def = nir_ssa_undef(b, phi->dest.ssa.num_components,
+ phi->dest.ssa.bit_size);
+ } else if (mov) {
+ /* If the sources were all movs from the same source with the same
+ * swizzle, then we can't just pick a random move because it may not
+ * dominate the phi node. Instead, we need to emit our own move after
+ * the phi which uses the shared source, and rewrite uses of the phi
+ * to use the move instead. This is ok, because while the movs may
+ * not all dominate the phi node, their shared source does.
+ */
+
+ b->cursor = nir_after_phis(block);
+ def = nir_mov_alu(b, mov->src[0], def->num_components);
+ }