Skip to content

Commit d6b5f8d

Browse files
committed
Optimize access of array member in a structure.
1 parent d1f61f2 commit d6b5f8d

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/codegen/llvm.zig

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5839,19 +5839,37 @@ pub const FuncGen = struct {
58395839
const array_ty = self.air.typeOf(bin_op.lhs);
58405840
const array_llvm_val = try self.resolveInst(bin_op.lhs);
58415841
const rhs = try self.resolveInst(bin_op.rhs);
5842+
const array_llvm_ty = try self.dg.lowerType(array_ty);
5843+
const elem_ty = array_ty.childType();
58425844
if (isByRef(array_ty)) {
5843-
const array_llvm_ty = try self.dg.lowerType(array_ty);
58445845
const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), rhs };
58455846
const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
5846-
const elem_ty = array_ty.childType();
58475847
if (isByRef(elem_ty)) {
58485848
if (canElideLoad(self, body_tail))
58495849
return elem_ptr;
58505850

58515851
const target = self.dg.module.getTarget();
58525852
return self.loadByRef(elem_ptr, elem_ty, elem_ty.abiAlignment(target), false);
58535853
} else {
5854+
const lhs_index = Air.refToIndex(bin_op.lhs).?;
58545855
const elem_llvm_ty = try self.dg.lowerType(elem_ty);
5856+
if (self.air.instructions.items(.tag)[lhs_index] == .load) {
5857+
const load_data = self.air.instructions.items(.data)[lhs_index];
5858+
const load_ptr = load_data.ty_op.operand;
5859+
const load_ptr_tag = self.air.instructions.items(.tag)[Air.refToIndex(load_ptr).?];
5860+
switch (load_ptr_tag) {
5861+
.struct_field_ptr,
5862+
.struct_field_ptr_index_0,
5863+
.struct_field_ptr_index_1,
5864+
.struct_field_ptr_index_2,
5865+
.struct_field_ptr_index_3 => {
5866+
const load_ptr_inst = try self.resolveInst(load_ptr);
5867+
const gep = self.builder.buildInBoundsGEP(array_llvm_ty, load_ptr_inst, &indices, indices.len, "");
5868+
return self.builder.buildLoad(elem_llvm_ty, gep, "");
5869+
},
5870+
else => {}
5871+
}
5872+
}
58555873
return self.builder.buildLoad(elem_llvm_ty, elem_ptr, "");
58565874
}
58575875
}

0 commit comments

Comments
 (0)