nir: Move nir_lower_mediump_outputs from ir3
[mesa.git] / src / compiler / nir / nir_lower_returns.c
index 423192adb8a2286dd395f0522466c84e4cf6fe18..ecbbf43da3bc9d409d72ef4a29664778346ca4a5 100644 (file)
@@ -37,6 +37,8 @@ struct lower_returns_state {
     * needs to be predicated on the return flag variable.
     */
    bool has_predicated_return;
+
+   bool removed_unreachable_code;
 };
 
 static bool lower_returns_in_cf_list(struct exec_list *cf_list,
@@ -48,7 +50,7 @@ predicate_following(nir_cf_node *node, struct lower_returns_state *state)
    nir_builder *b = &state->builder;
    b->cursor = nir_after_cf_node_and_phis(node);
 
-   if (nir_cursors_equal(b->cursor, nir_after_cf_list(state->cf_list)))
+   if (!state->loop && nir_cursors_equal(b->cursor, nir_after_cf_list(state->cf_list)))
       return; /* Nothing to predicate */
 
    assert(state->return_flag);
@@ -162,8 +164,9 @@ lower_returns_in_block(nir_block *block, struct lower_returns_state *state)
           */
          return false;
       } else {
+         state->removed_unreachable_code = true;
          nir_cf_delete(&list);
-         return true;
+         return false;
       }
    }
 
@@ -180,6 +183,12 @@ lower_returns_in_block(nir_block *block, struct lower_returns_state *state)
 
    nir_instr_remove(&jump->instr);
 
+   /* If this is a return in the last block of the function there is nothing
+    * more to do once its removed.
+    */
+   if (block == nir_impl_last_block(state->builder.impl))
+      return true;
+
    nir_builder *b = &state->builder;
 
    /* Set the return flag */
@@ -189,11 +198,11 @@ lower_returns_in_block(nir_block *block, struct lower_returns_state *state)
 
       /* Initialize the variable to 0 */
       b->cursor = nir_before_cf_list(&b->impl->body);
-      nir_store_var(b, state->return_flag, nir_imm_int(b, NIR_FALSE), 1);
+      nir_store_var(b, state->return_flag, nir_imm_false(b), 1);
    }
 
    b->cursor = nir_after_block(block);
-   nir_store_var(b, state->return_flag, nir_imm_int(b, NIR_TRUE), 1);
+   nir_store_var(b, state->return_flag, nir_imm_true(b), 1);
 
    if (state->loop) {
       /* We're in a loop;  we need to break out of it. */
@@ -256,13 +265,19 @@ nir_lower_returns_impl(nir_function_impl *impl)
    state.loop = NULL;
    state.return_flag = NULL;
    state.has_predicated_return = false;
+   state.removed_unreachable_code = false;
    nir_builder_init(&state.builder, impl);
 
    bool progress = lower_returns_in_cf_list(&impl->body, &state);
+   progress = progress || state.removed_unreachable_code;
 
    if (progress) {
       nir_metadata_preserve(impl, nir_metadata_none);
       nir_repair_ssa_impl(impl);
+   } else {
+#ifndef NDEBUG
+      impl->valid_metadata &= ~nir_metadata_not_properly_reset;
+#endif
    }
 
    return progress;