CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/MIPS/RiscV/RiscVRegCache.cpp
Views: 1401
1
// Copyright (c) 2023- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#ifndef offsetof
19
#include <cstddef>
20
#endif
21
22
#include "Common/CPUDetect.h"
23
#include "Core/MIPS/IR/IRInst.h"
24
#include "Core/MIPS/IR/IRAnalysis.h"
25
#include "Core/MIPS/RiscV/RiscVRegCache.h"
26
#include "Core/MIPS/JitCommon/JitState.h"
27
#include "Core/Reporting.h"
28
29
using namespace RiscVGen;
30
using namespace RiscVJitConstants;
31
32
RiscVRegCache::RiscVRegCache(MIPSComp::JitOptions *jo)
33
: IRNativeRegCacheBase(jo) {
34
// TODO: Update these when using RISC-V V.
35
config_.totalNativeRegs = NUM_RVGPR + NUM_RVFPR;
36
config_.mapUseVRegs = false;
37
config_.mapFPUSIMD = false;
38
}
39
40
void RiscVRegCache::Init(RiscVEmitter *emitter) {
41
emit_ = emitter;
42
}
43
44
void RiscVRegCache::SetupInitialRegs() {
45
IRNativeRegCacheBase::SetupInitialRegs();
46
47
// Treat R_ZERO a bit specially, but it's basically static alloc too.
48
nrInitial_[R_ZERO].mipsReg = MIPS_REG_ZERO;
49
nrInitial_[R_ZERO].normalized32 = true;
50
51
// Since we also have a fixed zero, mark it as a static allocation.
52
mrInitial_[MIPS_REG_ZERO].loc = MIPSLoc::REG_IMM;
53
mrInitial_[MIPS_REG_ZERO].nReg = R_ZERO;
54
mrInitial_[MIPS_REG_ZERO].imm = 0;
55
mrInitial_[MIPS_REG_ZERO].isStatic = true;
56
}
57
58
const int *RiscVRegCache::GetAllocationOrder(MIPSLoc type, MIPSMap flags, int &count, int &base) const {
59
base = X0;
60
61
if (type == MIPSLoc::REG) {
62
// X8 and X9 are the most ideal for static alloc because they can be used with compression.
63
// Otherwise we stick to saved regs - might not be necessary.
64
static const int allocationOrder[] = {
65
X8, X9, X12, X13, X14, X15, X5, X6, X7, X16, X17, X18, X19, X20, X21, X22, X23, X28, X29, X30, X31,
66
};
67
static const int allocationOrderStaticAlloc[] = {
68
X12, X13, X14, X15, X5, X6, X7, X16, X17, X21, X22, X23, X28, X29, X30, X31,
69
};
70
71
if (jo_->useStaticAlloc) {
72
count = ARRAY_SIZE(allocationOrderStaticAlloc);
73
return allocationOrderStaticAlloc;
74
} else {
75
count = ARRAY_SIZE(allocationOrder);
76
return allocationOrder;
77
}
78
} else if (type == MIPSLoc::FREG) {
79
// F8 through F15 are used for compression, so they are great.
80
static const int allocationOrder[] = {
81
F8, F9, F10, F11, F12, F13, F14, F15,
82
F0, F1, F2, F3, F4, F5, F6, F7,
83
F16, F17, F18, F19, F20, F21, F22, F23, F24, F25, F26, F27, F28, F29, F30, F31,
84
};
85
86
count = ARRAY_SIZE(allocationOrder);
87
return allocationOrder;
88
} else {
89
_assert_msg_(false, "Allocation order not yet implemented");
90
count = 0;
91
return nullptr;
92
}
93
}
94
95
const RiscVRegCache::StaticAllocation *RiscVRegCache::GetStaticAllocations(int &count) const {
96
static const StaticAllocation allocs[] = {
97
{ MIPS_REG_SP, X8, MIPSLoc::REG, true },
98
{ MIPS_REG_V0, X9, MIPSLoc::REG },
99
{ MIPS_REG_V1, X18, MIPSLoc::REG },
100
{ MIPS_REG_A0, X19, MIPSLoc::REG },
101
{ MIPS_REG_RA, X20, MIPSLoc::REG },
102
};
103
104
if (jo_->useStaticAlloc) {
105
count = ARRAY_SIZE(allocs);
106
return allocs;
107
}
108
return IRNativeRegCacheBase::GetStaticAllocations(count);
109
}
110
111
void RiscVRegCache::EmitLoadStaticRegisters() {
112
int count;
113
const StaticAllocation *allocs = GetStaticAllocations(count);
114
for (int i = 0; i < count; i++) {
115
int offset = GetMipsRegOffset(allocs[i].mr);
116
if (allocs[i].pointerified && jo_->enablePointerify) {
117
emit_->LWU((RiscVReg)allocs[i].nr, CTXREG, offset);
118
emit_->ADD((RiscVReg)allocs[i].nr, (RiscVReg)allocs[i].nr, MEMBASEREG);
119
} else {
120
emit_->LW((RiscVReg)allocs[i].nr, CTXREG, offset);
121
}
122
}
123
}
124
125
void RiscVRegCache::EmitSaveStaticRegisters() {
126
int count;
127
const StaticAllocation *allocs = GetStaticAllocations(count);
128
// This only needs to run once (by Asm) so checks don't need to be fast.
129
for (int i = 0; i < count; i++) {
130
int offset = GetMipsRegOffset(allocs[i].mr);
131
emit_->SW((RiscVReg)allocs[i].nr, CTXREG, offset);
132
}
133
}
134
135
void RiscVRegCache::FlushBeforeCall() {
136
// These registers are not preserved by function calls.
137
// They match between X0 and F0, conveniently.
138
for (int i = 5; i <= 7; ++i) {
139
FlushNativeReg(X0 + i);
140
FlushNativeReg(F0 + i);
141
}
142
for (int i = 10; i <= 17; ++i) {
143
FlushNativeReg(X0 + i);
144
FlushNativeReg(F0 + i);
145
}
146
for (int i = 28; i <= 31; ++i) {
147
FlushNativeReg(X0 + i);
148
FlushNativeReg(F0 + i);
149
}
150
}
151
152
bool RiscVRegCache::IsNormalized32(IRReg mipsReg) {
153
_dbg_assert_(IsValidGPR(mipsReg));
154
if (XLEN == 32)
155
return true;
156
if (mr[mipsReg].loc == MIPSLoc::REG || mr[mipsReg].loc == MIPSLoc::REG_IMM) {
157
return nr[mr[mipsReg].nReg].normalized32;
158
}
159
return false;
160
}
161
162
RiscVGen::RiscVReg RiscVRegCache::Normalize32(IRReg mipsReg, RiscVGen::RiscVReg destReg) {
163
_dbg_assert_(IsValidGPR(mipsReg));
164
_dbg_assert_(destReg == INVALID_REG || (destReg > X0 && destReg <= X31));
165
166
RiscVReg reg = (RiscVReg)mr[mipsReg].nReg;
167
if (XLEN == 32)
168
return reg;
169
170
switch (mr[mipsReg].loc) {
171
case MIPSLoc::IMM:
172
case MIPSLoc::MEM:
173
_assert_msg_(false, "Cannot normalize an imm or mem");
174
return INVALID_REG;
175
176
case MIPSLoc::REG:
177
case MIPSLoc::REG_IMM:
178
if (!nr[mr[mipsReg].nReg].normalized32) {
179
if (destReg == INVALID_REG) {
180
emit_->SEXT_W((RiscVReg)mr[mipsReg].nReg, (RiscVReg)mr[mipsReg].nReg);
181
nr[mr[mipsReg].nReg].normalized32 = true;
182
nr[mr[mipsReg].nReg].pointerified = false;
183
} else {
184
emit_->SEXT_W(destReg, (RiscVReg)mr[mipsReg].nReg);
185
}
186
} else if (destReg != INVALID_REG) {
187
emit_->SEXT_W(destReg, (RiscVReg)mr[mipsReg].nReg);
188
}
189
break;
190
191
case MIPSLoc::REG_AS_PTR:
192
_dbg_assert_(nr[mr[mipsReg].nReg].normalized32 == false);
193
if (destReg == INVALID_REG) {
194
// If we can pointerify, SEXT_W will be enough.
195
if (!jo_->enablePointerify)
196
AdjustNativeRegAsPtr(mr[mipsReg].nReg, false);
197
emit_->SEXT_W((RiscVReg)mr[mipsReg].nReg, (RiscVReg)mr[mipsReg].nReg);
198
mr[mipsReg].loc = MIPSLoc::REG;
199
nr[mr[mipsReg].nReg].normalized32 = true;
200
nr[mr[mipsReg].nReg].pointerified = false;
201
} else if (!jo_->enablePointerify) {
202
emit_->SUB(destReg, (RiscVReg)mr[mipsReg].nReg, MEMBASEREG);
203
emit_->SEXT_W(destReg, destReg);
204
} else {
205
emit_->SEXT_W(destReg, (RiscVReg)mr[mipsReg].nReg);
206
}
207
break;
208
209
default:
210
_assert_msg_(false, "Should not normalize32 floats");
211
break;
212
}
213
214
return destReg == INVALID_REG ? reg : destReg;
215
}
216
217
RiscVReg RiscVRegCache::TryMapTempImm(IRReg r) {
218
_dbg_assert_(IsValidGPR(r));
219
// If already mapped, no need for a temporary.
220
if (IsGPRMapped(r)) {
221
return R(r);
222
}
223
224
if (mr[r].loc == MIPSLoc::IMM) {
225
if (mr[r].imm == 0) {
226
return R_ZERO;
227
}
228
229
// Try our luck - check for an exact match in another rvreg.
230
for (int i = 0; i < TOTAL_MAPPABLE_IRREGS; ++i) {
231
if (mr[i].loc == MIPSLoc::REG_IMM && mr[i].imm == mr[r].imm) {
232
// Awesome, let's just use this reg.
233
return (RiscVReg)mr[i].nReg;
234
}
235
}
236
}
237
238
return INVALID_REG;
239
}
240
241
RiscVReg RiscVRegCache::GetAndLockTempGPR() {
242
RiscVReg reg = (RiscVReg)AllocateReg(MIPSLoc::REG, MIPSMap::INIT);
243
if (reg != INVALID_REG) {
244
nr[reg].tempLockIRIndex = irIndex_;
245
}
246
return reg;
247
}
248
249
RiscVReg RiscVRegCache::MapWithFPRTemp(const IRInst &inst) {
250
return (RiscVReg)MapWithTemp(inst, MIPSLoc::FREG);
251
}
252
253
RiscVReg RiscVRegCache::MapGPR(IRReg mipsReg, MIPSMap mapFlags) {
254
_dbg_assert_(IsValidGPR(mipsReg));
255
256
// Okay, not mapped, so we need to allocate an RV register.
257
IRNativeReg nreg = MapNativeReg(MIPSLoc::REG, mipsReg, 1, mapFlags);
258
return (RiscVReg)nreg;
259
}
260
261
RiscVReg RiscVRegCache::MapGPRAsPointer(IRReg reg) {
262
return (RiscVReg)MapNativeRegAsPointer(reg);
263
}
264
265
RiscVReg RiscVRegCache::MapFPR(IRReg mipsReg, MIPSMap mapFlags) {
266
_dbg_assert_(IsValidFPR(mipsReg));
267
_dbg_assert_(mr[mipsReg + 32].loc == MIPSLoc::MEM || mr[mipsReg + 32].loc == MIPSLoc::FREG);
268
269
IRNativeReg nreg = MapNativeReg(MIPSLoc::FREG, mipsReg + 32, 1, mapFlags);
270
if (nreg != -1)
271
return (RiscVReg)nreg;
272
return INVALID_REG;
273
}
274
275
void RiscVRegCache::AdjustNativeRegAsPtr(IRNativeReg nreg, bool state) {
276
RiscVReg r = (RiscVReg)(X0 + nreg);
277
_assert_(r >= X0 && r <= X31);
278
if (state) {
279
#ifdef MASKED_PSP_MEMORY
280
// This destroys the value...
281
_dbg_assert_(!nr[nreg].isDirty);
282
emit_->SLLIW(r, r, 2);
283
emit_->SRLIW(r, r, 2);
284
emit_->ADD(r, r, MEMBASEREG);
285
#else
286
// Clear the top bits to be safe.
287
if (cpu_info.RiscV_Zba) {
288
emit_->ADD_UW(r, r, MEMBASEREG);
289
} else {
290
_assert_(XLEN == 64);
291
emit_->SLLI(r, r, 32);
292
emit_->SRLI(r, r, 32);
293
emit_->ADD(r, r, MEMBASEREG);
294
}
295
#endif
296
nr[nreg].normalized32 = false;
297
} else {
298
#ifdef MASKED_PSP_MEMORY
299
_dbg_assert_(!nr[nreg].isDirty);
300
#endif
301
emit_->SUB(r, r, MEMBASEREG);
302
nr[nreg].normalized32 = false;
303
}
304
}
305
306
bool RiscVRegCache::IsNativeRegCompatible(IRNativeReg nreg, MIPSLoc type, MIPSMap flags, int lanes) {
307
// No special flags except VREG, skip the check for a little speed.
308
if (type != MIPSLoc::VREG)
309
return true;
310
return IRNativeRegCacheBase::IsNativeRegCompatible(nreg, type, flags, lanes);
311
}
312
313
void RiscVRegCache::LoadNativeReg(IRNativeReg nreg, IRReg first, int lanes) {
314
RiscVReg r = (RiscVReg)(X0 + nreg);
315
_dbg_assert_(r > X0);
316
_dbg_assert_(first != MIPS_REG_ZERO);
317
if (r <= X31) {
318
_assert_(lanes == 1 || (lanes == 2 && first == IRREG_LO));
319
if (lanes == 1)
320
emit_->LW(r, CTXREG, GetMipsRegOffset(first));
321
else if (lanes == 2)
322
emit_->LD(r, CTXREG, GetMipsRegOffset(first));
323
else
324
_assert_(false);
325
nr[nreg].normalized32 = true;
326
} else {
327
_dbg_assert_(r >= F0 && r <= F31);
328
// Multilane not yet supported.
329
_assert_(lanes == 1);
330
if (mr[first].loc == MIPSLoc::FREG) {
331
emit_->FL(32, r, CTXREG, GetMipsRegOffset(first));
332
} else {
333
_assert_msg_(mr[first].loc == MIPSLoc::FREG, "Cannot store this type: %d", (int)mr[first].loc);
334
}
335
}
336
}
337
338
void RiscVRegCache::StoreNativeReg(IRNativeReg nreg, IRReg first, int lanes) {
339
RiscVReg r = (RiscVReg)(X0 + nreg);
340
_dbg_assert_(r > X0);
341
_dbg_assert_(first != MIPS_REG_ZERO);
342
if (r <= X31) {
343
// Multilane not yet supported.
344
_assert_(lanes == 1 || (lanes == 2 && first == IRREG_LO));
345
_assert_(mr[first].loc == MIPSLoc::REG || mr[first].loc == MIPSLoc::REG_IMM);
346
if (lanes == 1)
347
emit_->SW(r, CTXREG, GetMipsRegOffset(first));
348
else if (lanes == 2)
349
emit_->SD(r, CTXREG, GetMipsRegOffset(first));
350
else
351
_assert_(false);
352
} else {
353
_dbg_assert_(r >= F0 && r <= F31);
354
// Multilane not yet supported.
355
_assert_(lanes == 1);
356
if (mr[first].loc == MIPSLoc::FREG) {
357
emit_->FS(32, r, CTXREG, GetMipsRegOffset(first));
358
} else {
359
_assert_msg_(mr[first].loc == MIPSLoc::FREG, "Cannot store this type: %d", (int)mr[first].loc);
360
}
361
}
362
}
363
364
void RiscVRegCache::SetNativeRegValue(IRNativeReg nreg, uint32_t imm) {
365
RiscVReg r = (RiscVReg)(X0 + nreg);
366
if (r == R_ZERO && imm == 0)
367
return;
368
_dbg_assert_(r > X0 && r <= X31);
369
emit_->LI(r, (int32_t)imm);
370
371
// We always use 32-bit immediates, so this is normalized now.
372
nr[nreg].normalized32 = true;
373
}
374
375
void RiscVRegCache::StoreRegValue(IRReg mreg, uint32_t imm) {
376
_assert_(IsValidGPRNoZero(mreg));
377
// Try to optimize using a different reg.
378
RiscVReg storeReg = INVALID_REG;
379
380
// Zero is super easy.
381
if (imm == 0) {
382
storeReg = R_ZERO;
383
} else {
384
// Could we get lucky? Check for an exact match in another rvreg.
385
for (int i = 0; i < TOTAL_MAPPABLE_IRREGS; ++i) {
386
if (mr[i].loc == MIPSLoc::REG_IMM && mr[i].imm == imm) {
387
// Awesome, let's just store this reg.
388
storeReg = (RiscVReg)mr[i].nReg;
389
break;
390
}
391
}
392
393
if (storeReg == INVALID_REG) {
394
emit_->LI(SCRATCH1, imm);
395
storeReg = SCRATCH1;
396
}
397
}
398
399
emit_->SW(storeReg, CTXREG, GetMipsRegOffset(mreg));
400
}
401
402
RiscVReg RiscVRegCache::R(IRReg mipsReg) {
403
_dbg_assert_(IsValidGPR(mipsReg));
404
_dbg_assert_(mr[mipsReg].loc == MIPSLoc::REG || mr[mipsReg].loc == MIPSLoc::REG_IMM);
405
if (mr[mipsReg].loc == MIPSLoc::REG || mr[mipsReg].loc == MIPSLoc::REG_IMM) {
406
return (RiscVReg)mr[mipsReg].nReg;
407
} else {
408
ERROR_LOG_REPORT(Log::JIT, "Reg %i not in riscv reg", mipsReg);
409
return INVALID_REG; // BAAAD
410
}
411
}
412
413
RiscVReg RiscVRegCache::RPtr(IRReg mipsReg) {
414
_dbg_assert_(IsValidGPR(mipsReg));
415
_dbg_assert_(mr[mipsReg].loc == MIPSLoc::REG || mr[mipsReg].loc == MIPSLoc::REG_IMM || mr[mipsReg].loc == MIPSLoc::REG_AS_PTR);
416
if (mr[mipsReg].loc == MIPSLoc::REG_AS_PTR) {
417
return (RiscVReg)mr[mipsReg].nReg;
418
} else if (mr[mipsReg].loc == MIPSLoc::REG || mr[mipsReg].loc == MIPSLoc::REG_IMM) {
419
int rv = mr[mipsReg].nReg;
420
_dbg_assert_(nr[rv].pointerified);
421
if (nr[rv].pointerified) {
422
return (RiscVReg)mr[mipsReg].nReg;
423
} else {
424
ERROR_LOG(Log::JIT, "Tried to use a non-pointer register as a pointer");
425
return INVALID_REG;
426
}
427
} else {
428
ERROR_LOG_REPORT(Log::JIT, "Reg %i not in riscv reg", mipsReg);
429
return INVALID_REG; // BAAAD
430
}
431
}
432
433
RiscVReg RiscVRegCache::F(IRReg mipsReg) {
434
_dbg_assert_(IsValidFPR(mipsReg));
435
_dbg_assert_(mr[mipsReg + 32].loc == MIPSLoc::FREG);
436
if (mr[mipsReg + 32].loc == MIPSLoc::FREG) {
437
return (RiscVReg)mr[mipsReg + 32].nReg;
438
} else {
439
ERROR_LOG_REPORT(Log::JIT, "Reg %i not in riscv reg", mipsReg);
440
return INVALID_REG; // BAAAD
441
}
442
}
443
444