shader->stage = stage;
+ shader->gs.vertices_out = 0;
+ shader->gs.invocations = 0;
+
return shader;
}
}
}
-void nir_dest_copy(nir_dest *dest, const nir_dest *src, void *mem_ctx)
+void nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr)
{
/* Copying an SSA definition makes no sense whatsoever. */
assert(!src->is_ssa);
dest->reg.base_offset = src->reg.base_offset;
dest->reg.reg = src->reg.reg;
if (src->reg.indirect) {
- dest->reg.indirect = ralloc(mem_ctx, nir_src);
- nir_src_copy(dest->reg.indirect, src->reg.indirect, mem_ctx);
+ dest->reg.indirect = ralloc(instr, nir_src);
+ nir_src_copy(dest->reg.indirect, src->reg.indirect, instr);
} else {
dest->reg.indirect = NULL;
}
}
void
-nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src, void *mem_ctx)
+nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
+ nir_alu_instr *instr)
{
- nir_src_copy(&dest->src, &src->src, mem_ctx);
+ nir_src_copy(&dest->src, &src->src, &instr->instr);
dest->abs = src->abs;
dest->negate = src->negate;
for (unsigned i = 0; i < 4; i++)
}
void
-nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src, void *mem_ctx)
+nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
+ nir_alu_instr *instr)
{
- nir_dest_copy(&dest->dest, &src->dest, mem_ctx);
+ nir_dest_copy(&dest->dest, &src->dest, &instr->instr);
dest->write_mask = src->write_mask;
dest->saturate = src->saturate;
}
}
void
-nir_instr_insert_before(nir_instr *instr, nir_instr *before)
-{
- assert(before->type != nir_instr_type_jump);
- before->block = instr->block;
- add_defs_uses(before);
- exec_node_insert_node_before(&instr->node, &before->node);
-}
-
-void
-nir_instr_insert_after(nir_instr *instr, nir_instr *after)
-{
- if (after->type == nir_instr_type_jump) {
- assert(instr == nir_block_last_instr(instr->block));
- assert(instr->type != nir_instr_type_jump);
- }
-
- after->block = instr->block;
- add_defs_uses(after);
- exec_node_insert_after(&instr->node, &after->node);
-
- if (after->type == nir_instr_type_jump)
- nir_handle_add_jump(after->block);
-}
-
-void
-nir_instr_insert_before_block(nir_block *block, nir_instr *before)
+nir_instr_insert(nir_cursor cursor, nir_instr *instr)
{
- if (before->type == nir_instr_type_jump)
- assert(exec_list_is_empty(&block->instr_list));
+ switch (cursor.option) {
+ case nir_cursor_before_block:
+ /* Only allow inserting jumps into empty blocks. */
+ if (instr->type == nir_instr_type_jump)
+ assert(exec_list_is_empty(&cursor.block->instr_list));
- before->block = block;
- add_defs_uses(before);
- exec_list_push_head(&block->instr_list, &before->node);
-
- if (before->type == nir_instr_type_jump)
- nir_handle_add_jump(block);
-}
-
-void
-nir_instr_insert_after_block(nir_block *block, nir_instr *after)
-{
- if (after->type == nir_instr_type_jump) {
- assert(exec_list_is_empty(&block->instr_list) ||
- nir_block_last_instr(block)->type != nir_instr_type_jump);
- }
-
- after->block = block;
- add_defs_uses(after);
- exec_list_push_tail(&block->instr_list, &after->node);
-
- if (after->type == nir_instr_type_jump)
- nir_handle_add_jump(block);
-}
-
-void
-nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before)
-{
- if (node->type == nir_cf_node_block) {
- nir_instr_insert_before_block(nir_cf_node_as_block(node), before);
- } else {
- nir_cf_node *prev = nir_cf_node_prev(node);
- assert(prev->type == nir_cf_node_block);
- nir_block *prev_block = nir_cf_node_as_block(prev);
-
- nir_instr_insert_before_block(prev_block, before);
+ instr->block = cursor.block;
+ add_defs_uses(instr);
+ exec_list_push_head(&cursor.block->instr_list, &instr->node);
+ break;
+ case nir_cursor_after_block: {
+ /* Inserting instructions after a jump is illegal. */
+ nir_instr *last = nir_block_last_instr(cursor.block);
+ assert(last == NULL || last->type != nir_instr_type_jump);
+ (void) last;
+
+ instr->block = cursor.block;
+ add_defs_uses(instr);
+ exec_list_push_tail(&cursor.block->instr_list, &instr->node);
+ break;
}
-}
+ case nir_cursor_before_instr:
+ assert(instr->type != nir_instr_type_jump);
+ instr->block = cursor.instr->block;
+ add_defs_uses(instr);
+ exec_node_insert_node_before(&cursor.instr->node, &instr->node);
+ break;
+ case nir_cursor_after_instr:
+ /* Inserting instructions after a jump is illegal. */
+ assert(cursor.instr->type != nir_instr_type_jump);
-void
-nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after)
-{
- if (node->type == nir_cf_node_block) {
- nir_instr_insert_after_block(nir_cf_node_as_block(node), after);
- } else {
- nir_cf_node *next = nir_cf_node_next(node);
- assert(next->type == nir_cf_node_block);
- nir_block *next_block = nir_cf_node_as_block(next);
+ /* Only allow inserting jumps at the end of the block. */
+ if (instr->type == nir_instr_type_jump)
+ assert(cursor.instr == nir_block_last_instr(cursor.instr->block));
- nir_instr_insert_before_block(next_block, after);
+ instr->block = cursor.instr->block;
+ add_defs_uses(instr);
+ exec_node_insert_after(&cursor.instr->node, &instr->node);
+ break;
}
-}
-void
-nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before)
-{
- nir_cf_node *first_node = exec_node_data(nir_cf_node,
- exec_list_get_head(list), node);
- nir_instr_insert_before_cf(first_node, before);
+ if (instr->type == nir_instr_type_jump)
+ nir_handle_add_jump(instr->block);
}
-void
-nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after)
+static bool
+src_is_valid(const nir_src *src)
{
- nir_cf_node *last_node = exec_node_data(nir_cf_node,
- exec_list_get_tail(list), node);
- nir_instr_insert_after_cf(last_node, after);
+ return src->is_ssa ? (src->ssa != NULL) : (src->reg.reg != NULL);
}
static bool
remove_use_cb(nir_src *src, void *state)
{
- list_del(&src->use_link);
+ if (src_is_valid(src))
+ list_del(&src->use_link);
return true;
}
}
}
-static bool
-src_is_valid(const nir_src *src)
-{
- return src->is_ssa ? (src->ssa != NULL) : (src->reg.reg != NULL);
-}
-
static void
src_remove_all_uses(nir_src *src)
{
src_add_all_uses(src, NULL, if_stmt);
}
+void
+nir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest, nir_dest new_dest)
+{
+ if (dest->is_ssa) {
+ /* We can only overwrite an SSA destination if it has no uses. */
+ assert(list_empty(&dest->ssa.uses) && list_empty(&dest->ssa.if_uses));
+ } else {
+ list_del(&dest->reg.def_link);
+ if (dest->reg.indirect)
+ src_remove_all_uses(dest->reg.indirect);
+ }
+
+ /* We can't re-write with an SSA def */
+ assert(!new_dest.is_ssa);
+
+ nir_dest_copy(dest, &new_dest, instr);
+
+ dest->reg.parent_instr = instr;
+ list_addtail(&dest->reg.def_link, &new_dest.reg.reg->defs);
+
+ if (dest->reg.indirect)
+ src_add_all_uses(dest->reg.indirect, instr, NULL);
+}
+
void
nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
unsigned num_components, const char *name)
}
void
-nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx)
+nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src)
{
assert(!new_src.is_ssa || def != new_src.ssa);
nir_foreach_use_safe(def, use_src) {
nir_instr *src_parent_instr = use_src->parent_instr;
list_del(&use_src->use_link);
- nir_src_copy(use_src, &new_src, mem_ctx);
+ nir_src_copy(use_src, &new_src, src_parent_instr);
src_add_all_uses(use_src, src_parent_instr, NULL);
}
nir_foreach_if_use_safe(def, use_src) {
nir_if *src_parent_if = use_src->parent_if;
list_del(&use_src->use_link);
- nir_src_copy(use_src, &new_src, mem_ctx);
+ nir_src_copy(use_src, &new_src, src_parent_if);
src_add_all_uses(use_src, NULL, src_parent_if);
}
}
return false;
}
+bool
+nir_foreach_block_in_cf_node(nir_cf_node *node, nir_foreach_block_cb cb,
+ void *state)
+{
+ return foreach_cf_node(node, cb, false, state);
+}
+
bool
nir_foreach_block(nir_function_impl *impl, nir_foreach_block_cb cb, void *state)
{
return nir_cf_node_as_if(next_node);
}
+nir_loop *
+nir_block_get_following_loop(nir_block *block)
+{
+ if (exec_node_is_tail_sentinel(&block->cf_node.node))
+ return NULL;
+
+ if (nir_cf_node_is_last(&block->cf_node))
+ return NULL;
+
+ nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
+
+ if (next_node->type != nir_cf_node_loop)
+ return NULL;
+
+ return nir_cf_node_as_loop(next_node);
+}
static bool
index_block(nir_block *block, void *state)
{
return true;
}
+/**
+ * The indices are applied top-to-bottom which has the very nice property
+ * that, if A dominates B, then A->index <= B->index.
+ */
void
nir_index_ssa_defs(nir_function_impl *impl)
{
nir_foreach_block(impl, index_ssa_block, &index);
impl->ssa_alloc = index;
}
+
+static bool
+index_instrs_block(nir_block *block, void *state)
+{
+ unsigned *index = state;
+ nir_foreach_instr(block, instr)
+ instr->index = (*index)++;
+
+ return true;
+}
+
+/**
+ * The indices are applied top-to-bottom which has the very nice property
+ * that, if A dominates B, then A->index <= B->index.
+ */
+unsigned
+nir_index_instrs(nir_function_impl *impl)
+{
+ unsigned index = 0;
+ nir_foreach_block(impl, index_instrs_block, &index);
+ return index;
+}
+
+nir_intrinsic_op
+nir_intrinsic_from_system_value(gl_system_value val)
+{
+ switch (val) {
+ case SYSTEM_VALUE_VERTEX_ID:
+ return nir_intrinsic_load_vertex_id;
+ case SYSTEM_VALUE_INSTANCE_ID:
+ return nir_intrinsic_load_instance_id;
+ case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
+ return nir_intrinsic_load_vertex_id_zero_base;
+ case SYSTEM_VALUE_BASE_VERTEX:
+ return nir_intrinsic_load_base_vertex;
+ case SYSTEM_VALUE_INVOCATION_ID:
+ return nir_intrinsic_load_invocation_id;
+ case SYSTEM_VALUE_FRONT_FACE:
+ return nir_intrinsic_load_front_face;
+ case SYSTEM_VALUE_SAMPLE_ID:
+ return nir_intrinsic_load_sample_id;
+ case SYSTEM_VALUE_SAMPLE_POS:
+ return nir_intrinsic_load_sample_pos;
+ case SYSTEM_VALUE_SAMPLE_MASK_IN:
+ return nir_intrinsic_load_sample_mask_in;
+ case SYSTEM_VALUE_LOCAL_INVOCATION_ID:
+ return nir_intrinsic_load_local_invocation_id;
+ case SYSTEM_VALUE_WORK_GROUP_ID:
+ return nir_intrinsic_load_work_group_id;
+ case SYSTEM_VALUE_NUM_WORK_GROUPS:
+ return nir_intrinsic_load_num_work_groups;
+ /* FINISHME: Add tessellation intrinsics.
+ case SYSTEM_VALUE_TESS_COORD:
+ case SYSTEM_VALUE_VERTICES_IN:
+ case SYSTEM_VALUE_PRIMITIVE_ID:
+ case SYSTEM_VALUE_TESS_LEVEL_OUTER:
+ case SYSTEM_VALUE_TESS_LEVEL_INNER:
+ */
+ default:
+ unreachable("system value does not directly correspond to intrinsic");
+ }
+}
+
+gl_system_value
+nir_system_value_from_intrinsic(nir_intrinsic_op intrin)
+{
+ switch (intrin) {
+ case nir_intrinsic_load_vertex_id:
+ return SYSTEM_VALUE_VERTEX_ID;
+ case nir_intrinsic_load_instance_id:
+ return SYSTEM_VALUE_INSTANCE_ID;
+ case nir_intrinsic_load_vertex_id_zero_base:
+ return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
+ case nir_intrinsic_load_base_vertex:
+ return SYSTEM_VALUE_BASE_VERTEX;
+ case nir_intrinsic_load_invocation_id:
+ return SYSTEM_VALUE_INVOCATION_ID;
+ case nir_intrinsic_load_front_face:
+ return SYSTEM_VALUE_FRONT_FACE;
+ case nir_intrinsic_load_sample_id:
+ return SYSTEM_VALUE_SAMPLE_ID;
+ case nir_intrinsic_load_sample_pos:
+ return SYSTEM_VALUE_SAMPLE_POS;
+ case nir_intrinsic_load_sample_mask_in:
+ return SYSTEM_VALUE_SAMPLE_MASK_IN;
+ case nir_intrinsic_load_local_invocation_id:
+ return SYSTEM_VALUE_LOCAL_INVOCATION_ID;
+ case nir_intrinsic_load_num_work_groups:
+ return SYSTEM_VALUE_NUM_WORK_GROUPS;
+ case nir_intrinsic_load_work_group_id:
+ return SYSTEM_VALUE_WORK_GROUP_ID;
+ /* FINISHME: Add tessellation intrinsics.
+ return SYSTEM_VALUE_TESS_COORD;
+ return SYSTEM_VALUE_VERTICES_IN;
+ return SYSTEM_VALUE_PRIMITIVE_ID;
+ return SYSTEM_VALUE_TESS_LEVEL_OUTER;
+ return SYSTEM_VALUE_TESS_LEVEL_INNER;
+ */
+ default:
+ unreachable("intrinsic doesn't produce a system value");
+ }
+}