Browse Source

Make llvm backend code use `PtrMap`; remove dead code

gingerBill 3 years ago
parent
commit
6be104e521

+ 6 - 9
src/llvm_backend.cpp

@@ -123,8 +123,7 @@ lbValue lb_get_equal_proc_for_type(lbModule *m, Type *type) {
 	Type *pt = alloc_type_pointer(type);
 	LLVMTypeRef ptr_type = lb_type(m, pt);
 
-	auto key = hash_type(type);
-	lbProcedure **found = map_get(&m->equal_procs, key);
+	lbProcedure **found = map_get(&m->equal_procs, type);
 	lbProcedure *compare_proc = nullptr;
 	if (found) {
 		compare_proc = *found;
@@ -140,7 +139,7 @@ lbValue lb_get_equal_proc_for_type(lbModule *m, Type *type) {
 	String proc_name = make_string_c(str);
 
 	lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_equal_proc);
-	map_set(&m->equal_procs, key, p);
+	map_set(&m->equal_procs, type, p);
 	lb_begin_procedure_body(p);
 
 	LLVMValueRef x = LLVMGetParam(p->value, 0);
@@ -279,8 +278,7 @@ lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type) {
 
 	Type *pt = alloc_type_pointer(type);
 
-	auto key = hash_type(type);
-	lbProcedure **found = map_get(&m->hasher_procs, key);
+	lbProcedure **found = map_get(&m->hasher_procs, type);
 	if (found) {
 		GB_ASSERT(*found != nullptr);
 		return {(*found)->value, (*found)->type};
@@ -294,7 +292,7 @@ lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type) {
 	String proc_name = make_string_c(str);
 
 	lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_hasher_proc);
-	map_set(&m->hasher_procs, key, p);
+	map_set(&m->hasher_procs, type, p);
 	lb_begin_procedure_body(p);
 	defer (lb_end_procedure_body(p));
 
@@ -433,7 +431,7 @@ lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type) {
 
 
 lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent) {
-	lbProcedure **found = map_get(&m->gen->anonymous_proc_lits, hash_pointer(expr));
+	lbProcedure **found = map_get(&m->gen->anonymous_proc_lits, expr);
 	if (found) {
 		return lb_find_procedure_value_from_entity(m, (*found)->entity);
 	}
@@ -473,8 +471,7 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A
 		string_map_set(&m->members, name, value);
 	}
 
-	map_set(&m->anonymous_proc_lits, hash_pointer(expr), p);
-	map_set(&m->gen->anonymous_proc_lits, hash_pointer(expr), p);
+	map_set(&m->gen->anonymous_proc_lits, expr, p);
 
 	return value;
 }

+ 12 - 14
src/llvm_backend.hpp

