aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-06-22 16:18:42 -0400
committerAndrew Kelley <andrew@ziglang.org>2019-06-22 16:18:42 -0400
commit86f362ce8e878188d40393e6f2feba0c60ddbcf0 (patch)
tree640b917eb1c5bbc1d4270a90bfd68905e68e7818
parent3c4b255a3c184e43e70bb8380f6e388c5594f149 (diff)
downloadzig-86f362ce8e878188d40393e6f2feba0c60ddbcf0.tar.gz
zig-86f362ce8e878188d40393e6f2feba0c60ddbcf0.zip
elide redundant safety check when switching on tagged unions
-rw-r--r--src/all_types.hpp1
-rw-r--r--src/codegen.cpp2
-rw-r--r--src/ir.cpp9
-rw-r--r--std/zig/render.zig6
4 files changed, 10 insertions, 8 deletions
diff --git a/src/all_types.hpp b/src/all_types.hpp
index 213a64dd9d..5b75d9f96a 100644
--- a/src/all_types.hpp
+++ b/src/all_types.hpp
@@ -2540,6 +2540,7 @@ struct IrInstructionStructFieldPtr {
struct IrInstructionUnionFieldPtr {
IrInstruction base;
+ bool safety_check_on;
bool initializing;
IrInstruction *union_ptr;
TypeUnionField *field;
diff --git a/src/codegen.cpp b/src/codegen.cpp
index 5b7169f312..4eea729dd9 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -3890,7 +3890,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
&field->enum_field->value);
gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
- } else if (ir_want_runtime_safety(g, &instruction->base)) {
+ } else if (instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base)) {
LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, union_type->data.unionation.gen_tag_index, "");
LLVMValueRef tag_value = gen_load_untyped(g, tag_field_ptr, 0, false, "");
diff --git a/src/ir.cpp b/src/ir.cpp
index e943788f97..823ddb13a3 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -1384,10 +1384,11 @@ static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, As
}
static IrInstruction *ir_build_union_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
- IrInstruction *union_ptr, TypeUnionField *field, bool initializing)
+ IrInstruction *union_ptr, TypeUnionField *field, bool safety_check_on, bool initializing)
{
IrInstructionUnionFieldPtr *instruction = ir_build_instruction<IrInstructionUnionFieldPtr>(irb, scope, source_node);
instruction->initializing = initializing;
+ instruction->safety_check_on = safety_check_on;
instruction->union_ptr = union_ptr;
instruction->field = field;
@@ -17514,7 +17515,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
IrInstruction *result;
if (ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope,
- source_instr->source_node, container_ptr, field, initializing);
+ source_instr->source_node, container_ptr, field, true, initializing);
result->value.type = ptr_type;
result->value.special = ConstValSpecialStatic;
} else {
@@ -17529,7 +17530,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
}
IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope,
- source_instr->source_node, container_ptr, field, initializing);
+ source_instr->source_node, container_ptr, field, true, initializing);
result->value.type = ptr_type;
return result;
}
@@ -19005,7 +19006,7 @@ static IrInstruction *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstru
}
IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb,
- instruction->base.scope, instruction->base.source_node, target_value_ptr, field, false);
+ instruction->base.scope, instruction->base.source_node, target_value_ptr, field, false, false);
result->value.type = get_pointer_to_type(ira->codegen, field->type_entry,
target_value_ptr->value.type->data.pointer.is_const);
return result;
diff --git a/std/zig/render.zig b/std/zig/render.zig
index ef5c8f2346..2e8e4481be 100644
--- a/std/zig/render.zig
+++ b/std/zig/render.zig
@@ -939,10 +939,10 @@ fn renderExpression(
}
switch (container_decl.init_arg_expr) {
- ast.Node.ContainerDecl.InitArg.None => {
+ .None => {
try renderToken(tree, stream, container_decl.kind_token, indent, start_col, Space.Space); // union
},
- ast.Node.ContainerDecl.InitArg.Enum => |enum_tag_type| {
+ .Enum => |enum_tag_type| {
try renderToken(tree, stream, container_decl.kind_token, indent, start_col, Space.None); // union
const lparen = tree.nextToken(container_decl.kind_token);
@@ -962,7 +962,7 @@ fn renderExpression(
try renderToken(tree, stream, tree.nextToken(enum_token), indent, start_col, Space.Space); // )
}
},
- ast.Node.ContainerDecl.InitArg.Type => |type_expr| {
+ .Type => |type_expr| {
try renderToken(tree, stream, container_decl.kind_token, indent, start_col, Space.None); // union
const lparen = tree.nextToken(container_decl.kind_token);