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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef jit_RegisterAllocator_h
#define jit_RegisterAllocator_h
#include "mozilla/Attributes.h"
#include "mozilla/MathAlgorithms.h"
#include "jit/LIR.h"
#include "jit/MIRGenerator.h"
#include "jit/MIRGraph.h"
// Generic structures and functions for use by register allocators.
namespace js {
namespace jit {
class LIRGenerator;
// Structure for running a liveness analysis on a finished register allocation.
// This analysis can be used for two purposes:
//
// - Check the integrity of the allocation, i.e. that the reads and writes of
// physical values preserve the semantics of the original virtual registers.
//
// - Populate safepoints with live registers, GC thing and value data, to
// streamline the process of prototyping new allocators.
struct AllocationIntegrityState
{
explicit AllocationIntegrityState(LIRGraph& graph)
: graph(graph)
{}
// Record all virtual registers in the graph. This must be called before
// register allocation, to pick up the original LUses.
MOZ_MUST_USE bool record();
// Perform the liveness analysis on the graph, and assert on an invalid
// allocation. This must be called after register allocation, to pick up
// all assigned physical values. If populateSafepoints is specified,
// safepoints will be filled in with liveness information.
MOZ_MUST_USE bool check(bool populateSafepoints);
private:
LIRGraph& graph;
// For all instructions and phis in the graph, keep track of the virtual
// registers for all inputs and outputs of the nodes. These are overwritten
// in place during register allocation. This information is kept on the
// side rather than in the instructions and phis themselves to avoid
// debug-builds-only bloat in the size of the involved structures.
struct InstructionInfo {
Vector<LAllocation, 2, SystemAllocPolicy> inputs;
Vector<LDefinition, 0, SystemAllocPolicy> temps;
Vector<LDefinition, 1, SystemAllocPolicy> outputs;
InstructionInfo()
{ }
InstructionInfo(const InstructionInfo& o)
{
AutoEnterOOMUnsafeRegion oomUnsafe;
if (!inputs.appendAll(o.inputs) ||
!temps.appendAll(o.temps) ||
!outputs.appendAll(o.outputs))
{
oomUnsafe.crash("InstructionInfo::InstructionInfo");
}
}
};
Vector<InstructionInfo, 0, SystemAllocPolicy> instructions;
struct BlockInfo {
Vector<InstructionInfo, 5, SystemAllocPolicy> phis;
BlockInfo() {}
BlockInfo(const BlockInfo& o) {
AutoEnterOOMUnsafeRegion oomUnsafe;
if (!phis.appendAll(o.phis))
oomUnsafe.crash("BlockInfo::BlockInfo");
}
};
Vector<BlockInfo, 0, SystemAllocPolicy> blocks;
Vector<LDefinition*, 20, SystemAllocPolicy> virtualRegisters;
// Describes a correspondence that should hold at the end of a block.
// The value which was written to vreg in the original LIR should be
// physically stored in alloc after the register allocation.
struct IntegrityItem
{
LBlock* block;
uint32_t vreg;
LAllocation alloc;
// Order of insertion into seen, for sorting.
uint32_t index;
typedef IntegrityItem Lookup;
static HashNumber hash(const IntegrityItem& item) {
HashNumber hash = item.alloc.hash();
hash = mozilla::RotateLeft(hash, 4) ^ item.vreg;
hash = mozilla::RotateLeft(hash, 4) ^ HashNumber(item.block->mir()->id());
return hash;
}
static bool match(const IntegrityItem& one, const IntegrityItem& two) {
return one.block == two.block
&& one.vreg == two.vreg
&& one.alloc == two.alloc;
}
};
// Items still to be processed.
Vector<IntegrityItem, 10, SystemAllocPolicy> worklist;
// Set of all items that have already been processed.
typedef HashSet<IntegrityItem, IntegrityItem, SystemAllocPolicy> IntegrityItemSet;
IntegrityItemSet seen;
MOZ_MUST_USE bool checkIntegrity(LBlock* block, LInstruction* ins, uint32_t vreg,
LAllocation alloc, bool populateSafepoints);
MOZ_MUST_USE bool checkSafepointAllocation(LInstruction* ins, uint32_t vreg, LAllocation alloc,
bool populateSafepoints);
MOZ_MUST_USE bool addPredecessor(LBlock* block, uint32_t vreg, LAllocation alloc);
void dump();
};
// Represents with better-than-instruction precision a position in the
// instruction stream.
//
// An issue comes up when performing register allocation as to how to represent
// information such as "this register is only needed for the input of
// this instruction, it can be clobbered in the output". Just having ranges
// of instruction IDs is insufficiently expressive to denote all possibilities.
// This class solves this issue by associating an extra bit with the instruction
// ID which indicates whether the position is the input half or output half of
// an instruction.
class CodePosition
{
private:
constexpr explicit CodePosition(uint32_t bits)
: bits_(bits)
{ }
static const unsigned int INSTRUCTION_SHIFT = 1;
static const unsigned int SUBPOSITION_MASK = 1;
uint32_t bits_;
public:
static const CodePosition MAX;
static const CodePosition MIN;
// This is the half of the instruction this code position represents, as
// described in the huge comment above.
enum SubPosition {
INPUT,
OUTPUT
};
constexpr CodePosition() : bits_(0)
{ }
CodePosition(uint32_t instruction, SubPosition where) {
MOZ_ASSERT(instruction < 0x80000000u);
MOZ_ASSERT(((uint32_t)where & SUBPOSITION_MASK) == (uint32_t)where);
bits_ = (instruction << INSTRUCTION_SHIFT) | (uint32_t)where;
}
uint32_t ins() const {
return bits_ >> INSTRUCTION_SHIFT;
}
uint32_t bits() const {
return bits_;
}
SubPosition subpos() const {
return (SubPosition)(bits_ & SUBPOSITION_MASK);
}
bool operator <(CodePosition other) const {
return bits_ < other.bits_;
}
bool operator <=(CodePosition other) const {
return bits_ <= other.bits_;
}
bool operator !=(CodePosition other) const {
return bits_ != other.bits_;
}
bool operator ==(CodePosition other) const {
return bits_ == other.bits_;
}
bool operator >(CodePosition other) const {
return bits_ > other.bits_;
}
bool operator >=(CodePosition other) const {
return bits_ >= other.bits_;
}
uint32_t operator -(CodePosition other) const {
MOZ_ASSERT(bits_ >= other.bits_);
return bits_ - other.bits_;
}
CodePosition previous() const {
MOZ_ASSERT(*this != MIN);
return CodePosition(bits_ - 1);
}
CodePosition next() const {
MOZ_ASSERT(*this != MAX);
return CodePosition(bits_ + 1);
}
};
// Structure to track all moves inserted next to instructions in a graph.
class InstructionDataMap
{
FixedList<LNode*> insData_;
public:
InstructionDataMap()
: insData_()
{ }
MOZ_MUST_USE bool init(MIRGenerator* gen, uint32_t numInstructions) {
if (!insData_.init(gen->alloc(), numInstructions))
return false;
memset(&insData_[0], 0, sizeof(LNode*) * numInstructions);
return true;
}
LNode*& operator[](CodePosition pos) {
return operator[](pos.ins());
}
LNode* const& operator[](CodePosition pos) const {
return operator[](pos.ins());
}
LNode*& operator[](uint32_t ins) {
return insData_[ins];
}
LNode* const& operator[](uint32_t ins) const {
return insData_[ins];
}
};
// Common superclass for register allocators.
class RegisterAllocator
{
void operator=(const RegisterAllocator&) = delete;
RegisterAllocator(const RegisterAllocator&) = delete;
protected:
// Context
MIRGenerator* mir;
LIRGenerator* lir;
LIRGraph& graph;
// Pool of all registers that should be considered allocateable
AllocatableRegisterSet allRegisters_;
// Computed data
InstructionDataMap insData;
Vector<CodePosition, 12, SystemAllocPolicy> entryPositions;
Vector<CodePosition, 12, SystemAllocPolicy> exitPositions;
RegisterAllocator(MIRGenerator* mir, LIRGenerator* lir, LIRGraph& graph)
: mir(mir),
lir(lir),
graph(graph),
allRegisters_(RegisterSet::All())
{
if (mir->compilingWasm()) {
#if defined(JS_CODEGEN_X64)
allRegisters_.take(AnyRegister(HeapReg));
#elif defined(JS_CODEGEN_ARM) || defined(JS_CODEGEN_MIPS32) || defined(JS_CODEGEN_MIPS64)
allRegisters_.take(AnyRegister(HeapReg));
allRegisters_.take(AnyRegister(GlobalReg));
#elif defined(JS_CODEGEN_ARM64)
allRegisters_.take(AnyRegister(HeapReg));
allRegisters_.take(AnyRegister(HeapLenReg));
allRegisters_.take(AnyRegister(GlobalReg));
#endif
} else {
if (FramePointer != InvalidReg && mir->instrumentedProfiling())
allRegisters_.take(AnyRegister(FramePointer));
}
}
MOZ_MUST_USE bool init();
TempAllocator& alloc() const {
return mir->alloc();
}
CodePosition outputOf(const LNode* ins) const {
return ins->isPhi()
? outputOf(ins->toPhi())
: outputOf(ins->toInstruction());
}
CodePosition outputOf(const LPhi* ins) const {
// All phis in a block write their outputs after all of them have
// read their inputs. Consequently, it doesn't make sense to talk
// about code positions in the middle of a series of phis.
LBlock* block = ins->block();
return CodePosition(block->getPhi(block->numPhis() - 1)->id(), CodePosition::OUTPUT);
}
CodePosition outputOf(const LInstruction* ins) const {
return CodePosition(ins->id(), CodePosition::OUTPUT);
}
CodePosition inputOf(const LNode* ins) const {
return ins->isPhi()
? inputOf(ins->toPhi())
: inputOf(ins->toInstruction());
}
CodePosition inputOf(const LPhi* ins) const {
// All phis in a block read their inputs before any of them write their
// outputs. Consequently, it doesn't make sense to talk about code
// positions in the middle of a series of phis.
return CodePosition(ins->block()->getPhi(0)->id(), CodePosition::INPUT);
}
CodePosition inputOf(const LInstruction* ins) const {
return CodePosition(ins->id(), CodePosition::INPUT);
}
CodePosition entryOf(const LBlock* block) {
return entryPositions[block->mir()->id()];
}
CodePosition exitOf(const LBlock* block) {
return exitPositions[block->mir()->id()];
}
LMoveGroup* getInputMoveGroup(LInstruction* ins);
LMoveGroup* getFixReuseMoveGroup(LInstruction* ins);
LMoveGroup* getMoveGroupAfter(LInstruction* ins);
CodePosition minimalDefEnd(LNode* ins) {
// Compute the shortest interval that captures vregs defined by ins.
// Watch for instructions that are followed by an OSI point.
// If moves are introduced between the instruction and the OSI point then
// safepoint information for the instruction may be incorrect.
while (true) {
LNode* next = insData[ins->id() + 1];
if (!next->isOsiPoint())
break;
ins = next;
}
return outputOf(ins);
}
void dumpInstructions();
};
static inline AnyRegister
GetFixedRegister(const LDefinition* def, const LUse* use)
{
return def->isFloatReg()
? AnyRegister(FloatRegister::FromCode(use->registerCode()))
: AnyRegister(Register::FromCode(use->registerCode()));
}
} // namespace jit
} // namespace js
#endif /* jit_RegisterAllocator_h */
|