Browse Source

Improve scalar -> array assignment when the scalar is constant in LLVM

gingerBill 1 year ago
parent
commit
4240e0025e
1 changed files with 24 additions and 2 deletions
  1. 24 2
      src/llvm_backend_expr.cpp

+ 24 - 2
src/llvm_backend_expr.cpp

@@ -2138,12 +2138,34 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) {
 
 
 	if (is_type_array_like(dst)) {
 	if (is_type_array_like(dst)) {
 		Type *elem = base_array_type(dst);
 		Type *elem = base_array_type(dst);
+		isize index_count = cast(isize)get_array_type_count(dst);
+
+		isize inlineable = type_size_of(dst) <= build_context.max_simd_align;
 		lbValue e = lb_emit_conv(p, value, elem);
 		lbValue e = lb_emit_conv(p, value, elem);
+		if (inlineable && lb_is_const(e)) {
+			lbAddr v = {};
+			if (e.value) {
+				TEMPORARY_ALLOCATOR_GUARD();
+				LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, index_count);
+				for (isize i = 0; i < index_count; i++) {
+					values[i] = e.value;
+				}
+				lbValue array_const_value = {};
+				array_const_value.type = t;
+				array_const_value.value = LLVMConstArray(lb_type(m, elem), values, cast(unsigned)index_count);
+				v = lb_add_global_generated(m, t, array_const_value);
+			} else {
+				v = lb_add_global_generated(m, t);
+			}
+
+			lb_make_global_private_const(v);
+			return lb_addr_load(p, v);
+		}
+
 		// NOTE(bill): Doesn't need to be zero because it will be initialized in the loops
 		// NOTE(bill): Doesn't need to be zero because it will be initialized in the loops
 		lbAddr v = lb_add_local_generated(p, t, false);
 		lbAddr v = lb_add_local_generated(p, t, false);
-		isize index_count = cast(isize)get_array_type_count(dst);
 
 
-		if (type_size_of(dst) > build_context.max_simd_align) {
+		if (!inlineable) {
 			auto loop_data = lb_loop_start(p, index_count, t_int);
 			auto loop_data = lb_loop_start(p, index_count, t_int);
 
 
 			lbValue elem = lb_emit_array_ep(p, v.addr, loop_data.idx);
 			lbValue elem = lb_emit_array_ep(p, v.addr, loop_data.idx);