Skip to content

Commit a63af8a

Browse files
shwqfVexu
authored andcommitted
Optimize access of array member in a structure.
1 parent 2606498 commit a63af8a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/codegen/llvm.zig

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5865,19 +5865,34 @@ pub const FuncGen = struct {
58655865
const array_ty = self.air.typeOf(bin_op.lhs);
58665866
const array_llvm_val = try self.resolveInst(bin_op.lhs);
58675867
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();
58685870
if (isByRef(array_ty)) {
5869-
const array_llvm_ty = try self.dg.lowerType(array_ty);
58705871
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();
58735872
if (isByRef(elem_ty)) {
5873+
const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
58745874
if (canElideLoad(self, body_tail))
58755875
return elem_ptr;
58765876

58775877
const target = self.dg.module.getTarget();
58785878
return self.loadByRef(elem_ptr, elem_ty, elem_ty.abiAlignment(target), false);
58795879
} else {
5880+
const lhs_index = Air.refToIndex(bin_op.lhs).?;
58805881
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, "");
58815896
return self.builder.buildLoad(elem_llvm_ty, elem_ptr, "");
58825897
}
58835898
}

0 commit comments

Comments
 (0)