aboutsummaryrefslogtreecommitdiff
path: root/src/llvm_backend.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-09-17 12:56:03 +0100
committergingerBill <bill@gingerbill.org>2022-09-17 12:56:03 +0100
commit0ebc2add030d224d59c0b8961dfa75a1f9d13b91 (patch)
tree626dd50254168b0f3d98d8bc3f79f5a680512aa4 /src/llvm_backend.cpp
parent7840c1b89f3e6a7b3d962e6f5e96aab77a3ddc3b (diff)
Use a cache when generating the map header to minimize stack wastage
Diffstat (limited to 'src/llvm_backend.cpp')
-rw-r--r--src/llvm_backend.cpp84
1 files changed, 47 insertions, 37 deletions
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp
index 126bcef11..142ecc348 100644
--- a/src/llvm_backend.cpp
+++ b/src/llvm_backend.cpp
@@ -502,48 +502,58 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A
lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
GB_ASSERT_MSG(is_type_pointer(map_val_ptr.type), "%s", type_to_string(map_val_ptr.type));
- lbAddr h = lb_add_local_generated(p, t_map_header, false); // all the values will be initialzed later
map_type = base_type(map_type);
GB_ASSERT(map_type->kind == Type_Map);
- Type *key_type = map_type->Map.key;
- Type *val_type = map_type->Map.value;
- gb_unused(val_type);
+ lbAddr h = {};
+ lbAddr *found = map_get(&p->map_header_cache, map_val_ptr.value);
+ if (found != nullptr) {
+ h = *found;
+ } else {
+ h = lb_add_local_generated(p, t_map_header, false); // all the values will be initialzed later
- GB_ASSERT(map_type->Map.entry_type->kind == Type_Struct);
- map_type->Map.entry_type->cached_size = -1;
- map_type->Map.entry_type->Struct.are_offsets_set = false;
-
- i64 entry_size = type_size_of (map_type->Map.entry_type);
- i64 entry_align = type_align_of (map_type->Map.entry_type);
-
- i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
- i64 key_size = type_size_of (map_type->Map.key);
+ Type *key_type = map_type->Map.key;
+ Type *val_type = map_type->Map.value;
+ gb_unused(val_type);
- i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
- i64 value_size = type_size_of (map_type->Map.value);
-
-
- Type *map_header_base = base_type(t_map_header);
- GB_ASSERT(map_header_base->Struct.fields.count == 8);
- Type *raw_map_ptr_type = map_header_base->Struct.fields[0]->type;
- LLVMValueRef const_values[8] = {};
- const_values[0] = LLVMConstNull(lb_type(p->module, raw_map_ptr_type));
- const_values[1] = lb_get_equal_proc_for_type(p->module, key_type) .value;
- const_values[2] = lb_const_int(p->module, t_int, entry_size) .value;
- const_values[3] = lb_const_int(p->module, t_int, entry_align) .value;
- const_values[4] = lb_const_int(p->module, t_uintptr, key_offset) .value;
- const_values[5] = lb_const_int(p->module, t_int, key_size) .value;
- const_values[6] = lb_const_int(p->module, t_uintptr, value_offset).value;
- const_values[7] = lb_const_int(p->module, t_int, value_size) .value;
-
- LLVMValueRef const_value = llvm_const_named_struct(p->module, t_map_header, const_values, gb_count_of(const_values));
- LLVMBuildStore(p->builder, const_value, h.addr.value);
-
- // NOTE(bill): Removes unnecessary allocation if split gep
- lbValue gep0 = lb_emit_struct_ep(p, h.addr, 0);
- lbValue m = lb_emit_conv(p, map_val_ptr, type_deref(gep0.type));
- lb_emit_store(p, gep0, m);
+ GB_ASSERT(map_type->Map.entry_type->kind == Type_Struct);
+ map_type->Map.entry_type->cached_size = -1;
+ map_type->Map.entry_type->Struct.are_offsets_set = false;
+
+ i64 entry_size = type_size_of (map_type->Map.entry_type);
+ i64 entry_align = type_align_of (map_type->Map.entry_type);
+
+ i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
+ i64 key_size = type_size_of (map_type->Map.key);
+
+ i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
+ i64 value_size = type_size_of (map_type->Map.value);
+
+
+ Type *map_header_base = base_type(t_map_header);
+ GB_ASSERT(map_header_base->Struct.fields.count == 8);
+ Type *raw_map_ptr_type = map_header_base->Struct.fields[0]->type;
+ LLVMValueRef const_values[8] = {};
+ const_values[0] = LLVMConstNull(lb_type(p->module, raw_map_ptr_type));
+ const_values[1] = lb_get_equal_proc_for_type(p->module, key_type) .value;
+ const_values[2] = lb_const_int(p->module, t_int, entry_size) .value;
+ const_values[3] = lb_const_int(p->module, t_int, entry_align) .value;
+ const_values[4] = lb_const_int(p->module, t_uintptr, key_offset) .value;
+ const_values[5] = lb_const_int(p->module, t_int, key_size) .value;
+ const_values[6] = lb_const_int(p->module, t_uintptr, value_offset).value;
+ const_values[7] = lb_const_int(p->module, t_int, value_size) .value;
+
+ LLVMValueRef const_value = llvm_const_named_struct(p->module, t_map_header, const_values, gb_count_of(const_values));
+ LLVMBuildStore(p->builder, const_value, h.addr.value);
+
+ // NOTE(bill): Removes unnecessary allocation if split gep
+ lbValue gep0 = lb_emit_struct_ep(p, h.addr, 0);
+ lbValue m = lb_emit_conv(p, map_val_ptr, type_deref(gep0.type));
+ lb_emit_store(p, gep0, m);
+
+
+ map_set(&p->map_header_cache, map_val_ptr.value, h);
+ }
return lb_addr_load(p, h);
}