Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/MIPS/IR/IRFrontend.cpp
3187 views
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 "Common/Log.h"
19
#include "Common/Serialize/Serializer.h"
20
#include "Common/Serialize/SerializeFuncs.h"
21
#include "Core/Debugger/Breakpoints.h"
22
#include "Core/Debugger/SymbolMap.h"
23
#include "Core/Reporting.h"
24
#include "Core/HLE/ReplaceTables.h"
25
#include "Core/MemMap.h"
26
#include "Core/MIPS/MIPSTables.h"
27
#include "Core/MIPS/IR/IRFrontend.h"
28
#include "Core/MIPS/IR/IRRegCache.h"
29
#include "Core/MIPS/IR/IRPassSimplify.h"
30
#include "Core/MIPS/IR/IRInterpreter.h"
31
#include "Core/MIPS/MIPSTracer.h"
32
33
#include <iterator>
34
35
namespace MIPSComp {
36
37
IRFrontend::IRFrontend(bool startDefaultPrefix) {
38
js.startDefaultPrefix = startDefaultPrefix;
39
js.hasSetRounding = false;
40
41
// The debugger sets this so that "go" on a breakpoint will actually... go.
42
// But if they reset, we can end up hitting it by mistake, since it's based on PC and ticks.
43
g_breakpoints.SetSkipFirst(0);
44
}
45
46
void IRFrontend::DoState(PointerWrap &p) {
47
auto s = p.Section("Jit", 1, 2);
48
if (!s)
49
return;
50
51
Do(p, js.startDefaultPrefix);
52
if (s >= 2) {
53
Do(p, js.hasSetRounding);
54
js.lastSetRounding = 0;
55
} else {
56
js.hasSetRounding = 1;
57
}
58
59
// The debugger sets this so that "go" on a breakpoint will actually... go.
60
// But if they reset, we can end up hitting it by mistake, since it's based on PC and ticks.
61
g_breakpoints.SetSkipFirst(0);
62
}
63
64
void IRFrontend::FlushAll() {
65
FlushPrefixV();
66
}
67
68
void IRFrontend::FlushPrefixV() {
69
if (js.startDefaultPrefix && !js.blockWrotePrefixes && js.HasNoPrefix()) {
70
// They started default, we never modified in memory, and they're default now.
71
// No reason to modify memory. This is common at end of blocks. Just clear dirty.
72
js.prefixSFlag = (JitState::PrefixState)(js.prefixSFlag & ~JitState::PREFIX_DIRTY);
73
js.prefixTFlag = (JitState::PrefixState)(js.prefixTFlag & ~JitState::PREFIX_DIRTY);
74
js.prefixDFlag = (JitState::PrefixState)(js.prefixDFlag & ~JitState::PREFIX_DIRTY);
75
return;
76
}
77
78
if ((js.prefixSFlag & JitState::PREFIX_DIRTY) != 0) {
79
ir.Write(IROp::SetCtrlVFPU, VFPU_CTRL_SPREFIX, ir.AddConstant(js.prefixS));
80
js.prefixSFlag = (JitState::PrefixState) (js.prefixSFlag & ~JitState::PREFIX_DIRTY);
81
}
82
83
if ((js.prefixTFlag & JitState::PREFIX_DIRTY) != 0) {
84
ir.Write(IROp::SetCtrlVFPU, VFPU_CTRL_TPREFIX, ir.AddConstant(js.prefixT));
85
js.prefixTFlag = (JitState::PrefixState) (js.prefixTFlag & ~JitState::PREFIX_DIRTY);
86
}
87
88
if ((js.prefixDFlag & JitState::PREFIX_DIRTY) != 0) {
89
ir.Write(IROp::SetCtrlVFPU, VFPU_CTRL_DPREFIX, ir.AddConstant(js.prefixD));
90
js.prefixDFlag = (JitState::PrefixState) (js.prefixDFlag & ~JitState::PREFIX_DIRTY);
91
}
92
93
// If we got here, we must've written prefixes to memory in this block.
94
js.blockWrotePrefixes = true;
95
}
96
97
void IRFrontend::EatInstruction(MIPSOpcode op) {
98
MIPSInfo info = MIPSGetInfo(op);
99
if (info & DELAYSLOT) {
100
ERROR_LOG_REPORT_ONCE(ateDelaySlot, Log::JIT, "Ate a branch op.");
101
}
102
if (js.inDelaySlot) {
103
ERROR_LOG_REPORT_ONCE(ateInDelaySlot, Log::JIT, "Ate an instruction inside a delay slot.");
104
}
105
106
CheckBreakpoint(GetCompilerPC() + 4);
107
js.numInstructions++;
108
js.compilerPC += 4;
109
js.downcountAmount += MIPSGetInstructionCycleEstimate(op);
110
}
111
112
void IRFrontend::CompileDelaySlot() {
113
js.inDelaySlot = true;
114
CheckBreakpoint(GetCompilerPC() + 4);
115
MIPSOpcode op = GetOffsetInstruction(1);
116
MIPSCompileOp(op, this);
117
js.inDelaySlot = false;
118
}
119
120
bool IRFrontend::CheckRounding(u32 blockAddress) {
121
bool cleanSlate = false;
122
if (js.hasSetRounding && !js.lastSetRounding) {
123
WARN_LOG(Log::JIT, "Detected rounding mode usage, rebuilding jit with checks");
124
// Won't loop, since hasSetRounding is only ever set to 1.
125
js.lastSetRounding = js.hasSetRounding;
126
cleanSlate = true;
127
}
128
129
// Drat. The VFPU hit an uneaten prefix at the end of a block.
130
if (js.startDefaultPrefix && js.MayHavePrefix()) {
131
WARN_LOG_REPORT(Log::JIT, "An uneaten prefix at end of block for %08x", blockAddress);
132
logBlocks = 1;
133
js.LogPrefix();
134
135
// Let's try that one more time. We won't get back here because we toggled the value.
136
js.startDefaultPrefix = false;
137
cleanSlate = true;
138
}
139
return cleanSlate;
140
}
141
142
void IRFrontend::Comp_ReplacementFunc(MIPSOpcode op) {
143
int index = op.encoding & MIPS_EMUHACK_VALUE_MASK;
144
145
const ReplacementTableEntry *entry = GetReplacementFunc(index);
146
if (!entry) {
147
ERROR_LOG(Log::HLE, "Invalid replacement op %08x", op.encoding);
148
return;
149
}
150
151
u32 funcSize = g_symbolMap->GetFunctionSize(GetCompilerPC());
152
bool disabled = (entry->flags & REPFLAG_DISABLED) != 0;
153
if (!disabled && funcSize != SymbolMap::INVALID_ADDRESS && funcSize > sizeof(u32)) {
154
// We don't need to disable hooks, the code will still run.
155
if ((entry->flags & (REPFLAG_HOOKENTER | REPFLAG_HOOKEXIT)) == 0) {
156
// Any breakpoint at the func entry was already tripped, so we can still run the replacement.
157
// That's a common case - just to see how often the replacement hits.
158
disabled = g_breakpoints.RangeContainsBreakPoint(GetCompilerPC() + sizeof(u32), funcSize - sizeof(u32));
159
}
160
}
161
162
if (disabled) {
163
MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this);
164
} else if (entry->replaceFunc) {
165
FlushAll();
166
RestoreRoundingMode();
167
ir.Write(IROp::SetPCConst, 0, ir.AddConstant(GetCompilerPC()));
168
ir.Write(IROp::CallReplacement, IRTEMP_0, ir.AddConstant(index));
169
170
if (entry->flags & (REPFLAG_HOOKENTER | REPFLAG_HOOKEXIT)) {
171
// Compile the original instruction at this address. We ignore cycles for hooks.
172
ApplyRoundingMode();
173
MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this);
174
} else {
175
ApplyRoundingMode();
176
// If IRTEMP_0 was set to 1, it means the replacement needs to run again (sliced.)
177
// This is necessary for replacements that take a lot of cycles.
178
ir.Write(IROp::Downcount, 0, ir.AddConstant(js.downcountAmount));
179
ir.Write(IROp::ExitToConstIfNeq, ir.AddConstant(GetCompilerPC()), IRTEMP_0, MIPS_REG_ZERO);
180
ir.Write(IROp::ExitToReg, 0, MIPS_REG_RA, 0);
181
js.compiling = false;
182
}
183
} else {
184
ERROR_LOG(Log::HLE, "Replacement function %s has neither jit nor regular impl", entry->name);
185
}
186
}
187
188
void IRFrontend::Comp_Generic(MIPSOpcode op) {
189
FlushAll();
190
ir.Write(IROp::Interpret, 0, ir.AddConstant(op.encoding));
191
const MIPSInfo info = MIPSGetInfo(op);
192
if ((info & IS_VFPU) != 0 && (info & VFPU_NO_PREFIX) == 0) {
193
// If it does eat them, it'll happen in MIPSCompileOp().
194
if ((info & OUT_EAT_PREFIX) == 0)
195
js.PrefixUnknown();
196
197
// Even if DISABLE'd, we want to set this flag so we overwrite.
198
if ((info & OUT_VFPU_PREFIX) != 0)
199
js.blockWrotePrefixes = true;
200
}
201
}
202
203
// Destroys SCRATCH2
204
void IRFrontend::RestoreRoundingMode(bool force) {
205
// If the game has never set an interesting rounding mode, we can safely skip this.
206
if (force || js.hasSetRounding) {
207
ir.Write(IROp::RestoreRoundingMode);
208
}
209
}
210
211
// Destroys SCRATCH1 and SCRATCH2
212
void IRFrontend::ApplyRoundingMode(bool force) {
213
// If the game has never set an interesting rounding mode, we can safely skip this.
214
if (force || js.hasSetRounding) {
215
ir.Write(IROp::ApplyRoundingMode);
216
}
217
}
218
219
// Destroys SCRATCH1 and SCRATCH2
220
void IRFrontend::UpdateRoundingMode() {
221
// We must set js.hasSetRounding at compile time, or this block will use the wrong rounding mode.
222
js.hasSetRounding = true;
223
ir.Write(IROp::UpdateRoundingMode);
224
}
225
226
void IRFrontend::Comp_DoNothing(MIPSOpcode op) {
227
}
228
229
int IRFrontend::Replace_fabsf() {
230
Crash();
231
return 0;
232
}
233
234
u32 IRFrontend::GetCompilerPC() {
235
return js.compilerPC;
236
}
237
238
MIPSOpcode IRFrontend::GetOffsetInstruction(int offset) {
239
return Memory::Read_Instruction(GetCompilerPC() + 4 * offset);
240
}
241
242
void IRFrontend::DoJit(u32 em_address, std::vector<IRInst> &instructions, u32 &mipsBytes) {
243
js.cancel = false;
244
js.blockStart = em_address;
245
js.compilerPC = em_address;
246
js.lastContinuedPC = 0;
247
js.initialBlockSize = 0;
248
js.nextExit = 0;
249
js.downcountAmount = 0;
250
js.curBlock = nullptr;
251
js.compiling = true;
252
js.hadBreakpoints = false;
253
js.blockWrotePrefixes = false;
254
js.inDelaySlot = false;
255
js.PrefixStart();
256
ir.Clear();
257
258
js.numInstructions = 0;
259
while (js.compiling) {
260
// Jit breakpoints are quite fast, so let's do them in release too.
261
CheckBreakpoint(GetCompilerPC());
262
263
MIPSOpcode inst = Memory::Read_Opcode_JIT(GetCompilerPC());
264
js.downcountAmount += MIPSGetInstructionCycleEstimate(inst);
265
MIPSCompileOp(inst, this);
266
js.compilerPC += 4;
267
js.numInstructions++;
268
}
269
270
if (js.cancel) {
271
// Clear the instructions to signal this was not compiled.
272
ir.Clear();
273
}
274
275
mipsBytes = js.compilerPC - em_address;
276
277
IRWriter simplified;
278
IRWriter *code = &ir;
279
if (!js.hadBreakpoints) {
280
std::vector<IRPassFunc> passes{
281
&ApplyMemoryValidation,
282
&RemoveLoadStoreLeftRight,
283
&OptimizeFPMoves,
284
&PropagateConstants,
285
&PurgeTemps,
286
&ReduceVec4Flush,
287
&OptimizeLoadsAfterStores,
288
// &ReorderLoadStore,
289
// &MergeLoadStore,
290
// &ThreeOpToTwoOp,
291
};
292
293
if (opts.optimizeForInterpreter) {
294
// Add special passes here.
295
passes.push_back(&OptimizeForInterpreter);
296
}
297
if (IRApplyPasses(passes.data(), passes.size(), ir, simplified, opts))
298
logBlocks = 1;
299
code = &simplified;
300
//if (ir.GetInstructions().size() >= 24)
301
// logBlocks = 1;
302
}
303
304
if (!mipsTracer.tracing_enabled) {
305
instructions = code->GetInstructions();
306
}
307
else {
308
std::vector<IRInst> block_instructions = code->GetInstructions();
309
instructions.reserve(block_instructions.capacity());
310
// The first instruction is "Downcount"
311
instructions.push_back(block_instructions.front());
312
instructions.push_back({ IROp::LogIRBlock, {0}, 0, 0, 0 });
313
std::copy(block_instructions.begin() + 1, block_instructions.end(), std::back_inserter(instructions));
314
}
315
316
if (logBlocks > 0 && dontLogBlocks == 0) {
317
char temp2[256];
318
NOTICE_LOG(Log::JIT, "=============== mips %08x ===============", em_address);
319
for (u32 cpc = em_address; cpc != GetCompilerPC(); cpc += 4) {
320
temp2[0] = 0;
321
MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp2, sizeof(temp2), true);
322
NOTICE_LOG(Log::JIT, "M: %08x %s", cpc, temp2);
323
}
324
}
325
326
if (logBlocks > 0 && dontLogBlocks == 0) {
327
NOTICE_LOG(Log::JIT, "=============== Original IR (%d instructions) ===============", (int)ir.GetInstructions().size());
328
for (size_t i = 0; i < ir.GetInstructions().size(); i++) {
329
char buf[256];
330
DisassembleIR(buf, sizeof(buf), ir.GetInstructions()[i]);
331
NOTICE_LOG(Log::JIT, "%s", buf);
332
}
333
NOTICE_LOG(Log::JIT, "=============== end =================");
334
}
335
336
if (logBlocks > 0 && dontLogBlocks == 0) {
337
NOTICE_LOG(Log::JIT, "=============== IR (%d instructions) ===============", (int)code->GetInstructions().size());
338
for (size_t i = 0; i < code->GetInstructions().size(); i++) {
339
char buf[256];
340
DisassembleIR(buf, sizeof(buf), code->GetInstructions()[i]);
341
NOTICE_LOG(Log::JIT, "%s", buf);
342
}
343
NOTICE_LOG(Log::JIT, "=============== end =================");
344
}
345
346
if (logBlocks > 0)
347
logBlocks--;
348
if (dontLogBlocks > 0)
349
dontLogBlocks--;
350
}
351
352
void IRFrontend::Comp_RunBlock(MIPSOpcode op) {
353
// This shouldn't be necessary, the dispatcher should catch us before we get here.
354
ERROR_LOG(Log::JIT, "Comp_RunBlock should never be reached!");
355
}
356
357
void IRFrontend::CheckBreakpoint(u32 addr) {
358
if (g_breakpoints.IsAddressBreakPoint(addr)) {
359
FlushAll();
360
361
// Can't skip this even at the start of a block, might impact block linking.
362
ir.Write(IROp::SetPCConst, 0, ir.AddConstant(GetCompilerPC()));
363
364
RestoreRoundingMode();
365
// At this point, downcount HAS the delay slot, but not the instruction itself.
366
int downcountOffset = 0;
367
if (js.inDelaySlot) {
368
MIPSOpcode branchOp = Memory::Read_Opcode_JIT(GetCompilerPC());
369
MIPSOpcode delayOp = Memory::Read_Opcode_JIT(addr);
370
downcountOffset = -MIPSGetInstructionCycleEstimate(delayOp);
371
if ((MIPSGetInfo(branchOp) & LIKELY) != 0) {
372
// Okay, we're in a likely branch. Also negate the branch cycles.
373
downcountOffset += -MIPSGetInstructionCycleEstimate(branchOp);
374
}
375
}
376
int downcountAmount = js.downcountAmount + downcountOffset;
377
if (downcountAmount != 0)
378
ir.Write(IROp::Downcount, 0, ir.AddConstant(downcountAmount));
379
// Note that this means downcount can't be metadata on the block.
380
js.downcountAmount = -downcountOffset;
381
ir.Write(IROp::Breakpoint, 0, ir.AddConstant(addr));
382
ApplyRoundingMode();
383
384
js.hadBreakpoints = true;
385
}
386
}
387
388
void IRFrontend::CheckMemoryBreakpoint(int rs, int offset) {
389
if (g_breakpoints.HasMemChecks()) {
390
FlushAll();
391
392
// Can't skip this even at the start of a block, might impact block linking.
393
ir.Write(IROp::SetPCConst, 0, ir.AddConstant(GetCompilerPC()));
394
395
RestoreRoundingMode();
396
// At this point, downcount HAS the delay slot, but not the instruction itself.
397
int downcountOffset = 0;
398
if (js.inDelaySlot) {
399
// We assume delay slot in compilerPC + 4.
400
MIPSOpcode branchOp = Memory::Read_Opcode_JIT(GetCompilerPC());
401
MIPSOpcode delayOp = Memory::Read_Opcode_JIT(GetCompilerPC() + 4);
402
downcountOffset = -MIPSGetInstructionCycleEstimate(delayOp);
403
if ((MIPSGetInfo(branchOp) & LIKELY) != 0) {
404
// Okay, we're in a likely branch. Also negate the branch cycles.
405
downcountOffset += -MIPSGetInstructionCycleEstimate(branchOp);
406
}
407
}
408
int downcountAmount = js.downcountAmount + downcountOffset;
409
if (downcountAmount != 0)
410
ir.Write(IROp::Downcount, 0, ir.AddConstant(downcountAmount));
411
// Note that this means downcount can't be metadata on the block.
412
js.downcountAmount = -downcountOffset;
413
ir.Write(IROp::MemoryCheck, js.inDelaySlot ? 4 : 0, rs, ir.AddConstant(offset));
414
ApplyRoundingMode();
415
416
js.hadBreakpoints = true;
417
}
418
}
419
420
} // namespace
421
422