summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Ekstrand <[email protected]>2014-12-05 12:05:55 -0800
committerJason Ekstrand <[email protected]>2015-01-15 07:20:21 -0800
commit6a5604ca6a7346278188cb05996444a5091070b5 (patch)
tree137cc28b944849b0f115f467ed85fd21eeef0456
parentcd4b995254fe29bae9ab5a9563cc615274d361ed (diff)
nir: Constant fold array indirects
Reviewed-by: Connor Abbott <[email protected]>
-rw-r--r--src/glsl/nir/nir_opt_constant_folding.c84
1 files changed, 76 insertions, 8 deletions
diff --git a/src/glsl/nir/nir_opt_constant_folding.c b/src/glsl/nir/nir_opt_constant_folding.c
index 61f672337d0..6ffa694a34f 100644
--- a/src/glsl/nir/nir_opt_constant_folding.c
+++ b/src/glsl/nir/nir_opt_constant_folding.c
@@ -49,9 +49,8 @@ struct constant_fold_state {
} \
static bool
-constant_fold_alu_instr(nir_alu_instr *instr, void *void_state)
+constant_fold_alu_instr(nir_alu_instr *instr, void *mem_ctx)
{
- struct constant_fold_state *state = void_state;
nir_load_const_instr *src[4], *dest;
if (!instr->dest.dest.is_ssa)
@@ -73,7 +72,7 @@ constant_fold_alu_instr(nir_alu_instr *instr, void *void_state)
/* We shouldn't have any saturate modifiers in the optimization loop. */
assert(!instr->dest.saturate);
- dest = nir_load_const_instr_create(state->mem_ctx);
+ dest = nir_load_const_instr_create(mem_ctx);
dest->array_elems = 0;
dest->num_components = instr->dest.dest.ssa.num_components;
@@ -228,7 +227,7 @@ constant_fold_alu_instr(nir_alu_instr *instr, void *void_state)
.ssa = &dest->dest.ssa,
};
- nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, new_src, state->mem_ctx);
+ nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, new_src, mem_ctx);
nir_instr_remove(&instr->instr);
ralloc_free(instr);
@@ -237,15 +236,84 @@ constant_fold_alu_instr(nir_alu_instr *instr, void *void_state)
}
static bool
+constant_fold_deref(nir_instr *instr, nir_deref_var *deref)
+{
+ bool progress = false;
+
+ for (nir_deref *tail = deref->deref.child; tail; tail = tail->child) {
+ if (tail->deref_type != nir_deref_type_array)
+ continue;
+
+ nir_deref_array *arr = nir_deref_as_array(tail);
+
+ if (arr->deref_array_type == nir_deref_array_type_indirect &&
+ arr->indirect.is_ssa &&
+ arr->indirect.ssa->parent_instr->type == nir_instr_type_load_const) {
+ nir_load_const_instr *indirect =
+ nir_instr_as_load_const(arr->indirect.ssa->parent_instr);
+
+ arr->base_offset += indirect->value.u[0];
+
+ nir_src empty = {
+ .is_ssa = true,
+ .ssa = NULL,
+ };
+
+ nir_instr_rewrite_src(instr, &arr->indirect, empty);
+
+ arr->deref_array_type = nir_deref_array_type_direct;
+
+ progress = true;
+ }
+ }
+
+ return progress;
+}
+
+static bool
+constant_fold_intrinsic_instr(nir_intrinsic_instr *instr)
+{
+ bool progress = false;
+
+ unsigned num_vars = nir_intrinsic_infos[instr->intrinsic].num_variables;
+ for (unsigned i = 0; i < num_vars; i++) {
+ progress |= constant_fold_deref(&instr->instr, instr->variables[i]);
+ }
+
+ return progress;
+}
+
+static bool
+constant_fold_tex_instr(nir_tex_instr *instr)
+{
+ if (instr->sampler)
+ return constant_fold_deref(&instr->instr, instr->sampler);
+ else
+ return false;
+}
+
+static bool
constant_fold_block(nir_block *block, void *void_state)
{
struct constant_fold_state *state = void_state;
nir_foreach_instr_safe(block, instr) {
- if (instr->type != nir_instr_type_alu)
- continue;
-
- state->progress |= constant_fold_alu_instr(nir_instr_as_alu(instr), state);
+ switch (instr->type) {
+ case nir_instr_type_alu:
+ state->progress |= constant_fold_alu_instr(nir_instr_as_alu(instr),
+ state->mem_ctx);
+ break;
+ case nir_instr_type_intrinsic:
+ state->progress |=
+ constant_fold_intrinsic_instr(nir_instr_as_intrinsic(instr));
+ break;
+ case nir_instr_type_tex:
+ state->progress |= constant_fold_tex_instr(nir_instr_as_tex(instr));
+ break;
+ default:
+ /* Don't know how to constant fold */
+ break;
+ }
}
return true;