authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-18 13:37:49-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-18 14:04:02-07:00
loga6f5aa71ac1e8c9a27ecfa6a50f0445a50545a5d
treefe02d07bcf03618bc4769fab9c6090a27b4792a1
parentac7217e1f5ff15a6fc8248b9c1c651b318f472ad

stage1: small IR memory optimization on CheckSwitchProngs


3 files changed, 35 insertions(+), 20 deletions(-)

src/stage1/all_types.hpp+2-2
...@@ -2610,7 +2610,8 @@ enum IrInstSrcId {...@@ -2610,7 +2610,8 @@ enum IrInstSrcId {
2610 IrInstSrcIdEnumToInt,2610 IrInstSrcIdEnumToInt,
2611 IrInstSrcIdIntToErr,2611 IrInstSrcIdIntToErr,
2612 IrInstSrcIdErrToInt,2612 IrInstSrcIdErrToInt,
2613 IrInstSrcIdCheckSwitchProngs,2613 IrInstSrcIdCheckSwitchProngsUnderYes,
2614 IrInstSrcIdCheckSwitchProngsUnderNo,
2614 IrInstSrcIdCheckStatementIsVoid,2615 IrInstSrcIdCheckStatementIsVoid,
2615 IrInstSrcIdTypeName,2616 IrInstSrcIdTypeName,
2616 IrInstSrcIdDeclRef,2617 IrInstSrcIdDeclRef,
...@@ -4021,7 +4022,6 @@ struct IrInstSrcCheckSwitchProngs {...@@ -4021,7 +4022,6 @@ struct IrInstSrcCheckSwitchProngs {
4021 IrInstSrcCheckSwitchProngsRange *ranges;4022 IrInstSrcCheckSwitchProngsRange *ranges;
4022 size_t range_count;4023 size_t range_count;
4023 AstNode* else_prong;4024 AstNode* else_prong;
4024 bool have_underscore_prong;
4025};4025};
40264026
4027struct IrInstSrcCheckStatementIsVoid {4027struct IrInstSrcCheckStatementIsVoid {
src/stage1/ir.cpp+19-13
...@@ -476,7 +476,8 @@ static void destroy_instruction_src(IrInstSrc *inst) {...@@ -476,7 +476,8 @@ static void destroy_instruction_src(IrInstSrc *inst) {
476 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcIntToErr *>(inst));476 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcIntToErr *>(inst));
477 case IrInstSrcIdErrToInt:477 case IrInstSrcIdErrToInt:
478 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcErrToInt *>(inst));478 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcErrToInt *>(inst));
479 case IrInstSrcIdCheckSwitchProngs:479 case IrInstSrcIdCheckSwitchProngsUnderNo:
480 case IrInstSrcIdCheckSwitchProngsUnderYes:
480 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCheckSwitchProngs *>(inst));481 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCheckSwitchProngs *>(inst));
481 case IrInstSrcIdCheckStatementIsVoid:482 case IrInstSrcIdCheckStatementIsVoid:
482 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCheckStatementIsVoid *>(inst));483 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCheckStatementIsVoid *>(inst));
...@@ -1471,10 +1472,6 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcErrToInt *) {...@@ -1471,10 +1472,6 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcErrToInt *) {
1471 return IrInstSrcIdErrToInt;1472 return IrInstSrcIdErrToInt;
1472}1473}
14731474
1474static constexpr IrInstSrcId ir_inst_id(IrInstSrcCheckSwitchProngs *) {
1475 return IrInstSrcIdCheckSwitchProngs;
1476}
1477
1478static constexpr IrInstSrcId ir_inst_id(IrInstSrcCheckStatementIsVoid *) {1475static constexpr IrInstSrcId ir_inst_id(IrInstSrcCheckStatementIsVoid *) {
1479 return IrInstSrcIdCheckStatementIsVoid;1476 return IrInstSrcIdCheckStatementIsVoid;
1480}1477}
...@@ -4351,13 +4348,19 @@ static IrInstSrc *ir_build_check_switch_prongs(IrBuilderSrc *irb, Scope *scope,...@@ -4351,13 +4348,19 @@ static IrInstSrc *ir_build_check_switch_prongs(IrBuilderSrc *irb, Scope *scope,
4351 IrInstSrc *target_value, IrInstSrcCheckSwitchProngsRange *ranges, size_t range_count,4348 IrInstSrc *target_value, IrInstSrcCheckSwitchProngsRange *ranges, size_t range_count,
4352 AstNode* else_prong, bool have_underscore_prong)4349 AstNode* else_prong, bool have_underscore_prong)
4353{4350{
4354 IrInstSrcCheckSwitchProngs *instruction = ir_build_instruction<IrInstSrcCheckSwitchProngs>(4351 IrInstSrcCheckSwitchProngs *instruction = heap::c_allocator.create<IrInstSrcCheckSwitchProngs>();
4355 irb, scope, source_node);4352 instruction->base.id = have_underscore_prong ?
4353 IrInstSrcIdCheckSwitchProngsUnderYes : IrInstSrcIdCheckSwitchProngsUnderNo;
4354 instruction->base.base.scope = scope;
4355 instruction->base.base.source_node = source_node;
4356 instruction->base.base.debug_id = exec_next_debug_id(irb->exec);
4357 instruction->base.owner_bb = irb->current_basic_block;
4358 ir_instruction_append(irb->current_basic_block, &instruction->base);
4359
4356 instruction->target_value = target_value;4360 instruction->target_value = target_value;
4357 instruction->ranges = ranges;4361 instruction->ranges = ranges;
4358 instruction->range_count = range_count;4362 instruction->range_count = range_count;
4359 instruction->else_prong = else_prong;4363 instruction->else_prong = else_prong;
4360 instruction->have_underscore_prong = have_underscore_prong;
43614364
4362 ir_ref_instruction(target_value, irb->current_basic_block);4365 ir_ref_instruction(target_value, irb->current_basic_block);
4363 for (size_t i = 0; i < range_count; i += 1) {4366 for (size_t i = 0; i < range_count; i += 1) {
...@@ -29706,7 +29709,7 @@ static IrInstGen *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrInstSrc...@@ -29706,7 +29709,7 @@ static IrInstGen *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrInstSrc
29706}29709}
2970729710
29708static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,29711static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
29709 IrInstSrcCheckSwitchProngs *instruction)29712 IrInstSrcCheckSwitchProngs *instruction, bool have_underscore_prong)
29710{29713{
29711 IrInstGen *target_value = instruction->target_value->child;29714 IrInstGen *target_value = instruction->target_value->child;
29712 ZigType *switch_type = target_value->value->type;29715 ZigType *switch_type = target_value->value->type;
...@@ -29771,7 +29774,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,...@@ -29771,7 +29774,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
29771 bigint_incr(&field_index);29774 bigint_incr(&field_index);
29772 }29775 }
29773 }29776 }
29774 if (instruction->have_underscore_prong) {29777 if (have_underscore_prong) {
29775 if (!switch_type->data.enumeration.non_exhaustive) {29778 if (!switch_type->data.enumeration.non_exhaustive) {
29776 ir_add_error(ira, &instruction->base.base,29779 ir_add_error(ira, &instruction->base.base,
29777 buf_sprintf("switch on exhaustive enum has `_` prong"));29780 buf_sprintf("switch on exhaustive enum has `_` prong"));
...@@ -32347,8 +32350,10 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc...@@ -32347,8 +32350,10 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc
32347 return ir_analyze_instruction_fn_proto(ira, (IrInstSrcFnProto *)instruction);32350 return ir_analyze_instruction_fn_proto(ira, (IrInstSrcFnProto *)instruction);
32348 case IrInstSrcIdTestComptime:32351 case IrInstSrcIdTestComptime:
32349 return ir_analyze_instruction_test_comptime(ira, (IrInstSrcTestComptime *)instruction);32352 return ir_analyze_instruction_test_comptime(ira, (IrInstSrcTestComptime *)instruction);
32350 case IrInstSrcIdCheckSwitchProngs:32353 case IrInstSrcIdCheckSwitchProngsUnderNo:
32351 return ir_analyze_instruction_check_switch_prongs(ira, (IrInstSrcCheckSwitchProngs *)instruction);32354 return ir_analyze_instruction_check_switch_prongs(ira, (IrInstSrcCheckSwitchProngs *)instruction, false);
32355 case IrInstSrcIdCheckSwitchProngsUnderYes:
32356 return ir_analyze_instruction_check_switch_prongs(ira, (IrInstSrcCheckSwitchProngs *)instruction, true);
32352 case IrInstSrcIdCheckStatementIsVoid:32357 case IrInstSrcIdCheckStatementIsVoid:
32353 return ir_analyze_instruction_check_statement_is_void(ira, (IrInstSrcCheckStatementIsVoid *)instruction);32358 return ir_analyze_instruction_check_statement_is_void(ira, (IrInstSrcCheckStatementIsVoid *)instruction);
32354 case IrInstSrcIdDeclRef:32359 case IrInstSrcIdDeclRef:
...@@ -32745,7 +32750,8 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) {...@@ -32745,7 +32750,8 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) {
32745 case IrInstSrcIdMemcpy:32750 case IrInstSrcIdMemcpy:
32746 case IrInstSrcIdBreakpoint:32751 case IrInstSrcIdBreakpoint:
32747 case IrInstSrcIdOverflowOp: // TODO when we support multiple returns this can be side effect free32752 case IrInstSrcIdOverflowOp: // TODO when we support multiple returns this can be side effect free
32748 case IrInstSrcIdCheckSwitchProngs:32753 case IrInstSrcIdCheckSwitchProngsUnderNo:
32754 case IrInstSrcIdCheckSwitchProngsUnderYes:
32749 case IrInstSrcIdCheckStatementIsVoid:32755 case IrInstSrcIdCheckStatementIsVoid:
32750 case IrInstSrcIdCheckRuntimeScope:32756 case IrInstSrcIdCheckRuntimeScope:
32751 case IrInstSrcIdPanic:32757 case IrInstSrcIdPanic:
src/stage1/ir_print.cpp+14-5
...@@ -270,8 +270,10 @@ const char* ir_inst_src_type_str(IrInstSrcId id) {...@@ -270,8 +270,10 @@ const char* ir_inst_src_type_str(IrInstSrcId id) {
270 return "SrcIntToErr";270 return "SrcIntToErr";
271 case IrInstSrcIdErrToInt:271 case IrInstSrcIdErrToInt:
272 return "SrcErrToInt";272 return "SrcErrToInt";
273 case IrInstSrcIdCheckSwitchProngs:273 case IrInstSrcIdCheckSwitchProngsUnderNo:
274 return "SrcCheckSwitchProngs";274 return "SrcCheckSwitchProngsUnderNo";
275 case IrInstSrcIdCheckSwitchProngsUnderYes:
276 return "SrcCheckSwitchProngsUnderYes";
275 case IrInstSrcIdCheckStatementIsVoid:277 case IrInstSrcIdCheckStatementIsVoid:
276 return "SrcCheckStatementIsVoid";278 return "SrcCheckStatementIsVoid";
277 case IrInstSrcIdTypeName:279 case IrInstSrcIdTypeName:
...@@ -2189,7 +2191,9 @@ static void ir_print_err_to_int(IrPrintGen *irp, IrInstGenErrToInt *instruction)...@@ -2189,7 +2191,9 @@ static void ir_print_err_to_int(IrPrintGen *irp, IrInstGenErrToInt *instruction)
2189 ir_print_other_inst_gen(irp, instruction->target);2191 ir_print_other_inst_gen(irp, instruction->target);
2190}2192}
21912193
2192static void ir_print_check_switch_prongs(IrPrintSrc *irp, IrInstSrcCheckSwitchProngs *instruction) {2194static void ir_print_check_switch_prongs(IrPrintSrc *irp, IrInstSrcCheckSwitchProngs *instruction,
2195 bool have_underscore_prong)
2196{
2193 fprintf(irp->f, "@checkSwitchProngs(");2197 fprintf(irp->f, "@checkSwitchProngs(");
2194 ir_print_other_inst_src(irp, instruction->target_value);2198 ir_print_other_inst_src(irp, instruction->target_value);
2195 fprintf(irp->f, ",");2199 fprintf(irp->f, ",");
...@@ -2202,6 +2206,8 @@ static void ir_print_check_switch_prongs(IrPrintSrc *irp, IrInstSrcCheckSwitchPr...@@ -2202,6 +2206,8 @@ static void ir_print_check_switch_prongs(IrPrintSrc *irp, IrInstSrcCheckSwitchPr
2202 }2206 }
2203 const char *have_else_str = instruction->else_prong != nullptr ? "yes" : "no";2207 const char *have_else_str = instruction->else_prong != nullptr ? "yes" : "no";
2204 fprintf(irp->f, ")else:%s", have_else_str);2208 fprintf(irp->f, ")else:%s", have_else_str);
2209 const char *have_under_str = have_underscore_prong ? "yes" : "no";
2210 fprintf(irp->f, " _:%s", have_under_str);
2205}2211}
22062212
2207static void ir_print_check_statement_is_void(IrPrintSrc *irp, IrInstSrcCheckStatementIsVoid *instruction) {2213static void ir_print_check_statement_is_void(IrPrintSrc *irp, IrInstSrcCheckStatementIsVoid *instruction) {
...@@ -2893,8 +2899,11 @@ static void ir_print_inst_src(IrPrintSrc *irp, IrInstSrc *instruction, bool trai...@@ -2893,8 +2899,11 @@ static void ir_print_inst_src(IrPrintSrc *irp, IrInstSrc *instruction, bool trai
2893 case IrInstSrcIdErrToInt:2899 case IrInstSrcIdErrToInt:
2894 ir_print_err_to_int(irp, (IrInstSrcErrToInt *)instruction);2900 ir_print_err_to_int(irp, (IrInstSrcErrToInt *)instruction);
2895 break;2901 break;
2896 case IrInstSrcIdCheckSwitchProngs:2902 case IrInstSrcIdCheckSwitchProngsUnderNo:
2897 ir_print_check_switch_prongs(irp, (IrInstSrcCheckSwitchProngs *)instruction);2903 ir_print_check_switch_prongs(irp, (IrInstSrcCheckSwitchProngs *)instruction, false);
2904 break;
2905 case IrInstSrcIdCheckSwitchProngsUnderYes:
2906 ir_print_check_switch_prongs(irp, (IrInstSrcCheckSwitchProngs *)instruction, true);
2898 break;2907 break;
2899 case IrInstSrcIdCheckStatementIsVoid:2908 case IrInstSrcIdCheckStatementIsVoid:
2900 ir_print_check_statement_is_void(irp, (IrInstSrcCheckStatementIsVoid *)instruction);2909 ir_print_check_statement_is_void(irp, (IrInstSrcCheckStatementIsVoid *)instruction);