@@ -8563,15 +8563,40 @@ is properly maintained. Do the output as indicated by octl.
85638563#endif /* BACK_END_IS_C_GEN_BE */
85648564
85658565#if BACK_END_IS_C_GEN_BE || BACK_END_IS_CP_GEN_BE
8566- #if DEBUG
85678566
85688567STATIC_THREAD uintptr_t
85698568 unique_id_counter ;
85708569 /* A ticket counter used by
8571- generate_unique_id_for_il_pointer to generate stable
8570+ generate_unique_id_for_temporary to generate stable
85728571 unique identifiers for a given pointer. */
85738572
8574- using a_ptr_unique_id_map = Ptr_map < void * , uintptr_t , General_allocator > ;
8573+ namespace {
8574+
8575+ enum class a_temporary_key : uintptr_t {};
8576+ /* The key type of ptr_to_unique_id: an IL pointer
8577+ converted to an integer. It is a distinct type
8578+ only so that it can have its own hash_ptr. */
8579+
8580+ inline uintptr_t hash_ptr (a_temporary_key key )
8581+ /*
8582+ Return a hash value for the given temporary key that is appropriate for
8583+ Ptr_map. IL entries of a given kind tend to be allocated at regular intervals,
8584+ so the default pointer hash (essentially the address itself) clusters badly in
8585+ the low-order bits that Ptr_map uses for indexing. Mix all the bits of the
8586+ address into the low-order bits (Fibonacci hashing).
8587+ */
8588+ {
8589+ constexpr unsigned half_width = sizeof (uintptr_t ) * 4 ;
8590+ uintptr_t hash = (uintptr_t )key *
8591+ (uintptr_t )0x9E3779B97F4A7C15ull ;
8592+
8593+ return hash ^ (hash >> half_width );
8594+ } /* hash_ptr */
8595+
8596+ } /* namespace */
8597+
8598+ using a_ptr_unique_id_map = Ptr_map < a_temporary_key , uintptr_t ,
8599+ General_allocator > ;
85758600 /* The type of ptr_to_unique_id used to map an
85768601 arbitrary pointer to a unique identifier from the
85778602 unique_id_counter. */
@@ -8580,48 +8605,40 @@ STATIC_THREAD a_ptr_unique_id_map
85808605 * ptr_to_unique_id ;
85818606 /* A map of a given pointer to its unique identifiers
85828607 from the unique_id_counter used by
8583- generate_unique_id_for_il_pointer . */
8608+ generate_unique_id_for_temporary . */
85848609
85858610
85868611static uintptr_t generate_unique_id_for_temporary (void * ptr )
85878612/*
8588- Return an identifier unique to the current translation unit for the given IL
8589- pointer.
8613+ Return an identifier unique to the current generated file for the given IL
8614+ pointer. Identifiers are assigned in order of first use, so (unlike the IL
8615+ pointer itself) they do not depend on where the IL happens to be allocated,
8616+ and the generated output is reproducible from run to run.
85908617*/
85918618{
85928619 uintptr_t result ;
85938620
85948621 if (ptr == NULL ) {
85958622 result = 0 ;
8596- } else if (ptr_to_unique_id -> get (ptr ) == 0 ) {
8597- result = ++ unique_id_counter ;
8598- ptr_to_unique_id -> map (ptr , result );
85998623 } else {
8600- result = ptr_to_unique_id -> get (ptr );
8624+ a_temporary_key key = (a_temporary_key )(uintptr_t )ptr ;
8625+
8626+ result = ptr_to_unique_id -> get (key );
8627+ if (result == 0 ) {
8628+ result = ++ unique_id_counter ;
8629+ ptr_to_unique_id -> map (key , result );
8630+ } /* if */
86018631 } /* if */
86028632 return result ;
86038633} /* generate_unique_id_for_temporary */
86048634
8605- #endif /* DEBUG */
86068635
86078636a_temp_var_name_buffer form_temporary_name_for_back_end (void * ptr )
86088637/*
86098638Return a temporary name generated for the given IL pointer.
86108639*/
86118640{
8612- uintptr_t unique_id ;
8613-
8614- #if DEBUG
8615- if (db_active ) {
8616- unique_id = generate_unique_id_for_temporary (ptr );
8617- } else
8618- #endif /* DEBUG */
8619- /* Do not add code here. */
8620- {
8621- unique_id = unique_id_for_il_pointer (ptr );
8622- } /* if */
8623-
8624- a_temp_var_name_buffer result ("__T" , unique_id );
8641+ a_temp_var_name_buffer result ("__T" , generate_unique_id_for_temporary (ptr ));
86258642 return result ;
86268643} /* form_temporary_name_for_back_end */
86278644
@@ -8632,11 +8649,9 @@ Initialize the common logic for a C-like language generating back end. These
86328649are initializations that must be redone for each generated file.
86338650*/
86348651{
8635- #if DEBUG
86368652 unique_id_counter = 0 ;
86378653 ptr_to_unique_id = alloc_general_of_type (a_ptr_unique_id_map );
86388654 construct (ptr_to_unique_id , /*mask_width=*/ 10u );
8639- #endif /* DEBUG */
86408655} /* il_to_str_back_end_file_init */
86418656
86428657#endif /* BACK_END_IS_C_GEN_BE || BACK_END_IS_CP_GEN_BE */
0 commit comments