From e7bda09e9317e6f02339a702171c87bf4f1077cf Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Wed, 19 Feb 2025 03:37:32 +0800 Subject: [PATCH] Remove unnecessary calls to `Crystal::CodeGenVisitor#union_type_and_value_pointer` --- src/compiler/crystal/codegen/cast.cr | 12 ++++++------ src/compiler/crystal/codegen/codegen.cr | 4 ++-- src/compiler/crystal/codegen/unions.cr | 3 ++- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/compiler/crystal/codegen/cast.cr b/src/compiler/crystal/codegen/cast.cr index 9f8111a5a4dc..4c26aa37bc8d 100644 --- a/src/compiler/crystal/codegen/cast.cr +++ b/src/compiler/crystal/codegen/cast.cr @@ -196,7 +196,7 @@ class Crystal::CodeGenVisitor end def assign_distinct(target_pointer, target_type : VirtualType, value_type : MixedUnionType, value) - _, union_value_ptr = union_type_and_value_pointer(value, value_type) + union_value_ptr = union_value(llvm_type(value_type), value) casted_value = cast_to_pointer(union_value_ptr, target_type) store load(llvm_type(target_type), casted_value), target_pointer end @@ -211,7 +211,7 @@ class Crystal::CodeGenVisitor def assign_distinct(target_pointer, target_type : VirtualMetaclassType, value_type : UnionType, value) # Can happen when assigning Foo+.class <- Bar.class | Baz.class with Bar < Foo and Baz < Foo - _, union_value_ptr = union_type_and_value_pointer(value, value_type) + union_value_ptr = union_value(llvm_type(value_type), value) casted_value = cast_to_pointer(union_value_ptr, target_type) store load(llvm_type(target_type), casted_value), target_pointer end @@ -269,7 +269,7 @@ class Crystal::CodeGenVisitor # target_type is Proc(*T, Nil) and all the types in the union are Proc(*T, R). # In that case we can simply get the union value and cast it to the target type. # Cast of a non-void proc to a void proc - _, union_value_ptr = union_type_and_value_pointer(value, value_type) + union_value_ptr = union_value(llvm_type(value_type), value) value = cast_to_pointer(union_value_ptr, target_type) store load(llvm_type(target_type), value), target_pointer end @@ -415,12 +415,12 @@ class Crystal::CodeGenVisitor end def downcast_distinct(value, to_type : NilableType, from_type : MixedUnionType) - _, value_ptr = union_type_and_value_pointer(value, from_type) + value_ptr = union_value(llvm_type(from_type), value) load(llvm_type(to_type), cast_to_pointer(value_ptr, to_type)) end def downcast_distinct(value, to_type : BoolType, from_type : MixedUnionType) - _, value_ptr = union_type_and_value_pointer(value, from_type) + value_ptr = union_value(llvm_type(from_type), value) value = cast_to_pointer(value_ptr, @program.int8) value = load(llvm_context.int8, value) trunc value, llvm_context.int1 @@ -439,7 +439,7 @@ class Crystal::CodeGenVisitor end end - _, value_ptr = union_type_and_value_pointer(value, from_type) + value_ptr = union_value(llvm_type(from_type), value) value = cast_to_pointer(value_ptr, to_type) to_lhs value, to_type end diff --git a/src/compiler/crystal/codegen/codegen.cr b/src/compiler/crystal/codegen/codegen.cr index e4d05ba4aaae..ed6cb39bc541 100644 --- a/src/compiler/crystal/codegen/codegen.cr +++ b/src/compiler/crystal/codegen/codegen.cr @@ -2429,9 +2429,9 @@ module Crystal target_type = type if type.is_a?(VirtualType) if type.struct? - if (_type = type.remove_indirection).is_a?(UnionType) + if (actual_type = type.remove_indirection).is_a?(UnionType) # For a struct we need to cast the second part of the union to the base type - _, value_ptr = union_type_and_value_pointer(pointer, _type) + value_ptr = union_value(llvm_type(actual_type), pointer) target_type = type.base_type pointer = cast_to_pointer value_ptr, target_type else diff --git a/src/compiler/crystal/codegen/unions.cr b/src/compiler/crystal/codegen/unions.cr index fdf1d81a4c95..04e93d28be57 100644 --- a/src/compiler/crystal/codegen/unions.cr +++ b/src/compiler/crystal/codegen/unions.cr @@ -179,7 +179,8 @@ module Crystal end private def type_id_impl(value, type : MixedUnionType) - union_type_and_value_pointer(value, type)[0] + struct_type = llvm_type(type) + load(llvm_context.int32, union_type_id(struct_type, value)) end end end