Skip to content

Commit 8ffbe29

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

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
@@ -5843,8 +5843,8 @@ pub const FuncGen = struct {
58435843
const elem_ty = array_ty.childType();
58445844
if (isByRef(array_ty)) {
58455845
const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), rhs };
5846-
const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
58475846
if (isByRef(elem_ty)) {
5847+
const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
58485848
if (canElideLoad(self, body_tail))
58495849
return elem_ptr;
58505850

@@ -5865,11 +5865,13 @@ pub const FuncGen = struct {
58655865
.struct_field_ptr_index_3 => {
58665866
const load_ptr_inst = try self.resolveInst(load_ptr);
58675867
const gep = self.builder.buildInBoundsGEP(array_llvm_ty, load_ptr_inst, &indices, indices.len, "");
5868+
array_llvm_val.removeUnusedLoadArray();
58685869
return self.builder.buildLoad(elem_llvm_ty, gep, "");
58695870
},
58705871
else => {}
58715872
}
58725873
}
5874+
const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, "");
58735875
return self.builder.buildLoad(elem_llvm_ty, elem_ptr, "");
58745876
}
58755877
}

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)