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/IR/IRFrontend.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 "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
CBreakPoints::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
CBreakPoints::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 = CBreakPoints::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, bool preload) {
243
js.cancel = false;
244
js.preloading = preload;
245
js.blockStart = em_address;
246
js.compilerPC = em_address;
247
js.lastContinuedPC = 0;
248
js.initialBlockSize = 0;
249
js.nextExit = 0;
250
js.downcountAmount = 0;
251
js.curBlock = nullptr;
252
js.compiling = true;
253
js.hadBreakpoints = false;
254
js.blockWrotePrefixes = false;
255
js.inDelaySlot = false;
256
js.PrefixStart();
257
ir.Clear();
258
259
js.numInstructions = 0;
260
while (js.compiling) {
261
// Jit breakpoints are quite fast, so let's do them in release too.
262
CheckBreakpoint(GetCompilerPC());
263
264
MIPSOpcode inst = Memory::Read_Opcode_JIT(GetCompilerPC());
265
js.downcountAmount += MIPSGetInstructionCycleEstimate(inst);
266
MIPSCompileOp(inst, this);
267
js.compilerPC += 4;
268
js.numInstructions++;
269
}
270
271
if (js.cancel) {
272
// Clear the instructions to signal this was not compiled.
273
ir.Clear();
274
}
275
276
mipsBytes = js.compilerPC - em_address;
277
278
IRWriter simplified;
279
IRWriter *code = &ir;
280
if (!js.hadBreakpoints) {
281
std::vector<IRPassFunc> passes{
282
&ApplyMemoryValidation,
283
&RemoveLoadStoreLeftRight,
284
&OptimizeFPMoves,
285
&PropagateConstants,
286
&PurgeTemps,
287
&ReduceVec4Flush,
288
&OptimizeLoadsAfterStores,
289
// &ReorderLoadStore,
290
// &MergeLoadStore,
291
// &ThreeOpToTwoOp,
292
};
293
294
if (opts.optimizeForInterpreter) {
295
// Add special passes here.
296
passes.push_back(&OptimizeForInterpreter);
297
}
298
if (IRApplyPasses(passes.data(), passes.size(), ir, simplified, opts))
299
logBlocks = 1;
300
code = &simplified;
301
//if (ir.GetInstructions().size() >= 24)
302
// logBlocks = 1;
303
}
304
305
if (!mipsTracer.tracing_enabled) {
306
instructions = code->GetInstructions();
307
}
308
else {
309
std::vector<IRInst> block_instructions = code->GetInstructions();
310
instructions.reserve(block_instructions.capacity());
311
// The first instruction is "Downcount"
312
instructions.push_back(block_instructions.front());
313
instructions.push_back({ IROp::LogIRBlock, 0, 0, 0, 0 });
314
std::copy(block_instructions.begin() + 1, block_instructions.end(), std::back_inserter(instructions));
315
}
316
317
if (logBlocks > 0 && dontLogBlocks == 0) {
318
char temp2[256];
319
NOTICE_LOG(Log::JIT, "=============== mips %08x ===============", em_address);
320
for (u32 cpc = em_address; cpc != GetCompilerPC(); cpc += 4) {
321
temp2[0] = 0;
322
MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp2, sizeof(temp2), true);
323
NOTICE_LOG(Log::JIT, "M: %08x %s", cpc, temp2);
324
}
325
}
326
327
if (logBlocks > 0 && dontLogBlocks == 0) {
328
NOTICE_LOG(Log::JIT, "=============== Original IR (%d instructions) ===============", (int)ir.GetInstructions().size());
329
for (size_t i = 0; i < ir.GetInstructions().size(); i++) {
330
char buf[256];
331
DisassembleIR(buf, sizeof(buf), ir.GetInstructions()[i]);
332
NOTICE_LOG(Log::JIT, "%s", buf);
333
}
334
NOTICE_LOG(Log::JIT, "=============== end =================");
335
}
336
337
if (logBlocks > 0 && dontLogBlocks == 0) {
338
NOTICE_LOG(Log::JIT, "=============== IR (%d instructions) ===============", (int)code->GetInstructions().size());
339
for (size_t i = 0; i < code->GetInstructions().size(); i++) {
340
char buf[256];
341
DisassembleIR(buf, sizeof(buf), code->GetInstructions()[i]);
342
NOTICE_LOG(Log::JIT, "%s", buf);
343
}
344
NOTICE_LOG(Log::JIT, "=============== end =================");
345
}
346
347
if (logBlocks > 0)
348
logBlocks--;
349
if (dontLogBlocks > 0)
350
dontLogBlocks--;
351
}
352
353
void IRFrontend::Comp_RunBlock(MIPSOpcode op) {
354
// This shouldn't be necessary, the dispatcher should catch us before we get here.
355
ERROR_LOG(Log::JIT, "Comp_RunBlock should never be reached!");
356
}
357
358
void IRFrontend::CheckBreakpoint(u32 addr) {
359
if (CBreakPoints::IsAddressBreakPoint(addr)) {
360
FlushAll();
361
362
// Can't skip this even at the start of a block, might impact block linking.
363
ir.Write(IROp::SetPCConst, 0, ir.AddConstant(GetCompilerPC()));
364
365
RestoreRoundingMode();
366
// At this point, downcount HAS the delay slot, but not the instruction itself.
367
int downcountOffset = 0;
368
if (js.inDelaySlot) {
369
MIPSOpcode branchOp = Memory::Read_Opcode_JIT(GetCompilerPC());
370
MIPSOpcode delayOp = Memory::Read_Opcode_JIT(addr);
371
downcountOffset = -MIPSGetInstructionCycleEstimate(delayOp);
372
if ((MIPSGetInfo(branchOp) & LIKELY) != 0) {
373
// Okay, we're in a likely branch. Also negate the branch cycles.
374
downcountOffset += -MIPSGetInstructionCycleEstimate(branchOp);
375
}
376
}
377
int downcountAmount = js.downcountAmount + downcountOffset;
378
if (downcountAmount != 0)
379
ir.Write(IROp::Downcount, 0, ir.AddConstant(downcountAmount));
380
// Note that this means downcount can't be metadata on the block.
381
js.downcountAmount = -downcountOffset;
382
ir.Write(IROp::Breakpoint, 0, ir.AddConstant(addr));
383
ApplyRoundingMode();
384
385
js.hadBreakpoints = true;
386
}
387
}
388
389
void IRFrontend::CheckMemoryBreakpoint(int rs, int offset) {
390
if (CBreakPoints::HasMemChecks()) {
391
FlushAll();
392
393
// Can't skip this even at the start of a block, might impact block linking.
394
ir.Write(IROp::SetPCConst, 0, ir.AddConstant(GetCompilerPC()));
395
396
RestoreRoundingMode();
397
// At this point, downcount HAS the delay slot, but not the instruction itself.
398
int downcountOffset = 0;
399
if (js.inDelaySlot) {
400
// We assume delay slot in compilerPC + 4.
401
MIPSOpcode branchOp = Memory::Read_Opcode_JIT(GetCompilerPC());
402
MIPSOpcode delayOp = Memory::Read_Opcode_JIT(GetCompilerPC() + 4);
403
downcountOffset = -MIPSGetInstructionCycleEstimate(delayOp);
404
if ((MIPSGetInfo(branchOp) & LIKELY) != 0) {
405
// Okay, we're in a likely branch. Also negate the branch cycles.
406
downcountOffset += -MIPSGetInstructionCycleEstimate(branchOp);
407
}
408
}
409
int downcountAmount = js.downcountAmount + downcountOffset;
410
if (downcountAmount != 0)
411
ir.Write(IROp::Downcount, 0, ir.AddConstant(downcountAmount));
412
// Note that this means downcount can't be metadata on the block.
413
js.downcountAmount = -downcountOffset;
414
ir.Write(IROp::MemoryCheck, js.inDelaySlot ? 4 : 0, rs, ir.AddConstant(offset));
415
ApplyRoundingMode();
416
417
js.hadBreakpoints = true;
418
}
419
}
420
421
} // namespace
422
423