-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathIRInst.h
422 lines (357 loc) · 7.92 KB
/
IRInst.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#pragma once
#include <cstdint>
#include <vector>
#include <utility>
#include "Common/CommonTypes.h"
#include "Core/MIPS/MIPS.h"
// Basic IR
//
// This IR refers implicitly to the MIPS register set and is simple to interpret.
// To do real compiler things with it and do full-function compilation, it probably
// needs to be lifted to a higher IR first, before being lowered onto each target.
// But this gets rid of a lot of MIPS idiosyncrasies that makes it tricky, like
// delay slots, and is very suitable for translation into other IRs. Can of course
// even be directly JIT-ed, but the gains will probably be tiny over our older direct
// MIPS->target JITs.
// Ops beginning with "OI" are specialized for IR Interpreter use. These will not be produced
// for the IR JITs.
enum class IROp : uint8_t {
SetConst,
SetConstF,
Mov,
Add,
Sub,
Neg,
Not,
And,
Or,
Xor,
AddConst,
OptAddConst,
SubConst,
AndConst,
OrConst,
XorConst,
OptAndConst,
OptOrConst,
Shl,
Shr,
Sar,
Ror,
// The shift is stored directly, not in the const table, so Imm instead of Const
ShlImm,
ShrImm,
SarImm,
RorImm,
Slt,
SltConst,
SltU,
SltUConst,
Clz,
// Conditional moves
MovZ,
MovNZ,
Max,
Min,
// Byte swaps. All CPUs have native ones so worth keeping.
BSwap16, // Swaps both the high and low byte pairs.
BSwap32,
// Weird Hi/Lo semantics preserved. Too annoying to do something more generic.
MtLo,
MtHi,
MfLo,
MfHi,
Mult,
MultU,
Madd,
MaddU,
Msub,
MsubU,
Div,
DivU,
// These take a constant from the pool as an offset.
// Loads from a constant address can be represented by using r0.
Load8,
Load8Ext,
Load16,
Load16Ext,
Load32,
Load32Left,
Load32Right,
Load32Linked,
LoadFloat,
LoadVec4,
Store8,
Store16,
Store32,
Store32Left,
Store32Right,
Store32Conditional,
StoreFloat,
StoreVec4,
Ext8to32,
Ext16to32,
ReverseBits,
FAdd,
FSub,
FMul,
FDiv,
FMin,
FMax,
FMov,
FSqrt,
FNeg,
FAbs,
FSign,
FRound,
FTrunc,
FCeil,
FFloor,
FCvtWS,
FCvtSW,
FCvtScaledWS,
FCvtScaledSW,
FMovFromGPR,
OptFCvtSWFromGPR,
FMovToGPR,
OptFMovToGPRShr8,
FSat0_1,
FSatMinus1_1,
FpCondFromReg,
FpCondToReg,
FpCtrlFromReg,
FpCtrlToReg,
VfpuCtrlToReg,
FCmp,
FCmovVfpuCC,
FCmpVfpuBit,
FCmpVfpuAggregate,
// Rounding Mode
RestoreRoundingMode,
ApplyRoundingMode,
UpdateRoundingMode,
SetCtrlVFPU,
SetCtrlVFPUReg,
SetCtrlVFPUFReg,
// 4-wide instructions to assist SIMD.
// Can of course add a pass to break them up if a target does not
// support SIMD.
Vec4Init,
Vec4Shuffle,
Vec4Blend,
Vec4Mov,
Vec4Add,
Vec4Sub,
Vec4Mul,
Vec4Div,
Vec4Scale,
Vec4Dot,
Vec4Neg,
Vec4Abs,
// vx2i
Vec2Unpack16To31, // Note that the result is shifted down by 1, hence 31
Vec2Unpack16To32,
Vec4Unpack8To32,
Vec4DuplicateUpperBitsAndShift1, // Bizarro vuc2i behaviour, in an instruction. Split?
Vec4ClampToZero,
Vec2ClampToZero,
Vec4Pack31To8,
Vec4Pack32To8,
Vec2Pack31To16,
Vec2Pack32To16,
// Slow special functions. Used on singles.
FSin,
FCos,
FRSqrt,
FRecip,
FAsin,
// Fake/System instructions
Interpret,
// Emit this before you exit. Semantic is to set the downcount
// that will be used at the actual exit.
Downcount, // src1 + (src2<<8)
// End-of-basic-block.
ExitToConst, // 0, const, downcount
ExitToReg,
ExitToConstIfEq, // const, reg1, reg2
ExitToConstIfNeq, // const, reg1, reg2
ExitToConstIfGtZ, // const, reg1, 0
ExitToConstIfGeZ, // const, reg1, 0
ExitToConstIfLtZ, // const, reg1, 0
ExitToConstIfLeZ, // const, reg1, 0
ExitToConstIfFpTrue,
ExitToConstIfFpFalse,
ExitToPC, // Used after a syscall to give us a way to do things before returning.
Syscall,
SetPC, // hack to make syscall returns work
SetPCConst, // hack to make replacement know PC
CallReplacement,
Break,
// Debugging breakpoints.
Breakpoint,
MemoryCheck,
ValidateAddress8,
ValidateAddress16,
ValidateAddress32,
ValidateAddress128,
Nop,
};
enum IRComparison {
Greater,
GreaterEqual,
Less,
LessEqual,
Equal,
NotEqual,
Bad,
};
// Some common vec4 constants.
enum class Vec4Init {
AllZERO,
AllONE,
AllMinusONE,
Set_1000,
Set_0100,
Set_0010,
Set_0001,
};
enum class IRRoundMode : uint8_t {
RINT_0 = 0,
CAST_1 = 1,
CEIL_2 = 2,
FLOOR_3 = 3,
};
// Hm, unused
inline IRComparison Invert(IRComparison comp) {
switch (comp) {
case IRComparison::Equal: return IRComparison::NotEqual;
case IRComparison::NotEqual: return IRComparison::Equal;
case IRComparison::Greater: return IRComparison::LessEqual;
case IRComparison::GreaterEqual: return IRComparison::Less;
case IRComparison::Less: return IRComparison::GreaterEqual;
case IRComparison::LessEqual: return IRComparison::Greater;
default:
return IRComparison::Bad;
}
}
inline IROp ComparisonToExit(IRComparison comp) {
switch (comp) {
case IRComparison::Equal: return IROp::ExitToConstIfEq;
case IRComparison::NotEqual: return IROp::ExitToConstIfNeq;
case IRComparison::Greater: return IROp::ExitToConstIfGtZ;
case IRComparison::GreaterEqual: return IROp::ExitToConstIfGeZ;
case IRComparison::Less: return IROp::ExitToConstIfLtZ;
case IRComparison::LessEqual: return IROp::ExitToConstIfLeZ;
default:
return IROp::Break;
}
}
enum IRFpCompareMode {
False = 0,
EitherUnordered,
EqualOrdered, // eq, seq (equal, ordered)
EqualUnordered, // ueq, ngl (equal, unordered)
LessOrdered, // olt, lt (less than, ordered)
LessUnordered, // ult, nge (less than, unordered)
LessEqualOrdered, // ole, le (less equal, ordered)
LessEqualUnordered, // ule, ngt (less equal, unordered)
};
typedef u8 IRReg;
enum : IRReg {
IRTEMP_0 = 192,
IRTEMP_1,
IRTEMP_2,
IRTEMP_3,
IRTEMP_LHS, // Reserved for use in branches
IRTEMP_RHS, // Reserved for use in branches
IRTEMP_LR_ADDR, // Reserved for left/right loads and stores.
IRTEMP_LR_VALUE, // Reserved for left/right loads and stores.
IRTEMP_LR_MASK, // Reserved for left/right loads and stores.
IRTEMP_LR_SHIFT, // Reserved for left/right loads and stores.
IRVTEMP_PFX_S = 224 - 32, // Relative to the FP regs
IRVTEMP_PFX_T = 228 - 32,
IRVTEMP_PFX_D = 232 - 32,
IRVTEMP_0 = 236 - 32,
// Hacky way to get to other state
IRREG_VFPU_CTRL_BASE = 208,
IRREG_VFPU_CC = 211,
IRREG_LO = 242, // offset of lo in MIPSState / 4
IRREG_HI = 243,
IRREG_FCR31 = 244,
IRREG_FPCOND = 245,
IRREG_LLBIT = 250,
};
enum IRFlags {
// Uses src3, not dest.
IRFLAG_SRC3 = 0x0001,
// Uses src3 AND dest (i.e. mutates dest.)
IRFLAG_SRC3DST = 0x0002,
// Exit instruction (maybe conditional.)
IRFLAG_EXIT = 0x0004,
// Instruction like Interpret which may read anything, but not an exit.
IRFLAG_BARRIER = 0x0008,
};
struct IRMeta {
IROp op;
const char *name;
char types[5]; // GGG
u32 flags;
};
// 64 bits.
struct IRInst {
IROp op;
union {
IRReg dest;
IRReg src3;
};
IRReg src1;
IRReg src2;
u32 constant;
};
// Returns the new PC.
u32 IRInterpret(MIPSState *ms, const IRInst *inst);
// Each IR block gets a constant pool.
class IRWriter {
public:
IRWriter &operator =(const IRWriter &w) {
insts_ = w.insts_;
return *this;
}
IRWriter &operator =(IRWriter &&w) {
insts_ = std::move(w.insts_);
return *this;
}
void Write(IROp op, u8 dst = 0, u8 src1 = 0, u8 src2 = 0);
void Write(IROp op, IRReg dst, IRReg src1, IRReg src2, uint32_t c) {
AddConstant(c);
Write(op, dst, src1, src2);
}
void Write(IRInst inst) {
insts_.push_back(inst);
}
void WriteSetConstant(u8 dst, u32 value);
int AddConstant(u32 value);
int AddConstantFloat(float value);
void Reserve(size_t s) {
insts_.reserve(s);
}
void Clear() {
insts_.clear();
}
void ReplaceConstant(size_t instNumber, u32 newConstant);
const std::vector<IRInst> &GetInstructions() const { return insts_; }
private:
std::vector<IRInst> insts_;
u32 nextConst_ = 0;
};
struct IROptions {
uint32_t disableFlags;
bool unalignedLoadStore;
bool unalignedLoadStoreVec4;
bool preferVec4;
bool preferVec4Dot;
bool optimizeForInterpreter;
};
const IRMeta *GetIRMeta(IROp op);
void DisassembleIR(char *buf, size_t bufsize, IRInst inst);
void InitIR();