Browse Source

Improve array programming code generation

gingerBill 4 years ago
parent
commit
c4dbc88a12
2 changed files with 28 additions and 13 deletions
  1. 13 6
      src/ir.cpp
  2. 15 7
      src/llvm_backend.cpp

+ 13 - 6
src/ir.cpp

@@ -4495,18 +4495,25 @@ irValue *ir_emit_arith(irProcedure *proc, TokenKind op, irValue *left, irValue *
 		if (inline_array_arith) {
 		if (inline_array_arith) {
 			// inline
 			// inline
 			for (i32 i = 0; i < count; i++) {
 			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);
 				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 {
 		} else {
 			auto loop_data = ir_loop_start(proc, count, t_i32);
 			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);
 			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);
 			ir_loop_end(proc, loop_data);
 		}
 		}

+ 15 - 7
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) {
 		if (inline_array_arith) {
 			for (i64 i = 0; i < count; i++) {
 			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);
 				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 {
 		} else {
-			auto loop_data = lb_loop_start(p, count);
+			auto loop_data = lb_loop_start(p, count, t_i32);
+
+			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, 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 = 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);
 			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);
 			lb_loop_end(p, loop_data);
 		}
 		}