authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-07 18:32:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-07 18:32:19-07:00
log87cdbb6c25f198fa88dcff530f34bfe3e9b14245
tree7464e006b76f61f7e4443e67abdd5ed627b3e821
parent6b3ce918db0bd73b5b1973b1baf358b6114810ed

improve performance for get_fn_type


4 files changed, 68 insertions(+), 51 deletions(-)

src/all_types.hpp+6-3
......@@ -810,11 +810,13 @@ struct AsmToken {
810810 int end;
811811};
812812
813// this struct is allocated with allocate_nonzero
813814struct FnTypeParamInfo {
814815 bool is_noalias;
815816 TypeTableEntry *type;
816817};
817818
819static const int fn_type_id_prealloc_param_info_count = 4;
818820struct FnTypeId {
819821 TypeTableEntry *return_type;
820822 FnTypeParamInfo *param_info;
......@@ -823,10 +825,11 @@ struct FnTypeId {
823825 bool is_naked;
824826 bool is_cold;
825827 bool is_extern;
828 FnTypeParamInfo prealloc_param_info[fn_type_id_prealloc_param_info_count];
826829};
827830
828uint32_t fn_type_id_hash(FnTypeId);
829bool fn_type_id_eql(FnTypeId a, FnTypeId b);
831uint32_t fn_type_id_hash(FnTypeId*);
832bool fn_type_id_eql(FnTypeId *a, FnTypeId *b);
830833
831834
832835struct TypeTableEntryPointer {
......@@ -1061,7 +1064,7 @@ struct CodeGen {
10611064 HashMap<Buf *, BuiltinFnEntry *, buf_hash, buf_eql_buf> builtin_fn_table;
10621065 HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> primitive_type_table;
10631066 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> unresolved_top_level_decls;
1064 HashMap<FnTypeId, TypeTableEntry *, fn_type_id_hash, fn_type_id_eql> fn_type_table;
1067 HashMap<FnTypeId *, TypeTableEntry *, fn_type_id_hash, fn_type_id_eql> fn_type_table;
10651068 HashMap<Buf *, ErrorTableEntry *, buf_hash, buf_eql_buf> error_table;
10661069
10671070 uint32_t next_unresolved_index;
src/analyze.cpp+54-45
......@@ -541,18 +541,21 @@ TypeTableEntry *get_typedecl_type(CodeGen *g, const char *name, TypeTableEntry *
541541}
542542
543543// accepts ownership of fn_type_id memory
544TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId fn_type_id) {
544TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
545545 auto table_entry = g->fn_type_table.maybe_get(fn_type_id);
546546 if (table_entry) {
547547 return table_entry->value;
548548 }
549549
550550 TypeTableEntry *fn_type = new_type_table_entry(TypeTableEntryIdFn);
551 fn_type->data.fn.fn_type_id = fn_type_id;
551 fn_type->data.fn.fn_type_id = *fn_type_id;
552 if (fn_type_id->param_info == &fn_type_id->prealloc_param_info[0]) {
553 fn_type->data.fn.fn_type_id.param_info = &fn_type->data.fn.fn_type_id.prealloc_param_info[0];
554 }
552555
553 if (fn_type_id.is_cold) {
556 if (fn_type_id->is_cold) {
554557 fn_type->data.fn.calling_convention = LLVMColdCallConv;
555 } else if (fn_type_id.is_extern) {
558 } else if (fn_type_id->is_extern) {
556559 fn_type->data.fn.calling_convention = LLVMCCallConv;
557560 } else {
558561 fn_type->data.fn.calling_convention = LLVMFastCallConv;
......@@ -560,12 +563,12 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId fn_type_id) {
560563
561564 // populate the name of the type
562565 buf_resize(&fn_type->name, 0);
563 const char *extern_str = fn_type_id.is_extern ? "extern " : "";
564 const char *naked_str = fn_type_id.is_naked ? "naked " : "";
565 const char *cold_str = fn_type_id.is_cold ? "cold " : "";
566 const char *extern_str = fn_type_id->is_extern ? "extern " : "";
567 const char *naked_str = fn_type_id->is_naked ? "naked " : "";
568 const char *cold_str = fn_type_id->is_cold ? "cold " : "";
566569 buf_appendf(&fn_type->name, "%s%s%sfn(", extern_str, naked_str, cold_str);
567 for (int i = 0; i < fn_type_id.param_count; i += 1) {
568 FnTypeParamInfo *param_info = &fn_type_id.param_info[i];
570 for (int i = 0; i < fn_type_id->param_count; i += 1) {
571 FnTypeParamInfo *param_info = &fn_type_id->param_info[i];
569572
570573 TypeTableEntry *param_type = param_info->type;
571574 const char *comma = (i == 0) ? "" : ", ";
......@@ -573,42 +576,42 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId fn_type_id) {
573576 buf_appendf(&fn_type->name, "%s%s%s", comma, noalias_str, buf_ptr(&param_type->name));
574577 }
575578
576 if (fn_type_id.is_var_args) {
577 const char *comma = (fn_type_id.param_count == 0) ? "" : ", ";
579 if (fn_type_id->is_var_args) {
580 const char *comma = (fn_type_id->param_count == 0) ? "" : ", ";
578581 buf_appendf(&fn_type->name, "%s...", comma);
579582 }
580583 buf_appendf(&fn_type->name, ")");
581 if (fn_type_id.return_type->id != TypeTableEntryIdVoid) {
582 buf_appendf(&fn_type->name, " -> %s", buf_ptr(&fn_type_id.return_type->name));
584 if (fn_type_id->return_type->id != TypeTableEntryIdVoid) {
585 buf_appendf(&fn_type->name, " -> %s", buf_ptr(&fn_type_id->return_type->name));
583586 }
584587
585588
586589 // next, loop over the parameters again and compute debug information
587590 // and codegen information
588 bool first_arg_return = handle_is_ptr(fn_type_id.return_type);
591 bool first_arg_return = handle_is_ptr(fn_type_id->return_type);
589592 // +1 for maybe making the first argument the return value
590 LLVMTypeRef *gen_param_types = allocate<LLVMTypeRef>(1 + fn_type_id.param_count);
593 LLVMTypeRef *gen_param_types = allocate<LLVMTypeRef>(1 + fn_type_id->param_count);
591594 // +1 because 0 is the return type and +1 for maybe making first arg ret val
592 LLVMZigDIType **param_di_types = allocate<LLVMZigDIType*>(2 + fn_type_id.param_count);
593 param_di_types[0] = fn_type_id.return_type->di_type;
595 LLVMZigDIType **param_di_types = allocate<LLVMZigDIType*>(2 + fn_type_id->param_count);
596 param_di_types[0] = fn_type_id->return_type->di_type;
594597 int gen_param_index = 0;
595598 TypeTableEntry *gen_return_type;
596599 if (first_arg_return) {
597 TypeTableEntry *gen_type = get_pointer_to_type(g, fn_type_id.return_type, false);
600 TypeTableEntry *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false);
598601 gen_param_types[gen_param_index] = gen_type->type_ref;
599602 gen_param_index += 1;
600603 // after the gen_param_index += 1 because 0 is the return type
601604 param_di_types[gen_param_index] = gen_type->di_type;
602605 gen_return_type = g->builtin_types.entry_void;
603 } else if (!type_has_bits(fn_type_id.return_type)) {
606 } else if (!type_has_bits(fn_type_id->return_type)) {
604607 gen_return_type = g->builtin_types.entry_void;
605608 } else {
606 gen_return_type = fn_type_id.return_type;
609 gen_return_type = fn_type_id->return_type;
607610 }
608611 fn_type->data.fn.gen_return_type = gen_return_type;
609612
610 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id.param_count);
611 for (int i = 0; i < fn_type_id.param_count; i += 1) {
613 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count);
614 for (int i = 0; i < fn_type_id->param_count; i += 1) {
612615 FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i];
613616 TypeTableEntry *type_entry = src_param_info->type;
614617 FnGenParamInfo *gen_param_info = &fn_type->data.fn.gen_param_info[i];
......@@ -639,13 +642,13 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId fn_type_id) {
639642 fn_type->data.fn.gen_param_count = gen_param_index;
640643
641644 fn_type->data.fn.raw_type_ref = LLVMFunctionType(gen_return_type->type_ref,
642 gen_param_types, gen_param_index, fn_type_id.is_var_args);
645 gen_param_types, gen_param_index, fn_type_id->is_var_args);
643646 fn_type->type_ref = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);
644647 LLVMZigDIFile *di_file = nullptr;
645648 fn_type->di_type = LLVMZigCreateSubroutineType(g->dbuilder, di_file,
646649 param_di_types, gen_param_index + 1, 0);
647650
648 g->fn_type_table.put(fn_type_id, fn_type);
651 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);
649652
650653 return fn_type;
651654}
......@@ -747,7 +750,13 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
747750 fn_type_id.is_naked = is_naked;
748751 fn_type_id.is_cold = is_cold;
749752 fn_type_id.param_count = node->data.fn_proto.params.length;
750 fn_type_id.param_info = allocate<FnTypeParamInfo>(fn_type_id.param_count);
753
754 if (fn_type_id.param_count > fn_type_id_prealloc_param_info_count) {
755 fn_type_id.param_info = allocate_nonzero<FnTypeParamInfo>(fn_type_id.param_count);
756 } else {
757 fn_type_id.param_info = &fn_type_id.prealloc_param_info[0];
758 }
759
751760 fn_type_id.is_var_args = fn_proto->is_var_args;
752761 fn_type_id.return_type = analyze_type_expr(g, import, import->block_context, node->data.fn_proto.return_type);
753762
......@@ -800,7 +809,7 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
800809 return g->builtin_types.entry_invalid;
801810 }
802811
803 return get_fn_type(g, fn_type_id);
812 return get_fn_type(g, &fn_type_id);
804813}
805814
806815
......@@ -5855,35 +5864,35 @@ static uint32_t hash_ptr(void *ptr) {
58555864 return a ^ b;
58565865}
58575866
5858uint32_t fn_type_id_hash(FnTypeId id) {
5867uint32_t fn_type_id_hash(FnTypeId *id) {
58595868 uint32_t result = 0;
5860 result += id.is_extern ? 3349388391 : 0;
5861 result += id.is_naked ? 608688877 : 0;
5862 result += id.is_cold ? 3605523458 : 0;
5863 result += id.is_var_args ? 1931444534 : 0;
5864 result += hash_ptr(id.return_type);
5865 result += id.param_count;
5866 for (int i = 0; i < id.param_count; i += 1) {
5867 FnTypeParamInfo *info = &id.param_info[i];
5869 result += id->is_extern ? 3349388391 : 0;
5870 result += id->is_naked ? 608688877 : 0;
5871 result += id->is_cold ? 3605523458 : 0;
5872 result += id->is_var_args ? 1931444534 : 0;
5873 result += hash_ptr(id->return_type);
5874 result += id->param_count;
5875 for (int i = 0; i < id->param_count; i += 1) {
5876 FnTypeParamInfo *info = &id->param_info[i];
58685877 result += info->is_noalias ? 892356923 : 0;
58695878 result += hash_ptr(info->type);
58705879 }
58715880 return result;
58725881}
58735882
5874bool fn_type_id_eql(FnTypeId a, FnTypeId b) {
5875 if (a.is_extern != b.is_extern ||
5876 a.is_naked != b.is_naked ||
5877 a.is_cold != b.is_cold ||
5878 a.return_type != b.return_type ||
5879 a.is_var_args != b.is_var_args ||
5880 a.param_count != b.param_count)
5883bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) {
5884 if (a->is_extern != b->is_extern ||
5885 a->is_naked != b->is_naked ||
5886 a->is_cold != b->is_cold ||
5887 a->return_type != b->return_type ||
5888 a->is_var_args != b->is_var_args ||
5889 a->param_count != b->param_count)
58815890 {
58825891 return false;
58835892 }
5884 for (int i = 0; i < a.param_count; i += 1) {
5885 FnTypeParamInfo *a_param_info = &a.param_info[i];
5886 FnTypeParamInfo *b_param_info = &b.param_info[i];
5893 for (int i = 0; i < a->param_count; i += 1) {
5894 FnTypeParamInfo *a_param_info = &a->param_info[i];
5895 FnTypeParamInfo *b_param_info = &b->param_info[i];
58875896
58885897 if (a_param_info->type != b_param_info->type) {
58895898 return false;
src/analyze.hpp+1-1
......@@ -23,7 +23,7 @@ TypeTableEntry *get_int_type(CodeGen *g, bool is_signed, int size_in_bits);
2323TypeTableEntry **get_c_int_type_ptr(CodeGen *g, CIntType c_int_type);
2424TypeTableEntry *get_c_int_type(CodeGen *g, CIntType c_int_type);
2525TypeTableEntry *get_typedecl_type(CodeGen *g, const char *name, TypeTableEntry *child_type);
26TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId fn_type_id);
26TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id);
2727TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type);
2828TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size);
2929TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import,
src/parseh.cpp+7-2
......@@ -506,7 +506,12 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const
506506 }
507507 }
508508
509 fn_type_id.param_info = allocate<FnTypeParamInfo>(fn_type_id.param_count);
509 if (fn_type_id.param_count > fn_type_id_prealloc_param_info_count) {
510 fn_type_id.param_info = allocate_nonzero<FnTypeParamInfo>(fn_type_id.param_count);
511 } else {
512 fn_type_id.param_info = &fn_type_id.prealloc_param_info[0];
513 }
514
510515 for (int i = 0; i < fn_type_id.param_count; i += 1) {
511516 QualType qt = fn_proto_ty->getParamType(i);
512517 TypeTableEntry *param_type = resolve_qual_type(c, qt, decl);
......@@ -521,7 +526,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const
521526 param_info->is_noalias = qt.isRestrictQualified();
522527 }
523528
524 return get_fn_type(c->codegen, fn_type_id);
529 return get_fn_type(c->codegen, &fn_type_id);
525530 }
526531 case Type::Record:
527532 {