@@ -104,7 +104,7 @@ RVOP(jal, {
104104#if !RV32_HAS (JIT )
105105#define LOOKUP_OR_UPDATE_BRANCH_HISTORY_TABLE () \
106106 /* \
107- * lookup branch history table \
107+ * Direct-mapped branch history table lookup. \
108108 * \
109109 * When handling trap, the branch history table should not be lookup since \
110110 * it causes return from the trap_handler. \
@@ -116,20 +116,18 @@ RVOP(jal, {
116116 { \
117117 IIF(RV32_HAS(SYSTEM)(if (!rv->is_trapped && !reloc_enable_mmu), )) \
118118 { \
119- for (int i = 0; i < HISTORY_SIZE; i++) { \
120- if (ir->branch_table->PC[i] == PC) { \
121- MUST_TAIL return ir->branch_table->target[i]->impl( \
122- rv, ir->branch_table->target[i], cycle, PC); \
123- } \
119+ /* Direct-mapped lookup: O(1) instead of O(n) linear search */ \
120+ const uint32_t bht_idx = (PC >> 2 ) & (HISTORY_SIZE - 1 ); \
121+ if (ir -> branch_table -> PC [bht_idx ] == PC && \
122+ ir -> branch_table -> target [bht_idx ]) { \
123+ MUST_TAIL return ir -> branch_table -> target [bht_idx ]-> impl ( \
124+ rv , ir -> branch_table -> target [bht_idx ], cycle , PC ); \
124125 } \
125126 block_t * block = block_find (& rv -> block_map , PC ); \
126127 if (block ) { \
127- /* update branch history table */ \
128- ir -> branch_table -> PC [ir -> branch_table -> idx ] = PC ; \
129- ir -> branch_table -> target [ir -> branch_table -> idx ] = \
130- block -> ir_head ; \
131- ir -> branch_table -> idx = \
132- (ir -> branch_table -> idx + 1 ) % HISTORY_SIZE ; \
128+ /* Direct replacement at computed index */ \
129+ ir -> branch_table -> PC [bht_idx ] = PC ; \
130+ ir -> branch_table -> target [bht_idx ] = block -> ir_head ; \
133131 MUST_TAIL return block -> ir_head -> impl (rv , block -> ir_head , \
134132 cycle , PC ); \
135133 } \
@@ -141,23 +139,22 @@ RVOP(jal, {
141139 { \
142140 block_t *block = cache_get(rv->block_cache, PC, true); \
143141 if (block) { \
144- for (int i = 0; i < HISTORY_SIZE; i++) { \
145- if (ir->branch_table->PC[i] == PC) { \
146- IIF(RV32_HAS(SYSTEM))( \
147- if (ir->branch_table->satp[i] == rv->csr_satp), ) \
148- { \
149- ir->branch_table->times[i]++; \
150- if (cache_hot(rv->block_cache, PC)) \
151- goto end_op; \
152- } \
142+ /* Direct-mapped lookup: O(1) instead of O(n) linear search */ \
143+ const uint32_t bht_idx = ( PC >> 2 ) & ( HISTORY_SIZE - 1 ); \
144+ if ( ir -> branch_table -> PC [ bht_idx ] == PC ) { \
145+ IIF ( RV32_HAS ( SYSTEM ))( \
146+ if ( ir -> branch_table -> satp [ bht_idx ] == rv -> csr_satp ), ) \
147+ { \
148+ ir -> branch_table -> times [ bht_idx ] ++ ; \
149+ if ( cache_hot ( rv -> block_cache , PC )) \
150+ goto end_op ; \
153151 } \
154152 } \
155- /* update branch history table using LFU replacement */ \
156- int min_idx = bht_find_min_idx (ir -> branch_table ); \
157- ir -> branch_table -> times [min_idx ] = 1 ; \
158- ir -> branch_table -> PC [min_idx ] = PC ; \
153+ /* Direct replacement at computed index */ \
154+ ir -> branch_table -> times [bht_idx ] = 1 ; \
155+ ir -> branch_table -> PC [bht_idx ] = PC ; \
159156 IIF (RV32_HAS (SYSTEM ))( \
160- ir -> branch_table -> satp [min_idx ] = rv -> csr_satp , ); \
157+ ir -> branch_table -> satp [bht_idx ] = rv -> csr_satp , ); \
161158 if (cache_hot (rv -> block_cache , PC )) \
162159 goto end_op ; \
163160 MUST_TAIL return block -> ir_head -> impl (rv , block -> ir_head , cycle , \
0 commit comments