@@ -114,25 +114,23 @@ struct lbModule {
 	CheckerInfo *info;
 	AstPackage *pkg; // associated
 
-	Map<LLVMTypeRef> types;                             // Key: Type *
-	Map<lbStructFieldRemapping> struct_field_remapping; // Key: LLVMTypeRef or Type *
-	Map<Type *> llvm_types;                             // Key: LLVMTypeRef
+	PtrMap<Type *, LLVMTypeRef> types;
+	PtrMap<void *, lbStructFieldRemapping> struct_field_remapping; // Key: LLVMTypeRef or Type *
 	i32 internal_type_level;
 
-	Map<lbValue>  values;           // Key: Entity *
-	Map<lbAddr>   soa_values;       // Key: Entity *
+	PtrMap<Entity *, lbValue> values;           
+	PtrMap<Entity *, lbAddr>  soa_values;       
 	StringMap<lbValue>  members;
 	StringMap<lbProcedure *> procedures;
-	Map<Entity *> procedure_values; // Key: LLVMValueRef
+	PtrMap<LLVMValueRef, Entity *> procedure_values;
 	Array<lbProcedure *> missing_procedures_to_check;
 
 	StringMap<LLVMValueRef> const_strings;
 
-	Map<lbProcedure *> anonymous_proc_lits; // Key: Ast *
-	Map<struct lbFunctionType *> function_type_map; // Key: Type *
+	PtrMap<Type *, struct lbFunctionType *> function_type_map; 
 
-	Map<lbProcedure *> equal_procs; // Key: Type *
-	Map<lbProcedure *> hasher_procs; // Key: Type *
+	PtrMap<Type *, lbProcedure *> equal_procs;
+	PtrMap<Type *, lbProcedure *> hasher_procs;
 
 	u32 nested_type_name_guid;
 
@@ -143,7 +141,7 @@ struct lbModule {
 
 	LLVMDIBuilderRef debug_builder;
 	LLVMMetadataRef debug_compile_unit;
-	Map<LLVMMetadataRef> debug_values; // Key: Pointer
+	PtrMap<void *, LLVMMetadataRef> debug_values; 
 
 	Array<lbIncompleteDebugType> debug_incomplete_types;
 };
@@ -155,11 +153,11 @@ struct lbGenerator {
 	Array<String> output_temp_paths;
 	String   output_base;
 	String   output_name;
-	Map<lbModule *> modules; // Key: AstPackage *
-	Map<lbModule *> modules_through_ctx; // Key: LLVMContextRef *
+	PtrMap<AstPackage *, lbModule *> modules; 
+	PtrMap<LLVMContextRef, lbModule *> modules_through_ctx; 
 	lbModule default_module;
 
-	Map<lbProcedure *> anonymous_proc_lits; // Key: Ast *
+	PtrMap<Ast *, lbProcedure *> anonymous_proc_lits; 
 
 	std::atomic<u32> global_array_index;
 	std::atomic<u32> global_generated_index;

+ 2 - 2
src/llvm_backend_debug.cpp

@@ -2,7 +2,7 @@ LLVMMetadataRef lb_get_llvm_metadata(lbModule *m, void *key) {
 	if (key == nullptr) {
 		return nullptr;
 	}
-	auto found = map_get(&m->debug_values, hash_pointer(key));
+	auto found = map_get(&m->debug_values, key);
 	if (found) {
 		return *found;
 	}
@@ -10,7 +10,7 @@ LLVMMetadataRef lb_get_llvm_metadata(lbModule *m, void *key) {
 }
 void lb_set_llvm_metadata(lbModule *m, void *key, LLVMMetadataRef value) {
 	if (key != nullptr) {
-		map_set(&m->debug_values, hash_pointer(key), value);
+		map_set(&m->debug_values, key, value);
 	}
 }
 

+ 3 - 3
src/llvm_backend_expr.cpp

@@ -3142,7 +3142,7 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
 }
 
 lbAddr lb_get_soa_variable_addr(lbProcedure *p, Entity *e) {
-	return map_must_get(&p->module->soa_values, hash_entity(e));
+	return map_must_get(&p->module->soa_values, e);
 }
 lbValue lb_get_using_variable(lbProcedure *p, Entity *e) {
 	GB_ASSERT(e->kind == Entity_Variable && e->flags & EntityFlag_Using);
@@ -3150,7 +3150,7 @@ lbValue lb_get_using_variable(lbProcedure *p, Entity *e) {
 	Entity *parent = e->using_parent;
 	Selection sel = lookup_field(parent->type, name, false);
 	GB_ASSERT(sel.entity != nullptr);
-	lbValue *pv = map_get(&p->module->values, hash_entity(parent));
+	lbValue *pv = map_get(&p->module->values, parent);
 
 	lbValue v = {};
 
@@ -3190,7 +3190,7 @@ lbAddr lb_build_addr_from_entity(lbProcedure *p, Entity *e, Ast *expr) {
 
 
 	lbValue v = {};
-	lbValue *found = map_get(&p->module->values, hash_entity(e));
+	lbValue *found = map_get(&p->module->values, e);
 	if (found) {
 		v = *found;
 	} else if (e->kind == Entity_Variable && e->flags & EntityFlag_Using) {

+ 25 - 31
src/llvm_backend_general.cpp

@@ -57,14 +57,12 @@ void lb_init_module(lbModule *m, Checker *c) {
 	gbAllocator a = heap_allocator();
 	map_init(&m->types, a);
 	map_init(&m->struct_field_remapping, a);
-	map_init(&m->llvm_types, a);
 	map_init(&m->values, a);
 	map_init(&m->soa_values, a);
 	string_map_init(&m->members, a);
 	map_init(&m->procedure_values, a);
 	string_map_init(&m->procedures, a);
 	string_map_init(&m->const_strings, a);
-	map_init(&m->anonymous_proc_lits, a);
 	map_init(&m->function_type_map, a);
 	map_init(&m->equal_procs, a);
 	map_init(&m->hasher_procs, a);
@@ -133,20 +131,20 @@ bool lb_init_generator(lbGenerator *gen, Checker *c) {
 			auto m = gb_alloc_item(permanent_allocator(), lbModule);
 			m->pkg = pkg;
 			m->gen = gen;
-			map_set(&gen->modules, hash_pointer(pkg), m);
+			map_set(&gen->modules, pkg, m);
 			lb_init_module(m, c);
 		}
 	}
 
 	gen->default_module.gen = gen;
-	map_set(&gen->modules, hash_pointer(nullptr), &gen->default_module);
+	map_set(&gen->modules, cast(AstPackage *)nullptr, &gen->default_module);
 	lb_init_module(&gen->default_module, c);
 
 
 	for_array(i, gen->modules.entries) {
 		lbModule *m = gen->modules.entries[i].value;
 		LLVMContextRef ctx = LLVMGetModuleContext(m->mod);
-		map_set(&gen->modules_through_ctx, hash_pointer(ctx), m);
+		map_set(&gen->modules_through_ctx, ctx, m);
 	}
 
 	return true;
@@ -251,7 +249,7 @@ bool lb_is_instr_terminating(LLVMValueRef instr) {
 
 
 lbModule *lb_pkg_module(lbGenerator *gen, AstPackage *pkg) {
-	auto *found = map_get(&gen->modules, hash_pointer(pkg));
+	auto *found = map_get(&gen->modules, pkg);
 	if (found) {
 		return *found;
 	}
@@ -1590,7 +1588,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 				return lb_type_internal(m, base);
 			}
 
-			LLVMTypeRef *found = map_get(&m->types, hash_type(base));
+			LLVMTypeRef *found = map_get(&m->types, base);
 			if (found) {
 				LLVMTypeKind kind = LLVMGetTypeKind(*found);
 				if (kind == LLVMStructTypeKind) {
@@ -1600,7 +1598,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 						return llvm_type;
 					}
 					llvm_type = LLVMStructCreateNamed(ctx, name);
-					map_set(&m->types, hash_type(type), llvm_type);
+					map_set(&m->types, type, llvm_type);
 					lb_clone_struct_type(llvm_type, *found);
 					return llvm_type;
 				}
@@ -1616,7 +1614,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 						return llvm_type;
 					}
 					llvm_type = LLVMStructCreateNamed(ctx, name);
-					map_set(&m->types, hash_type(type), llvm_type);
+					map_set(&m->types, type, llvm_type);
 					lb_clone_struct_type(llvm_type, lb_type(m, base));
 					return llvm_type;
 				}
@@ -1697,7 +1695,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 				for_array(i, entries_field_remapping) {
 					entries_field_remapping[i] = cast(i32)i;
 				}
-				map_set(&m->struct_field_remapping, hash_pointer(fields[1]), entries_field_remapping);
+				map_set(&m->struct_field_remapping, cast(void *)fields[1], entries_field_remapping);
 			}
 			
 			return LLVMStructTypeInContext(ctx, fields, field_count, false);
@@ -1721,8 +1719,8 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 				field_remapping[0] = 0;
 				
 				LLVMTypeRef struct_type = LLVMStructTypeInContext(ctx, fields, gb_count_of(fields), false);
-				map_set(&m->struct_field_remapping, hash_pointer(struct_type), field_remapping);
-				map_set(&m->struct_field_remapping, hash_pointer(type), field_remapping);
+				map_set(&m->struct_field_remapping, cast(void *)struct_type, field_remapping);
+				map_set(&m->struct_field_remapping, cast(void *)type, field_remapping);
 				return struct_type;
 			}
 			
@@ -1768,8 +1766,8 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 			}
 			
 			LLVMTypeRef struct_type = LLVMStructTypeInContext(ctx, fields.data, cast(unsigned)fields.count, type->Struct.is_packed);
-			map_set(&m->struct_field_remapping, hash_pointer(struct_type), field_remapping);
-			map_set(&m->struct_field_remapping, hash_pointer(type), field_remapping);			
+			map_set(&m->struct_field_remapping, cast(void *)struct_type, field_remapping);
+			map_set(&m->struct_field_remapping, cast(void *)type, field_remapping);			
 			#if 0
 			GB_ASSERT_MSG(lb_sizeof(struct_type) == full_type_size, 
 			              "(%lld) %s vs (%lld) %s", 
@@ -1930,7 +1928,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 				              LLVMGetTypeContext(ft->ret.type), ft->ctx, LLVMGetGlobalContext());
 			}
 
-			map_set(&m->function_type_map, hash_type(type), ft);
+			map_set(&m->function_type_map, type, ft);
 			LLVMTypeRef new_abi_fn_ptr_type = lb_function_type_to_llvm_ptr(ft, type->Proc.c_vararg);
 			LLVMTypeRef new_abi_fn_type = LLVMGetElementType(new_abi_fn_ptr_type);
 
@@ -1991,7 +1989,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 LLVMTypeRef lb_type(lbModule *m, Type *type) {
 	type = default_type(type);
 
-	LLVMTypeRef *found = map_get(&m->types, hash_type(type));
+	LLVMTypeRef *found = map_get(&m->types, type);
 	if (found) {
 		return *found;
 	}
@@ -2002,21 +2000,18 @@ LLVMTypeRef lb_type(lbModule *m, Type *type) {
 	llvm_type = lb_type_internal(m, type);
 	m->internal_type_level -= 1;
 	if (m->internal_type_level == 0) {
-		map_set(&m->types, hash_type(type), llvm_type);
-		if (is_type_named(type)) {
-			map_set(&m->llvm_types, hash_pointer(llvm_type), type);
-		}
+		map_set(&m->types, type, llvm_type);
 	}
 	return llvm_type;
 }
 
 lbFunctionType *lb_get_function_type(lbModule *m, lbProcedure *p, Type *pt) {
 	lbFunctionType **ft_found = nullptr;
-	ft_found = map_get(&m->function_type_map, hash_type(pt));
+	ft_found = map_get(&m->function_type_map, pt);
 	if (!ft_found) {
 		LLVMTypeRef llvm_proc_type = lb_type(p->module, pt);
 		gb_unused(llvm_proc_type);
-		ft_found = map_get(&m->function_type_map, hash_type(pt));
+		ft_found = map_get(&m->function_type_map, pt);
 	}
 	GB_ASSERT(ft_found != nullptr);
 
@@ -2027,12 +2022,11 @@ void lb_ensure_abi_function_type(lbModule *m, lbProcedure *p) {
 	if (p->abi_function_type != nullptr) {
 		return;
 	}
-	auto hash = hash_type(p->type);
-	lbFunctionType **ft_found = map_get(&m->function_type_map, hash);
+	lbFunctionType **ft_found = map_get(&m->function_type_map, p->type);
 	if (ft_found == nullptr) {
 		LLVMTypeRef llvm_proc_type = lb_type(p->module, p->type);
 		gb_unused(llvm_proc_type);
-		ft_found = map_get(&m->function_type_map, hash);
+		ft_found = map_get(&m->function_type_map, p->type);
 	}
 	GB_ASSERT(ft_found != nullptr);
 	p->abi_function_type = *ft_found;
@@ -2041,7 +2035,7 @@ void lb_ensure_abi_function_type(lbModule *m, lbProcedure *p) {
 
 void lb_add_entity(lbModule *m, Entity *e, lbValue val) {
 	if (e != nullptr) {
-		map_set(&m->values, hash_entity(e), val);
+		map_set(&m->values, e, val);
 	}
 }
 void lb_add_member(lbModule *m, String const &name, lbValue val) {
@@ -2054,7 +2048,7 @@ void lb_add_member(lbModule *m, StringHashKey const &key, lbValue val) {
 }
 void lb_add_procedure_value(lbModule *m, lbProcedure *p) {
 	if (p->entity != nullptr) {
-		map_set(&m->procedure_values, hash_pointer(p->value), p->entity);
+		map_set(&m->procedure_values, p->value, p->entity);
 	}
 	string_map_set(&m->procedures, p->name, p);
 }
@@ -2368,7 +2362,7 @@ lbValue lb_find_or_add_entity_string_byte_slice(lbModule *m, String const &str)
 
 
 lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *expr) {
-	auto *found = map_get(&m->values, hash_entity(e));
+	auto *found = map_get(&m->values, e);
 	if (found) {
 		auto v = *found;
 		// NOTE(bill): This is because pointers are already pointers in LLVM
@@ -2417,7 +2411,7 @@ lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) {
 	e = strip_entity_wrapping(e);
 	GB_ASSERT(e != nullptr);
 
-	auto *found = map_get(&m->values, hash_entity(e));
+	auto *found = map_get(&m->values, e);
 	if (found) {
 		return *found;
 	}
@@ -2437,7 +2431,7 @@ lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) {
 	if (!ignore_body) {
 		array_add(&m->missing_procedures_to_check, missing_proc);
 	}
-	found = map_get(&m->values, hash_entity(e));
+	found = map_get(&m->values, e);
 	if (found) {
 		return *found;
 	}
@@ -2501,7 +2495,7 @@ lbValue lb_find_value_from_entity(lbModule *m, Entity *e) {
 		return lb_find_procedure_value_from_entity(m, e);
 	}
 
-	auto *found = map_get(&m->values, hash_entity(e));
+	auto *found = map_get(&m->values, e);
 	if (found) {
 		return *found;
 	}

+ 1 - 1
src/llvm_backend_opt.cpp

@@ -387,7 +387,7 @@ void lb_run_remove_unused_function_pass(lbModule *m) {
 				continue;
 			}
 			
-			Entity **found = map_get(&m->procedure_values, hash_pointer(curr_func));
+			Entity **found = map_get(&m->procedure_values, curr_func);
 			if (found && *found) {
 				Entity *e = *found;
 				bool is_required = (e->flags & EntityFlag_Require) == EntityFlag_Require;

+ 1 - 1
src/llvm_backend_proc.cpp

@@ -948,7 +948,7 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
 
 	}
 
-	Entity **found = map_get(&p->module->procedure_values, hash_pointer(value.value));
+	Entity **found = map_get(&p->module->procedure_values, value.value);
 	if (found != nullptr) {
 		Entity *e = *found;
 		if (e != nullptr && entity_has_deferred_procedure(e)) {

+ 5 - 5
src/llvm_backend_stmt.cpp

@@ -670,7 +670,7 @@ void lb_build_range_stmt_struct_soa(lbProcedure *p, AstRangeStmt *rs, Scope *sco
 		Entity *e = entity_of_node(rs->vals[0]);
 		if (e != nullptr) {
 			lbAddr soa_val = lb_addr_soa_variable(array.addr, lb_addr_load(p, index), nullptr);
-			map_set(&p->module->soa_values, hash_entity(e), soa_val);
+			map_set(&p->module->soa_values, e, soa_val);
 		}
 	}
 	if (val_types[1]) {
@@ -1525,7 +1525,7 @@ void lb_build_return_stmt(lbProcedure *p, Slice<Ast *> const &return_results) {
 	} else if (return_count == 1) {
 		Entity *e = tuple->variables[0];
 		if (res_count == 0) {
-			lbValue found = map_must_get(&p->module->values, hash_entity(e));
+			lbValue found = map_must_get(&p->module->values, e);
 			res = lb_emit_load(p, found);
 		} else {
 			res = lb_build_expr(p, return_results[0]);
@@ -1534,7 +1534,7 @@ void lb_build_return_stmt(lbProcedure *p, Slice<Ast *> const &return_results) {
 		if (p->type->Proc.has_named_results) {
 			// NOTE(bill): store the named values before returning
 			if (e->token.string != "") {
-				lbValue found = map_must_get(&p->module->values, hash_entity(e));
+				lbValue found = map_must_get(&p->module->values, e);
 				lb_emit_store(p, found, lb_emit_conv(p, res, e->type));
 			}
 		}
@@ -1558,7 +1558,7 @@ void lb_build_return_stmt(lbProcedure *p, Slice<Ast *> const &return_results) {
 		} else {
 			for (isize res_index = 0; res_index < return_count; res_index++) {
 				Entity *e = tuple->variables[res_index];
-				lbValue found = map_must_get(&p->module->values, hash_entity(e));
+				lbValue found = map_must_get(&p->module->values, e);
 				lbValue res = lb_emit_load(p, found);
 				array_add(&results, res);
 			}
@@ -1580,7 +1580,7 @@ void lb_build_return_stmt(lbProcedure *p, Slice<Ast *> const &return_results) {
 				if (e->token.string == "") {
 					continue;
 				}
-				named_results[i] = map_must_get(&p->module->values, hash_entity(e));
+				named_results[i] = map_must_get(&p->module->values, e);
 				values[i] = lb_emit_conv(p, results[i], e->type);
 			}
 

+ 3 - 3
src/llvm_backend_utility.cpp

@@ -401,7 +401,7 @@ lbValue lb_emit_or_return(lbProcedure *p, Ast *arg, TypeAndValue const &tv) {
 			GB_ASSERT(end_entity->token.string.len != 0);
 
 			// NOTE(bill): store the named values before returning
-			lbValue found = map_must_get(&p->module->values, hash_entity(end_entity));
+			lbValue found = map_must_get(&p->module->values, end_entity);
 			lb_emit_store(p, found, rhs);
 
 			lb_build_return_stmt(p, {});
@@ -811,9 +811,9 @@ lbValue lb_address_from_load(lbProcedure *p, lbValue value) {
 lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t) {
 	t = base_type(t);
 	LLVMTypeRef struct_type = lb_type(m, t);
-	auto *field_remapping = map_get(&m->struct_field_remapping, hash_pointer(struct_type));
+	auto *field_remapping = map_get(&m->struct_field_remapping, cast(void *)struct_type);
 	if (field_remapping == nullptr) {
-		field_remapping = map_get(&m->struct_field_remapping, hash_pointer(t));
+		field_remapping = map_get(&m->struct_field_remapping, cast(void *)t);
 	}
 	GB_ASSERT(field_remapping != nullptr);
 	return *field_remapping;