* This is a pass for removing phi nodes that look like:
  * a = phi(b, b, b, ...)
  *
- * Note that we can't ignore undef sources here, or else we may create a
+ * Note that we can't always ignore undef sources here, or else we may create a
  * situation where the definition of b isn't dominated by its uses. We're
  * allowed to do this since the definition of b must dominate all of the
  * phi node's predecessors, which means it must dominate the phi node as well
          if (def == NULL) {
             def  = src->src.ssa;
             mov = get_parent_mov(def);
+         } else if (src->src.ssa->parent_instr->type == nir_instr_type_ssa_undef &&
+                    nir_block_dominates(def->parent_instr->block, src->pred)) {
+            /* Ignore this undef source. */
          } else {
             if (src->src.ssa != def && !matching_mov(mov, src->src.ssa)) {
                srcs_same = false;
    nir_builder bld;
    nir_builder_init(&bld, impl);
 
+   nir_metadata_require(impl, nir_metadata_dominance);
+
    nir_foreach_block(block, impl) {
       progress |= remove_phis_block(block, &bld);
    }