From c4dbc88a1239c719170931a62fe53c942f13a155 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 20 Oct 2020 15:38:41 +0100 Subject: [PATCH] Improve array programming code generation --- src/ir.cpp | 19 +++++++++++++------ src/llvm_backend.cpp | 22 +++++++++++++++------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 9882e9c50..dad33762f 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -4495,18 +4495,25 @@ irValue *ir_emit_arith(irProcedure *proc, TokenKind op, irValue *left, irValue * if (inline_array_arith) { // inline for (i32 i = 0; i < count; i++) { - irValue *x = ir_emit_load(proc, ir_emit_array_epi(proc, lhs, i)); - irValue *y = ir_emit_load(proc, ir_emit_array_epi(proc, rhs, i)); + irValue *x_ptr = ir_emit_array_epi(proc, lhs, i); + irValue *y_ptr = ir_emit_array_epi(proc, rhs, i); + irValue *dst_ptr = ir_emit_array_epi(proc, res, i); + irValue *x = ir_emit_load(proc, x_ptr); + irValue *y = ir_emit_load(proc, y_ptr); irValue *z = ir_emit_arith(proc, op, x, y, elem_type); - ir_emit_store(proc, ir_emit_array_epi(proc, res, i), z); + ir_emit_store(proc, dst_ptr, z); } } else { auto loop_data = ir_loop_start(proc, count, t_i32); - irValue *x = ir_emit_load(proc, ir_emit_array_ep(proc, lhs, loop_data.idx)); - irValue *y = ir_emit_load(proc, ir_emit_array_ep(proc, rhs, loop_data.idx)); + irValue *x_ptr = ir_emit_array_ep(proc, lhs, loop_data.idx); + irValue *y_ptr = ir_emit_array_ep(proc, rhs, loop_data.idx); + irValue *dst_ptr = ir_emit_array_ep(proc, res, loop_data.idx); + + irValue *x = ir_emit_load(proc, x_ptr); + irValue *y = ir_emit_load(proc, y_ptr); irValue *z = ir_emit_arith(proc, op, x, y, elem_type); - ir_emit_store(proc, ir_emit_array_ep(proc, res, loop_data.idx), z); + ir_emit_store(proc, dst_ptr, z); ir_loop_end(proc, loop_data); } diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 30e8a7f5b..904b07f23 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -5496,18 +5496,26 @@ lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Ty if (inline_array_arith) { for (i64 i = 0; i < count; i++) { - lbValue a = lb_emit_load(p, lb_emit_array_epi(p, x, i)); - lbValue b = lb_emit_load(p, lb_emit_array_epi(p, y, i)); + lbValue a_ptr = lb_emit_array_epi(p, x, i); + lbValue b_ptr = lb_emit_array_epi(p, y, i); + lbValue dst_ptr = lb_emit_array_epi(p, res.addr, i); + + lbValue a = lb_emit_load(p, a_ptr); + lbValue b = lb_emit_load(p, b_ptr); lbValue c = lb_emit_arith(p, op, a, b, elem_type); - lb_emit_store(p, lb_emit_array_epi(p, res.addr, i), c); + lb_emit_store(p, dst_ptr, c); } } else { - auto loop_data = lb_loop_start(p, count); + auto loop_data = lb_loop_start(p, count, t_i32); - lbValue a = lb_emit_load(p, lb_emit_array_ep(p, x, loop_data.idx)); - lbValue b = lb_emit_load(p, lb_emit_array_ep(p, y, loop_data.idx)); + lbValue a_ptr = lb_emit_array_ep(p, x, loop_data.idx); + lbValue b_ptr = lb_emit_array_ep(p, y, loop_data.idx); + lbValue dst_ptr = lb_emit_array_ep(p, res.addr, loop_data.idx); + + lbValue a = lb_emit_load(p, a_ptr); + lbValue b = lb_emit_load(p, b_ptr); lbValue c = lb_emit_arith(p, op, a, b, elem_type); - lb_emit_store(p, lb_emit_array_ep(p, res.addr, loop_data.idx), c); + lb_emit_store(p, dst_ptr, c); lb_loop_end(p, loop_data); }