Debian Codegraph @

js/src/jit/SimpleAllocator.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef jit_SimpleAllocator_h
8 #define jit_SimpleAllocator_h
9
10 #include "mozilla/Array.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/Maybe.h"
13
14 #include "jit/RegisterAllocator.h"
15 #include "jit/SparseBitSet.h"
16 #include "jit/StackSlotAllocator.h"
17
18 namespace js {
19 namespace jit {
20
21 class SimpleAllocator : protected RegisterAllocator {
22 // Information about a virtual register.
23 class VirtualRegister {
24 // The definition and the id of the LIR instruction that contains it.
25 LDefinition* def_;
26 uint32_t insId_;
27
28 // The id of the LIR instruction that's considered the 'last use' of this
29 // virtual register. After this instruction we free any register or stack
30 // slot allocated for this virtual register and we mark this virtual
31 // register as dead.
32 uint32_t lastUseInsId_;
33
34 // Either UINT32_MAX (meaning no stack slot has been allocated) or the stack
35 // slot offset and width of a slot we allocated for this virtual register.
36 static constexpr uint32_t NoStackSlotAndWidth = UINT32_MAX;
37 uint32_t stackSlotAndWidth_ = NoStackSlotAndWidth;
38
39 // If this virtual register has fixed register uses, we use this register
40 // hint when allocating registers to help avoid moves.
41 // This has three states:
42 // - Nothing: we didn't see any fixed register uses.
43 // - Some(valid AnyRegister): there are fixed register uses and they all use
44 // this register.
45 // - Some(Invalid AnyRegister): there are multiple fixed register uses for
46 // different registers and we gave up on assigning a register hint.
47 mozilla::Maybe<AnyRegister> fixedUseHint_;
48
49 // The index in the allocator's allocatedRegs_ vector, or NoRegisterIndex if
50 // this vreg has no allocated register.
51 //
52 // In some uncommon cases a vreg can have multiple registers assigned to it
53 // and in this case the index is for one of these registers.
54 // See hasMultipleRegsForVreg_.
55 static constexpr size_t NoRegisterIndex = UINT8_MAX;
56 uint8_t registerIndex_ = NoRegisterIndex;
57
58 // Assert registerIndex_ fits in a byte. There must be at most one
59 // AllocatedRegister entry for each AnyRegister value.
60 static_assert(sizeof(AnyRegister) == sizeof(registerIndex_));
61 static_assert(AnyRegister::Invalid == NoRegisterIndex);
62
63 // Whether this virtual register is for a Temp.
64 bool isTemp_ : 1;
65
66 // Whether this virtual register has a stack location. This location is
67 // either a stack slot we allocated (see stackSlotAndWidth_) or this virtual
68 // register came with an assigned stack location (for example for stack
69 // arguments).
70 bool hasStackLocation_ : 1;
71
72 // Whether this virtual register is for a GC type that needs to be added to
73 // safepoints.
74 bool isGCType_ : 1;
75
76 // Whether this virtual register is dead. This is set after allocating
77 // registers for the lastUseInsId_ instruction.
78 bool isDead_ : 1;
79
80 public:
81 VirtualRegister() = default;
82
83 VirtualRegister(VirtualRegister&) = delete;
84 void operator=(VirtualRegister&) = delete;
85
86 void init(LNode* ins, LDefinition* def, bool isTemp) {
87 def_ = def;
88 insId_ = ins->id();
89 lastUseInsId_ = ins->id();
90 isTemp_ = isTemp;
91 hasStackLocation_ = false;
92 isGCType_ = def->isSafepointGCType(ins);
93 isDead_ = false;
94 }
95
96 LDefinition* def() const { return def_; }
97 uint32_t insId() const { return insId_; }
98 uint32_t lastUseInsId() const { return lastUseInsId_; }
99
100 uint8_t registerIndex() const {
101 MOZ_ASSERT(hasRegister());
102 return registerIndex_;
103 }
104 bool hasRegister() const {
105 MOZ_ASSERT(!isDead());
106 return registerIndex_ != NoRegisterIndex;
107 }
108 void setRegisterIndex(uint8_t registerIndex) {
109 MOZ_ASSERT(!isDead());
110 MOZ_ASSERT(registerIndex < NoRegisterIndex);
111 registerIndex_ = registerIndex;
112 }
113 void clearRegisterIndex() {
114 MOZ_ASSERT(!isDead());
115 registerIndex_ = NoRegisterIndex;
116 }
117
118 LAllocation stackLocation() const {
119 MOZ_ASSERT(hasStackLocation());
120 if (hasAllocatedStackSlot()) {
121 return LStackSlot(stackSlot());
122 }
123 MOZ_ASSERT(def()->policy() == LDefinition::FIXED);
124 MOZ_ASSERT(!def()->output()->isAnyRegister());
125 return *def()->output();
126 }
127 void setHasStackLocation() {
128 MOZ_ASSERT(!isDead());
129 MOZ_ASSERT(!isTemp());
130 MOZ_ASSERT(!hasStackLocation_);
131 MOZ_ASSERT(stackSlotAndWidth_ == NoStackSlotAndWidth);
132 hasStackLocation_ = true;
133 }
134
135 bool hasAllocatedStackSlot() const {
136 MOZ_ASSERT(!isDead());
137 MOZ_ASSERT_IF(stackSlotAndWidth_ != NoStackSlotAndWidth,
138 hasStackLocation_);
139 return stackSlotAndWidth_ != NoStackSlotAndWidth;
140 }
141 LStackSlot::SlotAndWidth stackSlot() const {
142 MOZ_ASSERT(hasAllocatedStackSlot());
143 return LStackSlot::SlotAndWidth::fromData(stackSlotAndWidth_);
144 }
145 void setAllocatedStackSlot(LStackSlot::SlotAndWidth slot) {
146 setHasStackLocation();
147 MOZ_ASSERT(stackSlotAndWidth_ == NoStackSlotAndWidth);
148 MOZ_ASSERT(slot.data() != NoStackSlotAndWidth);
149 stackSlotAndWidth_ = slot.data();
150 }
151
152 void markDead() {
153 MOZ_ASSERT(!isDead());
154 isDead_ = true;
155 }
156
157 mozilla::Maybe<AnyRegister> fixedUseHint() const { return fixedUseHint_; }
158 void setFixedUseHint(AnyRegister reg) {
159 fixedUseHint_ = mozilla::Some(reg);
160 }
161
162 bool hasStackLocation() const { return hasStackLocation_; }
163 bool isGCType() const { return isGCType_; }
164 bool isTemp() const { return isTemp_; }
165 bool isDead() const { return isDead_; }
166
167 void updateLastUseId(uint32_t useInsId) {
168 MOZ_ASSERT(!isTemp());
169 MOZ_ASSERT(useInsId > insId_);
170 if (useInsId > lastUseInsId_) {
171 lastUseInsId_ = useInsId;
172 }
173 }
174 };
175
176 // Information about virtual registers.
177 Vector<VirtualRegister, 0, JitAllocPolicy> vregs_;
178
179 // Information about an allocated register.
180 class AllocatedRegister {
181 static constexpr size_t VregBits = 24;
182 static constexpr size_t RegisterBits = 8;
183
184 // The virtual register.
185 uint32_t vregId_ : VregBits;
186
187 // The AnyRegister code. We don't use AnyRegister directly because the
188 // compiler adds additional padding bytes on Windows if we do that.
189 uint32_t reg_ : RegisterBits;
190
191 // Id of the last LIR instruction that used this allocated register. Used
192 // for eviction heuristics.
193 uint32_t lastUsedAtInsId_;
194
195 static_assert(MAX_VIRTUAL_REGISTERS <= (1 << VregBits) - 1);
196 static_assert(sizeof(AnyRegister::Code) * 8 == RegisterBits);
197
198 public:
199 AllocatedRegister(uint32_t vreg, AnyRegister reg, uint32_t lastUsedAtInsId)
200 : vregId_(vreg), reg_(reg.code()), lastUsedAtInsId_(lastUsedAtInsId) {}
201
202 uint32_t vregId() const { return vregId_; }
203 AnyRegister reg() const { return AnyRegister::FromCode(reg_); }
204 uint32_t lastUsedAtInsId() const { return lastUsedAtInsId_; }
205 void setLastUsedAtInsId(uint32_t insId) {
206 MOZ_ASSERT(insId >= lastUsedAtInsId_);
207 lastUsedAtInsId_ = insId;
208 }
209 };
210 static_assert(sizeof(AllocatedRegister) == 2 * sizeof(uint32_t),
211 "AllocatedRegister should not have unnecessary padding");
212
213 // Information about allocated physical registers.
214 using AllocatedRegisterVector =
215 Vector<AllocatedRegister, 16, BackgroundSystemAllocPolicy>;
216 AllocatedRegisterVector allocatedRegs_;
217 AllocatableRegisterSet availableRegs_;
218
219 // Whether there might be multiple entries in allocatedRegs_ for the same
220 // vreg. This can happen for instance if an instruction has multiple fixed
221 // register uses for the same vreg.
222 //
223 // This is uncommon and we try to avoid it, but if it does happen we fix
224 // it up at the end of allocateForInstruction.
225 bool hasMultipleRegsForVreg_ = false;
226
227 // All registers allocated for the current LIR instruction.
228 AllocatableRegisterSet currentInsRegs_;
229
230 // Like currentInsRegs_, but does not include registers that are only used
231 // for at-start uses.
232 AllocatableRegisterSet currentInsRegsNotAtStart_;
233
234 // Registers required for fixed outputs/temps of the current LIR instruction.
235 AllocatableRegisterSet fixedTempRegs_;
236 AllocatableRegisterSet fixedOutputAndTempRegs_;
237
238 // The set of live GC things at the start of each basic block.
239 using VirtualRegBitSet = SparseBitSet<BackgroundSystemAllocPolicy>;
240 Vector<VirtualRegBitSet, 0, JitAllocPolicy> liveGCIn_;
241
242 // Vector sorted by instructionId in descending order. This is used by
243 // freeDeadVregsAfterInstruction to mark virtual registers as dead after their
244 // last use.
245 struct VregLastUse {
246 uint32_t instructionId;
247 uint32_t vregId;
248 VregLastUse(uint32_t instructionId, uint32_t vregId)
249 : instructionId(instructionId), vregId(vregId) {}
250 };
251 Vector<VregLastUse, 0, JitAllocPolicy> vregLastUses_;
252
253 // Vector used for spilling instruction outputs that are used across blocks.
254 Vector<LDefinition*, 4, BackgroundSystemAllocPolicy> eagerSpillOutputs_;
255
256 // Allocator used to allocate and free stack slots.
257 StackSlotAllocator stackSlotAllocator_;
258
259 // Register state for a small number of previous blocks. Blocks with a single
260 // predecessor block can optionally reuse this state.
261 //
262 // BlockStateLength should be small enough so that iterating over the array is
263 // very cheap. It's currently 4 and this was sufficient for a hit rate of
264 // 88.6% on a very large Wasm module.
265 static constexpr size_t BlockStateLength = 4;
266 struct BlockState {
267 uint32_t blockIndex = UINT32_MAX;
268 AllocatedRegisterVector allocatedRegs;
269 AllocatableRegisterSet availableRegs;
270 };
271 mozilla::Array<BlockState, BlockStateLength> blockStates_;
272 size_t nextBlockStateIndex_ = 0;
273
274 // Data used to insert a move for the input of a MUST_REUSE_INPUT definition.
275 struct ReusedInputReg {
276 LAllocation* source;
277 AnyRegister dest;
278 LDefinition::Type type;
279 ReusedInputReg(LAllocation* source, AnyRegister dest,
280 LDefinition::Type type)
281 : source(source), dest(dest), type(type) {}
282 };
283 Vector<ReusedInputReg, 4, BackgroundSystemAllocPolicy> reusedInputs_;
284
285 enum class AllocationKind { UseAtStart, Output, UseOrTemp };
286
287 [[nodiscard]] bool addAllocatedReg(LInstruction* ins, uint32_t vregId,
288 bool usedAtStart, AnyRegister reg) {
289 currentInsRegs_.addUnchecked(reg);
290 if (!usedAtStart) {
291 currentInsRegsNotAtStart_.addUnchecked(reg);
292 }
293 availableRegs_.take(reg);
294 MOZ_ASSERT_IF(vregs_[vregId].hasRegister(), hasMultipleRegsForVreg_);
295 vregs_[vregId].setRegisterIndex(allocatedRegs_.length());
296 return allocatedRegs_.emplaceBack(vregId, reg, ins->id());
297 }
298 void markUseOfAllocatedReg(LInstruction* ins, AllocatedRegister& allocated,
299 bool usedAtStart) {
300 MOZ_ASSERT(!availableRegs_.has(allocated.reg()));
301 allocated.setLastUsedAtInsId(ins->id());
302 currentInsRegs_.addUnchecked(allocated.reg());
303 if (!usedAtStart) {
304 currentInsRegsNotAtStart_.addUnchecked(allocated.reg());
305 }
306 }
307
308 [[nodiscard]] bool init();
309 [[nodiscard]] bool analyzeLiveness();
310 [[nodiscard]] bool allocateRegisters();
311
312 void removeAllocatedRegisterAtIndex(size_t index);
313
314 [[nodiscard]] bool tryReuseRegistersFromPredecessor(MBasicBlock* block);
315 void saveAndClearAllocatedRegisters(MBasicBlock* block);
316
317 void freeDeadVregsAfterInstruction(VirtualRegBitSet& liveGC, LNode* ins);
318
319 [[nodiscard]] bool allocateForBlockEnd(LBlock* block, LInstruction* ins);
320
321 bool ensureStackLocation(uint32_t vregId, LAllocation* allocation);
322 LAllocation registerOrStackLocation(LInstruction* ins, uint32_t vregId,
323 bool trackRegUse);
324
325 [[nodiscard]] bool spillRegister(LInstruction* ins,
326 AllocatedRegister allocated);
327
328 [[nodiscard]] bool evictRegister(LInstruction* ins, AnyRegister reg);
329
330 void scanDefinition(LInstruction* ins, LDefinition* def, bool isTemp);
331
332 [[nodiscard]] bool allocateForNonFixedDefOrUse(LInstruction* ins,
333 uint32_t vregId,
334 AllocationKind kind,
335 AnyRegister* reg);
336 [[nodiscard]] bool allocateForFixedUse(LInstruction* ins, const LUse* use,
337 AnyRegister* reg);
338 [[nodiscard]] bool allocateForRegisterUse(LInstruction* ins, const LUse* use,
339 AnyRegister* reg);
340
341 [[nodiscard]] bool allocateForDefinition(uint32_t blockLastId,
342 LInstruction* ins, LDefinition* def,
343 bool isTemp);
344 [[nodiscard]] bool allocateForInstruction(VirtualRegBitSet& liveGC,
345 uint32_t blockLastId,
346 LInstruction* ins);
347
348 [[nodiscard]] bool addLiveRegisterToSafepoint(LSafepoint* safepoint,
349 AllocatedRegister allocated);
350 [[nodiscard]] bool populateSafepoint(VirtualRegBitSet& liveGC,
351 LInstruction* ins,
352 LSafepoint* safepoint);
353
354 void assertValidRegisterStateBeforeInstruction() const;
355
356 public:
357 SimpleAllocator(MIRGenerator* mir, LIRGenerator* lir, LIRGraph& graph)
358 : RegisterAllocator(mir, lir, graph),
359 vregs_(mir->alloc()),
360 liveGCIn_(mir->alloc()),
361 vregLastUses_(mir->alloc()) {}
362
363 [[nodiscard]] bool go();
364 };
365
366 } // namespace jit
367 } // namespace js
368
369 #endif /* jit_SimpleAllocator_h */