Skip to content

Commit 970a9d0

Browse files
committed
Optimize access of array member in a structure.
1 parent 8a0a6b7 commit 970a9d0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/codegen/llvm.zig

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5720,19 +5720,37 @@ pub const FuncGen = struct {
57205720
const array_ty = self.air.typeOf(bin_op.lhs);
57215721
const array_llvm_val = try self.resolveInst(bin_op.lhs);
57225722
const rhs = try self.resolveInst(bin_op.rhs);
5723+
const array_llvm_ty = try self.dg.lowerType(array_ty);
5724+
const elem_ty = array_ty.childType();
57235725
if (isByRef(array_ty)) {
5724-
const array_llvm_ty = try self.dg.lowerType(array_ty);
57255726
const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), rhs };
57265727
const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
5727-
const elem_ty = array_ty.childType();
57285728
if (isByRef(elem_ty)) {
57295729
if (canElideLoad(self, body_tail))
57305730
return elem_ptr;
57315731

57325732
const target = self.dg.module.getTarget();
57335733
return self.loadByRef(elem_ptr, elem_ty, elem_ty.abiAlignment(target), false);
57345734
} else {
5735+
const lhs_index = Air.refToIndex(bin_op.lhs).?;
57355736
const elem_llvm_ty = try self.dg.lowerType(elem_ty);
5737+
if (self.air.instructions.items(.tag)[lhs_index] == .load) {
5738+
const load_data = self.air.instructions.items(.data)[lhs_index];
5739+
const load_ptr = load_data.ty_op.operand;
5740+
const load_ptr_tag = self.air.instructions.items(.tag)[Air.refToIndex(load_ptr).?];
5741+
switch (load_ptr_tag) {
5742+
.struct_field_ptr,
5743+
.struct_field_ptr_index_0,
5744+
.struct_field_ptr_index_1,
5745+
.struct_field_ptr_index_2,
5746+
.struct_field_ptr_index_3 => {
5747+
const load_ptr_inst = try self.resolveInst(load_ptr);
5748+
const gep = self.builder.buildInBoundsGEP(array_llvm_ty, load_ptr_inst, &indices, indices.len, "");
5749+
return self.builder.buildLoad(elem_llvm_ty, gep, "");
5750+
},
5751+
else => {}
5752+
}
5753+
}
57365754
return self.builder.buildLoad(elem_llvm_ty, elem_ptr, "");
57375755
}
57385756
}

test_io_uring_renameat_old

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello

0 commit comments

Comments
 (0)