void construct(WeakString &wstr) { construct_key(wstr); construct_properties(wstr); if (construct_value(wstr) == true) construct_children(wstr); };
nir_alu_instr * nir_replace_instr(nir_alu_instr *instr, const nir_search_expression *search, const nir_search_value *replace, void *mem_ctx) { uint8_t swizzle[4] = { 0, 0, 0, 0 }; for (unsigned i = 0; i < instr->dest.dest.ssa.num_components; ++i) swizzle[i] = i; assert(instr->dest.dest.is_ssa); struct match_state state; state.variables_seen = 0; if (!match_expression(search, instr, instr->dest.dest.ssa.num_components, swizzle, &state)) return NULL; /* Inserting a mov may be unnecessary. However, it's much easier to * simply let copy propagation clean this up than to try to go through * and rewrite swizzles ourselves. */ nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov); mov->dest.write_mask = instr->dest.write_mask; nir_ssa_dest_init(&mov->instr, &mov->dest.dest, instr->dest.dest.ssa.num_components, NULL); mov->src[0] = construct_value(replace, nir_op_infos[instr->op].output_type, instr->dest.dest.ssa.num_components, &state, &instr->instr, mem_ctx); nir_instr_insert_before(&instr->instr, &mov->instr); nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(&mov->dest.dest.ssa), mem_ctx); /* We know this one has no more uses because we just rewrote them all, * so we can remove it. The rest of the matched expression, however, we * don't know so much about. We'll just let dead code clean them up. */ nir_instr_remove(&instr->instr); return mov; }
struct expr *eval_binary_op(struct evaluator_state *state, struct expr* e, struct env_node *env) { assert(e->type == EXPR_BINARY); switch (e->binary_op.op) { case BIN_MEMBER: { // if left is union: // return eval(x.right_ident for x in left_union) // else: // return lookup(left_obj, right_ident) assert(e->binary_op.right->type == EXPR_IDENT); struct expr *left = eval_footprint_expr(state, e->binary_op.left, env); if (left->type == EXPR_UNION) { char *loop_var_name = new_ident_not_in(env, "loop_var"); struct expr *loop_var_ident = expr_new_with(e->direction, EXPR_IDENT); loop_var_ident->ident = loop_var_name; struct expr *loop_body = expr_new(); memcpy(loop_body, e, sizeof(struct expr)); loop_body->binary_op.left = loop_var_ident; struct expr *loop = expr_new_with(e->direction, EXPR_FOR); loop->for_loop.body = loop_body; loop->for_loop.ident = loop_var_name; loop->for_loop.over = left; return eval_footprint_expr(state, loop, env); } else if (left->type == EXPR_OBJECT) { return lookup_in_object(&left->object, e->binary_op.right->ident, e->direction); } else { assert(false); } } break; case BIN_APP: { struct expr *left = eval_footprint_expr(state, e->binary_op.left, env); struct expr *right = e->binary_op.right; assert(left->type == EXPR_FUNCTION); assert(right->type == EXPR_FUNCTION_ARGS); struct function func = left->func; struct env_node *function_env = env; struct string_node *current_arg_name = func.args; struct union_node *current_arg_value = e->binary_op.right->unioned; while (current_arg_value != NULL) { assert(current_arg_name != NULL); // not too many arguments struct expr *e = eval_footprint_expr(state, current_arg_value->expr, env); function_env = env_new_with(current_arg_name->value, e, function_env); current_arg_name = current_arg_name->next; current_arg_value = current_arg_value->next; } assert(current_arg_name == NULL); // not too few arguments function_env = env_new_with(func.name, construct_function(func, e->direction), function_env); struct expr *new_expr = expr_clone(func.expr); set_direction_recursive(new_expr, e->direction); return eval_footprint_expr(state, new_expr, function_env); } break; default: { int64_t left, right; _Bool left_success, right_success; struct expr *partial_left, *partial_right; left_success = eval_to_value(state, e->binary_op.left, env, &partial_left, &left); right_success = eval_to_value(state, e->binary_op.right, env, &partial_right, &right); if (!left_success || !right_success) { // cache miss, state modified struct expr *new_expr = expr_clone(e); if (left_success) { new_expr->binary_op.left = construct_value(left, e->direction); } else { new_expr->binary_op.left = partial_left; } if (right_success) { new_expr->binary_op.right = construct_value(right, e->direction); } else { new_expr->binary_op.right = partial_right; } return new_expr; } switch (e->binary_op.op) { case BIN_GT: { return construct_value(left > right ? 1 : 0, e->direction); } break; case BIN_LT: { return construct_value(left < right ? 1 : 0, e->direction); } break; case BIN_GTE: { return construct_value(left >= right ? 1 : 0, e->direction); } break; case BIN_LTE: { return construct_value(left <= right ? 1 : 0, e->direction); } break; case BIN_EQ: { return construct_value(left == right ? 1 : 0, e->direction); } break; case BIN_NE: { return construct_value(left != right ? 1 : 0, e->direction); } break; case BIN_AND: { return construct_value(!!left && !!right ? 1 : 0, e->direction); } break; case BIN_OR: { return construct_value(!!left || !!right ? 1 : 0, e->direction); } break; case BIN_ADD: { return construct_value(left + right, e->direction); } break; case BIN_SUB: { return construct_value(left - right, e->direction); } break; case BIN_MUL: { return construct_value(left * right, e->direction); } break; case BIN_DIV: { return construct_value(left / right, e->direction); } break; case BIN_MOD: { return construct_value(left % right, e->direction); } break; case BIN_SHL: { return construct_value(left << right, e->direction); } break; case BIN_SHR: { return construct_value(left >> right, e->direction); } break; case BIN_BITAND: { return construct_value(left & right, e->direction); } break; case BIN_BITOR: { return construct_value(left | right, e->direction); } break; case BIN_BITXOR: { return construct_value(left ^ right, e->direction); } break; default: assert(false); break; } } break; } }
static nir_alu_src construct_value(const nir_search_value *value, nir_alu_type type, unsigned num_components, struct match_state *state, nir_instr *instr, void *mem_ctx) { switch (value->type) { case nir_search_value_expression: { const nir_search_expression *expr = nir_search_value_as_expression(value); if (nir_op_infos[expr->opcode].output_size != 0) num_components = nir_op_infos[expr->opcode].output_size; nir_alu_instr *alu = nir_alu_instr_create(mem_ctx, expr->opcode); nir_ssa_dest_init(&alu->instr, &alu->dest.dest, num_components, NULL); alu->dest.write_mask = (1 << num_components) - 1; alu->dest.saturate = false; for (unsigned i = 0; i < nir_op_infos[expr->opcode].num_inputs; i++) { /* If the source is an explicitly sized source, then we need to reset * the number of components to match. */ if (nir_op_infos[alu->op].input_sizes[i] != 0) num_components = nir_op_infos[alu->op].input_sizes[i]; alu->src[i] = construct_value(expr->srcs[i], nir_op_infos[alu->op].input_types[i], num_components, state, instr, mem_ctx); } nir_instr_insert_before(instr, &alu->instr); nir_alu_src val; val.src = nir_src_for_ssa(&alu->dest.dest.ssa); val.negate = false; val.abs = false, memcpy(val.swizzle, identity_swizzle, sizeof val.swizzle); return val; } case nir_search_value_variable: { const nir_search_variable *var = nir_search_value_as_variable(value); assert(state->variables_seen & (1 << var->variable)); nir_alu_src val = { NIR_SRC_INIT }; nir_alu_src_copy(&val, &state->variables[var->variable], mem_ctx); assert(!var->is_constant); return val; } case nir_search_value_constant: { const nir_search_constant *c = nir_search_value_as_constant(value); nir_load_const_instr *load = nir_load_const_instr_create(mem_ctx, 1); switch (type) { case nir_type_float: load->def.name = ralloc_asprintf(mem_ctx, "%f", c->data.f); load->value.f[0] = c->data.f; break; case nir_type_int: load->def.name = ralloc_asprintf(mem_ctx, "%d", c->data.i); load->value.i[0] = c->data.i; break; case nir_type_unsigned: case nir_type_bool: load->value.u[0] = c->data.u; break; default: unreachable("Invalid alu source type"); } nir_instr_insert_before(instr, &load->instr); nir_alu_src val; val.src = nir_src_for_ssa(&load->def); val.negate = false; val.abs = false, memset(val.swizzle, 0, sizeof val.swizzle); return val; } default: unreachable("Invalid search value type"); } }