| ... | @@ -15945,54 +15945,154 @@ static Stage1AirInst *ir_analyze_instruction_optional_unwrap_ptr(IrAnalyze *ira, | ... | @@ -15945,54 +15945,154 @@ static Stage1AirInst *ir_analyze_instruction_optional_unwrap_ptr(IrAnalyze *ira, |
| 15945 | } | 15945 | } |
| 15946 | | 15946 | |
| 15947 | static Stage1AirInst *ir_analyze_instruction_ctz(IrAnalyze *ira, Stage1ZirInstCtz *instruction) { | 15947 | static Stage1AirInst *ir_analyze_instruction_ctz(IrAnalyze *ira, Stage1ZirInstCtz *instruction) { |
| | 15948 | Error err; |
| | 15949 | |
| 15948 | ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child); | 15950 | ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child); |
| 15949 | if (type_is_invalid(int_type)) | 15951 | if (type_is_invalid(int_type)) |
| 15950 | return ira->codegen->invalid_inst_gen; | 15952 | return ira->codegen->invalid_inst_gen; |
| 15951 | | 15953 | |
| 15952 | Stage1AirInst *op = ir_implicit_cast(ira, instruction->op->child, int_type); | 15954 | Stage1AirInst *uncasted_op = instruction->op->child; |
| | 15955 | if (type_is_invalid(uncasted_op->value->type)) |
| | 15956 | return ira->codegen->invalid_inst_gen; |
| | 15957 | |
| | 15958 | uint32_t vector_len = UINT32_MAX; // means not a vector |
| | 15959 | if (uncasted_op->value->type->id == ZigTypeIdArray) { |
| | 15960 | bool can_be_vec_elem; |
| | 15961 | if ((err = is_valid_vector_elem_type(ira->codegen, uncasted_op->value->type->data.array.child_type, |
| | 15962 | &can_be_vec_elem))) |
| | 15963 | { |
| | 15964 | return ira->codegen->invalid_inst_gen; |
| | 15965 | } |
| | 15966 | if (can_be_vec_elem) { |
| | 15967 | vector_len = uncasted_op->value->type->data.array.len; |
| | 15968 | } |
| | 15969 | } else if (uncasted_op->value->type->id == ZigTypeIdVector) { |
| | 15970 | vector_len = uncasted_op->value->type->data.vector.len; |
| | 15971 | } |
| | 15972 | |
| | 15973 | bool is_vector = (vector_len != UINT32_MAX); |
| | 15974 | ZigType *op_type = is_vector ? get_vector_type(ira->codegen, vector_len, int_type) : int_type; |
| | 15975 | |
| | 15976 | Stage1AirInst *op = ir_implicit_cast(ira, uncasted_op, op_type); |
| 15953 | if (type_is_invalid(op->value->type)) | 15977 | if (type_is_invalid(op->value->type)) |
| 15954 | return ira->codegen->invalid_inst_gen; | 15978 | return ira->codegen->invalid_inst_gen; |
| 15955 | | 15979 | |
| 15956 | if (int_type->data.integral.bit_count == 0) | 15980 | if (int_type->data.integral.bit_count == 0) |
| 15957 | return ir_const_unsigned(ira, instruction->base.scope, instruction->base.source_node, 0); | 15981 | return ir_const_unsigned(ira, instruction->base.scope, instruction->base.source_node, 0); |
| 15958 | | 15982 | |
| | 15983 | ZigType *smallest_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count); |
| | 15984 | |
| 15959 | if (instr_is_comptime(op)) { | 15985 | if (instr_is_comptime(op)) { |
| 15960 | ZigValue *val = ir_resolve_const(ira, op, UndefOk); | 15986 | ZigValue *val = ir_resolve_const(ira, op, UndefOk); |
| 15961 | if (val == nullptr) | 15987 | if (val == nullptr) |
| 15962 | return ira->codegen->invalid_inst_gen; | 15988 | return ira->codegen->invalid_inst_gen; |
| 15963 | if (val->special == ConstValSpecialUndef) | 15989 | if (val->special == ConstValSpecialUndef) |
| 15964 | return ir_const_undef(ira, instruction->base.scope, instruction->base.source_node, ira->codegen->builtin_types.entry_num_lit_int); | 15990 | return ir_const_undef(ira, instruction->base.scope, instruction->base.source_node, ira->codegen->builtin_types.entry_num_lit_int); |
| 15965 | size_t result_usize = bigint_ctz(&op->value->data.x_bigint, int_type->data.integral.bit_count); | 15991 | |
| 15966 | return ir_const_unsigned(ira, instruction->base.scope, instruction->base.source_node, result_usize); | 15992 | if (is_vector) { |
| | 15993 | ZigType *smallest_vec_type = get_vector_type(ira->codegen, vector_len, smallest_type); |
| | 15994 | Stage1AirInst *result = ir_const(ira, instruction->base.scope, instruction->base.source_node, smallest_vec_type); |
| | 15995 | expand_undef_array(ira->codegen, val); |
| | 15996 | result->value->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(smallest_vec_type->data.vector.len); |
| | 15997 | for (unsigned i = 0; i < smallest_vec_type->data.vector.len; i += 1) { |
| | 15998 | ZigValue *op_elem_val = &val->data.x_array.data.s_none.elements[i]; |
| | 15999 | if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, instruction->base.source_node, |
| | 16000 | op_elem_val, UndefOk))) |
| | 16001 | { |
| | 16002 | return ira->codegen->invalid_inst_gen; |
| | 16003 | } |
| | 16004 | ZigValue *result_elem_val = &result->value->data.x_array.data.s_none.elements[i]; |
| | 16005 | result_elem_val->type = smallest_type; |
| | 16006 | result_elem_val->special = op_elem_val->special; |
| | 16007 | if (op_elem_val->special == ConstValSpecialUndef) |
| | 16008 | continue; |
| | 16009 | size_t value = bigint_ctz(&op_elem_val->data.x_bigint, int_type->data.integral.bit_count); |
| | 16010 | bigint_init_unsigned(&result->value->data.x_array.data.s_none.elements[i].data.x_bigint, value); |
| | 16011 | } |
| | 16012 | return result; |
| | 16013 | } else { |
| | 16014 | size_t result_usize = bigint_ctz(&op->value->data.x_bigint, int_type->data.integral.bit_count); |
| | 16015 | return ir_const_unsigned(ira, instruction->base.scope, instruction->base.source_node, result_usize); |
| | 16016 | } |
| 15967 | } | 16017 | } |
| 15968 | | 16018 | |
| 15969 | ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count); | 16019 | ZigType *return_type = is_vector ? get_vector_type(ira->codegen, vector_len, smallest_type) : smallest_type; |
| 15970 | return ir_build_ctz_gen(ira, instruction->base.scope, instruction->base.source_node, return_type, op); | 16020 | return ir_build_ctz_gen(ira, instruction->base.scope, instruction->base.source_node, return_type, op); |
| 15971 | } | 16021 | } |
| 15972 | | 16022 | |
| 15973 | static Stage1AirInst *ir_analyze_instruction_clz(IrAnalyze *ira, Stage1ZirInstClz *instruction) { | 16023 | static Stage1AirInst *ir_analyze_instruction_clz(IrAnalyze *ira, Stage1ZirInstClz *instruction) { |
| | 16024 | Error err; |
| | 16025 | |
| 15974 | ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child); | 16026 | ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child); |
| 15975 | if (type_is_invalid(int_type)) | 16027 | if (type_is_invalid(int_type)) |
| 15976 | return ira->codegen->invalid_inst_gen; | 16028 | return ira->codegen->invalid_inst_gen; |
| 15977 | | 16029 | |
| 15978 | Stage1AirInst *op = ir_implicit_cast(ira, instruction->op->child, int_type); | 16030 | Stage1AirInst *uncasted_op = instruction->op->child; |
| | 16031 | if (type_is_invalid(uncasted_op->value->type)) |
| | 16032 | return ira->codegen->invalid_inst_gen; |
| | 16033 | |
| | 16034 | uint32_t vector_len = UINT32_MAX; // means not a vector |
| | 16035 | if (uncasted_op->value->type->id == ZigTypeIdArray) { |
| | 16036 | bool can_be_vec_elem; |
| | 16037 | if ((err = is_valid_vector_elem_type(ira->codegen, uncasted_op->value->type->data.array.child_type, |
| | 16038 | &can_be_vec_elem))) |
| | 16039 | { |
| | 16040 | return ira->codegen->invalid_inst_gen; |
| | 16041 | } |
| | 16042 | if (can_be_vec_elem) { |
| | 16043 | vector_len = uncasted_op->value->type->data.array.len; |
| | 16044 | } |
| | 16045 | } else if (uncasted_op->value->type->id == ZigTypeIdVector) { |
| | 16046 | vector_len = uncasted_op->value->type->data.vector.len; |
| | 16047 | } |
| | 16048 | |
| | 16049 | bool is_vector = (vector_len != UINT32_MAX); |
| | 16050 | ZigType *op_type = is_vector ? get_vector_type(ira->codegen, vector_len, int_type) : int_type; |
| | 16051 | |
| | 16052 | Stage1AirInst *op = ir_implicit_cast(ira, uncasted_op, op_type); |
| 15979 | if (type_is_invalid(op->value->type)) | 16053 | if (type_is_invalid(op->value->type)) |
| 15980 | return ira->codegen->invalid_inst_gen; | 16054 | return ira->codegen->invalid_inst_gen; |
| 15981 | | 16055 | |
| 15982 | if (int_type->data.integral.bit_count == 0) | 16056 | if (int_type->data.integral.bit_count == 0) |
| 15983 | return ir_const_unsigned(ira, instruction->base.scope, instruction->base.source_node, 0); | 16057 | return ir_const_unsigned(ira, instruction->base.scope, instruction->base.source_node, 0); |
| 15984 | | 16058 | |
| | 16059 | ZigType *smallest_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count); |
| | 16060 | |
| 15985 | if (instr_is_comptime(op)) { | 16061 | if (instr_is_comptime(op)) { |
| 15986 | ZigValue *val = ir_resolve_const(ira, op, UndefOk); | 16062 | ZigValue *val = ir_resolve_const(ira, op, UndefOk); |
| 15987 | if (val == nullptr) | 16063 | if (val == nullptr) |
| 15988 | return ira->codegen->invalid_inst_gen; | 16064 | return ira->codegen->invalid_inst_gen; |
| 15989 | if (val->special == ConstValSpecialUndef) | 16065 | if (val->special == ConstValSpecialUndef) |
| 15990 | return ir_const_undef(ira, instruction->base.scope, instruction->base.source_node, ira->codegen->builtin_types.entry_num_lit_int); | 16066 | return ir_const_undef(ira, instruction->base.scope, instruction->base.source_node, ira->codegen->builtin_types.entry_num_lit_int); |
| 15991 | size_t result_usize = bigint_clz(&op->value->data.x_bigint, int_type->data.integral.bit_count); | 16067 | |
| 15992 | return ir_const_unsigned(ira, instruction->base.scope, instruction->base.source_node, result_usize); | 16068 | if (is_vector) { |
| | 16069 | ZigType *smallest_vec_type = get_vector_type(ira->codegen, vector_len, smallest_type); |
| | 16070 | Stage1AirInst *result = ir_const(ira, instruction->base.scope, instruction->base.source_node, smallest_vec_type); |
| | 16071 | expand_undef_array(ira->codegen, val); |
| | 16072 | result->value->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(smallest_vec_type->data.vector.len); |
| | 16073 | for (unsigned i = 0; i < smallest_vec_type->data.vector.len; i += 1) { |
| | 16074 | ZigValue *op_elem_val = &val->data.x_array.data.s_none.elements[i]; |
| | 16075 | if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, instruction->base.source_node, |
| | 16076 | op_elem_val, UndefOk))) |
| | 16077 | { |
| | 16078 | return ira->codegen->invalid_inst_gen; |
| | 16079 | } |
| | 16080 | ZigValue *result_elem_val = &result->value->data.x_array.data.s_none.elements[i]; |
| | 16081 | result_elem_val->type = smallest_type; |
| | 16082 | result_elem_val->special = op_elem_val->special; |
| | 16083 | if (op_elem_val->special == ConstValSpecialUndef) |
| | 16084 | continue; |
| | 16085 | size_t value = bigint_clz(&op_elem_val->data.x_bigint, int_type->data.integral.bit_count); |
| | 16086 | bigint_init_unsigned(&result->value->data.x_array.data.s_none.elements[i].data.x_bigint, value); |
| | 16087 | } |
| | 16088 | return result; |
| | 16089 | } else { |
| | 16090 | size_t result_usize = bigint_clz(&op->value->data.x_bigint, int_type->data.integral.bit_count); |
| | 16091 | return ir_const_unsigned(ira, instruction->base.scope, instruction->base.source_node, result_usize); |
| | 16092 | } |
| 15993 | } | 16093 | } |
| 15994 | | 16094 | |
| 15995 | ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count); | 16095 | ZigType *return_type = is_vector ? get_vector_type(ira->codegen, vector_len, smallest_type) : smallest_type; |
| 15996 | return ir_build_clz_gen(ira, instruction->base.scope, instruction->base.source_node, return_type, op); | 16096 | return ir_build_clz_gen(ira, instruction->base.scope, instruction->base.source_node, return_type, op); |
| 15997 | } | 16097 | } |
| 15998 | | 16098 | |