Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary calls to Crystal::CodeGenVisitor#union_type_and_value_pointer #15491

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/compiler/crystal/codegen/cast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/crystal/codegen/unions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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