Skip to content

Commit 49aa7fc

Browse files
committed
Optimize access of array member in a structure.
1 parent 21d2e63 commit 49aa7fc

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/codegen/llvm.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5750,8 +5750,8 @@ pub const FuncGen = struct {
57505750
const elem_ty = array_ty.childType();
57515751
if (isByRef(array_ty)) {
57525752
const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), rhs };
5753-
const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
57545753
if (isByRef(elem_ty)) {
5754+
const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
57555755
if (canElideLoad(self, body_tail))
57565756
return elem_ptr;
57575757

@@ -5772,11 +5772,13 @@ pub const FuncGen = struct {
57725772
.struct_field_ptr_index_3 => {
57735773
const load_ptr_inst = try self.resolveInst(load_ptr);
57745774
const gep = self.builder.buildInBoundsGEP(array_llvm_ty, load_ptr_inst, &indices, indices.len, "");
5775+
array_llvm_val.removeUnusedLoadArray();
57755776
return self.builder.buildLoad(elem_llvm_ty, gep, "");
57765777
},
57775778
else => {}
57785779
}
57795780
}
5781+
const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
57805782
return self.builder.buildLoad(elem_llvm_ty, elem_ptr, "");
57815783
}
57825784
}

src/codegen/llvm/bindings.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ pub const Value = opaque {
256256

257257
pub const addByValAttr = ZigLLVMAddByValAttr;
258258
extern fn ZigLLVMAddByValAttr(Fn: *Value, ArgNo: c_uint, type: *Type) void;
259+
260+
pub const removeUnusedLoadArray = ZigLLVMRemoveUnusedLoadArray;
261+
extern fn ZigLLVMRemoveUnusedLoadArray(Val: *Value) void;
259262
};
260263

261264
pub const Type = opaque {

src/zig_llvm.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ unsigned ZigLLVMDataLayoutGetProgramAddressSpace(LLVMTargetDataRef TD) {
186186
return unwrap(TD)->getProgramAddressSpace();
187187
}
188188

189+
void ZigLLVMRemoveUnusedLoadArray(LLVMValueRef Val) {
190+
auto *Ptr = unwrap(Val);
191+
if (auto *AI = dyn_cast<AllocaInst>(Ptr)) {
192+
if (AI->hasOneUse()) {
193+
if (auto *CI = dyn_cast<CallInst>(AI->user_back())) {
194+
if (CI->getIntrinsicID() == Intrinsic::memcpy) {
195+
CI->eraseFromParent();
196+
AI->eraseFromParent();
197+
}
198+
}
199+
}
200+
}
201+
}
202+
189203
namespace {
190204
// LLVM's time profiler can provide a hierarchy view of the time spent
191205
// in each component. It generates JSON report in Chrome's "Trace Event"

src/zig_llvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,4 +599,6 @@ ZIG_EXTERN_C void ZigLLVMGetNativeTarget(enum ZigLLVM_ArchType *arch_type,
599599
ZIG_EXTERN_C unsigned ZigLLVMDataLayoutGetStackAlignment(LLVMTargetDataRef TD);
600600
ZIG_EXTERN_C unsigned ZigLLVMDataLayoutGetProgramAddressSpace(LLVMTargetDataRef TD);
601601

602+
ZIG_EXTERN_C void ZigLLVMRemoveUnusedLoadArray(LLVMValueRef Val);
603+
602604
#endif

0 commit comments

Comments
 (0)