diff options
| author | gingerBill <bill@gingerbill.org> | 2020-11-29 16:03:21 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2020-11-29 16:03:21 +0000 |
| commit | 7fbc081119c57bf378a317fe6618eacd0465834d (patch) | |
| tree | 7965f742073f6cd72c53481936e95df0e3a804a9 /src/ir.cpp | |
| parent | 1dfe0cdd1de44d6c8b94c4e902a731655f8972c9 (diff) | |
Improve const hash
Diffstat (limited to 'src/ir.cpp')
| -rw-r--r-- | src/ir.cpp | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index 90b0eda2a..cb60b9e3b 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -3615,6 +3615,29 @@ irValue *ir_gen_map_header(irProcedure *proc, irValue *map_val_ptr, Type *map_ty return ir_emit_load(proc, h); } +irValue *ir_const_hash(irModule *m, irValue *key, Type *key_type) { + irValue *hashed_key = nullptr; + + if (key->kind == irValue_Constant) { + u64 hash = 0xcbf29ce484222325; + if (is_type_string(key_type)) { + GB_ASSERT(key->Constant.value.kind == ExactValue_String); + String s = key->Constant.value.value_string; + hash = fnv64a(s.text, s.len); + } else { + return nullptr; + } + // TODO(bill): other const hash types + + if (build_context.word_size == 4) { + hash &= 0xffffffffull; + } + hashed_key = ir_const_uintptr(hash); + } + + return hashed_key; +} + irValue *ir_gen_map_hash(irProcedure *proc, irValue *key, Type *key_type) { Type *hash_type = t_u64; irValue *v = ir_add_local_generated(proc, t_map_hash, true); @@ -3624,12 +3647,14 @@ irValue *ir_gen_map_hash(irProcedure *proc, irValue *key, Type *key_type) { irValue *key_ptr = ir_address_from_load_or_generate_local(proc, key); key_ptr = ir_emit_conv(proc, key_ptr, t_rawptr); - irValue *hasher = ir_get_hasher_proc_for_type(proc->module, key_type); - - auto args = array_make<irValue *>(permanent_allocator(), 2); - args[0] = key_ptr; - args[1] = ir_value_constant(t_uintptr, exact_value_i64(0)); - irValue *hashed_key = ir_emit_call(proc, hasher, args); + irValue *hashed_key = ir_const_hash(proc->module, key, key_type); + if (hashed_key == nullptr) { + irValue *hasher = ir_get_hasher_proc_for_type(proc->module, key_type); + auto args = array_make<irValue *>(permanent_allocator(), 2); + args[0] = key_ptr; + args[1] = ir_value_constant(t_uintptr, exact_value_i64(0)); + hashed_key = ir_emit_call(proc, hasher, args); + } ir_emit_store(proc, ir_emit_struct_ep(proc, v, 0), hashed_key); ir_emit_store(proc, ir_emit_struct_ep(proc, v, 1), key_ptr); |