authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-22 16:18:42-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-22 16:18:42-04:00
log86f362ce8e878188d40393e6f2feba0c60ddbcf0
tree640b917eb1c5bbc1d4270a90bfd68905e68e7818
parent3c4b255a3c184e43e70bb8380f6e388c5594f149
signature Commit is signed but in an unrecognized format.

elide redundant safety check when switching on tagged unions


4 files changed, 10 insertions(+), 8 deletions(-)

src/all_types.hpp+1
......@@ -2540,6 +2540,7 @@ struct IrInstructionStructFieldPtr {
25402540struct IrInstructionUnionFieldPtr {
25412541 IrInstruction base;
25422542
2543 bool safety_check_on;
25432544 bool initializing;
25442545 IrInstruction *union_ptr;
25452546 TypeUnionField *field;
src/codegen.cpp+1-1
......@@ -3890,7 +3890,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
38903890 LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
38913891 &field->enum_field->value);
38923892 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
3893 } else if (ir_want_runtime_safety(g, &instruction->base)) {
3893 } else if (instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base)) {
38943894 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, union_type->data.unionation.gen_tag_index, "");
38953895 LLVMValueRef tag_value = gen_load_untyped(g, tag_field_ptr, 0, false, "");
38963896
src/ir.cpp+5-4
......@@ -1384,10 +1384,11 @@ static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, As
13841384}
13851385
13861386static IrInstruction *ir_build_union_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1387 IrInstruction *union_ptr, TypeUnionField *field, bool initializing)
1387 IrInstruction *union_ptr, TypeUnionField *field, bool safety_check_on, bool initializing)
13881388{
13891389 IrInstructionUnionFieldPtr *instruction = ir_build_instruction<IrInstructionUnionFieldPtr>(irb, scope, source_node);
13901390 instruction->initializing = initializing;
1391 instruction->safety_check_on = safety_check_on;
13911392 instruction->union_ptr = union_ptr;
13921393 instruction->field = field;
13931394
......@@ -17514,7 +17515,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1751417515 IrInstruction *result;
1751517516 if (ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
1751617517 result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope,
17517 source_instr->source_node, container_ptr, field, initializing);
17518 source_instr->source_node, container_ptr, field, true, initializing);
1751817519 result->value.type = ptr_type;
1751917520 result->value.special = ConstValSpecialStatic;
1752017521 } else {
......@@ -17529,7 +17530,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1752917530 }
1753017531
1753117532 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope,
17532 source_instr->source_node, container_ptr, field, initializing);
17533 source_instr->source_node, container_ptr, field, true, initializing);
1753317534 result->value.type = ptr_type;
1753417535 return result;
1753517536 }
......@@ -19005,7 +19006,7 @@ static IrInstruction *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstru
1900519006 }
1900619007
1900719008 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb,
19008 instruction->base.scope, instruction->base.source_node, target_value_ptr, field, false);
19009 instruction->base.scope, instruction->base.source_node, target_value_ptr, field, false, false);
1900919010 result->value.type = get_pointer_to_type(ira->codegen, field->type_entry,
1901019011 target_value_ptr->value.type->data.pointer.is_const);
1901119012 return result;
std/zig/render.zig+3-3
......@@ -939,10 +939,10 @@ fn renderExpression(
939939 }
940940
941941 switch (container_decl.init_arg_expr) {
942 ast.Node.ContainerDecl.InitArg.None => {
942 .None => {
943943 try renderToken(tree, stream, container_decl.kind_token, indent, start_col, Space.Space); // union
944944 },
945 ast.Node.ContainerDecl.InitArg.Enum => |enum_tag_type| {
945 .Enum => |enum_tag_type| {
946946 try renderToken(tree, stream, container_decl.kind_token, indent, start_col, Space.None); // union
947947
948948 const lparen = tree.nextToken(container_decl.kind_token);
......@@ -962,7 +962,7 @@ fn renderExpression(
962962 try renderToken(tree, stream, tree.nextToken(enum_token), indent, start_col, Space.Space); // )
963963 }
964964 },
965 ast.Node.ContainerDecl.InitArg.Type => |type_expr| {
965 .Type => |type_expr| {
966966 try renderToken(tree, stream, container_decl.kind_token, indent, start_col, Space.None); // union
967967
968968 const lparen = tree.nextToken(container_decl.kind_token);