Skip to content

Commit abba66e

Browse files
committed
Optimize access of array member in a structure.
1 parent b3f4e0d commit abba66e

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
@@ -5746,19 +5746,37 @@ pub const FuncGen = struct {
57465746
const array_ty = self.air.typeOf(bin_op.lhs);
57475747
const array_llvm_val = try self.resolveInst(bin_op.lhs);
57485748
const rhs = try self.resolveInst(bin_op.rhs);
5749+
const array_llvm_ty = try self.dg.lowerType(array_ty);
5750+
const elem_ty = array_ty.childType();
57495751
if (isByRef(array_ty)) {
5750-
const array_llvm_ty = try self.dg.lowerType(array_ty);
57515752
const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), rhs };
57525753
const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
5753-
const elem_ty = array_ty.childType();
57545754
if (isByRef(elem_ty)) {
57555755
if (canElideLoad(self, body_tail))
57565756
return elem_ptr;
57575757

57585758
const target = self.dg.module.getTarget();
57595759
return self.loadByRef(elem_ptr, elem_ty, elem_ty.abiAlignment(target), false);
57605760
} else {
5761+
const lhs_index = Air.refToIndex(bin_op.lhs).?;
57615762
const elem_llvm_ty = try self.dg.lowerType(elem_ty);
5763+
if (self.air.instructions.items(.tag)[lhs_index] == .load) {
5764+
const load_data = self.air.instructions.items(.data)[lhs_index];
5765+
const load_ptr = load_data.ty_op.operand;
5766+
const load_ptr_tag = self.air.instructions.items(.tag)[Air.refToIndex(load_ptr).?];
5767+
switch (load_ptr_tag) {
5768+
.struct_field_ptr,
5769+
.struct_field_ptr_index_0,
5770+
.struct_field_ptr_index_1,
5771+
.struct_field_ptr_index_2,
5772+
.struct_field_ptr_index_3 => {
5773+
const load_ptr_inst = try self.resolveInst(load_ptr);
5774+
const gep = self.builder.buildInBoundsGEP(array_llvm_ty, load_ptr_inst, &indices, indices.len, "");
5775+
return self.builder.buildLoad(elem_llvm_ty, gep, "");
5776+
},
5777+
else => {}
5778+
}
5779+
}
57625780
return self.builder.buildLoad(elem_llvm_ty, elem_ptr, "");
57635781
}
57645782
}

0 commit comments

Comments
 (0)