4512392dcbdc44250163904940ee47dd1c99e516
[pcsx_rearmed.git] / deps / lightrec / blockcache.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * Copyright (C) 2015-2021 Paul Cercueil <paul@crapouillou.net>
4  */
5
6 #include "blockcache.h"
7 #include "debug.h"
8 #include "lightrec-private.h"
9 #include "memmanager.h"
10
11 #include <stdbool.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 /* Must be power of two */
16 #define LUT_SIZE 0x4000
17
18 struct blockcache {
19         struct lightrec_state *state;
20         struct block * lut[LUT_SIZE];
21 };
22
23 u16 lightrec_get_lut_entry(const struct block *block)
24 {
25         return (kunseg(block->pc) >> 2) & (LUT_SIZE - 1);
26 }
27
28 struct block * lightrec_find_block(struct blockcache *cache, u32 pc)
29 {
30         struct block *block;
31
32         pc = kunseg(pc);
33
34         for (block = cache->lut[(pc >> 2) & (LUT_SIZE - 1)];
35              block; block = block->next)
36                 if (kunseg(block->pc) == pc)
37                         return block;
38
39         return NULL;
40 }
41
42 struct block * lightrec_find_block_from_lut(struct blockcache *cache,
43                                             u16 lut_entry, u32 addr_in_block)
44 {
45         struct block *block;
46         u32 pc;
47
48         addr_in_block = kunseg(addr_in_block);
49
50         for (block = cache->lut[lut_entry]; block; block = block->next) {
51                 pc = kunseg(block->pc);
52                 if (addr_in_block >= pc &&
53                     addr_in_block < pc + (block->nb_ops << 2))
54                         return block;
55         }
56
57         return NULL;
58 }
59
60 void remove_from_code_lut(struct blockcache *cache, struct block *block)
61 {
62         struct lightrec_state *state = cache->state;
63         u32 offset = lut_offset(block->pc);
64
65         if (block->function) {
66                 memset(&state->code_lut[offset], 0,
67                        block->nb_ops * sizeof(*state->code_lut));
68         }
69 }
70
71 void lightrec_register_block(struct blockcache *cache, struct block *block)
72 {
73         u32 pc = kunseg(block->pc);
74         struct block *old;
75
76         old = cache->lut[(pc >> 2) & (LUT_SIZE - 1)];
77         if (old)
78                 block->next = old;
79
80         cache->lut[(pc >> 2) & (LUT_SIZE - 1)] = block;
81
82         remove_from_code_lut(cache, block);
83 }
84
85 void lightrec_unregister_block(struct blockcache *cache, struct block *block)
86 {
87         u32 pc = kunseg(block->pc);
88         struct block *old = cache->lut[(pc >> 2) & (LUT_SIZE - 1)];
89
90         if (old == block) {
91                 cache->lut[(pc >> 2) & (LUT_SIZE - 1)] = old->next;
92                 return;
93         }
94
95         for (; old; old = old->next) {
96                 if (old->next == block) {
97                         old->next = block->next;
98                         return;
99                 }
100         }
101
102         pr_err("Block at PC 0x%x is not in cache\n", block->pc);
103 }
104
105 void lightrec_free_block_cache(struct blockcache *cache)
106 {
107         struct block *block, *next;
108         unsigned int i;
109
110         for (i = 0; i < LUT_SIZE; i++) {
111                 for (block = cache->lut[i]; block; block = next) {
112                         next = block->next;
113                         lightrec_free_block(cache->state, block);
114                 }
115         }
116
117         lightrec_free(cache->state, MEM_FOR_LIGHTREC, sizeof(*cache), cache);
118 }
119
120 struct blockcache * lightrec_blockcache_init(struct lightrec_state *state)
121 {
122         struct blockcache *cache;
123
124         cache = lightrec_calloc(state, MEM_FOR_LIGHTREC, sizeof(*cache));
125         if (!cache)
126                 return NULL;
127
128         cache->state = state;
129
130         return cache;
131 }
132
133 u32 lightrec_calculate_block_hash(const struct block *block)
134 {
135         const u32 *code = block->code;
136         u32 hash = 0xffffffff;
137         unsigned int i;
138
139         /* Jenkins one-at-a-time hash algorithm */
140         for (i = 0; i < block->nb_ops; i++) {
141                 hash += *code++;
142                 hash += (hash << 10);
143                 hash ^= (hash >> 6);
144         }
145
146         hash += (hash << 3);
147         hash ^= (hash >> 11);
148         hash += (hash << 15);
149
150         return hash;
151 }
152
153 bool lightrec_block_is_outdated(struct lightrec_state *state, struct block *block)
154 {
155         void **lut_entry = &state->code_lut[lut_offset(block->pc)];
156         bool outdated;
157
158         if (*lut_entry)
159                 return false;
160
161         outdated = block->hash != lightrec_calculate_block_hash(block);
162         if (likely(!outdated)) {
163                 /* The block was marked as outdated, but the content is still
164                  * the same */
165                 if (block->function)
166                         *lut_entry = block->function;
167                 else
168                         *lut_entry = state->get_next_block;
169         }
170
171         return outdated;
172 }