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/ARM64/Arm64RegCache.cpp
Views: 1401
1
// Copyright (c) 2012- 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
#include "ppsspp_config.h"
19
#if PPSSPP_ARCH(ARM64)
20
21
#include "Common/Log.h"
22
#include "Core/MemMap.h"
23
#include "Core/MIPS/ARM64/Arm64RegCache.h"
24
#include "Core/MIPS/ARM64/Arm64Jit.h"
25
#include "Core/MIPS/MIPSAnalyst.h"
26
#include "Core/Reporting.h"
27
#include "Common/Arm64Emitter.h"
28
29
#ifndef offsetof
30
#include "stddef.h"
31
#endif
32
33
using namespace Arm64Gen;
34
using namespace Arm64JitConstants;
35
36
Arm64RegCache::Arm64RegCache(MIPSState *mipsState, MIPSComp::JitState *js, MIPSComp::JitOptions *jo) : mips_(mipsState), js_(js), jo_(jo) {
37
}
38
39
void Arm64RegCache::Init(ARM64XEmitter *emitter) {
40
emit_ = emitter;
41
}
42
43
void Arm64RegCache::Start(MIPSAnalyst::AnalysisResults &stats) {
44
for (int i = 0; i < NUM_ARMREG; i++) {
45
ar[i].mipsReg = MIPS_REG_INVALID;
46
ar[i].isDirty = false;
47
ar[i].pointerified = false;
48
ar[i].tempLocked = false;
49
}
50
for (int i = 0; i < NUM_MIPSREG; i++) {
51
mr[i].loc = ML_MEM;
52
mr[i].reg = INVALID_REG;
53
mr[i].imm = -1;
54
mr[i].spillLock = false;
55
mr[i].isStatic = false;
56
}
57
int numStatics;
58
const StaticAllocation *statics = GetStaticAllocations(numStatics);
59
for (int i = 0; i < numStatics; i++) {
60
ar[statics[i].ar].mipsReg = statics[i].mr;
61
ar[statics[i].ar].pointerified = statics[i].pointerified && jo_->enablePointerify;
62
mr[statics[i].mr].loc = ML_ARMREG;
63
mr[statics[i].mr].reg = statics[i].ar;
64
mr[statics[i].mr].isStatic = true;
65
mr[statics[i].mr].spillLock = true;
66
}
67
}
68
69
const ARM64Reg *Arm64RegCache::GetMIPSAllocationOrder(int &count) {
70
// See register alloc remarks in Arm64Asm.cpp
71
72
// W19-W23 are most suitable for static allocation. Those that are chosen for static allocation
73
// should be omitted here and added in GetStaticAllocations.
74
static const ARM64Reg allocationOrder[] = {
75
W19, W20, W21, W22, W23, W0, W1, W2, W3, W4, W5, W6, W7, W8, W9, W10, W11, W12, W13, W14, W15,
76
};
77
static const ARM64Reg allocationOrderStaticAlloc[] = {
78
W0, W1, W2, W3, W4, W5, W6, W7, W8, W9, W10, W11, W12, W13, W14, W15,
79
};
80
81
if (jo_->useStaticAlloc) {
82
count = ARRAY_SIZE(allocationOrderStaticAlloc);
83
return allocationOrderStaticAlloc;
84
} else {
85
count = ARRAY_SIZE(allocationOrder);
86
return allocationOrder;
87
}
88
}
89
90
const Arm64RegCache::StaticAllocation *Arm64RegCache::GetStaticAllocations(int &count) {
91
static const StaticAllocation allocs[] = {
92
{MIPS_REG_SP, W19, true},
93
{MIPS_REG_V0, W20},
94
{MIPS_REG_V1, W22},
95
{MIPS_REG_A0, W21},
96
{MIPS_REG_RA, W23},
97
};
98
99
if (jo_->useStaticAlloc) {
100
count = ARRAY_SIZE(allocs);
101
return allocs;
102
} else {
103
count = 0;
104
return nullptr;
105
}
106
}
107
108
void Arm64RegCache::EmitLoadStaticRegisters() {
109
int count;
110
const StaticAllocation *allocs = GetStaticAllocations(count);
111
// TODO: Use LDP when possible.
112
for (int i = 0; i < count; i++) {
113
int offset = GetMipsRegOffset(allocs[i].mr);
114
emit_->LDR(INDEX_UNSIGNED, allocs[i].ar, CTXREG, offset);
115
if (allocs[i].pointerified && jo_->enablePointerify) {
116
emit_->MOVK(EncodeRegTo64(allocs[i].ar), ((uint64_t)Memory::base) >> 32, SHIFT_32);
117
}
118
}
119
}
120
121
void Arm64RegCache::EmitSaveStaticRegisters() {
122
int count;
123
const StaticAllocation *allocs = GetStaticAllocations(count);
124
// TODO: Use STP when possible.
125
// This only needs to run once (by Asm) so checks don't need to be fast.
126
for (int i = 0; i < count; i++) {
127
int offset = GetMipsRegOffset(allocs[i].mr);
128
emit_->STR(INDEX_UNSIGNED, allocs[i].ar, CTXREG, offset);
129
}
130
}
131
132
void Arm64RegCache::FlushBeforeCall() {
133
// These registers are not preserved by function calls.
134
for (int i = 0; i < 19; ++i) {
135
FlushArmReg(ARM64Reg(W0 + i));
136
}
137
FlushArmReg(W30);
138
}
139
140
bool Arm64RegCache::IsInRAM(MIPSGPReg reg) {
141
return mr[reg].loc == ML_MEM;
142
}
143
144
bool Arm64RegCache::IsMapped(MIPSGPReg mipsReg) {
145
return mr[mipsReg].loc == ML_ARMREG || mr[mipsReg].loc == ML_ARMREG_IMM;
146
}
147
148
bool Arm64RegCache::IsMappedAsPointer(MIPSGPReg mipsReg) {
149
if (mr[mipsReg].loc == ML_ARMREG) {
150
return ar[mr[mipsReg].reg].pointerified;
151
} else if (mr[mipsReg].loc == ML_ARMREG_IMM) {
152
if (ar[mr[mipsReg].reg].pointerified) {
153
ERROR_LOG(Log::JIT, "Really shouldn't be pointerified here");
154
}
155
} else if (mr[mipsReg].loc == ML_ARMREG_AS_PTR) {
156
return true;
157
}
158
return false;
159
}
160
161
void Arm64RegCache::MarkDirty(ARM64Reg reg) {
162
ar[reg].isDirty = true;
163
}
164
165
void Arm64RegCache::SetRegImm(ARM64Reg reg, u64 imm) {
166
if (reg == INVALID_REG) {
167
ERROR_LOG(Log::JIT, "SetRegImm to invalid register: at %08x", js_->compilerPC);
168
return;
169
}
170
// On ARM64, at least Cortex A57, good old MOVT/MOVW (MOVK in 64-bit) is really fast.
171
emit_->MOVI2R(reg, imm);
172
// ar[reg].pointerified = false;
173
}
174
175
void Arm64RegCache::MapRegTo(ARM64Reg reg, MIPSGPReg mipsReg, int mapFlags) {
176
if (mr[mipsReg].isStatic) {
177
ERROR_LOG(Log::JIT, "Cannot MapRegTo static register %d", mipsReg);
178
return;
179
}
180
ar[reg].isDirty = (mapFlags & MAP_DIRTY) ? true : false;
181
if ((mapFlags & MAP_NOINIT) != MAP_NOINIT) {
182
if (mipsReg == MIPS_REG_ZERO) {
183
// If we get a request to map the zero register, at least we won't spend
184
// time on a memory access...
185
emit_->MOVI2R(reg, 0);
186
187
// This way, if we SetImm() it, we'll keep it.
188
mr[mipsReg].loc = ML_ARMREG_IMM;
189
mr[mipsReg].imm = 0;
190
} else {
191
switch (mr[mipsReg].loc) {
192
case ML_MEM:
193
{
194
int offset = GetMipsRegOffset(mipsReg);
195
ARM64Reg loadReg = reg;
196
// INFO_LOG(Log::JIT, "MapRegTo %d mips: %d offset %d", (int)reg, mipsReg, offset);
197
if (mipsReg == MIPS_REG_LO) {
198
loadReg = EncodeRegTo64(loadReg);
199
}
200
// TODO: Scan ahead / hint when loading multiple regs?
201
// We could potentially LDP if mipsReg + 1 or mipsReg - 1 is needed.
202
emit_->LDR(INDEX_UNSIGNED, loadReg, CTXREG, offset);
203
mr[mipsReg].loc = ML_ARMREG;
204
break;
205
}
206
case ML_IMM:
207
SetRegImm(reg, mr[mipsReg].imm);
208
ar[reg].isDirty = true; // IMM is always dirty.
209
210
// If we are mapping dirty, it means we're gonna overwrite.
211
// So the imm value is no longer valid.
212
if (mapFlags & MAP_DIRTY)
213
mr[mipsReg].loc = ML_ARMREG;
214
else
215
mr[mipsReg].loc = ML_ARMREG_IMM;
216
break;
217
default:
218
_assert_msg_(mr[mipsReg].loc != ML_ARMREG_AS_PTR, "MapRegTo with a pointer?");
219
mr[mipsReg].loc = ML_ARMREG;
220
break;
221
}
222
}
223
} else {
224
mr[mipsReg].loc = ML_ARMREG;
225
}
226
ar[reg].mipsReg = mipsReg;
227
ar[reg].pointerified = false;
228
mr[mipsReg].reg = reg;
229
}
230
231
ARM64Reg Arm64RegCache::AllocateReg() {
232
int allocCount;
233
const ARM64Reg *allocOrder = GetMIPSAllocationOrder(allocCount);
234
235
allocate:
236
for (int i = 0; i < allocCount; i++) {
237
ARM64Reg reg = allocOrder[i];
238
239
if (ar[reg].mipsReg == MIPS_REG_INVALID && !ar[reg].tempLocked) {
240
return reg;
241
}
242
}
243
244
// Still nothing. Let's spill a reg and goto 10.
245
// TODO: Use age or something to choose which register to spill?
246
// TODO: Spill dirty regs first? or opposite?
247
bool clobbered;
248
ARM64Reg bestToSpill = FindBestToSpill(true, &clobbered);
249
if (bestToSpill == INVALID_REG) {
250
bestToSpill = FindBestToSpill(false, &clobbered);
251
}
252
253
if (bestToSpill != INVALID_REG) {
254
if (clobbered) {
255
DiscardR(ar[bestToSpill].mipsReg);
256
} else {
257
FlushArmReg(bestToSpill);
258
}
259
// Now one must be free.
260
goto allocate;
261
}
262
263
// Uh oh, we have all of them spilllocked....
264
ERROR_LOG_REPORT(Log::JIT, "Out of spillable registers at PC %08x!!!", mips_->pc);
265
return INVALID_REG;
266
}
267
268
ARM64Reg Arm64RegCache::FindBestToSpill(bool unusedOnly, bool *clobbered) {
269
int allocCount;
270
const ARM64Reg *allocOrder = GetMIPSAllocationOrder(allocCount);
271
272
static const int UNUSED_LOOKAHEAD_OPS = 30;
273
274
*clobbered = false;
275
for (int i = 0; i < allocCount; i++) {
276
ARM64Reg reg = allocOrder[i];
277
if (ar[reg].mipsReg != MIPS_REG_INVALID && mr[ar[reg].mipsReg].spillLock)
278
continue;
279
if (ar[reg].tempLocked)
280
continue;
281
282
// As it's in alloc-order, we know it's not static so we don't need to check for that.
283
284
// Awesome, a clobbered reg. Let's use it.
285
if (MIPSAnalyst::IsRegisterClobbered(ar[reg].mipsReg, compilerPC_, UNUSED_LOOKAHEAD_OPS)) {
286
bool canClobber = true;
287
// HI is stored inside the LO reg. They both have to clobber at the same time.
288
if (ar[reg].mipsReg == MIPS_REG_LO) {
289
canClobber = MIPSAnalyst::IsRegisterClobbered(MIPS_REG_HI, compilerPC_, UNUSED_LOOKAHEAD_OPS);
290
}
291
if (canClobber) {
292
*clobbered = true;
293
return reg;
294
}
295
}
296
297
// Not awesome. A used reg. Let's try to avoid spilling.
298
if (unusedOnly && MIPSAnalyst::IsRegisterUsed(ar[reg].mipsReg, compilerPC_, UNUSED_LOOKAHEAD_OPS)) {
299
continue;
300
}
301
302
return reg;
303
}
304
305
return INVALID_REG;
306
}
307
308
ARM64Reg Arm64RegCache::TryMapTempImm(MIPSGPReg r) {
309
// If already mapped, no need for a temporary.
310
if (IsMapped(r)) {
311
return R(r);
312
}
313
314
if (mr[r].loc == ML_IMM) {
315
if (mr[r].imm == 0) {
316
return WZR;
317
}
318
319
// Try our luck - check for an exact match in another armreg.
320
for (int i = 0; i < NUM_MIPSREG; ++i) {
321
if (mr[i].loc == ML_ARMREG_IMM && mr[i].imm == mr[r].imm) {
322
// Awesome, let's just use this reg.
323
return mr[i].reg;
324
}
325
}
326
}
327
328
return INVALID_REG;
329
}
330
331
ARM64Reg Arm64RegCache::GetAndLockTempR() {
332
ARM64Reg reg = AllocateReg();
333
if (reg != INVALID_REG) {
334
ar[reg].tempLocked = true;
335
}
336
return reg;
337
}
338
339
// TODO: Somewhat smarter spilling - currently simply spills the first available, should do
340
// round robin or FIFO or something.
341
ARM64Reg Arm64RegCache::MapReg(MIPSGPReg mipsReg, int mapFlags) {
342
if (mipsReg == MIPS_REG_HI) {
343
ERROR_LOG_REPORT(Log::JIT, "Cannot map HI in Arm64RegCache");
344
return INVALID_REG;
345
}
346
347
if (mipsReg == MIPS_REG_INVALID) {
348
ERROR_LOG(Log::JIT, "Cannot map invalid register");
349
return INVALID_REG;
350
}
351
352
ARM64Reg armReg = mr[mipsReg].reg;
353
354
if (mr[mipsReg].isStatic) {
355
if (armReg == INVALID_REG) {
356
ERROR_LOG(Log::JIT, "MapReg on statically mapped reg %d failed - armReg got lost", mipsReg);
357
}
358
if (mr[mipsReg].loc == ML_IMM) {
359
// Back into the register, with or without the imm value.
360
// If noinit, the MAP_DIRTY check below will take care of the rest.
361
if ((mapFlags & MAP_NOINIT) != MAP_NOINIT) {
362
SetRegImm(armReg, mr[mipsReg].imm);
363
mr[mipsReg].loc = ML_ARMREG_IMM;
364
ar[armReg].pointerified = false;
365
}
366
} else if (mr[mipsReg].loc == ML_ARMREG_AS_PTR) {
367
// Was mapped as pointer, now we want it mapped as a value, presumably to
368
// add or subtract stuff to it.
369
if ((mapFlags & MAP_NOINIT) != MAP_NOINIT) {
370
emit_->SUB(EncodeRegTo64(armReg), EncodeRegTo64(armReg), MEMBASEREG);
371
}
372
mr[mipsReg].loc = ML_ARMREG;
373
}
374
// Erasing the imm on dirty (necessary since otherwise we will still think it's ML_ARMREG_IMM and return
375
// true for IsImm and calculate crazily wrong things). /unknown
376
if (mapFlags & MAP_DIRTY) {
377
mr[mipsReg].loc = ML_ARMREG; // As we are dirty, can't keep ARMREG_IMM, we will quickly drift out of sync
378
ar[armReg].pointerified = false;
379
ar[armReg].isDirty = true; // Not that it matters
380
}
381
return mr[mipsReg].reg;
382
}
383
384
// Let's see if it's already mapped. If so we just need to update the dirty flag.
385
// We don't need to check for ML_NOINIT because we assume that anyone who maps
386
// with that flag immediately writes a "known" value to the register.
387
if (mr[mipsReg].loc == ML_ARMREG || mr[mipsReg].loc == ML_ARMREG_IMM) {
388
if (ar[armReg].mipsReg != mipsReg) {
389
ERROR_LOG_REPORT(Log::JIT, "Register mapping out of sync! %i", mipsReg);
390
}
391
if (mapFlags & MAP_DIRTY) {
392
// Mapping dirty means the old imm value is invalid.
393
mr[mipsReg].loc = ML_ARMREG;
394
ar[armReg].isDirty = true;
395
// If reg is written to, pointerification is lost.
396
ar[armReg].pointerified = false;
397
}
398
399
return mr[mipsReg].reg;
400
} else if (mr[mipsReg].loc == ML_ARMREG_AS_PTR) {
401
// Was mapped as pointer, now we want it mapped as a value, presumably to
402
// add or subtract stuff to it.
403
if ((mapFlags & MAP_NOINIT) != MAP_NOINIT) {
404
emit_->SUB(EncodeRegTo64(armReg), EncodeRegTo64(armReg), MEMBASEREG);
405
}
406
mr[mipsReg].loc = ML_ARMREG;
407
if (mapFlags & MAP_DIRTY) {
408
ar[armReg].isDirty = true;
409
}
410
return (ARM64Reg)mr[mipsReg].reg;
411
}
412
413
// Okay, not mapped, so we need to allocate an ARM register.
414
ARM64Reg reg = AllocateReg();
415
if (reg != INVALID_REG) {
416
// Grab it, and load the value into it (if requested).
417
MapRegTo(reg, mipsReg, mapFlags);
418
}
419
420
return reg;
421
}
422
423
Arm64Gen::ARM64Reg Arm64RegCache::MapRegAsPointer(MIPSGPReg reg) {
424
// Already mapped.
425
if (mr[reg].loc == ML_ARMREG_AS_PTR) {
426
return mr[reg].reg;
427
}
428
429
ARM64Reg retval = INVALID_REG;
430
if (mr[reg].loc != ML_ARMREG && mr[reg].loc != ML_ARMREG_IMM) {
431
retval = MapReg(reg);
432
} else {
433
retval = mr[reg].reg;
434
}
435
436
if (mr[reg].loc == ML_ARMREG || mr[reg].loc == ML_ARMREG_IMM) {
437
// If there was an imm attached, discard it.
438
mr[reg].loc = ML_ARMREG;
439
ARM64Reg a = DecodeReg(mr[reg].reg);
440
if (!jo_->enablePointerify) {
441
// Convert to a pointer by adding the base and clearing off the top bits.
442
// If SP, we can probably avoid the top bit clear, let's play with that later.
443
#ifdef MASKED_PSP_MEMORY
444
emit_->ANDI2R(EncodeRegTo64(a), EncodeRegTo64(a), 0x3FFFFFFF);
445
#endif
446
emit_->ADD(EncodeRegTo64(a), EncodeRegTo64(a), MEMBASEREG);
447
mr[reg].loc = ML_ARMREG_AS_PTR;
448
} else if (!ar[a].pointerified) {
449
emit_->MOVK(EncodeRegTo64(a), ((uint64_t)Memory::base) >> 32, SHIFT_32);
450
ar[a].pointerified = true;
451
}
452
} else {
453
ERROR_LOG(Log::JIT, "MapRegAsPointer : MapReg failed to allocate a register?");
454
}
455
return retval;
456
}
457
458
void Arm64RegCache::MapIn(MIPSGPReg rs) {
459
MapReg(rs);
460
}
461
462
void Arm64RegCache::MapInIn(MIPSGPReg rd, MIPSGPReg rs) {
463
SpillLock(rd, rs);
464
MapReg(rd);
465
MapReg(rs);
466
ReleaseSpillLock(rd, rs);
467
}
468
469
void Arm64RegCache::MapDirtyIn(MIPSGPReg rd, MIPSGPReg rs, bool avoidLoad) {
470
SpillLock(rd, rs);
471
bool load = !avoidLoad || rd == rs;
472
MapReg(rd, load ? MAP_DIRTY : MAP_NOINIT);
473
MapReg(rs);
474
ReleaseSpillLock(rd, rs);
475
}
476
477
void Arm64RegCache::MapDirtyInIn(MIPSGPReg rd, MIPSGPReg rs, MIPSGPReg rt, bool avoidLoad) {
478
SpillLock(rd, rs, rt);
479
bool load = !avoidLoad || (rd == rs || rd == rt);
480
MapReg(rd, load ? MAP_DIRTY : MAP_NOINIT);
481
MapReg(rt);
482
MapReg(rs);
483
ReleaseSpillLock(rd, rs, rt);
484
}
485
486
void Arm64RegCache::MapDirtyDirtyIn(MIPSGPReg rd1, MIPSGPReg rd2, MIPSGPReg rs, bool avoidLoad) {
487
SpillLock(rd1, rd2, rs);
488
bool load1 = !avoidLoad || rd1 == rs;
489
bool load2 = !avoidLoad || rd2 == rs;
490
MapReg(rd1, load1 ? MAP_DIRTY : MAP_NOINIT);
491
MapReg(rd2, load2 ? MAP_DIRTY : MAP_NOINIT);
492
MapReg(rs);
493
ReleaseSpillLock(rd1, rd2, rs);
494
}
495
496
void Arm64RegCache::MapDirtyDirtyInIn(MIPSGPReg rd1, MIPSGPReg rd2, MIPSGPReg rs, MIPSGPReg rt, bool avoidLoad) {
497
SpillLock(rd1, rd2, rs, rt);
498
bool load1 = !avoidLoad || (rd1 == rs || rd1 == rt);
499
bool load2 = !avoidLoad || (rd2 == rs || rd2 == rt);
500
MapReg(rd1, load1 ? MAP_DIRTY : MAP_NOINIT);
501
MapReg(rd2, load2 ? MAP_DIRTY : MAP_NOINIT);
502
MapReg(rt);
503
MapReg(rs);
504
ReleaseSpillLock(rd1, rd2, rs, rt);
505
}
506
507
void Arm64RegCache::FlushArmReg(ARM64Reg r) {
508
if (r == INVALID_REG) {
509
ERROR_LOG(Log::JIT, "FlushArmReg called on invalid register %d", r);
510
return;
511
}
512
if (ar[r].mipsReg == MIPS_REG_INVALID) {
513
// Nothing to do, reg not mapped.
514
if (ar[r].isDirty) {
515
ERROR_LOG_REPORT(Log::JIT, "Dirty but no mipsreg?");
516
}
517
return;
518
}
519
if (mr[ar[r].mipsReg].isStatic) {
520
ERROR_LOG(Log::JIT, "Cannot FlushArmReg a statically mapped register");
521
return;
522
}
523
auto &mreg = mr[ar[r].mipsReg];
524
if (mreg.loc == ML_ARMREG_IMM || ar[r].mipsReg == MIPS_REG_ZERO) {
525
// We know its immediate value, no need to STR now.
526
mreg.loc = ML_IMM;
527
mreg.reg = INVALID_REG;
528
} else {
529
if (mreg.loc == ML_IMM || ar[r].isDirty) {
530
if (mreg.loc == ML_ARMREG_AS_PTR) {
531
// Unpointerify, in case dirty.
532
emit_->SUB(EncodeRegTo64(r), EncodeRegTo64(r), MEMBASEREG);
533
mreg.loc = ML_ARMREG;
534
}
535
// Note: may be a 64-bit reg.
536
ARM64Reg storeReg = ARM64RegForFlush(ar[r].mipsReg);
537
if (storeReg != INVALID_REG)
538
emit_->STR(INDEX_UNSIGNED, storeReg, CTXREG, GetMipsRegOffset(ar[r].mipsReg));
539
}
540
mreg.loc = ML_MEM;
541
mreg.reg = INVALID_REG;
542
mreg.imm = 0;
543
}
544
ar[r].isDirty = false;
545
ar[r].mipsReg = MIPS_REG_INVALID;
546
ar[r].pointerified = false;
547
}
548
549
void Arm64RegCache::DiscardR(MIPSGPReg mipsReg) {
550
if (mr[mipsReg].isStatic) {
551
// Simply do nothing unless it's an IMM/ARMREG_IMM/ARMREG_AS_PTR, in case we just switch it over to ARMREG, losing the value.
552
ARM64Reg armReg = mr[mipsReg].reg;
553
if (mr[mipsReg].loc == ML_ARMREG_IMM || mr[mipsReg].loc == ML_IMM || mr[mipsReg].loc == ML_ARMREG_AS_PTR) {
554
// Ignore the imm value, restore sanity
555
mr[mipsReg].loc = ML_ARMREG;
556
ar[armReg].pointerified = false;
557
ar[armReg].isDirty = false;
558
}
559
return;
560
}
561
const RegMIPSLoc prevLoc = mr[mipsReg].loc;
562
if (prevLoc == ML_ARMREG || prevLoc == ML_ARMREG_IMM || prevLoc == ML_ARMREG_AS_PTR) {
563
ARM64Reg armReg = mr[mipsReg].reg;
564
ar[armReg].isDirty = false;
565
ar[armReg].mipsReg = MIPS_REG_INVALID;
566
ar[armReg].pointerified = false;
567
mr[mipsReg].reg = INVALID_REG;
568
if (mipsReg == MIPS_REG_ZERO) {
569
mr[mipsReg].loc = ML_IMM;
570
} else {
571
mr[mipsReg].loc = ML_MEM;
572
}
573
mr[mipsReg].imm = 0;
574
}
575
if (prevLoc == ML_IMM && mipsReg != MIPS_REG_ZERO) {
576
mr[mipsReg].loc = ML_MEM;
577
mr[mipsReg].imm = 0;
578
}
579
}
580
581
ARM64Reg Arm64RegCache::ARM64RegForFlush(MIPSGPReg r) {
582
if (mr[r].isStatic)
583
return INVALID_REG; // No flushing needed
584
585
switch (mr[r].loc) {
586
case ML_IMM:
587
if (r == MIPS_REG_ZERO) {
588
return INVALID_REG;
589
}
590
// Zero is super easy.
591
if (mr[r].imm == 0) {
592
return WZR;
593
}
594
// Could we get lucky? Check for an exact match in another armreg.
595
for (int i = 0; i < NUM_MIPSREG; ++i) {
596
if (mr[i].loc == ML_ARMREG_IMM && mr[i].imm == mr[r].imm) {
597
// Awesome, let's just store this reg.
598
return mr[i].reg;
599
}
600
}
601
return INVALID_REG;
602
603
case ML_ARMREG:
604
case ML_ARMREG_IMM:
605
if (mr[r].reg == INVALID_REG) {
606
ERROR_LOG_REPORT(Log::JIT, "ARM64RegForFlush: MipsReg %d had bad ArmReg", r);
607
return INVALID_REG;
608
}
609
// No need to flush if it's zero or not dirty.
610
if (r == MIPS_REG_ZERO || !ar[mr[r].reg].isDirty) {
611
return INVALID_REG;
612
}
613
if (r == MIPS_REG_LO) {
614
return EncodeRegTo64(mr[r].reg);
615
}
616
return mr[r].reg;
617
618
case ML_ARMREG_AS_PTR:
619
return INVALID_REG;
620
621
case ML_MEM:
622
return INVALID_REG;
623
624
default:
625
ERROR_LOG_REPORT(Log::JIT, "ARM64RegForFlush: MipsReg %d with invalid location %d", r, mr[r].loc);
626
return INVALID_REG;
627
}
628
}
629
630
void Arm64RegCache::FlushR(MIPSGPReg r) {
631
if (mr[r].isStatic) {
632
ERROR_LOG(Log::JIT, "Cannot flush static reg %d", r);
633
return;
634
}
635
636
switch (mr[r].loc) {
637
case ML_IMM:
638
// IMM is always "dirty".
639
if (r == MIPS_REG_LO) {
640
SetRegImm(SCRATCH1_64, mr[r].imm);
641
emit_->STR(INDEX_UNSIGNED, SCRATCH1_64, CTXREG, GetMipsRegOffset(r));
642
} else if (r != MIPS_REG_ZERO) {
643
// Try to optimize using a different reg.
644
ARM64Reg storeReg = ARM64RegForFlush(r);
645
if (storeReg == INVALID_REG) {
646
SetRegImm(SCRATCH1, mr[r].imm);
647
storeReg = SCRATCH1;
648
}
649
emit_->STR(INDEX_UNSIGNED, storeReg, CTXREG, GetMipsRegOffset(r));
650
}
651
break;
652
653
case ML_ARMREG:
654
case ML_ARMREG_IMM:
655
if (ar[mr[r].reg].isDirty) {
656
// Note: might be a 64-bit reg.
657
ARM64Reg storeReg = ARM64RegForFlush(r);
658
if (storeReg != INVALID_REG) {
659
emit_->STR(INDEX_UNSIGNED, storeReg, CTXREG, GetMipsRegOffset(r));
660
}
661
ar[mr[r].reg].isDirty = false;
662
}
663
ar[mr[r].reg].mipsReg = MIPS_REG_INVALID;
664
ar[mr[r].reg].pointerified = false;
665
break;
666
667
case ML_ARMREG_AS_PTR:
668
if (ar[mr[r].reg].isDirty) {
669
emit_->SUB(EncodeRegTo64(mr[r].reg), EncodeRegTo64(mr[r].reg), MEMBASEREG);
670
// We set this so ARM64RegForFlush knows it's no longer a pointer.
671
mr[r].loc = ML_ARMREG;
672
ARM64Reg storeReg = ARM64RegForFlush(r);
673
if (storeReg != INVALID_REG) {
674
emit_->STR(INDEX_UNSIGNED, storeReg, CTXREG, GetMipsRegOffset(r));
675
}
676
ar[mr[r].reg].isDirty = false;
677
}
678
ar[mr[r].reg].mipsReg = MIPS_REG_INVALID;
679
break;
680
681
case ML_MEM:
682
// Already there, nothing to do.
683
break;
684
685
default:
686
ERROR_LOG_REPORT(Log::JIT, "FlushR: MipsReg %d with invalid location %d", r, mr[r].loc);
687
break;
688
}
689
if (r == MIPS_REG_ZERO) {
690
mr[r].loc = ML_IMM;
691
} else {
692
mr[r].loc = ML_MEM;
693
}
694
mr[r].reg = INVALID_REG;
695
mr[r].imm = 0;
696
}
697
698
void Arm64RegCache::FlushAll() {
699
// Note: make sure not to change the registers when flushing:
700
// Branching code expects the armreg to retain its value.
701
702
// LO can't be included in a 32-bit pair, since it's 64 bit.
703
// Flush it first so we don't get it confused.
704
FlushR(MIPS_REG_LO);
705
706
// Try to flush in pairs when possible.
707
// 1 because MIPS_REG_ZERO isn't flushable anyway.
708
// 31 because 30 and 31 are the last possible pair - MIPS_REG_FPCOND, etc. are too far away.
709
for (int i = 1; i < 31; i++) {
710
MIPSGPReg mreg1 = MIPSGPReg(i);
711
MIPSGPReg mreg2 = MIPSGPReg(i + 1);
712
ARM64Reg areg1 = ARM64RegForFlush(mreg1);
713
ARM64Reg areg2 = ARM64RegForFlush(mreg2);
714
715
// If either one doesn't have a reg yet, try flushing imms to scratch regs.
716
if (areg1 == INVALID_REG && IsPureImm(mreg1) && !mr[i].isStatic) {
717
areg1 = SCRATCH1;
718
}
719
if (areg2 == INVALID_REG && IsPureImm(mreg2) && !mr[i + 1].isStatic) {
720
areg2 = SCRATCH2;
721
}
722
723
if (areg1 != INVALID_REG && areg2 != INVALID_REG) {
724
// Actually put the imms in place now that we know we can do the STP.
725
// We didn't do it before in case the other wouldn't work.
726
if (areg1 == SCRATCH1) {
727
SetRegImm(areg1, GetImm(mreg1));
728
}
729
if (areg2 == SCRATCH2) {
730
SetRegImm(areg2, GetImm(mreg2));
731
}
732
733
// We can use a paired store, awesome.
734
emit_->STP(INDEX_SIGNED, areg1, areg2, CTXREG, GetMipsRegOffset(mreg1));
735
736
// Now we mark them as stored by discarding.
737
DiscardR(mreg1);
738
DiscardR(mreg2);
739
}
740
}
741
742
// Final pass to grab any that were left behind.
743
for (int i = 0; i < NUM_MIPSREG; i++) {
744
MIPSGPReg mipsReg = MIPSGPReg(i);
745
if (mr[i].isStatic) {
746
Arm64Gen::ARM64Reg armReg = mr[i].reg;
747
// Cannot leave any IMMs in registers, not even ML_ARMREG_IMM, can confuse the regalloc later if this flush is mid-block
748
// due to an interpreter fallback that changes the register.
749
if (mr[i].loc == ML_IMM) {
750
SetRegImm(mr[i].reg, mr[i].imm);
751
mr[i].loc = ML_ARMREG;
752
ar[armReg].pointerified = false;
753
} else if (mr[i].loc == ML_ARMREG_IMM) {
754
// The register already contains the immediate.
755
if (ar[armReg].pointerified) {
756
ERROR_LOG(Log::JIT, "ML_ARMREG_IMM but pointerified. Wrong.");
757
ar[armReg].pointerified = false;
758
}
759
mr[i].loc = ML_ARMREG;
760
} else if (mr[i].loc == ML_ARMREG_AS_PTR) {
761
emit_->SUB(EncodeRegTo64(armReg), EncodeRegTo64(armReg), MEMBASEREG);
762
mr[i].loc = ML_ARMREG;
763
}
764
if (i != MIPS_REG_ZERO && mr[i].reg == INVALID_REG) {
765
ERROR_LOG(Log::JIT, "ARM reg of static %i is invalid", i);
766
continue;
767
}
768
} else {
769
FlushR(mipsReg);
770
}
771
}
772
773
int count = 0;
774
const StaticAllocation *allocs = GetStaticAllocations(count);
775
for (int i = 0; i < count; i++) {
776
if (allocs[i].pointerified && !ar[allocs[i].ar].pointerified && jo_->enablePointerify) {
777
// Re-pointerify
778
emit_->MOVK(EncodeRegTo64(allocs[i].ar), ((uint64_t)Memory::base) >> 32, SHIFT_32);
779
ar[allocs[i].ar].pointerified = true;
780
} else if (!allocs[i].pointerified) {
781
// If this register got pointerified on the way, mark it as not, so that after save/reload (like in an interpreter fallback), it won't be regarded as such, as it simply won't be.
782
ar[allocs[i].ar].pointerified = false;
783
}
784
}
785
// Sanity check
786
for (int i = 0; i < NUM_ARMREG; i++) {
787
if (ar[i].mipsReg != MIPS_REG_INVALID && mr[ar[i].mipsReg].isStatic == false) {
788
ERROR_LOG_REPORT(Log::JIT, "Flush fail: ar[%i].mipsReg=%i", i, ar[i].mipsReg);
789
}
790
}
791
}
792
793
void Arm64RegCache::SetImm(MIPSGPReg r, u64 immVal) {
794
if (r == MIPS_REG_HI) {
795
ERROR_LOG_REPORT(Log::JIT, "Cannot set HI imm in Arm64RegCache");
796
return;
797
}
798
if (r == MIPS_REG_ZERO && immVal != 0) {
799
ERROR_LOG_REPORT(Log::JIT, "Trying to set immediate %08x to r0 at %08x", (u32)immVal, compilerPC_);
800
return;
801
}
802
803
if (mr[r].loc == ML_ARMREG_IMM && mr[r].imm == immVal) {
804
// Already have that value, let's keep it in the reg.
805
return;
806
}
807
808
if (r != MIPS_REG_LO) {
809
// All regs on the PSP are 32 bit, but LO we treat as HI:LO so is 64 full bits.
810
immVal = immVal & 0xFFFFFFFF;
811
}
812
813
if (mr[r].isStatic) {
814
mr[r].loc = ML_IMM;
815
mr[r].imm = immVal;
816
ar[mr[r].reg].pointerified = false;
817
// We do not change reg to INVALID_REG for obvious reasons..
818
} else {
819
// Zap existing value if cached in a reg
820
if (mr[r].reg != INVALID_REG) {
821
ar[mr[r].reg].mipsReg = MIPS_REG_INVALID;
822
ar[mr[r].reg].isDirty = false;
823
ar[mr[r].reg].pointerified = false;
824
}
825
mr[r].loc = ML_IMM;
826
mr[r].imm = immVal;
827
mr[r].reg = INVALID_REG;
828
}
829
}
830
831
bool Arm64RegCache::IsImm(MIPSGPReg r) const {
832
if (r == MIPS_REG_ZERO)
833
return true;
834
else
835
return mr[r].loc == ML_IMM || mr[r].loc == ML_ARMREG_IMM;
836
}
837
838
bool Arm64RegCache::IsPureImm(MIPSGPReg r) const {
839
if (r == MIPS_REG_ZERO)
840
return true;
841
else
842
return mr[r].loc == ML_IMM;
843
}
844
845
u64 Arm64RegCache::GetImm(MIPSGPReg r) const {
846
if (r == MIPS_REG_ZERO)
847
return 0;
848
if (mr[r].loc != ML_IMM && mr[r].loc != ML_ARMREG_IMM) {
849
ERROR_LOG_REPORT(Log::JIT, "Trying to get imm from non-imm register %i", r);
850
}
851
return mr[r].imm;
852
}
853
854
int Arm64RegCache::GetMipsRegOffset(MIPSGPReg r) {
855
if (r < 32)
856
return r * 4;
857
switch (r) {
858
case MIPS_REG_HI:
859
return offsetof(MIPSState, hi);
860
case MIPS_REG_LO:
861
return offsetof(MIPSState, lo);
862
case MIPS_REG_FPCOND:
863
return offsetof(MIPSState, fpcond);
864
case MIPS_REG_VFPUCC:
865
return offsetof(MIPSState, vfpuCtrl[VFPU_CTRL_CC]);
866
default:
867
ERROR_LOG_REPORT(Log::JIT, "bad mips register %i", r);
868
return 0; // or what?
869
}
870
}
871
872
void Arm64RegCache::SpillLock(MIPSGPReg r1, MIPSGPReg r2, MIPSGPReg r3, MIPSGPReg r4) {
873
mr[r1].spillLock = true;
874
if (r2 != MIPS_REG_INVALID) mr[r2].spillLock = true;
875
if (r3 != MIPS_REG_INVALID) mr[r3].spillLock = true;
876
if (r4 != MIPS_REG_INVALID) mr[r4].spillLock = true;
877
}
878
879
void Arm64RegCache::ReleaseSpillLocksAndDiscardTemps() {
880
for (int i = 0; i < NUM_MIPSREG; i++) {
881
if (!mr[i].isStatic)
882
mr[i].spillLock = false;
883
}
884
for (int i = 0; i < NUM_ARMREG; i++) {
885
ar[i].tempLocked = false;
886
}
887
}
888
889
void Arm64RegCache::ReleaseSpillLock(MIPSGPReg r1, MIPSGPReg r2, MIPSGPReg r3, MIPSGPReg r4) {
890
if (!mr[r1].isStatic)
891
mr[r1].spillLock = false;
892
if (r2 != MIPS_REG_INVALID && !mr[r2].isStatic)
893
mr[r2].spillLock = false;
894
if (r3 != MIPS_REG_INVALID && !mr[r3].isStatic)
895
mr[r3].spillLock = false;
896
if (r4 != MIPS_REG_INVALID && !mr[r4].isStatic)
897
mr[r4].spillLock = false;
898
}
899
900
ARM64Reg Arm64RegCache::R(MIPSGPReg mipsReg) {
901
if (mr[mipsReg].loc == ML_ARMREG || mr[mipsReg].loc == ML_ARMREG_IMM) {
902
return mr[mipsReg].reg;
903
} else {
904
ERROR_LOG_REPORT(Log::JIT, "Reg %i not in arm reg. compilerPC = %08x", mipsReg, compilerPC_);
905
return INVALID_REG; // BAAAD
906
}
907
}
908
909
ARM64Reg Arm64RegCache::RPtr(MIPSGPReg mipsReg) {
910
if (mr[mipsReg].loc == ML_ARMREG_AS_PTR) {
911
return (ARM64Reg)mr[mipsReg].reg;
912
} else if (mr[mipsReg].loc == ML_ARMREG || mr[mipsReg].loc == ML_ARMREG_IMM) {
913
int a = mr[mipsReg].reg;
914
if (ar[a].pointerified) {
915
return (ARM64Reg)mr[mipsReg].reg;
916
} else {
917
ERROR_LOG(Log::JIT, "Tried to use a non-pointer register as a pointer");
918
return INVALID_REG;
919
}
920
} else {
921
ERROR_LOG_REPORT(Log::JIT, "Reg %i not in arm reg. compilerPC = %08x", mipsReg, compilerPC_);
922
return INVALID_REG; // BAAAD
923
}
924
}
925
926
#endif // PPSSPP_ARCH(ARM64)
927
928