authorgravatar for vvvv0932313@gmail.comshwqf <vvvv0932313@gmail.com> 2022-12-17 00:13:18+08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-05-12 09:04:03+03:00
loge46d7a3699da654a2cca4b48c4408bc7c4208398
tree3347749525af1ca88e343bb721a09959e1f929de
parent8de2f77f3be72b7c89f4691ddd1e70196ebe5d6c

Optimize access of array member in a structure.


1 files changed, 18 insertions(+), 3 deletions(-)

src/codegen/llvm.zig+18-3
...@@ -5865,19 +5865,34 @@ pub const FuncGen = struct {...@@ -5865,19 +5865,34 @@ pub const FuncGen = struct {
5865 const array_ty = self.air.typeOf(bin_op.lhs);5865 const array_ty = self.air.typeOf(bin_op.lhs);
5866 const array_llvm_val = try self.resolveInst(bin_op.lhs);5866 const array_llvm_val = try self.resolveInst(bin_op.lhs);
5867 const rhs = try self.resolveInst(bin_op.rhs);5867 const rhs = try self.resolveInst(bin_op.rhs);
5868 const array_llvm_ty = try self.dg.lowerType(array_ty);
5869 const elem_ty = array_ty.childType();
5868 if (isByRef(array_ty)) {5870 if (isByRef(array_ty)) {
5869 const array_llvm_ty = try self.dg.lowerType(array_ty);
5870 const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), rhs };5871 const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), rhs };
5871 const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
5872 const elem_ty = array_ty.childType();
5873 if (isByRef(elem_ty)) {5872 if (isByRef(elem_ty)) {
5873 const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
5874 if (canElideLoad(self, body_tail))5874 if (canElideLoad(self, body_tail))
5875 return elem_ptr;5875 return elem_ptr;
58765876
5877 const target = self.dg.module.getTarget();5877 const target = self.dg.module.getTarget();
5878 return self.loadByRef(elem_ptr, elem_ty, elem_ty.abiAlignment(target), false);5878 return self.loadByRef(elem_ptr, elem_ty, elem_ty.abiAlignment(target), false);
5879 } else {5879 } else {
5880 const lhs_index = Air.refToIndex(bin_op.lhs).?;
5880 const elem_llvm_ty = try self.dg.lowerType(elem_ty);5881 const elem_llvm_ty = try self.dg.lowerType(elem_ty);
5882 if (self.air.instructions.items(.tag)[lhs_index] == .load) {
5883 const load_data = self.air.instructions.items(.data)[lhs_index];
5884 const load_ptr = load_data.ty_op.operand;
5885 const load_ptr_tag = self.air.instructions.items(.tag)[Air.refToIndex(load_ptr).?];
5886 switch (load_ptr_tag) {
5887 .struct_field_ptr, .struct_field_ptr_index_0, .struct_field_ptr_index_1, .struct_field_ptr_index_2, .struct_field_ptr_index_3 => {
5888 const load_ptr_inst = try self.resolveInst(load_ptr);
5889 const gep = self.builder.buildInBoundsGEP(array_llvm_ty, load_ptr_inst, &indices, indices.len, "");
5890 return self.builder.buildLoad(elem_llvm_ty, gep, "");
5891 },
5892 else => {},
5893 }
5894 }
5895 const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
5881 return self.builder.buildLoad(elem_llvm_ty, elem_ptr, "");5896 return self.builder.buildLoad(elem_llvm_ty, elem_ptr, "");
5882 }5897 }
5883 }5898 }